12 MalVal
*EVAL(MalVal
*ast
, Env
*env
);
15 MalVal
*READ(char prompt
[], char *str
) {
21 line
= _readline(prompt
);
28 if (!str
) { MAL_GC_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
) {
66 if (!ast
|| mal_error
) return NULL
;
67 //g_print("EVAL: %s\n", _pr_str(ast,1));
68 if (ast
->type
!= MAL_LIST
) {
69 return eval_ast(ast
, env
);
71 if (!ast
|| mal_error
) return NULL
;
74 //g_print("EVAL apply list: %s\n", _pr_str(ast,1));
76 if (_count(ast
) == 0) { return ast
; }
77 MalVal
*a0
= _nth(ast
, 0);
78 if ((a0
->type
& MAL_SYMBOL
) &&
79 strcmp("def!", a0
->val
.string
) == 0) {
80 //g_print("eval apply def!\n");
81 MalVal
*a1
= _nth(ast
, 1),
83 MalVal
*res
= EVAL(a2
, env
);
84 if (mal_error
) return NULL
;
85 env_set(env
, a1
, res
);
87 } else if ((a0
->type
& MAL_SYMBOL
) &&
88 strcmp("let*", a0
->val
.string
) == 0) {
89 //g_print("eval apply let*\n");
90 MalVal
*a1
= _nth(ast
, 1),
93 assert_type(a1
, MAL_LIST
|MAL_VECTOR
,
94 "let* bindings must be list or vector");
96 assert((len
% 2) == 0, "odd number of let* bindings forms");
97 Env
*let_env
= new_env(env
, NULL
, NULL
);
98 for(i
=0; i
<len
; i
+=2) {
99 key
= g_array_index(a1
->val
.array
, MalVal
*, i
);
100 val
= g_array_index(a1
->val
.array
, MalVal
*, i
+1);
101 assert_type(key
, MAL_SYMBOL
, "let* bind to non-symbol");
102 env_set(let_env
, key
, EVAL(val
, let_env
));
104 return EVAL(a2
, let_env
);
105 } else if ((a0
->type
& MAL_SYMBOL
) &&
106 strcmp("do", a0
->val
.string
) == 0) {
107 //g_print("eval apply do\n");
108 MalVal
*el
= eval_ast(_rest(ast
), env
);
110 } else if ((a0
->type
& MAL_SYMBOL
) &&
111 strcmp("if", a0
->val
.string
) == 0) {
112 //g_print("eval apply if\n");
113 MalVal
*a1
= _nth(ast
, 1);
114 MalVal
*cond
= EVAL(a1
, env
);
115 if (!cond
|| mal_error
) return NULL
;
116 if (cond
->type
& (MAL_FALSE
|MAL_NIL
)) {
117 // eval false slot form
118 if (ast
->val
.array
->len
> 3) {
119 return EVAL(_nth(ast
, 3), env
);
124 // eval true slot form
125 MalVal
*a2
= _nth(ast
, 2);
126 return EVAL(a2
, env
);
128 } else if ((a0
->type
& MAL_SYMBOL
) &&
129 strcmp("fn*", a0
->val
.string
) == 0) {
130 //g_print("eval apply fn*\n");
131 MalVal
*mf
= malval_new(MAL_FUNCTION_MAL
, NULL
);
132 mf
->val
.func
.evaluator
= EVAL
;
133 mf
->val
.func
.args
= _nth(ast
, 1);
134 mf
->val
.func
.body
= _nth(ast
, 2);
135 mf
->val
.func
.env
= env
;
138 //g_print("eval apply\n");
139 MalVal
*el
= eval_ast(ast
, env
);
140 if (!el
|| mal_error
) { return NULL
; }
141 MalVal
*f
= _first(el
),
143 assert_type(f
, MAL_FUNCTION_C
|MAL_FUNCTION_MAL
,
144 "cannot apply '%s'", _pr_str(f
,1));
145 return _apply(f
, args
);
150 char *PRINT(MalVal
*exp
) {
154 return _pr_str(exp
,1);
160 MalVal
*RE(Env
*env
, char *prompt
, char *str
) {
162 ast
= READ(prompt
, str
);
163 if (!ast
|| mal_error
) return NULL
;
164 exp
= EVAL(ast
, env
);
166 malval_free(ast
); // Free input structure
171 // Setup the initial REPL environment
174 void init_repl_env() {
175 repl_env
= new_env(NULL
, NULL
, NULL
);
177 // core.c: defined using C
179 for(i
=0; i
< (sizeof(core_ns
) / sizeof(core_ns
[0])); i
++) {
181 malval_new_symbol(core_ns
[i
].name
),
182 malval_new_function(core_ns
[i
].func
, core_ns
[i
].arg_cnt
));
185 // core.mal: defined using the language itself
186 RE(repl_env
, "", "(def! not (fn* (a) (if a false true)))");
197 // Set the initial prompt and environment
198 snprintf(prompt
, sizeof(prompt
), "user> ");
203 exp
= RE(repl_env
, prompt
, NULL
);
204 if (mal_error
&& strcmp("EOF", mal_error
->val
.string
) == 0) {
210 fprintf(stderr
, "Error: %s\n", _pr_str(mal_error
,1));
211 malval_free(mal_error
);
215 MAL_GC_FREE(output
); // Free output string
218 //malval_free(exp); // Free evaluated expression