Implement "do" form

This commit is contained in:
Konstantin Nazarov 2024-10-10 18:26:37 +01:00
parent 908df85c63
commit 6b8651145e
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
3 changed files with 16 additions and 0 deletions

View file

@ -867,6 +867,13 @@ Result<Expression> Compiler::compile_let(Context& context, Symbol& op,
return std::move(ex_res);
}
Result<Expression> Compiler::compile_do(Context& context, Symbol& op,
const Value& expr) {
auto rest = TRY(expr.rest());
return compile_body(context, rest);
}
Result<Expression> Compiler::compile_body(Context& context, const Value& expr) {
auto cur = TRY(expr.copy());
@ -975,6 +982,8 @@ Result<Expression> Compiler::compile_list(Context& context, const Value& expr) {
return compile_fn(context, sym, expr);
} else if (TRY(sym.cmp("let")) == 0) {
return compile_let(context, sym, expr);
} else if (TRY(sym.cmp("do")) == 0) {
return compile_do(context, sym, expr);
} else if (TRY(sym.cmp("quote")) == 0) {
return compile_quote(context, sym, expr);
} else if (TRY(sym.cmp("syntax")) == 0) {

View file

@ -60,6 +60,8 @@ class Compiler {
const Value& expr);
Result<Expression> compile_let(Context& context, Symbol& op,
const Value& expr);
Result<Expression> compile_do(Context& context, Symbol& op,
const Value& expr);
Result<Expression> compile_body(Context& context, const Value& expr);
Result<Expression> compile_function_call(Context& context, const Value& expr);

View file

@ -50,3 +50,8 @@
(assert (= (map '(1 2 3) (fn square (x) (* x x)))
'(1 4 9)))
;; "do" form returns the value of last expression
(assert (= (do 1 2 3) 3))