Mako/parser/tests/expressions_test.go
2025-06-11 00:23:50 -05:00

331 lines
8.5 KiB
Go

package parser_test
import (
"testing"
"git.sharkk.net/Sharkk/Mako/parser"
)
func TestPrefixExpressions(t *testing.T) {
tests := []struct {
input string
operator string
value any
}{
{"-5", "-", 5.0},
{"-x", "-", "x"},
{"-true", "-", true},
{"-(1 + 2)", "-", "(1.00 + 2.00)"},
{"not true", "not", true},
{"not false", "not", false},
{"not x", "not", "x"},
{"not (a == b)", "not", "(a == b)"},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
l := parser.NewLexer(tt.input)
p := parser.NewParser(l)
expr := p.ParseExpression(parser.LOWEST)
checkParserErrors(t, p)
prefix, ok := expr.(*parser.PrefixExpression)
if !ok {
t.Fatalf("expected PrefixExpression, got %T", expr)
}
if prefix.Operator != tt.operator {
t.Errorf("expected operator %s, got %s", tt.operator, prefix.Operator)
}
switch expected := tt.value.(type) {
case float64:
testNumberLiteral(t, prefix.Right, expected)
case string:
if expected == "x" {
testIdentifier(t, prefix.Right, expected)
} else {
// It's an expression string
if prefix.Right.String() != expected {
t.Errorf("expected %s, got %s", expected, prefix.Right.String())
}
}
case bool:
testBooleanLiteral(t, prefix.Right, expected)
}
})
}
}
func TestLogicalExpressions(t *testing.T) {
tests := []struct {
input string
leftValue any
operator string
rightValue any
}{
{"true and false", true, "and", false},
{"false or true", false, "or", true},
{"x and y", "x", "and", "y"},
{"a or b", "a", "or", "b"},
{"success and valid", "success", "and", "valid"},
{"error or fallback", "error", "or", "fallback"},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
l := parser.NewLexer(tt.input)
p := parser.NewParser(l)
expr := p.ParseExpression(parser.LOWEST)
checkParserErrors(t, p)
infix, ok := expr.(*parser.InfixExpression)
if !ok {
t.Fatalf("expected InfixExpression, got %T", expr)
}
if infix.Operator != tt.operator {
t.Errorf("expected operator %s, got %s", tt.operator, infix.Operator)
}
// Test left operand
switch leftVal := tt.leftValue.(type) {
case string:
testIdentifier(t, infix.Left, leftVal)
case bool:
testBooleanLiteral(t, infix.Left, leftVal)
}
// Test right operand
switch rightVal := tt.rightValue.(type) {
case string:
testIdentifier(t, infix.Right, rightVal)
case bool:
testBooleanLiteral(t, infix.Right, rightVal)
}
})
}
}
func TestComparisonExpressions(t *testing.T) {
tests := []struct {
input string
leftValue any
operator string
rightValue any
}{
{"1 == 1", 1.0, "==", 1.0},
{"1 != 2", 1.0, "!=", 2.0},
{"x < y", "x", "<", "y"},
{"a > b", "a", ">", "b"},
{"5 <= 10", 5.0, "<=", 10.0},
{"10 >= 5", 10.0, ">=", 5.0},
{"true == false", true, "==", false},
{"nil != x", nil, "!=", "x"},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
l := parser.NewLexer(tt.input)
p := parser.NewParser(l)
expr := p.ParseExpression(parser.LOWEST)
checkParserErrors(t, p)
infix, ok := expr.(*parser.InfixExpression)
if !ok {
t.Fatalf("expected InfixExpression, got %T", expr)
}
if infix.Operator != tt.operator {
t.Errorf("expected operator %s, got %s", tt.operator, infix.Operator)
}
// Test left operand
switch leftVal := tt.leftValue.(type) {
case float64:
testNumberLiteral(t, infix.Left, leftVal)
case string:
testIdentifier(t, infix.Left, leftVal)
case bool:
testBooleanLiteral(t, infix.Left, leftVal)
case nil:
testNilLiteral(t, infix.Left)
}
// Test right operand
switch rightVal := tt.rightValue.(type) {
case float64:
testNumberLiteral(t, infix.Right, rightVal)
case string:
testIdentifier(t, infix.Right, rightVal)
case bool:
testBooleanLiteral(t, infix.Right, rightVal)
case nil:
testNilLiteral(t, infix.Right)
}
})
}
}
func TestInfixExpressions(t *testing.T) {
tests := []struct {
input string
leftValue any
operator string
rightValue any
}{
{"5 + 5", 5.0, "+", 5.0},
{"5 - 5", 5.0, "-", 5.0},
{"5 * 5", 5.0, "*", 5.0},
{"5 / 5", 5.0, "/", 5.0},
{"true + false", true, "+", false},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
l := parser.NewLexer(tt.input)
p := parser.NewParser(l)
expr := p.ParseExpression(parser.LOWEST)
checkParserErrors(t, p)
testInfixExpression(t, expr, tt.leftValue, tt.operator, tt.rightValue)
})
}
}
func TestOperatorPrecedence(t *testing.T) {
tests := []struct {
input string
expected string
}{
// Arithmetic precedence
{"1 + 2 * 3", "(1.00 + (2.00 * 3.00))"},
{"2 * 3 + 4", "((2.00 * 3.00) + 4.00)"},
{"(1 + 2) * 3", "((1.00 + 2.00) * 3.00)"},
{"1 + 2 - 3", "((1.00 + 2.00) - 3.00)"},
{"2 * 3 / 4", "((2.00 * 3.00) / 4.00)"},
// Prefix with arithmetic
{"-1 + 2", "((-1.00) + 2.00)"},
{"-(1 + 2)", "(-(1.00 + 2.00))"},
{"-x * 2", "((-x) * 2.00)"},
{"not true", "(not true)"},
{"not x", "(not x)"},
// Logical operator precedence
{"true or false and true", "(true or (false and true))"},
{"false and true or false", "((false and true) or false)"},
{"a or b and c", "(a or (b and c))"},
{"x and y or z", "((x and y) or z)"},
// Logical with comparison
{"a == b and c != d", "((a == b) and (c != d))"},
{"x < y or z > w", "((x < y) or (z > w))"},
{"not a == b", "((not a) == b)"},
{"not x and y", "((not x) and y)"},
// Logical with arithmetic
{"a + b and c * d", "((a + b) and (c * d))"},
{"x or y + z", "(x or (y + z))"},
{"not a + b", "((not a) + b)"},
// Comparison precedence
{"1 + 2 == 3", "((1.00 + 2.00) == 3.00)"},
{"1 * 2 < 3 + 4", "((1.00 * 2.00) < (3.00 + 4.00))"},
{"a + b != c * d", "((a + b) != (c * d))"},
{"x <= y + z", "(x <= (y + z))"},
{"a * b >= c / d", "((a * b) >= (c / d))"},
// Comparison chaining
{"a == b != c", "((a == b) != c)"},
{"x < y <= z", "((x < y) <= z)"},
// Member access with operators
{"table.key + 1", "(table.key + 1.00)"},
{"arr[0] * 2", "(arr[0.00] * 2.00)"},
{"obj.x == obj.y", "(obj.x == obj.y)"},
{"-table.value", "(-table.value)"},
{"not obj.active", "(not obj.active)"},
{"obj.x and obj.y", "(obj.x and obj.y)"},
// Complex combinations
{"-x + y * z == a.b", "(((-x) + (y * z)) == a.b)"},
{"(a + b) * c <= d[0]", "(((a + b) * c) <= d[0.00])"},
{"not success and attempts > 0 or fallback", "(((not success) and (attempts > 0.00)) or fallback)"},
{"a == b and c > d or e != f", "(((a == b) and (c > d)) or (e != f))"},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
l := parser.NewLexer(tt.input)
p := parser.NewParser(l)
expr := p.ParseExpression(parser.LOWEST)
checkParserErrors(t, p)
if expr.String() != tt.expected {
t.Errorf("expected %s, got %s", tt.expected, expr.String())
}
})
}
}
func TestComplexLogicalExpressions(t *testing.T) {
tests := []struct {
input string
desc string
}{
{"not (a and b)", "negated grouped logical"},
{"a and b or c and d", "mixed logical operators"},
{"success and valid or error and retry", "complex boolean logic"},
{"not failed and attempts < max_attempts", "logical with comparison"},
{"(ready or waiting) and not cancelled", "grouped logical with negation"},
{"x > 0 and y > 0 and z > 0", "multiple and conditions"},
{"error or warning or info", "multiple or conditions"},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
l := parser.NewLexer(tt.input)
p := parser.NewParser(l)
expr := p.ParseExpression(parser.LOWEST)
checkParserErrors(t, p)
if expr == nil {
t.Error("expected non-nil expression")
}
// Verify the expression can be converted to string (basic sanity check)
result := expr.String()
if result == "" {
t.Error("expected non-empty string representation")
}
})
}
}
func TestComplexExpressionsWithComparisons(t *testing.T) {
tests := []struct {
input string
desc string
}{
{"x + 1 == y * 2", "arithmetic comparison"},
{"table.count > arr[0] + 5", "member access comparison"},
{"-value <= max", "prefix comparison"},
{"(a + b) != (c - d)", "grouped comparison"},
{"obj.x < obj.y and obj.y > obj.z", "multiple comparisons with logical"},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
l := parser.NewLexer(tt.input)
p := parser.NewParser(l)
expr := p.ParseExpression(parser.LOWEST)
checkParserErrors(t, p)
if expr == nil {
t.Error("expected non-nil expression")
}
})
}
}