diff --git a/lib/pinc_compiler/compiler.ml b/lib/pinc_compiler/compiler.ml index 060479c..5f8e0fd 100644 --- a/lib/pinc_compiler/compiler.ml +++ b/lib/pinc_compiler/compiler.ml @@ -341,7 +341,6 @@ and compile_binary_expression t ~left ~op ~right = let t = compile_expr t left in let t = compile_expr t right in emit t Pinc_Bytecode.Instruction.I_Index - | Pinc_Types.Operators.Binary.FUNCTION_CALL -> raise_notrace TODO | Pinc_Types.Operators.Binary.PIPE -> raise_notrace TODO | Pinc_Types.Operators.Binary.ARRAY_ADD -> raise_notrace TODO | Pinc_Types.Operators.Binary.MERGE -> raise_notrace TODO diff --git a/lib/pinc_format/Formatter.ml b/lib/pinc_format/Formatter.ml index c2a2003..43d5928 100644 --- a/lib/pinc_format/Formatter.ml +++ b/lib/pinc_format/Formatter.ml @@ -246,7 +246,6 @@ and format_binary_expression left op right = | Parsetree.Operators.Binary.OR -> l ^^ space ^^ twice bar ^^ space ^^ r | Parsetree.Operators.Binary.DOT_ACCESS -> l ^^ dot ^^ r | Parsetree.Operators.Binary.BRACKET_ACCESS -> l ^^ brackets r - | Parsetree.Operators.Binary.FUNCTION_CALL -> l ^^ parens r | Parsetree.Operators.Binary.PIPE -> l ^^ break 1 ^^ ifflat empty (twice space) ^^ bar ^^ rangle ^^ space ^^ r | Parsetree.Operators.Binary.ARRAY_ADD -> l ^^ space ^^ langle ^^ minus ^^ space ^^ r diff --git a/lib/pinc_interpreter/Interpreter.ml b/lib/pinc_interpreter/Interpreter.ml index df5eb9c..e814e70 100644 --- a/lib/pinc_interpreter/Interpreter.ml +++ b/lib/pinc_interpreter/Interpreter.ml @@ -215,8 +215,6 @@ and eval_expression ~state expression = eval_range ~state ~inclusive:false left right | Ast.BinaryExpression (left, Ast.Operators.Binary.INCLUSIVE_RANGE, right) -> eval_range ~state ~inclusive:true left right - | Ast.BinaryExpression (left, Ast.Operators.Binary.FUNCTION_CALL, right) -> - eval_function_call ~state ~arguments:[ right ] left | Ast.BinaryExpression (left, Ast.Operators.Binary.PIPE, right) -> eval_binary_pipe ~state left right diff --git a/lib/pinc_parser/Parser.ml b/lib/pinc_parser/Parser.ml index 6b1151f..e8aaa43 100644 --- a/lib/pinc_parser/Parser.ml +++ b/lib/pinc_parser/Parser.ml @@ -773,7 +773,6 @@ module Rules = struct | Token.ARROW_LEFT -> Some Operators.Binary.ARRAY_ADD | Token.ATAT -> Some Operators.Binary.MERGE | Token.LEFT_BRACK -> Some Operators.Binary.BRACKET_ACCESS - | Token.LEFT_PAREN -> Some Operators.Binary.FUNCTION_CALL | Token.DOTDOT -> Some Operators.Binary.RANGE | Token.DOTDOTDOT -> Some Operators.Binary.INCLUSIVE_RANGE | Token.PIPE -> Some Operators.Binary.PIPE @@ -781,83 +780,97 @@ module Rules = struct and parse_expression ?(prio = -999) t = let rec loop ~prio ~left t = - if t.token.typ = Token.SEMICOLON then - left - else ( - match - parse_binary_operator t - with - | None -> left - | Some operator -> - let precedence = Operators.Binary.get_precedence operator in - if precedence < prio then - left - else ( - let expression_annotations = parse_annotations t in - let expression_start = t.token.location in - next t; - (* + let expression_start = t.token.location in + match t.token.typ with + | Token.SEMICOLON -> left + | Token.LEFT_PAREN -> + let expression_annotations = parse_annotations t in + next t; + let arguments = + t |> Helpers.separated_list ~sep:Token.COMMA ~fn:parse_expression + in + let expression_desc = + Parsetree.P_FunctionCall { function_definition = left; arguments } + in + expect Token.RIGHT_PAREN t; + let expression_end = t.token.location in + let expression_loc = Location.merge ~s:expression_start ~e:expression_end () in + let left = + Parsetree. + { + expression_desc; + expression_loc; + expression_annotations; + expression_parenthesized = false; + } + in + loop ~left ~prio t + | _ -> ( + match parse_binary_operator t with + | None -> left + | Some operator -> + let precedence = Operators.Binary.get_precedence operator in + if precedence < prio then + left + else ( + let expression_annotations = parse_annotations t in + next t; + (* NOTE: The new_prio was moved out of the | operator branch, so it now also updates the prio on function calls. Tests are still passing, but if there are precendence errors with functions in the future, this is probably the reason. - 2026-06-26 *) - let new_prio = - match Operators.Binary.get_associativity operator with - | Assoc_Left -> precedence + 1 - | Assoc_Right -> precedence - in - let expression_desc = - match operator with - | Operators.Binary.FUNCTION_CALL -> - let arguments = - t |> Helpers.separated_list ~sep:Token.COMMA ~fn:parse_expression - in - Parsetree.P_FunctionCall { function_definition = left; arguments } - | Operators.Binary.DOT_ACCESS -> - let id, loc = Helpers.expect_identifier ~typ:`Lower t in - let expr = - Parsetree. - { - expression_loc = loc; - expression_desc = Parsetree.P_LowercaseIdentifierExpression id; - expression_parenthesized = false; - expression_annotations = []; - } - in - Parsetree.P_BinaryExpression (left, operator, expr) - | operator -> ( - match parse_expression ~prio:new_prio t with - | None -> - Diagnostics.raise_error - t.token.location - ("Expected expression on right hand side of `" - ^ Operators.Binary.to_string operator - ^ "`") - | Some right -> Parsetree.P_BinaryExpression (left, operator, right)) - in - let expect_close token = expect token t in - let () = - match operator with - | Operators.Binary.FUNCTION_CALL -> expect_close Token.RIGHT_PAREN - | Operators.Binary.BRACKET_ACCESS -> expect_close Token.RIGHT_BRACK - | _ -> () - in - let expression_end = t.token.location in - let expression_loc = - Location.merge ~s:expression_start ~e:expression_end () - in - let left = - Parsetree. - { - expression_desc; - expression_loc; - expression_annotations; - expression_parenthesized = false; - } - in - loop ~left ~prio t)) + let new_prio = + match Operators.Binary.get_associativity operator with + | Assoc_Left -> precedence + 1 + | Assoc_Right -> precedence + in + let expression_desc = + match operator with + | Operators.Binary.DOT_ACCESS -> + let id, loc = Helpers.expect_identifier ~typ:`Lower t in + let expr = + Parsetree. + { + expression_loc = loc; + expression_desc = Parsetree.P_LowercaseIdentifierExpression id; + expression_parenthesized = false; + expression_annotations = []; + } + in + Parsetree.P_BinaryExpression (left, operator, expr) + | operator -> ( + match parse_expression ~prio:new_prio t with + | None -> + Diagnostics.raise_error + t.token.location + ("Expected expression on right hand side of `" + ^ Operators.Binary.to_string operator + ^ "`") + | Some right -> Parsetree.P_BinaryExpression (left, operator, right) + ) + in + let () = + match operator with + | Operators.Binary.BRACKET_ACCESS -> expect Token.RIGHT_BRACK t + | _ -> () + in + let expression_end = t.token.location in + let expression_loc = + Location.merge ~s:expression_start ~e:expression_end () + in + let left = + Parsetree. + { + expression_desc; + expression_loc; + expression_annotations; + expression_parenthesized = false; + } + in + loop ~left ~prio t)) in let* left = parse_expression_part t in Some (loop ~prio ~left t) diff --git a/lib/pinc_types/Operators.ml b/lib/pinc_types/Operators.ml index 9678361..66fd2e2 100644 --- a/lib/pinc_types/Operators.ml +++ b/lib/pinc_types/Operators.ml @@ -23,7 +23,6 @@ module Binary = struct | OR | DOT_ACCESS | BRACKET_ACCESS - | FUNCTION_CALL | PIPE | ARRAY_ADD | MERGE @@ -32,7 +31,6 @@ module Binary = struct let get_precedence = function | DOT_ACCESS -> 110 - | FUNCTION_CALL -> 100 | POW -> 70 | MODULO | TIMES | DIV -> 60 | PLUS | MINUS -> 50 @@ -69,7 +67,6 @@ module Binary = struct | ARRAY_ADD -> "<-" | MERGE -> "@@" | BRACKET_ACCESS -> "[" - | FUNCTION_CALL -> "(" | RANGE -> ".." | INCLUSIVE_RANGE -> "..." | PIPE -> "|>" diff --git a/lib/pinc_vm/vm.ml b/lib/pinc_vm/vm.ml index efaff93..bbdf62a 100644 --- a/lib/pinc_vm/vm.ml +++ b/lib/pinc_vm/vm.ml @@ -62,7 +62,6 @@ let rec execute_binary_operation t op = | Pinc_Types.Operators.Binary.CONCAT -> execute_binary_concat l r | Pinc_Types.Operators.Binary.DOT_ACCESS -> execute_binary_dot_access l r | Pinc_Types.Operators.Binary.BRACKET_ACCESS -> execute_binary_bracket_access l r - | Pinc_Types.Operators.Binary.FUNCTION_CALL -> raise_notrace TODO | Pinc_Types.Operators.Binary.PIPE -> raise_notrace TODO | Pinc_Types.Operators.Binary.ARRAY_ADD -> raise_notrace TODO | Pinc_Types.Operators.Binary.MERGE -> raise_notrace TODO