Skip to content
This repository has been archived by the owner on Dec 10, 2023. It is now read-only.

Commit

Permalink
fix hlorenzi#112: add #once directive
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmett81 authored and hlorenzi committed Oct 21, 2021
1 parent 1ecb2d5 commit 4453e9b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/asm/parser/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ pub fn parse_file<TFilename: Into<String>>(
fileserver: &dyn util::FileServer,
filename: TFilename,
span: Option<&diagn::Span>,
parsed_filenames: &mut std::collections::HashSet<String>)
parsed_filenames: &mut std::collections::HashSet<String>,
once_filenames: &mut std::collections::HashSet<String>)
-> Result<(), ()>
{
let filename = filename.into();

if once_filenames.contains(&filename)
{
return Ok(());
}

let chars = fileserver.get_chars(report.clone(), &filename, span)?;
let tokens = syntax::tokenize(report.clone(), &filename, &chars)?;
let parser = syntax::Parser::new(Some(report.clone()), &tokens);
Expand All @@ -25,6 +32,7 @@ pub fn parse_file<TFilename: Into<String>>(
filename: std::rc::Rc::new(filename.clone()),
parser,
parsed_filenames,
once_filenames,
};

//println!("{:#?}", state.parser.tokens.iter().map(|t| t.kind).collect::<Vec<_>>());
Expand All @@ -33,7 +41,7 @@ pub fn parse_file<TFilename: Into<String>>(
{
parse_line(&mut state)?;
}
parsed_filenames.remove(&filename);
Ok(())
}
Expand Down Expand Up @@ -109,6 +117,7 @@ pub fn parse_directive(state: &mut asm::parser::State)
"ruledef" | "cpudef" => asm::parser::parse_directive_ruledef(state, &tk_directive, true)?,
"subruledef" | "tokendef" => asm::parser::parse_directive_ruledef(state, &tk_directive, false)?,
"include" => asm::parser::parse_directive_include(state)?,
"once" => asm::parser::parse_directive_once(state)?,
"res" => asm::parser::parse_directive_res(state)?,
"align" => asm::parser::parse_directive_align(state)?,
"labelalign" => asm::parser::parse_directive_labelalign(state)?,
Expand Down
13 changes: 12 additions & 1 deletion src/asm/parser/include.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,16 @@ pub fn parse_directive_include(
state.fileserver,
new_filename,
Some(&tk_filename.span),
state.parsed_filenames)
state.parsed_filenames,
state.once_filenames)
}


pub fn parse_directive_once(
state: &mut asm::parser::State)
-> Result<(), ()>
{
state.once_filenames.insert(state.filename.as_ref().clone());

Ok(())
}
1 change: 1 addition & 0 deletions src/asm/parser/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ pub struct State<'a>
pub filename: std::rc::Rc<String>,
pub parser: syntax::Parser<'a>,
pub parsed_filenames: &'a mut std::collections::HashSet<String>,
pub once_filenames: &'a mut std::collections::HashSet<String>,
}
1 change: 1 addition & 0 deletions src/asm/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ impl Assembler
fileserver,
filename,
None,
&mut std::collections::HashSet::new(),
&mut std::collections::HashSet::new());

if pass_report.has_errors() || result.is_err()
Expand Down
9 changes: 9 additions & 0 deletions tests/once/1.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ruledef
{
emit {x: u8} => x
}

#include "once.asm"
#include "once.asm"
#include "once.asm"
emit $ ; = 0xcd01
2 changes: 2 additions & 0 deletions tests/once/once.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#once
#d8 0xCD

0 comments on commit 4453e9b

Please sign in to comment.