Add lexer dump to main and fix a couple of bugs

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-01-27 19:07:57 -05:00
parent a5388c8b86
commit c4c196a136
3 changed files with 35 additions and 6 deletions

View File

@@ -2,6 +2,29 @@ mod common;
//mod syn;
mod vm;
fn main() {
println!("Hello, world!");
use std::{
io::{self, Read},
process,
};
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
fn get_input_string() -> io::Result<String> {
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(())
}

View File

@@ -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)]

View File

@@ -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" <s:Label> => Directive::Section(s.to_string()),
r"\.section" <s:Label> => Directive::Section(s.to_string()),
r"\.org" <v:Value> => Directive::Org(v),
}
pub Line: Line = {
Line: Line = {
<Directive> => Line::Directive(<>),
<Inst> => Line::Inst(<>),
<LabelDef> => Line::LabelDef(<>),
}
pub Program: Vec<Line> = {
<Line*> => <>,
}