Fix type checker not promoting ints. Fix some issues in the codegen.

This commit is contained in:
2024-09-11 07:24:10 +02:00
parent 517209c886
commit ff668b6c95
5 changed files with 98 additions and 24 deletions

View File

@@ -41,13 +41,48 @@ indent :: (state : *Codegen_State, indentation : int) {
for 1..indentation append(*state.builder, " ");
}
dx11_type_to_string :: (type_variable : Type_Variable) -> string {
if type_variable.type == {
case .Invalid;
return "{{invalid}}";
case .Unit;
return "()";
case .Int; {
return "int";
}
case .Half; {
return "half";
}
case .Float; {
return "float";
}
case .Double; {
return "double";
}
case .Sampler; {
return "SamplerState";
}
case .Texture2D; {
return "Texture2D";
}
case .Function; #through;
case .Struct; {
return type_variable.typename;
}
case .Array;
return "array";
}
return "";
}
emit_field :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
find_result := find_symbol(state.scope_stack, node.name, state.current_scope);
field := h2tv(state.type_variables, find_result.type_variable);
indent(state, indentation);
print_to_builder(*state.builder, "% ", type_to_string(field));
print_to_builder(*state.builder, "% ", dx11_type_to_string(field));
print_to_builder(*state.builder, "%", node.name);
if field.type == .Sampler {
@@ -74,6 +109,8 @@ emit_field :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
if hint.ident_value == "position" {
// @Incomplete(nb): Should be a lookup table somewhere
append(*state.builder, " : POSITION");
} else if hint.ident_value == "uv" {
append(*state.builder, " : TEXCOORD0");
}
}
}
@@ -97,7 +134,7 @@ emit_call :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
emit_node(state, args.children[0], 0);
append(*state.builder, ".");
print_to_builder(*state.builder, "%(", node.name);
print_to_builder(*state.builder, "Sample(");
for i : 1..args.children.count - 1 {
child := args.children[i];
@@ -199,7 +236,7 @@ emit_function :: (state : *Codegen_State, node : *AST_Node, indentation : int, e
if function_variable.return_type_variable {
return_variable := h2tv(state.type_variables, function_variable.return_type_variable);
print_to_builder(*state.builder, "% ", type_to_string(return_variable));
print_to_builder(*state.builder, "% ", dx11_type_to_string(return_variable));
} else {
append(*state.builder, "void ");
}
@@ -390,7 +427,7 @@ emit_struct :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
emit_field_list(state, field_list, indentation);
append(*state.builder, "}\n\n");
append(*state.builder, "};\n\n");
state.current_scope = current_scope;
}
@@ -434,20 +471,20 @@ emit_declaration :: (state : *Codegen_State, node : *AST_Node) {
codegen :: (state : *Codegen_State) -> Codegen_Result {
found_function : bool = false;
found_struct : bool = false;
// found_struct : bool = false;
for variable : state.type_variables {
if variable.type == .Struct && variable.kind == .Declaration && !variable.builtin {
if variable.source_node.kind == .Properties continue;
if variable.source_node.kind == .Meta continue;
print_to_builder(*state.builder, "struct %;\n", variable.source_node.name);
found_struct = true;
}
}
// for variable : state.type_variables {
// if variable.type == .Struct && variable.kind == .Declaration && !variable.builtin {
// if variable.source_node.kind == .Properties continue;
// if variable.source_node.kind == .Meta continue;
// print_to_builder(*state.builder, "struct %;\n", variable.source_node.name);
// found_struct = true;
// }
// }
if found_struct {
append(*state.builder, "\n");
}
// if found_struct {
// append(*state.builder, "\n");
// }
for variable : state.type_variables {
if variable.type == .Function && !variable.builtin