Added inferred types and missing length function.

This commit is contained in:
2025-01-10 22:44:15 +01:00
parent 8bd766281e
commit ec31046d30
16 changed files with 218 additions and 13 deletions

View File

@@ -264,7 +264,7 @@ error_node :: (parse_state : *Parse_State, message : string) -> *AST_Node {
advance_to_sync_point :: (parse_state : *Parse_State) { advance_to_sync_point :: (parse_state : *Parse_State) {
while true { while true {
if parse_state.current.kind == .TOKEN_SEMICOLON || parse_state.current.kind == .TOKEN_RIGHTBRACE || if parse_state.current.kind == .TOKEN_SEMICOLON || parse_state.current.kind == .TOKEN_RIGHTBRACE ||
parse_state.current.kind == .TOKEN_LEFTBRACE{ parse_state.current.kind == .TOKEN_LEFTBRACE {
break; break;
} }
advance(parse_state); advance(parse_state);
@@ -644,8 +644,12 @@ field_declaration :: (parse_state : *Parse_State, identifier_token : *Token) ->
advance(parse_state); advance(parse_state);
node.array_field = true; node.array_field = true;
} else { } else {
missing_type_specifier(parse_state, identifier_token, "Expected type specifier after field name."); if !check(parse_state, .TOKEN_ASSIGN) {
return node; internal_error_message(*parse_state.result.messages, "Unimplemented error message.", parse_state.path);
return node;
}
// missing_type_specifier(parse_state, identifier_token, "Expected type specifier after field name.");
} }
if check(parse_state, .TOKEN_AT) { if check(parse_state, .TOKEN_AT) {
@@ -1088,6 +1092,8 @@ parse :: (result : *Compile_Result) {
file.ast_root = parse_state.result.root; file.ast_root = parse_state.result.root;
file.ast_nodes = parse_state.result.nodes; file.ast_nodes = parse_state.result.nodes;
copy_messages(parse_state.result.messages, *result.messages); copy_messages(parse_state.result.messages, *result.messages);
result.had_error |= parse_state.result.had_error;
} }
} }

View File

@@ -1241,9 +1241,14 @@ create_field :: (checker : *Semantic_Checker, node : *AST_Node) -> Type_Variable
r := from_handle(checker, rhs); r := from_handle(checker, rhs);
assert(l.type != .Unresolved_Expression && r.type != .Unresolved_Expression); assert(l.type != .Unresolved_Expression && r.type != .Unresolved_Expression);
if !types_compatible(checker, handle, rhs) { if l.type == .Unresolved_Variable {
type_mismatch(checker, l.source_node, r.source_node, rhs, handle); l.type = r.type;
return 0; l.typename = r.typename;
} else {
if !types_compatible(checker, handle, rhs) {
type_mismatch(checker, l.source_node, r.source_node, rhs, handle);
return 0;
}
} }
} }
@@ -1419,7 +1424,6 @@ check_node :: (checker : *Semantic_Checker, node : *AST_Node) -> Type_Variable_H
} }
} }
case .TOKEN_ASSIGN; { case .TOKEN_ASSIGN; {
if !types_compatible(checker, lhs_var, rhs_var, true) { if !types_compatible(checker, lhs_var, rhs_var, true) {
type_mismatch(checker, node.parent, node.children[1], lhs_var, rhs_var); type_mismatch(checker, node.parent, node.children[1], lhs_var, rhs_var);
return 0; return 0;
@@ -1614,7 +1618,7 @@ add_hlsl_builtins :: (checker : *Semantic_Checker) {
append(*sb, "/"); append(*sb, "/");
} }
append(*sb, "hlsl_builtin.shd"); append(*sb, "hlsl_builtin.ink");
path := builder_to_string(*sb); path := builder_to_string(*sb);
@@ -1686,11 +1690,13 @@ check :: (result : *Compile_Result) {
init_semantic_checker(*checker, file.ast_root, file.file.path); init_semantic_checker(*checker, file.ast_root, file.file.path);
// @Performance: Should have this built in stuff done earlier and only once
add_hlsl_builtins(*checker); add_hlsl_builtins(*checker);
type_check(*checker, file.ast_root); type_check(*checker, file.ast_root);
file.semantic_check_result = checker.result; file.semantic_check_result = checker.result;
result.had_error |= checker.result.had_error;
copy_messages(checker.result.messages, *result.messages); copy_messages(checker.result.messages, *result.messages);
} }
} }

View File

@@ -2,6 +2,7 @@
//~ nbr: General improvements //~ nbr: General improvements
// //
// [x] Print out all failed tests in a list at the end // [x] Print out all failed tests in a list at the end
// [ ] Use new compiler API with Compile_Result and Compiled_File instead
// [ ] Use unix (posix? bash? ascii?) color codes for errors // [ ] Use unix (posix? bash? ascii?) color codes for errors
// [ ] Print golden file as green and new output as red // [ ] Print golden file as green and new output as red

View File

@@ -83,6 +83,10 @@ int4x4 :: struct {
#foreign distance :: (float3, float3) -> float; #foreign distance :: (float3, float3) -> float;
#foreign distance :: (float4, float4) -> float; #foreign distance :: (float4, float4) -> float;
#foreign length :: (float2) -> float;
#foreign length :: (float3) -> float;
#foreign length :: (float4) -> float;
#foreign dot :: (float2, float2) -> float; #foreign dot :: (float2, float2) -> float;
#foreign dot :: (float3, float3) -> float; #foreign dot :: (float3, float3) -> float;
#foreign dot :: (float4, float4) -> float; #foreign dot :: (float4, float4) -> float;

View File

@@ -401,6 +401,10 @@ compile_file :: (compiler : *Shader_Compiler, paths : []string) -> Compile_Resul
check(*result); check(*result);
codegen(*result); codegen(*result);
if result.had_error {
return result;
}
for *file : result.files { for *file : result.files {
check_result := file.semantic_check_result; check_result := file.semantic_check_result;
if check_result.vertex_entry_point { if check_result.vertex_entry_point {

View File

@@ -0,0 +1,24 @@
float bar();
float foo();
float bar()
{
return 5.0f;
}
float foo()
{
return bar();
}
float4 vs_main(float3 pos : POSITION) : SV_POSITION
{
float f = 2.0f;
int i = 10;
f = foo();
float2 v2 = float2(2, 2);
float3 v3 = float3(2, 2, 3);
float4 v4 = float4(4, 5, 6, 7);
return float4(1, 1, 1, 1);
}

View File

@@ -9,6 +9,7 @@ test/field_assignment.ink codegen
test/function_call.ink codegen test/function_call.ink codegen
test/function_call_out_of_order_declaration.ink codegen test/function_call_out_of_order_declaration.ink codegen
test/function_call_return.ink codegen test/function_call_return.ink codegen
test/inferred_types.ink codegen
test/meta_block.ink codegen test/meta_block.ink codegen
test/multiple_functions.ink codegen test/multiple_functions.ink codegen
test/multiple_semicolons_everywhere.ink codegen test/multiple_semicolons_everywhere.ink codegen

View File

@@ -10,6 +10,7 @@ test/function_call.ink compile
test/function_call_out_of_order_declaration.ink compile test/function_call_out_of_order_declaration.ink compile
test/function_call_return.ink compile test/function_call_return.ink compile
test/functions_with_same_name.ink compile test/functions_with_same_name.ink compile
test/inferred_types.ink compile
test/meta_block.ink compile test/meta_block.ink compile
test/multiple_functions.ink compile test/multiple_functions.ink compile
test/multiple_semicolons_everywhere.ink compile test/multiple_semicolons_everywhere.ink compile

17
test/inferred_types.ink Normal file
View File

@@ -0,0 +1,17 @@
bar :: () -> float {
return 5.0;
}
foo :: () -> float {
return bar();
}
vertex main :: (pos : float3 @position) -> float4 @position {
f := 2.0;
i := 10;
f = foo();
v2 := float2(2, 2);
v3 := float3(2, 2, 3);
v4 := float4(4, 5, 6, 7);
return float4(1, 1, 1, 1);
}

View File

@@ -0,0 +1,105 @@
{kind = TOKEN_IDENTIFIER; ; index = 0 ; length = 3 line = 1 ; column = 0 ; value ='bar'; }
{kind = TOKEN_DOUBLECOLON; ; index = 4 ; length = 2 line = 1 ; column = 4 ; value ='::'; }
{kind = TOKEN_LEFTPAREN; ; index = 7 ; length = 1 line = 1 ; column = 7 ; value ='('; }
{kind = TOKEN_RIGHTPAREN; ; index = 8 ; length = 1 line = 1 ; column = 8 ; value =')'; }
{kind = TOKEN_ARROW; ; index = 10 ; length = 2 line = 1 ; column = 10 ; value ='->'; }
{kind = TOKEN_IDENTIFIER; ; index = 13 ; length = 5 line = 1 ; column = 13 ; value ='float'; }
{kind = TOKEN_LEFTBRACE; ; index = 19 ; length = 1 line = 1 ; column = 19 ; value ='{'; }
{kind = TOKEN_RETURN; ; index = 23 ; length = 6 line = 2 ; column = 0 ; value ='return'; }
{kind = TOKEN_FLOATLITERAL; ; index = 30 ; length = 3 line = 2 ; column = 7 ; value ='5'; }
{kind = TOKEN_SEMICOLON; ; index = 33 ; length = 1 line = 2 ; column = 10 ; value =';'; }
{kind = TOKEN_RIGHTBRACE; ; index = 36 ; length = 1 line = 3 ; column = 0 ; value ='}'; }
{kind = TOKEN_IDENTIFIER; ; index = 41 ; length = 3 line = 5 ; column = 0 ; value ='foo'; }
{kind = TOKEN_DOUBLECOLON; ; index = 45 ; length = 2 line = 5 ; column = 4 ; value ='::'; }
{kind = TOKEN_LEFTPAREN; ; index = 48 ; length = 1 line = 5 ; column = 7 ; value ='('; }
{kind = TOKEN_RIGHTPAREN; ; index = 49 ; length = 1 line = 5 ; column = 8 ; value =')'; }
{kind = TOKEN_ARROW; ; index = 51 ; length = 2 line = 5 ; column = 10 ; value ='->'; }
{kind = TOKEN_IDENTIFIER; ; index = 54 ; length = 5 line = 5 ; column = 13 ; value ='float'; }
{kind = TOKEN_LEFTBRACE; ; index = 60 ; length = 1 line = 5 ; column = 19 ; value ='{'; }
{kind = TOKEN_RETURN; ; index = 64 ; length = 6 line = 6 ; column = 0 ; value ='return'; }
{kind = TOKEN_IDENTIFIER; ; index = 71 ; length = 3 line = 6 ; column = 7 ; value ='bar'; }
{kind = TOKEN_LEFTPAREN; ; index = 74 ; length = 1 line = 6 ; column = 10 ; value ='('; }
{kind = TOKEN_RIGHTPAREN; ; index = 75 ; length = 1 line = 6 ; column = 11 ; value =')'; }
{kind = TOKEN_SEMICOLON; ; index = 76 ; length = 1 line = 6 ; column = 12 ; value =';'; }
{kind = TOKEN_RIGHTBRACE; ; index = 79 ; length = 1 line = 7 ; column = 0 ; value ='}'; }
{kind = TOKEN_VERTEX; ; index = 84 ; length = 6 line = 9 ; column = 0 ; value ='vertex'; }
{kind = TOKEN_IDENTIFIER; ; index = 91 ; length = 4 line = 9 ; column = 7 ; value ='main'; }
{kind = TOKEN_DOUBLECOLON; ; index = 96 ; length = 2 line = 9 ; column = 12 ; value ='::'; }
{kind = TOKEN_LEFTPAREN; ; index = 99 ; length = 1 line = 9 ; column = 15 ; value ='('; }
{kind = TOKEN_IDENTIFIER; ; index = 100 ; length = 3 line = 9 ; column = 16 ; value ='pos'; }
{kind = TOKEN_COLON; ; index = 104 ; length = 1 line = 9 ; column = 20 ; value =':'; }
{kind = TOKEN_IDENTIFIER; ; index = 106 ; length = 6 line = 9 ; column = 22 ; value ='float3'; }
{kind = TOKEN_AT; ; index = 113 ; length = 1 line = 9 ; column = 29 ; value ='@'; }
{kind = TOKEN_IDENTIFIER; ; index = 114 ; length = 8 line = 9 ; column = 30 ; value ='position'; }
{kind = TOKEN_RIGHTPAREN; ; index = 122 ; length = 1 line = 9 ; column = 38 ; value =')'; }
{kind = TOKEN_ARROW; ; index = 124 ; length = 2 line = 9 ; column = 40 ; value ='->'; }
{kind = TOKEN_IDENTIFIER; ; index = 127 ; length = 6 line = 9 ; column = 43 ; value ='float4'; }
{kind = TOKEN_AT; ; index = 134 ; length = 1 line = 9 ; column = 50 ; value ='@'; }
{kind = TOKEN_IDENTIFIER; ; index = 135 ; length = 8 line = 9 ; column = 51 ; value ='position'; }
{kind = TOKEN_LEFTBRACE; ; index = 144 ; length = 1 line = 9 ; column = 60 ; value ='{'; }
{kind = TOKEN_IDENTIFIER; ; index = 151 ; length = 1 line = 10 ; column = 3 ; value ='f'; }
{kind = TOKEN_COLON; ; index = 153 ; length = 1 line = 10 ; column = 5 ; value =':'; }
{kind = TOKEN_ASSIGN; ; index = 154 ; length = 1 line = 10 ; column = 6 ; value ='='; }
{kind = TOKEN_FLOATLITERAL; ; index = 156 ; length = 3 line = 10 ; column = 8 ; value ='2'; }
{kind = TOKEN_SEMICOLON; ; index = 159 ; length = 1 line = 10 ; column = 11 ; value =';'; }
{kind = TOKEN_IDENTIFIER; ; index = 166 ; length = 1 line = 11 ; column = 3 ; value ='i'; }
{kind = TOKEN_COLON; ; index = 168 ; length = 1 line = 11 ; column = 5 ; value =':'; }
{kind = TOKEN_ASSIGN; ; index = 169 ; length = 1 line = 11 ; column = 6 ; value ='='; }
{kind = TOKEN_INTLITERAL; ; index = 171 ; length = 2 line = 11 ; column = 8 ; value ='10'; }
{kind = TOKEN_SEMICOLON; ; index = 173 ; length = 1 line = 11 ; column = 10 ; value =';'; }
{kind = TOKEN_IDENTIFIER; ; index = 180 ; length = 1 line = 12 ; column = 3 ; value ='f'; }
{kind = TOKEN_ASSIGN; ; index = 182 ; length = 1 line = 12 ; column = 5 ; value ='='; }
{kind = TOKEN_IDENTIFIER; ; index = 184 ; length = 3 line = 12 ; column = 7 ; value ='foo'; }
{kind = TOKEN_LEFTPAREN; ; index = 187 ; length = 1 line = 12 ; column = 10 ; value ='('; }
{kind = TOKEN_RIGHTPAREN; ; index = 188 ; length = 1 line = 12 ; column = 11 ; value =')'; }
{kind = TOKEN_SEMICOLON; ; index = 189 ; length = 1 line = 12 ; column = 12 ; value =';'; }
{kind = TOKEN_IDENTIFIER; ; index = 196 ; length = 2 line = 13 ; column = 3 ; value ='v2'; }
{kind = TOKEN_COLON; ; index = 199 ; length = 1 line = 13 ; column = 6 ; value =':'; }
{kind = TOKEN_ASSIGN; ; index = 200 ; length = 1 line = 13 ; column = 7 ; value ='='; }
{kind = TOKEN_IDENTIFIER; ; index = 202 ; length = 6 line = 13 ; column = 9 ; value ='float2'; }
{kind = TOKEN_LEFTPAREN; ; index = 208 ; length = 1 line = 13 ; column = 15 ; value ='('; }
{kind = TOKEN_INTLITERAL; ; index = 209 ; length = 1 line = 13 ; column = 16 ; value ='2'; }
{kind = TOKEN_COMMA; ; index = 210 ; length = 1 line = 13 ; column = 17 ; value =','; }
{kind = TOKEN_INTLITERAL; ; index = 212 ; length = 1 line = 13 ; column = 19 ; value ='2'; }
{kind = TOKEN_RIGHTPAREN; ; index = 213 ; length = 1 line = 13 ; column = 20 ; value =')'; }
{kind = TOKEN_SEMICOLON; ; index = 214 ; length = 1 line = 13 ; column = 21 ; value =';'; }
{kind = TOKEN_IDENTIFIER; ; index = 221 ; length = 2 line = 14 ; column = 3 ; value ='v3'; }
{kind = TOKEN_COLON; ; index = 224 ; length = 1 line = 14 ; column = 6 ; value =':'; }
{kind = TOKEN_ASSIGN; ; index = 225 ; length = 1 line = 14 ; column = 7 ; value ='='; }
{kind = TOKEN_IDENTIFIER; ; index = 227 ; length = 6 line = 14 ; column = 9 ; value ='float3'; }
{kind = TOKEN_LEFTPAREN; ; index = 233 ; length = 1 line = 14 ; column = 15 ; value ='('; }
{kind = TOKEN_INTLITERAL; ; index = 234 ; length = 1 line = 14 ; column = 16 ; value ='2'; }
{kind = TOKEN_COMMA; ; index = 235 ; length = 1 line = 14 ; column = 17 ; value =','; }
{kind = TOKEN_INTLITERAL; ; index = 237 ; length = 1 line = 14 ; column = 19 ; value ='2'; }
{kind = TOKEN_COMMA; ; index = 238 ; length = 1 line = 14 ; column = 20 ; value =','; }
{kind = TOKEN_INTLITERAL; ; index = 240 ; length = 1 line = 14 ; column = 22 ; value ='3'; }
{kind = TOKEN_RIGHTPAREN; ; index = 241 ; length = 1 line = 14 ; column = 23 ; value =')'; }
{kind = TOKEN_SEMICOLON; ; index = 242 ; length = 1 line = 14 ; column = 24 ; value =';'; }
{kind = TOKEN_IDENTIFIER; ; index = 249 ; length = 2 line = 15 ; column = 3 ; value ='v4'; }
{kind = TOKEN_COLON; ; index = 252 ; length = 1 line = 15 ; column = 6 ; value =':'; }
{kind = TOKEN_ASSIGN; ; index = 253 ; length = 1 line = 15 ; column = 7 ; value ='='; }
{kind = TOKEN_IDENTIFIER; ; index = 255 ; length = 6 line = 15 ; column = 9 ; value ='float4'; }
{kind = TOKEN_LEFTPAREN; ; index = 261 ; length = 1 line = 15 ; column = 15 ; value ='('; }
{kind = TOKEN_INTLITERAL; ; index = 262 ; length = 1 line = 15 ; column = 16 ; value ='4'; }
{kind = TOKEN_COMMA; ; index = 263 ; length = 1 line = 15 ; column = 17 ; value =','; }
{kind = TOKEN_INTLITERAL; ; index = 265 ; length = 1 line = 15 ; column = 19 ; value ='5'; }
{kind = TOKEN_COMMA; ; index = 266 ; length = 1 line = 15 ; column = 20 ; value =','; }
{kind = TOKEN_INTLITERAL; ; index = 268 ; length = 1 line = 15 ; column = 22 ; value ='6'; }
{kind = TOKEN_COMMA; ; index = 269 ; length = 1 line = 15 ; column = 23 ; value =','; }
{kind = TOKEN_INTLITERAL; ; index = 271 ; length = 1 line = 15 ; column = 25 ; value ='7'; }
{kind = TOKEN_RIGHTPAREN; ; index = 272 ; length = 1 line = 15 ; column = 26 ; value =')'; }
{kind = TOKEN_SEMICOLON; ; index = 273 ; length = 1 line = 15 ; column = 27 ; value =';'; }
{kind = TOKEN_RETURN; ; index = 280 ; length = 6 line = 16 ; column = 3 ; value ='return'; }
{kind = TOKEN_IDENTIFIER; ; index = 287 ; length = 6 line = 16 ; column = 10 ; value ='float4'; }
{kind = TOKEN_LEFTPAREN; ; index = 293 ; length = 1 line = 16 ; column = 16 ; value ='('; }
{kind = TOKEN_INTLITERAL; ; index = 294 ; length = 1 line = 16 ; column = 17 ; value ='1'; }
{kind = TOKEN_COMMA; ; index = 295 ; length = 1 line = 16 ; column = 18 ; value =','; }
{kind = TOKEN_INTLITERAL; ; index = 297 ; length = 1 line = 16 ; column = 20 ; value ='1'; }
{kind = TOKEN_COMMA; ; index = 298 ; length = 1 line = 16 ; column = 21 ; value =','; }
{kind = TOKEN_INTLITERAL; ; index = 300 ; length = 1 line = 16 ; column = 23 ; value ='1'; }
{kind = TOKEN_COMMA; ; index = 301 ; length = 1 line = 16 ; column = 24 ; value =','; }
{kind = TOKEN_INTLITERAL; ; index = 303 ; length = 1 line = 16 ; column = 26 ; value ='1'; }
{kind = TOKEN_RIGHTPAREN; ; index = 304 ; length = 1 line = 16 ; column = 27 ; value =')'; }
{kind = TOKEN_SEMICOLON; ; index = 305 ; length = 1 line = 16 ; column = 28 ; value =';'; }
{kind = TOKEN_RIGHTBRACE; ; index = 308 ; length = 1 line = 17 ; column = 0 ; value ='}'; }
{kind = TOKEN_EOF; ; index = 309 ; length = 0 line = 17 ; column = 1 ; value =''; }

View File

@@ -13,6 +13,7 @@ test/function_call_out_of_order_declaration.ink lex
test/function_call_return.ink lex test/function_call_return.ink lex
test/functions_with_same_name.ink lex test/functions_with_same_name.ink lex
test/function_with_int_return.ink lex test/function_with_int_return.ink lex
test/inferred_types.ink lex
test/meta_block.ink lex test/meta_block.ink lex
test/multiple_functions.ink lex test/multiple_functions.ink lex
test/multiple_semicolons_everywhere.ink lex test/multiple_semicolons_everywhere.ink lex

View File

@@ -1,4 +1,4 @@
test/field_without_type_specifier.ink:2,0: error: Expected type specifier after field name. (program
x := 5.0; (fun vertex vs_main
^ []
 (:= x 5)))

View File

@@ -0,0 +1,18 @@
(program
(fun bar -> float
[]
(return 5))
(fun foo -> float
[]
(return (bar)))
(fun vertex vs_main -> float4 (@position)
[(:= pos float3 (@position))]
(:= f 2)
(:= i 10)
(= f (foo))
(:= v2 (float2 2 2))
(:= v3 (float3 2 2 3))
(:= v4 (float4 4 5 6 7))
(return (float4 1 1 1 1))))

View File

@@ -13,6 +13,7 @@ test/function_call_out_of_order_declaration.ink parse
test/function_call_return.ink parse test/function_call_return.ink parse
test/functions_with_same_name.ink parse test/functions_with_same_name.ink parse
test/function_with_int_return.ink parse test/function_with_int_return.ink parse
test/inferred_types.ink parse
test/meta_block.ink parse test/meta_block.ink parse
test/multiple_functions.ink parse test/multiple_functions.ink parse
test/multiple_semicolons_everywhere.ink parse test/multiple_semicolons_everywhere.ink parse

View File

@@ -0,0 +1,15 @@
scope (global) [
[foo] : () -> float
[vertex__vs_main] : (pos : float3) -> float4
[bar] : () -> float
scope (bar) []
scope (foo) []
scope (vertex__vs_main) [
[v2] : float2
[i] : int
[v4] : float4
[pos] : float3
[v3] : float3
[f] : float
]
]

View File

@@ -11,6 +11,7 @@ test/function_call_out_of_order_declaration.ink semant
test/function_call_return.ink semant test/function_call_return.ink semant
test/functions_with_same_name.ink semant test/functions_with_same_name.ink semant
test/function_with_int_return.ink semant test/function_with_int_return.ink semant
test/inferred_types.ink semant
test/multiple_functions.ink semant test/multiple_functions.ink semant
test/multiple_semicolons_everywhere.ink semant test/multiple_semicolons_everywhere.ink semant
test/pass_and_access_struct_fields_in_functions.ink semant test/pass_and_access_struct_fields_in_functions.ink semant