grammar,compiler: Add Support For Fixed-Width Integers
Distinguishing between signed and unsigned fixed-width integer types must still be done. Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -18,8 +18,8 @@
|
||||
import Common
|
||||
import Foundation
|
||||
import Macros
|
||||
import P4Runtime
|
||||
import P4Lang
|
||||
import P4Runtime
|
||||
import SwiftTreeSitter
|
||||
import Testing
|
||||
import TreeSitter
|
||||
@@ -36,13 +36,15 @@ import TreeSitterP4
|
||||
transition reject;
|
||||
}
|
||||
};
|
||||
""";
|
||||
"""
|
||||
|
||||
let err = Program.Compile(simple_parser_declaration)
|
||||
guard case Result.Error(let e) = err else {
|
||||
assert(false, "Expected an error, but had success")
|
||||
}
|
||||
#expect(e.msg().contains("Failed to parse a statement element: Could not parse a P4 type from \(invalid_type_name)"))
|
||||
#expect(
|
||||
e.msg().contains(
|
||||
"Failed to parse a statement element: Could not parse a P4 type from \(invalid_type_name)"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +64,7 @@ import TreeSitterP4
|
||||
#RequireErrorResult(
|
||||
Error(
|
||||
withMessage:
|
||||
"{112, 16}: Failed to parse a statement element: {112, 8}: Cannot assign value with type Boolean to identifier where_to with type String"
|
||||
"{112, 16}: Failed to parse a statement element: {112, 8}: Cannot assign value with type Boolean to identifier where_to with type String"
|
||||
),
|
||||
Program.Compile(simple_parser_declaration)))
|
||||
}
|
||||
@@ -83,7 +85,7 @@ import TreeSitterP4
|
||||
#RequireErrorResult(
|
||||
Error(
|
||||
withMessage:
|
||||
"{114, 22}: Failed to parse a statement element: {114, 8}: Cannot assign value with type String to identifier where_to with type Boolean"
|
||||
"{114, 22}: Failed to parse a statement element: {114, 8}: Cannot assign value with type String to identifier where_to with type Boolean"
|
||||
),
|
||||
Program.Compile(simple_parser_declaration)))
|
||||
}
|
||||
@@ -128,6 +130,90 @@ import TreeSitterP4
|
||||
Program.Compile(simple_parser_declaration)))
|
||||
}
|
||||
|
||||
@Test func test_invalid_type_in_declaration3() async throws {
|
||||
let simple_parser_declaration = """
|
||||
parser main_parser() {
|
||||
state start {
|
||||
int<5> specific_width_int = 5;
|
||||
int unspecific_width_int = specific_width_int;
|
||||
transition reject;
|
||||
}
|
||||
};
|
||||
"""
|
||||
|
||||
let error = try! #UseErrorResult(Program.Compile(simple_parser_declaration))
|
||||
|
||||
#expect(
|
||||
error.msg().contains(
|
||||
"Cannot initialize specific_width_int (with type Int (width: Width(5))) from expression with type Int (width: Infinite)"
|
||||
))
|
||||
}
|
||||
|
||||
@Test func test_valid_specific_width_int_type_in_declaration() async throws {
|
||||
let simple_parser_declaration = """
|
||||
parser main_parser() {
|
||||
state start {
|
||||
int<5> specific_width_int = 5w5;
|
||||
int<5> unspecific_width_int = specific_width_int;
|
||||
transition reject;
|
||||
}
|
||||
};
|
||||
"""
|
||||
|
||||
#expect(#RequireOkResult(Program.Compile(simple_parser_declaration)))
|
||||
}
|
||||
|
||||
@Test func test_expression_in_declaration_initializer_specific_width_int_type() async throws {
|
||||
let simple_parser_declaration = """
|
||||
parser main_parser() {
|
||||
state start {
|
||||
int<5> specific_width_int = 5w5;
|
||||
bool where_to = 5w5 == specific_width_int == true;
|
||||
transition select (where_to) {
|
||||
true: accept;
|
||||
false: reject;
|
||||
};
|
||||
}
|
||||
};
|
||||
"""
|
||||
|
||||
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())
|
||||
|
||||
// 5w5 == specific_width_int == true
|
||||
// true == true
|
||||
// true
|
||||
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
|
||||
}
|
||||
|
||||
@Test func test_expression_in_declaration_initializer_specific_width_int_type2() async throws {
|
||||
let simple_parser_declaration = """
|
||||
parser main_parser() {
|
||||
state start {
|
||||
int<5> specific_width_int = 5w5;
|
||||
bool where_to = 5w6 == specific_width_int == false;
|
||||
transition select (where_to) {
|
||||
true: accept;
|
||||
false: reject;
|
||||
};
|
||||
}
|
||||
};
|
||||
"""
|
||||
|
||||
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())
|
||||
|
||||
// 5w6 == specific_width_int == false
|
||||
// false == false
|
||||
// true
|
||||
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
|
||||
}
|
||||
|
||||
|
||||
@Test func test_expression_in_declaration_initializer() async throws {
|
||||
let simple_parser_declaration = """
|
||||
parser main_parser() {
|
||||
@@ -141,7 +227,8 @@ import TreeSitterP4
|
||||
};
|
||||
"""
|
||||
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
|
||||
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
|
||||
let runtime = try #UseOkResult(
|
||||
P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
|
||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||
|
||||
// 5 == 5 == true
|
||||
@@ -163,7 +250,8 @@ import TreeSitterP4
|
||||
};
|
||||
"""
|
||||
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
|
||||
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
|
||||
let runtime = try #UseOkResult(
|
||||
P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
|
||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||
|
||||
// 5 == 5 == true
|
||||
@@ -185,7 +273,8 @@ import TreeSitterP4
|
||||
};
|
||||
"""
|
||||
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
|
||||
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
|
||||
let runtime = try #UseOkResult(
|
||||
P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
|
||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||
|
||||
// 6 == 5 == true
|
||||
@@ -207,7 +296,8 @@ import TreeSitterP4
|
||||
};
|
||||
"""
|
||||
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
|
||||
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
|
||||
let runtime = try #UseOkResult(
|
||||
P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
|
||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||
|
||||
// 6 == 5 == false
|
||||
@@ -229,7 +319,8 @@ import TreeSitterP4
|
||||
};
|
||||
"""
|
||||
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
|
||||
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
|
||||
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.
|
||||
@@ -253,29 +344,32 @@ import TreeSitterP4
|
||||
};
|
||||
"""
|
||||
var test_types = VarTypeScopes().enter()
|
||||
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
|
||||
test_types = test_types.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
|
||||
#expect(
|
||||
#RequireErrorResult(
|
||||
Error(
|
||||
withMessage:
|
||||
"{49, 22}: Failed to parse a statement element: Cannot initialize where_to (with type Boolean) from expression with type Int"
|
||||
"{49, 22}: Failed to parse a statement element: Cannot initialize where_to (with type Boolean) from expression with type Int (width: Infinite)"
|
||||
),
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_types)))
|
||||
}
|
||||
|
||||
@Test func test_simple_compiler_parser_parameters_invalid_types() async throws {
|
||||
let simple_parser_declaration = """
|
||||
parser main_parser(bool pmtr, string smtr, int imtr) {
|
||||
state start {
|
||||
pmtr = 1;
|
||||
transition accept;
|
||||
}
|
||||
};
|
||||
"""
|
||||
parser main_parser(bool pmtr, string smtr, int imtr) {
|
||||
state start {
|
||||
pmtr = 1;
|
||||
transition accept;
|
||||
}
|
||||
};
|
||||
"""
|
||||
#expect(
|
||||
#RequireErrorResult(
|
||||
Error(
|
||||
withMessage: "{85, 9}: Failed to parse a statement element: {85, 4}: Cannot assign value with type Int to identifier pmtr with type Boolean"
|
||||
withMessage:
|
||||
"{85, 9}: Failed to parse a statement element: {85, 4}: Cannot assign value with type Int (width: Infinite) to identifier pmtr with type Boolean"
|
||||
),
|
||||
Program.Compile(simple_parser_declaration)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user