From 8d72420b042084835ecae3151cea1b08226260ba Mon Sep 17 00:00:00 2001 From: MCorange Date: Tue, 14 Mar 2023 19:36:30 +0200 Subject: [PATCH] added @8 and !\8 memory ops --- src/compile/linux_x86_64.rs | 16 ++++++++++++++++ src/constants.rs | 8 +++++--- src/interpret/linux_x86_64.rs | 27 ++++++++++++++++++++------- src/parser.rs | 9 +++++++-- test.mcl | 4 +++- 5 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/compile/linux_x86_64.rs b/src/compile/linux_x86_64.rs index 41b336a..6ef26a3 100644 --- a/src/compile/linux_x86_64.rs +++ b/src/compile/linux_x86_64.rs @@ -96,6 +96,22 @@ pub fn compile(tokens: Vec, 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 => { diff --git a/src/constants.rs b/src/constants.rs index 608d414..7f02c7e 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -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, diff --git a/src/interpret/linux_x86_64.rs b/src/interpret/linux_x86_64.rs index 760c436..b686485 100644 --- a/src/interpret/linux_x86_64.rs +++ b/src/interpret/linux_x86_64.rs @@ -2,7 +2,7 @@ use crate::constants::OpType; // use crate::util::logger; use color_eyre::Result; -fn stack_pop(stack: &mut Vec) -> Result { +fn stack_pop(stack: &mut Vec) -> Result { match stack.pop() { Some(i) => Ok(i), None => Err("Stack underflow"), @@ -10,16 +10,16 @@ fn stack_pop(stack: &mut Vec) -> Result { } pub fn run(tokens: Vec) -> Result<(), &'static str>{ - let mut stack: Vec = Vec::new(); + let mut stack: Vec = Vec::new(); let mut ti = 0; - let mut mem: [u8; 16*1024]; + let mut mem: Vec = 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) -> 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) -> 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; }, diff --git a/src/parser.rs b/src/parser.rs index 1dea3da..c8814a8 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -82,8 +82,8 @@ impl Parser { } let pos = (token.file.clone(), token.line, token.col); match token.text.as_str() { - t if t.parse::().is_ok() => { - let num = t.parse::().unwrap(); + t if t.parse::().is_ok() => { // negative numbers not yet implemented + let num = t.parse::().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)), + diff --git a/test.mcl b/test.mcl index e1c9038..365e313 100644 --- a/test.mcl +++ b/test.mcl @@ -1 +1,3 @@ -mem print \ No newline at end of file +mem 255 @8 + +mem !8 print \ No newline at end of file