Add new words and assembly emedding
This commit is contained in:
parent
053f3c09a2
commit
30b608e5aa
4 changed files with 504 additions and 19 deletions
|
@ -26,7 +26,7 @@ pub struct Data {
|
|||
}
|
||||
|
||||
impl Data {
|
||||
pub fn new() -> Self {
|
||||
pub fn default() -> Self {
|
||||
Self {
|
||||
words: HashMap::new(),
|
||||
strings: IndexSet::new(),
|
||||
|
@ -35,13 +35,29 @@ impl Data {
|
|||
|
||||
pub fn generate_instructions(&mut self, body: parser::ast::Body) -> Result<Vec<Instruction>> {
|
||||
let mut instructions: Vec<Instruction> = vec![];
|
||||
for node in body {
|
||||
let mut iter = body.into_iter().peekable();
|
||||
while let Some(node) = iter.next() {
|
||||
match node {
|
||||
_ if iter.next_if_eq(&node).is_some() => {
|
||||
let count = iter.by_ref().peeking_take_while(|n| *n == node).count() + 2;
|
||||
instructions.push(Instruction::Multiple {
|
||||
instruction: Box::new(
|
||||
self.generate_instructions(vec![node])?
|
||||
.into_iter()
|
||||
.next()
|
||||
.unwrap(),
|
||||
),
|
||||
count,
|
||||
});
|
||||
}
|
||||
parser::ast::Node::Comment(_) => {}
|
||||
parser::ast::Node::String { mode, string } => {
|
||||
let id = self.strings.insert_full(string).0;
|
||||
instructions.push(match mode.as_str() {
|
||||
"." => Instruction::DotQuote(id),
|
||||
"." => {
|
||||
let id = self.strings.insert_full(string).0;
|
||||
Instruction::DotQuote(id)
|
||||
}
|
||||
"asm" => Instruction::AsmQuote(string),
|
||||
_ => bail!("Unknown string mode: {}", mode),
|
||||
});
|
||||
}
|
||||
|
@ -109,6 +125,17 @@ impl Data {
|
|||
hence::arg::Arg::Variable("CORE_MEM_MEM".to_string()),
|
||||
],
|
||||
},
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "define".to_string(),
|
||||
args: vec![
|
||||
hence::arg::Arg::Variable("MEM_ALLOC_PTR".to_string()),
|
||||
hence::arg::Arg::BinaryExpression {
|
||||
left: Box::new(hence::arg::Arg::Variable("CORE_MEM_MEM".to_string())),
|
||||
right: Box::new(hence::arg::Arg::Number(16)),
|
||||
op: hence::arg::BinaryExpressionOperator::Add,
|
||||
},
|
||||
],
|
||||
},
|
||||
// macros
|
||||
// stack_transfer_alu
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
|
@ -195,6 +222,22 @@ impl Data {
|
|||
name: "endmacro".to_string(),
|
||||
args: vec![],
|
||||
},
|
||||
// setup
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "std_rset".to_string(),
|
||||
args: vec![
|
||||
hence::arg::Arg::Variable("CORE_REG_A".to_string()),
|
||||
hence::arg::Arg::BinaryExpression {
|
||||
left: Box::new(hence::arg::Arg::Variable("MEM_ALLOC_PTR".to_string())),
|
||||
right: Box::new(hence::arg::Arg::Number(1)),
|
||||
op: hence::arg::BinaryExpressionOperator::Add,
|
||||
},
|
||||
],
|
||||
},
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "std_set".to_string(),
|
||||
args: vec![hence::arg::Arg::Variable("MEM_ALLOC_PTR".to_string())],
|
||||
},
|
||||
// jump_main
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "jump_main".to_string(),
|
||||
|
@ -262,7 +305,7 @@ impl Data {
|
|||
}
|
||||
|
||||
pub fn compile(ast: parser::ast::AST) -> Result<hence::parser::ast::AST> {
|
||||
let mut data = Data::new();
|
||||
let mut data = Data::default();
|
||||
let instructions = data.generate_instructions(ast.body)?;
|
||||
|
||||
Ok(hence::parser::ast::AST {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue