ref, add tests

This commit is contained in:
2020-05-05 23:10:36 +08:00
parent f62f966eaf
commit 57e91ff32e
3 changed files with 179 additions and 98 deletions

104
src/ins.rs Normal file
View File

@@ -0,0 +1,104 @@
#[derive(Debug)]
pub enum InsType {
Path(InsColl),
Node(Instruction),
}
#[derive(Debug)]
pub struct Ins {
pub c: char,
pub ins_type: InsType,
}
#[derive(Debug)]
pub struct InsColl {
pub inses: Vec<Ins>,
}
pub fn make_instruction_tree() -> InsColl {
InsColl{ inses: vec![
Ins{ c: '', ins_type: InsType::Path(InsColl{ inses: vec![
Ins{ c: '', ins_type: InsType::Node(Instruction::Push) },
Ins{ c: '', ins_type: InsType::Path(InsColl{ inses: vec![
Ins{ c: '', ins_type: InsType::Node(Instruction::Dup) },
Ins{ c: '', ins_type: InsType::Node(Instruction::Swap) },
Ins{ c: '', ins_type: InsType::Node(Instruction::Pop) }
]})},
Ins{ c: '', ins_type: InsType::Path(InsColl{ inses: vec![
Ins{ c: '', ins_type: InsType::Node(Instruction::CopyN) },
Ins{ c: '', ins_type: InsType::Node(Instruction::PopN) }
]})},
]})},
Ins{ c: '', ins_type: InsType::Path(InsColl{ inses: vec![
Ins{ c: '', ins_type: InsType::Path(InsColl{ inses: vec![
Ins{ c: '', ins_type: InsType::Path(InsColl{ inses: vec![
Ins{ c: '', ins_type: InsType::Node(Instruction::Add) },
Ins{ c: '', ins_type: InsType::Node(Instruction::Sub) },
Ins{ c: '', ins_type: InsType::Node(Instruction::Mul) },
]})},
Ins{ c: '', ins_type: InsType::Path(InsColl{ inses: vec![
Ins{ c: '', ins_type: InsType::Node(Instruction::Div) },
Ins{ c: '', ins_type: InsType::Node(Instruction::Mod) },
]})},
]})},
Ins{ c: '', ins_type: InsType::Path(InsColl{ inses: vec![
Ins{ c: '', ins_type: InsType::Node(Instruction::Store) },
Ins{ c: '', ins_type: InsType::Node(Instruction::Retrieve) },
]})},
Ins{ c: '', ins_type: InsType::Path(InsColl{ inses: vec![
Ins{ c: '', ins_type: InsType::Path(InsColl{ inses: vec![
Ins{ c: '', ins_type: InsType::Node(Instruction::StdOutChar) },
Ins{ c: '', ins_type: InsType::Node(Instruction::StdOutNum) },
]})},
Ins{ c: '', ins_type: InsType::Path(InsColl{ inses: vec![
Ins{ c: '', ins_type: InsType::Node(Instruction::StdInChar) },
Ins{ c: '', ins_type: InsType::Node(Instruction::StdInNum) },
]})},
]})},
]})},
Ins{ c: '', ins_type: InsType::Path(InsColl{ inses: vec![
Ins{ c: '', ins_type: InsType::Path(InsColl{ inses: vec![
Ins{ c: '', ins_type: InsType::Node(Instruction::DefineLabel) },
Ins{ c: '', ins_type: InsType::Node(Instruction::CallAtLabel) },
Ins{ c: '', ins_type: InsType::Node(Instruction::GotoLabel) },
]})},
Ins{ c: '', ins_type: InsType::Path(InsColl{ inses: vec![
Ins{ c: '', ins_type: InsType::Node(Instruction::GotoLabelE0) },
Ins{ c: '', ins_type: InsType::Node(Instruction::GotoLabelL0) },
Ins{ c: '', ins_type: InsType::Node(Instruction::ReturnCallAt) },
]})},
Ins{ c: '', ins_type: InsType::Path(InsColl{ inses: vec![
Ins{ c: '', ins_type: InsType::Node(Instruction::End) },
]})},
]})},
]}
}
#[derive(Debug, Clone)]
pub enum Instruction {
Push,
Dup,
CopyN,
Swap,
Pop,
PopN,
Add,
Sub,
Mul,
Div,
Mod,
Store,
Retrieve,
DefineLabel,
CallAtLabel,
GotoLabel,
GotoLabelE0,
GotoLabelL0,
ReturnCallAt,
End,
StdOutChar,
StdOutNum,
StdInChar,
StdInNum,
}