Skip to content

Commit

Permalink
jump
Browse files Browse the repository at this point in the history
  • Loading branch information
salomaosnff committed Oct 1, 2023
1 parent db448fd commit 4162bb6
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 51 deletions.
29 changes: 14 additions & 15 deletions code.lang
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
SPUSH '\n'
SPUSH '!'
SPUSH 'd'
SPUSH 'l'
SPUSH 'r'
SPUSH 'o'
SPUSH 'W'
SPUSH ' '
SPUSH 'o'
SPUSH 'l'
SPUSH 'l'
SPUSH 'e'
SPUSH 'H'

SPUSH 13
SPUSH 'A'
DEC
LABEL 0
INC
SPEEK
SPUSH 1
SPUSH 1
WRITE
SPEEK
SPUSH 'Z'
LT
SPUSH 0
JUMPI
SPUSH "\n"
SPUSH 1
WRITE
52 changes: 45 additions & 7 deletions src/lang/opcode/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,39 @@ pub fn compile_file(file: File) -> Vec<OpCode> {
continue;
}

if c == '0' {
let next = line.next().expect("Invalid char");
if c == '"' {
let mut char = line.next().expect("Invalid char");
let mut length = 0;

while char != '"' {
let arg = if char == '\\' {
char = line.next().expect("Invalid char");

match char {
'n' => '\n' as i32,
't' => '\t' as i32,
'r' => '\r' as i32,
_ => char as i32,
}
} else {
char as i32
};

buffer.push(OpCode::SPUSH(arg));
length += 1;
char = line.next().expect("Invalid char");
}

buffer.push(OpCode::SPUSH(length));

op.clear();

match next {
'x' => {
continue;
}

if c == '0' {
match line.next() {
Some('x') => {
let mut hex = String::new();

while let Some(c) = line.next() {
Expand All @@ -99,7 +127,7 @@ pub fn compile_file(file: File) -> Vec<OpCode> {

args.push(i32::from_str_radix(&hex, 16).unwrap());
}
'b' => {
Some('b') => {
let mut bin = String::new();

while let Some(c) = line.next() {
Expand All @@ -112,7 +140,7 @@ pub fn compile_file(file: File) -> Vec<OpCode> {

args.push(i32::from_str_radix(&bin, 2).unwrap());
}
'o' => {
Some('o') => {
let mut oct = String::new();

while let Some(c) = line.next() {
Expand All @@ -125,7 +153,7 @@ pub fn compile_file(file: File) -> Vec<OpCode> {

args.push(i32::from_str_radix(&oct, 8).unwrap());
}
c if c.is_numeric() => {
Some(c) if c.is_numeric() => {
let mut dec = String::new();

dec.push(c);
Expand All @@ -140,6 +168,9 @@ pub fn compile_file(file: File) -> Vec<OpCode> {

args.push(i32::from_str_radix(&dec, 10).unwrap());
}
None => {
args.push(0);
}
_ => panic!("Invalid char"),
}

Expand Down Expand Up @@ -173,14 +204,21 @@ pub fn compile_file(file: File) -> Vec<OpCode> {
match op.as_str() {
"NOOP" => buffer.push(OpCode::NOP),
"HALT" => buffer.push(OpCode::HALT),
"SPEEK" => buffer.push(OpCode::SPEEK),
"SPUSH" => buffer.push(OpCode::SPUSH(args[0])),
"INC" => buffer.push(OpCode::INC),
"DEC" => buffer.push(OpCode::DEC),
"ADD" => buffer.push(OpCode::ADD),
"SUB" => buffer.push(OpCode::SUB),
"MUL" => buffer.push(OpCode::MUL),
"DIV" => buffer.push(OpCode::DIV),
"MOD" => buffer.push(OpCode::MOD),
"POW" => buffer.push(OpCode::POW),
"WRITE" => buffer.push(OpCode::WRITE),
"LABEL" => buffer.push(OpCode::LABEL(args[0])),
"JUMP" => buffer.push(OpCode::JUMP),
"JUMPI" => buffer.push(OpCode::JUMPI),
"LT" => buffer.push(OpCode::LT),
op => panic!("Invalid opcode {op}"),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lang/opcode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ pub fn pow() -> OpCode {

pub fn write() -> OpCode {
OpCode::WRITE
}
}
87 changes: 75 additions & 12 deletions src/lang/opcode/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,40 @@ use super::assembler::Assembler;
const NOOP_OPCODE: u8 = 0x00;
const HALT_OPCODE: u8 = 0x01;
const SPUSH_OPCODE: u8 = 0x02;
const ADD_OPCODE: u8 = 0x03;
const SUB_OPCODE: u8 = 0x04;
const MUL_OPCODE: u8 = 0x05;
const DIV_OPCODE: u8 = 0x06;
const MOD_OPCODE: u8 = 0x07;
const POW_OPCODE: u8 = 0x08;
const WRITE_OPCODE: u8 = 0x09;
const SPEEK_OPCODE: u8 = 0x03;
const INC_OPCODE: u8 = 0x04;
const DEC_OPCODE: u8 = 0x05;
const ADD_OPCODE: u8 = 0x06;
const SUB_OPCODE: u8 = 0x07;
const MUL_OPCODE: u8 = 0x08;
const DIV_OPCODE: u8 = 0x09;
const MOD_OPCODE: u8 = 0x0A;
const POW_OPCODE: u8 = 0x0B;
const WRITE_OPCODE: u8 = 0x0C;
const LABEL_OPCODE: u8 = 0x0D;
const JUMP_OPCODE: u8 = 0x0E;
const LT_OPCODE: u8 = 0x0F;
const JUMPI_OPCODE: u8 = 0x10;

#[derive(Clone)]
pub enum OpCode {
NOP,
HALT,
SPUSH(i32),
SPEEK,
ADD,
SUB,
MUL,
DIV,
MOD,
POW,
WRITE,
LABEL(i32),
JUMP,
INC,
DEC,
LT,
JUMPI,
}

impl Debug for OpCode {
Expand All @@ -43,14 +57,24 @@ impl Assembler for OpCode {
bytes.extend(op.to_be_bytes().iter());
bytes
}
OpCode::SPEEK => vec![SPEEK_OPCODE],
OpCode::INC => vec![INC_OPCODE],
OpCode::DEC => vec![DEC_OPCODE],
OpCode::ADD => vec![ADD_OPCODE],
OpCode::SUB => vec![SUB_OPCODE],
OpCode::MUL => vec![MUL_OPCODE],
OpCode::DIV => vec![DIV_OPCODE],
OpCode::MOD => vec![MOD_OPCODE],
OpCode::POW => vec![POW_OPCODE],
OpCode::WRITE => vec![WRITE_OPCODE],
_ => panic!("Invalid opcode"),
OpCode::LABEL(op) => {
let mut bytes = vec![LABEL_OPCODE];
bytes.extend(op.to_be_bytes().iter());
bytes
}
OpCode::JUMP => vec![JUMP_OPCODE],
OpCode::LT => vec![LT_OPCODE],
OpCode::JUMPI => vec![JUMPI_OPCODE],
}
}

Expand All @@ -59,15 +83,20 @@ impl Assembler for OpCode {
OpCode::NOP => String::from("NOP"),
OpCode::HALT => String::from("HALT"),
OpCode::SPUSH(value) => format!("SPUSH {}", value),
OpCode::SPEEK => String::from("SPEEK"),
OpCode::INC => String::from("INC"),
OpCode::DEC => String::from("DEC"),
OpCode::ADD => String::from("ADD"),
OpCode::SUB => String::from("SUB"),
OpCode::MUL => String::from("MUL"),
OpCode::DIV => String::from("DIV"),
OpCode::MOD => String::from("MOD"),
OpCode::POW => String::from("POW"),
OpCode::WRITE => String::from("WRITE"),

_ => panic!("Invalid opcode"),
OpCode::LABEL(value) => format!("LABEL {}", value),
OpCode::JUMP => String::from("JUMP"),
OpCode::LT => String::from("LT"),
OpCode::JUMPI => String::from("JUMPI"),
}
}

Expand All @@ -91,6 +120,18 @@ impl Assembler for OpCode {

Some(OpCode::SPUSH(i32::from_be_bytes(buffer)))
}
SPEEK_OPCODE => {
bytes.remove(0);
Some(OpCode::SPEEK)
}
INC_OPCODE => {
bytes.remove(0);
Some(OpCode::INC)
}
DEC_OPCODE => {
bytes.remove(0);
Some(OpCode::DEC)
}
ADD_OPCODE => {
bytes.remove(0);
Some(OpCode::ADD)
Expand All @@ -114,12 +155,34 @@ impl Assembler for OpCode {
POW_OPCODE => {
bytes.remove(0);
Some(OpCode::POW)
},
}
WRITE_OPCODE => {
bytes.remove(0);
Some(OpCode::WRITE)
}
LABEL_OPCODE => {
bytes.remove(0);
let mut buffer = [0; 4];

for i in 0..4 {
buffer[i] = bytes.remove(0);
}

Some(OpCode::LABEL(i32::from_be_bytes(buffer)))
}
JUMP_OPCODE => {
bytes.remove(0);
Some(OpCode::JUMP)
}
LT_OPCODE => {
bytes.remove(0);
Some(OpCode::LT)
}
JUMPI_OPCODE => {
bytes.remove(0);
Some(OpCode::JUMPI)
}
_ => None,
}
}
}
}
9 changes: 6 additions & 3 deletions src/lang/vm/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ impl Stack {
}
}

pub fn peek(&self) -> [u8; STACK_ITEM_SIZE] {
return self.get(self.sp - 1);
}

pub fn push(&mut self, value: [u8; STACK_ITEM_SIZE]) {
if self.sp >= self.size {
vm_panic("StackOverflow", "Maximum stack size exceeded!");
Expand All @@ -38,14 +42,13 @@ impl Stack {
let mut value = [0; STACK_ITEM_SIZE];

self.sp -= 1;

let index = self.sp * STACK_ITEM_SIZE;

for i in 0..STACK_ITEM_SIZE {
value[i] = self.data[index];
self.data.remove(index);
}


return value;
}
Expand Down Expand Up @@ -93,4 +96,4 @@ impl Stack {
pub fn dump(&self) -> &Vec<u8> {
return &self.data;
}
}
}
18 changes: 17 additions & 1 deletion src/lang/vm/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::IO;

pub struct Stdin;
pub struct Stdout;
pub struct Stderr;

impl IO for Stdin {
fn read(&mut self, buffer: &mut [u8]) {
Expand All @@ -22,12 +23,27 @@ impl IO for Stdout {
eprintln!("STDOUT é somente escrita!")
}

fn write(&mut self, buffer: &[u8]) {
std::io::stdout()
.lock()
.write_all(buffer)
.expect("Falha na escrita do terminal!");

// println!("STDOUT >> {:?}", buffer);
}
}

impl IO for Stderr {
fn read(&mut self, buffer: &mut [u8]) {
eprintln!("STDERR é somente escrita!")
}

fn write(&mut self, buffer: &[u8]) {
// std::io::stdout()
// .lock()
// .write_all(buffer)
// .expect("Falha na escrita do terminal!");

println!("STDOUT >> {:?}", buffer);
println!("STDERR >> {:?}", buffer);
}
}
Loading

0 comments on commit 4162bb6

Please sign in to comment.