73
src/vm/syn/parser.lalrpop
Normal file
73
src/vm/syn/parser.lalrpop
Normal file
@@ -0,0 +1,73 @@
|
||||
use std::str::FromStr;
|
||||
use crate::vm::{
|
||||
syn::ast::*,
|
||||
};
|
||||
|
||||
grammar;
|
||||
|
||||
// TODO : instkind
|
||||
InstOp: InstOp = {
|
||||
"add" => InstOp::Add,
|
||||
"mul" => InstOp::Mul,
|
||||
"div" => InstOp::Div,
|
||||
"neg" => InstOp::Neg,
|
||||
"and" => InstOp::And,
|
||||
"or" => InstOp::Or,
|
||||
"xor" => InstOp::Xor,
|
||||
"shl" => InstOp::Shl,
|
||||
"shr" => InstOp::Shr,
|
||||
"cmpeq" => InstOp::CmpEq,
|
||||
"cmplt" => InstOp::CmpLt,
|
||||
"jz" => InstOp::Jz,
|
||||
"jnz" => InstOp::Jnz,
|
||||
"load" => InstOp::Load,
|
||||
"store" => InstOp::Store,
|
||||
"storeimm" => InstOp::StoreImm,
|
||||
"copy" => InstOp::Copy,
|
||||
"nop" => InstOp::Nop,
|
||||
"halt" => InstOp::Halt,
|
||||
}
|
||||
|
||||
LabelDef: String = {
|
||||
<Label> ":" => <>
|
||||
}
|
||||
|
||||
pub Label: String = {
|
||||
"[a-zA-Z]+" => String::from(<>),
|
||||
}
|
||||
|
||||
pub Section: Section = {
|
||||
".code" => Section::Code,
|
||||
".data" => Section::Data,
|
||||
}
|
||||
|
||||
pub Inst: Inst = {
|
||||
<op:InstOp> <head:Value> <tail:("," <Value>)+> => {
|
||||
Inst {
|
||||
op,
|
||||
args: {
|
||||
let mut tail = tail;
|
||||
tail.insert(0, head);
|
||||
tail
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
<op:InstOp> <head:Value?> => {
|
||||
Inst { op, args: if let Some(head) = head { vec![head] } else { vec![] } }
|
||||
}
|
||||
}
|
||||
|
||||
pub Value: Value = {
|
||||
<Number> => Value::Number(<>),
|
||||
<Label> => Value::Label(<>),
|
||||
}
|
||||
|
||||
pub Number: u64 = {
|
||||
<s:r"[0-9]+"> => u64::from_str(s).unwrap()
|
||||
}
|
||||
|
||||
pub Line: Line = {
|
||||
<Section> => Line::Section(<>),
|
||||
<Inst> => Line::Inst(<>),
|
||||
}
|
||||
Reference in New Issue
Block a user