From fa4b912b888dd8fe4246f458e468c3a40af5ebce Mon Sep 17 00:00:00 2001 From: Konstantin Nazarov Date: Sat, 7 Sep 2024 12:16:55 +0100 Subject: [PATCH] Remove reduntant syntax_first/syntax_rest (use regular first/rest instead) --- src/common.cpp | 45 +++++++++-------------- src/common.hpp | 9 ++--- src/compiler.cpp | 94 ++++++++++++++++++++++++------------------------ 3 files changed, 69 insertions(+), 79 deletions(-) diff --git a/src/common.cpp b/src/common.cpp index 89dec1e..7821582 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -768,36 +768,25 @@ Result syntax_is_nil(const Value& value) { return val.is(); } -Result syntax_first(const Value& value) { - if (!value.is()) return value.first(); - - auto val = TRY(value.to()->expression()); - return val.first(); -} - -Result syntax_second(const Value& value) { - if (!value.is()) return value.second(); - - auto val = TRY(value.to()->expression()); - return val.second(); -} - -Result syntax_third(const Value& value) { - if (!value.is()) return value.third(); - - auto val = TRY(value.to()->expression()); - return val.third(); -} - -Result syntax_rest(const Value& value) { - if (!value.is()) return value.rest(); - - auto val = TRY(value.to()->expression()); - return val.rest(); -} - Result syntax_unwrap(const Value& value) { if (!value.is()) return value.copy(); return TRY(value.to()->expression()); } + +Result Syntax::first() const { + auto val = TRY(expression()); + return val.first(); +} +Result Syntax::second() const { + auto val = TRY(expression()); + return val.second(); +} +Result Syntax::third() const { + auto val = TRY(expression()); + return val.third(); +} +Result Syntax::rest() const { + auto val = TRY(expression()); + return val.rest(); +} diff --git a/src/common.hpp b/src/common.hpp index 4098d02..2de9afc 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -709,6 +709,11 @@ class Syntax : public Object { Result srcloc() const; Result expression() const; + Result first() const final; + Result second() const final; + Result third() const final; + Result rest() const final; + virtual Result copy_value() const final; Result copy() const; @@ -1246,8 +1251,4 @@ Result Array::append(const V& value) const { Result syntax_is_list(const Value& value); Result syntax_is_nil(const Value& value); -Result syntax_first(const Value& value); -Result syntax_second(const Value& value); -Result syntax_third(const Value& value); -Result syntax_rest(const Value& value); Result syntax_unwrap(const Value& value); diff --git a/src/compiler.cpp b/src/compiler.cpp index 1bbf221..1ab1349 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -245,7 +245,7 @@ Result Compiler::compile_expr(Context& context, const Value& expr) { Result Compiler::compile_primop(Context& context, Symbol& op, const Value& expr) { - Value cur = TRY(syntax_rest(expr)); + Value cur = TRY(expr.rest()); Expression ex = TRY(Expression::create()); Oc opcode = Oc::Unknown; @@ -270,7 +270,7 @@ Result Compiler::compile_primop(Context& context, Symbol& op, return ex; } - auto subexpr = TRY(syntax_first(cur)); + auto subexpr = TRY(cur.first()); auto comp = TRY(compile_expr(context, subexpr)); @@ -278,16 +278,16 @@ Result Compiler::compile_primop(Context& context, Symbol& op, uint64_t firstreg = comp.reg; uint64_t reg = firstreg; - cur = TRY(syntax_rest(cur)); + cur = TRY(cur.rest()); while (!cur.is()) { - auto subexpr = TRY(syntax_first(cur)); + auto subexpr = TRY(cur.first()); auto comp = TRY(compile_expr(context, subexpr)); ex.add_code(comp.code); - auto rest = TRY(syntax_rest(cur)); + auto rest = TRY(cur.rest()); uint64_t res = 0; if (rest.is()) @@ -309,7 +309,7 @@ Result Compiler::compile_primop(Context& context, Symbol& op, Result Compiler::compile_comparison(Context& context, Symbol& op, const Value& expr) { - Value cur = TRY(syntax_rest(expr)); + Value cur = TRY(expr.rest()); Expression ex = TRY(Expression::create()); Oc opcode = Oc::Unknown; @@ -346,7 +346,7 @@ Result Compiler::compile_comparison(Context& context, Symbol& op, int64_t false_c = TRY(context.add_const(TRY(Bool::create(false)))); TRY(ex.add_opcode(Oc::Mov, {0, (int64_t)result}, {1, true_c})); - auto subexpr = TRY(syntax_first(cur)); + auto subexpr = TRY(cur.first()); auto comp = TRY(compile_expr(context, subexpr)); @@ -354,16 +354,16 @@ Result Compiler::compile_comparison(Context& context, Symbol& op, uint64_t firstreg = comp.reg; uint64_t reg = firstreg; - cur = TRY(syntax_rest(cur)); + cur = TRY(cur.rest()); while (!cur.is()) { - auto subexpr = TRY(syntax_first(cur)); + auto subexpr = TRY(cur.first()); auto comp = TRY(compile_expr(context, subexpr)); ex.add_code(comp.code); - auto rest = TRY(syntax_rest(cur)); + auto rest = TRY(cur.rest()); TRY(ex.add_opcode(opcode, {0, (int64_t)reg}, {0, (int64_t)comp.reg}, {0, (int64_t)cmp_expected})); @@ -381,25 +381,25 @@ Result Compiler::compile_comparison(Context& context, Symbol& op, Result Compiler::compile_if(Context& context, Symbol& op, const Value& expr) { - Value first = TRY(syntax_rest(expr)); + Value first = TRY(expr.rest()); Expression ex = TRY(Expression::create()); if (!TRY(syntax_is_list(first))) { return ERROR(CompilationError); } - Value second = TRY(syntax_rest(first)); + Value second = TRY(first.rest()); if (!TRY(syntax_is_list(second))) { return ERROR(CompilationError); } - Value third = TRY(syntax_rest(second)); + Value third = TRY(second.rest()); if (!TRY(syntax_is_list(third))) { return ERROR(CompilationError); } - auto condition = TRY(syntax_first(first)); + auto condition = TRY(first.first()); auto condition_comp = TRY(compile_expr(context, condition)); @@ -407,14 +407,14 @@ Result Compiler::compile_if(Context& context, Symbol& op, uint64_t firstreg = condition_comp.reg; uint64_t reg = firstreg; - auto option1 = TRY(syntax_first(second)); + auto option1 = TRY(second.first()); auto option1_comp = TRY(compile_expr(context, option1)); uint64_t option1_reg = option1_comp.reg; context.maxreg = firstreg + 1; - auto option2 = TRY(syntax_first(third)); + auto option2 = TRY(third.first()); auto option2_comp = TRY(compile_expr(context, option2)); int64_t true_const = TRY(context.add_const(TRY(Bool::create(true)))); @@ -440,7 +440,7 @@ Result Compiler::compile_if(Context& context, Symbol& op, Result Compiler::compile_and(Context& context, Symbol& op, const Value& expr) { - Value param = TRY(syntax_rest(expr)); + Value param = TRY(expr.rest()); Expression ex = TRY(Expression::create()); if (!TRY(syntax_is_list(param))) { return ERROR(CompilationError); @@ -459,8 +459,8 @@ Result Compiler::compile_and(Context& context, Symbol& op, return ERROR(CompilationError); } - auto rest = TRY(syntax_rest(param)); - Value param_val = TRY(syntax_first(param)); + auto rest = TRY(param.rest()); + Value param_val = TRY(param.first()); auto param_ex = TRY(compile_expr(context, param_val)); @@ -487,7 +487,7 @@ Result Compiler::compile_and(Context& context, Symbol& op, Result Compiler::compile_or(Context& context, Symbol& op, const Value& expr) { - Value param = TRY(syntax_rest(expr)); + Value param = TRY(expr.rest()); Expression ex = TRY(Expression::create()); if (!TRY(syntax_is_list(param))) { return ERROR(CompilationError); @@ -506,8 +506,8 @@ Result Compiler::compile_or(Context& context, Symbol& op, return ERROR(CompilationError); } - auto rest = TRY(syntax_rest(param)); - Value param_val = TRY(syntax_first(param)); + auto rest = TRY(param.rest()); + Value param_val = TRY(param.first()); auto param_ex = TRY(compile_expr(context, param_val)); @@ -534,13 +534,13 @@ Result Compiler::compile_or(Context& context, Symbol& op, Result Compiler::compile_not(Context& context, Symbol& op, const Value& expr) { - Value first = TRY(syntax_rest(expr)); + Value first = TRY(expr.rest()); Expression ex = TRY(Expression::create()); if (!TRY(syntax_is_list(first))) { return ERROR(CompilationError); } - auto first_expr = TRY(syntax_first(first)); + auto first_expr = TRY(first.first()); uint64_t result = context.alloc_reg(); int64_t true_c = TRY(context.add_const(TRY(Bool::create(true)))); @@ -561,11 +561,11 @@ Result Compiler::compile_not(Context& context, Symbol& op, Result Compiler::compile_def(Context& context, Symbol& op, const Value& expr) { - auto varname = TRY(syntax_second(expr)); + auto varname = TRY(expr.second()); auto varname_unwrapped = TRY(syntax_unwrap(varname)); if (!varname_unwrapped.is()) return ERROR(CompilationError); - auto var_expr = TRY(syntax_third(expr)); + auto var_expr = TRY(expr.third()); uint64_t maxreg = context.maxreg; Expression ex = TRY(Expression::create()); @@ -588,7 +588,7 @@ Result Compiler::compile_fn(Context& context, Symbol& op, Context ctx = TRY(Context::create(context)); ctx.maxreg = 1; // Reserve the slot for function itself - auto first = TRY(syntax_rest(expr)); + auto first = TRY(expr.rest()); if (!TRY(syntax_is_list(first))) { return ERROR(CompilationError); @@ -596,28 +596,28 @@ Result Compiler::compile_fn(Context& context, Symbol& op, Value name = TRY(Nil::create()); - auto maybe_name = TRY(syntax_first(first)); + auto maybe_name = TRY(first.first()); auto maybe_name_unwrapped = TRY(syntax_unwrap(maybe_name)); if (maybe_name_unwrapped.is()) { name = TRY(maybe_name_unwrapped.copy()); ctx.fname = TRY(name.copy()); - first = TRY(syntax_rest(first)); + first = TRY(first.rest()); if (!TRY(syntax_is_list(first))) { return ERROR(CompilationError); } } - auto param = TRY(syntax_first(first)); + auto param = TRY(first.first()); uint64_t arity = 0; while (!TRY(syntax_is_nil(param))) { if (!TRY(syntax_is_list(param))) { return ERROR(CompilationError); } - auto param_name = TRY(syntax_first(param)); + auto param_name = TRY(param.first()); auto param_name_unwrapped = TRY(syntax_unwrap(param_name)); if (!param_name_unwrapped.is()) { @@ -626,11 +626,11 @@ Result Compiler::compile_fn(Context& context, Symbol& op, int64_t reg = TRY(ctx.add_var(param_name_unwrapped)); - param = TRY(syntax_rest(param)); + param = TRY(param.rest()); arity++; } - Value second = TRY(syntax_rest(first)); + Value second = TRY(first.rest()); if (!TRY(syntax_is_list(second))) { return ERROR(CompilationError); @@ -724,7 +724,7 @@ Result Compiler::compile_let(Context& context, Symbol& op, // Save the variable bindings to restore it later Dict saved_vars = TRY(context.variables_dict.copy()); - auto first = TRY(syntax_rest(expr)); + auto first = TRY(expr.rest()); if (!TRY(syntax_is_list(first))) { return ERROR(CompilationError); @@ -732,20 +732,20 @@ Result Compiler::compile_let(Context& context, Symbol& op, uint64_t maxreg = context.maxreg; - auto bindings = TRY(syntax_first(first)); + auto bindings = TRY(first.first()); Expression ex_res = TRY(Expression::create()); while (!bindings.is()) { - auto binding = TRY(syntax_first(bindings)); + auto binding = TRY(bindings.first()); if (!TRY(syntax_is_list(binding))) { return ERROR(CompilationError); } - auto binding_name = TRY(syntax_first(binding)); + auto binding_name = TRY(binding.first()); auto binding_name_unwrapped = TRY(syntax_unwrap(binding_name)); - auto binding_expr = TRY(syntax_second(binding)); + auto binding_expr = TRY(binding.second()); if (!binding_name_unwrapped.is()) return ERROR(CompilationError); @@ -756,10 +756,10 @@ Result Compiler::compile_let(Context& context, Symbol& op, ex_res.add_code(ex.code); - bindings = TRY(syntax_rest(bindings)); + bindings = TRY(bindings.rest()); } - Value second = TRY(syntax_rest(first)); + Value second = TRY(first.rest()); if (!TRY(syntax_is_list(second))) { return ERROR(CompilationError); @@ -788,7 +788,7 @@ Result Compiler::compile_body(Context& context, const Value& expr) { if (!TRY(syntax_is_list(cur))) { return ERROR(CompilationError); } - auto expr_val = TRY(syntax_first(cur)); + auto expr_val = TRY(cur.first()); // debug_print(expr_val); @@ -796,7 +796,7 @@ Result Compiler::compile_body(Context& context, const Value& expr) { TRY(ex_res.add_code(expr.code)); - cur = TRY(syntax_rest(cur)); + cur = TRY(cur.rest()); if (cur.is()) { ex_res.reg = expr.reg; @@ -812,9 +812,9 @@ Result Compiler::compile_function_call(Context& context, const Value& expr) { auto ex = TRY(Expression::create()); - auto first = TRY(syntax_first(expr)); + auto first = TRY(expr.first()); auto first_unwrapped = TRY(syntax_unwrap(first)); - auto param = TRY(syntax_rest(expr)); + auto param = TRY(expr.rest()); bool is_self_call = false; uint64_t firstreg = context.maxreg; @@ -834,12 +834,12 @@ Result Compiler::compile_function_call(Context& context, if (!TRY(syntax_is_list(param))) { return ERROR(CompilationError); } - Value param_val = TRY(syntax_first(param)); + Value param_val = TRY(param.first()); auto param_ex = TRY(compile_expr(context, param_val)); TRY(ex.add_code(param_ex.code)); - param = TRY(syntax_rest(param)); + param = TRY(param.rest()); } if (is_self_call) { @@ -856,7 +856,7 @@ Result Compiler::compile_function_call(Context& context, } Result Compiler::compile_list(Context& context, const Value& expr) { - auto first = TRY(syntax_first(expr)); + auto first = TRY(expr.first()); auto unwrapped = TRY(syntax_unwrap(first)); if (unwrapped.is()) {