compiler: Type Check All Binary Operators

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-05-22 20:42:35 -04:00
parent bc51b4e280
commit 97a672bd6d
4 changed files with 57 additions and 111 deletions
+5
View File
@@ -495,6 +495,11 @@ extension BinaryOperatorExpression: CompilableExpression {
),
]
if !left_hand_side.type().eq(right_hand_side.type()) {
return Result.Error(
Error(withMessage: "Types of values used with binary expression are not the same"))
}
guard let selected_evaluator = evaluators[binary_operator_expression_node.nodeType!] else {
return Result.Error(
Error(withMessage: "No evaluator for \(binary_operator_expression_node.nodeType!)"))
@@ -321,7 +321,7 @@ import TreeSitterP4
let simple = """
parser main_parser() {
state start {
transition select (10 == (true + 5)) {
transition select (true + false) {
true: accept;
false: reject;
};
@@ -333,7 +333,7 @@ import TreeSitterP4
#RequireErrorResult(
Error(
withMessage:
"{72, 16}: Could not parse transition select expression selector expression: Mathematical operation on operands with non-int type is not allowed"
"{72, 12}: Could not parse transition select expression selector expression: Mathematical operation on operands with non-int type is not allowed"
),
Program.Compile(simple)))
}
@@ -342,7 +342,7 @@ import TreeSitterP4
let simple = """
parser main_parser() {
state start {
transition select (10 == (5 + false)) {
transition select (5 + false) {
true: accept;
false: reject;
};
@@ -354,28 +354,7 @@ import TreeSitterP4
#RequireErrorResult(
Error(
withMessage:
"{72, 17}: Could not parse transition select expression selector expression: Mathematical operation on operands with non-int type is not allowed"
),
Program.Compile(simple)))
}
@Test func test_simple_parser_binary_operator_add_non_integer3() async throws {
let simple = """
parser main_parser() {
state start {
transition select (10 == (false + false)) {
true: accept;
false: reject;
};
}
};
"""
#expect(
#RequireErrorResult(
Error(
withMessage:
"{72, 21}: Could not parse transition select expression selector expression: Mathematical operation on operands with non-int type is not allowed"
"{72, 9}: Could not parse transition select expression selector expression: Types of values used with binary expression are not the same"
),
Program.Compile(simple)))
}
@@ -405,7 +384,7 @@ import TreeSitterP4
let simple = """
parser main_parser() {
state start {
transition select (10 == (true - 5)) {
transition select (true - false) {
true: accept;
false: reject;
};
@@ -417,7 +396,7 @@ import TreeSitterP4
#RequireErrorResult(
Error(
withMessage:
"{72, 16}: Could not parse transition select expression selector expression: Mathematical operation on operands with non-int type is not allowed"
"{72, 12}: Could not parse transition select expression selector expression: Mathematical operation on operands with non-int type is not allowed"
),
Program.Compile(simple)))
}
@@ -426,7 +405,7 @@ import TreeSitterP4
let simple = """
parser main_parser() {
state start {
transition select (10 == (5 - false)) {
transition select (5 - false) {
true: accept;
false: reject;
};
@@ -438,33 +417,11 @@ import TreeSitterP4
#RequireErrorResult(
Error(
withMessage:
"{72, 17}: Could not parse transition select expression selector expression: Mathematical operation on operands with non-int type is not allowed"
"{72, 9}: Could not parse transition select expression selector expression: Types of values used with binary expression are not the same"
),
Program.Compile(simple)))
}
@Test func test_simple_parser_binary_operator_subtract_non_integer3() async throws {
let simple = """
parser main_parser() {
state start {
transition select (10 == (false - false)) {
true: accept;
false: reject;
};
}
};
"""
#expect(
#RequireErrorResult(
Error(
withMessage:
"{72, 21}: Could not parse transition select expression selector expression: Mathematical operation on operands with non-int type is not allowed"
),
Program.Compile(simple)))
}
// Multiply Integers
@Test func test_simple_parser_binary_operator_multiply_integer() async throws {
@@ -490,7 +447,7 @@ import TreeSitterP4
let simple = """
parser main_parser() {
state start {
transition select (10 == (true * 5)) {
transition select (true * false) {
true: accept;
false: reject;
};
@@ -502,7 +459,7 @@ import TreeSitterP4
#RequireErrorResult(
Error(
withMessage:
"{72, 16}: Could not parse transition select expression selector expression: Mathematical operation on operands with non-int type is not allowed"
"{72, 12}: Could not parse transition select expression selector expression: Mathematical operation on operands with non-int type is not allowed"
),
Program.Compile(simple)))
}
@@ -511,7 +468,7 @@ import TreeSitterP4
let simple = """
parser main_parser() {
state start {
transition select (10 == (5 * false)) {
transition select (5 * false) {
true: accept;
false: reject;
};
@@ -523,28 +480,7 @@ import TreeSitterP4
#RequireErrorResult(
Error(
withMessage:
"{72, 17}: Could not parse transition select expression selector expression: Mathematical operation on operands with non-int type is not allowed"
),
Program.Compile(simple)))
}
@Test func test_simple_parser_binary_operator_multiply_non_integer3() async throws {
let simple = """
parser main_parser() {
state start {
transition select (10 == (false * false)) {
true: accept;
false: reject;
};
}
};
"""
#expect(
#RequireErrorResult(
Error(
withMessage:
"{72, 21}: Could not parse transition select expression selector expression: Mathematical operation on operands with non-int type is not allowed"
"{72, 9}: Could not parse transition select expression selector expression: Types of values used with binary expression are not the same"
),
Program.Compile(simple)))
}
@@ -574,7 +510,7 @@ import TreeSitterP4
let simple = """
parser main_parser() {
state start {
transition select (10 == (true / 5)) {
transition select (true / false) {
true: accept;
false: reject;
};
@@ -586,7 +522,7 @@ import TreeSitterP4
#RequireErrorResult(
Error(
withMessage:
"{72, 16}: Could not parse transition select expression selector expression: Mathematical operation on operands with non-int type is not allowed"
"{72, 12}: Could not parse transition select expression selector expression: Mathematical operation on operands with non-int type is not allowed"
),
Program.Compile(simple)))
}
@@ -595,7 +531,7 @@ import TreeSitterP4
let simple = """
parser main_parser() {
state start {
transition select (10 == (5 / false)) {
transition select (5 / false) {
true: accept;
false: reject;
};
@@ -607,29 +543,7 @@ import TreeSitterP4
#RequireErrorResult(
Error(
withMessage:
"{72, 17}: Could not parse transition select expression selector expression: Mathematical operation on operands with non-int type is not allowed"
"{72, 9}: Could not parse transition select expression selector expression: Types of values used with binary expression are not the same"
),
Program.Compile(simple)))
}
@Test func test_simple_parser_binary_operator_divide_non_integer3() async throws {
let simple = """
parser main_parser() {
state start {
transition select (10 == (false / false)) {
true: accept;
false: reject;
};
}
};
"""
#expect(
#RequireErrorResult(
Error(
withMessage:
"{72, 21}: Could not parse transition select expression selector expression: Mathematical operation on operands with non-int type is not allowed"
),
Program.Compile(simple)))
}
+29
View File
@@ -463,3 +463,32 @@ import TreeSitterP4
)
}
@Test func test_field_read_equality_invalid_type() async throws {
let simple_parser_declaration = """
parser main_parser() {
state start {
transition select (ts.yesno == "testing") {
true: accept;
false: reject;
};
}
};
"""
var test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
])
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type))
#expect(
#RequireErrorResult(
Error(
withMessage: "{68, 21}: Could not parse transition select expression selector expression: Types of values used with binary expression are not the same"
),
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
)
}
+7 -9
View File
@@ -318,17 +318,15 @@ import TreeSitterP4
}
};
"""
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
// TODO: This test should throw an error.
#expect(
#RequireErrorResult(
Error(
withMessage:
"{49, 35}: Failed to parse a statement element: Types of values used with binary expression are not the same"
),
Program.Compile(simple_parser_declaration)))
// false == 5 == true
// false == true
// false
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
}
@Test func test_expression_in_declaration_initializer_invalid_types2() async throws {