Remove reduntant syntax_first/syntax_rest (use regular first/rest instead)
This commit is contained in:
parent
12509f65df
commit
fa4b912b88
3 changed files with 69 additions and 79 deletions
|
@ -768,36 +768,25 @@ Result<bool> syntax_is_nil(const Value& value) {
|
|||
return val.is<Nil>();
|
||||
}
|
||||
|
||||
Result<Value> syntax_first(const Value& value) {
|
||||
if (!value.is<Syntax>()) return value.first();
|
||||
|
||||
auto val = TRY(value.to<Syntax>()->expression());
|
||||
return val.first();
|
||||
}
|
||||
|
||||
Result<Value> syntax_second(const Value& value) {
|
||||
if (!value.is<Syntax>()) return value.second();
|
||||
|
||||
auto val = TRY(value.to<Syntax>()->expression());
|
||||
return val.second();
|
||||
}
|
||||
|
||||
Result<Value> syntax_third(const Value& value) {
|
||||
if (!value.is<Syntax>()) return value.third();
|
||||
|
||||
auto val = TRY(value.to<Syntax>()->expression());
|
||||
return val.third();
|
||||
}
|
||||
|
||||
Result<Value> syntax_rest(const Value& value) {
|
||||
if (!value.is<Syntax>()) return value.rest();
|
||||
|
||||
auto val = TRY(value.to<Syntax>()->expression());
|
||||
return val.rest();
|
||||
}
|
||||
|
||||
Result<Value> syntax_unwrap(const Value& value) {
|
||||
if (!value.is<Syntax>()) return value.copy();
|
||||
|
||||
return TRY(value.to<Syntax>()->expression());
|
||||
}
|
||||
|
||||
Result<Value> Syntax::first() const {
|
||||
auto val = TRY(expression());
|
||||
return val.first();
|
||||
}
|
||||
Result<Value> Syntax::second() const {
|
||||
auto val = TRY(expression());
|
||||
return val.second();
|
||||
}
|
||||
Result<Value> Syntax::third() const {
|
||||
auto val = TRY(expression());
|
||||
return val.third();
|
||||
}
|
||||
Result<Value> Syntax::rest() const {
|
||||
auto val = TRY(expression());
|
||||
return val.rest();
|
||||
}
|
||||
|
|
|
@ -709,6 +709,11 @@ class Syntax : public Object {
|
|||
Result<Value> srcloc() const;
|
||||
Result<Value> expression() const;
|
||||
|
||||
Result<Value> first() const final;
|
||||
Result<Value> second() const final;
|
||||
Result<Value> third() const final;
|
||||
Result<Value> rest() const final;
|
||||
|
||||
virtual Result<Value> copy_value() const final;
|
||||
Result<Syntax> copy() const;
|
||||
|
||||
|
@ -1246,8 +1251,4 @@ Result<Array> Array::append(const V& value) const {
|
|||
|
||||
Result<bool> syntax_is_list(const Value& value);
|
||||
Result<bool> syntax_is_nil(const Value& value);
|
||||
Result<Value> syntax_first(const Value& value);
|
||||
Result<Value> syntax_second(const Value& value);
|
||||
Result<Value> syntax_third(const Value& value);
|
||||
Result<Value> syntax_rest(const Value& value);
|
||||
Result<Value> syntax_unwrap(const Value& value);
|
||||
|
|
|
@ -245,7 +245,7 @@ Result<Expression> Compiler::compile_expr(Context& context, const Value& expr) {
|
|||
|
||||
Result<Expression> 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<Expression> 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<Expression> 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<Nil>()) {
|
||||
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<Nil>())
|
||||
|
@ -309,7 +309,7 @@ Result<Expression> Compiler::compile_primop(Context& context, Symbol& op,
|
|||
|
||||
Result<Expression> 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<Expression> 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<Expression> 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<Nil>()) {
|
||||
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<Expression> Compiler::compile_comparison(Context& context, Symbol& op,
|
|||
|
||||
Result<Expression> 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<Expression> 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<Expression> Compiler::compile_if(Context& context, Symbol& op,
|
|||
|
||||
Result<Expression> 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<Expression> 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<Expression> Compiler::compile_and(Context& context, Symbol& op,
|
|||
|
||||
Result<Expression> 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<Expression> 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<Expression> Compiler::compile_or(Context& context, Symbol& op,
|
|||
|
||||
Result<Expression> 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<Expression> Compiler::compile_not(Context& context, Symbol& op,
|
|||
|
||||
Result<Expression> 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<Symbol>()) 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<Expression> 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<Expression> 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<Symbol>()) {
|
||||
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<Symbol>()) {
|
||||
|
@ -626,11 +626,11 @@ Result<Expression> 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<Expression> 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<Expression> 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<Nil>()) {
|
||||
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<Symbol>()) return ERROR(CompilationError);
|
||||
|
||||
|
@ -756,10 +756,10 @@ Result<Expression> 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<Expression> 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<Expression> 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<Nil>()) {
|
||||
ex_res.reg = expr.reg;
|
||||
|
@ -812,9 +812,9 @@ Result<Expression> 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<Expression> 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<Expression> Compiler::compile_function_call(Context& context,
|
|||
}
|
||||
|
||||
Result<Expression> 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<Symbol>()) {
|
||||
|
|
Loading…
Reference in a new issue