diff --git a/src/error.rs b/src/error.rs index 251f1a2..3de05d9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -9,6 +9,27 @@ pub struct CompilerError { pub notes: Vec<(String, Span)>, pub help: Option, } +impl CompilerError { + pub fn new(msg: String, span: Span, notes: Vec<(String, Span)>, help: Option) -> Self { + Self { + msg, + span, + notes, + help, + } + } + + pub fn simple(msg: String, span: Span) -> Self { + Self::new_notes(msg, span, Vec::new()) + } + + pub fn help(msg: String, span: Span, help: String) -> Self { + Self::new(msg, span, Vec::new(), Some(help)) + } + pub fn new_notes(msg: String, span: Span, notes: Vec<(String, Span)>) -> Self { + Self::new(msg, span, notes, None) + } +} pub type Result = std::result::Result; diff --git a/src/ir.rs b/src/ir.rs index 34c34c3..bbd34cd 100644 --- a/src/ir.rs +++ b/src/ir.rs @@ -40,6 +40,7 @@ pub enum Stmt { Mul { to: Place, value: Value }, Div { to: Place, value: Value }, Jmp { to: Location }, + Je { to: Location }, Cmp { lhs: Value, rhs: Value }, } @@ -64,11 +65,42 @@ impl CompileCtx { let to = self.compile_place(to)?; let stmt = Stmt::Mov { from, to }; self.stmts.push(stmt); - self.spans.push(p_stmt.span); } - _ => todo!("other stmt"), + StmtKind::Add { to, value } => { + let to = self.compile_place(to)?; + let value = self.compile_value(value)?; + let stmt = Stmt::Add { to, value }; + self.stmts.push(stmt); + } + StmtKind::Sub { to, value } => { + let to = self.compile_place(to)?; + let value = self.compile_value(value)?; + let stmt = Stmt::Sub { to, value }; + self.stmts.push(stmt); + } + StmtKind::Mul { to, value } => { + let to = self.compile_place(to)?; + let value = self.compile_value(value)?; + let stmt = Stmt::Mul { to, value }; + self.stmts.push(stmt); + } + StmtKind::Div { to, value } => { + let to = self.compile_place(to)?; + let value = self.compile_value(value)?; + let stmt = Stmt::Div { to, value }; + self.stmts.push(stmt); + } + StmtKind::Jmp { .. } => todo!("jmp"), + StmtKind::Je { .. } => todo!("je"), + StmtKind::Cmp { lhs, rhs } => { + let lhs = self.compile_value(lhs)?; + let rhs = self.compile_value(rhs)?; + let stmt = Stmt::Cmp { lhs, rhs }; + self.stmts.push(stmt); + } + StmtKind::Label { .. } => {} } - + self.spans.push(p_stmt.span); Ok(()) } diff --git a/src/main.rs b/src/main.rs index bc0ef07..c78c3fe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ -use std::io; +use std::{io, process}; + +use crate::error::CompilerError; mod error; mod ir; @@ -8,12 +10,15 @@ fn main() -> Result<(), io::Error> { let file = std::fs::read_to_string("./test.at")?; let result = parser::parse(&file); - match result { - Ok(ast) => { - dbg_pls::color!(ast); - } - Err(error) => error::report(error, "test.at", &file), - } + let ast = result.unwrap_or_else(|e| report_and_exit(&file, e)); + dbg_pls::color!(&ast); + let stmts = ir::compile(ast.into_iter()).unwrap_or_else(|e| report_and_exit(&file, e)); + dbg_pls::color!(stmts.0); Ok(()) } + +fn report_and_exit(file: &str, error: CompilerError) -> ! { + error::report(error, "test.at", &file); + process::exit(1); +} diff --git a/src/parser.rs b/src/parser.rs index 2ec952c..42dc1d1 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -88,7 +88,7 @@ pub enum ExprKind { Register(u8), Number(u64), Addr(Box), - Name(String), + Symbol(String), } struct Parser<'a, I> @@ -99,23 +99,6 @@ where } impl CompilerError { - fn new(msg: String, span: Span, notes: Vec<(String, Span)>, help: Option) -> Self { - Self { - msg, - span, - notes, - help, - } - } - - fn simple(msg: String, span: Span) -> Self { - Self::new_notes(msg, span, Vec::new()) - } - - fn new_notes(msg: String, span: Span, notes: Vec<(String, Span)>) -> Self { - Self::new(msg, span, notes, None) - } - fn not_allowed(span: Span, token: &str) -> Self { Self::simple(format!("`{token}` is not allowed here"), span) } @@ -260,7 +243,7 @@ where return Ok(expr(ExprKind::Register(n), span)); } } - expr(ExprKind::Name(name.to_owned()), span) + expr(ExprKind::Symbol(name.to_owned()), span) } Token::Mov => return Err(CompilerError::not_allowed(span, "mov")), Token::Jmp => return Err(CompilerError::not_allowed(span, "jmp")),