This commit is contained in:
Lumi Kalt 2024-01-20 13:04:58 +00:00
commit eb23e06fec
9 changed files with 84 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "riscv_interpreter"
version = "0.1.0"

4
Cargo.toml Normal file
View file

@ -0,0 +1,4 @@
[package]
name = "riscv_interpreter"
version = "0.1.0"
edition = "2021"

3
src/env.rs Normal file
View file

@ -0,0 +1,3 @@
pub enum Env {}

39
src/err.rs Normal file
View file

@ -0,0 +1,39 @@
use std::fmt::{self, Display, Formatter};
pub enum SyntaxErr {
TraillingComma,
UnmatchedParen,
}
impl Display for SyntaxErr {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
SyntaxErr::TraillingComma => write!(f, "trailling comma"),
SyntaxErr::UnmatchedParen => write!(f, "unmatched parenthesis"),
}
}
}
pub enum RuntimeErr {
InvalidRegister(String),
UnexpectedImmediate,
UnexpectedRegister,
InvalidOp(String),
InvalidOpArity(String, usize, usize),
}
impl Display for RuntimeErr {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
RuntimeErr::InvalidRegister(reg) => write!(f, "invalid register {}", reg),
RuntimeErr::UnexpectedImmediate => write!(f, "unexpected immediate"),
RuntimeErr::UnexpectedRegister => write!(f, "unexpected register"),
RuntimeErr::InvalidOp(op) => write!(f, "invalid operation {}", op),
RuntimeErr::InvalidOpArity(op, expected, actual) => write!(
f,
"invalid operation arity {} expected {} got {}",
op, expected, actual
),
}
}
}

3
src/lib.rs Normal file
View file

@ -0,0 +1,3 @@
pub mod env;
pub mod err;
pub mod parser;

3
src/main.rs Normal file
View file

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

23
src/parser.rs Normal file
View file

@ -0,0 +1,23 @@
pub enum Expr {
/// 1, 2, -1
Immediate(i64),
/// zero, r1, pc
Register(String),
/// add, xor, j
Op(Vec<Expr>),
/// <label>:
LabelDef(String),
/// j <label>
LabelRef(String),
}
pub struct Location {
pub line: usize,
pub col: usize,
pub start: usize,
pub end: usize,
}
pub fn parse(input: &str) -> Result<Expr, ()> {
todo!()
}

1
test.s Normal file
View file

@ -0,0 +1 @@
add r1 r2 r1