This commit is contained in:
nora 2022-07-04 12:13:00 +02:00
parent 30735f6298
commit 8d03964f76
11 changed files with 60 additions and 46 deletions

View file

@ -3,12 +3,15 @@ use dbg_pls::DebugPls;
use logos::Span;
#[derive(Debug, DebugPls)]
// tag::error[]
pub struct CompilerError {
pub msg: String,
pub span: Span,
pub notes: Vec<(String, Span)>,
pub help: Option<String>,
}
// end::error[]
impl CompilerError {
pub fn new(msg: String, span: Span, notes: Vec<(String, Span)>, help: Option<String>) -> Self {
Self {

View file

@ -212,9 +212,7 @@ impl InterpretCtx {
fn write_addr(&mut self, addr: usize, value: u64) {
assert!(addr + 7 < self.memory.len());
let bytes = value.to_le_bytes();
for i in 0..8 {
self.memory[addr + i] = bytes[i];
}
self.memory[addr..(addr + 8)].copy_from_slice(&bytes[..8]);
}
fn reg(&self, reg: Register) -> u64 {

View file

@ -157,7 +157,7 @@ impl CompileCtx {
nested.span,
"save the first result in a temporary register".to_string(),
)),
ExprKind::Symbol(_) => return Err(CompilerError::simple(
ExprKind::Symbol(_) => Err(CompilerError::simple(
"symbol not allowed here".to_owned(),
expr.span,
))
@ -169,7 +169,7 @@ impl CompileCtx {
match expr.kind {
ExprKind::Number(n) => Ok(Value::Literal(n)),
ExprKind::Symbol(_) => {
return Err(CompilerError::simple(
Err(CompilerError::simple(
"symbol not allowed here".to_owned(),
expr.span,
))

View file

@ -1,3 +1,5 @@
#![forbid(unsafe_code)]
use std::{io, process};
use crate::error::CompilerError;
@ -22,6 +24,6 @@ fn main() -> Result<(), io::Error> {
}
fn report_and_exit(file: &str, error: CompilerError) -> ! {
error::report(error, "test.at", &file);
error::report(error, "test.at", file);
process::exit(1);
}

View file

@ -63,6 +63,7 @@ impl DebugPls for Stmt {
}
#[derive(Debug, PartialEq, Eq, DebugPls)]
// tag::stmt[]
pub enum StmtKind {
Mov { to: Expr, from: Expr },
Movb { to: Expr, from: Expr },
@ -76,6 +77,7 @@ pub enum StmtKind {
Cmp { lhs: Expr, rhs: Expr },
Label { name: String },
}
// end::stmt[]
#[derive(Debug, PartialEq, Eq)]
pub struct Expr {
@ -145,7 +147,7 @@ where
{
fn program(&mut self) -> Result<Vec<Stmt>> {
let mut stmts = Vec::new();
while let Ok(_) = self.peek() {
while self.peek().is_ok() {
let stmt = self.stmt()?;
stmts.push(stmt);
}
@ -220,7 +222,7 @@ where
}
Token::Label(name) => {
let name = name
.strip_suffix(":")
.strip_suffix(':')
.expect("lexer produced invalid label")
.to_owned();
stmt(span, StmtKind::Label { name })
@ -232,7 +234,7 @@ where
Token::Word(word) => {
return Err(CompilerError::new(
"{word}".to_string(),
span.clone(),
span,
vec![],
Some(format!("Consider using a label instead: `{}:`", word)),
))
@ -253,7 +255,7 @@ where
}
Token::Number(n) => expr(ExprKind::Number(n), span),
Token::Word(name) => {
if let Some(r_number) = name.strip_prefix("r") {
if let Some(r_number) = name.strip_prefix('r') {
if let Ok(n) = r_number.parse::<u8>() {
if n > 15 {
return Err(CompilerError::new(
@ -286,17 +288,15 @@ where
}
fn peek(&mut self) -> Result<&(Token<'a>, Span)> {
self.iter.peek().ok_or(CompilerError::eof())
self.iter.peek().ok_or_else(CompilerError::eof)
}
fn next(&mut self) -> Result<(Token<'a>, Span)> {
self.iter.next().ok_or(CompilerError::eof())
self.iter.next().ok_or_else(CompilerError::eof)
}
}
// tag::parse[]
pub fn parse(src: &str) -> Result<Vec<Stmt>> {
// end::parse[]
let lexer = lex(src).spanned();
let mut parser = Parser {
iter: lexer.peekable(),