diff --git a/include/int.mcl b/include/int.mcl new file mode 100644 index 0000000..6a5e3b9 --- /dev/null +++ b/include/int.mcl @@ -0,0 +1,5 @@ +macro NULL 0 end +macro false 0 end +macro true 1 end +macro div divmod drop end +macro mod divmod swap drop end \ No newline at end of file diff --git a/src/compile/linux_x86_64.rs b/src/compile/linux_x86_64.rs index 0ce18de..dd02b9f 100644 --- a/src/compile/linux_x86_64.rs +++ b/src/compile/linux_x86_64.rs @@ -202,7 +202,6 @@ pub fn compile(tokens: Vec, args: Args) -> Result{ writeln!(writer, " cmove rcx, rdx")?; writeln!(writer, " push rcx")?; ti += 1; - }, OpType::Lt => { writeln!(writer, " ;; -- lt")?; @@ -214,7 +213,6 @@ pub fn compile(tokens: Vec, args: Args) -> Result{ writeln!(writer, " cmovl rcx, rdx")?; writeln!(writer, " push rcx")?; ti += 1; - }, OpType::Gt => { writeln!(writer, " ;; -- gt")?; @@ -226,7 +224,39 @@ pub fn compile(tokens: Vec, args: Args) -> Result{ writeln!(writer, " cmovg rcx, rdx")?; writeln!(writer, " push rcx")?; ti += 1; - + }, + OpType::NotEquals => { + writeln!(writer, " ;; -- not equals")?; + writeln!(writer, " mov rcx, 1")?; + writeln!(writer, " mov rdx, 0")?; + writeln!(writer, " pop rax")?; + writeln!(writer, " pop rbx")?; + writeln!(writer, " cmp rax, rbx")?; + writeln!(writer, " cmove rcx, rdx")?; + writeln!(writer, " push rcx")?; + ti += 1; + }, + OpType::Le => { + writeln!(writer, " ;; -- lt")?; + writeln!(writer, " mov rcx, 0")?; + writeln!(writer, " mov rdx, 1")?; + writeln!(writer, " pop rbx")?; + writeln!(writer, " pop rax")?; + writeln!(writer, " cmp rax, rbx")?; + writeln!(writer, " cmovle rcx, rdx")?; + writeln!(writer, " push rcx")?; + ti += 1; + }, + OpType::Ge => { + writeln!(writer, " ;; -- gt")?; + writeln!(writer, " mov rcx, 0")?; + writeln!(writer, " mov rdx, 1")?; + writeln!(writer, " pop rbx")?; + writeln!(writer, " pop rax")?; + writeln!(writer, " cmp rax, rbx")?; + writeln!(writer, " cmovge rcx, rdx")?; + writeln!(writer, " push rcx")?; + ti += 1; }, OpType::Band => { writeln!(writer, " ;; -- band")?; @@ -260,14 +290,14 @@ pub fn compile(tokens: Vec, args: Args) -> Result{ writeln!(writer, " push rbx")?; ti += 1; }, - OpType::Div => { + OpType::DivMod => { writeln!(writer, " ;; -- div")?; writeln!(writer, " xor rdx, rdx")?; writeln!(writer, " pop rbx")?; writeln!(writer, " pop rax")?; writeln!(writer, " div rbx")?; writeln!(writer, " push rax")?; - //writeln!(writer, " push rdx")?; + writeln!(writer, " push rdx")?; ti += 1; }, OpType::Mul => { @@ -276,7 +306,6 @@ pub fn compile(tokens: Vec, args: Args) -> Result{ writeln!(writer, " pop rbx")?; writeln!(writer, " mul rbx")?; writeln!(writer, " push rax")?; - //writeln!(writer, " push rdx")?; ti += 1; }, diff --git a/src/constants.rs b/src/constants.rs index 5d511cf..475aaed 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -22,11 +22,14 @@ pub enum OpType { Equals, Gt, Lt, + Ge, + Le, + NotEquals, Band, // & Bor, // | Shr, // >> Shl, // << - Div, // / + DivMod, // / Mul, @@ -98,11 +101,14 @@ impl OpType { &OpType::Equals => "=", &OpType::Gt => ">", &OpType::Lt => "<", + &OpType::NotEquals => "!=", + &OpType::Le => "<=", + &OpType::Ge => ">=", &OpType::Band => "band", &OpType::Bor => "bor", &OpType::Shr => "shr", &OpType::Shl => "shl", - &OpType::Div => "/", + &OpType::DivMod => "divmod", &OpType::Mul => "*", &OpType::If => "if", &OpType::Else => "else", diff --git a/src/interpret/linux_x86_64/mod.rs b/src/interpret/linux_x86_64/mod.rs index 163ded6..b8644ad 100644 --- a/src/interpret/linux_x86_64/mod.rs +++ b/src/interpret/linux_x86_64/mod.rs @@ -152,6 +152,24 @@ pub fn run(tokens: Vec) -> Result{ stack.push((b < a) as u64); ti += 1; }, + OpType::NotEquals => { + let a = stack_pop(&mut stack, &pos)?; + let b = stack_pop(&mut stack, &pos)?; + stack.push((b != a) as u64); + ti += 1; + }, + OpType::Ge => { + let a = stack_pop(&mut stack, &pos)?; + let b = stack_pop(&mut stack, &pos)?; + stack.push((b >= a) as u64); + ti += 1; + }, + OpType::Le => { + let a = stack_pop(&mut stack, &pos)?; + let b = stack_pop(&mut stack, &pos)?; + stack.push((b <= a) as u64); + ti += 1; + }, OpType::Band => { let a = stack_pop(&mut stack, &pos)?; @@ -181,10 +199,11 @@ pub fn run(tokens: Vec) -> Result{ ti += 1; } - OpType::Div => { + OpType::DivMod => { let a = stack_pop(&mut stack, &pos)?; let b = stack_pop(&mut stack, &pos)?; stack.push((b / a) as u64); + stack.push((b % a) as u64); ti += 1; } OpType::Mul => { diff --git a/src/parser.rs b/src/parser.rs index dffa457..82078b6 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -133,7 +133,7 @@ pub fn lookup_word>(s: String, _pos: P) -> ("bor", OpType::Bor), ("shr", OpType::Shr), ("shl", OpType::Shl), - ("/", OpType::Div), + ("divmod", OpType::DivMod), ("*", OpType::Mul), // block