Skip to content

Commit

Permalink
testing hash_map
Browse files Browse the repository at this point in the history
  • Loading branch information
kuviman committed Dec 8, 2024
1 parent 3c7538c commit d5f3a55
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 71 deletions.
5 changes: 2 additions & 3 deletions examples/hash_map.ks
Original file line number Diff line number Diff line change
Expand Up @@ -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";
};
136 changes: 68 additions & 68 deletions std/lib.ks
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
Expand All @@ -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
Expand Down Expand Up @@ -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] {
Expand All @@ -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) => `(
Expand All @@ -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";
Expand Down
11 changes: 11 additions & 0 deletions tests/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions tests/hash_map.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
2 :: int32
:Some "world" :: | :Some string | :None
:None :: | :Some string | :None
iterated
iterated

0 comments on commit d5f3a55

Please sign in to comment.