Rewritten field access, lose functionality but I'm fine with it, it's simpler now,

might need to support casting
This commit is contained in:
Krzosa Karol
2022-06-11 23:19:41 +02:00
parent ac371b7203
commit 0fcdd02c5b
5 changed files with 42 additions and 116 deletions

View File

@@ -184,7 +184,8 @@ gen_expr(Ast_Expr *ast){
CASE(BINARY, Binary){ CASE(BINARY, Binary){
if(node->op == TK_Dot){ if(node->op == TK_Dot){
gen_expr(node->left); gen_expr(node->left);
if(node->type->kind == TYPE_POINTER) gen("->"); if(!node->type) gen("__");
else if(node->type->kind == TYPE_POINTER) gen("->");
else gen("."); else gen(".");
gen_expr(node->right); gen_expr(node->right);
return; return;
@@ -426,7 +427,11 @@ compile_files(Array<String> filename){
// by default it's name of the file // by default it's name of the file
// but if you add [package name] then it's overwritten // but if you add [package name] then it's overwritten
Token *token = token_get(); Token *token = token_get();
String filename_without_ext = string_chop_last_period(token->file.s); String filename_without_ext = {};
if(string_find(token->file.s, "."_s, MatchFlag_None, &filename_without_ext.len)){
filename_without_ext.str = token->file.str;
} else filename_without_ext = token->file.s;
it.name = pctx->intern(filename_without_ext); it.name = pctx->intern(filename_without_ext);
if(token_is(SAME_SCOPE) && token_is_keyword(keyword_package, 1)){ if(token_is(SAME_SCOPE) && token_is_keyword(keyword_package, 1)){

View File

@@ -1,63 +0,0 @@
//-----------------------------------------------------------------------------
// Function types
//-----------------------------------------------------------------------------
test_function :: (thing: S64): *S64
function_type: test_function
const_function_alias :: test_function
// null_function: (t: S64): *S64 = null
//-----------------------------------------------------------------------------
// Booleans
//-----------------------------------------------------------------------------
Boolean: Bool = true
//-----------------------------------------------------------------------------
// Nulls
//-----------------------------------------------------------------------------
// int_null: S64 = null
// str_null: String = null
// Bool_null: Bool = null
//-----------------------------------------------------------------------------
// Compound expressions
//-----------------------------------------------------------------------------
array1: [4]S64 = {1,2,3,4}
imp_array := [5]S64{1,2}
// imp_array_a := [5]S64{1,2,3,4,5,6}
// imp_array_b: [5]S64 = {1,2,3,4,5,6}
imp_array_c: [5]S64 = {[0] = 1, [2] = 2, [1] = 0} // @todo this should be illegal
//-----------------------------------------------------------------------------
// Pointers
//-----------------------------------------------------------------------------
pointer_decl : *S64
variable_from_deref: S64 = *pointer_decl
pointer_from_var : *S64 = &variable_from_deref
Boolean_pointer := &Boolean
//-----------------------------------------------------------------------------
// Implicit type
//-----------------------------------------------------------------------------
implicit_int :: 10
implicit_str :: "Hello world"
//-----------------------------------------------------------------------------
// Pointers
//-----------------------------------------------------------------------------
// pointer1: *S64 = 0
// pointer2: *S64 = pointer1
// pointer3: **S64 = 0
//-----------------------------------------------------------------------------
// String types
//-----------------------------------------------------------------------------
string1 :: "Test"
string2 :: string1
//-----------------------------------------------------------------------------
// Constant S64 variables
//-----------------------------------------------------------------------------
thing0 :: 10
thing1 :: thing0 + 11
thing2 :: thing1 + 20
combin :: thing0 + thing1 + thing2

View File

@@ -207,8 +207,8 @@ int main(int argument_count, char **arguments){
// files.add("order1.kl"_s); // files.add("order1.kl"_s);
// files.add("order2.kl"_s); // files.add("order2.kl"_s);
// files.add("new_types.kl"_s); // files.add("new_types.kl"_s);
// files.add("enums.kl"_s); files.add("enums.kl"_s);
files.add("globals.kl"_s); // files.add("G.globals.kl"_s);
// files.add("euler.kl"_s); // files.add("euler.kl"_s);
String result = compile_files(files); String result = compile_files(files);
printf("%s", result.str); printf("%s", result.str);

View File

@@ -261,14 +261,14 @@ try_converting_untyped_to_typed(Operand *op){
} }
} }
enum{ FLAG32(Typecheck_Flag){
TYPE_AND_EXPR_REQUIRED = 0, TYPE_AND_EXPR_REQUIRED = 0,
TYPE_CAN_BE_NULL = 1, TYPE_CAN_BE_NULL = 1,
EXPR_CAN_BE_NULL = 2 EXPR_CAN_BE_NULL = 2
}; };
function void function void
make_sure_value_is_compatible_with_type(Token *pos, Operand *expr, Ast_Type *type, U64 debug_flag){ make_sure_value_is_compatible_with_type(Token *pos, Operand *expr, Ast_Type *type, Typecheck_Flag debug_flag){
if(type == expr->type){ if(type == expr->type){
assert(type); assert(type);
assert(expr->type); assert(expr->type);
@@ -617,8 +617,6 @@ resolve_compound_struct(Ast_Call *node, Ast_Type *type){
} }
global Ast_Scope *field_access_scope; // @memes
function Operand function Operand
resolve_field_access(Ast_Expr *node, Ast_Scope *context){ resolve_field_access(Ast_Expr *node, Ast_Scope *context){
Ast_Binary *current = 0; Ast_Binary *current = 0;
@@ -629,37 +627,35 @@ resolve_field_access(Ast_Expr *node, Ast_Scope *context){
node = current->left; node = current->left;
} }
Ast_Scope *new_context = 0; if(node->kind != AST_IDENT)
if(!context && node->kind == AST_IDENT){ compiler_error(node->pos, "Required to be identifier in field access");
auto ident = (Ast_Atom *)node;
Ast_Package *package = search_for_package(ident->intern_val); Ast_Atom *ident = (Ast_Atom *)node;
if(package) { Ast_Scope *scope = context ? context : node->parent_scope;
new_context = package; Search_Flag flag = context ? SEARCH_ONLY_CURRENT_SCOPE : SEARCH_ALSO_FOR_PACKAGE;
} Ast_Decl *decl = resolve_name(scope, node->pos, ident->intern_val, flag);
if(decl->kind == AST_PACKAGE){
assert(next);
return resolve_field_access(next, (Ast_Scope *)decl);
} }
if(!new_context){ Ast_Type *type = decl->type;
field_access_scope = context; if(type == type_type && is_enum(decl->type_val)) type = decl->type_val;
Operand op = resolve_expr(node, AST_CANT_BE_NULL); if(current) current->type = type;
if(is_pointer(type)) type = type->base;
Ast_Type *type = op.type; type_complete(type);
if(op.type == type_type && is_enum(op.type_val)) type = op.type_val; if(next && !is_struct(type) && !is_enum(type)){
if(current) current->type = type; compiler_error(node->pos, "Dot access");
if(is_pointer(type)) type = type->base; }
type_complete(type);
if(next && !is_struct(type) && !is_enum(type)){ if(!next){
compiler_error(node->pos, "Dot access"); Operand op = operand(decl);
} return op;
if(!next)
return op;
new_context = ((Ast_Decl *)type->ast)->scope;
} }
assert(next); assert(next);
return resolve_field_access(next, new_context); return resolve_field_access(next, ((Ast_Decl *)type->ast)->scope);
} }
function Operand function Operand
@@ -671,24 +667,12 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){
switch(ast->kind){ switch(ast->kind){
CASE(IDENT, Atom){ CASE(IDENT, Atom){
Ast_Scope *scope = node->parent_scope; node->resolved_decl = resolve_name(node->parent_scope, node->pos, node->intern_val);
Search_Flag flags = 0;
if(field_access_scope){
set_flag(flags, SEARCH_ONLY_CURRENT_SCOPE);
scope = field_access_scope;
}
Ast_Decl *decl = resolve_name(scope, node->pos, node->intern_val, flags);
field_access_scope = 0; // @memes
node->resolved_decl = decl; Operand result = operand(node->resolved_decl);
Operand result = operand(decl); if(node->resolved_decl->kind == AST_CONST){
if(decl->kind != AST_VAR){ rewrite_into_const(node, Ast_Atom, node->resolved_decl->value);
if(decl->kind == AST_CONST){
rewrite_into_const(node, Ast_Atom, decl->value);
}
result.is_const = 1;
} }
return result; return result;
BREAK(); BREAK();
} }

View File

@@ -24,12 +24,12 @@ function Ast_Decl *resolve_name(Ast_Scope *parent_scope, Token *pos, Intern_Stri
// Operands // Operands
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
function Operand function Operand
operand(Ast_Decl *sym){ operand(Ast_Decl *decl){
Operand result = {}; Operand result = {};
result.type = sym->type; result.type = decl->type;
result.is_const = sym->kind == AST_CONST ? true : false; result.is_const = decl->kind != AST_VAR ? true : false;
result.is_lvalue= sym->kind == AST_CONST ? false : true; // Cant assign to const values result.is_lvalue= decl->kind == AST_CONST ? false : true; // Cant assign to const values
result.value = sym->value; result.value = decl->value;
return result; return result;
} }