Skip to content

Commit

Permalink
feat: virtual machine more instructions supported
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Russak committed Dec 2, 2023
1 parent 1601cc7 commit b1e095f
Show file tree
Hide file tree
Showing 9 changed files with 768 additions and 257 deletions.
126 changes: 126 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ crate-type = ["lib"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
enum-display = "0.1.3"
lexer = { path = "../lexer" }
num = "0.4.1"
num-derive = "0.4.1"
num-traits = "0.2.17"
parser = { path = "../parser" }
61 changes: 11 additions & 50 deletions crates/compiler/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,21 @@
mod vm;
mod op_code;
mod instr;

extern crate num;
#[macro_use]
extern crate num_derive;
#[macro_use]
extern crate enum_display;

use std::{fmt::{Display, Formatter}};

use lexer::PError;
use parser::{Node, Op, Value};
pub use vm::VM;
pub use instr::{Instr, Instrs};
pub use op_code::OpCode;

#[derive(Debug, Clone, Copy)]
pub enum OpCode {
Move,
Load,
Load0,
Load1,
Store,
Exit,
Add,
Sub,
Mul,
Div,
}

impl Display for OpCode {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
OpCode::Move => write!(f, "Move"),
OpCode::Load => write!(f, "Load"),
OpCode::Load0 => write!(f, "Load0"),
OpCode::Load1 => write!(f, "Load1"),
OpCode::Store => write!(f, "Store"),
OpCode::Exit => write!(f, "Pop"),
OpCode::Add => write!(f, "Add"),
OpCode::Sub => write!(f, "Sub"),
OpCode::Mul => write!(f, "Mul"),
OpCode::Div => write!(f, "Div"),
}
}
}

impl From<u8> for OpCode {
fn from(val: u8) -> OpCode {
match val {
0 => OpCode::Move,
1 => OpCode::Load,
2 => OpCode::Load0,
3 => OpCode::Load1,
4 => OpCode::Store,
5 => OpCode::Exit,
6 => OpCode::Add,
7 => OpCode::Sub,
8 => OpCode::Mul,
9 => OpCode::Div,
_ => panic!("Unknown op code: {}", val),
}
}
}

struct Instruction {
op_code: OpCode,
Expand Down Expand Up @@ -205,15 +168,13 @@ mod tests {

use super::*;

#[test]
fn compile_simple() {
let mut node = Node::new(Op::Scope, Location::Eof);
node.add(Node::new(Op::Value, Location::Eof).set_value(1.into()));
let bytes = compile(&node).unwrap();
assert_eq!(bytes, vec![3, 5]);
}

#[test]
fn compile_binary_op() {
let mut node = Node::new(Op::Scope, Location::Eof);
let mut add = Node::new(Op::Add, Location::Eof);
Expand Down
Loading

0 comments on commit b1e095f

Please sign in to comment.