Updated builtin functions for scope.
This commit is contained in:
parent
5b0950cabb
commit
4a516d8edb
@ -123,7 +123,7 @@ void ast_exc_print(ASTExcData* data, int i) {
|
|||||||
INDENT_END;
|
INDENT_END;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTBIFData* ast_bif_data_init(AST* fn(size_t, AST**)) {
|
ASTBIFData* ast_bif_data_init(AST* fn(size_t, AST**, Scope*)) {
|
||||||
return (ASTBIFData*)fn;
|
return (ASTBIFData*)fn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,11 +5,11 @@
|
|||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
AST* builtin_sum(size_t argc, AST** argv) {
|
AST* builtin_sum(size_t argc, AST** argv, Scope* parent) {
|
||||||
ASTNumData total = 0;
|
ASTNumData total = 0;
|
||||||
|
|
||||||
for (int i = 0; i < argc; i++) {
|
for (int i = 0; i < argc; i++) {
|
||||||
AST* arg = exec_exp(argv[i]);
|
AST* arg = exec_exp(argv[i], parent);
|
||||||
if (arg->type == AST_TYPE_EXC)
|
if (arg->type == AST_TYPE_EXC)
|
||||||
return ast_init(
|
return ast_init(
|
||||||
AST_TYPE_EXC,
|
AST_TYPE_EXC,
|
||||||
@ -27,9 +27,9 @@ AST* builtin_sum(size_t argc, AST** argv) {
|
|||||||
return ast_init(AST_TYPE_NUM, ast_num_data_init(total));
|
return ast_init(AST_TYPE_NUM, ast_num_data_init(total));
|
||||||
}
|
}
|
||||||
|
|
||||||
AST* builtin_sub(size_t argc, AST** argv) {
|
AST* builtin_sub(size_t argc, AST** argv, Scope* parent) {
|
||||||
log_dbg("Got here");
|
log_dbg("Got here");
|
||||||
AST* first = exec_exp(*argv);
|
AST* first = exec_exp(*argv, parent);
|
||||||
if (first->type == AST_TYPE_EXC)
|
if (first->type == AST_TYPE_EXC)
|
||||||
return ast_init(
|
return ast_init(
|
||||||
AST_TYPE_EXC,
|
AST_TYPE_EXC,
|
||||||
@ -44,7 +44,7 @@ AST* builtin_sub(size_t argc, AST** argv) {
|
|||||||
ASTNumData total = *(ASTNumData*)first->data;
|
ASTNumData total = *(ASTNumData*)first->data;
|
||||||
|
|
||||||
for (int i = 1; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
AST* arg = exec_exp(argv[i]);
|
AST* arg = exec_exp(argv[i], parent);
|
||||||
if (arg->type == AST_TYPE_EXC)
|
if (arg->type == AST_TYPE_EXC)
|
||||||
return ast_init(
|
return ast_init(
|
||||||
AST_TYPE_EXC,
|
AST_TYPE_EXC,
|
||||||
@ -62,9 +62,9 @@ AST* builtin_sub(size_t argc, AST** argv) {
|
|||||||
return ast_init(AST_TYPE_NUM, ast_num_data_init(total));
|
return ast_init(AST_TYPE_NUM, ast_num_data_init(total));
|
||||||
}
|
}
|
||||||
|
|
||||||
AST* builtin_mul(size_t argc, AST** argv) {
|
AST* builtin_mul(size_t argc, AST** argv, Scope* parent) {
|
||||||
log_dbg("Got here");
|
log_dbg("Got here");
|
||||||
AST* first = exec_exp(*argv);
|
AST* first = exec_exp(*argv, parent);
|
||||||
if (first->type == AST_TYPE_EXC)
|
if (first->type == AST_TYPE_EXC)
|
||||||
return ast_init(
|
return ast_init(
|
||||||
AST_TYPE_EXC,
|
AST_TYPE_EXC,
|
||||||
@ -79,7 +79,7 @@ AST* builtin_mul(size_t argc, AST** argv) {
|
|||||||
ASTNumData total = *(ASTNumData*)first->data;
|
ASTNumData total = *(ASTNumData*)first->data;
|
||||||
|
|
||||||
for (int i = 1; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
AST* arg = exec_exp(argv[i]);
|
AST* arg = exec_exp(argv[i], parent);
|
||||||
if (arg->type == AST_TYPE_EXC)
|
if (arg->type == AST_TYPE_EXC)
|
||||||
return ast_init(
|
return ast_init(
|
||||||
AST_TYPE_EXC,
|
AST_TYPE_EXC,
|
||||||
@ -97,9 +97,9 @@ AST* builtin_mul(size_t argc, AST** argv) {
|
|||||||
return ast_init(AST_TYPE_NUM, ast_num_data_init(total));
|
return ast_init(AST_TYPE_NUM, ast_num_data_init(total));
|
||||||
}
|
}
|
||||||
|
|
||||||
AST* builtin_div(size_t argc, AST** argv) {
|
AST* builtin_div(size_t argc, AST** argv, Scope* parent) {
|
||||||
log_dbg("Got here");
|
log_dbg("Got here");
|
||||||
AST* first = exec_exp(*argv);
|
AST* first = exec_exp(*argv, parent);
|
||||||
if (first->type == AST_TYPE_EXC)
|
if (first->type == AST_TYPE_EXC)
|
||||||
return ast_init(
|
return ast_init(
|
||||||
AST_TYPE_EXC,
|
AST_TYPE_EXC,
|
||||||
@ -114,7 +114,7 @@ AST* builtin_div(size_t argc, AST** argv) {
|
|||||||
ASTNumData total = *(ASTNumData*)first->data;
|
ASTNumData total = *(ASTNumData*)first->data;
|
||||||
|
|
||||||
for (int i = 1; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
AST* arg = exec_exp(argv[i]);
|
AST* arg = exec_exp(argv[i], parent);
|
||||||
if (arg->type == AST_TYPE_EXC)
|
if (arg->type == AST_TYPE_EXC)
|
||||||
return ast_init(
|
return ast_init(
|
||||||
AST_TYPE_EXC,
|
AST_TYPE_EXC,
|
||||||
|
@ -72,7 +72,7 @@ void ast_exc_print(ASTExcData*, int i);
|
|||||||
typedef AST* (*ASTBIFData)(size_t argc, AST** argv);
|
typedef AST* (*ASTBIFData)(size_t argc, AST** argv);
|
||||||
|
|
||||||
// Create a built-in function.
|
// Create a built-in function.
|
||||||
ASTBIFData* ast_bif_data_init(AST* fn(size_t, AST**));
|
ASTBIFData* ast_bif_data_init(AST* fn(size_t, AST**, Scope*));
|
||||||
|
|
||||||
// There is no `ASTBIFData` destroy function, as function pointers are immortal.
|
// There is no `ASTBIFData` destroy function, as function pointers are immortal.
|
||||||
|
|
||||||
|
@ -4,20 +4,20 @@
|
|||||||
#include "ast.h"
|
#include "ast.h"
|
||||||
|
|
||||||
// Sum some nums.
|
// Sum some nums.
|
||||||
AST* builtin_sum(size_t argc, AST** argv);
|
AST* builtin_sum(size_t argc, AST** argv, Scope* parent);
|
||||||
|
|
||||||
// Subtract nums.
|
// Subtract nums.
|
||||||
AST* builtin_sub(size_t argc, AST** argv);
|
AST* builtin_sub(size_t argc, AST** argv, Scope* parent);
|
||||||
|
|
||||||
// Multiply nums.
|
// Multiply nums.
|
||||||
AST* builtin_mul(size_t argc, AST** argv);
|
AST* builtin_mul(size_t argc, AST** argv, Scope* parent);
|
||||||
|
|
||||||
// Divide nums.
|
// Divide nums.
|
||||||
AST* builtin_div(size_t argc, AST** argv);
|
AST* builtin_div(size_t argc, AST** argv, Scope* parent);
|
||||||
|
|
||||||
struct builtin_data {
|
struct builtin_data {
|
||||||
char* name;
|
char* name;
|
||||||
AST* (*fn)(size_t argc, AST** argv);
|
AST* (*fn)(size_t argc, AST** argv, Scope* parent);
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct builtin_data BUILTIN_FNS[] = {
|
static struct builtin_data BUILTIN_FNS[] = {
|
||||||
|
@ -16,6 +16,6 @@ void scope_destroy(Scope* scope);
|
|||||||
// Destroy the current `Scope` only.
|
// Destroy the current `Scope` only.
|
||||||
void scope_destroy_psv(Scope *scope);
|
void scope_destroy_psv(Scope *scope);
|
||||||
// Insert a key/val pair into the `HTab` of a `Scope`.
|
// Insert a key/val pair into the `HTab` of a `Scope`.
|
||||||
inline void scope_add(Scope* scope, char* key, void* val);
|
void scope_add(Scope* scope, char* key, void* val);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1 +1 @@
|
|||||||
Subproject commit 73237c5d224169c7b4d2ec8321f9ac92e8071708
|
Subproject commit cdf1d0297effc2736ee847e557b4275b4f02310b
|
Loading…
x
Reference in New Issue
Block a user