Skip to content

Commit

Permalink
limited ident resolving, only handles expressions in expression state…
Browse files Browse the repository at this point in the history
…ments (not expressions in ifelse or loop statements)
  • Loading branch information
Lokathor committed Dec 12, 2024
1 parent 5d0f1d0 commit 8d289dd
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
48 changes: 45 additions & 3 deletions src/ast/data/expression.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub enum Expression {
NumLit(FileSpanned<StrID>),
Ident(FileSpanned<StrID>),
Expand All @@ -9,6 +9,7 @@ pub enum Expression {
Macro(FileSpanned<StrID>, FileSpanned<Vec<FileSpanned<TokenTree>>>),

I32(FileSpanned<i32>),
StaticLabel(FileSpanned<StrID>),

/// `[x]`
Deref(Box<FileSpanned<Self>>),
Expand Down Expand Up @@ -67,6 +68,7 @@ pub enum Expression {
/// `x = y`
Assign(Box<FileSpanned<Self>>, Box<FileSpanned<Self>>),

#[default]
ExpressionError,
}
impl Expression {
Expand All @@ -86,7 +88,7 @@ impl Expression {
*self = op(take_name, take_args)
}
NumLit(_) | Ident(_) | Register(_) | Bool(_) | I32(_)
| ExpressionError => (),
| StaticLabel(_) | ExpressionError => (),
Deref(file_spanned) | Neg(file_spanned) | Ref(file_spanned)
| Inc(file_spanned) | Dec(file_spanned) => file_spanned.map_macros(op),
Dot(file_spanned, file_spanned1)
Expand Down Expand Up @@ -125,7 +127,7 @@ impl Expression {
*self = op(take_num);
}
Macro(_, _) => (),
NumLit(_) | Ident(_) | Register(_) | Bool(_) | I32(_)
Ident(_) | Register(_) | Bool(_) | I32(_) | StaticLabel(_)
| ExpressionError => (),
Deref(file_spanned) | Neg(file_spanned) | Ref(file_spanned)
| Inc(file_spanned) | Dec(file_spanned) => file_spanned.map_num_lit(op),
Expand All @@ -152,4 +154,44 @@ impl Expression {
}
}
}

/// Maps the closure over any `Macro` atoms within the expression.
pub fn map_ident<F>(&mut self, op: &mut F)
where
F: FnMut(FileSpanned<StrID>) -> Expression,
{
use Expression::*;
match self {
Ident(i) => {
let take_ident = FileSpanned::take(i);
*self = op(take_ident);
}
Macro(_, _) => (),
NumLit(_) | Register(_) | Bool(_) | I32(_) | StaticLabel(_)
| ExpressionError => (),
Deref(file_spanned) | Neg(file_spanned) | Ref(file_spanned)
| Inc(file_spanned) | Dec(file_spanned) => file_spanned.map_ident(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_ident(op);
file_spanned1.map_ident(op);
}
}
}
}
25 changes: 25 additions & 0 deletions src/ast/data/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,28 @@ pub enum Statement {
Return,
StatementError,
}
impl Statement {
pub fn map_expressions<F>(&mut self, op: &mut F)
where
F: FnMut(FileSpanned<Expression>) -> Expression,
{
match self {
Statement::Expression(x) => {
let exp: FileSpanned<Expression> = FileSpanned::take(x);
**x = op(exp);
}
Statement::IfElse(if_else) => {
let exp: FileSpanned<Expression> = FileSpanned::take(&mut if_else.test);
if_else.test._payload = op(exp);
}
Statement::Loop(file_spanned) => todo!(),
Statement::Break(_)
| Statement::Continue(_)
| Statement::Call(_)
| Statement::Return
| Statement::StatementError => {
todo!()
}
}
}
}
19 changes: 19 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,25 @@ impl Ast {
}
}
}

pub fn resolve_identifiers(&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_ident(&mut |i| {
if let Some(x) = self.evaluated_consts.get(&i) {
Expression::I32(FileSpanned::new(*x, i._span))
} else if self.evaluated_statics.contains_key(&i) {
Expression::StaticLabel(i)
} 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 @@ -196,6 +196,7 @@ pub fn do_ast(args: AstArgs) {
ast.run_static_eval();
ast.resolve_size_of_static();
ast.resolve_numeric_literals();
ast.resolve_identifiers();
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 8d289dd

Please sign in to comment.