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
@@ -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)))
}