11 MalVal
*EVAL(MalVal
*ast
, Env
*env
);
14 MalVal
*READ(char prompt
[], char *str
) {
20 line
= _readline(prompt
);
27 if (!str
) { free(line
); }
32 MalVal
*eval_ast(MalVal
*ast
, Env
*env
) {
33 if (!ast
|| mal_error
) return NULL
;
34 if (ast
->type
== MAL_SYMBOL
) {
35 //g_print("EVAL symbol: %s\n", ast->val.string);
36 return env_get(env
, ast
->val
.string
);
37 } else if ((ast
->type
== MAL_LIST
) || (ast
->type
== MAL_VECTOR
)) {
38 //g_print("EVAL sequential: %s\n", _pr_str(ast,1));
39 MalVal
*el
= _map2((MalVal
*(*)(void*, void*))EVAL
, ast
, env
);
40 if (!el
|| mal_error
) return NULL
;
43 } else if (ast
->type
== MAL_HASH_MAP
) {
44 //g_print("EVAL hash_map: %s\n", _pr_str(ast,1));
47 MalVal
*seq
= malval_new_list(MAL_LIST
,
48 g_array_sized_new(TRUE
, TRUE
, sizeof(MalVal
*),
50 g_hash_table_iter_init (&iter
, ast
->val
.hash_table
);
51 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
52 MalVal
*kname
= malval_new_string((char *)key
);
53 g_array_append_val(seq
->val
.array
, kname
);
54 MalVal
*new_val
= EVAL((MalVal
*)value
, env
);
55 g_array_append_val(seq
->val
.array
, new_val
);
57 return _hash_map(seq
);
59 //g_print("EVAL scalar: %s\n", _pr_str(ast,1));
64 MalVal
*EVAL(MalVal
*ast
, Env
*env
) {
65 if (!ast
|| mal_error
) return NULL
;
66 //g_print("EVAL: %s\n", _pr_str(ast,1));
67 if (ast
->type
!= MAL_LIST
) {
68 return eval_ast(ast
, env
);
70 if (!ast
|| mal_error
) return NULL
;
73 //g_print("EVAL apply list: %s\n", _pr_str(ast,1));
75 if (_count(ast
) == 0) { return ast
; }
76 MalVal
*a0
= _nth(ast
, 0);
77 if ((a0
->type
& MAL_SYMBOL
) &&
78 strcmp("def!", a0
->val
.string
) == 0) {
79 //g_print("eval apply def!\n");
80 MalVal
*a1
= _nth(ast
, 1),
82 MalVal
*res
= EVAL(a2
, env
);
83 env_set(env
, a1
->val
.string
, res
);
85 } else if ((a0
->type
& MAL_SYMBOL
) &&
86 strcmp("let*", a0
->val
.string
) == 0) {
87 //g_print("eval apply let*\n");
88 MalVal
*a1
= _nth(ast
, 1),
91 assert_type(a1
, MAL_LIST
|MAL_VECTOR
,
92 "let* bindings must be list or vector");
94 assert((len
% 2) == 0, "odd number of let* bindings forms");
95 Env
*let_env
= new_env(env
, NULL
, NULL
);
96 for(i
=0; i
<len
; i
+=2) {
97 key
= g_array_index(a1
->val
.array
, MalVal
*, i
);
98 val
= g_array_index(a1
->val
.array
, MalVal
*, i
+1);
99 assert_type(key
, MAL_SYMBOL
, "let* bind to non-symbol");
100 env_set(let_env
, key
->val
.string
, EVAL(val
, let_env
));
102 return EVAL(a2
, let_env
);
103 } else if ((a0
->type
& MAL_SYMBOL
) &&
104 strcmp("do", a0
->val
.string
) == 0) {
105 //g_print("eval apply do\n");
106 MalVal
*el
= eval_ast(_rest(ast
), env
);
108 } else if ((a0
->type
& MAL_SYMBOL
) &&
109 strcmp("if", a0
->val
.string
) == 0) {
110 //g_print("eval apply if\n");
111 MalVal
*a1
= _nth(ast
, 1);
112 MalVal
*cond
= EVAL(a1
, env
);
113 if (!ast
|| mal_error
) return NULL
;
114 if (cond
->type
& (MAL_FALSE
|MAL_NIL
)) {
115 // eval false slot form
116 MalVal
*a3
= _nth(ast
, 3);
118 return EVAL(a3
, env
);
123 // eval true slot form
124 MalVal
*a2
= _nth(ast
, 2);
125 return EVAL(a2
, env
);
127 } else if ((a0
->type
& MAL_SYMBOL
) &&
128 strcmp("fn*", a0
->val
.string
) == 0) {
129 //g_print("eval apply fn*\n");
130 MalVal
*mf
= malval_new(MAL_FUNCTION_MAL
, NULL
);
131 mf
->val
.func
.evaluator
= EVAL
;
132 mf
->val
.func
.args
= _nth(ast
, 1);
133 mf
->val
.func
.body
= _nth(ast
, 2);
134 mf
->val
.func
.env
= env
;
137 //g_print("eval apply\n");
138 MalVal
*el
= eval_ast(ast
, env
);
139 if (!el
|| mal_error
) { return NULL
; }
140 MalVal
*f
= _first(el
),
142 assert_type(f
, MAL_FUNCTION_C
|MAL_FUNCTION_MAL
,
143 "cannot apply '%s'", _pr_str(f
,1));
144 return _apply(f
, args
);
149 char *PRINT(MalVal
*exp
) {
151 fprintf(stderr
, "Error: %s\n", mal_error
->val
.string
);
152 malval_free(mal_error
);
156 return _pr_str(exp
,1);
162 MalVal
*RE(Env
*env
, char *prompt
, char *str
) {
164 ast
= READ(prompt
, str
);
165 if (!ast
|| mal_error
) return NULL
;
166 exp
= EVAL(ast
, env
);
168 malval_free(ast
); // Free input structure
173 // Setup the initial REPL environment
176 void init_repl_env() {
177 repl_env
= new_env(NULL
, NULL
, NULL
);
179 // core.c: defined using C
181 for(i
=0; i
< (sizeof(core_ns
) / sizeof(core_ns
[0])); i
++) {
182 env_set(repl_env
, core_ns
[i
].name
,
183 malval_new_function(core_ns
[i
].func
, core_ns
[i
].arg_cnt
));
186 // core.mal: defined using the language itself
187 RE(repl_env
, "", "(def! not (fn* (a) (if a false true)))");
196 // Set the initial prompt and environment
197 snprintf(prompt
, sizeof(prompt
), "user> ");
202 exp
= RE(repl_env
, prompt
, NULL
);
203 if (mal_error
&& strcmp("EOF", mal_error
->val
.string
) == 0) {
209 g_print("%s\n", output
);
210 free(output
); // Free output string
213 //malval_free(exp); // Free evaluated expression