compile some things

This commit is contained in:
nora 2022-06-20 12:09:02 +02:00
parent 9253e6036c
commit 4037fadd51
4 changed files with 70 additions and 29 deletions

View file

@ -9,6 +9,27 @@ pub struct CompilerError {
pub notes: Vec<(String, Span)>,
pub help: Option<String>,
}
impl CompilerError {
pub fn new(msg: String, span: Span, notes: Vec<(String, Span)>, help: Option<String>) -> 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<T> = std::result::Result<T, CompilerError>;

View file

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

View file

@ -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);
}

View file

@ -88,7 +88,7 @@ pub enum ExprKind {
Register(u8),
Number(u64),
Addr(Box<Expr>),
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<String>) -> 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")),