Skip to content

Commit

Permalink
refactor: code simplification by unifying Symbol and SymbolKind.
Browse files Browse the repository at this point in the history
  • Loading branch information
plusvic committed Oct 29, 2024
1 parent e44ea52 commit 33d7043
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 153 deletions.
37 changes: 19 additions & 18 deletions lib/src/compiler/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::compiler::{
};
use crate::scanner::RuntimeObjectHandle;
use crate::string_pool::{BStringPool, StringPool};
use crate::symbols::SymbolKind;
use crate::symbols::Symbol;
use crate::types::{Array, Func, Map, Type, TypeValue, Value};
use crate::utils::cast;
use crate::wasm;
Expand Down Expand Up @@ -308,23 +308,24 @@ fn emit_expr(
}

Expr::Ident { symbol } => {
match symbol.kind() {
SymbolKind::Rule(rule_id) => {
match symbol.as_ref() {
Symbol::Rule(rule_id) => {
// Emit code that checks if a rule has matched, leaving
// zero or one at the top of the stack.
emit_check_for_rule_match(ctx, *rule_id, instr);
}
SymbolKind::Var(var) => {
Symbol::Func(func) => {
emit_func_call(ctx, func, instr);
}
Symbol::Var { var, .. } => {
// The symbol represents a variable in WASM memory,
// emit code for loading its value into the stack.
load_var(ctx, instr, *var);
}
SymbolKind::Func(func) => {
emit_func_call(ctx, func, instr);
}
SymbolKind::Field { index, is_root, .. } => {
Symbol::Field { index, is_root, type_value, .. } => {
let index: i32 = (*index).try_into().unwrap();
match symbol.type_value() {

match type_value {
TypeValue::Integer(_) => {
ctx.lookup_list.push((index, *is_root));
emit_lookup_integer(ctx, instr);
Expand Down Expand Up @@ -372,7 +373,7 @@ fn emit_expr(
emit_lookup_object(ctx, instr);
}
}
emit_func_call(ctx, func, instr);
emit_func_call(ctx, &func, instr);
ctx.lookup_list.clear();
}
TypeValue::Regexp(_) => {
Expand Down Expand Up @@ -929,7 +930,7 @@ fn emit_field_access(
// Rust code.
for operand in field_access.operands.iter().dropping_back(1) {
if let Expr::Ident { symbol } = ir.get(*operand) {
if let SymbolKind::Field { index, is_root, .. } = symbol.kind() {
if let Symbol::Field { index, is_root, .. } = symbol.as_ref() {
ctx.lookup_list.push((*index as i32, *is_root));
continue;
}
Expand Down Expand Up @@ -986,7 +987,7 @@ fn emit_pattern_match(
}
// When the pattern ID is not known, the ID is taken from a variable.
Expr::PatternMatchVar { symbol, anchor } => {
if let SymbolKind::Var(var) = symbol.kind() {
if let Symbol::Var { var, .. } = symbol.as_ref() {
load_var(ctx, instr, *var);
// load_var returns an I64, convert it to I32 because
// PatternId is an I32.
Expand Down Expand Up @@ -1038,8 +1039,8 @@ fn emit_pattern_count(
range
}
Expr::PatternCountVar { symbol, range } => {
match symbol.kind() {
SymbolKind::Var(var) => {
match symbol.as_ref() {
Symbol::Var { var, .. } => {
load_var(ctx, instr, *var);
// load_var returns an I64, convert it to I32.
instr.unop(UnaryOp::I32WrapI64);
Expand Down Expand Up @@ -1083,8 +1084,8 @@ fn emit_pattern_offset(
index
}
Expr::PatternOffsetVar { symbol, index } => {
match symbol.kind() {
SymbolKind::Var(var) => {
match symbol.as_ref() {
Symbol::Var { var, .. } => {
load_var(ctx, instr, *var);
// load_var returns an I64, convert it to I32.
instr.unop(UnaryOp::I32WrapI64);
Expand Down Expand Up @@ -1132,8 +1133,8 @@ fn emit_pattern_length(
index
}
Expr::PatternLengthVar { symbol, index } => {
match symbol.kind() {
SymbolKind::Var(var) => {
match symbol.as_ref() {
Symbol::Var { var, .. } => {
load_var(ctx, instr, *var);
// load_var returns an I64, convert it to I32.
instr.unop(UnaryOp::I32WrapI64);
Expand Down
22 changes: 8 additions & 14 deletions lib/src/compiler/ir/ast2ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::errors::CustomError;
use crate::errors::PotentiallySlowLoop;
use crate::modules::BUILTIN_MODULES;
use crate::re;
use crate::symbols::{Symbol, SymbolKind, SymbolLookup, SymbolTable};
use crate::symbols::{Symbol, SymbolLookup, SymbolTable};
use crate::types::{Map, Regexp, Type, TypeValue, Value};

/// How many patterns a rule can have. If a rule has more than this number of
Expand Down Expand Up @@ -645,7 +645,7 @@ fn expr_from_ast(
// If the symbol is a structure field, and it has an ACL, check if
// the conditions imposed in the ACL are met. If the conditions are
// not met an error is raised.
if let SymbolKind::Field { acl: Some(acl), .. } = symbol.kind() {
if let Symbol::Field { acl: Some(ref acl), .. } = symbol {
for entry in acl {
// True if any of the features in the `accept_if` list is
// present in the compiler. If the list is empty it's also
Expand Down Expand Up @@ -1173,10 +1173,10 @@ fn for_of_expr_from_ast(

loop_vars.insert(
"$",
Symbol::new(
TypeValue::Integer(Value::Unknown),
SymbolKind::Var(next_pattern_id),
),
Symbol::Var {
var: next_pattern_id,
type_value: TypeValue::Integer(Value::Unknown),
},
);

ctx.symbol_table.push(Rc::new(loop_vars));
Expand Down Expand Up @@ -1334,10 +1334,7 @@ fn for_in_expr_from_ast(
for (loop_var, type_value) in iter::zip(loop_vars, expected_vars) {
let var = stack_frame.new_var(type_value.ty());
variables.push(var);
symbols.insert(
loop_var.name,
Symbol::new(type_value, SymbolKind::Var(var)),
);
symbols.insert(loop_var.name, Symbol::Var { var, type_value });
}

// Put the loop variables into scope.
Expand Down Expand Up @@ -1380,10 +1377,7 @@ fn with_expr_from_ast(
declarations.push((var, expr));

// Insert the variable into the symbol table.
symbols.insert(
item.identifier.name,
Symbol::new(type_value, SymbolKind::Var(var)),
);
symbols.insert(item.identifier.name, Symbol::Var { var, type_value });
}

// Put the with variables into scope.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/compiler/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1540,7 +1540,7 @@ impl Expr {
| Expr::Shl { .. }
| Expr::Shr { .. } => Type::Integer,

Expr::Ident { symbol, .. } => symbol.type_value().ty(),
Expr::Ident { symbol, .. } => symbol.ty(),
Expr::FieldAccess(field_access) => field_access.type_value.ty(),
Expr::FuncCall(fn_call) => fn_call.type_value.ty(),
Expr::Lookup(lookup) => lookup.type_value.ty(),
Expand Down
8 changes: 4 additions & 4 deletions lib/src/compiler/ir/tests/testdata/1.folding.ir
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ RULE test_5
AND
EQ
FIELD_ACCESS
IDENT Symbol { type_value: struct, kind: Field { index: 0, is_root: true, acl: None } }
IDENT Symbol { type_value: integer(unknown), kind: Field { index: 1, is_root: false, acl: None } }
IDENT Field { index: 0, is_root: true, type_value: struct, acl: None }
IDENT Field { index: 1, is_root: false, type_value: integer(unknown), acl: None }
CONST integer(0)

RULE test_6
ADD
FIELD_ACCESS
IDENT Symbol { type_value: struct, kind: Field { index: 0, is_root: true, acl: None } }
IDENT Symbol { type_value: integer(unknown), kind: Field { index: 1, is_root: false, acl: None } }
IDENT Field { index: 0, is_root: true, type_value: struct, acl: None }
IDENT Field { index: 1, is_root: false, type_value: integer(unknown), acl: None }
CONST integer(1)
CONST integer(2)

Expand Down
8 changes: 4 additions & 4 deletions lib/src/compiler/ir/tests/testdata/1.no-folding.ir
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ RULE test_5
AND
EQ
FIELD_ACCESS
IDENT Symbol { type_value: struct, kind: Field { index: 0, is_root: true, acl: None } }
IDENT Symbol { type_value: integer(unknown), kind: Field { index: 1, is_root: false, acl: None } }
IDENT Field { index: 0, is_root: true, type_value: struct, acl: None }
IDENT Field { index: 1, is_root: false, type_value: integer(unknown), acl: None }
CONST integer(0)
CONST boolean(true)
NOT
Expand All @@ -49,8 +49,8 @@ RULE test_5
RULE test_6
ADD
FIELD_ACCESS
IDENT Symbol { type_value: struct, kind: Field { index: 0, is_root: true, acl: None } }
IDENT Symbol { type_value: integer(unknown), kind: Field { index: 1, is_root: false, acl: None } }
IDENT Field { index: 0, is_root: true, type_value: struct, acl: None }
IDENT Field { index: 1, is_root: false, type_value: integer(unknown), acl: None }
CONST integer(1)
CONST integer(2)

Expand Down
36 changes: 18 additions & 18 deletions lib/src/compiler/ir/tests/testdata/2.folding.ir
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,39 @@ RULE test
PATTERN_COUNT PatternIdx(1)
FOR_IN
FIELD_ACCESS
IDENT Symbol { type_value: struct, kind: Field { index: 0, is_root: true, acl: None } }
IDENT Symbol { type_value: array, kind: Field { index: 19, is_root: false, acl: None } }
IDENT Field { index: 0, is_root: true, type_value: struct, acl: None }
IDENT Field { index: 19, is_root: false, type_value: array, acl: None }
AND
LE
FIELD_ACCESS
IDENT Symbol { type_value: struct, kind: Var(Var { ty: struct, index: 17 }) }
IDENT Symbol { type_value: integer(unknown), kind: Field { index: 0, is_root: false, acl: None } }
IDENT Var { var: Var { ty: struct, index: 17 }, type_value: struct }
IDENT Field { index: 0, is_root: false, type_value: integer(unknown), acl: None }
PATTERN_OFFSET PatternIdx(0) INDEX
IDENT Symbol { type_value: integer(unknown), kind: Var(Var { ty: integer, index: 5 }) }
IDENT Var { var: Var { ty: integer, index: 5 }, type_value: integer(unknown) }
LE
PATTERN_OFFSET PatternIdx(0) INDEX
IDENT Symbol { type_value: integer(unknown), kind: Var(Var { ty: integer, index: 5 }) }
IDENT Var { var: Var { ty: integer, index: 5 }, type_value: integer(unknown) }
ADD
FIELD_ACCESS
IDENT Symbol { type_value: struct, kind: Var(Var { ty: struct, index: 17 }) }
IDENT Symbol { type_value: integer(unknown), kind: Field { index: 0, is_root: false, acl: None } }
IDENT Var { var: Var { ty: struct, index: 17 }, type_value: struct }
IDENT Field { index: 0, is_root: false, type_value: integer(unknown), acl: None }
FIELD_ACCESS
IDENT Symbol { type_value: struct, kind: Var(Var { ty: struct, index: 17 }) }
IDENT Symbol { type_value: integer(unknown), kind: Field { index: 1, is_root: false, acl: None } }
IDENT Var { var: Var { ty: struct, index: 17 }, type_value: struct }
IDENT Field { index: 1, is_root: false, type_value: integer(unknown), acl: None }
LE
FIELD_ACCESS
IDENT Symbol { type_value: struct, kind: Var(Var { ty: struct, index: 17 }) }
IDENT Symbol { type_value: integer(unknown), kind: Field { index: 0, is_root: false, acl: None } }
IDENT Var { var: Var { ty: struct, index: 17 }, type_value: struct }
IDENT Field { index: 0, is_root: false, type_value: integer(unknown), acl: None }
PATTERN_OFFSET PatternIdx(1) INDEX
IDENT Symbol { type_value: integer(unknown), kind: Var(Var { ty: integer, index: 11 }) }
IDENT Var { var: Var { ty: integer, index: 11 }, type_value: integer(unknown) }
LE
PATTERN_OFFSET PatternIdx(1) INDEX
IDENT Symbol { type_value: integer(unknown), kind: Var(Var { ty: integer, index: 11 }) }
IDENT Var { var: Var { ty: integer, index: 11 }, type_value: integer(unknown) }
ADD
FIELD_ACCESS
IDENT Symbol { type_value: struct, kind: Var(Var { ty: struct, index: 17 }) }
IDENT Symbol { type_value: integer(unknown), kind: Field { index: 0, is_root: false, acl: None } }
IDENT Var { var: Var { ty: struct, index: 17 }, type_value: struct }
IDENT Field { index: 0, is_root: false, type_value: integer(unknown), acl: None }
FIELD_ACCESS
IDENT Symbol { type_value: struct, kind: Var(Var { ty: struct, index: 17 }) }
IDENT Symbol { type_value: integer(unknown), kind: Field { index: 1, is_root: false, acl: None } }
IDENT Var { var: Var { ty: struct, index: 17 }, type_value: struct }
IDENT Field { index: 1, is_root: false, type_value: integer(unknown), acl: None }

36 changes: 18 additions & 18 deletions lib/src/compiler/ir/tests/testdata/2.no-folding.ir
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,39 @@ RULE test
PATTERN_COUNT PatternIdx(1)
FOR_IN
FIELD_ACCESS
IDENT Symbol { type_value: struct, kind: Field { index: 0, is_root: true, acl: None } }
IDENT Symbol { type_value: array, kind: Field { index: 19, is_root: false, acl: None } }
IDENT Field { index: 0, is_root: true, type_value: struct, acl: None }
IDENT Field { index: 19, is_root: false, type_value: array, acl: None }
AND
LE
FIELD_ACCESS
IDENT Symbol { type_value: struct, kind: Var(Var { ty: struct, index: 17 }) }
IDENT Symbol { type_value: integer(unknown), kind: Field { index: 0, is_root: false, acl: None } }
IDENT Var { var: Var { ty: struct, index: 17 }, type_value: struct }
IDENT Field { index: 0, is_root: false, type_value: integer(unknown), acl: None }
PATTERN_OFFSET PatternIdx(0) INDEX
IDENT Symbol { type_value: integer(unknown), kind: Var(Var { ty: integer, index: 5 }) }
IDENT Var { var: Var { ty: integer, index: 5 }, type_value: integer(unknown) }
LE
PATTERN_OFFSET PatternIdx(0) INDEX
IDENT Symbol { type_value: integer(unknown), kind: Var(Var { ty: integer, index: 5 }) }
IDENT Var { var: Var { ty: integer, index: 5 }, type_value: integer(unknown) }
ADD
FIELD_ACCESS
IDENT Symbol { type_value: struct, kind: Var(Var { ty: struct, index: 17 }) }
IDENT Symbol { type_value: integer(unknown), kind: Field { index: 0, is_root: false, acl: None } }
IDENT Var { var: Var { ty: struct, index: 17 }, type_value: struct }
IDENT Field { index: 0, is_root: false, type_value: integer(unknown), acl: None }
FIELD_ACCESS
IDENT Symbol { type_value: struct, kind: Var(Var { ty: struct, index: 17 }) }
IDENT Symbol { type_value: integer(unknown), kind: Field { index: 1, is_root: false, acl: None } }
IDENT Var { var: Var { ty: struct, index: 17 }, type_value: struct }
IDENT Field { index: 1, is_root: false, type_value: integer(unknown), acl: None }
LE
FIELD_ACCESS
IDENT Symbol { type_value: struct, kind: Var(Var { ty: struct, index: 17 }) }
IDENT Symbol { type_value: integer(unknown), kind: Field { index: 0, is_root: false, acl: None } }
IDENT Var { var: Var { ty: struct, index: 17 }, type_value: struct }
IDENT Field { index: 0, is_root: false, type_value: integer(unknown), acl: None }
PATTERN_OFFSET PatternIdx(1) INDEX
IDENT Symbol { type_value: integer(unknown), kind: Var(Var { ty: integer, index: 11 }) }
IDENT Var { var: Var { ty: integer, index: 11 }, type_value: integer(unknown) }
LE
PATTERN_OFFSET PatternIdx(1) INDEX
IDENT Symbol { type_value: integer(unknown), kind: Var(Var { ty: integer, index: 11 }) }
IDENT Var { var: Var { ty: integer, index: 11 }, type_value: integer(unknown) }
ADD
FIELD_ACCESS
IDENT Symbol { type_value: struct, kind: Var(Var { ty: struct, index: 17 }) }
IDENT Symbol { type_value: integer(unknown), kind: Field { index: 0, is_root: false, acl: None } }
IDENT Var { var: Var { ty: struct, index: 17 }, type_value: struct }
IDENT Field { index: 0, is_root: false, type_value: integer(unknown), acl: None }
FIELD_ACCESS
IDENT Symbol { type_value: struct, kind: Var(Var { ty: struct, index: 17 }) }
IDENT Symbol { type_value: integer(unknown), kind: Field { index: 1, is_root: false, acl: None } }
IDENT Var { var: Var { ty: struct, index: 17 }, type_value: struct }
IDENT Field { index: 1, is_root: false, type_value: integer(unknown), acl: None }

8 changes: 4 additions & 4 deletions lib/src/compiler/ir/tests/testdata/3.folding.ir
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ RULE test
EQ
FN_CALL
FIELD_ACCESS
IDENT Symbol { type_value: struct, kind: Field { index: 0, is_root: true, acl: None } }
IDENT Symbol { type_value: function, kind: Field { index: 4, is_root: false, acl: None } }
IDENT Field { index: 0, is_root: true, type_value: struct, acl: None }
IDENT Field { index: 4, is_root: false, type_value: function, acl: None }
CONST integer(0)
FILESIZE
CONST string("feba6c919e3797e7778e8f2e85fa033d")
EQ
FN_CALL
FIELD_ACCESS
IDENT Symbol { type_value: struct, kind: Field { index: 0, is_root: true, acl: None } }
IDENT Symbol { type_value: function, kind: Field { index: 4, is_root: false, acl: None } }
IDENT Field { index: 0, is_root: true, type_value: struct, acl: None }
IDENT Field { index: 4, is_root: false, type_value: function, acl: None }
CONST integer(0)
FILESIZE
CONST string("275876e34cf609db118f3d84b799a790")
Expand Down
8 changes: 4 additions & 4 deletions lib/src/compiler/ir/tests/testdata/3.no-folding.ir
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ RULE test
EQ
FN_CALL
FIELD_ACCESS
IDENT Symbol { type_value: struct, kind: Field { index: 0, is_root: true, acl: None } }
IDENT Symbol { type_value: function, kind: Field { index: 4, is_root: false, acl: None } }
IDENT Field { index: 0, is_root: true, type_value: struct, acl: None }
IDENT Field { index: 4, is_root: false, type_value: function, acl: None }
CONST integer(0)
FILESIZE
CONST string("feba6c919e3797e7778e8f2e85fa033d")
EQ
FN_CALL
FIELD_ACCESS
IDENT Symbol { type_value: struct, kind: Field { index: 0, is_root: true, acl: None } }
IDENT Symbol { type_value: function, kind: Field { index: 4, is_root: false, acl: None } }
IDENT Field { index: 0, is_root: true, type_value: struct, acl: None }
IDENT Field { index: 4, is_root: false, type_value: function, acl: None }
CONST integer(0)
FILESIZE
CONST string("275876e34cf609db118f3d84b799a790")
Expand Down
Loading

0 comments on commit 33d7043

Please # to comment.