From 5b4749bbf3121e782af53466d0de75eb1449ddb4 Mon Sep 17 00:00:00 2001 From: Jan Max Meyer Date: Mon, 11 Nov 2024 01:11:20 +0100 Subject: [PATCH] Added more tests and fixed Int+=Float bug --- src/compiler/prelude.rs | 158 +++++++++++++++++++++++++++++++++++++++- src/prelude.tok | 5 +- src/value/refvalue.rs | 2 +- tests/aggregates.tok | 40 ++++++++++ 4 files changed, 200 insertions(+), 5 deletions(-) diff --git a/src/compiler/prelude.rs b/src/compiler/prelude.rs index dff40aa..82a82fe 100644 --- a/src/compiler/prelude.rs +++ b/src/compiler/prelude.rs @@ -1464,11 +1464,163 @@ impl Compiler { "children" => (value!([ (value!([ - "emit" => "op_unary_not", + "emit" => "op_logical_or", "children" => (value!([ - "emit" => "identifier", - "value" => "cnt" + (value!([ + "emit" => "op_unary_not", + "children" => + (value!([ + "emit" => "identifier", + "value" => "cnt" + ])) + ])), + (value!([ + "emit" => "op_logical_and", + "children" => + (value!([ + (value!([ + "emit" => "op_logical_and", + "children" => + (value!([ + (value!([ + "emit" => "op_logical_and", + "children" => + (value!([ + (value!([ + "emit" => "comparison", + "children" => + (value!([ + (value!([ + "emit" => "call", + "children" => + (value!([ + (value!([ + "emit" => "identifier", + "value" => "type" + ])), + (value!([ + "emit" => "callarg", + "children" => + (value!([ + "emit" => "identifier", + "value" => "res" + ])) + ])) + ])) + ])), + (value!([ + "emit" => "cmp_neq", + "children" => + (value!([ + "emit" => "value_string", + "value" => "int" + ])) + ])) + ])) + ])), + (value!([ + "emit" => "comparison", + "children" => + (value!([ + (value!([ + "emit" => "call", + "children" => + (value!([ + (value!([ + "emit" => "identifier", + "value" => "type" + ])), + (value!([ + "emit" => "callarg", + "children" => + (value!([ + "emit" => "identifier", + "value" => "res" + ])) + ])) + ])) + ])), + (value!([ + "emit" => "cmp_neq", + "children" => + (value!([ + "emit" => "value_string", + "value" => "float" + ])) + ])) + ])) + ])) + ])) + ])), + (value!([ + "emit" => "comparison", + "children" => + (value!([ + (value!([ + "emit" => "call", + "children" => + (value!([ + (value!([ + "emit" => "identifier", + "value" => "type" + ])), + (value!([ + "emit" => "callarg", + "children" => + (value!([ + "emit" => "identifier", + "value" => "res" + ])) + ])) + ])) + ])), + (value!([ + "emit" => "cmp_neq", + "children" => + (value!([ + "emit" => "value_string", + "value" => "bool" + ])) + ])) + ])) + ])) + ])) + ])), + (value!([ + "emit" => "comparison", + "children" => + (value!([ + (value!([ + "emit" => "call", + "children" => + (value!([ + (value!([ + "emit" => "identifier", + "value" => "type" + ])), + (value!([ + "emit" => "callarg", + "children" => + (value!([ + "emit" => "identifier", + "value" => "res" + ])) + ])) + ])) + ])), + (value!([ + "emit" => "cmp_neq", + "children" => + (value!([ + "emit" => "value_string", + "value" => "null" + ])) + ])) + ])) + ])) + ])) + ])) ])) ])), (value!([ diff --git a/src/prelude.tok b/src/prelude.tok index 13fbe88..371bed2 100644 --- a/src/prelude.tok +++ b/src/prelude.tok @@ -148,6 +148,9 @@ avg : @value { cnt++ } - if !cnt return void + # TODO: Facilities for type checking divisable types must be improved! + if !cnt || (type(res) != "int" && type(res) != "float" && type(res) != "bool" && type(res) != "null") + return void + res / cnt } diff --git a/src/value/refvalue.rs b/src/value/refvalue.rs index a0df654..24d7181 100644 --- a/src/value/refvalue.rs +++ b/src/value/refvalue.rs @@ -144,7 +144,7 @@ impl RefValue { }, // Int inline fast-lane - (Value::Int(int), _) => match op { + (Value::Int(int), no_float) if !no_float.is("float") => match op { "iadd" => { *int += that.to_i64()?; return Ok(self.clone()); diff --git a/tests/aggregates.tok b/tests/aggregates.tok index 4751cd4..c5527fc 100644 --- a/tests/aggregates.tok +++ b/tests/aggregates.tok @@ -1,6 +1,7 @@ #testmode:repl # Aggregate Functions +# aggregate with list of integers l = list(range(10)) min(l) @@ -8,23 +9,53 @@ max(l) sum(l) avg(l) +# aggregate with list of mixed float/int +l = 1, 23.5, 42, 3.1415 + +min(l) +max(l) +sum(l) +avg(l) + +# aggregate with single value min(42) max(42) sum(42) avg(42) +# aggregate with void print(type(min(void))) print(type(max(void))) print(type(sum(void))) print(type(avg(void))) +# special cases + +## aggregate str + +s = "Hello" +min(s) +max(s) +sum(s) +print(type(avg(s))) # should be void + +## aggrgate list of list, or mixed +sum(((1,2),3)) +sum((1,(2,3))) +sum(((1,2),(3,4))) #--- + #0 #9 #45 #4.5 +#1 +#42 +#69.6415 +#17.410375 + #42 #42 #42 @@ -34,3 +65,12 @@ print(type(avg(void))) #void #void #void + +#"H" +#"o" +#"Hello" +#void + +#(1, 2, 3) +#(1, 2, 3) +#(1, 2, 3, 4)