diff --git a/src/main.rs b/src/main.rs index 48832fa..28c6a39 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,29 @@ mod common; //mod syn; mod vm; -fn main() { - println!("Hello, world!"); +use std::{ + io::{self, Read}, + process, +}; + +type Result = std::result::Result>; + +fn get_input_string() -> io::Result { + let mut buffer = String::new(); + io::stdin().read_to_string(&mut buffer)?; + Ok(buffer) +} + +fn main() -> Result<()> { + use vm::syn::parser::ProgramParser; + let contents = get_input_string()?; + let ast = match ProgramParser::new().parse(&contents) { + Ok(ast) => ast, + Err(err) => { + eprintln!("{}", err); + process::exit(1); + }, + }; + println!("{:?}", ast); + Ok(()) } diff --git a/src/vm/syn/ast.rs b/src/vm/syn/ast.rs index e7e2974..e41a27a 100644 --- a/src/vm/syn/ast.rs +++ b/src/vm/syn/ast.rs @@ -1,4 +1,4 @@ -use crate::vm::{inst::Inst, reg::Reg}; +use crate::vm::inst::Inst; #[derive(Debug, Clone, PartialEq, Eq)] pub enum Line { @@ -10,6 +10,7 @@ pub enum Line { #[derive(Debug, Clone, PartialEq, Eq)] pub enum Directive { Section(String), + Org(Value), } #[derive(Debug, Clone, PartialEq, Eq)] diff --git a/src/vm/syn/parser.lalrpop b/src/vm/syn/parser.lalrpop index 1e89e74..60425fc 100644 --- a/src/vm/syn/parser.lalrpop +++ b/src/vm/syn/parser.lalrpop @@ -17,7 +17,7 @@ Value: Value = { } Label: String = { - "[a-zA-Z]+" => String::from(<>), + r"[a-zA-Z]+" => String::from(<>), } Number: u64 = { @@ -69,11 +69,16 @@ Inst: Inst = { } Directive: Directive = { - ".section" => Directive::Section(s.to_string()), + r"\.section" => Directive::Section(s.to_string()), + r"\.org" => Directive::Org(v), } -pub Line: Line = { +Line: Line = { => Line::Directive(<>), => Line::Inst(<>), => Line::LabelDef(<>), } + +pub Program: Vec = { + => <>, +}