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_Expr:Ast{
Ast_Type *resolved_type;
union{
Ast_Type *resolved_type;
Ast_Type *index_original_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 "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){
// test_x64_stuff();
#if OS_WINDOWS
// Set output mode to handle virtual terminal sequences
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
@@ -221,7 +216,6 @@ int main(int argument_count, char **arguments){
test_array();
test_string_builder();
test_intern_table();
test_bucket_arrays();
emit_line_directives = true;
String program_name = "examples/types_as_first_class_values.kl"_s;
@@ -231,10 +225,11 @@ int main(int argument_count, char **arguments){
Scratch scratch;
Array<OS_File_Info> examples = os_list_dir(scratch, "examples"_s);
For(examples){
if(it.is_directory) continue;
compile_file(it.absolute_path, COMPILE_AND_RUN);
}
compile_file("examples/arrays_and_slices.kl"_s, COMPILE_AND_RUN);
// For(examples){
// if(it.is_directory) continue;
// compile_file(it.absolute_path, COMPILE_AND_RUN);
// }
__debugbreak();
}

View File

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

View File

@@ -587,10 +587,10 @@ resolve_stmt(Ast *ast, Ast_Type *ret){
Operand op;
if(is_tuple(ret)){
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{
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);
}
@@ -606,7 +606,7 @@ resolve_stmt(Ast *ast, Ast_Type *ret){
Ast_Type *sub_type = 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();
@@ -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));
node->index_original_type = left.type;
node->resolved_type = left.type->arr.base;
// @todo: type_architecture?
// 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));
}
return operand_lvalue(left.type->arr.base);
return operand_lvalue(node->resolved_type);
BREAK();
}
@@ -1214,13 +1215,10 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){
CASE(LENGTH_OF, Builtin){
Operand name = resolve_expr(node->expr, inherit_flag(flags, AST_CANT_BE_NULL));
node->resolved_type = type_s64;
if(name.type == type_type){
if(is_array(name.type_val)){
Value value = value_int(name.type_val->arr.size);
rewrite_into_const(node, Ast_Builtin, value);
return operand_const_rvalue(value);
}
else compiler_error(node->pos, "Can't get length of type %Q", typestring(name.type_val));
if(is_array(name.type)){
Value value = value_int(name.type->arr.size);
rewrite_into_const(node, Ast_Builtin, value);
return operand_const_rvalue(value);
}
else if(name.type->kind == TYPE_UNTYPED_STRING){
Value value = value_int(name.intern_val.len);