Broke builtins.

This commit is contained in:
2025-09-06 19:58:46 +02:00
parent 11c936ba7f
commit 9cf51a1534
6 changed files with 238 additions and 113 deletions

View File

@@ -40,7 +40,7 @@ Type_Kind :: enum {
Array;
}
Type_Variable_Kind :: enum {
Source_Kind :: enum {
Expression;
Declaration; // struct, properties, function, etc.
}
@@ -60,7 +60,7 @@ Typenames :: string.[
// We can just have the built-in types and then we can declare structs, functions, buffers etc. as one time things.
Type_Variable :: struct {
type : Type_Kind;
kind : Type_Variable_Kind;
source_kind : Source_Kind;
builtin : bool;
name : string;
@@ -704,6 +704,92 @@ new_type_variable :: (checker : *Semantic_Checker) -> *Type_Variable, Type_Varia
return from_handle(checker, handle), handle;
}
new_builtin_type_variable :: (checker : *Semantic_Checker, type : Type_Kind, source : Source_Kind, name : string, typename : string = "") -> *Type_Variable, Type_Variable_Handle {
tv, handle := new_type_variable(checker);
tv.name = name;
tv.type = type;
tv.source_kind = source;
tv.builtin = true;
tv.typename = typename;
return tv, handle;
}
Arg :: struct {
name : string;
typename : string;
}
new_builtin_struct :: (checker : *Semantic_Checker, name : string, members : []Arg) -> *Type_Variable, Type_Variable_Handle {
tv, handle := new_builtin_type_variable(checker, .Struct, .Declaration, name, name);
// @Incomplete: Skip for now. This is solely for error reporting?
// At least let's not make a big deal out of it for now.
// We could report builtin nodes in a special way instead of with an actual source location.
// builtin_node := new_builtin_struct_node(checker.result.nodes, members, checker.result.allocator;
symbol : Defined_Symbol;
symbol.name = name;
// symbol.source_node = builtin_node;
symbol.builtin = true;
symbol.type_variable = handle;
add_symbol_to_scope(checker.state, *checker.result.scope_stack, checker.current_scope, name, symbol);
scope, scope_handle := push_scope(checker, name, .Struct);
tv.scope = scope_handle;
for member : members {
typename : string;
kind := lookup_type(checker, checker.current_scope, member.typename, *typename);
member_var, member_handle := new_builtin_type_variable(checker, kind, .Expression, member.name);
member_var.scope = tv.scope;
member_symbol : Defined_Symbol;
member_symbol.name = member.name;
member_symbol.type_variable = member_handle;
add_symbol_to_scope(checker.state, *checker.result.scope_stack, checker.current_scope, member.name, symbol);
add_child(checker, handle, member_handle);
}
pop_scope(checker);
return from_handle(checker, handle), handle;
}
// new_builtin_function :: (checker : *Semantic_Checker, name : string, args : []Arg) -> *Type_Variable, Type_Variable_Handle {
// tv, handle := new_builtin_type_variable(checker, .Function, .Declaration, name);
// // @Incomplete: Skip for now. This is solely for error reporting?
// // At least let's not make a big deal out of it for now.
// // We could report builtin nodes in a special way instead of with an actual source location.
// // builtin_node := new_builtin_struct_node(checker.result.nodes, members, checker.result.allocator;
// symbol : Defined_Symbol;
// symbol.name = name;
// // symbol.source_node = builtin_node;
// symbol.builtin = true;
// symbol.type_variable = handle;
// add_symbol_to_scope(checker.state, *checker.result.scope_stack, checker.current_scope, name, symbol);
// scope, scope_handle := push_scope(checker, name, .Struct);
// tv.scope = scope_handle;
// for arg : args {
// typename : string;
// kind := get_type_from_identifier(checker, checker.current_scope, arg, *typename);
// arg_var, arg_handle := new_builtin_type_variable(checker, kind, .Expression, typename);
// add_child(checker, handle, arg_handle);
// }
// pop_scope(checker);
// return from_handle(checker, handle), handle;
// }
add_child :: (variable : *Type_Variable, child : Type_Variable_Handle) {
assert(variable.children.count < Type_Variable.MAX_TYPE_VARIABLE_CHILDREN);
array_add(*variable.children, child);
@@ -846,9 +932,7 @@ proper_type_to_string :: (variables : []Type_Variable, var : Type_Variable, allo
return "______not proper type______";
}
get_type_from_identifier :: (checker : *Semantic_Checker, scope : Scope_Handle, node : *AST_Node, typename : *string = null) -> Type_Kind {
type_string := node.token.ident_value;
lookup_type :: (checker : *Semantic_Checker, scope : Scope_Handle, type_string : string, typename : *string = null) -> Type_Kind {
if type_string == {
case Typenames[Type_Kind.Int]; return .Int;
case Typenames[Type_Kind.Half]; return .Half;
@@ -874,6 +958,11 @@ get_type_from_identifier :: (checker : *Semantic_Checker, scope : Scope_Handle,
return .Invalid;
}
lookup_type :: (checker : *Semantic_Checker, scope : Scope_Handle, node : *AST_Node, typename : *string = null) -> Type_Kind {
type_string := node.token.ident_value;
return lookup_type(checker, scope, type_string, typename);
}
check_block :: (checker : *Semantic_Checker, node : *AST_Node) {
for child : node.children {
check_node(checker, child);
@@ -883,7 +972,7 @@ check_block :: (checker : *Semantic_Checker, node : *AST_Node) {
declare_struct :: (checker : *Semantic_Checker, node : *AST_Node, name : string) -> Type_Variable_Handle {
variable, handle := new_type_variable(checker);
variable.type = .Struct;
variable.kind = .Declaration;
variable.source_kind = .Declaration;
variable.name = name;
variable.source_node = node;
variable.typename = name;
@@ -975,7 +1064,7 @@ declare_function :: (checker : *Semantic_Checker, node : *AST_Node, builtin : bo
variable, handle := new_type_variable(checker);
variable.type = .Function;
variable.kind = .Declaration;
variable.source_kind = .Declaration;
variable.name = node.name;
variable.source_node = node;
variable.builtin = builtin;
@@ -1022,7 +1111,7 @@ declare_function :: (checker : *Semantic_Checker, node : *AST_Node, builtin : bo
node_child := field_list.children[i];
typename : string;
arg_type := get_type_from_identifier(checker, checker.current_scope, node_child, *typename);
arg_type := lookup_type(checker, checker.current_scope, node_child, *typename);
other_arg := from_handle(checker, arg);
if arg_type != other_arg.type {
@@ -1082,7 +1171,7 @@ declare_function :: (checker : *Semantic_Checker, node : *AST_Node, builtin : bo
if builtin && node.token.ident_value.count > 0 {
return_var, return_handle := new_type_variable(checker);
return_var.type = get_type_from_identifier(checker, checker.current_scope, node, *return_var.typename);
return_var.type = lookup_type(checker, checker.current_scope, node, *return_var.typename);
from_handle(checker, handle).return_type_variable= return_handle;
}
@@ -1202,7 +1291,7 @@ check_field :: (checker : *Semantic_Checker, node : *AST_Node) -> Type_Variable_
variable, handle := new_type_variable(checker);
variable.name = node.name;
typename : string;
variable.type = get_type_from_identifier(checker, checker.current_scope, node, *typename);
variable.type = lookup_type(checker, checker.current_scope, node, *typename);
variable.is_array = node.array_field;
@@ -1214,12 +1303,12 @@ check_field :: (checker : *Semantic_Checker, node : *AST_Node) -> Type_Variable_
}
}
if variable.kind == .Declaration && variable.type == .Sampler {
if variable.source_kind == .Declaration && variable.type == .Sampler {
variable.resource_index = checker.current_sampler_index;
checker.current_sampler_index += 1;
}
if variable.kind == .Declaration && variable.type == .Texture2D {
if variable.source_kind == .Declaration && variable.type == .Texture2D {
variable.resource_index = checker.current_texture_index;
checker.current_texture_index += 1;
}
@@ -1250,7 +1339,7 @@ check_field :: (checker : *Semantic_Checker, node : *AST_Node) -> Type_Variable_
}
if node.token.ident_value.count > 0 {
variable.type = get_type_from_identifier(checker, checker.current_scope, node);
variable.type = lookup_type(checker, checker.current_scope, node);
}
if node.children.count > 0 {
@@ -1687,17 +1776,16 @@ types_compatible :: (checker : *Semantic_Checker, lhs : Type_Variable_Handle, rh
return false;
}
// add_type :: (checker : *Semantic_Checker, kind : Type_Kind, members : ..Type_Kind) {
add_builtins_new :: (checker : *Semantic_Checker) {
// }
checker.state = .Adding_Builtins;
float_name := Typenames[Type_Kind.Float];
// new_builtin_struct(checker, "float2", .[.{"x", float_name}, .{"y", float_name}]);
// new_builtin_struct(checker, "float3", .[.{"x", float_name}, .{"y", float_name}, .{"z", float_name}]);
// new_builtin_struct(checker, "float4", .[.{"x", float_name}, .{"y", float_name}, .{"z", float_name}, .{"w", float_name}]);
// add_builtins :: (checler : *Semantic_Checker) {
// //~ Types
// add_type(checker, "float2", .Struct, .Float, .Float);
// add_type(checker, "float3", .Struct, .Float, .Float, .Float);
// //~ Functions
// }
checker.state = .Type_Checking;
}
add_builtins :: (checker : *Semantic_Checker) {
source_location := #location().fully_pathed_filename;
@@ -1733,27 +1821,6 @@ add_builtins :: (checker : *Semantic_Checker) {
lex(checker.result);
parse(checker.result);
type_check(checker, checker.result.root);
// lex_result := lex(*lexer,, *temp);
// if lex_result.had_error {
// print("%\n", report_messages(lex_result.messages));
// return;
// }
// parse_state : Parse_State;
// init_parse_state(*parse_state, lex_result.tokens, lexer.path);
// parse_result := parse(*parse_state);
// if parse_result.had_error {
// print("%\n", report_messages(parse_result.messages));
// return;
// }
// type_check(checker, parse_result.root);
// if checker.had_error {
// print("%\n", report_messages(checker.messages));
// return;
// }
for *type_var : checker.result.type_variables {
type_var.builtin = true;
@@ -1777,18 +1844,18 @@ check :: (result : *Compile_Result) {
checker.current_sampler_index = 0;
checker.current_texture_index = 0;
checker.result = result;
file := result.file;
root := result.root;
array_reserve(*checker.messages, 32);
result.root = null;
add_builtins_new(*checker);
add_builtins(*checker);
init_semantic_checker(*checker, result.root, result.file.path);
// Actually, these are not built-ins, they should be a core lib you import.
// So if we add imports, we can just add this file.
// Maybe we should just do #load
add_builtins(*checker);
checker.result.file = file;
result.root = root;
type_check(*checker, result.root);
@@ -1927,7 +1994,7 @@ pretty_print_scope :: (current_scope : Scope_Handle, scope_stack : Scope_Stack,
case .CBuffer; #through;
case .Properties; #through;
case .Struct; {
if type_variable.typename.count > 0 && type_variable.kind != .Declaration {
if type_variable.typename.count > 0 && type_variable.source_kind != .Declaration {
indent(builder, indentation + 1);
print_key(*scope_stack, current_scope, builder, key);
print_type_variable(builder, variables, type_variable);
@@ -1953,7 +2020,7 @@ pretty_print_scope :: (current_scope : Scope_Handle, scope_stack : Scope_Stack,
case .CBuffer; #through;
case .Properties; #through;
case .Struct; {
if type_variable.typename.count > 0 && type_variable.kind != .Declaration {
if type_variable.typename.count > 0 && type_variable.source_kind != .Declaration {
indent(builder, indentation + 1);
print_key(*scope_stack, current_scope, builder, key);
print_to_builder(builder, "%\n", type_variable.typename);