Add function evaluation
This commit is contained in:
37
src/main.c
37
src/main.c
@@ -1,5 +1,5 @@
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "uclisp.h"
|
||||
|
||||
@@ -15,7 +15,7 @@ int main(int argc, const char **argv) {
|
||||
|
||||
ucl_state_put(state, "let", ucl_special_create(ucl_special_let));
|
||||
ucl_state_put(state, "if", ucl_special_create(ucl_special_if));
|
||||
|
||||
ucl_state_put(state, "defun", ucl_special_create(ucl_special_defun));
|
||||
|
||||
ucl_state_put(state, "print", ucl_builtin_create(ucl_builtin_print));
|
||||
ucl_state_put(state, "list", ucl_builtin_create(ucl_builtin_list));
|
||||
@@ -35,13 +35,32 @@ int main(int argc, const char **argv) {
|
||||
ucl_state_put(state, "int-p", ucl_builtin_create(ucl_builtin_int_p));
|
||||
ucl_state_put(state, "list-p", ucl_builtin_create(ucl_builtin_list_p));
|
||||
|
||||
struct ucl_object *sexp = ucl_parse(argv[1]);
|
||||
struct ucl_object *result = ucl_evaluate(state, ucl_car(sexp));
|
||||
if (argc < 2) {
|
||||
while (1) {
|
||||
printf("> ");
|
||||
char *line = NULL;
|
||||
size_t len = 0;
|
||||
ssize_t lineSize = 0;
|
||||
lineSize = getline(&line, &len, stdin);
|
||||
|
||||
if (result->type == UCL_TYPE_ERROR) {
|
||||
printf("%s", result->error);
|
||||
return -1;
|
||||
struct ucl_object *sexp = ucl_parse(line);
|
||||
struct ucl_object *result = ucl_evaluate(state, ucl_car(sexp));
|
||||
ucl_print_obj(result);
|
||||
printf("\n");
|
||||
|
||||
free(line);
|
||||
}
|
||||
} else {
|
||||
struct ucl_object *sexp = ucl_parse(argv[1]);
|
||||
struct ucl_object *result = ucl_evaluate(state, ucl_car(sexp));
|
||||
|
||||
if (result->type == UCL_TYPE_ERROR) {
|
||||
printf("%s", result->error);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// '(let (()) (defun foo (a b) (+ a b)) (foo 1 2))'
|
||||
|
||||
Reference in New Issue
Block a user