summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.c4
-rw-r--r--src/visitor.c71
2 files changed, 68 insertions, 7 deletions
diff --git a/src/main.c b/src/main.c
index 8a3edb5..92e7764 100644
--- a/src/main.c
+++ b/src/main.c
@@ -92,11 +92,11 @@ int main(int argc, char **argv) {
/* TODO: TEST HASH TABLE COLLISIONS */
/* DONE: TEST BUILTIN FUNCTIONS */
- lexer_t *lexer = init_lexer("(+ (+ 3 4) 4)");
+ lexer_t *lexer = init_lexer("(/ (+ 3.0 4.0) 4)");
parser_t *parser = init_parser(lexer);
visitor_t *visitor = init_visitor(parser);
ast_t *root = eval(visitor);
ast_t *res = root->subnodes[0];
- printf("%d\n", res->int_value);
+ printf("%f\n", res->float_value);
return 0;
}
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);
}
}