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

46
src/vm/syn/ast.rs Normal file
View File

@@ -0,0 +1,46 @@
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Line {
Section(Section),
Inst(Inst),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Section {
Code,
Data,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InstOp {
Add,
Mul,
Div,
Neg,
And,
Or,
Xor,
Shl,
Shr,
CmpEq,
CmpLt,
Jz,
Jnz,
Load,
Store,
StoreImm,
Copy,
Nop,
Halt,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Inst {
pub op: InstOp,
pub args: Vec<Value>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Value {
Number(u64),
Label(String),
}

4
src/vm/syn/mod.rs Normal file
View File

@@ -0,0 +1,4 @@
use lalrpop_util::lalrpop_mod;
lalrpop_mod!(pub parser, "/vm/syn/parser.rs");
pub mod ast;

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(<>),
}

19
src/vm/syn/pass.rs Normal file
View File

@@ -0,0 +1,19 @@
use crate::vm::syn::ast::*;
pub trait Pass<In> {
type Out;
fn pass(self, inval: In) -> Self::Out;
}
/// Confirms the instructions and their validity.
pub struct InstPass {
}
impl Pass<Vec<Line>> for InstPass {
type Out = Vec<Line>;
fn pass(self, inval: Vec<Line>) -> Self::Out {
inval
}
}