From b4d119230bed03a7a097e2293822474d35409a9c Mon Sep 17 00:00:00 2001 From: Niels Bross Date: Wed, 15 Jan 2025 21:32:18 +0100 Subject: [PATCH] Proper pretty printing and error handling on non-cool if condition. --- AST.jai | 178 ++++++++++++++++++------------- Parsing.jai | 4 +- Semantic_Analysis.jai | 17 ++- test/codegen_all.suite | 3 + test/compile_all.suite | 3 + test/lex/non_bool_cond.golden | 35 ++++++ test/lex/simple_if.golden | 37 +++++++ test/lex_all.suite | 4 + test/non_bool_cond.ink | 6 ++ test/parse/non_bool_cond.golden | 6 ++ test/parse/simple_else_if.golden | 14 +-- test/parse/simple_if.golden | 6 ++ test/parse/simple_if_else.golden | 7 ++ test/parse_all.suite | 4 + test/semant/non_bool_cond.golden | 6 ++ test/semant_all.suite | 4 + 16 files changed, 249 insertions(+), 85 deletions(-) create mode 100644 test/lex/non_bool_cond.golden create mode 100644 test/lex/simple_if.golden create mode 100644 test/non_bool_cond.ink create mode 100644 test/parse/non_bool_cond.golden create mode 100644 test/parse/simple_if.golden create mode 100644 test/parse/simple_if_else.golden create mode 100644 test/semant/non_bool_cond.golden diff --git a/AST.jai b/AST.jai index 2bb3040..8abc868 100644 --- a/AST.jai +++ b/AST.jai @@ -72,22 +72,25 @@ AST_Node :: struct { pixel_entry_point : bool; } - // =========================================================== // Pretty printing -pretty_print_call :: (node : *AST_Node, indentation : int, builder : *String_Builder) { - indent(builder, indentation); +pretty_print_call :: (node : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) { + if !skip_indent { + indent(builder, indentation); + } append(builder, "("); append(builder, node.name); if node.children.count > 0 { append(builder, " "); - pretty_print_children(node.children[0], indentation, builder, flags = 0); + pretty_print_children(node.children[0], indentation, builder, flags = 0, skip_indent = true); } append(builder, ")"); } -pretty_print_arglist :: (node : *AST_Node, indentation : int, builder : *String_Builder) { - indent(builder, indentation); +pretty_print_arglist :: (node : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) { + if !skip_indent { + indent(builder, indentation); + } append(builder, "["); pretty_print_children(node, indentation + 1, builder, flags = .NewLine); @@ -95,8 +98,10 @@ pretty_print_arglist :: (node : *AST_Node, indentation : int, builder : *String_ append(builder, "]"); } -pretty_print_fieldlist :: (node : *AST_Node, indentation : int, builder : *String_Builder) { - indent(builder, indentation); +pretty_print_fieldlist :: (node : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) { + if !skip_indent { + indent(builder, indentation); + } append(builder, "["); pretty_print_children(node, indentation + 1, builder, flags = .NewLine); @@ -104,14 +109,16 @@ pretty_print_fieldlist :: (node : *AST_Node, indentation : int, builder : *Strin append(builder, "]"); } -pretty_print_field :: (node : *AST_Node, indentation : int, builder : *String_Builder) { - indent(builder, indentation); +pretty_print_field :: (node : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) { + if !skip_indent { + indent(builder, indentation); + } print_to_builder(builder, tprint("(:= %", node.name)); if node.kind != .Unnamed_Field && node.token.ident_value.count > 0 { if node.array_field { append(builder, " ["); - pretty_print_node(node.children[0], 0, builder); + pretty_print_node(node.children[0], indentation, builder, true); append(builder, "]."); print_to_builder(builder, "%", node.token.ident_value); } else { @@ -128,28 +135,24 @@ pretty_print_field :: (node : *AST_Node, indentation : int, builder : *String_Bu if !node.array_field && node.children.count > 0 { append(builder, " "); - pretty_print_node(node.children[0], indentation, builder); + pretty_print_node(node.children[0], indentation, builder, true); } append(builder, ")"); } Children_Print_Flags :: enum_flags { - NewLine :: 1 << 0; - Separator :: 1 << 1; - Space :: 1 << 2; + NewLine :: 1 << 0; + Separator :: 1 << 1; + Space :: 1 << 2; + Dont_Skip_Indent_On_First :: 1 << 3; } -pretty_print_block :: (node : *AST_Node, indentation : int, builder : *String_Builder) { - for child : node.children { - pretty_print_node(child, indentation, builder); - if it_index != node.children.count - 1 { - append(builder, "\n"); - } - } +pretty_print_block :: (node : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) { + pretty_print_children(node, indentation, builder, flags = .NewLine | .Dont_Skip_Indent_On_First); } -pretty_print_children :: (parent : *AST_Node, indentation : int, builder : *String_Builder, flags : Children_Print_Flags = .Separator) { +pretty_print_children :: (parent : *AST_Node, indentation : int, builder : *String_Builder, flags : Children_Print_Flags = .Separator, skip_indent := false) { if !parent { return; } @@ -162,7 +165,20 @@ pretty_print_children :: (parent : *AST_Node, indentation : int, builder : *Stri if !child continue; - ind := ifx it_index > 0 indentation else 0; + + ind := indentation; + if flags & .Dont_Skip_Indent_On_First { + ind = indentation; + } else { + if it_index == 0 { + ind = 0; + } + } + + if skip_indent{ + ind = 0; + } + // skip := ifx it_index > 0 then false else true; pretty_print_node(child, ind, builder); if it_index != children.count - 1 { @@ -211,8 +227,10 @@ op_to_string :: (oper : Token) -> string { return ""; } -pretty_print_binary :: (node : *AST_Node, indentation : int, builder : *String_Builder) { - indent(builder, indentation); +pretty_print_binary :: (node : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) { + if !skip_indent { + indent(builder, indentation); + } append(builder, "("); op := node.token; @@ -225,16 +243,20 @@ pretty_print_binary :: (node : *AST_Node, indentation : int, builder : *String_B append(builder, ")"); } -pretty_print_unary :: (node : *AST_Node, indentation : int, builder : *String_Builder) { - indent(builder, indentation); +pretty_print_unary :: (node : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) { + if !skip_indent { + indent(builder, indentation); + } op := node.token; print_to_builder(builder, op_to_string(op)); pretty_print_node(node.children[0], 0, builder); } -print_return_node :: (node : *AST_Node, indentation : int, builder : *String_Builder) { - indent(builder, indentation); +print_return_node :: (node : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) { + if !skip_indent { + indent(builder, indentation); + } append(builder, "(return "); pretty_print_children(node, indentation, builder); @@ -242,8 +264,10 @@ print_return_node :: (node : *AST_Node, indentation : int, builder : *String_Bui append(builder, ")"); } -pretty_print_if :: (node : *AST_Node, indentation : int, builder : *String_Builder) { - indent(builder, indentation); +pretty_print_if :: (node : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) { + if !skip_indent { + indent(builder, indentation); + } append(builder, "(if "); condition := node.children[0]; @@ -251,83 +275,87 @@ pretty_print_if :: (node : *AST_Node, indentation : int, builder : *String_Build append(builder, "\n"); body := node.children[1]; - pretty_print_node(body, indentation, builder); + pretty_print_node(body, indentation + 4, builder); if node.children.count == 3 { append(builder, "\n"); - pretty_print_node(node.children[2], indentation, builder); + pretty_print_node(node.children[2], indentation + 4, builder); } append(builder, ")"); } -print_expression_statement :: (node : *AST_Node, indentation : int, builder : *String_Builder) { - indent(builder, indentation); +print_expression_statement :: (node : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) { + if !skip_indent { + indent(builder, indentation); + } if node.children[0] { pretty_print_node(node.children[0], 0, builder); } } -pretty_print_node :: (node : *AST_Node, indentation : int, builder : *String_Builder) { +pretty_print_node :: (node : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) { if node.kind == { case .Return; { - print_return_node(node, indentation, builder); + print_return_node(node, indentation, builder, skip_indent); } case .If; { - pretty_print_if(node, indentation + 4, builder); + pretty_print_if(node, indentation, builder, skip_indent); } case .Struct; case .ArgList; { - pretty_print_arglist(node, indentation + 2, builder); + pretty_print_arglist(node, indentation + 2, builder, skip_indent); } case .FieldList; { - pretty_print_fieldlist(node, indentation + 2, builder); + pretty_print_fieldlist(node, indentation + 2, builder, skip_indent); } case .Field; { - pretty_print_field(node, indentation, builder); + pretty_print_field(node, indentation, builder, skip_indent); } case .Unnamed_Field; { - pretty_print_field(node, indentation, builder); + pretty_print_field(node, indentation, builder, skip_indent); } case .Block; { - pretty_print_block(node, indentation, builder); + pretty_print_block(node, indentation, builder, skip_indent); } case .Binary; { - pretty_print_binary(node, indentation, builder); + pretty_print_binary(node, indentation, builder, skip_indent); } case .Unary; { - pretty_print_unary(node, indentation, builder); + pretty_print_unary(node, indentation, builder, skip_indent); } case .Variable; { - pretty_print_variable(node, indentation, builder); + pretty_print_variable(node, indentation, builder, skip_indent); } case .Expression_Statement; { - print_expression_statement(node, indentation, builder); + print_expression_statement(node, indentation, builder, skip_indent); } case .Integer; { - print_to_builder(builder, "%", node.integer_value); + print_to_builder(builder, "%", node.integer_value, skip_indent); } case .Float; { - print_to_builder(builder, "%", node.float_value); + print_to_builder(builder, "%", node.float_value, skip_indent); } case .Call; { - pretty_print_call(node, indentation, builder); + pretty_print_call(node, indentation, builder, skip_indent); } case .Error; { - print_to_builder(builder, "(error \"%\")", node.name); + print_to_builder(builder, "(error \"%\")", node.name, skip_indent); } } } -pretty_print_variable :: (node : *AST_Node, indentation : int, builder : *String_Builder) { - indent(builder, indentation); +pretty_print_variable :: (node : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) { + if !skip_indent { + indent(builder, indentation); + } print_to_builder(builder, "%", node.name); for child : node.children { if child.kind == .Variable { append(builder, "."); - pretty_print_variable(child, indentation, builder); + pretty_print_variable(child, indentation, builder, skip_indent = true); } else if child.kind == .Unary { append(builder, "["); pretty_print_node(child.children[0], 0, builder); @@ -336,8 +364,10 @@ pretty_print_variable :: (node : *AST_Node, indentation : int, builder : *String } } -pretty_print_declaration :: (declaration : *AST_Node, indentation : int, builder : *String_Builder) { - indent(builder, indentation); +pretty_print_declaration :: (declaration : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) { + if !skip_indent { + indent(builder, indentation); + } append(builder, "("); if declaration.foreign_declaration { @@ -385,24 +415,24 @@ pretty_print_declaration :: (declaration : *AST_Node, indentation : int, builder } if declaration.children.count > 0 { - print_to_builder(builder, "\n"); - if declaration.kind == .Function { - field_list := declaration.children[0]; - pretty_print_fieldlist(field_list, indentation + 1, builder); - append(builder, "\n"); - - if declaration.children.count > 1 { - body := declaration.children[1]; - pretty_print_node(body, indentation + 1, builder); - } - } else if declaration.kind == .Struct { - pretty_print_node(declaration.children[0], indentation + 1, builder); - } else { - pretty_print_node(declaration.children[0], indentation + 1, builder); - } - // print_to_builder(builder, "\n"); - // pretty_print_children(declaration, indentation + 1, builder, flags = .NewLine); + // if declaration.kind == .Function { + // field_list := declaration.children[0]; + // pretty_print_fieldlist(field_list, indentation + 1, builder); + // append(builder, "\n"); + + // if declaration.children.count > 1 { + // body := declaration.children[1]; + // pretty_print_node(body, indentation + 1, builder, true); + // } + // } else if declaration.kind == .Struct { + // pretty_print_node(declaration.children[0], indentation + 1, builder); + // } else { + // pretty_print_node(declaration.children[0], indentation + 1, builder); + // } + + print_to_builder(builder, "\n"); + pretty_print_children(declaration, indentation + 1, builder, flags = .NewLine); } append(builder, ")"); diff --git a/Parsing.jai b/Parsing.jai index 710041d..b2503bb 100644 --- a/Parsing.jai +++ b/Parsing.jai @@ -794,6 +794,7 @@ statement :: (parse_state : *Parse_State) -> *AST_Node { if_expression := expression(parse_state); add_child(node, if_expression); // consume(parse_state, .TOKEN_LEFTBRACE, "Expect '{' after if-condition."); + source_location.end = parse_state.previous; if_body := block(parse_state); if if_body.children.count > 0 { @@ -804,8 +805,7 @@ statement :: (parse_state : *Parse_State) -> *AST_Node { else_node := else_statement(parse_state); add_child(node, else_node); } - - source_location.end = parse_state.previous; + node.source_location = source_location; return node; diff --git a/Semantic_Analysis.jai b/Semantic_Analysis.jai index ea72bd9..61625e3 100644 --- a/Semantic_Analysis.jai +++ b/Semantic_Analysis.jai @@ -52,6 +52,7 @@ Typenames :: string.[ "double" , "Texture2D", "Sampler" , + "bool" , ]; Type_Variable :: struct { @@ -497,8 +498,10 @@ Attempting to access a field on a primitive type '%'. if_condition_has_to_be_boolean_type :: (checker : *Semantic_Checker, usage_site : *AST_Node, handle : Type_Variable_Handle) { /* Type of expression in if condition has to be bool. - if x > 100 { + if 100.0 ^^^^^^^ + + 100.0 has type float */ builder : String_Builder; @@ -510,9 +513,19 @@ if_condition_has_to_be_boolean_type :: (checker : *Semantic_Checker, usage_site indent(*builder, 1); cyan(*builder); - print_to_builder(*builder, "%\n", print_from_source_location(usage_site.source_location)); + location := usage_site.source_location; + + print_to_builder(*builder, "%\n", print_from_source_location(location)); indent(*builder, 1); + print_token_pointer(*builder, usage_site.children[0].source_location.begin); + append(*builder, "\n"); + + indent(*builder, 1); + + var := from_handle(checker, handle); + print_to_builder(*builder, "% has type %\n", print_from_source_location(*usage_site.children[0].source_location), proper_type_to_string(checker, var)); + message := builder_to_string(*builder,, temp); record_error(checker, message, usage_site.source_location, false); diff --git a/test/codegen_all.suite b/test/codegen_all.suite index c939b7c..1dd698b 100644 --- a/test/codegen_all.suite +++ b/test/codegen_all.suite @@ -16,6 +16,9 @@ test/multiple_semicolons_everywhere.ink codegen test/pass_and_access_struct_fields_in_functions.ink codegen test/passthrough.ink codegen test/property_rename.ink codegen +test/simple_else_if.ink codegen +test/simple_if_else.ink codegen +test/simple_if.ink codegen test/simple_struct_access.ink codegen test/struct_within_struct.ink codegen test/unary.ink codegen diff --git a/test/compile_all.suite b/test/compile_all.suite index 58b17f2..7f12ef6 100644 --- a/test/compile_all.suite +++ b/test/compile_all.suite @@ -16,6 +16,9 @@ test/multiple_functions.ink compile test/multiple_semicolons_everywhere.ink compile test/pass_and_access_struct_fields_in_functions.ink compile test/passthrough.ink compile +test/simple_else_if.ink compile +test/simple_if_else.ink compile +test/simple_if.ink compile test/simple_struct_access.ink compile test/struct_within_struct.ink compile test/unary.ink compile diff --git a/test/lex/non_bool_cond.golden b/test/lex/non_bool_cond.golden new file mode 100644 index 0000000..38f1583 --- /dev/null +++ b/test/lex/non_bool_cond.golden @@ -0,0 +1,35 @@ +{kind = TOKEN_VERTEX; ; index = 0 ; length = 6 line = 1 ; column = 0 ; value ='vertex'; } +{kind = TOKEN_IDENTIFIER; ; index = 7 ; length = 4 line = 1 ; column = 7 ; value ='main'; } +{kind = TOKEN_DOUBLECOLON; ; index = 12 ; length = 2 line = 1 ; column = 12 ; value ='::'; } +{kind = TOKEN_LEFTPAREN; ; index = 15 ; length = 1 line = 1 ; column = 15 ; value ='('; } +{kind = TOKEN_IDENTIFIER; ; index = 16 ; length = 3 line = 1 ; column = 16 ; value ='pos'; } +{kind = TOKEN_COLON; ; index = 20 ; length = 1 line = 1 ; column = 20 ; value =':'; } +{kind = TOKEN_IDENTIFIER; ; index = 22 ; length = 6 line = 1 ; column = 22 ; value ='float3'; } +{kind = TOKEN_AT; ; index = 29 ; length = 1 line = 1 ; column = 29 ; value ='@'; } +{kind = TOKEN_IDENTIFIER; ; index = 30 ; length = 8 line = 1 ; column = 30 ; value ='position'; } +{kind = TOKEN_RIGHTPAREN; ; index = 38 ; length = 1 line = 1 ; column = 38 ; value =')'; } +{kind = TOKEN_ARROW; ; index = 40 ; length = 2 line = 1 ; column = 40 ; value ='->'; } +{kind = TOKEN_IDENTIFIER; ; index = 43 ; length = 6 line = 1 ; column = 43 ; value ='float4'; } +{kind = TOKEN_AT; ; index = 50 ; length = 1 line = 1 ; column = 50 ; value ='@'; } +{kind = TOKEN_IDENTIFIER; ; index = 51 ; length = 8 line = 1 ; column = 51 ; value ='position'; } +{kind = TOKEN_LEFTBRACE; ; index = 60 ; length = 1 line = 1 ; column = 60 ; value ='{'; } +{kind = TOKEN_IF; ; index = 64 ; length = 2 line = 2 ; column = 0 ; value ='if'; } +{kind = TOKEN_FLOATLITERAL; ; index = 67 ; length = 3 line = 2 ; column = 3 ; value ='1'; } +{kind = TOKEN_LEFTBRACE; ; index = 71 ; length = 1 line = 2 ; column = 7 ; value ='{'; } +{kind = TOKEN_RETURN; ; index = 76 ; length = 6 line = 3 ; column = 0 ; value ='return'; } +{kind = TOKEN_IDENTIFIER; ; index = 83 ; length = 6 line = 3 ; column = 7 ; value ='float4'; } +{kind = TOKEN_LEFTPAREN; ; index = 89 ; length = 1 line = 3 ; column = 13 ; value ='('; } +{kind = TOKEN_IDENTIFIER; ; index = 90 ; length = 3 line = 3 ; column = 14 ; value ='pos'; } +{kind = TOKEN_COMMA; ; index = 93 ; length = 1 line = 3 ; column = 17 ; value =','; } +{kind = TOKEN_FLOATLITERAL; ; index = 95 ; length = 3 line = 3 ; column = 19 ; value ='1'; } +{kind = TOKEN_RIGHTPAREN; ; index = 98 ; length = 1 line = 3 ; column = 22 ; value =')'; } +{kind = TOKEN_SEMICOLON; ; index = 99 ; length = 1 line = 3 ; column = 23 ; value =';'; } +{kind = TOKEN_RIGHTBRACE; ; index = 103 ; length = 1 line = 4 ; column = 0 ; value ='}'; } +{kind = TOKEN_RETURN; ; index = 107 ; length = 6 line = 5 ; column = 0 ; value ='return'; } +{kind = TOKEN_IDENTIFIER; ; index = 114 ; length = 6 line = 5 ; column = 7 ; value ='float4'; } +{kind = TOKEN_LEFTPAREN; ; index = 120 ; length = 1 line = 5 ; column = 13 ; value ='('; } +{kind = TOKEN_FLOATLITERAL; ; index = 121 ; length = 3 line = 5 ; column = 14 ; value ='0'; } +{kind = TOKEN_RIGHTPAREN; ; index = 124 ; length = 1 line = 5 ; column = 17 ; value =')'; } +{kind = TOKEN_SEMICOLON; ; index = 125 ; length = 1 line = 5 ; column = 18 ; value =';'; } +{kind = TOKEN_RIGHTBRACE; ; index = 128 ; length = 1 line = 6 ; column = 0 ; value ='}'; } +{kind = TOKEN_EOF; ; index = 131 ; length = 0 line = 7 ; column = 0 ; value =''; } diff --git a/test/lex/simple_if.golden b/test/lex/simple_if.golden new file mode 100644 index 0000000..9808533 --- /dev/null +++ b/test/lex/simple_if.golden @@ -0,0 +1,37 @@ +{kind = TOKEN_VERTEX; ; index = 0 ; length = 6 line = 1 ; column = 0 ; value ='vertex'; } +{kind = TOKEN_IDENTIFIER; ; index = 7 ; length = 4 line = 1 ; column = 7 ; value ='main'; } +{kind = TOKEN_DOUBLECOLON; ; index = 12 ; length = 2 line = 1 ; column = 12 ; value ='::'; } +{kind = TOKEN_LEFTPAREN; ; index = 15 ; length = 1 line = 1 ; column = 15 ; value ='('; } +{kind = TOKEN_IDENTIFIER; ; index = 16 ; length = 3 line = 1 ; column = 16 ; value ='pos'; } +{kind = TOKEN_COLON; ; index = 20 ; length = 1 line = 1 ; column = 20 ; value =':'; } +{kind = TOKEN_IDENTIFIER; ; index = 22 ; length = 6 line = 1 ; column = 22 ; value ='float3'; } +{kind = TOKEN_AT; ; index = 29 ; length = 1 line = 1 ; column = 29 ; value ='@'; } +{kind = TOKEN_IDENTIFIER; ; index = 30 ; length = 8 line = 1 ; column = 30 ; value ='position'; } +{kind = TOKEN_RIGHTPAREN; ; index = 38 ; length = 1 line = 1 ; column = 38 ; value =')'; } +{kind = TOKEN_ARROW; ; index = 40 ; length = 2 line = 1 ; column = 40 ; value ='->'; } +{kind = TOKEN_IDENTIFIER; ; index = 43 ; length = 6 line = 1 ; column = 43 ; value ='float4'; } +{kind = TOKEN_AT; ; index = 50 ; length = 1 line = 1 ; column = 50 ; value ='@'; } +{kind = TOKEN_IDENTIFIER; ; index = 51 ; length = 8 line = 1 ; column = 51 ; value ='position'; } +{kind = TOKEN_LEFTBRACE; ; index = 60 ; length = 1 line = 1 ; column = 60 ; value ='{'; } +{kind = TOKEN_IF; ; index = 64 ; length = 2 line = 2 ; column = 0 ; value ='if'; } +{kind = TOKEN_INTLITERAL; ; index = 67 ; length = 1 line = 2 ; column = 3 ; value ='0'; } +{kind = TOKEN_GREATER; ; index = 69 ; length = 1 line = 2 ; column = 5 ; value ='>'; } +{kind = TOKEN_INTLITERAL; ; index = 71 ; length = 3 line = 2 ; column = 7 ; value ='100'; } +{kind = TOKEN_LEFTBRACE; ; index = 75 ; length = 1 line = 2 ; column = 11 ; value ='{'; } +{kind = TOKEN_RETURN; ; index = 80 ; length = 6 line = 3 ; column = 0 ; value ='return'; } +{kind = TOKEN_IDENTIFIER; ; index = 87 ; length = 6 line = 3 ; column = 7 ; value ='float4'; } +{kind = TOKEN_LEFTPAREN; ; index = 93 ; length = 1 line = 3 ; column = 13 ; value ='('; } +{kind = TOKEN_IDENTIFIER; ; index = 94 ; length = 3 line = 3 ; column = 14 ; value ='pos'; } +{kind = TOKEN_COMMA; ; index = 97 ; length = 1 line = 3 ; column = 17 ; value =','; } +{kind = TOKEN_FLOATLITERAL; ; index = 99 ; length = 3 line = 3 ; column = 19 ; value ='1'; } +{kind = TOKEN_RIGHTPAREN; ; index = 102 ; length = 1 line = 3 ; column = 22 ; value =')'; } +{kind = TOKEN_SEMICOLON; ; index = 103 ; length = 1 line = 3 ; column = 23 ; value =';'; } +{kind = TOKEN_RIGHTBRACE; ; index = 107 ; length = 1 line = 4 ; column = 0 ; value ='}'; } +{kind = TOKEN_RETURN; ; index = 111 ; length = 6 line = 5 ; column = 0 ; value ='return'; } +{kind = TOKEN_IDENTIFIER; ; index = 118 ; length = 6 line = 5 ; column = 7 ; value ='float4'; } +{kind = TOKEN_LEFTPAREN; ; index = 124 ; length = 1 line = 5 ; column = 13 ; value ='('; } +{kind = TOKEN_FLOATLITERAL; ; index = 125 ; length = 3 line = 5 ; column = 14 ; value ='0'; } +{kind = TOKEN_RIGHTPAREN; ; index = 128 ; length = 1 line = 5 ; column = 17 ; value =')'; } +{kind = TOKEN_SEMICOLON; ; index = 129 ; length = 1 line = 5 ; column = 18 ; value =';'; } +{kind = TOKEN_RIGHTBRACE; ; index = 132 ; length = 1 line = 6 ; column = 0 ; value ='}'; } +{kind = TOKEN_EOF; ; index = 135 ; length = 0 line = 7 ; column = 0 ; value =''; } diff --git a/test/lex_all.suite b/test/lex_all.suite index 699b02d..b4e73d5 100644 --- a/test/lex_all.suite +++ b/test/lex_all.suite @@ -17,10 +17,14 @@ test/inferred_types.ink lex test/meta_block.ink lex test/multiple_functions.ink lex test/multiple_semicolons_everywhere.ink lex +test/non_bool_cond.ink lex test/pass_and_access_struct_fields_in_functions.ink lex test/passthrough.ink lex test/property_rename.ink lex test/redeclared_variable.ink lex +test/simple_else_if.ink lex +test/simple_if_else.ink lex +test/simple_if.ink lex test/simple_struct_access.ink lex test/struct_access_primitive_type.ink lex test/struct_within_struct.ink lex diff --git a/test/non_bool_cond.ink b/test/non_bool_cond.ink new file mode 100644 index 0000000..9ec6373 --- /dev/null +++ b/test/non_bool_cond.ink @@ -0,0 +1,6 @@ +vertex main :: (pos : float3 @position) -> float4 @position { + if 1.0 { + return float4(pos, 1.0); + } + return float4(0.0); +} diff --git a/test/parse/non_bool_cond.golden b/test/parse/non_bool_cond.golden new file mode 100644 index 0000000..19b9cad --- /dev/null +++ b/test/parse/non_bool_cond.golden @@ -0,0 +1,6 @@ +(program + (fun vertex vs_main -> float4 (@position) + [(:= pos float3 (@position))] + (if 1 + (return (float4 pos 1))) + (return (float4 0)))) \ No newline at end of file diff --git a/test/parse/simple_else_if.golden b/test/parse/simple_else_if.golden index fd3b76b..111f8a9 100644 --- a/test/parse/simple_else_if.golden +++ b/test/parse/simple_else_if.golden @@ -1,9 +1,9 @@ (program (fun vertex vs_main -> float4 (@position) - [(:= pos float3 (@position))] - (if (> pos.x 100) - (return (float4 pos 1)) - (if (> pos.x 50) - (return (float4 pos 1)) - (return (float4 1)))) - (return (float4 0)))) \ No newline at end of file + [(:= pos float3 (@position))] + (if (> pos.x 100) + (return (float4 pos 1)) + (if (> pos.x 50) + (return (float4 pos 1)) + (return (float4 1)))) + (return (float4 0)))) \ No newline at end of file diff --git a/test/parse/simple_if.golden b/test/parse/simple_if.golden new file mode 100644 index 0000000..3473f24 --- /dev/null +++ b/test/parse/simple_if.golden @@ -0,0 +1,6 @@ +(program + (fun vertex vs_main -> float4 (@position) + [(:= pos float3 (@position))] + (if (> 0 100) + (return (float4 pos 1))) + (return (float4 0)))) \ No newline at end of file diff --git a/test/parse/simple_if_else.golden b/test/parse/simple_if_else.golden new file mode 100644 index 0000000..ab5dcdf --- /dev/null +++ b/test/parse/simple_if_else.golden @@ -0,0 +1,7 @@ +(program + (fun vertex vs_main -> float4 (@position) + [(:= pos float3 (@position))] + (if (> 0 100) + (return (float4 pos 1)) + (return (float4 1))) + (return (float4 0)))) \ No newline at end of file diff --git a/test/parse_all.suite b/test/parse_all.suite index 9c9584c..30b4b93 100644 --- a/test/parse_all.suite +++ b/test/parse_all.suite @@ -17,10 +17,14 @@ test/inferred_types.ink parse test/meta_block.ink parse test/multiple_functions.ink parse test/multiple_semicolons_everywhere.ink parse +test/non_bool_cond.ink parse test/pass_and_access_struct_fields_in_functions.ink parse test/passthrough.ink parse test/property_rename.ink parse test/redeclared_variable.ink parse +test/simple_else_if.ink parse +test/simple_if_else.ink parse +test/simple_if.ink parse test/simple_struct_access.ink parse test/struct_access_primitive_type.ink parse test/struct_within_struct.ink parse diff --git a/test/semant/non_bool_cond.golden b/test/semant/non_bool_cond.golden new file mode 100644 index 0000000..87b2739 --- /dev/null +++ b/test/semant/non_bool_cond.golden @@ -0,0 +1,6 @@ +test/non_bool_cond.ink:0,0: error: Type of expression in if condition has to be bool. + if 1.0 + ^^^ + 1.0 has type float + + \ No newline at end of file diff --git a/test/semant_all.suite b/test/semant_all.suite index 7773b41..0108363 100644 --- a/test/semant_all.suite +++ b/test/semant_all.suite @@ -14,10 +14,14 @@ test/function_with_int_return.ink semant test/inferred_types.ink semant test/multiple_functions.ink semant test/multiple_semicolons_everywhere.ink semant +test/non_bool_cond.ink semant test/pass_and_access_struct_fields_in_functions.ink semant test/passthrough.ink semant test/property_rename.ink semant test/redeclared_variable.ink semant +test/simple_else_if.ink semant +test/simple_if_else.ink semant +test/simple_if.ink semant test/simple_struct_access.ink semant test/struct_access_primitive_type.ink semant test/struct_within_struct.ink semant