From cc66b02bf8cd1eb28a912c96065d65a9a57ee34a Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 5 Jul 2024 17:11:30 +0200 Subject: [PATCH] Added more tests --- src/build/ast/Parse.go | 4 ++-- src/build/core/ExpressionToRegister.go | 5 +++++ src/build/errors/CompileErrors.go | 2 +- tests/errors/InvalidInstructionExpression.q | 3 +++ tests/errors/{MissingAssignValue.q => MissingOperand.q} | 0 tests/errors/MissingOperand2.q | 3 +++ tests/errors_test.go | 4 +++- 7 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 tests/errors/InvalidInstructionExpression.q rename tests/errors/{MissingAssignValue.q => MissingOperand.q} (100%) create mode 100644 tests/errors/MissingOperand2.q diff --git a/src/build/ast/Parse.go b/src/build/ast/Parse.go index 674f121..760302e 100644 --- a/src/build/ast/Parse.go +++ b/src/build/ast/Parse.go @@ -61,7 +61,7 @@ func toASTNode(tokens token.List) (Node, error) { switch { case IsVariableDefinition(expr): if len(expr.Children) < 2 { - return nil, errors.New(errors.MissingAssignValue, nil, expr.Token.End()) + return nil, errors.New(errors.MissingOperand, nil, expr.Token.End()) } name := expr.Children[0].Token @@ -70,7 +70,7 @@ func toASTNode(tokens token.List) (Node, error) { case IsAssignment(expr): if len(expr.Children) < 2 { - return nil, errors.New(errors.MissingAssignValue, nil, expr.Token.End()) + return nil, errors.New(errors.MissingOperand, nil, expr.Token.End()) } name := expr.Children[0].Token diff --git a/src/build/core/ExpressionToRegister.go b/src/build/core/ExpressionToRegister.go index fade8b8..2f6cf6d 100644 --- a/src/build/core/ExpressionToRegister.go +++ b/src/build/core/ExpressionToRegister.go @@ -4,6 +4,7 @@ import ( "git.akyoto.dev/cli/q/src/build/asm" "git.akyoto.dev/cli/q/src/build/ast" "git.akyoto.dev/cli/q/src/build/cpu" + "git.akyoto.dev/cli/q/src/build/errors" "git.akyoto.dev/cli/q/src/build/expression" ) @@ -23,6 +24,10 @@ func (f *Function) ExpressionToRegister(node *expression.Expression, register cp return err } + if len(node.Children) < 2 { + return errors.New(errors.MissingOperand, f.File, node.Token.End()) + } + left := node.Children[0] right := node.Children[1] final := register diff --git a/src/build/errors/CompileErrors.go b/src/build/errors/CompileErrors.go index 13d5948..886d1f8 100644 --- a/src/build/errors/CompileErrors.go +++ b/src/build/errors/CompileErrors.go @@ -3,7 +3,7 @@ package errors var ( InvalidStatement = &Base{"Invalid statement"} InvalidExpression = &Base{"Invalid expression"} - MissingAssignValue = &Base{"Missing assignment value"} + MissingOperand = &Base{"Missing operand"} MissingMainFunction = &Base{"Missing main function"} NotImplemented = &Base{"Not implemented"} ) diff --git a/tests/errors/InvalidInstructionExpression.q b/tests/errors/InvalidInstructionExpression.q new file mode 100644 index 0000000..c90293f --- /dev/null +++ b/tests/errors/InvalidInstructionExpression.q @@ -0,0 +1,3 @@ +main() { + 2+3 +} \ No newline at end of file diff --git a/tests/errors/MissingAssignValue.q b/tests/errors/MissingOperand.q similarity index 100% rename from tests/errors/MissingAssignValue.q rename to tests/errors/MissingOperand.q diff --git a/tests/errors/MissingOperand2.q b/tests/errors/MissingOperand2.q new file mode 100644 index 0000000..9a030f4 --- /dev/null +++ b/tests/errors/MissingOperand2.q @@ -0,0 +1,3 @@ +main() { + syscall(2+) +} \ No newline at end of file diff --git a/tests/errors_test.go b/tests/errors_test.go index 6299be8..c3d2a11 100644 --- a/tests/errors_test.go +++ b/tests/errors_test.go @@ -19,17 +19,19 @@ var errs = []struct { {"ExpectedFunctionParameters.q", errors.ExpectedFunctionParameters}, {"InvalidInstructionIdentifier.q", &errors.InvalidInstruction{Instruction: "abc"}}, {"InvalidInstructionNumber.q", &errors.InvalidInstruction{Instruction: "123"}}, + {"InvalidInstructionExpression.q", &errors.InvalidInstruction{Instruction: "+"}}, {"InvalidExpression.q", errors.InvalidExpression}, {"InvalidOperator.q", &errors.InvalidOperator{Operator: "+++"}}, {"InvalidCharacter.q", &errors.InvalidCharacter{Character: "@"}}, {"InvalidCharacter2.q", &errors.InvalidCharacter{Character: "@"}}, {"InvalidCharacter3.q", &errors.InvalidCharacter{Character: "@"}}, - {"MissingAssignValue.q", errors.MissingAssignValue}, {"MissingBlockEnd.q", errors.MissingBlockEnd}, {"MissingBlockStart.q", errors.MissingBlockStart}, {"MissingGroupEnd.q", errors.MissingGroupEnd}, {"MissingGroupStart.q", errors.MissingGroupStart}, {"MissingMainFunction.q", errors.MissingMainFunction}, + {"MissingOperand.q", errors.MissingOperand}, + {"MissingOperand2.q", errors.MissingOperand}, {"VariableAlreadyExists.q", &errors.VariableAlreadyExists{Name: "x"}}, {"UnknownIdentifier.q", &errors.UnknownIdentifier{Name: "x"}}, {"UnknownIdentifier2.q", &errors.UnknownIdentifier{Name: "x"}},