Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
hlorenzi committed Nov 2, 2024
1 parent 602919d commit 17c5d8e
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 35 deletions.
34 changes: 34 additions & 0 deletions src/asm/decls/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::*;


pub fn collect(
report: &mut diagn::Report,
ast: &mut asm::AstTopLevel,
decls: &mut asm::ItemDecls)
-> Result<(), ()>
{
for any_node in &mut ast.nodes
{
let asm::AstAny::DirectiveMacro(ref mut node) = any_node
else { continue };

if node.item_ref.is_some()
{
continue;
}


let item_ref = decls.ruledefs.declare(
report,
node.header_span,
&util::SymbolContext::new_global(),
decls.ruledefs.generate_anonymous_name(),
0,
util::SymbolKind::Other)?;

node.item_ref = Some(item_ref);
}


Ok(())
}
2 changes: 2 additions & 0 deletions src/asm/decls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::*;
mod bank;
mod bankdef;
mod ruledef;
mod macros;
mod symbol;
mod function;

Expand Down Expand Up @@ -50,6 +51,7 @@ pub fn collect(
bankdef::collect(report, ast, decls)?;
bank::collect(report, ast, decls)?;
ruledef::collect(report, ast, decls)?;
macros::collect(report, ast, decls)?;
symbol::collect(report, ast, decls)?;
function::collect(report, ast, decls)?;

Expand Down
4 changes: 2 additions & 2 deletions src/asm/defs/ruledef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub fn resolve_rule(
let mut exact_parts = 0;
let mut parameters = Vec::<RuleParameter>::new();

for ast_part in &ast_rule.pattern
for ast_part in &ast_rule.pattern.parts
{
let part = {
match &ast_part
Expand Down Expand Up @@ -137,7 +137,7 @@ pub fn resolve_rule(
}

Ok(Rule {
pattern_span: ast_rule.pattern_span,
pattern_span: ast_rule.pattern.span,
pattern,
exact_part_count: exact_parts,
parameters,
Expand Down
3 changes: 3 additions & 0 deletions src/asm/parser/directive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ pub fn parse(
"subruledef" => Ok(asm::AstAny::DirectiveRuledef(
asm::parser::directive_ruledef::parse(report, walker, true, header_span)?)),

"macro" => Ok(asm::AstAny::DirectiveMacro(
asm::parser::directive_ruledef::parse_macro(report, walker, header_span)?)),

"assert" => Ok(asm::AstAny::DirectiveAssert(
asm::parser::directive_assert::parse(report, walker, header_span)?)),

Expand Down
21 changes: 2 additions & 19 deletions src/asm/parser/directive_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn parse(
{
let expr = expr::parse(report, walker)?;

let true_arm = parse_braced_block(report, walker)?;
let true_arm = asm::parser::parse_braced_toplevel(report, walker)?;
let false_arm = parse_else_blocks(report, walker)?;

Ok(AstDirectiveIf {
Expand All @@ -33,23 +33,6 @@ pub fn parse(
}


fn parse_braced_block(
report: &mut diagn::Report,
walker: &mut syntax::Walker)
-> Result<asm::AstTopLevel, ()>
{
walker.expect(report, syntax::TokenKind::BraceOpen)?;

let block = asm::parser::parse_nested_toplevel(
report,
walker)?;

walker.expect(report, syntax::TokenKind::BraceClose)?;

Ok(block)
}


fn parse_else_blocks(
report: &mut diagn::Report,
walker: &mut syntax::Walker)
Expand All @@ -74,7 +57,7 @@ fn parse_else_blocks(
report,
syntax::TokenKind::Identifier)?;

Ok(Some(parse_braced_block(report, walker)?))
Ok(Some(asm::parser::parse_braced_toplevel(report, walker)?))
}
else if directive_name == "elif"
{
Expand Down
86 changes: 73 additions & 13 deletions src/asm/parser/directive_ruledef.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
use crate::*;


#[derive(Clone, Debug)]
pub struct AstDirectiveMacro
{
pub header_span: diagn::Span,
pub pattern: AstMatchPattern,
pub contents: asm::AstTopLevel,

pub item_ref: Option<util::ItemRef::<asm::Ruledef>>,
}


#[derive(Clone, Debug)]
pub struct AstDirectiveRuledef
{
Expand All @@ -14,11 +25,18 @@ pub struct AstDirectiveRuledef
}


#[derive(Clone, Debug)]
pub struct AstMatchPattern
{
pub span: diagn::Span,
pub parts: Vec<AstRulePatternPart>,
}


#[derive(Clone, Debug)]
pub struct AstRule
{
pub pattern_span: diagn::Span,
pub pattern: Vec<AstRulePatternPart>,
pub pattern: AstMatchPattern,
pub expr: expr::Expr,
}

Expand Down Expand Up @@ -97,13 +115,60 @@ pub fn parse(
}


pub fn parse_macro(
report: &mut diagn::Report,
walker: &mut syntax::Walker,
header_span: diagn::Span)
-> Result<AstDirectiveMacro, ()>
{
let pattern = parse_match_pattern(
report,
walker,
false)?;

let contents = asm::parser::parse_braced_toplevel(
report,
walker)?;

walker.expect_linebreak(report)?;

Ok(AstDirectiveMacro {
header_span,
pattern,
contents,

item_ref: None,
})
}


fn parse_rule(
report: &mut diagn::Report,
walker: &mut syntax::Walker,
is_subruledef: bool)
-> Result<AstRule, ()>
{
let mut pattern_span = diagn::Span::new_dummy();
let pattern = parse_match_pattern(
report,
walker,
is_subruledef)?;

let expr = expr::parse(report, walker)?;

Ok(AstRule {
pattern,
expr,
})
}


fn parse_match_pattern(
report: &mut diagn::Report,
walker: &mut syntax::Walker,
is_subruledef: bool)
-> Result<AstMatchPattern, ()>
{
let mut span = diagn::Span::new_dummy();
let mut pattern = Vec::new();
let mut has_used_empty_specifier = false;

Expand All @@ -118,7 +183,7 @@ fn parse_rule(
let tk = walker.next_token();
walker.advance_to_token_end(&tk);

pattern_span = pattern_span.join(tk.span);
span = span.join(tk.span);


if tk.kind == syntax::TokenKind::BraceOpen
Expand All @@ -136,7 +201,7 @@ fn parse_rule(
pattern.push(AstRulePatternPart::Parameter(param));

let tk_close = walker.expect(report, syntax::TokenKind::BraceClose)?;
pattern_span = pattern_span.join(tk_close.span);
span = span.join(tk_close.span);
}
}

Expand All @@ -163,7 +228,6 @@ fn parse_rule(
}
}


let tk_heavy_arrow = walker.expect(report, syntax::TokenKind::HeavyArrowRight)?;

if pattern.len() == 0 && !has_used_empty_specifier
Expand All @@ -175,13 +239,9 @@ fn parse_rule(
return Err(());
}


let expr = expr::parse(report, walker)?;

Ok(AstRule {
pattern_span,
pattern,
expr,
Ok(AstMatchPattern {
span,
parts: pattern,
})
}

Expand Down
20 changes: 20 additions & 0 deletions src/asm/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub use directive_res::AstDirectiveRes;
mod directive_ruledef;
pub use directive_ruledef::{
AstDirectiveRuledef,
AstDirectiveMacro,
AstRule,
AstRulePatternPart,
AstRuleParameter,
Expand Down Expand Up @@ -90,6 +91,7 @@ pub enum AstAny
DirectiveIf(AstDirectiveIf),
DirectiveInclude(AstDirectiveInclude),
DirectiveLabelAlign(AstDirectiveLabelAlign),
DirectiveMacro(AstDirectiveMacro),
DirectiveNoEmit(AstDirectiveNoEmit),
DirectiveOnce(AstDirectiveOnce),
DirectiveRes(AstDirectiveRes),
Expand Down Expand Up @@ -255,6 +257,23 @@ pub fn parse(
}


pub fn parse_braced_toplevel(
report: &mut diagn::Report,
walker: &mut syntax::Walker)
-> Result<AstTopLevel, ()>
{
walker.expect(report, syntax::TokenKind::BraceOpen)?;

let block = parse_nested_toplevel(
report,
walker)?;

walker.expect(report, syntax::TokenKind::BraceClose)?;

Ok(block)
}


pub fn parse_nested_toplevel(
report: &mut diagn::Report,
walker: &mut syntax::Walker)
Expand Down Expand Up @@ -344,6 +363,7 @@ impl AstAny
AstAny::DirectiveOnce(node) => node.header_span,
AstAny::DirectiveRes(node) => node.header_span,
AstAny::DirectiveRuledef(node) => node.header_span,
AstAny::DirectiveMacro(node) => node.header_span,
AstAny::Instruction(node) => node.span,
AstAny::Symbol(node) => node.decl_span,
}
Expand Down
3 changes: 2 additions & 1 deletion src/asm/resolver/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ impl<'ast, 'decls> ResolveIterator<'ast, 'decls>
asm::AstAny::DirectiveLabelAlign(..) |
asm::AstAny::DirectiveNoEmit(..) |
asm::AstAny::DirectiveOnce(..) |
asm::AstAny::DirectiveRuledef(..) =>
asm::AstAny::DirectiveRuledef(..) |
asm::AstAny::DirectiveMacro(..) =>
{
self.index += 1;
node = ResolverNode::None;
Expand Down
5 changes: 5 additions & 0 deletions tests/macro/ok_arg_simple.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#macro emit {x: u8} => {
#d8 {x}
}

emit 0xab ; = 0xab
5 changes: 5 additions & 0 deletions tests/macro/ok_simple.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#macro zero => {
#d8 0
}

zero ; = 0x00

0 comments on commit 17c5d8e

Please sign in to comment.