summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/.print.c.swpbin0 -> 12288 bytes
-rw-r--r--src/main.c36
-rw-r--r--src/print.c49
-rw-r--r--src/visitor.c19
4 files changed, 66 insertions, 38 deletions
diff --git a/src/.print.c.swp b/src/.print.c.swp
new file mode 100644
index 0000000..ec0e153
--- /dev/null
+++ b/src/.print.c.swp
Binary files differ
diff --git a/src/main.c b/src/main.c
index 3dbbf86..2c192cb 100644
--- a/src/main.c
+++ b/src/main.c
@@ -21,28 +21,6 @@ int main(int argc, char **argv) {
/* t = lexer_collect_next(lexer); */
/* } */
- /* TODO: TEST REPL POSSIBILITY */
- /* printf("Welcome to the NXS REPL.\n"); */
-
- /* char *buf = malloc(2); */
- /* size_t size = 2; */
- /* lexer_t *lexer; */
- /* lexer = init_lexer("a"); */
- /* token_t *t; */
- /* while (true) { */
- /* printf("> "); */
- /* fflush(stdout); */
- /* getline(&buf, &size, stdin); */
- /* strcat(buf, "\0"); */
- /* lexer_reset(lexer, buf); */
- /* t = lexer_collect_next(lexer); */
- /* while (!lexer->finished) { */
- /* printf("%d\t%s\n", t->type, t->value); */
- /* t = lexer_collect_next(lexer); */
- /* printf("lmao\n"); */
- /* } */
- /* } */
-
/* DONE: TEST PARSER, VISITOR, PRINTER (self evaluating types) */
/* lexer_t *lexer = init_lexer("\"hello world\""); */
/* parser_t *parser = init_parser(lexer); */
@@ -108,5 +86,19 @@ int main(int argc, char **argv) {
ast_t *res = root->subnodes[0];
print(res);
+ /* TODO: TEST REPL POSSIBILITY */
+ /* printf("Welcome to the NXS REPL.\n"); */
+
+ /* char *buf = malloc(2); */
+ /* size_t size = 2; */
+ /* lexer_t *lexer; */
+ /* lexer = init_lexer("a"); */
+ /* while (true) { */
+ /* printf("> "); */
+ /* fflush(stdout); */
+ /* getline(&buf, &size, stdin); */
+ /* lexer_reset(lexer, buf); */
+ /* } */
+
return 0;
}
diff --git a/src/print.c b/src/print.c
index 9339549..b2d4f32 100644
--- a/src/print.c
+++ b/src/print.c
@@ -2,24 +2,59 @@
#include "./include/ast.h"
#include <stdio.h>
-void print_string(ast_t *str) { printf("=> \"%s\"\n", str->string_value); }
+void print_string(ast_t *str) { printf("%s\n", str->string_value); }
-void print_int(ast_t *i) { printf("=> %d\n", i->int_value); }
+void print_int(ast_t *i) { printf("%d\n", i->int_value); }
void print_bool(ast_t *b) {
if (b->bool_value)
- printf("=> T\n");
+ printf("T\n");
else
- printf("=> F\n");
+ printf("F\n");
}
-void print_float(ast_t *f) { printf("=> %f\n", f->float_value); }
+void print_float(ast_t *f) { printf("%f\n", f->float_value); }
-void print_symbol(ast_t *s) { printf("=> %s\n", s->string_value); }
+void print_symbol(ast_t *s) { printf("S: %s\n", s->string_value); }
void print_func(ast_t *f) { print_pair(f->cdr); }
-void print_pair(ast_t *p) {}
+void print_pair(ast_t *p) {
+ printf("(");
+ ast_t *cur = p;
+ bool is_last = false;
+
+ while (cur != NULL && (cur->cdr != NULL && cur->cdr->car != NULL)) {
+ if (cur->cdr == NULL) {
+ is_last = true;
+ }
+ switch (cur->car->type) {
+ case AST_STRING:
+ printf("%s", cur->car->string_value);
+ break;
+ case AST_INT:
+ printf("%d", cur->car->int_value);
+ break;
+ case AST_FLOAT:
+ printf("%d", cur->car->int_value);
+ break;
+ case AST_BOOL:
+ printf("%d", cur->car->int_value);
+ break;
+ case AST_FUNCTION:
+ break;
+ case AST_SYMBOL:
+ printf("%d", cur->car->int_value);
+ break;
+ case AST_PAIR:
+ printf("%d", cur->car->int_value);
+ break;
+ case AST_ROOT:
+ break;
+ }
+ cur = cur->cdr;
+ }
+}
void print(ast_t *res) {
switch (res->type) {
diff --git a/src/visitor.c b/src/visitor.c
index f6008dd..44b89ef 100644
--- a/src/visitor.c
+++ b/src/visitor.c
@@ -194,7 +194,7 @@ ast_t *eval_list(visitor_t *v, ast_t *e) {
ast_t *arg1 = eval_expr(v, args->car);
- if ((arg1->type == AST_PAIR)) {
+ if (arg1->type == AST_PAIR) {
return arg1->car;
} else
eval_error(v, e);
@@ -209,7 +209,7 @@ ast_t *eval_list(visitor_t *v, ast_t *e) {
ast_t *arg1 = eval_expr(v, args->car);
- if ((arg1->type == AST_STRING)) {
+ if (arg1->type == AST_STRING) {
return init_ast_int(strlen(arg1->string_value));
} else
eval_error(v, e);
@@ -219,7 +219,7 @@ ast_t *eval_list(visitor_t *v, ast_t *e) {
ast_t *arg1 = eval_expr(v, args->car);
- if ((arg1->type == AST_BOOL)) {
+ if (arg1->type == AST_BOOL) {
return init_ast_bool(true);
} else
return init_ast_bool(false);
@@ -229,7 +229,7 @@ ast_t *eval_list(visitor_t *v, ast_t *e) {
ast_t *arg1 = eval_expr(v, args->car);
- if ((arg1->type == AST_INT)) {
+ if (arg1->type == AST_INT) {
return init_ast_bool(true);
} else
return init_ast_bool(false);
@@ -239,7 +239,7 @@ ast_t *eval_list(visitor_t *v, ast_t *e) {
ast_t *arg1 = eval_expr(v, args->car);
- if ((arg1->type == AST_FLOAT)) {
+ if (arg1->type == AST_FLOAT) {
return init_ast_bool(true);
} else
return init_ast_bool(false);
@@ -249,7 +249,7 @@ ast_t *eval_list(visitor_t *v, ast_t *e) {
ast_t *arg1 = eval_expr(v, args->car);
- if ((arg1->type == AST_SYMBOL)) {
+ if (arg1->type == AST_SYMBOL) {
return init_ast_bool(true);
} else
return init_ast_bool(false);
@@ -259,7 +259,7 @@ ast_t *eval_list(visitor_t *v, ast_t *e) {
ast_t *arg1 = eval_expr(v, args->car);
- if ((arg1->type == AST_FUNCTION)) {
+ if (arg1->type == AST_FUNCTION) {
return init_ast_bool(true);
} else
return init_ast_bool(false);
@@ -269,7 +269,7 @@ ast_t *eval_list(visitor_t *v, ast_t *e) {
ast_t *arg1 = eval_expr(v, args->car);
- if ((arg1->type == AST_PAIR)) {
+ if (arg1->type == AST_PAIR) {
return init_ast_bool(true);
} else
return init_ast_bool(false);
@@ -279,7 +279,7 @@ ast_t *eval_list(visitor_t *v, ast_t *e) {
ast_t *arg1 = eval_expr(v, args->car);
- if ((arg1->type == AST_STRING)) {
+ if (arg1->type == AST_STRING) {
int a = atoi(arg1->string_value);
return init_ast_int(a);
} else
@@ -436,6 +436,7 @@ ast_t *eval_expr(visitor_t *v, ast_t *e) {
return eval_symbol(v, e);
else {
eval_error(v, e);
+ return NULL;
}
}