diff --git a/src/main.rs b/src/main.rs index badbbe7..4ac1308 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,6 @@ use core::panic; use std::collections::HashMap; use std::env; -use std::fmt::format; use std::fs; use std::iter::Peekable; use std::process::exit; @@ -24,25 +23,25 @@ enum TokenizerState Comment, } -#[derive(Debug,Clone,Copy)] +#[derive(Debug,Clone,Copy, PartialEq)] enum Datatype { Int, String, Bool, //Pointer, - Any, + // Any, } -impl PartialEq for Datatype -{ - fn eq(&self, other: &Self) -> bool - { - core::mem::discriminant(self) == core::mem::discriminant(&Datatype::Any) || - core::mem::discriminant(other) == core::mem::discriminant(&Datatype::Any) || - core::mem::discriminant(self) == core::mem::discriminant(other) - } -} +// impl PartialEq for Datatype +// { +// fn eq(&self, other: &Self) -> bool +// { +// core::mem::discriminant(self) == core::mem::discriminant(&Datatype::Any) || +// core::mem::discriminant(other) == core::mem::discriminant(&Datatype::Any) || +// core::mem::discriminant(self) == core::mem::discriminant(other) +// } +// } #[derive(Debug)] struct Function @@ -84,8 +83,9 @@ fn main() { let intrinsics: HashMap<&str, (Vec, Vec)> = HashMap::from( [ - ("print", (Vec::from([Datatype::Any]), Vec::new())), - ("println", (Vec::from([Datatype::Any]), Vec::new())), + ("print", (Vec::from([Datatype::String]), Vec::new())), + ("println", (Vec::from([Datatype::String]), Vec::new())), + ("intToStr", (Vec::from([Datatype::Int]), Vec::from([Datatype::String]))), ("-", (Vec::from([Datatype::Int, Datatype::Int]), Vec::from([Datatype::Int]))), ("+", (Vec::from([Datatype::Int, Datatype::Int]), Vec::from([Datatype::Int]))), ("*", (Vec::from([Datatype::Int, Datatype::Int]), Vec::from([Datatype::Int]))), @@ -494,6 +494,11 @@ fn interpret_program(operations: &Vec, queue: &mut Vec, funct { output += format!("{}\n", queue.remove(0)).as_str(); } + "intToStr" => + { + let val = queue.remove(0).clone(); + queue.push(val); + } _ => { return Err(format!("Unexpected intrinsic '{}' at {}:{}", intrinsic_name, line, col)); @@ -948,7 +953,7 @@ fn extract_arrays(tokens: &mut Vec, intrinsics: &HashMap<&str, (Vec = Vec::new(); let default_val = match datatype { - Datatype::Any | Datatype::String => String::new(), + Datatype::String => String::new(), Datatype::Bool => String::from("false"), Datatype::Int => String::from("0"), }; @@ -988,7 +993,7 @@ fn str_to_datatype(s: &str, line: i32, col: i32) -> Result { match s { - "any" => Ok(Datatype::Any), + //"any" => Ok(Datatype::Any), "bool" => Ok(Datatype::Bool), "int" => Ok(Datatype::Int), "str" => Ok(Datatype::String), @@ -1034,7 +1039,7 @@ fn extract_functions(tokens: &mut Vec, intrinsics: &HashMap<&str, (Vec ins.push(Datatype::Any), + //"any" => ins.push(Datatype::Any), "str" => ins.push(Datatype::String), "int" => ins.push(Datatype::Int), "bool" => ins.push(Datatype::Bool), @@ -1069,7 +1074,7 @@ fn extract_functions(tokens: &mut Vec, intrinsics: &HashMap<&str, (Vec outs.push(Datatype::Any), + //"any" => outs.push(Datatype::Any), "str" => outs.push(Datatype::String), "int" => outs.push(Datatype::Int), "bool" => outs.push(Datatype::Bool), diff --git a/tests/array_tests.qbl b/tests/array_tests.qbl index a75cb25..30d04f4 100644 --- a/tests/array_tests.qbl +++ b/tests/array_tests.qbl @@ -10,7 +10,8 @@ function => dump true 0 while { - dup test.read req print + // i + dup test.read req intToStr req print 1 + test.length dup < req } deq "" println diff --git a/tests/invalid_function_name_intrinsic.qbl b/tests/invalid_function_name_intrinsic.qbl index bb66172..09895a7 100644 --- a/tests/invalid_function_name_intrinsic.qbl +++ b/tests/invalid_function_name_intrinsic.qbl @@ -1,8 +1,8 @@ //invalid,Function name print at 3:22 is already an intrinsic:END: -function int => print +function str => print { deq } -42 print +"42" print diff --git a/tests/invalid_function_name_operation.qbl b/tests/invalid_function_name_operation.qbl index dbe3236..f7571cd 100644 --- a/tests/invalid_function_name_operation.qbl +++ b/tests/invalid_function_name_operation.qbl @@ -1,8 +1,8 @@ //invalid,Expected function name but got deq at 3:20:END: -function int => deq +function str => deq { deq } -42 print \ No newline at end of file +"42" print \ No newline at end of file diff --git a/tests/missing_function_name.qbl b/tests/missing_function_name.qbl index be8070a..1237085 100644 --- a/tests/missing_function_name.qbl +++ b/tests/missing_function_name.qbl @@ -1,8 +1,8 @@ //invalid,Expected function name but got { at 4:2:END: -function int => +function str => { deq } -42 print +"42" print diff --git a/tests/recursion.qbl b/tests/recursion.qbl index f942510..9a8ff23 100644 --- a/tests/recursion.qbl +++ b/tests/recursion.qbl @@ -14,4 +14,4 @@ function int int int int => int fibonacci } } -20 0 1 0 fibonacci println \ No newline at end of file +20 0 1 0 fibonacci intToStr println \ No newline at end of file diff --git a/tests/req_impl.qbl b/tests/req_impl.qbl index f8d5d12..9b4351e 100644 --- a/tests/req_impl.qbl +++ b/tests/req_impl.qbl @@ -4,5 +4,5 @@ function int => int req_impl { } -1 2 3 req_impl print print println -1 2 3 req print print println \ No newline at end of file +1 2 3 req_impl intToStr intToStr intToStr print print println +1 2 3 req intToStr intToStr intToStr print print println \ No newline at end of file diff --git a/tests/test.qbl b/tests/test.qbl index 152d4b0..281254d 100644 --- a/tests/test.qbl +++ b/tests/test.qbl @@ -5,9 +5,9 @@ // Dequeues, enqueues 42 and 17, prints the head -function any => int foo +function int => int foo { - deq 42 17 print + deq 42 17 intToStr req print } "test2" print false diff --git a/tests/typecheck_function_multiple_io.qbl b/tests/typecheck_function_multiple_io.qbl index 054ae6b..9108f09 100644 --- a/tests/typecheck_function_multiple_io.qbl +++ b/tests/typecheck_function_multiple_io.qbl @@ -1,9 +1,9 @@ //valid,42footesttest2stuff //:END: -function int str any => str str str foo +function int str str => str str str foo { - print req deq "test" "test2" "stuff" print + intToStr req req print req deq "test" "test2" "stuff" print } 42 "foo" "bar" foo print print println \ No newline at end of file diff --git a/tests/while.qbl b/tests/while.qbl index 69c8da0..69fb4de 100644 --- a/tests/while.qbl +++ b/tests/while.qbl @@ -7,16 +7,29 @@ true while } 10 0 dup > req +// true 10 while { - dup print - + // i + dup intToStr req print 1 - 0 dup > req } deq +function bool => str boolToStr +{ + if + { + "true" + } + else + { + "false" + } +} + true true true while { false } -print println \ No newline at end of file +boolToStr boolToStr print println \ No newline at end of file