From c4c196a136df6288334024d567bee54eb6c7c980 Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Mon, 27 Jan 2020 19:07:57 -0500 Subject: [PATCH] Add lexer dump to main and fix a couple of bugs Signed-off-by: Alek Ratzloff --- src/main.rs | 27 +++++++++++++++++++++++++-- src/vm/syn/ast.rs | 3 ++- src/vm/syn/parser.lalrpop | 11 ++++++++--- 3 files changed, 35 insertions(+), 6 deletions(-) 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 = { + => <>, +}