Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 307b003e11 | |||
| 85211b127d | |||
| 02f258415d | |||
| 5eae0712bf | |||
| e28b3c4f37 | |||
| 9e3a642810 | |||
| e62121c75b | |||
| ffdce64df8 | |||
| d2daa7ae6d | |||
| abf9eb73c8 | |||
| 39b55b51da | |||
| 5bf989a640 | |||
| 3dacee0be4 | |||
| d035724d20 | |||
| 24f5aa30ea | |||
| 2a014fd210 | |||
| 788c4a8e82 | |||
| 7646177030 | |||
| b128b3357a | |||
| 4d5188d9d6 | |||
| e28a990b85 | |||
| 1f1f589dd4 | |||
| 6816392173 | |||
| 3c6fb5466e | |||
| 74dbf724a5 | |||
| 807482583a | |||
| 7b86fecc6f | |||
| 6b91264f84 | |||
| d9246c7ea1 | |||
| 1c4943828f |
56
README.md
56
README.md
@ -2,6 +2,56 @@
|
|||||||
|
|
||||||
## High level Components
|
## High level Components
|
||||||
|
|
||||||
- [ ] Lexer: Transforms text into Tokens
|
- [x] Lexer: Transforms text into Tokens
|
||||||
- [ ] Parser: Transforms Tokens into Abstract Syntax Tree
|
- [x] Parser: Transforms Tokens into Abstract Syntax Tree
|
||||||
- [ ] Interpreter (tree-walk-interpreter): Walks the tree and evaluates the expressions / statements
|
- [x] Interpreter (tree-walk-interpreter): Walks the tree and evaluates the expressions / statements
|
||||||
|
- [ ] Abstract Syntax Tree Optimizer
|
||||||
|
|
||||||
|
## Language features
|
||||||
|
|
||||||
|
- [x] Math expressions
|
||||||
|
- [x] Unary operators
|
||||||
|
- [x] Negate `-X`
|
||||||
|
- [x] Parentheses `(X+Y)*Z`
|
||||||
|
- [x] Logical boolean operators
|
||||||
|
- [x] Variables
|
||||||
|
- [x] Declaration
|
||||||
|
- [x] Assignment
|
||||||
|
- [x] While loop `while X { ... }`
|
||||||
|
- [x] If else statement `if X { ... } else { ... }`
|
||||||
|
- [x] If Statement
|
||||||
|
- [x] Else statement
|
||||||
|
- [ ] Line comments `//`
|
||||||
|
- [x] Strings
|
||||||
|
- [x] For loops `for X; Y; Z { ... }`
|
||||||
|
- [ ] IO Intrinsics
|
||||||
|
- [x] Print
|
||||||
|
- [ ] ReadLine
|
||||||
|
|
||||||
|
## Grammar
|
||||||
|
### Expressions
|
||||||
|
```
|
||||||
|
LITERAL = I64 | Str
|
||||||
|
expr_primary = LITERAL | IDENT | "(" expr ")" | "-" expr_primary
|
||||||
|
expr_mul = expr_primary (("*" | "/" | "%") expr_primary)*
|
||||||
|
expr_add = expr_mul (("+" | "-") expr_mul)*
|
||||||
|
expr_shift = expr_add ((">>" | "<<") expr_add)*
|
||||||
|
expr_rel = expr_shift ((">" | ">=" | "<" | "<=") expr_shift)*
|
||||||
|
expr_equ = expr_rel (("==" | "!=") expr_rel)*
|
||||||
|
expr_band = expr_equ ("&" expr_equ)*
|
||||||
|
expr_bxor = expr_band ("^" expr_band)*
|
||||||
|
expr_bor = expr_bxor ("|" expr_bxor)*
|
||||||
|
expr = expr_bor
|
||||||
|
```
|
||||||
|
|
||||||
|
## Statements
|
||||||
|
```
|
||||||
|
stmt_expr = expr
|
||||||
|
stmt_let = "let" IDENT "=" expr
|
||||||
|
stmt_while = "while" expr "{" (stmt)* "}"
|
||||||
|
stmt_for = "for" stmt_let ";" expr ";" expr "{" (stmt)* "}"
|
||||||
|
stmt_if = "if" expr "{" (stmt)* "}" ( "else" "{" (stmt)* "}" )
|
||||||
|
stmt_dbgprint = "$$" expr
|
||||||
|
stmt_print = "$" expr
|
||||||
|
stmt = stmt_expr | stmt_let | stmt_while | stmt_for | stmt_if | stmt_dbgprint | stmt_print
|
||||||
|
```
|
||||||
|
|||||||
104
src/ast.rs
Normal file
104
src/ast.rs
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
/// Types for binary operators
|
||||||
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||||
|
pub enum BinOpType {
|
||||||
|
/// Addition
|
||||||
|
Add,
|
||||||
|
|
||||||
|
/// Subtraction
|
||||||
|
Sub,
|
||||||
|
|
||||||
|
/// Multiplication
|
||||||
|
Mul,
|
||||||
|
|
||||||
|
/// Divide
|
||||||
|
Div,
|
||||||
|
|
||||||
|
/// Modulo
|
||||||
|
Mod,
|
||||||
|
|
||||||
|
/// Bitwise OR (inclusive or)
|
||||||
|
BOr,
|
||||||
|
|
||||||
|
/// Bitwise And
|
||||||
|
BAnd,
|
||||||
|
|
||||||
|
/// Bitwise Xor (exclusive or)
|
||||||
|
BXor,
|
||||||
|
|
||||||
|
/// Shift Left
|
||||||
|
Shl,
|
||||||
|
|
||||||
|
/// Shift Right
|
||||||
|
Shr,
|
||||||
|
|
||||||
|
/// Check equality
|
||||||
|
Equ,
|
||||||
|
|
||||||
|
/// Check unequality
|
||||||
|
Neq,
|
||||||
|
|
||||||
|
/// Check greater than
|
||||||
|
Gt,
|
||||||
|
|
||||||
|
/// Check greater or equal
|
||||||
|
Ge,
|
||||||
|
|
||||||
|
/// Check less than
|
||||||
|
Lt,
|
||||||
|
|
||||||
|
/// Check less or equal
|
||||||
|
Le,
|
||||||
|
|
||||||
|
/// Assign to a variable
|
||||||
|
Assign,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Types for unary operators
|
||||||
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||||
|
pub enum UnOpType {
|
||||||
|
/// Negation
|
||||||
|
Neg,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A full program abstract syntax tree. This consists of zero or more statements that represents
|
||||||
|
/// a program.
|
||||||
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||||
|
pub struct Ast {
|
||||||
|
pub prog: Vec<Stmt>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||||
|
pub enum Stmt {
|
||||||
|
/// Just a simple expression. This might be an assignment, a function call or a calculation.
|
||||||
|
Expr(Expr),
|
||||||
|
/// A variable declaration and assignment. (variable name, assigned value)
|
||||||
|
Let(String, Expr),
|
||||||
|
/// A while loop consisting of a condition and a body. (condition, body)
|
||||||
|
While(Expr, Ast),
|
||||||
|
/// A for loop consisting of an initialization declaration, a condition, an advancement and a
|
||||||
|
/// body. ((variable name, initial value), condition, advancement, body)
|
||||||
|
For((String, Expr), Expr, Expr, Ast),
|
||||||
|
/// If statement consisting of a condition, a true_body and a false_body.
|
||||||
|
/// (condition, true_body, false_body)
|
||||||
|
If(Expr, Ast, Ast),
|
||||||
|
/// Debug print the value of an expression (show the internal type together with the value)
|
||||||
|
DbgPrint(Expr),
|
||||||
|
/// Print the value of an expression
|
||||||
|
Print(Expr),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||||
|
pub enum Expr {
|
||||||
|
/// Integer literal (64-bit)
|
||||||
|
I64(i64),
|
||||||
|
/// String literal
|
||||||
|
Str(Rc<String>),
|
||||||
|
/// Identifier (variable name)
|
||||||
|
Ident(String),
|
||||||
|
/// Binary operation. Consists of type, left hand side and right hand side
|
||||||
|
BinOp(BinOpType, Box<Expr>, Box<Expr>),
|
||||||
|
/// Unary operation. Consists of type and the value that is operated on
|
||||||
|
UnOp(UnOpType, Box<Expr>),
|
||||||
|
}
|
||||||
197
src/bytecode.rs
Normal file
197
src/bytecode.rs
Normal file
@ -0,0 +1,197 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use crate::ast::{Ast, Expr, Stmt, BinOpType};
|
||||||
|
|
||||||
|
pub mod op {
|
||||||
|
type OpSize = u32;
|
||||||
|
|
||||||
|
pub const PUSH: OpSize = 0;
|
||||||
|
pub const POP: OpSize = 1;
|
||||||
|
pub const LOAD: OpSize = 2;
|
||||||
|
pub const STORE: OpSize = 3;
|
||||||
|
pub const ADD: OpSize = 4;
|
||||||
|
pub const SUB: OpSize = 5;
|
||||||
|
pub const MUL: OpSize = 6;
|
||||||
|
pub const DIV: OpSize = 7;
|
||||||
|
pub const MOD: OpSize = 8;
|
||||||
|
pub const BOR: OpSize = 9;
|
||||||
|
pub const BAND: OpSize = 10;
|
||||||
|
pub const BXOR: OpSize = 11;
|
||||||
|
pub const SHL: OpSize = 12;
|
||||||
|
pub const SHR: OpSize = 13;
|
||||||
|
pub const EQ: OpSize = 14;
|
||||||
|
pub const NEQ: OpSize = 15;
|
||||||
|
pub const GT: OpSize = 16;
|
||||||
|
pub const GE: OpSize = 17;
|
||||||
|
pub const LT: OpSize = 18;
|
||||||
|
pub const LE: OpSize = 19;
|
||||||
|
pub const JUMP: OpSize = 20;
|
||||||
|
pub const JUMP_TRUE: OpSize = 21;
|
||||||
|
pub const JUMP_FALSE: OpSize = 22;
|
||||||
|
pub const PRINT: OpSize = 23;
|
||||||
|
pub const DBG_PRINT: OpSize = 24;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct Compiler {
|
||||||
|
ops: Vec<u32>,
|
||||||
|
global_vars: HashMap<String, u16>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Compiler {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Compiler::default()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn compile(&mut self, ast: &Ast) {
|
||||||
|
for stmt in &ast.prog {
|
||||||
|
match stmt {
|
||||||
|
Stmt::Expr(expr) => {
|
||||||
|
self.compile_expr(expr);
|
||||||
|
self.ops.push(op::POP);
|
||||||
|
}
|
||||||
|
Stmt::Let(name, rhs) => {
|
||||||
|
let id = self.global_vars.len() as u16;
|
||||||
|
self.global_vars.insert(name.clone(), id);
|
||||||
|
|
||||||
|
self.compile_expr(rhs);
|
||||||
|
self.gen_store(id);
|
||||||
|
}
|
||||||
|
Stmt::While(cond, body) => {
|
||||||
|
self.compile_expr(cond);
|
||||||
|
|
||||||
|
self.ops.push(op::JUMP_FALSE);
|
||||||
|
let idx_jmp = self.ops.len();
|
||||||
|
self.gen_i64(0);
|
||||||
|
|
||||||
|
let idx_start = self.ops.len();
|
||||||
|
|
||||||
|
self.compile(body);
|
||||||
|
|
||||||
|
// check condition before loop jump
|
||||||
|
self.compile_expr(cond);
|
||||||
|
|
||||||
|
self.ops.push(op::JUMP_TRUE);
|
||||||
|
self.gen_i64(idx_start as i64);
|
||||||
|
|
||||||
|
self.overwrite_i64(idx_jmp, self.ops.len() as i64);
|
||||||
|
}
|
||||||
|
Stmt::For(_, _, _, _) => todo!(),
|
||||||
|
Stmt::If(cond, if_block, else_block) => {
|
||||||
|
self.compile_expr(cond);
|
||||||
|
|
||||||
|
self.ops.push(op::JUMP_FALSE);
|
||||||
|
let idx_if = self.ops.len();
|
||||||
|
self.gen_i64(0);
|
||||||
|
|
||||||
|
self.compile(if_block);
|
||||||
|
|
||||||
|
self.ops.push(op::JUMP);
|
||||||
|
let idx_else = self.ops.len();
|
||||||
|
self.gen_i64(0);
|
||||||
|
|
||||||
|
self.overwrite_i64(idx_if, self.ops.len() as i64);
|
||||||
|
|
||||||
|
self.compile(else_block);
|
||||||
|
|
||||||
|
self.overwrite_i64(idx_else, self.ops.len() as i64);
|
||||||
|
|
||||||
|
},
|
||||||
|
Stmt::DbgPrint(expr) => {
|
||||||
|
self.compile_expr(expr);
|
||||||
|
self.ops.push(op::DBG_PRINT);
|
||||||
|
}
|
||||||
|
Stmt::Print(expr) => {
|
||||||
|
self.compile_expr(expr);
|
||||||
|
self.ops.push(op::PRINT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn into_ops(self) -> Vec<u32> {
|
||||||
|
self.ops
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn compile_expr(&mut self, expr: &Expr) {
|
||||||
|
match expr {
|
||||||
|
Expr::I64(val) => {
|
||||||
|
self.ops.push(op::PUSH);
|
||||||
|
self.gen_i64(*val)
|
||||||
|
}
|
||||||
|
Expr::Ident(name) => {
|
||||||
|
match self.global_vars.get(name).copied() {
|
||||||
|
Some(addr) => self.gen_load(addr),
|
||||||
|
None => panic!("Variable '{}' used before declaration", name),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Expr::BinOp(bo, lhs, rhs) => self.compile_binop(bo, lhs, rhs),
|
||||||
|
Expr::UnOp(_, _) => todo!(),
|
||||||
|
Expr::Str(_) => todo!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compile_binop(&mut self, bo: &BinOpType, lhs: &Expr, rhs: &Expr) {
|
||||||
|
|
||||||
|
if matches!(bo, BinOpType::Assign) {
|
||||||
|
self.compile_expr(rhs);
|
||||||
|
|
||||||
|
if let Expr::Ident(name) = lhs {
|
||||||
|
let addr = *self.global_vars.get(name).expect("Trying to assign var before decl");
|
||||||
|
self.gen_store(addr);
|
||||||
|
} else {
|
||||||
|
panic!("Trying to assign value to rvalue");
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.compile_expr(lhs);
|
||||||
|
self.compile_expr(rhs);
|
||||||
|
|
||||||
|
match bo {
|
||||||
|
BinOpType::Add => self.ops.push(op::ADD),
|
||||||
|
BinOpType::Sub => self.ops.push(op::SUB),
|
||||||
|
BinOpType::Mul => self.ops.push(op::MUL),
|
||||||
|
BinOpType::Div => self.ops.push(op::DIV),
|
||||||
|
BinOpType::Mod => self.ops.push(op::MOD),
|
||||||
|
BinOpType::BOr => self.ops.push(op::BOR),
|
||||||
|
BinOpType::BAnd => self.ops.push(op::BAND),
|
||||||
|
BinOpType::BXor => self.ops.push(op::BXOR),
|
||||||
|
BinOpType::Shl => self.ops.push(op::SHL),
|
||||||
|
BinOpType::Shr => self.ops.push(op::SHR),
|
||||||
|
BinOpType::Equ => self.ops.push(op::EQ),
|
||||||
|
BinOpType::Neq => self.ops.push(op::NEQ),
|
||||||
|
BinOpType::Gt => self.ops.push(op::GT),
|
||||||
|
BinOpType::Ge => self.ops.push(op::GE),
|
||||||
|
BinOpType::Lt => self.ops.push(op::LT),
|
||||||
|
BinOpType::Le => self.ops.push(op::LE),
|
||||||
|
BinOpType::Assign => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_i64(&mut self, val: i64) {
|
||||||
|
self.ops.push((val & u32::MAX as i64) as u32);
|
||||||
|
self.ops.push((val >> 32) as u32);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn overwrite_i64(&mut self, idx: usize, val: i64) {
|
||||||
|
self.ops[idx] = (val & u32::MAX as i64) as u32;
|
||||||
|
self.ops[idx+1] = (val >> 32) as u32;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_load(&mut self, addr: u16) {
|
||||||
|
self.ops.push(op::LOAD | (addr << 8) as u32);
|
||||||
|
// self.gen_i64(addr as i64)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_store(&mut self, addr: u16) {
|
||||||
|
self.ops.push(op::STORE | (addr << 8) as u32);
|
||||||
|
// self.gen_i64(addr as i64)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn compile(ast: &Ast) -> Vec<u32> {
|
||||||
|
let mut compiler = Compiler::new();
|
||||||
|
compiler.compile(ast);
|
||||||
|
compiler.into_ops()
|
||||||
|
}
|
||||||
@ -1,33 +1,132 @@
|
|||||||
use crate::parser::{Ast, BinOpType};
|
use std::{collections::HashMap, fmt::Display, rc::Rc};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
ast::{Ast, BinOpType, Expr, Stmt, UnOpType},
|
||||||
|
lexer::lex,
|
||||||
|
parser::parse,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||||
pub enum Value {
|
pub enum Value {
|
||||||
I64(i64),
|
I64(i64),
|
||||||
|
Str(Rc<String>),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Interpreter {
|
pub struct Interpreter {
|
||||||
// Runtime storage, for example variables ...
|
/// The variable table maps all variables by their names to their values
|
||||||
|
vartable: HashMap<String, Value>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Interpreter {
|
impl Interpreter {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {}
|
let vartable = HashMap::new();
|
||||||
|
Self { vartable }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(&mut self, prog: Ast) {
|
pub fn run_text(&mut self, code: &str, print_tokens: bool, print_ast: bool) {
|
||||||
let result = self.resolve_expr(prog);
|
let tokens = lex(code);
|
||||||
|
if print_tokens {
|
||||||
println!("Result = {:?}", result);
|
println!("Tokens: {:?}", tokens);
|
||||||
|
}
|
||||||
|
let ast = parse(tokens);
|
||||||
|
if print_ast {
|
||||||
|
println!("Ast:\n{:#?}", ast);
|
||||||
|
}
|
||||||
|
self.run(&ast);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resolve_expr(&mut self, expr: Ast) -> Value {
|
pub fn run(&mut self, prog: &Ast) {
|
||||||
match expr {
|
for stmt in &prog.prog {
|
||||||
Ast::I64(val) => Value::I64(val),
|
match stmt {
|
||||||
Ast::BinOp(bo, lhs, rhs) => self.resolve_binop(bo, *lhs, *rhs),
|
Stmt::Expr(expr) => {
|
||||||
|
self.resolve_expr(expr);
|
||||||
|
}
|
||||||
|
Stmt::DbgPrint(expr) => {
|
||||||
|
let result = self.resolve_expr(expr);
|
||||||
|
println!("{:?}", result);
|
||||||
|
}
|
||||||
|
Stmt::Print(expr) => {
|
||||||
|
let result = self.resolve_expr(expr);
|
||||||
|
print!("{}", result);
|
||||||
|
}
|
||||||
|
Stmt::Let(name, rhs) => {
|
||||||
|
let result = self.resolve_expr(rhs);
|
||||||
|
self.vartable.insert(name.clone(), result);
|
||||||
|
}
|
||||||
|
Stmt::For(init, condition, advance, body) => {
|
||||||
|
// Execute initital let instruction
|
||||||
|
let init_val = self.resolve_expr(&init.1);
|
||||||
|
self.vartable.insert(init.0.clone(), init_val);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
// Check condition
|
||||||
|
match self.resolve_expr(condition) {
|
||||||
|
Value::I64(val) if val == 0 => break,
|
||||||
|
Value::I64(_) => (),
|
||||||
|
|
||||||
|
Value::Str(text) if text.is_empty() => break,
|
||||||
|
Value::Str(_) => (),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute loop body
|
||||||
|
self.run(body);
|
||||||
|
|
||||||
|
// Execute advancement
|
||||||
|
self.resolve_expr(advance);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Stmt::While(condition, body) => {
|
||||||
|
loop {
|
||||||
|
// Check condition
|
||||||
|
match self.resolve_expr(condition) {
|
||||||
|
Value::I64(val) if val == 0 => break,
|
||||||
|
Value::I64(_) => (),
|
||||||
|
|
||||||
|
Value::Str(text) if text.is_empty() => break,
|
||||||
|
Value::Str(_) => (),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute loop body
|
||||||
|
self.run(body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Stmt::If(condition, body_if, body_else) => {
|
||||||
|
if matches!(self.resolve_expr(condition), Value::I64(0)) {
|
||||||
|
self.run(body_else);
|
||||||
|
} else {
|
||||||
|
self.run(body_if);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resolve_binop(&mut self, bo: BinOpType, lhs: Ast, rhs: Ast) -> Value {
|
fn resolve_expr(&mut self, expr: &Expr) -> Value {
|
||||||
|
match expr {
|
||||||
|
Expr::I64(val) => Value::I64(*val),
|
||||||
|
Expr::Str(name) => Value::Str(name.clone()),
|
||||||
|
Expr::BinOp(bo, lhs, rhs) => self.resolve_binop(bo, &lhs, &rhs),
|
||||||
|
Expr::UnOp(uo, val) => self.resolve_unop(uo, &val),
|
||||||
|
Expr::Ident(name) => match self.vartable.get(name) {
|
||||||
|
None => panic!("Runtime error: Use of undeclared variable '{}'", name),
|
||||||
|
Some(val) => val.clone(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resolve_binop(&mut self, bo: &BinOpType, lhs: &Expr, rhs: &Expr) -> Value {
|
||||||
|
// Treat assignment separate from the other expressions
|
||||||
|
if matches!(bo, BinOpType::Assign) {
|
||||||
|
match lhs {
|
||||||
|
Expr::Ident(name) => {
|
||||||
|
let rhs = self.resolve_expr(rhs);
|
||||||
|
self.vartable.get_mut(name).map(|var| *var = rhs.clone());
|
||||||
|
return rhs;
|
||||||
|
}
|
||||||
|
_ => panic!("Runtime error: Left hand side of assignment must be an identifier"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let lhs = self.resolve_expr(lhs);
|
let lhs = self.resolve_expr(lhs);
|
||||||
let rhs = self.resolve_expr(rhs);
|
let rhs = self.resolve_expr(rhs);
|
||||||
|
|
||||||
@ -35,36 +134,70 @@ impl Interpreter {
|
|||||||
(Value::I64(lhs), Value::I64(rhs)) => match bo {
|
(Value::I64(lhs), Value::I64(rhs)) => match bo {
|
||||||
BinOpType::Add => Value::I64(lhs + rhs),
|
BinOpType::Add => Value::I64(lhs + rhs),
|
||||||
BinOpType::Mul => Value::I64(lhs * rhs),
|
BinOpType::Mul => Value::I64(lhs * rhs),
|
||||||
|
BinOpType::Sub => Value::I64(lhs - rhs),
|
||||||
|
BinOpType::Div => Value::I64(lhs / rhs),
|
||||||
|
BinOpType::Mod => Value::I64(lhs % rhs),
|
||||||
|
BinOpType::BOr => Value::I64(lhs | rhs),
|
||||||
|
BinOpType::BAnd => Value::I64(lhs & rhs),
|
||||||
|
BinOpType::BXor => Value::I64(lhs ^ rhs),
|
||||||
|
BinOpType::Shr => Value::I64(lhs >> rhs),
|
||||||
|
BinOpType::Shl => Value::I64(lhs << rhs),
|
||||||
|
BinOpType::Equ => Value::I64(if lhs == rhs { 1 } else { 0 }),
|
||||||
|
BinOpType::Neq => Value::I64(if lhs != rhs { 1 } else { 0 }),
|
||||||
|
BinOpType::Gt => Value::I64(if lhs > rhs { 1 } else { 0 }),
|
||||||
|
BinOpType::Ge => Value::I64(if lhs >= rhs { 1 } else { 0 }),
|
||||||
|
BinOpType::Lt => Value::I64(if lhs < rhs { 1 } else { 0 }),
|
||||||
|
BinOpType::Le => Value::I64(if lhs <= rhs { 1 } else { 0 }),
|
||||||
|
BinOpType::Assign => unreachable!(),
|
||||||
},
|
},
|
||||||
// _ => panic!("Value types are not compatible"),
|
_ => panic!("Value types are not compatible"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resolve_unop(&mut self, uo: &UnOpType, val: &Expr) -> Value {
|
||||||
|
let val = self.resolve_expr(val);
|
||||||
|
match val {
|
||||||
|
Value::I64(val) => match uo {
|
||||||
|
UnOpType::Neg => Value::I64(-val),
|
||||||
|
},
|
||||||
|
_ => panic!("Invalid unary operation for type"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for Value {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Value::I64(val) => write!(f, "{}", val),
|
||||||
|
Value::Str(text) => write!(f, "{}", text),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use crate::parser::{Ast, BinOpType};
|
|
||||||
use super::{Interpreter, Value};
|
use super::{Interpreter, Value};
|
||||||
|
use crate::ast::{BinOpType, Expr};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_interpreter_expr() {
|
fn test_interpreter_expr() {
|
||||||
// Expression: 1 + 2 * 3 + 4
|
// Expression: 1 + 2 * 3 + 4
|
||||||
// With precedence: (1 + (2 * 3)) + 4
|
// With precedence: (1 + (2 * 3)) + 4
|
||||||
let ast = Ast::BinOp(
|
let ast = Expr::BinOp(
|
||||||
BinOpType::Add,
|
BinOpType::Add,
|
||||||
Ast::BinOp(
|
Expr::BinOp(
|
||||||
BinOpType::Add,
|
BinOpType::Add,
|
||||||
Ast::I64(1).into(),
|
Expr::I64(1).into(),
|
||||||
Ast::BinOp(BinOpType::Mul, Ast::I64(2).into(), Ast::I64(3).into()).into(),
|
Expr::BinOp(BinOpType::Mul, Expr::I64(2).into(), Expr::I64(3).into()).into(),
|
||||||
)
|
)
|
||||||
.into(),
|
.into(),
|
||||||
Ast::I64(4).into(),
|
Expr::I64(4).into(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let expected = Value::I64(11);
|
let expected = Value::I64(11);
|
||||||
|
|
||||||
let mut interpreter = Interpreter::new();
|
let mut interpreter = Interpreter::new();
|
||||||
let actual = interpreter.resolve_expr(ast);
|
let actual = interpreter.resolve_expr(&ast);
|
||||||
|
|
||||||
assert_eq!(expected, actual);
|
assert_eq!(expected, actual);
|
||||||
}
|
}
|
||||||
|
|||||||
240
src/lexer.rs
240
src/lexer.rs
@ -1,20 +1,11 @@
|
|||||||
use std::{iter::Peekable, str::Chars};
|
use std::{iter::Peekable, str::Chars};
|
||||||
|
|
||||||
use crate::parser::BinOpType;
|
use crate::token::{Keyword, Literal, Token};
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
/// Lex the provided code into a Token Buffer
|
||||||
pub enum Token {
|
pub fn lex(code: &str) -> Vec<Token> {
|
||||||
/// Integer literal (64-bit)
|
let mut lexer = Lexer::new(code);
|
||||||
I64(i64),
|
lexer.lex()
|
||||||
|
|
||||||
/// Plus (+)
|
|
||||||
Add,
|
|
||||||
|
|
||||||
/// Asterisk (*)
|
|
||||||
Mul,
|
|
||||||
|
|
||||||
/// End of file
|
|
||||||
EoF,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Lexer<'a> {
|
struct Lexer<'a> {
|
||||||
@ -27,87 +18,218 @@ impl<'a> Lexer<'a> {
|
|||||||
Self { code }
|
Self { code }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Advance to next character and return the removed char. If there is no next char, '\0'
|
||||||
|
/// is returned.
|
||||||
|
fn next(&mut self) -> char {
|
||||||
|
self.code.next().unwrap_or('\0')
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the next character without removing it. If there is no next char, '\0' is returned.
|
||||||
|
fn peek(&mut self) -> char {
|
||||||
|
self.code.peek().copied().unwrap_or('\0')
|
||||||
|
}
|
||||||
|
|
||||||
fn lex(&mut self) -> Vec<Token> {
|
fn lex(&mut self) -> Vec<Token> {
|
||||||
let mut tokens = Vec::new();
|
let mut tokens = Vec::new();
|
||||||
|
|
||||||
while let Some(ch) = self.next() {
|
loop {
|
||||||
match ch {
|
match self.next() {
|
||||||
|
// End of text
|
||||||
|
'\0' => break,
|
||||||
|
|
||||||
// Skip whitespace
|
// Skip whitespace
|
||||||
' ' => (),
|
' ' | '\r' | '\n' | '\t' => (),
|
||||||
|
|
||||||
// Lex numbers
|
// Handle tokens that span two characters
|
||||||
'0'..='9' => {
|
'>' if matches!(self.peek(), '>') => {
|
||||||
let mut sval = String::from(ch);
|
self.next();
|
||||||
|
tokens.push(Token::Shr);
|
||||||
// Do as long as a next char exists and it is a numeric char
|
}
|
||||||
while let Some('0'..='9') = self.peek() {
|
'<' if matches!(self.peek(), '<') => {
|
||||||
// The next char is verified to be Some, so unwrap is safe
|
self.next();
|
||||||
sval.push(self.next().unwrap());
|
tokens.push(Token::Shl);
|
||||||
}
|
}
|
||||||
|
'=' if matches!(self.peek(), '=') => {
|
||||||
// TODO: We only added numeric chars to the string, but the conversion could still fail
|
self.next();
|
||||||
tokens.push(Token::I64(sval.parse().unwrap()));
|
tokens.push(Token::Equ);
|
||||||
|
}
|
||||||
|
'!' if matches!(self.peek(), '=') => {
|
||||||
|
self.next();
|
||||||
|
tokens.push(Token::Neq);
|
||||||
|
}
|
||||||
|
'<' if matches!(self.peek(), '=') => {
|
||||||
|
self.next();
|
||||||
|
tokens.push(Token::Le);
|
||||||
|
}
|
||||||
|
'>' if matches!(self.peek(), '=') => {
|
||||||
|
self.next();
|
||||||
|
tokens.push(Token::Ge);
|
||||||
|
}
|
||||||
|
'$' if matches!(self.peek(), '$') => {
|
||||||
|
self.next();
|
||||||
|
tokens.push(Token::DoubleDollar);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle tokens that span one character
|
||||||
'+' => tokens.push(Token::Add),
|
'+' => tokens.push(Token::Add),
|
||||||
|
'-' => tokens.push(Token::Sub),
|
||||||
'*' => tokens.push(Token::Mul),
|
'*' => tokens.push(Token::Mul),
|
||||||
|
'/' => tokens.push(Token::Div),
|
||||||
|
'%' => tokens.push(Token::Mod),
|
||||||
|
'|' => tokens.push(Token::BOr),
|
||||||
|
'&' => tokens.push(Token::BAnd),
|
||||||
|
'^' => tokens.push(Token::BXor),
|
||||||
|
'(' => tokens.push(Token::LParen),
|
||||||
|
')' => tokens.push(Token::RParen),
|
||||||
|
'<' => tokens.push(Token::Lt),
|
||||||
|
'>' => tokens.push(Token::Gt),
|
||||||
|
'=' => tokens.push(Token::Assign),
|
||||||
|
';' => tokens.push(Token::Semicolon),
|
||||||
|
'{' => tokens.push(Token::LBrace),
|
||||||
|
'}' => tokens.push(Token::RBrace),
|
||||||
|
'$' => tokens.push(Token::Dollar),
|
||||||
|
|
||||||
//TODO: Don't panic, keep calm
|
// Handle special multicharacter tokens
|
||||||
_ => panic!("Lexer encountered unexpected char: '{}'", ch),
|
|
||||||
|
// Lex numbers
|
||||||
|
ch @ '0'..='9' => tokens.push(self.lex_number(ch)),
|
||||||
|
|
||||||
|
// Lex strings
|
||||||
|
'"' => tokens.push(self.lex_string()),
|
||||||
|
|
||||||
|
// Lex identifiers
|
||||||
|
ch @ ('a'..='z' | 'A'..='Z' | '_') => tokens.push(self.lex_ident(ch)),
|
||||||
|
|
||||||
|
// Any other character is unexpected
|
||||||
|
ch => panic!("Lexer encountered unexpected char: '{}'", ch),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tokens
|
tokens
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Advance to next character and return the removed char
|
fn lex_number(&mut self, first_char: char) -> Token {
|
||||||
fn next(&mut self) -> Option<char> {
|
let mut sval = String::from(first_char);
|
||||||
self.code.next()
|
|
||||||
|
// Do as long as a next char exists and it is a numeric char
|
||||||
|
loop {
|
||||||
|
// The next char is verified to be Some, so unwrap is safe
|
||||||
|
match self.peek() {
|
||||||
|
// Underscore is a separator, so remove it but don't add to number
|
||||||
|
'_' => {
|
||||||
|
self.next();
|
||||||
|
}
|
||||||
|
'0'..='9' => {
|
||||||
|
sval.push(self.next());
|
||||||
|
}
|
||||||
|
// Next char is not a number, so stop and finish the number token
|
||||||
|
_ => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: We only added numeric chars to the string, but the conversion could still fail
|
||||||
|
Token::Literal(Literal::I64(sval.parse().unwrap()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the next character without removing it
|
/// Lex an identifier from the character stream. The first char has to have been consumed
|
||||||
fn peek(&mut self) -> Option<char> {
|
/// from the stream already and is passed as an argument instead.
|
||||||
self.code.peek().copied()
|
fn lex_ident(&mut self, first_char: char) -> Token {
|
||||||
|
let mut ident = String::from(first_char);
|
||||||
|
|
||||||
|
// Do as long as a next char exists and it is a valid ident char
|
||||||
|
while let 'a'..='z' | 'A'..='Z' | '_' | '0'..='9' = self.peek() {
|
||||||
|
// The next char is verified to be Some, so unwrap is safe
|
||||||
|
ident.push(self.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the identifier is a keyword
|
||||||
|
match ident.as_str() {
|
||||||
|
"true" => Token::Literal(Literal::I64(1)),
|
||||||
|
"false" => Token::Literal(Literal::I64(0)),
|
||||||
|
"let" => Token::Keyword(Keyword::Let),
|
||||||
|
"while" => Token::Keyword(Keyword::While),
|
||||||
|
"if" => Token::Keyword(Keyword::If),
|
||||||
|
"else" => Token::Keyword(Keyword::Else),
|
||||||
|
"for" => Token::Keyword(Keyword::For),
|
||||||
|
|
||||||
|
_ => Token::Ident(ident),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Lex the provided code into a Token Buffer
|
/// Lex a string token from the character stream. This requires the initial quote '"' to be
|
||||||
///
|
/// consumed before.
|
||||||
/// TODO: Don't panic and implement error handling using Result
|
fn lex_string(&mut self) -> Token {
|
||||||
pub fn lex(code: &str) -> Vec<Token> {
|
let mut text = String::new();
|
||||||
let mut lexer = Lexer::new(code);
|
|
||||||
lexer.lex()
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Token {
|
let mut escape = false;
|
||||||
pub fn try_to_binop(&self) -> Option<BinOpType> {
|
|
||||||
Some(match self {
|
// Do as long as a next char exists and it is not '"'
|
||||||
Token::Add => BinOpType::Add,
|
loop {
|
||||||
Token::Mul => BinOpType::Mul,
|
if escape {
|
||||||
_ => return None,
|
escape = false;
|
||||||
})
|
|
||||||
|
// Escape characters
|
||||||
|
match self.next() {
|
||||||
|
'\\' => text.push('\\'),
|
||||||
|
'n' => text.push('\n'),
|
||||||
|
'r' => text.push('\r'),
|
||||||
|
't' => text.push('\t'),
|
||||||
|
ch => panic!("Invalid string escape: '{:?}'", ch),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
match self.peek() {
|
||||||
|
// Doublequote '"' ends the string lexing
|
||||||
|
'"' => {
|
||||||
|
self.next();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// Backslash '\' escapes the next character
|
||||||
|
'\\' => {
|
||||||
|
self.next();
|
||||||
|
escape = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reached end of text but didn't encounter closing doublequote '"'
|
||||||
|
'\0' => panic!("String is never terminated (missing '\"')"),
|
||||||
|
|
||||||
|
_ => text.push(self.next()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Token::Literal(Literal::Str(text))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use crate::token::Literal;
|
||||||
|
|
||||||
use super::{lex, Token};
|
use super::{lex, Token};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_lexer() {
|
fn test_lexer() {
|
||||||
let code = "33 +5*2 + 4456467*2334+3";
|
let code = "33 +5*2 + 4456467*2334+3 % - / << ^ | & >>";
|
||||||
let expected = vec![
|
let expected = vec![
|
||||||
Token::I64(33),
|
Token::Literal(Literal::I64(33)),
|
||||||
Token::Add,
|
Token::Add,
|
||||||
Token::I64(5),
|
Token::Literal(Literal::I64(5)),
|
||||||
Token::Mul,
|
Token::Mul,
|
||||||
Token::I64(2),
|
Token::Literal(Literal::I64(2)),
|
||||||
Token::Add,
|
Token::Add,
|
||||||
Token::I64(4456467),
|
Token::Literal(Literal::I64(4456467)),
|
||||||
Token::Mul,
|
Token::Mul,
|
||||||
Token::I64(2334),
|
Token::Literal(Literal::I64(2334)),
|
||||||
Token::Add,
|
Token::Add,
|
||||||
Token::I64(3),
|
Token::Literal(Literal::I64(3)),
|
||||||
|
Token::Mod,
|
||||||
|
Token::Sub,
|
||||||
|
Token::Div,
|
||||||
|
Token::Shl,
|
||||||
|
Token::BXor,
|
||||||
|
Token::BOr,
|
||||||
|
Token::BAnd,
|
||||||
|
Token::Shr,
|
||||||
];
|
];
|
||||||
|
|
||||||
let actual = lex(code);
|
let actual = lex(code);
|
||||||
|
|||||||
@ -1,3 +1,7 @@
|
|||||||
pub mod lexer;
|
pub mod lexer;
|
||||||
pub mod parser;
|
pub mod parser;
|
||||||
pub mod interpreter;
|
pub mod interpreter;
|
||||||
|
pub mod token;
|
||||||
|
pub mod ast;
|
||||||
|
pub mod bytecode;
|
||||||
|
pub mod vm;
|
||||||
|
|||||||
68
src/main.rs
68
src/main.rs
@ -1,23 +1,63 @@
|
|||||||
use nek_lang::{lexer::lex, parser::parse, interpreter::Interpreter};
|
use std::{env::args, io::Write};
|
||||||
|
|
||||||
|
use nek_lang::{interpreter::Interpreter, lexer::lex, parser::parse, bytecode::compile, vm::Vm};
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
struct CliConfig {
|
||||||
|
print_tokens: bool,
|
||||||
|
print_ast: bool,
|
||||||
|
interactive: bool,
|
||||||
|
file: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let mut cfg = CliConfig::default();
|
||||||
|
|
||||||
let mut code = String::new();
|
for arg in args().skip(1) {
|
||||||
|
match arg.as_str() {
|
||||||
std::io::stdin().read_line(&mut code).unwrap();
|
"--tokens" | "-t" => cfg.print_tokens = true,
|
||||||
let code = code.trim();
|
"--ast" | "-a" => cfg.print_ast = true,
|
||||||
|
"--interactive" | "-i" => cfg.interactive = true,
|
||||||
let tokens = lex(&code);
|
file if cfg.file.is_none() => cfg.file = Some(file.to_string()),
|
||||||
|
_ => panic!("Invalid argument: '{}'", arg),
|
||||||
println!("Tokens: {:?}\n", tokens);
|
}
|
||||||
|
}
|
||||||
let ast = parse(tokens);
|
|
||||||
|
|
||||||
println!("Ast: {:#?}\n", ast);
|
|
||||||
|
|
||||||
let mut interpreter = Interpreter::new();
|
let mut interpreter = Interpreter::new();
|
||||||
|
|
||||||
interpreter.run(ast);
|
if let Some(file) = &cfg.file {
|
||||||
|
let code = std::fs::read_to_string(file).expect(&format!("File not found: '{}'", file));
|
||||||
|
let tokens = lex(&code);
|
||||||
|
let ast = parse(tokens);
|
||||||
|
|
||||||
|
let prog = compile(&ast);
|
||||||
|
|
||||||
|
// println!("{:?}", prog);
|
||||||
|
|
||||||
|
let mut vm = Vm::new(prog);
|
||||||
|
|
||||||
|
vm.run();
|
||||||
|
|
||||||
|
// interpreter.run_text(&code, cfg.print_tokens, cfg.print_ast);
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.interactive || cfg.file.is_none() {
|
||||||
|
|
||||||
|
let mut code = String::new();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
print!(">> ");
|
||||||
|
std::io::stdout().flush().unwrap();
|
||||||
|
|
||||||
|
code.clear();
|
||||||
|
std::io::stdin().read_line(&mut code).unwrap();
|
||||||
|
let code = code.trim();
|
||||||
|
|
||||||
|
if code == "exit" {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
interpreter.run_text(&code, cfg.print_tokens, cfg.print_ast);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
268
src/parser.rs
268
src/parser.rs
@ -1,24 +1,9 @@
|
|||||||
use std::iter::Peekable;
|
use std::iter::Peekable;
|
||||||
|
|
||||||
use crate::lexer::Token;
|
use crate::{
|
||||||
|
ast::{Ast, BinOpType, Expr, Stmt, UnOpType},
|
||||||
/// Types for binary operators
|
token::{Keyword, Literal, Token},
|
||||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
};
|
||||||
pub enum BinOpType {
|
|
||||||
/// Addition
|
|
||||||
Add,
|
|
||||||
|
|
||||||
/// Multiplication
|
|
||||||
Mul,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
|
||||||
pub enum Ast {
|
|
||||||
/// Integer literal (64-bit)
|
|
||||||
I64(i64),
|
|
||||||
/// Binary operation. Consists of type, left hand side and right hand side
|
|
||||||
BinOp(BinOpType, Box<Ast>, Box<Ast>),
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Parser<T: Iterator<Item = Token>> {
|
struct Parser<T: Iterator<Item = Token>> {
|
||||||
tokens: Peekable<T>,
|
tokens: Peekable<T>,
|
||||||
@ -31,18 +16,173 @@ impl<T: Iterator<Item = Token>> Parser<T> {
|
|||||||
Self { tokens }
|
Self { tokens }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse(&mut self) -> Ast {
|
/// Get the next Token without removing it
|
||||||
self.parse_expr()
|
fn peek(&mut self) -> &Token {
|
||||||
|
self.tokens.peek().unwrap_or(&Token::EoF)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_expr(&mut self) -> Ast {
|
/// Advance to next Token and return the removed Token
|
||||||
|
fn next(&mut self) -> Token {
|
||||||
|
self.tokens.next().unwrap_or(Token::EoF)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse(&mut self) -> Ast {
|
||||||
|
let mut prog = Vec::new();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let stmt = match self.peek() {
|
||||||
|
Token::Semicolon => {
|
||||||
|
self.next();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Token::EoF => break,
|
||||||
|
Token::RBrace => break,
|
||||||
|
|
||||||
|
Token::Keyword(keyword) => match keyword {
|
||||||
|
Keyword::Let => self.parse_let_stmt(),
|
||||||
|
Keyword::While => self.parse_while(),
|
||||||
|
Keyword::If => self.parse_if(),
|
||||||
|
Keyword::For => self.parse_for(),
|
||||||
|
Keyword::Else => panic!("Unexpected else keyword"),
|
||||||
|
},
|
||||||
|
|
||||||
|
Token::Dollar => {
|
||||||
|
self.next();
|
||||||
|
Stmt::Print(self.parse_expr())
|
||||||
|
}
|
||||||
|
Token::DoubleDollar => {
|
||||||
|
self.next();
|
||||||
|
Stmt::DbgPrint(self.parse_expr())
|
||||||
|
}
|
||||||
|
// By default try to parse an expression
|
||||||
|
_ => Stmt::Expr(self.parse_expr()),
|
||||||
|
};
|
||||||
|
|
||||||
|
prog.push(stmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ast { prog }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_for(&mut self) -> Stmt {
|
||||||
|
if !matches!(self.next(), Token::Keyword(Keyword::For)) {
|
||||||
|
panic!("Error parsing for: Expected for token");
|
||||||
|
}
|
||||||
|
|
||||||
|
let init = match self.parse_let_stmt() {
|
||||||
|
Stmt::Let(name, rhs) => (name, rhs),
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
|
||||||
|
if !matches!(self.next(), Token::Semicolon) {
|
||||||
|
panic!("Error parsing for: Expected semicolon token");
|
||||||
|
}
|
||||||
|
|
||||||
|
let condition = self.parse_expr();
|
||||||
|
|
||||||
|
if !matches!(self.next(), Token::Semicolon) {
|
||||||
|
panic!("Error parsing for: Expected semicolon token");
|
||||||
|
}
|
||||||
|
|
||||||
|
let advance = self.parse_expr();
|
||||||
|
|
||||||
|
if !matches!(self.next(), Token::LBrace) {
|
||||||
|
panic!("Error parsing for: Expected '{{' token");
|
||||||
|
}
|
||||||
|
|
||||||
|
let body = self.parse();
|
||||||
|
|
||||||
|
if !matches!(self.next(), Token::RBrace) {
|
||||||
|
panic!("Error parsing for: Expected '}}' token");
|
||||||
|
}
|
||||||
|
|
||||||
|
Stmt::For(init, condition, advance, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_if(&mut self) -> Stmt {
|
||||||
|
if !matches!(self.next(), Token::Keyword(Keyword::If)) {
|
||||||
|
panic!("Error parsing if: Expected if token");
|
||||||
|
}
|
||||||
|
|
||||||
|
let condition = self.parse_expr();
|
||||||
|
|
||||||
|
if !matches!(self.next(), Token::LBrace) {
|
||||||
|
panic!("Error parsing if: Expected '{{' token");
|
||||||
|
}
|
||||||
|
|
||||||
|
let body_if = self.parse();
|
||||||
|
|
||||||
|
if !matches!(self.next(), Token::RBrace) {
|
||||||
|
panic!("Error parsing if: Expected '}}' token");
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut body_else = Ast { prog: Vec::new() };
|
||||||
|
|
||||||
|
if matches!(self.peek(), Token::Keyword(Keyword::Else)) {
|
||||||
|
self.next();
|
||||||
|
|
||||||
|
if !matches!(self.next(), Token::LBrace) {
|
||||||
|
panic!("Error parsing else: Expected '{{' token");
|
||||||
|
}
|
||||||
|
|
||||||
|
body_else = self.parse();
|
||||||
|
|
||||||
|
if !matches!(self.next(), Token::RBrace) {
|
||||||
|
panic!("Error parsing else: Expected '}}' token");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Stmt::If(condition, body_if, body_else)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_while(&mut self) -> Stmt {
|
||||||
|
if !matches!(self.next(), Token::Keyword(Keyword::While)) {
|
||||||
|
panic!("Error parsing while: Expected while token");
|
||||||
|
}
|
||||||
|
|
||||||
|
let condition = self.parse_expr();
|
||||||
|
|
||||||
|
if !matches!(self.next(), Token::LBrace) {
|
||||||
|
panic!("Error parsing while: Expected '{{' token");
|
||||||
|
}
|
||||||
|
|
||||||
|
let body = self.parse();
|
||||||
|
|
||||||
|
if !matches!(self.next(), Token::RBrace) {
|
||||||
|
panic!("Error parsing while: Expected '}}' token");
|
||||||
|
}
|
||||||
|
|
||||||
|
Stmt::While(condition, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_let_stmt(&mut self) -> Stmt {
|
||||||
|
if !matches!(self.next(), Token::Keyword(Keyword::Let)) {
|
||||||
|
panic!("Error parsing let: Expected let token");
|
||||||
|
}
|
||||||
|
|
||||||
|
let name = match self.next() {
|
||||||
|
Token::Ident(name) => name,
|
||||||
|
_ => panic!("Error parsing let: Expected identifier after let"),
|
||||||
|
};
|
||||||
|
|
||||||
|
if !matches!(self.next(), Token::Assign) {
|
||||||
|
panic!("Error parsing let: Expected assignment token");
|
||||||
|
}
|
||||||
|
|
||||||
|
let rhs = self.parse_expr();
|
||||||
|
|
||||||
|
Stmt::Let(name, rhs)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_expr(&mut self) -> Expr {
|
||||||
let lhs = self.parse_primary();
|
let lhs = self.parse_primary();
|
||||||
self.parse_expr_precedence(lhs, 0)
|
self.parse_expr_precedence(lhs, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse binary expressions with a precedence equal to or higher than min_prec
|
/// Parse binary expressions with a precedence equal to or higher than min_prec
|
||||||
fn parse_expr_precedence(&mut self, mut lhs: Ast, min_prec: u8) -> Ast {
|
fn parse_expr_precedence(&mut self, mut lhs: Expr, min_prec: u8) -> Expr {
|
||||||
while let Some(binop) = &self.peek().try_to_binop() {
|
while let Some(binop) = &self.peek().try_to_binop() {
|
||||||
|
// Stop if the next operator has a lower binding power
|
||||||
if !(binop.precedence() >= min_prec) {
|
if !(binop.precedence() >= min_prec) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -61,30 +201,40 @@ impl<T: Iterator<Item = Token>> Parser<T> {
|
|||||||
rhs = self.parse_expr_precedence(rhs, binop.precedence() + 1);
|
rhs = self.parse_expr_precedence(rhs, binop.precedence() + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
lhs = Ast::BinOp(binop, lhs.into(), rhs.into());
|
lhs = Expr::BinOp(binop, lhs.into(), rhs.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
lhs
|
lhs
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse a primary expression (for now only number)
|
/// Parse a primary expression (for now only number)
|
||||||
fn parse_primary(&mut self) -> Ast {
|
fn parse_primary(&mut self) -> Expr {
|
||||||
match self.next() {
|
match self.next() {
|
||||||
Token::I64(val) => Ast::I64(val),
|
Token::Literal(Literal::I64(val)) => Expr::I64(val),
|
||||||
|
|
||||||
|
Token::Literal(Literal::Str(text)) => Expr::Str(text.into()),
|
||||||
|
|
||||||
|
Token::Ident(name) => Expr::Ident(name),
|
||||||
|
|
||||||
|
Token::LParen => {
|
||||||
|
// The tokens was an opening parenthesis, so parse a full expression again as the
|
||||||
|
// expression inside the parentheses `"(" expr ")"`
|
||||||
|
let inner = self.parse_expr();
|
||||||
|
|
||||||
|
// If there is no closing parenthesis after the expression, it is a syntax error
|
||||||
|
if !matches!(self.next(), Token::RParen) {
|
||||||
|
panic!("Error parsing primary expr: Missing closing parenthesis ')'");
|
||||||
|
}
|
||||||
|
|
||||||
|
inner
|
||||||
|
}
|
||||||
|
|
||||||
|
Token::Sub => Expr::UnOp(UnOpType::Neg, self.parse_primary().into()),
|
||||||
|
|
||||||
tok => panic!("Error parsing primary expr: Unexpected Token '{:?}'", tok),
|
tok => panic!("Error parsing primary expr: Unexpected Token '{:?}'", tok),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the next Token without removing it
|
|
||||||
fn peek(&mut self) -> &Token {
|
|
||||||
self.tokens.peek().unwrap_or(&Token::EoF)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Advance to next Token and return the removed Token
|
|
||||||
fn next(&mut self) -> Token {
|
|
||||||
self.tokens.next().unwrap_or(Token::EoF)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse<T: Iterator<Item = Token>, A: IntoIterator<IntoIter = T>>(tokens: A) -> Ast {
|
pub fn parse<T: Iterator<Item = Token>, A: IntoIterator<IntoIter = T>>(tokens: A) -> Ast {
|
||||||
@ -95,44 +245,62 @@ pub fn parse<T: Iterator<Item = Token>, A: IntoIterator<IntoIter = T>>(tokens: A
|
|||||||
impl BinOpType {
|
impl BinOpType {
|
||||||
/// Get the precedence for a binary operator. Higher value means the OP is stronger binding.
|
/// Get the precedence for a binary operator. Higher value means the OP is stronger binding.
|
||||||
/// For example Multiplication is stronger than addition, so Mul has higher precedence than Add.
|
/// For example Multiplication is stronger than addition, so Mul has higher precedence than Add.
|
||||||
|
///
|
||||||
|
/// The operator precedences are derived from the C language operator precedences. While not all
|
||||||
|
/// C operators are included or the exact same, the precedence oder is the same.
|
||||||
|
/// See: https://en.cppreference.com/w/c/language/operator_precedence
|
||||||
fn precedence(&self) -> u8 {
|
fn precedence(&self) -> u8 {
|
||||||
match self {
|
match self {
|
||||||
BinOpType::Add => 0,
|
BinOpType::Assign => 0,
|
||||||
BinOpType::Mul => 1,
|
BinOpType::BOr => 1,
|
||||||
|
BinOpType::BXor => 2,
|
||||||
|
BinOpType::BAnd => 3,
|
||||||
|
BinOpType::Equ | BinOpType::Neq => 4,
|
||||||
|
BinOpType::Gt | BinOpType::Ge | BinOpType::Lt | BinOpType::Le => 5,
|
||||||
|
BinOpType::Shl | BinOpType::Shr => 6,
|
||||||
|
BinOpType::Add | BinOpType::Sub => 7,
|
||||||
|
BinOpType::Mul | BinOpType::Div | BinOpType::Mod => 8,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::{parse, Ast, BinOpType};
|
use super::{parse, BinOpType, Expr};
|
||||||
use crate::lexer::Token;
|
use crate::{
|
||||||
|
parser::{Ast, Stmt},
|
||||||
|
token::{Literal, Token},
|
||||||
|
};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parser() {
|
fn test_parser() {
|
||||||
// Expression: 1 + 2 * 3 + 4
|
// Expression: 1 + 2 * 3 + 4
|
||||||
// With precedence: (1 + (2 * 3)) + 4
|
// With precedence: (1 + (2 * 3)) + 4
|
||||||
let tokens = [
|
let tokens = [
|
||||||
Token::I64(1),
|
Token::Literal(Literal::I64(1)),
|
||||||
Token::Add,
|
Token::Add,
|
||||||
Token::I64(2),
|
Token::Literal(Literal::I64(2)),
|
||||||
Token::Mul,
|
Token::Mul,
|
||||||
Token::I64(3),
|
Token::Literal(Literal::I64(3)),
|
||||||
Token::Add,
|
Token::Sub,
|
||||||
Token::I64(4),
|
Token::Literal(Literal::I64(4)),
|
||||||
];
|
];
|
||||||
|
|
||||||
let expected = Ast::BinOp(
|
let expected = Expr::BinOp(
|
||||||
BinOpType::Add,
|
BinOpType::Sub,
|
||||||
Ast::BinOp(
|
Expr::BinOp(
|
||||||
BinOpType::Add,
|
BinOpType::Add,
|
||||||
Ast::I64(1).into(),
|
Expr::I64(1).into(),
|
||||||
Ast::BinOp(BinOpType::Mul, Ast::I64(2).into(), Ast::I64(3).into()).into(),
|
Expr::BinOp(BinOpType::Mul, Expr::I64(2).into(), Expr::I64(3).into()).into(),
|
||||||
)
|
)
|
||||||
.into(),
|
.into(),
|
||||||
Ast::I64(4).into(),
|
Expr::I64(4).into(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let expected = Ast {
|
||||||
|
prog: vec![Stmt::Expr(expected)],
|
||||||
|
};
|
||||||
|
|
||||||
let actual = parse(tokens);
|
let actual = parse(tokens);
|
||||||
assert_eq!(expected, actual);
|
assert_eq!(expected, actual);
|
||||||
}
|
}
|
||||||
|
|||||||
147
src/token.rs
Normal file
147
src/token.rs
Normal file
@ -0,0 +1,147 @@
|
|||||||
|
use crate::ast::BinOpType;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
pub enum Literal {
|
||||||
|
/// Integer literal (64-bit)
|
||||||
|
I64(i64),
|
||||||
|
|
||||||
|
/// String literal ("Some string")
|
||||||
|
Str(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
pub enum Keyword {
|
||||||
|
/// Let identifier (let)
|
||||||
|
Let,
|
||||||
|
|
||||||
|
/// While (while)
|
||||||
|
While,
|
||||||
|
|
||||||
|
/// For (for)
|
||||||
|
For,
|
||||||
|
|
||||||
|
/// If (if)
|
||||||
|
If,
|
||||||
|
|
||||||
|
/// Else (else)
|
||||||
|
Else,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
pub enum Token {
|
||||||
|
/// Literal values
|
||||||
|
Literal(Literal),
|
||||||
|
|
||||||
|
/// Identifier (variable / function / ... name)
|
||||||
|
Ident(String),
|
||||||
|
|
||||||
|
/// Specific identifiers that have a special meaning as keywords
|
||||||
|
Keyword(Keyword),
|
||||||
|
|
||||||
|
/// Left parenthesis ('(')
|
||||||
|
LParen,
|
||||||
|
|
||||||
|
/// Right parentheses (')')
|
||||||
|
RParen,
|
||||||
|
|
||||||
|
/// Left brace ({)
|
||||||
|
LBrace,
|
||||||
|
|
||||||
|
/// Right brace (})
|
||||||
|
RBrace,
|
||||||
|
|
||||||
|
/// Dollar sign ($)
|
||||||
|
Dollar,
|
||||||
|
|
||||||
|
/// Double Dollar sign ($$)
|
||||||
|
DoubleDollar,
|
||||||
|
|
||||||
|
/// Assignment (single equal) (=)
|
||||||
|
Assign,
|
||||||
|
|
||||||
|
/// Plus (+)
|
||||||
|
Add,
|
||||||
|
|
||||||
|
/// Minus (-)
|
||||||
|
Sub,
|
||||||
|
|
||||||
|
/// Asterisk (*)
|
||||||
|
Mul,
|
||||||
|
|
||||||
|
/// Slash (/)
|
||||||
|
Div,
|
||||||
|
|
||||||
|
/// Percent (%)
|
||||||
|
Mod,
|
||||||
|
|
||||||
|
/// Pipe (|)
|
||||||
|
BOr,
|
||||||
|
|
||||||
|
/// Ampersand (&)
|
||||||
|
BAnd,
|
||||||
|
|
||||||
|
/// Circumflex (^)
|
||||||
|
BXor,
|
||||||
|
|
||||||
|
/// Shift Left (<<)
|
||||||
|
Shl,
|
||||||
|
|
||||||
|
/// Shift Right (>>)
|
||||||
|
Shr,
|
||||||
|
|
||||||
|
/// Equal sign (==)
|
||||||
|
Equ,
|
||||||
|
|
||||||
|
/// Not Equal sign (!=)
|
||||||
|
Neq,
|
||||||
|
|
||||||
|
/// Greater than (>)
|
||||||
|
Gt,
|
||||||
|
|
||||||
|
/// Greater or equal (>=)
|
||||||
|
Ge,
|
||||||
|
|
||||||
|
/// Less than (<)
|
||||||
|
Lt,
|
||||||
|
|
||||||
|
/// Less or equal (<=)
|
||||||
|
Le,
|
||||||
|
|
||||||
|
/// Semicolon (;)
|
||||||
|
Semicolon,
|
||||||
|
|
||||||
|
/// End of file
|
||||||
|
EoF,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Token {
|
||||||
|
pub fn try_to_binop(&self) -> Option<BinOpType> {
|
||||||
|
Some(match self {
|
||||||
|
Token::Add => BinOpType::Add,
|
||||||
|
Token::Sub => BinOpType::Sub,
|
||||||
|
|
||||||
|
Token::Mul => BinOpType::Mul,
|
||||||
|
Token::Div => BinOpType::Div,
|
||||||
|
Token::Mod => BinOpType::Mod,
|
||||||
|
|
||||||
|
Token::BAnd => BinOpType::BAnd,
|
||||||
|
Token::BOr => BinOpType::BOr,
|
||||||
|
Token::BXor => BinOpType::BXor,
|
||||||
|
|
||||||
|
Token::Shl => BinOpType::Shl,
|
||||||
|
Token::Shr => BinOpType::Shr,
|
||||||
|
|
||||||
|
Token::Equ => BinOpType::Equ,
|
||||||
|
Token::Neq => BinOpType::Neq,
|
||||||
|
|
||||||
|
Token::Gt => BinOpType::Gt,
|
||||||
|
Token::Ge => BinOpType::Ge,
|
||||||
|
Token::Lt => BinOpType::Lt,
|
||||||
|
Token::Le => BinOpType::Le,
|
||||||
|
|
||||||
|
Token::Assign => BinOpType::Assign,
|
||||||
|
|
||||||
|
_ => return None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
208
src/vm.rs
Normal file
208
src/vm.rs
Normal file
@ -0,0 +1,208 @@
|
|||||||
|
use crate::{bytecode::op::*, interpreter::Value};
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct Vm {
|
||||||
|
prog: Vec<u32>,
|
||||||
|
ip: usize,
|
||||||
|
stack: Vec<Value>,
|
||||||
|
|
||||||
|
/// This isn't actually a heap. It's actually still more of a f*cked up stack
|
||||||
|
heap: Vec<Value>,
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! binop_stack {
|
||||||
|
($self:ident, $op:tt) => {
|
||||||
|
{
|
||||||
|
let rhs = $self.stack.pop().unwrap();
|
||||||
|
let lhs = $self.stack.last_mut().unwrap();
|
||||||
|
match (lhs, rhs) {
|
||||||
|
(Value::I64(lhs), Value::I64(rhs)) => *lhs = *lhs $op rhs,
|
||||||
|
_ => panic!("Invalid data for add"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Vm {
|
||||||
|
pub fn new(prog: Vec<u32>) -> Self {
|
||||||
|
Self {
|
||||||
|
prog,
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run(&mut self) {
|
||||||
|
while let Some(op) = self.prog.get(self.ip).copied() {
|
||||||
|
self.ip += 1;
|
||||||
|
|
||||||
|
match op & 0xff {
|
||||||
|
PUSH => {
|
||||||
|
let val = self.read_i64();
|
||||||
|
self.stack.push(Value::I64(val));
|
||||||
|
}
|
||||||
|
POP => {
|
||||||
|
self.stack.pop();
|
||||||
|
}
|
||||||
|
LOAD => {
|
||||||
|
// let addr = self.read_i64() as usize;
|
||||||
|
let addr = (op >> 8) as usize;
|
||||||
|
|
||||||
|
if let Some(val) = self.heap.get(addr) {
|
||||||
|
self.stack.push(val.clone());
|
||||||
|
} else {
|
||||||
|
panic!("Trying to load from uninitialized heap");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
STORE => {
|
||||||
|
let val = self
|
||||||
|
.stack
|
||||||
|
.pop()
|
||||||
|
.expect("Trying to pop value from stack for storing");
|
||||||
|
// let addr = self.read_i64() as usize;
|
||||||
|
let addr = (op >> 8) as usize;
|
||||||
|
|
||||||
|
if self.heap.len() == addr {
|
||||||
|
self.heap.push(val);
|
||||||
|
} else {
|
||||||
|
self.heap[addr] = val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PRINT => {
|
||||||
|
let val = self
|
||||||
|
.stack
|
||||||
|
.pop()
|
||||||
|
.expect("Trying to pop value from stack for printing");
|
||||||
|
print!("{}", val);
|
||||||
|
}
|
||||||
|
DBG_PRINT => {
|
||||||
|
let val = self
|
||||||
|
.stack
|
||||||
|
.pop()
|
||||||
|
.expect("Trying to pop value from stack for printing");
|
||||||
|
print!("{:?}", val);
|
||||||
|
}
|
||||||
|
|
||||||
|
ADD => {
|
||||||
|
binop_stack!(self, +);
|
||||||
|
// self.stack.push(Value::I64(vals.0 + vals.1))
|
||||||
|
}
|
||||||
|
SUB => {
|
||||||
|
binop_stack!(self, -);
|
||||||
|
// let vals = self.pop2_i64();
|
||||||
|
// self.stack.push(Value::I64(vals.0 - vals.1))
|
||||||
|
}
|
||||||
|
MUL => {
|
||||||
|
binop_stack!(self, *);
|
||||||
|
// let vals = self.pop2_i64();
|
||||||
|
// self.stack.push(Value::I64(vals.0 * vals.1))
|
||||||
|
}
|
||||||
|
DIV => {
|
||||||
|
binop_stack!(self, /);
|
||||||
|
// let vals = self.pop2_i64();
|
||||||
|
// self.stack.push(Value::I64(vals.0 / vals.1))
|
||||||
|
}
|
||||||
|
MOD => {
|
||||||
|
binop_stack!(self, %);
|
||||||
|
// let vals = self.pop2_i64();
|
||||||
|
// self.stack.push(Value::I64(vals.0 % vals.1))
|
||||||
|
}
|
||||||
|
EQ => {
|
||||||
|
let vals = self.pop2_i64();
|
||||||
|
self.stack
|
||||||
|
.push(Value::I64(if vals.0 == vals.1 { 1 } else { 0 }))
|
||||||
|
}
|
||||||
|
NEQ => {
|
||||||
|
let vals = self.pop2_i64();
|
||||||
|
self.stack
|
||||||
|
.push(Value::I64(if vals.0 != vals.1 { 1 } else { 0 }))
|
||||||
|
}
|
||||||
|
GT => {
|
||||||
|
let vals = self.pop2_i64();
|
||||||
|
self.stack
|
||||||
|
.push(Value::I64(if vals.0 > vals.1 { 1 } else { 0 }))
|
||||||
|
}
|
||||||
|
GE => {
|
||||||
|
let vals = self.pop2_i64();
|
||||||
|
self.stack
|
||||||
|
.push(Value::I64(if vals.0 >= vals.1 { 1 } else { 0 }))
|
||||||
|
}
|
||||||
|
LT => {
|
||||||
|
let vals = self.pop2_i64();
|
||||||
|
self.stack
|
||||||
|
.push(Value::I64(if vals.0 < vals.1 { 1 } else { 0 }))
|
||||||
|
}
|
||||||
|
LE => {
|
||||||
|
let vals = self.pop2_i64();
|
||||||
|
self.stack
|
||||||
|
.push(Value::I64(if vals.0 <= vals.1 { 1 } else { 0 }))
|
||||||
|
}
|
||||||
|
BOR => {
|
||||||
|
let vals = self.pop2_i64();
|
||||||
|
self.stack.push(Value::I64(vals.0 | vals.1))
|
||||||
|
}
|
||||||
|
BAND => {
|
||||||
|
let vals = self.pop2_i64();
|
||||||
|
self.stack.push(Value::I64(vals.0 & vals.1))
|
||||||
|
}
|
||||||
|
BXOR => {
|
||||||
|
let vals = self.pop2_i64();
|
||||||
|
self.stack.push(Value::I64(vals.0 ^ vals.1))
|
||||||
|
}
|
||||||
|
SHL => {
|
||||||
|
let vals = self.pop2_i64();
|
||||||
|
self.stack.push(Value::I64(vals.0 << vals.1))
|
||||||
|
}
|
||||||
|
SHR => {
|
||||||
|
let vals = self.pop2_i64();
|
||||||
|
self.stack.push(Value::I64(vals.0 >> vals.1))
|
||||||
|
}
|
||||||
|
JUMP => {
|
||||||
|
self.ip = self.read_i64() as usize;
|
||||||
|
}
|
||||||
|
JUMP_TRUE => {
|
||||||
|
let jmp_target = self.read_i64() as usize;
|
||||||
|
if !matches!(self.stack.pop(), Some(Value::I64(0))) {
|
||||||
|
self.ip = jmp_target;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
JUMP_FALSE => {
|
||||||
|
let jmp_target = self.read_i64() as usize;
|
||||||
|
if matches!(self.stack.pop(), Some(Value::I64(0))) {
|
||||||
|
self.ip = jmp_target;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => panic!("Invalid opcode")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pop2_i64(&mut self) -> (i64, i64) {
|
||||||
|
let rhs = self.stack.pop();
|
||||||
|
let lhs = self.stack.pop();
|
||||||
|
match (lhs, rhs) {
|
||||||
|
(Some(Value::I64(lhs)), Some(Value::I64(rhs))) => (lhs, rhs),
|
||||||
|
_ => panic!("Invalid data for add"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_i64(&mut self) -> i64 {
|
||||||
|
let mut val = if let Some(val) = self.prog.get(self.ip).copied() {
|
||||||
|
val
|
||||||
|
} else {
|
||||||
|
panic!("Expected Value as next OP")
|
||||||
|
} as i64;
|
||||||
|
|
||||||
|
self.ip += 1;
|
||||||
|
|
||||||
|
val |= (if let Some(val) = self.prog.get(self.ip).copied() {
|
||||||
|
val
|
||||||
|
} else {
|
||||||
|
panic!("Expected Value as next OP")
|
||||||
|
} as i64)
|
||||||
|
<< 32;
|
||||||
|
|
||||||
|
self.ip += 1;
|
||||||
|
|
||||||
|
val
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user