Initial commit

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-01-25 19:17:39 -05:00
commit ddfcec0427
20 changed files with 1561 additions and 0 deletions

73
src/vm/syn/parser.lalrpop Normal file
View 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(<>),
}