Compare commits

7 Commits

16 changed files with 124 additions and 75 deletions

View File

@@ -283,7 +283,6 @@ pretty_print_variable :: (node : *AST_Node, indentation : int, builder : *String
print_to_builder(builder, "%", node.name);
for child : node.children {
print("%\n", child.kind);
if child.kind == .Variable {
append(builder, ".");
pretty_print_variable(child, indentation, builder);

View File

@@ -1,3 +1,12 @@
/////////////////////////////////////
//~ nbr:
//
/////////////////////////////////////
//~ nbr: Codegen TODOs
//
// [ ] Prefix output of property values with __PROPERTIES so we don't get name clashes
Output_Language :: enum {
HLSL;
GLSL; // @Incomplete
@@ -82,7 +91,16 @@ emit_field :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
field := h2tv(state.type_variables, find_result.type_variable);
indent(state, indentation);
print_to_builder(*state.builder, "% ", dx11_type_to_string(field));
if field.struct_field_parent {
parent_tv := h2tv(state.type_variables, field.struct_field_parent.type_variable);
if parent_tv.typename == "properties" {
append(*state.builder, "__PROPERTIES__");
}
}
print_to_builder(*state.builder, "%", node.name);
if field.type == .Sampler {
@@ -100,7 +118,7 @@ emit_field :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
emit_node(state, child, 0);
}
for i :0..field.child_count - 1 {
for i :0..field.children.count - 1 {
child := h2tv(state.type_variables, field.children[i]);
emit_node(state, child.source_node, 0);
}
@@ -111,6 +129,8 @@ emit_field :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
append(*state.builder, " : POSITION");
} else if hint.ident_value == "uv" {
append(*state.builder, " : TEXCOORD0");
} else if hint.ident_value == "outposition" {
append(*state.builder, " : SV_POSITION");
}
}
}
@@ -363,6 +383,13 @@ emit_node :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
is_properties := type_var.typename == "properties";
if !is_properties {
if type_var.struct_field_parent {
parent_tv := h2tv(state.type_variables, type_var.struct_field_parent.type_variable);
if parent_tv.typename == "properties" {
append(*state.builder, "__PROPERTIES__");
}
}
print_to_builder(*state.builder, "%", node.name);
}
@@ -375,6 +402,11 @@ emit_node :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
}
case .Binary; {
indent(*state.builder, indentation);
if node.token.kind != .TOKEN_ASSIGN {
append(*state.builder, "(");
}
lhs := node.children[0];
rhs := node.children[1];
emit_node(state, lhs, 0);
@@ -383,6 +415,9 @@ emit_node :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
emit_operator(state, node.token.kind);
append(*state.builder, " ");
emit_node(state, rhs, 0);
if node.token.kind != .TOKEN_ASSIGN {
append(*state.builder, ")");
}
}
case .Unary; {
assert(false, "Not implemented yet: unary");

View File

@@ -71,9 +71,8 @@ Type_Variable :: struct {
typename : string;
is_array : bool;
MAX_TYPE_VARIABLE_CHILDREN :: 16;
children : [MAX_TYPE_VARIABLE_CHILDREN]Type_Variable_Handle;
child_count : int;
MAX_TYPE_VARIABLE_CHILDREN :: 32;
children : Static_Array(Type_Variable_Handle, MAX_TYPE_VARIABLE_CHILDREN);
//@Note(niels): For constant buffers
resource_index : u32;
@@ -362,10 +361,9 @@ no_matching_overload_found :: (checker : *Semantic_Checker, call : *AST_Node, ov
func_var := h2tv(checker, func.type_variable);
if arg_list.children.count != func_var.child_count {
print_to_builder(*builder, "Not enough arguments: Wanted %, got %.\n\n", func_var.child_count, arg_list.children.count);
if arg_list.children.count != func_var.children.count {
print_to_builder(*builder, "Not enough arguments: Wanted %, got %.\n\n", func_var.children.count, arg_list.children.count);
}
}
}
@@ -544,7 +542,7 @@ type_mismatch :: (checker : *Semantic_Checker, usage_site : *AST_Node, expect_no
if got_var.builtin {
print_to_builder(*builder, "% :: (", got_var.name);
for i: 0..got_var.child_count - 1{
for i: 0..got_var.children.count - 1{
child_handle := got_var.children[i];
child := h2tv(checker, child_handle);
@@ -717,16 +715,16 @@ new_type_variable :: (checker : *Semantic_Checker) -> *Type_Variable, Type_Varia
}
add_child :: (variable : *Type_Variable, child : Type_Variable_Handle) {
assert(variable.child_count < Type_Variable.MAX_TYPE_VARIABLE_CHILDREN);
variable.children[variable.child_count] = child;
variable.child_count += 1;
assert(variable.children.count < Type_Variable.MAX_TYPE_VARIABLE_CHILDREN);
array_add(*variable.children, child);
// variable.children[variable.children.count] = child;
// variable.children.count += 1;
}
add_child :: (checker : *Semantic_Checker, handle : Type_Variable_Handle, child : Type_Variable_Handle) {
variable := h2tv(checker, handle);
assert(variable.child_count < Type_Variable.MAX_TYPE_VARIABLE_CHILDREN);
variable.children[variable.child_count] = child;
variable.child_count += 1;
assert(variable.children.count < Type_Variable.MAX_TYPE_VARIABLE_CHILDREN);
array_add(*variable.children, child);
}
init_semantic_checker :: (checker : *Semantic_Checker, root : *AST_Node, path : string) {
@@ -1029,7 +1027,7 @@ declare_function :: (checker : *Semantic_Checker, node : *AST_Node, builtin : bo
}
all_same : bool = true;
for i : 0..func_var.child_count - 1 {
for i : 0..func_var.children.count - 1 {
arg := func_var.children[i];
node_child := field_list.children[i];
@@ -1129,7 +1127,7 @@ create_function_constraint :: (checker : *Semantic_Checker, node : *AST_Node) {
constraint.kind = .Function_Decl;
constraint.function.symbol_variable = function.type_variable;
for i : 0..variable.child_count - 1 {
for i : 0..variable.children.count - 1 {
arg_var := variable.children[i];
if arg_var > 0 {
@@ -1379,11 +1377,11 @@ create_call_constraint :: (checker : *Semantic_Checker, node : *AST_Node, type_v
function := h2tv(checker, func.type_variable);
if arg_count != function.child_count {
if arg_count != function.children.count {
continue;
}
if node.children.count == 0 && function.child_count == 0 {
if node.children.count == 0 && function.children.count == 0 {
overload_found = true;
break;
}
@@ -1393,7 +1391,7 @@ create_call_constraint :: (checker : *Semantic_Checker, node : *AST_Node, type_v
for arg : arg_vars {
function_param := function.children[it_index];
if !types_compatible(checker, arg.var, function_param) {
if !types_compatible(checker, arg.var, function_param, true) {
if all_args_match {
arg_node = arg.node;
}
@@ -1573,7 +1571,7 @@ Unification_Result :: enum {
Unification_Failure;
}
types_compatible :: (checker : *Semantic_Checker, lhs : Type_Variable_Handle, rhs : Type_Variable_Handle) -> bool {
types_compatible :: (checker : *Semantic_Checker, lhs : Type_Variable_Handle, rhs : Type_Variable_Handle, param_matching : bool = false) -> bool {
lhs_var := h2tv(checker, lhs);
rhs_var := h2tv(checker, rhs);
@@ -1582,12 +1580,14 @@ types_compatible :: (checker : *Semantic_Checker, lhs : Type_Variable_Handle, rh
case .Half; #through;
case .Float; #through;
case .Double; {
if rhs_var.type == .Struct {
if rhs_var.typename == {
case "float2"; #through;
case "float3"; #through;
case "float4"; {
return true;
if !param_matching {
if rhs_var.type == .Struct {
if rhs_var.typename == {
case "float2"; #through;
case "float3"; #through;
case "float4"; {
return true;
}
}
}
}
@@ -1630,11 +1630,11 @@ types_compatible :: (checker : *Semantic_Checker, lhs : Type_Variable_Handle, rh
lhs_struct_var := h2tv(checker, lhs_struct.type_variable);
rhs_struct_var := h2tv(checker, rhs_struct.type_variable);
if lhs_struct_var.child_count != rhs_struct_var.child_count {
if lhs_struct_var.children.count != rhs_struct_var.children.count {
return false;
}
for i : 0..lhs_struct_var.child_count - 1 {
for i : 0..lhs_struct_var.children.count - 1 {
lhs_child := lhs_struct_var.children[i];
rhs_child := rhs_struct_var.children[i];
if !types_compatible(checker, lhs_child, rhs_child) {
@@ -1916,14 +1916,14 @@ pretty_print_struct :: (checker : *Semantic_Checker, builder : *String_Builder,
print_key(checker, builder, name);
append(builder, "{");
for 0..struct_type.child_count - 1 {
for 0..struct_type.children.count - 1 {
child_handle := struct_type.children[it];
child := h2tv(checker, child_handle);
print_to_builder(builder, child.name);
append(builder, " : ");
print_to_builder(builder, type_to_string(child));
if it < struct_type.child_count - 1 {
if it < struct_type.children.count - 1 {
append(builder, ", ");
}
}
@@ -2187,14 +2187,14 @@ pretty_print_type_variable :: (checker : *Semantic_Checker, type_variable : *Typ
if type_variable.kind == .Declaration {
append(builder, "{");
for 0..type_variable.child_count - 1 {
for 0..type_variable.children.count - 1 {
child_handle := type_variable.children[it];
child := h2tv(checker, child_handle);
print_to_builder(builder, child.name);
append(builder, " : ");
print_to_builder(builder, type_to_string(child));
if it < type_variable.child_count - 1 {
if it < type_variable.children.count - 1 {
append(builder, ", ");
}
}

View File

@@ -310,8 +310,8 @@ run_semantic_analysis_test :: (file_path : string, root : *AST_Node, output_type
result_text = pretty_print_symbol_table(*checker, temp);
constraints := pretty_print_type_constraints(*checker, temp);
type_vars := pretty_print_type_variables(*checker, temp);
print("Constraints\n%\n", constraints);
print("Solution\n%\n", type_vars);
// print("Constraints\n%\n", constraints);
// print("Solution\n%\n", type_vars);
}
if output_type & .StdOut {
@@ -477,7 +477,23 @@ run_test :: (file_path : string, stage_flags : Stage_Flags, results : *[..]Resul
}
run_test :: (test_case : Test_Case, results : *[..]Result, output_type : Output_Type = 0) {
print("%Running test: %\n", cyan(), test_case.path);
print("%Running test: %......", cyan(), test_case.path);
// path 30
// len 35
// == 5
// path 20
// len = 35
// == 15
len := 50;
rest := len - test_case.path.count;
for i: 0..rest {
print(" ");
}
run_test(test_case.path, test_case.stage_flags, results, output_type);
}
@@ -525,7 +541,7 @@ run_test_suite :: (using suite : *Test_Suite, output_type : Output_Type = 0) {
}
}
print("\n");
// print("\n");
}
print("\n");
@@ -620,26 +636,26 @@ evaluate_result :: (result : Result) {
if #complete result.type == {
case .File_Read_Failed; {
print("%", red());
print("% failed with File_Read_Failed\n", result.path);
print(" %", red());
print("failed with File_Read_Failed\n");
}
case .Golden_File_Not_Found; {
print("%", red());
print("% failed with Golden File Not Found for stage %\n", result.path, stage);
print(" %", red());
print("failed with Golden File Not Found for stage %\n", stage);
}
case .StdOut; {
}
case .Golden_Output; {
print("%", yellow());
print("% output new golden file at %\n", result.path, result.golden_path);
print(" %", yellow());
print("output new golden file at %\n", result.golden_path);
}
case .Passed; {
print("%", green());
print("% passed %\n", result.path, stage);
print(" %", green());
print("passed %\n", stage);
}
case .Failed; {
print("%", red());
print("% failed %\n", result.path, stage);
print(" %", red());
print("failed %\n", stage);
}
}

View File

@@ -56,6 +56,7 @@ Hint_Kind :: enum {
None;
Position;
UV;
Target;
Custom;
@@ -319,7 +320,7 @@ type_variable_to_field :: (checker : *Semantic_Checker, variable : *Type_Variabl
type_var := h2tv(checker, find_result.type_variable);
for i : 0..type_var.child_count - 1 {
for i : 0..type_var.children.count - 1 {
child := type_var.children[i];
child_field := type_variable_to_field(checker, h2tv(checker, child));
array_add(*type.children, child_field);
@@ -335,6 +336,8 @@ type_variable_to_field :: (checker : *Semantic_Checker, variable : *Type_Variabl
if hint.ident_value == "position" {
// @Incomplete(nb): Should be a lookup table somewhere
field_hint.kind = .Position;
} else if hint.ident_value == "uv" {
field_hint.kind = .UV;
} else if starts_with(hint.ident_value, "target") {
// @Incomplete(nb): Should be a lookup table somewhere
index_str : string;
@@ -453,7 +456,7 @@ compile_file :: (compiler : *Shader_Compiler, path : string) -> Compilation_Resu
cb := array_add(*result.collection.cbuffers);
for i : 0..variable.child_count - 1 {
for i : 0..variable.children.count - 1 {
child := variable.children[i];
field : Property_Field;
field.base_field = type_variable_to_field(*checker, h2tv(*checker, child));;
@@ -467,7 +470,7 @@ compile_file :: (compiler : *Shader_Compiler, path : string) -> Compilation_Resu
if find_result {
property_variable := h2tv(check_result.type_variables, find_result.type_variable);
for i : 0..property_variable.child_count - 1 {
for i : 0..property_variable.children.count - 1 {
child := property_variable.children[i];
field := type_variable_to_field(*checker, h2tv(*checker, child));
prop_field : Property_Field;

View File

@@ -1,5 +1,5 @@
void vs_main()
{
float x = 2.0f + 5.0f;
float x = (2.0f + 5.0f);
}

View File

@@ -1,6 +1,6 @@
cbuffer __PROPERTIES : register(b0)
{
float4 color;
float4 __PROPERTIES__color;
}
@@ -11,6 +11,6 @@ float3 vs_main(float3 pos : POSITION) : SV_POSITION
float4 ps_main() : SV_TARGET
{
return color;
return __PROPERTIES__color;
}

View File

@@ -2,6 +2,6 @@ void vs_main()
{
float x = 5.0f;
float y = 3000.0f;
float z = y * y + x;
float z = ((y * y) + x);
}

View File

@@ -1,4 +1,2 @@
struct Foo;
struct Foo {}
struct Foo {};

View File

@@ -1,16 +1,16 @@
cbuffer __PROPERTIES : register(b0)
{
float4 color;
float4 __PROPERTIES__color;
}
float3 vs_main(float3 pos : POSITION, float2 uv) : SV_POSITION
float3 vs_main(float3 pos : POSITION, float2 uv : TEXCOORD0) : SV_POSITION
{
return pos;
}
float4 ps_main() : SV_TARGET
{
return color;
return __PROPERTIES__color;
}

View File

@@ -8,7 +8,7 @@ int foo()
float bar()
{
return 1235.0f * 500;
return (1235.0f * 500);
}
void vs_main()

View File

@@ -1,15 +1,13 @@
struct Foo;
float foo(Foo f);
struct Foo
{
float some_data;
}
};
float foo(Foo f)
{
return f.some_data * 2.0f;
return (f.some_data * 2.0f);
}
void vs_main()

View File

@@ -1,9 +1,7 @@
struct Data;
struct Data
{
float4 color;
}
};
void vs_main()
{

View File

@@ -1,15 +1,12 @@
struct Foo;
struct Bar;
struct Foo
{
float4 color;
}
};
struct Bar
{
Foo t;
}
};
void vs_main()
{

7
test/double_access.shd Normal file
View File

@@ -0,0 +1,7 @@
p :: properties {
v : float2;
}
vertex main ::() {
x : float = p.v.x / p.v.y;
}

View File

@@ -6,8 +6,6 @@ test/empty_struct.shd semant
test/empty_vertex_main.shd semant
test/empty_vertex_main_with_position_parameter.shd semant
test/field_assignment.shd semant
test/field_without_type_specifier.shd semant
test/float_suffix.shd semant
test/function_call.shd semant
test/function_call_out_of_order_declaration.shd semant
test/function_call_return.shd semant