diff options
author | Preston Pan <preston@nullring.xyz> | 2023-01-04 19:45:38 -0800 |
---|---|---|
committer | Preston Pan <preston@nullring.xyz> | 2023-01-04 19:45:38 -0800 |
commit | c090ab2336d4f2f8536ca47a17f3e689299ea45e (patch) | |
tree | 65af756a24473f33a738398994c065d0f232ad70 /src/visitor.c | |
parent | 94f847851cd5fb81da18564a1b858ff20240d97a (diff) |
math builtins done
Diffstat (limited to 'src/visitor.c')
-rw-r--r-- | src/visitor.c | 71 |
1 files changed, 66 insertions, 5 deletions
diff --git a/src/visitor.c b/src/visitor.c index acafd2e..d5f5a8f 100644 --- a/src/visitor.c +++ b/src/visitor.c @@ -85,8 +85,7 @@ ast_t *eval_symbol(visitor_t *v, ast_t *e) { hash_table_add(v->eval_table, e->string_value, eval); return eval; } else - printf("DEBUG 2\n"); - eval_error(v, e); + eval_error(v, e); } /* Helper function to get the size of an AST linked list; useful for checking @@ -126,10 +125,73 @@ ast_t *eval_list(visitor_t *v, ast_t *e) { return init_ast_float(arg1->float_value + arg2->int_value); } else if ((arg1->type == AST_FLOAT) && (arg2->type) == AST_FLOAT) { return init_ast_float(arg1->float_value + arg2->float_value); - } + } else + eval_error(v, e); + } else if (strcmp(function->string_value, "-") == 0) { + if (cmp != 2) + eval_error(v, e); + + ast_t *arg1 = eval_expr(v, args->car); + ast_t *arg2 = eval_expr(v, args->cdr->car); + + if ((arg1->type == AST_INT) && (arg2->type == AST_INT)) { + return init_ast_int(arg1->int_value - arg2->int_value); + } else if ((arg1->type == AST_INT) && (arg2->type == AST_FLOAT)) { + return init_ast_float(arg1->int_value - arg2->float_value); + } else if ((arg1->type == AST_FLOAT) && (arg2->type == AST_INT)) { + return init_ast_float(arg1->float_value - arg2->int_value); + } else if ((arg1->type == AST_FLOAT) && (arg2->type) == AST_FLOAT) { + return init_ast_float(arg1->float_value - arg2->float_value); + } else + eval_error(v, e); + } else if (strcmp(function->string_value, "*") == 0) { + if (cmp != 2) + eval_error(v, e); + + ast_t *arg1 = eval_expr(v, args->car); + ast_t *arg2 = eval_expr(v, args->cdr->car); + + if ((arg1->type == AST_INT) && (arg2->type == AST_INT)) { + return init_ast_int(arg1->int_value * arg2->int_value); + } else if ((arg1->type == AST_INT) && (arg2->type == AST_FLOAT)) { + return init_ast_float(arg1->int_value * arg2->float_value); + } else if ((arg1->type == AST_FLOAT) && (arg2->type == AST_INT)) { + return init_ast_float(arg1->float_value * arg2->int_value); + } else if ((arg1->type == AST_FLOAT) && (arg2->type) == AST_FLOAT) { + return init_ast_float(arg1->float_value * arg2->float_value); + } else + eval_error(v, e); + } else if (strcmp(function->string_value, "/") == 0) { + if (cmp != 2) + eval_error(v, e); + + ast_t *arg1 = eval_expr(v, args->car); + ast_t *arg2 = eval_expr(v, args->cdr->car); + + if ((arg1->type == AST_INT) && (arg2->type == AST_INT)) { + return init_ast_float(arg1->int_value / arg2->int_value); + } else if ((arg1->type == AST_INT) && (arg2->type == AST_FLOAT)) { + return init_ast_float(arg1->int_value / arg2->float_value); + } else if ((arg1->type == AST_FLOAT) && (arg2->type == AST_INT)) { + return init_ast_float(arg1->float_value / arg2->int_value); + } else if ((arg1->type == AST_FLOAT) && (arg2->type) == AST_FLOAT) { + return init_ast_float(arg1->float_value / arg2->float_value); + } else + eval_error(v, e); + } else if (strcmp(function->string_value, "%") == 0) { + if (cmp != 2) + eval_error(v, e); + + ast_t *arg1 = eval_expr(v, args->car); + ast_t *arg2 = eval_expr(v, args->cdr->car); + + if ((arg1->type == AST_INT) && (arg2->type == AST_INT)) { + return init_ast_int(arg1->int_value * arg2->int_value); + } else + eval_error(v, e); } } - + /* NON BUILT-INS */ /* Checking that the parameters are actually valid */ if (function->type != AST_FUNCTION) eval_error(v, e->car); @@ -167,7 +229,6 @@ ast_t *eval_expr(visitor_t *v, ast_t *e) { else if (e->type == AST_SYMBOL) return eval_symbol(v, e); else { - printf("DEBUG\n"); eval_error(v, e); } } |