Skip to content

Commit

Permalink
fully flattens references to static data now.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lokathor committed Dec 16, 2024
1 parent dcbbbc1 commit 7dcf3e5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
56 changes: 49 additions & 7 deletions src/ast/data/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ pub enum Expression {
Macro(FileSpanned<StrID>, FileSpanned<Vec<FileSpanned<TokenTree>>>),

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

/// `[x]`
Deref(Box<FileSpanned<Self>>),
Expand Down Expand Up @@ -88,7 +89,7 @@ impl Expression {
*self = op(take_name, take_args)
}
NumLit(_) | Ident(_) | Register(_) | Bool(_) | I32(_)
| StaticLabel(_) | ExpressionError => (),
| StaticIdent(_) | RefToStatic(_) | 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 All @@ -112,6 +113,7 @@ impl Expression {
file_spanned.map_macros(op);
file_spanned1.map_macros(op);
}
RefToStatic(file_spanned) => todo!(),
}
}

Expand All @@ -127,8 +129,8 @@ impl Expression {
*self = op(take_num);
}
Macro(_, _) => (),
Ident(_) | Register(_) | Bool(_) | I32(_) | StaticLabel(_)
| ExpressionError => (),
Ident(_) | Register(_) | Bool(_) | I32(_) | StaticIdent(_)
| RefToStatic(_) | 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)
Expand All @@ -155,7 +157,7 @@ impl Expression {
}
}

/// Maps the closure over any `Macro` atoms within the expression.
/// Maps the closure over any `Ident` atoms within the expression.
pub fn map_ident<F>(&mut self, op: &mut F)
where
F: FnMut(FileSpanned<StrID>) -> Expression,
Expand All @@ -167,8 +169,8 @@ impl Expression {
*self = op(take_ident);
}
Macro(_, _) => (),
NumLit(_) | Register(_) | Bool(_) | I32(_) | StaticLabel(_)
| ExpressionError => (),
NumLit(_) | Register(_) | Bool(_) | I32(_) | StaticIdent(_)
| RefToStatic(_) | 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)
Expand All @@ -194,4 +196,44 @@ impl Expression {
}
}
}

/// Maps the closure over any `Ref` atoms within the expression.
pub fn map_ref<F>(&mut self, op: &mut F)
where
F: FnMut(FileSpanned<Expression>) -> Expression,
{
use Expression::*;
match self {
Ref(i) => {
let take_ident = FileSpanned::take(i);
*self = op(take_ident);
}
Macro(_, _) => (),
NumLit(_) | Register(_) | Bool(_) | I32(_) | StaticIdent(_)
| Ident(_) | RefToStatic(_) | ExpressionError => (),
Deref(file_spanned) | Neg(file_spanned) | Inc(file_spanned)
| Dec(file_spanned) => file_spanned.map_ref(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_ref(op);
file_spanned1.map_ref(op);
}
}
}
}
16 changes: 15 additions & 1 deletion src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl Ast {
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)
Expression::StaticIdent(i)
} else {
self.err_bucket.push(todo!());
Expression::ExpressionError
Expand All @@ -155,6 +155,20 @@ impl Ast {
});
}
}

pub fn resolve_ref(&mut self) {
for func in self.functions.values_mut() {
func.expressions_mut().for_each(|xpr| {
xpr.map_ref(&mut |xpr| match xpr._payload {
Expression::StaticIdent(i) => Expression::RefToStatic(i),
_ => {
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 @@ -197,6 +197,7 @@ pub fn do_ast(args: AstArgs) {
ast.resolve_size_of_static();
ast.resolve_numeric_literals();
ast.resolve_identifiers();
ast.resolve_ref();
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 7dcf3e5

Please sign in to comment.