added @8 and !\8 memory ops

This commit is contained in:
MCorange 2023-03-14 19:36:30 +02:00
parent 782c3aadde
commit 8d72420b04
5 changed files with 51 additions and 13 deletions

View File

@ -96,6 +96,22 @@ pub fn compile(tokens: Vec<Operator>, args: Args) -> Result<()>{
writeln!(writer, " push mem")?;
ti += 1;
}
OpType::Load8 => {
writeln!(writer, " ; -- LOAD64")?;
writeln!(writer, " pop rax")?;
writeln!(writer, " xor rbx, rbx")?;
writeln!(writer, " mov bl, [rax]")?;
writeln!(writer, " push rbx")?;
ti += 1;
}
OpType::Store8 => {
writeln!(writer, " ; -- STORE64")?;
writeln!(writer, " pop rbx")?;
writeln!(writer, " pop rax")?;
writeln!(writer, " mov [rax], bl")?;
ti += 1;
}
// math
OpType::Plus => {

View File

@ -15,7 +15,9 @@ pub enum OpType {
Lt,
While,
Do,
Mem
Mem,
Load8,
Store8
}
@ -27,13 +29,13 @@ pub enum OpType {
#[derive(Debug, Clone)]
pub struct Operator {
pub typ: OpType,
pub value: i32,
pub value: i64,
pub jmp: i32,
pub pos: (String, u32, u32)
}
impl Operator {
pub fn new(typ: OpType, value: i32, file: String, row: u32, col: u32) -> Self {
pub fn new(typ: OpType, value: i64, file: String, row: u32, col: u32) -> Self {
Self {
typ,
value,

View File

@ -2,7 +2,7 @@ use crate::constants::OpType;
// use crate::util::logger;
use color_eyre::Result;
fn stack_pop(stack: &mut Vec<i32>) -> Result<i32, &'static str> {
fn stack_pop(stack: &mut Vec<u64>) -> Result<u64, &'static str> {
match stack.pop() {
Some(i) => Ok(i),
None => Err("Stack underflow"),
@ -10,16 +10,16 @@ fn stack_pop(stack: &mut Vec<i32>) -> Result<i32, &'static str> {
}
pub fn run(tokens: Vec<crate::constants::Operator>) -> Result<(), &'static str>{
let mut stack: Vec<i32> = Vec::new();
let mut stack: Vec<u64> = Vec::new();
let mut ti = 0;
let mut mem: [u8; 16*1024];
let mut mem: Vec<u64> = Vec::with_capacity(crate::compile::MEM_SZ as usize);
while ti < tokens.len() {
let token = &tokens[ti];
match token.typ {
// stack
OpType::Push => {
stack.push(token.value);
stack.push(token.value as u64);
ti += 1;
},
OpType::Pop => {
@ -45,6 +45,19 @@ pub fn run(tokens: Vec<crate::constants::Operator>) -> Result<(), &'static str>{
stack.push(0);
ti += 1;
}
OpType::Load8 => {
let a = stack_pop(&mut stack)?;
stack.push(mem[a as usize]);
ti += 1;
}
OpType::Store8 => {
let a = stack_pop(&mut stack)?;
let b = stack_pop(&mut stack)?;
mem[b as usize] = a;
ti += 1;
}
// math
OpType::Plus => {
@ -62,19 +75,19 @@ pub fn run(tokens: Vec<crate::constants::Operator>) -> Result<(), &'static str>{
OpType::Equals => {
let a = stack_pop(&mut stack)?;
let b = stack_pop(&mut stack)?;
stack.push((a == b) as i32);
stack.push((a == b) as u64);
ti += 1;
},
OpType::Gt => {
let b = stack_pop(&mut stack)?;
let a = stack_pop(&mut stack)?;
stack.push((a > b) as i32);
stack.push((a > b) as u64);
ti += 1;
},
OpType::Lt => {
let b = stack_pop(&mut stack)?;
let a = stack_pop(&mut stack)?;
stack.push((a < b) as i32);
stack.push((a < b) as u64);
ti += 1;
},

View File

@ -82,8 +82,8 @@ impl Parser {
}
let pos = (token.file.clone(), token.line, token.col);
match token.text.as_str() {
t if t.parse::<i32>().is_ok() => {
let num = t.parse::<i32>().unwrap();
t if t.parse::<u64>().is_ok() => { // negative numbers not yet implemented
let num = t.parse::<i64>().unwrap();
tokens.push(Operator::new(OpType::Push, num, token.file.clone(), token.line, token.col));
},
@ -106,7 +106,12 @@ impl Parser {
"end" => tokens.push(Operator::new(OpType::End, 0, token.file.clone(), token.line, token.col)),
"while" => tokens.push(Operator::new(OpType::While, 0, token.file.clone(), token.line, token.col)),
"do" => tokens.push(Operator::new(OpType::Do, 0, token.file.clone(), token.line, token.col)),
// mem
"mem" => tokens.push(Operator::new(OpType::Mem, 0, token.file.clone(), token.line, token.col)),
"!8" => tokens.push(Operator::new(OpType::Load8, 0, token.file.clone(), token.line, token.col)),
"@8" => tokens.push(Operator::new(OpType::Store8, 0, token.file.clone(), token.line, token.col)),

View File

@ -1 +1,3 @@
mem print
mem 255 @8
mem !8 print