Implement "do" form
This commit is contained in:
parent
908df85c63
commit
6b8651145e
3 changed files with 16 additions and 0 deletions
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Reference in a new issue