Skip to content

Commit

Permalink
resolves numeric literals
Browse files Browse the repository at this point in the history
  • Loading branch information
Lokathor committed Dec 10, 2024
1 parent bac0c05 commit 5d0f1d0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/ast/data/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,44 @@ impl Expression {
}
}
}

/// Maps the closure over any `Macro` atoms within the expression.
pub fn map_num_lit<F>(&mut self, op: &mut F)
where
F: FnMut(FileSpanned<StrID>) -> Expression,
{
use Expression::*;
match self {
NumLit(num) => {
let take_num = FileSpanned::take(num);
*self = op(take_num);
}
Macro(_, _) => (),
NumLit(_) | Ident(_) | Register(_) | Bool(_) | I32(_)
| ExpressionError => (),
Deref(file_spanned) | Neg(file_spanned) | Ref(file_spanned)
| Inc(file_spanned) | Dec(file_spanned) => file_spanned.map_num_lit(op),
Dot(file_spanned, file_spanned1)
| Mul(file_spanned, file_spanned1)
| Div(file_spanned, file_spanned1)
| Mod(file_spanned, file_spanned1)
| Add(file_spanned, file_spanned1)
| Sub(file_spanned, file_spanned1)
| ShiftLeft(file_spanned, file_spanned1)
| ShiftRight(file_spanned, file_spanned1)
| BitAnd(file_spanned, file_spanned1)
| BitXor(file_spanned, file_spanned1)
| BitOr(file_spanned, file_spanned1)
| Eq(file_spanned, file_spanned1)
| Ne(file_spanned, file_spanned1)
| Lt(file_spanned, file_spanned1)
| Gt(file_spanned, file_spanned1)
| Le(file_spanned, file_spanned1)
| Ge(file_spanned, file_spanned1)
| Assign(file_spanned, file_spanned1) => {
file_spanned.map_num_lit(op);
file_spanned1.map_num_lit(op);
}
}
}
}
17 changes: 17 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,23 @@ impl Ast {
}
}
}

pub fn resolve_numeric_literals(&mut self) {
for func in self.functions.values_mut() {
for statement in &mut func.statements {
if let Statement::Expression(ex) = &mut statement._payload {
ex.map_num_lit(&mut |num| {
if let Some(x) = num_lit_to_i32(num) {
Expression::I32(FileSpanned::new(x, num._span))
} else {
self.err_bucket.push(todo!());
Expression::ExpressionError
}
});
}
}
}
}
}

fn num_lit_to_i32(n: FileSpanned<StrID>) -> Option<i32> {
Expand Down
1 change: 1 addition & 0 deletions src/bin/yagbas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ pub fn do_ast(args: AstArgs) {
ast.run_const_eval();
ast.run_static_eval();
ast.resolve_size_of_static();
ast.resolve_numeric_literals();
println!("{ast:?}");
err_bucket.append(&mut ast.err_bucket);
report_all_the_errors(src_files, err_bucket, args.message_size);
Expand Down

0 comments on commit 5d0f1d0

Please sign in to comment.