Add string support to henceforth
This commit is contained in:
parent
47529445df
commit
20dfd1244d
16 changed files with 824 additions and 353 deletions
526
henceforth/src/lib/compiler/instruction.rs
Normal file
526
henceforth/src/lib/compiler/instruction.rs
Normal file
|
@ -0,0 +1,526 @@
|
|||
use anyhow::{bail, Result};
|
||||
|
||||
use crate::compiler;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Instruction {
|
||||
Nop,
|
||||
Debug,
|
||||
Quit,
|
||||
Push(u16),
|
||||
Drop,
|
||||
Depth,
|
||||
Pick,
|
||||
Dup,
|
||||
Swap,
|
||||
Over,
|
||||
Rot,
|
||||
Nip,
|
||||
I,
|
||||
J,
|
||||
Tuck,
|
||||
Fetch,
|
||||
FetchPrint,
|
||||
Store,
|
||||
PlusStore,
|
||||
MinusStore,
|
||||
Cells,
|
||||
Allot,
|
||||
Dot,
|
||||
Emit,
|
||||
Space,
|
||||
Spaces,
|
||||
Cr,
|
||||
DotQuote(usize),
|
||||
Count,
|
||||
Not,
|
||||
And,
|
||||
Nand,
|
||||
Or,
|
||||
Nor,
|
||||
Xor,
|
||||
Xnor,
|
||||
Lsh,
|
||||
Rsh,
|
||||
Compare,
|
||||
Eq,
|
||||
Neq,
|
||||
Lt,
|
||||
Leq,
|
||||
Gt,
|
||||
Geq,
|
||||
Min,
|
||||
Max,
|
||||
Boolean,
|
||||
Invert,
|
||||
Plus,
|
||||
OnePlus,
|
||||
TwoPlus,
|
||||
Minus,
|
||||
OneMinus,
|
||||
TwoMinus,
|
||||
Times,
|
||||
Divide,
|
||||
Mod,
|
||||
Call(String),
|
||||
}
|
||||
|
||||
impl Instruction {
|
||||
pub fn from_word(word: &str) -> Option<Self> {
|
||||
match word.to_lowercase().as_str() {
|
||||
"nop" => Some(Instruction::Nop),
|
||||
"debug" => Some(Instruction::Debug),
|
||||
"quit" => Some(Instruction::Quit),
|
||||
"drop" => Some(Instruction::Drop),
|
||||
"depth" => Some(Instruction::Depth),
|
||||
"pick" => Some(Instruction::Pick),
|
||||
"dup" => Some(Instruction::Dup),
|
||||
"swap" => Some(Instruction::Swap),
|
||||
"over" => Some(Instruction::Over),
|
||||
"rot" => Some(Instruction::Rot),
|
||||
"nip" => Some(Instruction::Nip),
|
||||
"i" => Some(Instruction::I),
|
||||
"j" => Some(Instruction::J),
|
||||
"tuck" => Some(Instruction::Tuck),
|
||||
"@" => Some(Instruction::Fetch),
|
||||
"?" => Some(Instruction::FetchPrint),
|
||||
"!" => Some(Instruction::Store),
|
||||
"+!" => Some(Instruction::PlusStore),
|
||||
"-!" => Some(Instruction::MinusStore),
|
||||
"cells" => Some(Instruction::Cells),
|
||||
"allot" => Some(Instruction::Allot),
|
||||
"." => Some(Instruction::Dot),
|
||||
"emit" => Some(Instruction::Emit),
|
||||
"space" => Some(Instruction::Space),
|
||||
"spaces" => Some(Instruction::Spaces),
|
||||
"cr" => Some(Instruction::Cr),
|
||||
"count" => Some(Instruction::Count),
|
||||
"not" => Some(Instruction::Not),
|
||||
"and" => Some(Instruction::And),
|
||||
"nand" => Some(Instruction::Nand),
|
||||
"or" => Some(Instruction::Or),
|
||||
"nor" => Some(Instruction::Nor),
|
||||
"xor" => Some(Instruction::Xor),
|
||||
"xnor" => Some(Instruction::Xnor),
|
||||
"lshift" => Some(Instruction::Lsh),
|
||||
"rshift" => Some(Instruction::Rsh),
|
||||
"compare" => Some(Instruction::Compare),
|
||||
"=" => Some(Instruction::Eq),
|
||||
"!=" => Some(Instruction::Neq),
|
||||
"<" => Some(Instruction::Lt),
|
||||
"<=" => Some(Instruction::Leq),
|
||||
">" => Some(Instruction::Gt),
|
||||
">=" => Some(Instruction::Geq),
|
||||
"min" => Some(Instruction::Min),
|
||||
"max" => Some(Instruction::Max),
|
||||
"boolean" => Some(Instruction::Boolean),
|
||||
"invert" => Some(Instruction::Invert),
|
||||
"+" => Some(Instruction::Plus),
|
||||
"1+" => Some(Instruction::OnePlus),
|
||||
"2+" => Some(Instruction::TwoPlus),
|
||||
"-" => Some(Instruction::Minus),
|
||||
"1-" => Some(Instruction::OneMinus),
|
||||
"2-" => Some(Instruction::TwoMinus),
|
||||
"*" => Some(Instruction::Times),
|
||||
"/" => Some(Instruction::Divide),
|
||||
"mod" => Some(Instruction::Mod),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl compiler::Compilable<compiler::Data, hence::parser::ast::Body> for Instruction {
|
||||
fn compile(&self, data: &compiler::Data) -> Result<hence::parser::ast::Body> {
|
||||
match self {
|
||||
Instruction::Nop => Ok(vec![hence::parser::ast::Node::Call {
|
||||
name: "nop".to_string(),
|
||||
arg: None,
|
||||
}]),
|
||||
Instruction::Debug => Ok(vec![hence::parser::ast::Node::Call {
|
||||
name: "dbg".to_string(),
|
||||
arg: None,
|
||||
}]),
|
||||
Instruction::Quit => Ok(vec![hence::parser::ast::Node::MacroCall {
|
||||
name: "std_stop".to_string(),
|
||||
args: vec![],
|
||||
}]),
|
||||
Instruction::Push(x) => Ok(vec![hence::parser::ast::Node::Call {
|
||||
name: "push".to_string(),
|
||||
arg: Some(hence::arg::Arg::Number(*x)),
|
||||
}]),
|
||||
Instruction::Drop => Ok(vec![hence::parser::ast::Node::Call {
|
||||
name: "pop".to_string(),
|
||||
arg: None,
|
||||
}]),
|
||||
Instruction::Depth => Ok(vec![
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "tsr".to_string(),
|
||||
arg: Some(hence::arg::Arg::Variable("CORE_REG_SP".to_string())),
|
||||
},
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "tls".to_string(),
|
||||
arg: None,
|
||||
},
|
||||
]),
|
||||
Instruction::Pick => Ok(vec![]),
|
||||
Instruction::Dup => Ok(vec![
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "tss".to_string(),
|
||||
arg: None,
|
||||
},
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "tls".to_string(),
|
||||
arg: None,
|
||||
},
|
||||
]),
|
||||
Instruction::Swap => Ok(vec![
|
||||
// hence::parser::ast::Node::Call {
|
||||
// name: "ld".to_string(),
|
||||
// arg: Some(hence::arg::Arg::Variable("CORE_REG_A".to_string())),
|
||||
// },
|
||||
// hence::parser::ast::Node::Call {
|
||||
// name: "ld".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: "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::Call {
|
||||
name: "tsr".to_string(),
|
||||
arg: Some(hence::arg::Arg::Variable("CORE_REG_A".to_string())),
|
||||
},
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "tls".to_string(),
|
||||
arg: None,
|
||||
},
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "tsr".to_string(),
|
||||
arg: Some(hence::arg::Arg::Variable("CORE_REG_B".to_string())),
|
||||
},
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "tls".to_string(),
|
||||
arg: None,
|
||||
},
|
||||
]),
|
||||
Instruction::Over => Ok(vec![]),
|
||||
Instruction::Rot => Ok(vec![]),
|
||||
Instruction::Nip => Ok(vec![]),
|
||||
Instruction::I => Ok(vec![]),
|
||||
Instruction::J => Ok(vec![]),
|
||||
Instruction::Tuck => Ok(vec![]),
|
||||
Instruction::Fetch => Ok(vec![]),
|
||||
Instruction::FetchPrint => Ok(vec![]),
|
||||
Instruction::Store => Ok(vec![]),
|
||||
Instruction::PlusStore => Ok(vec![]),
|
||||
Instruction::MinusStore => Ok(vec![]),
|
||||
Instruction::Cells => Ok(vec![]),
|
||||
Instruction::Allot => Ok(vec![]),
|
||||
Instruction::Dot => Ok(vec![
|
||||
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: "std_set".to_string(),
|
||||
args: vec![hence::arg::Arg::Variable("CORE_MEM_OUT".to_string())],
|
||||
},
|
||||
]),
|
||||
Instruction::Emit => Ok(vec![]),
|
||||
Instruction::Space => Ok(vec![]),
|
||||
Instruction::Spaces => Ok(vec![]),
|
||||
Instruction::Cr => Ok(vec![
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "std_rset".to_string(),
|
||||
args: vec![
|
||||
hence::arg::Arg::Variable("CORE_REG_A".to_string()),
|
||||
hence::arg::Arg::Number(0x0a),
|
||||
],
|
||||
},
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "std_set".to_string(),
|
||||
args: vec![hence::arg::Arg::Variable("CORE_MEM_CHR".to_string())],
|
||||
},
|
||||
]),
|
||||
Instruction::DotQuote(x) => {
|
||||
let loop_label = format!("loop_strings_{}", x);
|
||||
let data_label = format!("data_strings_{}", x);
|
||||
let data_end_label = format!("data_strings_end_{}", x);
|
||||
|
||||
Ok(vec![
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "std_rset".to_string(),
|
||||
args: vec![
|
||||
hence::arg::Arg::Variable("CORE_REG_B".to_string()),
|
||||
hence::arg::Arg::Variable(data_label),
|
||||
],
|
||||
},
|
||||
hence::parser::ast::Node::Label(loop_label.clone()),
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "tsr".to_string(),
|
||||
arg: Some(hence::arg::Arg::Variable("CORE_REG_B".to_string())),
|
||||
},
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "get".to_string(),
|
||||
arg: None,
|
||||
},
|
||||
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: "std_set".to_string(),
|
||||
args: vec![hence::arg::Arg::Variable("CORE_MEM_CHR".to_string())],
|
||||
},
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "std_rset".to_string(),
|
||||
args: vec![
|
||||
hence::arg::Arg::Variable("CORE_REG_A".to_string()),
|
||||
hence::arg::Arg::Number(1),
|
||||
],
|
||||
},
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "std_alu".to_string(),
|
||||
args: vec![hence::arg::Arg::Variable("CORE_ALU_ADD".to_string())],
|
||||
},
|
||||
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_rset".to_string(),
|
||||
args: vec![
|
||||
hence::arg::Arg::Variable("CORE_REG_A".to_string()),
|
||||
hence::arg::Arg::Variable(data_end_label),
|
||||
],
|
||||
},
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "std_alu".to_string(),
|
||||
args: vec![hence::arg::Arg::Variable("CORE_ALU_GT".to_string())],
|
||||
},
|
||||
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: "std_cond_jump".to_string(),
|
||||
args: vec![hence::arg::Arg::Variable(loop_label)],
|
||||
},
|
||||
])
|
||||
}
|
||||
Instruction::Count => Ok(vec![]),
|
||||
Instruction::Not => Ok(vec![]),
|
||||
Instruction::And => Ok(vec![]),
|
||||
Instruction::Nand => Ok(vec![]),
|
||||
Instruction::Or => Ok(vec![]),
|
||||
Instruction::Nor => Ok(vec![]),
|
||||
Instruction::Xor => Ok(vec![]),
|
||||
Instruction::Xnor => Ok(vec![]),
|
||||
Instruction::Lsh => Ok(vec![]),
|
||||
Instruction::Rsh => Ok(vec![]),
|
||||
Instruction::Compare => Ok(vec![]),
|
||||
Instruction::Eq => Ok(vec![]),
|
||||
Instruction::Neq => Ok(vec![]),
|
||||
Instruction::Lt => Ok(vec![]),
|
||||
Instruction::Leq => Ok(vec![]),
|
||||
Instruction::Gt => Ok(vec![]),
|
||||
Instruction::Geq => Ok(vec![]),
|
||||
Instruction::Min => Ok(vec![]),
|
||||
Instruction::Max => Ok(vec![]),
|
||||
Instruction::Boolean => Ok(vec![]),
|
||||
Instruction::Invert => Ok(vec![]),
|
||||
Instruction::Plus => Ok(vec![
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "stack_transfer_alu".to_string(),
|
||||
args: vec![],
|
||||
},
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "std_alu".to_string(),
|
||||
args: vec![hence::arg::Arg::Variable("CORE_ALU_ADD".to_string())],
|
||||
},
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "tls".to_string(),
|
||||
arg: None,
|
||||
},
|
||||
]),
|
||||
Instruction::OnePlus => Ok(vec![
|
||||
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: "std_rset".to_string(),
|
||||
args: vec![
|
||||
hence::arg::Arg::Variable("CORE_REG_B".to_string()),
|
||||
hence::arg::Arg::Number(1),
|
||||
],
|
||||
},
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "std_alu".to_string(),
|
||||
args: vec![hence::arg::Arg::Variable("CORE_ALU_ADD".to_string())],
|
||||
},
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "tls".to_string(),
|
||||
arg: None,
|
||||
},
|
||||
]),
|
||||
Instruction::TwoPlus => Ok(vec![
|
||||
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: "std_rset".to_string(),
|
||||
args: vec![
|
||||
hence::arg::Arg::Variable("CORE_REG_B".to_string()),
|
||||
hence::arg::Arg::Number(2),
|
||||
],
|
||||
},
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "std_alu".to_string(),
|
||||
args: vec![hence::arg::Arg::Variable("CORE_ALU_ADD".to_string())],
|
||||
},
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "tls".to_string(),
|
||||
arg: None,
|
||||
},
|
||||
]),
|
||||
Instruction::Minus => Ok(vec![
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "stack_transfer_alu".to_string(),
|
||||
args: vec![],
|
||||
},
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "std_alu".to_string(),
|
||||
args: vec![hence::arg::Arg::Variable("CORE_ALU_SUB".to_string())],
|
||||
},
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "tls".to_string(),
|
||||
arg: None,
|
||||
},
|
||||
]),
|
||||
Instruction::OneMinus => Ok(vec![
|
||||
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: "std_rset".to_string(),
|
||||
args: vec![
|
||||
hence::arg::Arg::Variable("CORE_REG_B".to_string()),
|
||||
hence::arg::Arg::Number(1),
|
||||
],
|
||||
},
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "std_alu".to_string(),
|
||||
args: vec![hence::arg::Arg::Variable("CORE_ALU_SUB".to_string())],
|
||||
},
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "tls".to_string(),
|
||||
arg: None,
|
||||
},
|
||||
]),
|
||||
Instruction::TwoMinus => Ok(vec![
|
||||
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: "std_rset".to_string(),
|
||||
args: vec![
|
||||
hence::arg::Arg::Variable("CORE_REG_B".to_string()),
|
||||
hence::arg::Arg::Number(2),
|
||||
],
|
||||
},
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "std_alu".to_string(),
|
||||
args: vec![hence::arg::Arg::Variable("CORE_ALU_SUB".to_string())],
|
||||
},
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "tls".to_string(),
|
||||
arg: None,
|
||||
},
|
||||
]),
|
||||
Instruction::Times => Ok(vec![
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "stack_transfer_alu".to_string(),
|
||||
args: vec![],
|
||||
},
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "std_alu".to_string(),
|
||||
args: vec![hence::arg::Arg::Variable("CORE_ALU_MUL".to_string())],
|
||||
},
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "tls".to_string(),
|
||||
arg: None,
|
||||
},
|
||||
]),
|
||||
Instruction::Divide => Ok(vec![
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "stack_transfer_alu".to_string(),
|
||||
args: vec![],
|
||||
},
|
||||
hence::parser::ast::Node::MacroCall {
|
||||
name: "std_alu".to_string(),
|
||||
args: vec![hence::arg::Arg::Variable("CORE_ALU_DIV".to_string())],
|
||||
},
|
||||
hence::parser::ast::Node::Call {
|
||||
name: "tls".to_string(),
|
||||
arg: None,
|
||||
},
|
||||
]),
|
||||
Instruction::Mod => Ok(vec![]),
|
||||
Instruction::Call(x) => match data.words.get(x) {
|
||||
Some(w) => {
|
||||
if w.times_used > 1 {
|
||||
Ok(vec![hence::parser::ast::Node::MacroCall {
|
||||
name: "call_word".to_string(),
|
||||
args: vec![hence::arg::Arg::Variable(format!("words_{}", w.id))],
|
||||
}])
|
||||
} else {
|
||||
Ok(w.instructions
|
||||
.iter()
|
||||
.map(|ins| ins.compile(data))
|
||||
.collect::<Result<Vec<hence::parser::ast::Body>>>()?
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.collect())
|
||||
}
|
||||
}
|
||||
None => bail!("Unknown word: {}", x),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue