12 MalVal
*EVAL(MalVal
*ast
, Env
*env
);
15 MalVal
*READ(char prompt
[], char *str
) {
21 line
= _readline(prompt
);
28 if (!str
) { free(line
); }
33 MalVal
*eval_ast(MalVal
*ast
, Env
*env
) {
34 if (!ast
|| mal_error
) return NULL
;
35 if (ast
->type
== MAL_SYMBOL
) {
36 //g_print("EVAL symbol: %s\n", ast->val.string);
37 return env_get(env
, ast
);
38 } else if ((ast
->type
== MAL_LIST
) || (ast
->type
== MAL_VECTOR
)) {
39 //g_print("EVAL sequential: %s\n", _pr_str(ast,1));
40 MalVal
*el
= _map2((MalVal
*(*)(void*, void*))EVAL
, ast
, env
);
41 if (!el
|| mal_error
) return NULL
;
44 } else if (ast
->type
== MAL_HASH_MAP
) {
45 //g_print("EVAL hash_map: %s\n", _pr_str(ast,1));
48 MalVal
*seq
= malval_new_list(MAL_LIST
,
49 g_array_sized_new(TRUE
, TRUE
, sizeof(MalVal
*),
51 g_hash_table_iter_init (&iter
, ast
->val
.hash_table
);
52 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
53 MalVal
*kname
= malval_new_string((char *)key
);
54 g_array_append_val(seq
->val
.array
, kname
);
55 MalVal
*new_val
= EVAL((MalVal
*)value
, env
);
56 g_array_append_val(seq
->val
.array
, new_val
);
58 return _hash_map(seq
);
60 //g_print("EVAL scalar: %s\n", _pr_str(ast,1));
65 MalVal
*EVAL(MalVal
*ast
, Env
*env
) {
68 if (!ast
|| mal_error
) return NULL
;
69 //g_print("EVAL: %s\n", _pr_str(ast,1));
70 if (ast
->type
!= MAL_LIST
) {
71 return eval_ast(ast
, env
);
73 if (!ast
|| mal_error
) return NULL
;
76 //g_print("EVAL apply list: %s\n", _pr_str(ast,1));
78 if (_count(ast
) == 0) { return ast
; }
79 MalVal
*a0
= _nth(ast
, 0);
80 if ((a0
->type
& MAL_SYMBOL
) &&
81 strcmp("def!", a0
->val
.string
) == 0) {
82 //g_print("eval apply def!\n");
83 MalVal
*a1
= _nth(ast
, 1),
85 MalVal
*res
= EVAL(a2
, env
);
86 if (mal_error
) return NULL
;
87 env_set(env
, a1
, res
);
89 } else if ((a0
->type
& MAL_SYMBOL
) &&
90 strcmp("let*", a0
->val
.string
) == 0) {
91 //g_print("eval apply let*\n");
92 MalVal
*a1
= _nth(ast
, 1),
95 assert_type(a1
, MAL_LIST
|MAL_VECTOR
,
96 "let* bindings must be list or vector");
98 assert((len
% 2) == 0, "odd number of let* bindings forms");
99 Env
*let_env
= new_env(env
, NULL
, NULL
);
100 for(i
=0; i
<len
; i
+=2) {
101 key
= g_array_index(a1
->val
.array
, MalVal
*, i
);
102 val
= g_array_index(a1
->val
.array
, MalVal
*, i
+1);
103 assert_type(key
, MAL_SYMBOL
, "let* bind to non-symbol");
104 env_set(let_env
, key
, EVAL(val
, let_env
));
109 } else if ((a0
->type
& MAL_SYMBOL
) &&
110 strcmp("do", a0
->val
.string
) == 0) {
111 //g_print("eval apply do\n");
112 eval_ast(_slice(ast
, 1, _count(ast
)-1), env
);
115 } else if ((a0
->type
& MAL_SYMBOL
) &&
116 strcmp("if", a0
->val
.string
) == 0) {
117 //g_print("eval apply if\n");
118 MalVal
*a1
= _nth(ast
, 1);
119 MalVal
*cond
= EVAL(a1
, env
);
120 if (!cond
|| mal_error
) return NULL
;
121 if (cond
->type
& (MAL_FALSE
|MAL_NIL
)) {
122 // eval false slot form
123 if (ast
->val
.array
->len
> 3) {
129 // eval true slot form
133 } else if ((a0
->type
& MAL_SYMBOL
) &&
134 strcmp("fn*", a0
->val
.string
) == 0) {
135 //g_print("eval apply fn*\n");
136 MalVal
*mf
= malval_new(MAL_FUNCTION_MAL
, NULL
);
137 mf
->val
.func
.evaluator
= EVAL
;
138 mf
->val
.func
.args
= _nth(ast
, 1);
139 mf
->val
.func
.body
= _nth(ast
, 2);
140 mf
->val
.func
.env
= env
;
143 //g_print("eval apply\n");
144 MalVal
*el
= eval_ast(ast
, env
);
145 if (!el
|| mal_error
) { return NULL
; }
146 MalVal
*f
= _first(el
),
148 assert_type(f
, MAL_FUNCTION_C
|MAL_FUNCTION_MAL
,
149 "cannot apply '%s'", _pr_str(f
,1));
150 if (f
->type
& MAL_FUNCTION_MAL
) {
151 ast
= f
->val
.func
.body
;
152 env
= new_env(f
->val
.func
.env
, f
->val
.func
.args
, args
);
155 return _apply(f
, args
);
163 char *PRINT(MalVal
*exp
) {
165 fprintf(stderr
, "Error: %s\n", mal_error
->val
.string
);
166 malval_free(mal_error
);
170 return _pr_str(exp
,1);
176 MalVal
*RE(Env
*env
, char *prompt
, char *str
) {
178 ast
= READ(prompt
, str
);
179 if (!ast
|| mal_error
) return NULL
;
180 exp
= EVAL(ast
, env
);
182 malval_free(ast
); // Free input structure
187 // Setup the initial REPL environment
190 void init_repl_env() {
191 repl_env
= new_env(NULL
, NULL
, NULL
);
193 // core.c: defined using C
195 for(i
=0; i
< (sizeof(core_ns
) / sizeof(core_ns
[0])); i
++) {
197 malval_new_symbol(core_ns
[i
].name
),
198 malval_new_function(core_ns
[i
].func
, core_ns
[i
].arg_cnt
));
201 // core.mal: defined using the language itself
202 RE(repl_env
, "", "(def! not (fn* (a) (if a false true)))");
211 // Set the initial prompt and environment
212 snprintf(prompt
, sizeof(prompt
), "user> ");
217 exp
= RE(repl_env
, prompt
, NULL
);
218 if (mal_error
&& strcmp("EOF", mal_error
->val
.string
) == 0) {
224 g_print("%s\n", output
);
225 free(output
); // Free output string
228 //malval_free(exp); // Free evaluated expression