diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-03-08 14:23:16 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-03-08 14:23:16 -0500 |
| commit | 07c2b0ec26600d8609870d9f5c0867d48b801db4 (patch) | |
| tree | 9c7f5cf8c394312e3da4a2371081d2a64b622b6e | |
| parent | 8e62018546cd9beb357b0452535cf216c7cfa0cc (diff) | |
Array methods
| -rw-r--r-- | builtins/array.c | 15 | ||||
| -rw-r--r-- | builtins/array.h | 4 | ||||
| -rw-r--r-- | compile.c | 164 | ||||
| -rw-r--r-- | typecheck.c | 42 | ||||
| -rw-r--r-- | types.h | 6 |
5 files changed, 160 insertions, 71 deletions
diff --git a/builtins/array.c b/builtins/array.c index 245c2202..8ee771ea 100644 --- a/builtins/array.c +++ b/builtins/array.c @@ -54,7 +54,7 @@ public void Array__insert(array_t *arr, const void *item, int64_t index, const T arr->free = 4; arr->data = arr->atomic ? GC_MALLOC_ATOMIC(arr->free * item_size) : GC_MALLOC(arr->free * item_size); arr->stride = item_size; - } else if (arr->free < 1 || (int64_t)arr->stride != item_size) { + } else if (arr->free < 1 || arr->data_refcount || (int64_t)arr->stride != item_size) { arr->free = MAX(15, MIN(1, arr->length/4)); void *copy = arr->atomic ? GC_MALLOC_ATOMIC((arr->length + arr->free) * item_size) : GC_MALLOC((arr->length + arr->free) * item_size); for (int64_t i = 0; i < index-1; i++) @@ -65,9 +65,6 @@ public void Array__insert(array_t *arr, const void *item, int64_t index, const T arr->data_refcount = 0; arr->stride = item_size; } else { - if (arr->data_refcount) - Array__compact(arr, type); - if (index != arr->length+1) memmove((void*)arr->data + index*item_size, arr->data + (index-1)*item_size, (arr->length - index)*item_size); } @@ -88,7 +85,7 @@ public void Array__insert_all(array_t *arr, array_t to_insert, int64_t index, co if (!arr->data) { arr->free = to_insert.length; arr->data = arr->atomic ? GC_MALLOC_ATOMIC(item_size*arr->free) : GC_MALLOC(item_size*arr->free); - } else if ((int64_t)arr->free < (int64_t)to_insert.length || (int64_t)arr->stride != item_size) { + } else if ((int64_t)arr->free < (int64_t)to_insert.length || arr->data_refcount || (int64_t)arr->stride != item_size) { arr->free = to_insert.length; void *copy = arr->atomic ? GC_MALLOC_ATOMIC((arr->length + arr->free) * item_size) : GC_MALLOC((arr->length + arr->free) * item_size); for (int64_t i = 0; i < index-1; i++) @@ -98,9 +95,6 @@ public void Array__insert_all(array_t *arr, array_t to_insert, int64_t index, co arr->data = copy; arr->data_refcount = 0; } else { - if (arr->data_refcount) - Array__compact(arr, type); - if (index != arr->length+1) memmove((void*)arr->data + index*item_size, arr->data + (index-1)*item_size, (arr->length - index + to_insert.length-1)*item_size); } @@ -179,7 +173,7 @@ public void *Array__random(array_t arr) return arr.data + arr.stride*index; } -public array_t Array__slice(array_t *array, int64_t first, int64_t stride, int64_t length, const TypeInfo *type) +public array_t Array__slice(array_t *array, int64_t first, int64_t length, int64_t stride, const TypeInfo *type) { if (stride > MAX_STRIDE || stride < MIN_STRIDE) fail("Stride is too big: %ld", stride); @@ -263,9 +257,8 @@ public bool Array__contains(array_t array, void *item, const TypeInfo *type) return false; } -public void Array__clear(array_t *array, const TypeInfo *type) +public void Array__clear(array_t *array) { - (void)type; *array = (array_t){.data=0, .length=0}; } diff --git a/builtins/array.h b/builtins/array.h index 926dd4af..52fd65e3 100644 --- a/builtins/array.h +++ b/builtins/array.h @@ -52,10 +52,10 @@ void Array__remove(array_t *arr, int64_t index, int64_t count, const TypeInfo *t void Array__sort(array_t *arr, const TypeInfo *type); void Array__shuffle(array_t *arr, const TypeInfo *type); void *Array__random(array_t arr); -void Array__clear(array_t *array, const TypeInfo *type); +void Array__clear(array_t *array); void Array__compact(array_t *arr, const TypeInfo *type); bool Array__contains(array_t array, void *item, const TypeInfo *type); -array_t Array__slice(array_t *array, int64_t first, int64_t stride, int64_t length, const TypeInfo *type); +array_t Array__slice(array_t *array, int64_t first, int64_t length, int64_t stride, const TypeInfo *type); array_t Array__concat(array_t x, array_t y, const TypeInfo *type); uint32_t Array__hash(const array_t *arr, const TypeInfo *type); int32_t Array__compare(const array_t *x, const array_t *y, const TypeInfo *type); @@ -148,6 +148,68 @@ static void check_assignable(env_t *env, ast_t *ast) } } +static CORD compile_arguments(env_t *env, ast_t *call_ast, arg_t *spec_args, arg_ast_t *call_args) +{ + table_t used_args = {}; + CORD code = CORD_EMPTY; + env_t *default_scope = fresh_scope(env); + default_scope->locals->fallback = env->globals; + for (arg_t *spec_arg = spec_args; spec_arg; spec_arg = spec_arg->next) { + // Find keyword: + if (spec_arg->name) { + for (arg_ast_t *call_arg = call_args; call_arg; call_arg = call_arg->next) { + if (call_arg->name && streq(call_arg->name, spec_arg->name)) { + type_t *actual_t = get_type(env, call_arg->value); + if (!can_promote(actual_t, spec_arg->type)) + code_err(call_arg->value, "This argument is supposed to be a %T, but this value is a %T", spec_arg->type, actual_t); + Table_str_set(&used_args, call_arg->name, call_arg); + if (code) code = CORD_cat(code, ", "); + code = CORD_cat(code, compile(env, call_arg->value)); + goto found_it; + } + } + } + // Find positional: + int64_t i = 1; + for (arg_ast_t *call_arg = call_args; call_arg; call_arg = call_arg->next) { + if (call_arg->name) continue; + const char *pseudoname = heap_strf("%ld", i++); + if (!Table_str_get(&used_args, pseudoname)) { + type_t *actual_t = get_type(env, call_arg->value); + if (!can_promote(actual_t, spec_arg->type)) + code_err(call_arg->value, "This argument is supposed to be a %T, but this value is a %T", spec_arg->type, actual_t); + Table_str_set(&used_args, pseudoname, call_arg); + if (code) code = CORD_cat(code, ", "); + code = CORD_cat(code, compile(env, call_arg->value)); + goto found_it; + } + } + + if (spec_arg->default_val) { + if (code) code = CORD_cat(code, ", "); + code = CORD_cat(code, compile(default_scope, spec_arg->default_val)); + goto found_it; + } + + assert(spec_arg->name); + code_err(call_ast, "The required argument '%s' was not provided", spec_arg->name); + found_it: continue; + } + + int64_t i = 1; + for (arg_ast_t *call_arg = call_args; call_arg; call_arg = call_arg->next) { + if (call_arg->name) { + if (!Table_str_get(&used_args, call_arg->name)) + code_err(call_arg->value, "There is no argument with the name '%s'", call_arg->name); + } else { + const char *pseudoname = heap_strf("%ld", i++); + if (!Table_str_get(&used_args, pseudoname)) + code_err(call_arg->value, "This is one argument too many!"); + } + } + return code; +} + CORD compile(env_t *env, ast_t *ast) { switch (ast->tag) { @@ -639,7 +701,55 @@ CORD compile(env_t *env, ast_t *ast) env->code->funcs = CORD_all(env->code->funcs, code, " ", body); return CORD_EMPTY; } - case FunctionCall: case MethodCall: { + case MethodCall: { + auto call = Match(ast, MethodCall); + type_t *self_t = get_type(env, call->self); + type_t *self_value_t = value_type(self_t); + switch (self_value_t->tag) { + case ArrayType: { + // TODO: check for readonly + if (streq(call->name, "insert")) { + type_t *item_t = Match(self_value_t, ArrayType)->item_type; + CORD self = compile_to_pointer_depth(env, call->self, 1, false); + arg_t *arg_spec = new(arg_t, .name="item", .type=Type(PointerType, .pointed=item_t, .is_stack=true, .is_readonly=true), + .next=new(arg_t, .name="at", .type=Type(IntType, .bits=64), .default_val=FakeAST(Int, .i=0, .bits=64))); + return CORD_all("Array__insert(", self, ", ", compile_arguments(env, ast, arg_spec, call->args), ", ", + compile_type_info(env, self_value_t), ")"); + } else if (streq(call->name, "remove")) { + CORD self = compile_to_pointer_depth(env, call->self, 1, false); + arg_t *arg_spec = new(arg_t, .name="index", .type=Type(IntType, .bits=64), .default_val=FakeAST(Int, .i=-1, .bits=64), + .next=new(arg_t, .name="count", .type=Type(IntType, .bits=64), .default_val=FakeAST(Int, .i=1, .bits=64))); + return CORD_all("Array__remove(", self, ", ", compile_arguments(env, ast, arg_spec, call->args), ", ", + compile_type_info(env, self_value_t), ")"); + } else if (streq(call->name, "random")) { + CORD self = compile_to_pointer_depth(env, call->self, 0, false); + return CORD_all("Array__random(", self, ")"); + } else if (streq(call->name, "shuffle")) { + CORD self = compile_to_pointer_depth(env, call->self, 1, false); + return CORD_all("Array__shuffle(", self, ", ", compile_type_info(env, self_value_t), ")"); + } else if (streq(call->name, "sort")) { + CORD self = compile_to_pointer_depth(env, call->self, 1, false); + return CORD_all("Array__sort(", self, ", ", compile_type_info(env, self_value_t), ")"); + } else if (streq(call->name, "clear")) { + CORD self = compile_to_pointer_depth(env, call->self, 1, false); + return CORD_all("Array__compact(", self, ")"); + } else if (streq(call->name, "slice")) { + CORD self = compile_to_pointer_depth(env, call->self, 1, false); + arg_t *arg_spec = new(arg_t, .name="first", .type=Type(IntType, .bits=64), .default_val=FakeAST(Int, .i=1, .bits=64), + .next=new(arg_t, .name="length", .type=Type(IntType, .bits=64), .default_val=FakeAST(Int, .i=INT64_MAX, .bits=64), + .next=new(arg_t, .name="stride", .type=Type(IntType, .bits=64), .default_val=FakeAST(Int, .i=1, .bits=64)))); + return CORD_all("Array__slice(", self, ", ", compile_arguments(env, ast, arg_spec, call->args), ", ", + compile_type_info(env, self_value_t), ")"); + } else code_err(ast, "There is no '%s' method for arrays", call->name); + } + case TableType: { + goto fncall; + } + default: goto fncall; + } + } + case FunctionCall: { + fncall:; type_t *fn_t; arg_ast_t *args; CORD fn; @@ -665,56 +775,8 @@ CORD compile(env_t *env, ast_t *ast) fn = b->code; } - CORD code = CORD_cat_char(fn, '('); - // Pass 1: assign keyword args - // Pass 2: assign positional args - // Pass 3: compile and typecheck each arg - table_t arg_bindings = {}; - for (arg_ast_t *call_arg = args; call_arg; call_arg = call_arg->next) { - if (call_arg->name) - Table_str_set(&arg_bindings, call_arg->name, call_arg->value); - } - for (arg_ast_t *call_arg = args; call_arg; call_arg = call_arg->next) { - if (call_arg->name) - continue; - - const char *name = NULL; - for (arg_t *fn_arg = Match(fn_t, FunctionType)->args; fn_arg; fn_arg = fn_arg->next) { - if (!Table_str_get(&arg_bindings, fn_arg->name)) { - name = fn_arg->name; - break; - } - } - if (name) - Table_str_set(&arg_bindings, name, call_arg->value); - else - code_err(call_arg->value, "This is too many arguments to the function: %T", fn_t); - } - - // TODO: ensure args get executed in order (e.g. `foo(y=get_next(1), x=get_next(2))` - // should not execute out of order) - for (arg_t *fn_arg = Match(fn_t, FunctionType)->args; fn_arg; fn_arg = fn_arg->next) { - ast_t *arg = Table_str_get(&arg_bindings, fn_arg->name); - if (arg) { - Table_str_remove(&arg_bindings, fn_arg->name); - } else { - arg = fn_arg->default_val; - } - if (!arg) - code_err(ast, "The required argument '%s' is not provided", fn_arg->name); - - code = CORD_cat(code, compile(env, arg)); - if (fn_arg->next) code = CORD_cat(code, ", "); - } - - struct { - const char *name; - ast_t *ast; - } *invalid = Table_str_entry(&arg_bindings, 1); - if (invalid) - code_err(invalid->ast, "There is no argument named %s for %T", invalid->name, fn_t); - - return CORD_cat_char(code, ')'); + CORD code = CORD_all(fn, "(", compile_arguments(env, ast, Match(fn_t, FunctionType)->args, args), ")"); + return code; } case If: { auto if_ = Match(ast, If); diff --git a/typecheck.c b/typecheck.c index ac6ed811..47fe8c64 100644 --- a/typecheck.c +++ b/typecheck.c @@ -403,13 +403,41 @@ type_t *get_type(env_t *env, ast_t *ast) } case MethodCall: { auto call = Match(ast, MethodCall); - type_t *fn_type_t = get_method_type(env, call->self, call->name); - if (!fn_type_t) - code_err(ast, "No such method!"); - if (fn_type_t->tag != FunctionType) - code_err(ast, "This isn't a method, it's a %T", fn_type_t); - auto fn_type = Match(fn_type_t, FunctionType); - return fn_type->ret; + type_t *self_value_t = value_type(get_type(env, call->self)); + switch (self_value_t->tag) { + case ArrayType: { + if (streq(call->name, "insert")) return Type(VoidType); + else if (streq(call->name, "insert_all")) return Type(VoidType); + else if (streq(call->name, "remove")) return Type(VoidType); + else if (streq(call->name, "sort")) return Type(VoidType); + else if (streq(call->name, "shuffle")) return Type(VoidType); + else if (streq(call->name, "random")) + return Type(PointerType, .pointed=Match(self_value_t, ArrayType)->item_type, .is_optional=true, .is_readonly=true); + else if (streq(call->name, "clear")) return Type(VoidType); + else if (streq(call->name, "compact")) return Type(VoidType); + else if (streq(call->name, "contains")) return Type(VoidType); + else if (streq(call->name, "slice")) return self_value_t; + else if (streq(call->name, "concat")) return self_value_t; + else code_err(ast, "There is no '%s' method for arrays", call->name); + } + case TableType: { + auto table = Match(self_value_t, TableType); + if (streq(call->name, "get")) return Type(PointerType, .pointed=table->value_type, .is_readonly=true, .is_optional=true); + else if (streq(call->name, "set")) return Type(VoidType); + else if (streq(call->name, "remove")) return Type(VoidType); + else if (streq(call->name, "clear")) return Type(VoidType); + else code_err(ast, "There is no '%s' method for tables", call->name); + } + default: { + type_t *fn_type_t = get_method_type(env, call->self, call->name); + if (!fn_type_t) + code_err(ast, "No such method!"); + if (fn_type_t->tag != FunctionType) + code_err(ast, "This isn't a method, it's a %T", fn_type_t); + auto fn_type = Match(fn_type_t, FunctionType); + return fn_type->ret; + } + } } case Block: { auto block = Match(ast, Block); @@ -16,6 +16,12 @@ typedef struct arg_s { struct arg_s *next; } arg_t; +#define ARG_LIST(...) ({\ + arg_t *args[] = {__VA_ARGS__}; \ + for (size_t i = 0; i < sizeof(args)/sizeof(args[0])-1; i++) \ + args[i]->next = args[i+1]; \ + args[0]; }) + typedef struct tag_s { const char *name; int64_t tag_value; |
