ref, add tests
This commit is contained in:
104
src/ins.rs
Normal file
104
src/ins.rs
Normal 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,
|
||||
}
|
||||
Reference in New Issue
Block a user