From d5f3a55b69a0d6a7c610a8a1aed3b588db7f03b3 Mon Sep 17 00:00:00 2001 From: kuviman Date: Sun, 8 Dec 2024 19:20:13 +0400 Subject: [PATCH] testing hash_map --- examples/hash_map.ks | 5 +- std/lib.ks | 136 +++++++++++++++++++++--------------------- tests/examples.rs | 11 ++++ tests/hash_map.output | 5 ++ 4 files changed, 86 insertions(+), 71 deletions(-) create mode 100644 tests/hash_map.output diff --git a/examples/hash_map.ks b/examples/hash_map.ks index df22a82..526871e 100644 --- a/examples/hash_map.ks +++ b/examples/hash_map.ks @@ -3,12 +3,11 @@ use std.*; let mut map = HashMap_new (); map = HashMap_insert (map, "hello", "world"); map = HashMap_insert (map, "second", "2"); -dbg map; +#dbg map; dbg (HashMap_size map); dbg (HashMap_get (map, "hello")); dbg (HashMap_get (map, "world")); for key :: string, value :: string in HashMap_iter map { - dbg key; - dbg value; + print "iterated"; }; diff --git a/std/lib.ks b/std/lib.ks index 4335b1f..eb5cd37 100644 --- a/std/lib.ks +++ b/std/lib.ks @@ -3,21 +3,21 @@ module: const @"syntax" = import "./syntax.ks"; impl syntax @"syntax".invoke_macro = macro (.@"macro", .arg) => `( - compile_ast ($@"macro" !! `($arg)) + compile_ast ($@"macro" !! `($arg)) ); impl syntax @"syntax".pipe_right = macro (.arg, .f) => `( - let arg = $arg; - let f = $f; - f arg + let arg = $arg; + let f = $f; + f arg ); impl syntax @"syntax".pipe_left = macro (.f, .arg) => `( - let f = $f; - let arg = $arg; - f arg + let f = $f; + let arg = $arg; + f arg ); impl syntax @"syntax".let_infer = macro (.pattern) => `( - (let $pattern = _; $pattern) + (let $pattern = _; $pattern) ); const type = native "type"; @@ -42,9 +42,9 @@ const default_number_type :: type = native "default_number_type"; const panic :: string -> () = native "panic"; const print :: string -> () with output = line => ( - let output = current output; - output.write line; - output.write "\n"; + let output = current output; + output.write line; + output.write "\n"; ); const filesystem :: type = native "filesystem"; @@ -56,13 +56,13 @@ const read_file :: string -> string with filesystem = fn (path) { const contains :: (.s = string, .substring = string) -> bool = native "contains"; const dbg = forall[T] { - (value :: T) => ( - let output = current output; - output.write <| native "dbg" value; - output.write " :: "; - output.write <| native "dbg_type" T; - output.write "\n"; - ) + (value :: T) => ( + let output = current output; + output.write <| native "dbg" value; + output.write " :: "; + output.write <| native "dbg_type" T; + output.write "\n"; + ) }; # TODO T: randomizable @@ -112,23 +112,23 @@ const TypeName = (.name = string) as type; impl int32 as TypeName = (.name = "int32"); const Parse = forall[Self] { - .parse = string -> Self, + .parse = string -> Self, }; impl int32 as Parse = ( - .parse = native "parse", + .parse = native "parse", ); impl int64 as Parse = ( - .parse = native "parse", + .parse = native "parse", ); impl float64 as Parse = ( - .parse = native "parse", + .parse = native "parse", ); const parse = forall[T] { - (T as Parse).parse + (T as Parse).parse }; const generator_handler = forall[T] { @@ -137,31 +137,31 @@ const generator_handler = forall[T] { native "set_native" (.name = "generator_handler", .value = generator_handler); const loop_context = forall[T] { - ( - .@"break" = T -> () with (), # TODO never - .@"continue" = () -> () with (), - ) :: type + ( + .@"break" = T -> () with (), # TODO never + .@"continue" = () -> () with (), + ) :: type }; impl syntax @"syntax".@"loop" = macro (.body) => `( - unwindable for_loop ( - let body = () => ( - const BodyResult = forall[T] { newtype :Break T | :Continue }; - let body_result :: BodyResult[_] = unwindable body ( - with ( - .@"break" = () => unwind body (:Break ()), - .@"continue" = () => unwind body (:Continue), - ) :: loop_context[()]; - $body; - :Continue - ); - match body_result { - | :Break value => unwind for_loop value - | :Continue => () - }; - ); - native "loop" body - ) + unwindable for_loop ( + let body = () => ( + const BodyResult = forall[T] { newtype :Break T | :Continue }; + let body_result :: BodyResult[_] = unwindable body ( + with ( + .@"break" = () => unwind body (:Break ()), + .@"continue" = () => unwind body (:Continue), + ) :: loop_context[()]; + $body; + :Continue + ); + match body_result { + | :Break value => unwind for_loop value + | :Continue => () + }; + ); + native "loop" body + ) ); impl syntax @"syntax".@"while" = macro (.cond, .body) => `( @@ -171,41 +171,41 @@ impl syntax @"syntax".@"while" = macro (.cond, .body) => `( ); impl syntax @"syntax".for_loop = macro (.value_pattern, .generator, .body) => `( - unwindable for_loop ( - let handler = $value_pattern => ( - const BodyResult = forall[T] { newtype :Break T | :Continue }; - let body_result :: BodyResult[_] = unwindable body ( - with ( - .@"break" = () => unwind body (:Break ()), - .@"continue" = () => unwind body (:Continue), - ) :: loop_context[()]; - $body; - :Continue - ); - match body_result { - | :Break value => unwind for_loop value - | :Continue => () - }; - ); - with (.handle = handler) :: generator_handler[_]; - $generator; - ) + unwindable for_loop ( + let handler = $value_pattern => ( + const BodyResult = forall[T] { newtype :Break T | :Continue }; + let body_result :: BodyResult[_] = unwindable body ( + with ( + .@"break" = () => unwind body (:Break ()), + .@"continue" = () => unwind body (:Continue), + ) :: loop_context[()]; + $body; + :Continue + ); + match body_result { + | :Break value => unwind for_loop value + | :Continue => () + }; + ); + with (.handle = handler) :: generator_handler[_]; + $generator; + ) ); impl syntax @"syntax".@"yield" = macro (.value) => `( - (current generator_handler[_]).handle $value + (current generator_handler[_]).handle $value ); impl syntax @"syntax".break_without_value = macro _ => `( - break () + break () ); impl syntax @"syntax".break_with_value = macro (.value) => `( - (current loop_context[_]).@"break" $value + (current loop_context[_]).@"break" $value ); impl syntax @"syntax".@"continue" = macro _ => `( - (current loop_context[()]).@"continue" () # TODO infer loop context arg + (current loop_context[()]).@"continue" () # TODO infer loop context arg ); const char_ord :: char -> int32 = native "char_ord"; diff --git a/tests/examples.rs b/tests/examples.rs index dd211d9..27960ca 100644 --- a/tests/examples.rs +++ b/tests/examples.rs @@ -190,6 +190,17 @@ fn ast_nested_scope() { #[test] +fn test_hash_map() { + test(Case { + name: "hash_map", + comment_lines: None, + input: "", + expect_output: include_str!("hash_map.output"), + }); +} + +#[test] + fn test_unsafe() { test(Case { name: "unsafe", diff --git a/tests/hash_map.output b/tests/hash_map.output new file mode 100644 index 0000000..d28e6f4 --- /dev/null +++ b/tests/hash_map.output @@ -0,0 +1,5 @@ +2 :: int32 +:Some "world" :: | :Some string | :None +:None :: | :Some string | :None +iterated +iterated