feat: Add read() builtin to read to array/string

This commit is contained in:
overflowerror 2024-04-27 18:30:40 +02:00
parent b346c25033
commit aa28a78102
4 changed files with 25 additions and 0 deletions

View file

@ -6,6 +6,7 @@
decl(print);
decl(read_char);
decl(read);
decl(to_str);
@ -15,6 +16,7 @@ static struct builtin_list_item {
} builtins[] = {
{"print", print},
{"read_char", read_char},
{"read", read},
{"to_str", to_str},
};

View file

@ -47,3 +47,22 @@ extern region_t* read_char(FILE* out, scope_t* scope, size_t argc, region_t** ar
return result;
}
extern region_t* read(FILE* out, scope_t* scope, size_t argc, region_t** argv) {
if (argc == 0) {
// default to read_char
return read_char(out, scope, argc, argv);
}
if (argc != 1) {
panic("read() has at most one argument");
}
region_t* region = argv[0];
for (size_t i = 0; i < region->size; i++) {
move_offset(region, i); input();
}
return region;
}

View file

@ -0,0 +1,2 @@
print(read([3]));
print("\n");

View file

@ -0,0 +1,2 @@
>>>[-]>[-]<[-<<<+>>>>+<]>[-<+>]<[-]>[-]<[-<<+>>>+<]>[-<+>]<[-]>[-]<[-<+>>+<]>[-<+>]<<<<,>,>,<<.>.>.
<<[-]++++++++++.