feat: Add basic map loop implementation

This commit is contained in:
overflowerror 2024-05-02 15:44:18 +02:00
parent 3567a3c40b
commit 5b4d040488
6 changed files with 111 additions and 0 deletions

51
brainfuck/main.c Normal file
View file

@ -0,0 +1,51 @@
#include <stdio.h>
#include <string.h>
#define BAND_LENGTH (30000)
char band[BAND_LENGTH];
size_t position = 0;
void init_band() {
memset(band, 0, BAND_LENGTH);
}
int error(const char* msg) {
fprintf(stderr, "%s\n", msg);
return 1;
}
int run(FILE* input) {
int c;
while((c = getc(input)) != EOF) {
switch (c) {
case '+': band[position]++; break;
case '-': band[position]--; break;
case '<':
if (--position < 0) return error("band underflow");
break;
case '>':
if (++position >= BAND_LENGTH) return error("band overflow");
break;
case '.': putchar(band[position]); break;
case ',': band[position] = getchar(); break;
}
}
return 0;
}
int main(int argc, char** argv) {
if (argc != 2) {
return 1;
}
FILE* input = stdin;
if (strcmp(argv[1], "-") != 0) {
input = fopen(argv[1], "r");
}
init_band();
return run(input);
}

View file

@ -148,6 +148,32 @@ static void codegen_while_statement(FILE* out, scope_t* scope, struct while_stat
}
static void codegen_map_statement(FILE* out, scope_t* scope, struct map_statement statement) {
// we need a local scope for index and value
scope_t* local_scope = scope_new(scope);
region_t* list = scope_get(scope, statement.list_id);
if (!list) {
fprintf(stderr, "variable not found: %s\n", statement.list_id);
panic("variable not found");
}
region_t* index = scope_add(local_scope, statement.index_id, 1);
move_to(index); reset();
region_t* value = scope_add_ref(local_scope, list, 0, 1);
scope_existing(local_scope, value, statement.ref_id);
for (size_t i = 0; i < list->size; i++) {
codegen_block(out, local_scope, statement.block);
value->start++;
move_to(index); inc();
}
scope_free(local_scope);
}
static void codegen_expr_statement(FILE* out, scope_t* scope, struct expr_statement statement) {
region_t* region = codegen_expr(out, scope, statement.expr);
if (region->is_temp) {
@ -172,6 +198,9 @@ void codegen_statement(FILE* out, scope_t* scope, struct statement* statement) {
case WHILE_STATEMENT:
codegen_while_statement(out, scope, statement->while_loop);
break;
case MAP_STATEMENT:
codegen_map_statement(out, scope, statement->map_loop);
break;
default:
fprintf(stderr, "statement kind: %d\n", statement->kind);
panic("unknown statement kind");

View file

@ -0,0 +1,6 @@
var list = []{'f', 'o', 'o', 'b', 'a', 'r'};
map(_, value in list) {
print(value);
print('\n');
}

View file

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

View file

@ -0,0 +1,5 @@
var list = []{0, 0, 0, 0};
map(index, _ in list) {
print(to_str(index), '\n');
}

View file

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