Fixing array examples

This commit is contained in:
Krzosa Karol
2022-07-28 13:20:51 +02:00
parent 4a0234155c
commit 78a0f54319
5 changed files with 44 additions and 24 deletions

View File

@@ -77,8 +77,8 @@ struct Ast{
struct Ast_Type; struct Ast_Type;
struct Ast_Expr:Ast{ struct Ast_Expr:Ast{
Ast_Type *resolved_type;
union{ union{
Ast_Type *resolved_type;
Ast_Type *index_original_type; Ast_Type *index_original_type;
Ast_Type *cast_after_type; Ast_Type *cast_after_type;
}; };

View File

@@ -0,0 +1,27 @@
/*
Static arrays are exactly like c arrays, difference is
we can easily then get a slice of that array.
Slices are static arrays + length, they simplify array handling.
This allows us to pass an array into a function easily, then that function
can still access that length information easily.
Passing a pointer to array + length is a common pattern in C. So it would
be nice if handling of that was simplified.
*/
main :: (): int
static_array: [8]int
// We can get size of array using length_of builtin
#assert(length_of(static_array) == 8)
element: int = static_array[0]
assert(static_array[1] == 0)
assert(element == 0)
// Variables are zeroed by default
// assert(static_array[7] == 0)
// for static_array
// it = 1

View File

@@ -187,14 +187,9 @@ For modules it's a bit different cause they should be distributed as valid.
#include "compiler.cpp" #include "compiler.cpp"
#include "c_language_codegen.cpp" #include "c_language_codegen.cpp"
#include "intermediate_representation.cpp"
// #include "bytecode_interpreter.cpp"
// #include "bytecode.cpp"
#include "x64_funtime.cpp"
int main(int argument_count, char **arguments){ int main(int argument_count, char **arguments){
// test_x64_stuff();
#if OS_WINDOWS #if OS_WINDOWS
// Set output mode to handle virtual terminal sequences // Set output mode to handle virtual terminal sequences
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
@@ -221,7 +216,6 @@ int main(int argument_count, char **arguments){
test_array(); test_array();
test_string_builder(); test_string_builder();
test_intern_table(); test_intern_table();
test_bucket_arrays();
emit_line_directives = true; emit_line_directives = true;
String program_name = "examples/types_as_first_class_values.kl"_s; String program_name = "examples/types_as_first_class_values.kl"_s;
@@ -231,10 +225,11 @@ int main(int argument_count, char **arguments){
Scratch scratch; Scratch scratch;
Array<OS_File_Info> examples = os_list_dir(scratch, "examples"_s); Array<OS_File_Info> examples = os_list_dir(scratch, "examples"_s);
For(examples){ compile_file("examples/arrays_and_slices.kl"_s, COMPILE_AND_RUN);
if(it.is_directory) continue; // For(examples){
compile_file(it.absolute_path, COMPILE_AND_RUN); // if(it.is_directory) continue;
} // compile_file(it.absolute_path, COMPILE_AND_RUN);
// }
__debugbreak(); __debugbreak();
} }

View File

@@ -1,5 +1,5 @@
Os :: #import "os_windows.kl" Os :: #import "os_windows.kl"
SizeU :: U64 SizeU :: U64
arena_di: U64 arena_di: U64
Arena :: struct Arena :: struct

View File

@@ -587,10 +587,10 @@ resolve_stmt(Ast *ast, Ast_Type *ret){
Operand op; Operand op;
if(is_tuple(ret)){ if(is_tuple(ret)){
Ast_Type *sub_type = ret->agg.members[it.i].type; Ast_Type *sub_type = ret->agg.members[it.i].type;
op = resolve_expr(*it.it, AST_CAN_BE_NULL, sub_type); op = resolve_expr(*it.item, AST_CAN_BE_NULL, sub_type);
} }
else{ else{
op = resolve_expr(*it.it, AST_CAN_BE_NULL, ret); op = resolve_expr(*it.item, AST_CAN_BE_NULL, ret);
convert_untyped_to_typed(node->pos, &op.value, ret); convert_untyped_to_typed(node->pos, &op.value, ret);
} }
@@ -606,7 +606,7 @@ resolve_stmt(Ast *ast, Ast_Type *ret){
Ast_Type *sub_type = type; Ast_Type *sub_type = type;
if(is_tuple(type)) sub_type = type->agg.members[it.i].type; if(is_tuple(type)) sub_type = type->agg.members[it.i].type;
try_propagating_resolved_type_to_untyped_literals(*it.it, sub_type); try_propagating_resolved_type_to_untyped_literals(*it.item, sub_type);
} }
BREAK(); BREAK();
@@ -1054,6 +1054,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){
compiler_error(node->pos, "Trying to index the array with invalid type, expected [Int] got instead %Q", typestring(index.type)); compiler_error(node->pos, "Trying to index the array with invalid type, expected [Int] got instead %Q", typestring(index.type));
node->index_original_type = left.type; node->index_original_type = left.type;
node->resolved_type = left.type->arr.base;
// @todo: type_architecture? // @todo: type_architecture?
// we only try to convert the index cause array can't be const // we only try to convert the index cause array can't be const
@@ -1073,7 +1074,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){
compiler_error(node->pos, "Indexing variable that is not an [Array] or [Pointer], it's of type %Q instead", typestring(left.type)); compiler_error(node->pos, "Indexing variable that is not an [Array] or [Pointer], it's of type %Q instead", typestring(left.type));
} }
return operand_lvalue(left.type->arr.base); return operand_lvalue(node->resolved_type);
BREAK(); BREAK();
} }
@@ -1214,13 +1215,10 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){
CASE(LENGTH_OF, Builtin){ CASE(LENGTH_OF, Builtin){
Operand name = resolve_expr(node->expr, inherit_flag(flags, AST_CANT_BE_NULL)); Operand name = resolve_expr(node->expr, inherit_flag(flags, AST_CANT_BE_NULL));
node->resolved_type = type_s64; node->resolved_type = type_s64;
if(name.type == type_type){ if(is_array(name.type)){
if(is_array(name.type_val)){ Value value = value_int(name.type->arr.size);
Value value = value_int(name.type_val->arr.size); rewrite_into_const(node, Ast_Builtin, value);
rewrite_into_const(node, Ast_Builtin, value); return operand_const_rvalue(value);
return operand_const_rvalue(value);
}
else compiler_error(node->pos, "Can't get length of type %Q", typestring(name.type_val));
} }
else if(name.type->kind == TYPE_UNTYPED_STRING){ else if(name.type->kind == TYPE_UNTYPED_STRING){
Value value = value_int(name.intern_val.len); Value value = value_int(name.intern_val.len);