add != >= <= ops and change / to divmod and add int.mcl stdlib entry

This commit is contained in:
MCorange 2023-03-20 16:53:30 +02:00
parent 427f904d0b
commit 3953cd7ad6
5 changed files with 69 additions and 10 deletions

5
include/int.mcl Normal file
View File

@ -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

View File

@ -202,7 +202,6 @@ pub fn compile(tokens: Vec<Operator>, args: Args) -> Result<i32>{
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<Operator>, args: Args) -> Result<i32>{
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<Operator>, args: Args) -> Result<i32>{
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<Operator>, args: Args) -> Result<i32>{
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<Operator>, args: Args) -> Result<i32>{
writeln!(writer, " pop rbx")?;
writeln!(writer, " mul rbx")?;
writeln!(writer, " push rax")?;
//writeln!(writer, " push rdx")?;
ti += 1;
},

View File

@ -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",

View File

@ -152,6 +152,24 @@ pub fn run(tokens: Vec<crate::constants::Operator>) -> Result<i32>{
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<crate::constants::Operator>) -> Result<i32>{
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 => {

View File

@ -133,7 +133,7 @@ pub fn lookup_word<P: Deref<Target = (String, u32, u32)>>(s: String, _pos: P) ->
("bor", OpType::Bor),
("shr", OpType::Shr),
("shl", OpType::Shl),
("/", OpType::Div),
("divmod", OpType::DivMod),
("*", OpType::Mul),
// block