Add string support to henceforth
This commit is contained in:
parent
47529445df
commit
20dfd1244d
16 changed files with 824 additions and 353 deletions
|
@ -1,61 +1,52 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use sailfish::TemplateOnce;
|
||||
use indexmap::IndexSet;
|
||||
use itertools::Itertools;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::parser;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Instruction {
|
||||
Push(u16),
|
||||
pub mod instruction;
|
||||
pub use crate::compiler::instruction::Instruction;
|
||||
|
||||
Drop,
|
||||
Add,
|
||||
Sub,
|
||||
Dot,
|
||||
|
||||
Call(String),
|
||||
}
|
||||
|
||||
impl Instruction {
|
||||
pub fn from_word(word: &str) -> Option<Self> {
|
||||
match word {
|
||||
"drop" => Some(Instruction::Drop),
|
||||
"+" => Some(Instruction::Add),
|
||||
"-" => Some(Instruction::Sub),
|
||||
"." => Some(Instruction::Dot),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
pub trait Compilable<T, U> {
|
||||
fn compile(&self, data: &T) -> Result<U>;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Word {
|
||||
id: u16,
|
||||
instructions: Vec<Instruction>,
|
||||
pub id: usize,
|
||||
pub instructions: Vec<Instruction>,
|
||||
pub times_used: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Data {
|
||||
instructions: Vec<Instruction>,
|
||||
words: HashMap<String, Word>,
|
||||
pub words: HashMap<String, Word>,
|
||||
pub strings: IndexSet<String>,
|
||||
}
|
||||
|
||||
impl Data {
|
||||
fn new() -> Self {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
instructions: vec![],
|
||||
words: HashMap::new(),
|
||||
strings: IndexSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn generate_instructions(&mut self, body: parser::ast::Body) -> Result<()> {
|
||||
pub fn generate_instructions(&mut self, body: parser::ast::Body) -> Result<Vec<Instruction>> {
|
||||
let mut instructions: Vec<Instruction> = vec![];
|
||||
for node in body {
|
||||
match node {
|
||||
parser::ast::Node::Comment(_) => {}
|
||||
parser::ast::Node::String { mode, string } => {}
|
||||
parser::ast::Node::String { mode, string } => {
|
||||
let id = self.strings.insert_full(string).0;
|
||||
instructions.push(match mode.as_str() {
|
||||
"." => Instruction::DotQuote(id),
|
||||
_ => bail!("Unknown string mode: {}", mode),
|
||||
});
|
||||
}
|
||||
parser::ast::Node::Number(x) => {
|
||||
self.instructions.push(Instruction::Push(x as u16));
|
||||
instructions.push(instruction::Instruction::Push(x as u16));
|
||||
}
|
||||
parser::ast::Node::WordDefinition {
|
||||
name,
|
||||
|
@ -68,25 +59,23 @@ impl Data {
|
|||
bail!("Word already exists as user word definition: {}", name);
|
||||
}
|
||||
|
||||
let pre_instructions = self.instructions.clone();
|
||||
self.instructions.clear();
|
||||
self.generate_instructions(body)?;
|
||||
let instructions = self.instructions.clone();
|
||||
self.instructions = pre_instructions;
|
||||
let ins = self.generate_instructions(body)?;
|
||||
|
||||
self.words.insert(
|
||||
name,
|
||||
Word {
|
||||
id: self.words.len() as u16,
|
||||
instructions,
|
||||
id: self.words.len(),
|
||||
instructions: ins,
|
||||
times_used: 0,
|
||||
},
|
||||
);
|
||||
}
|
||||
parser::ast::Node::Word(x) => {
|
||||
if let Some(ins) = Instruction::from_word(&x) {
|
||||
self.instructions.push(ins);
|
||||
} else if self.words.contains_key(&x) {
|
||||
self.instructions.push(Instruction::Call(x));
|
||||
instructions.push(ins);
|
||||
} else if let Some(w) = self.words.get_mut(&x) {
|
||||
instructions.push(Instruction::Call(x));
|
||||
w.times_used += 1;
|
||||
} else {
|
||||
bail!("Word does not exist: {}", x);
|
||||
}
|
||||
|
@ -94,20 +83,198 @@ impl Data {
|
|||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(instructions)
|
||||
}
|
||||
|
||||
pub fn embed(&self, body: hence::parser::ast::Body) -> hence::parser::ast::Body {
|
||||
let mut x = vec![
|
||||
// includes
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "include".to_string(),
|
||||
args: vec![hence::arg::Arg::String("$lib/core.asm".to_string())],
|
||||
},
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "include".to_string(),
|
||||
args: vec![hence::arg::Arg::String("$lib/std.asm".to_string())],
|
||||
},
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "include".to_string(),
|
||||
args: vec![hence::arg::Arg::String("$lib/main.asm".to_string())],
|
||||
},
|
||||
// constants
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "define".to_string(),
|
||||
args: vec![
|
||||
hence::arg::Arg::Variable("MEM_CALL_STACK".to_string()),
|
||||
hence::arg::Arg::Variable("CORE_MEM_MEM".to_string()),
|
||||
],
|
||||
},
|
||||
// macros
|
||||
// stack_transfer_alu
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "macro".to_string(),
|
||||
args: vec![hence::arg::Arg::Variable("stack_transfer_alu".to_string())],
|
||||
},
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "std_ld".to_string(),
|
||||
args: vec![],
|
||||
},
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "tlr".to_string(),
|
||||
arg: Some(hence::arg::Arg::Variable("CORE_REG_B".to_string())),
|
||||
},
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "std_ld".to_string(),
|
||||
args: vec![],
|
||||
},
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "tlr".to_string(),
|
||||
arg: Some(hence::arg::Arg::Variable("CORE_REG_A".to_string())),
|
||||
},
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "endmacro".to_string(),
|
||||
args: vec![],
|
||||
},
|
||||
// call_word
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "macro".to_string(),
|
||||
args: vec![
|
||||
hence::arg::Arg::Variable("call_word".to_string()),
|
||||
hence::arg::Arg::Variable("call_word_arg_0_label".to_string()),
|
||||
],
|
||||
},
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "ts".to_string(),
|
||||
arg: Some(hence::arg::Arg::BinaryExpression {
|
||||
left: Box::new(hence::arg::Arg::Variable("OFFSET".to_string())),
|
||||
right: Box::new(hence::arg::Arg::Number(3 + 3 + 3 + 1 + 3 + 1)),
|
||||
op: hence::arg::BinaryExpressionOperator::Add,
|
||||
}),
|
||||
},
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "tlr".to_string(),
|
||||
arg: Some(hence::arg::Arg::Variable("CORE_REG_A".to_string())),
|
||||
},
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "ts".to_string(),
|
||||
arg: Some(hence::arg::Arg::Variable("MEM_CALL_STACK".to_string())),
|
||||
},
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "set".to_string(),
|
||||
arg: None,
|
||||
},
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "ts".to_string(),
|
||||
arg: Some(hence::arg::Arg::Variable(
|
||||
"call_word_arg_0_label".to_string(),
|
||||
)),
|
||||
},
|
||||
hence::parser::ast::Node::Call {
|
||||
// tlr CORE_REG_PC -> tlr 0 -> tlr
|
||||
name: "tlr".to_string(),
|
||||
arg: Some(hence::arg::Arg::Variable("CORE_REG_PC".to_string())),
|
||||
},
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "endmacro".to_string(),
|
||||
args: vec![],
|
||||
},
|
||||
// return_word
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "macro".to_string(),
|
||||
args: vec![hence::arg::Arg::Variable("return_word".to_string())],
|
||||
},
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "std_get".to_string(),
|
||||
args: vec![hence::arg::Arg::Variable("MEM_CALL_STACK".to_string())],
|
||||
},
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "tlr".to_string(),
|
||||
arg: Some(hence::arg::Arg::Variable("CORE_REG_PC".to_string())),
|
||||
},
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "endmacro".to_string(),
|
||||
args: vec![],
|
||||
},
|
||||
// jump_main
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "jump_main".to_string(),
|
||||
args: vec![],
|
||||
},
|
||||
// data
|
||||
hence::parser::ast::Node::Label("data".to_string()),
|
||||
hence::parser::ast::Node::Label("data_strings".to_string()),
|
||||
];
|
||||
|
||||
for (id, s) in self.strings.iter().enumerate() {
|
||||
x.extend([
|
||||
hence::parser::ast::Node::Label(format!("data_strings_{}", id)),
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "bytes".to_string(),
|
||||
args: vec![hence::arg::Arg::String(s.to_string())],
|
||||
},
|
||||
hence::parser::ast::Node::Label(format!("data_strings_end_{}", id)),
|
||||
]);
|
||||
}
|
||||
|
||||
// words
|
||||
x.push(hence::parser::ast::Node::Label("words".to_string()));
|
||||
for (name, word) in &self
|
||||
.words
|
||||
.iter()
|
||||
.filter(|(_, w)| w.times_used > 1)
|
||||
.sorted_by(|a, b| Ord::cmp(&a.1.id, &b.1.id))
|
||||
.collect::<Vec<_>>()
|
||||
{
|
||||
x.extend(vec![
|
||||
hence::parser::ast::Node::Label(format!("words_{}", word.id)),
|
||||
hence::parser::ast::Node::Comment(format!("word: \"{}\"", name)),
|
||||
]);
|
||||
x.extend(
|
||||
word.instructions
|
||||
.iter()
|
||||
.map(|ins| ins.compile(self))
|
||||
.collect::<Result<Vec<hence::parser::ast::Body>>>()
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.flatten(),
|
||||
);
|
||||
x.push(hence::parser::ast::Node::MacroCall {
|
||||
name: "return_word".to_string(),
|
||||
args: vec![],
|
||||
});
|
||||
}
|
||||
|
||||
x.extend([
|
||||
hence::parser::ast::Node::Label("main".to_string()),
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "main".to_string(),
|
||||
args: vec![hence::arg::Arg::Variable("main".to_string())],
|
||||
},
|
||||
]);
|
||||
x.extend(body);
|
||||
x.push(hence::parser::ast::Node::MacroCall {
|
||||
name: "std_stop".to_string(),
|
||||
args: vec![],
|
||||
});
|
||||
|
||||
x
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(TemplateOnce)]
|
||||
#[template(path = "assembly.asm")]
|
||||
pub struct Template {
|
||||
pub data: Data,
|
||||
}
|
||||
|
||||
pub fn compile(ast: parser::ast::AST) -> Result<String> {
|
||||
pub fn compile(ast: parser::ast::AST) -> Result<hence::parser::ast::AST> {
|
||||
let mut data = Data::new();
|
||||
data.generate_instructions(ast.body)?;
|
||||
dbg!(&data);
|
||||
let instructions = data.generate_instructions(ast.body)?;
|
||||
|
||||
Ok(Template { data }.render_once()?)
|
||||
Ok(hence::parser::ast::AST {
|
||||
body: data.embed(
|
||||
instructions
|
||||
.iter()
|
||||
.map(|ins| ins.compile(&data))
|
||||
.collect::<Result<Vec<hence::parser::ast::Body>>>()
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.collect(),
|
||||
),
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue