compiler, runtime: Refactor P4Type to P4QualifiedType

Also, refer to the different pieces of the qualified type as
qualifiers and not attributes.

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-05-04 07:29:51 -04:00
parent a84e778e79
commit 7c660b2b0c
30 changed files with 264 additions and 264 deletions
+17 -17
View File
@@ -40,11 +40,11 @@ import TreeSitterP4
};
"""
var test_declarations = VarTypeScopes().enter()
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Int()))))
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ta"),
withValue: P4Value(P4ArrayValue(withType: P4Type(P4Int()), withValue: [
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))
])))
let program = try #UseOkResult(
@@ -68,7 +68,7 @@ import TreeSitterP4
};
"""
var test_declarations = VarTypeScopes().enter()
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Int()))
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Int()))
#expect(
#RequireErrorResult(
Error(
@@ -91,12 +91,12 @@ import TreeSitterP4
};
"""
var test_declarations = VarTypeScopes().enter()
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Int()))))
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ta"),
withValue: P4Value(P4ArrayValue(withType: P4Type(P4Int()), withValue: [
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))
])))
let program = try #UseOkResult(
@@ -119,11 +119,11 @@ import TreeSitterP4
};
"""
var test_declarations = VarTypeScopes().enter()
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Int()))))
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ta"),
withValue: P4Value(P4ArrayValue(withType: P4Type(P4Int()), withValue: [
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3)),
])))
let program = try #UseOkResult(
@@ -146,11 +146,11 @@ import TreeSitterP4
};
"""
var test_declarations = VarTypeScopes().enter()
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Int()))))
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ta"),
withValue: P4Value(P4ArrayValue(withType: P4Type(P4Int()), withValue: [
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3)),
])))
let program = try #UseOkResult(
@@ -174,16 +174,16 @@ import TreeSitterP4
};
"""
var test_declarations = VarTypeScopes().enter()
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Array(withValueType: P4Type(P4Int()))))))
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))))
var test_values = VarValueScopes().enter()
let nested = P4Value(P4ArrayValue(
withType: P4Type(P4Int()),
withType: P4QualifiedType(P4Int()),
withValue: [P4Value(P4IntValue(withValue: 5)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))]))
test_values = test_values.declare(
identifier: Identifier(name: "ta"),
withValue: P4Value(P4ArrayValue(withType: P4Type(P4Array(withValueType: P4Type(P4Int()))), withValue: [nested])))
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))), withValue: [nested])))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
@@ -207,11 +207,11 @@ import TreeSitterP4
};
"""
var test_declarations = VarTypeScopes().enter()
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Int()))))
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ta"),
withValue: P4Value(P4ArrayValue(withType: P4Type(P4Int()), withValue: [
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3)),
])))
let program = try #UseOkResult(
@@ -236,16 +236,16 @@ import TreeSitterP4
};
"""
var test_declarations = VarTypeScopes().enter()
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Array(withValueType: P4Type(P4Int()))))))
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))))
var test_values = VarValueScopes().enter()
let nested = P4Value(P4ArrayValue(
withType: P4Type(P4Int()),
withType: P4QualifiedType(P4Int()),
withValue: [P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))]))
test_values = test_values.declare(
identifier: Identifier(name: "ta"),
withValue: P4Value(P4ArrayValue(withType: P4Type(P4Array(withValueType: P4Type(P4Int()))), withValue: [nested])))
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))), withValue: [nested])))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
@@ -42,8 +42,8 @@ import TreeSitterP4
"""
let test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
])
var test_types = TypeTypeScopes().enter()
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
@@ -75,8 +75,8 @@ import TreeSitterP4
"""
let test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
])
var test_types = TypeTypeScopes().enter()
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
@@ -109,8 +109,8 @@ import TreeSitterP4
"""
let test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
])
var test_types = TypeTypeScopes().enter()
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
@@ -146,8 +146,8 @@ import TreeSitterP4
"""
let test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
])
var test_types = TypeTypeScopes().enter()
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
@@ -183,8 +183,8 @@ import TreeSitterP4
"""
let test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
])
var test_types = TypeTypeScopes().enter()
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
+6 -6
View File
@@ -41,8 +41,8 @@ import P4Lang
}
};
"""
let x = { (tipe: P4Type) -> Bool in
switch tipe.dataType() {
let x = { (tipe: P4QualifiedType) -> Bool in
switch tipe.baseType() {
case let c as Control: c.name == "simple"
default: false
}
@@ -79,8 +79,8 @@ import P4Lang
};
"""
let filter = { (tipe: P4Type) -> Bool in
switch tipe.dataType() {
let filter = { (tipe: P4QualifiedType) -> Bool in
switch tipe.baseType() {
case let c as Control: c.name == "simple" || c.name == "complex"
default: false
}
@@ -106,8 +106,8 @@ import P4Lang
}
};
"""
let x = { (tipe: P4Type) -> Bool in
switch tipe.dataType() {
let x = { (tipe: P4QualifiedType) -> Bool in
switch tipe.baseType() {
case let c as Control: c.name == "simple"
default: false
}
+35 -35
View File
@@ -53,24 +53,24 @@ import TreeSitterP4
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
// Pull the control out of the compiled program.
let controls = program.InstancesWithTypes() { (tipe: P4Type) -> Bool in
switch tipe.dataType() {
let controls = program.InstancesWithTypes() { (tipe: P4QualifiedType) -> Bool in
switch tipe.baseType() {
case let c as Control: c.name == "simple"
default: false
}
}
var control = ((controls[0].dataType() as P4DataType) as! Control)
var control = ((controls[0].baseType() as P4DataType) as! Control)
// Add entries to the table.
control = control.updateTable(
addEntry: (
P4Value(P4BooleanValue(withValue: true)),
TypedIdentifier(name: "a", withType: P4Type(Action()))
TypedIdentifier(name: "a", withType: P4QualifiedType(Action()))
)
).updateTable(
addEntry: (
P4Value(P4BooleanValue(withValue: false)),
TypedIdentifier(name: "b", withType: P4Type(Action()))
TypedIdentifier(name: "b", withType: P4QualifiedType(Action()))
))
// Set a variable in the global scope for the inout first parameter.
@@ -79,14 +79,14 @@ import TreeSitterP4
identifier: Identifier(name: "result_arg"),
withValue: P4Value(
P4IntValue(withValue: 0),
P4Type(P4Int())))
P4QualifiedType(P4Int())))
let runtime = try #UseOkResult(
P4Runtime.Runtime<P4TableHitMissValue, Control>.create(control: control, withGlobalValues: global_values))
let (hit_miss, updated_execution) = try #UseOkResult(runtime.run(
withArguments: ArgumentList([
Argument(TypedIdentifier(name: "result_arg", withType: P4Type(P4Int())), atIndex: 0),
Argument(TypedIdentifier(name: "result_arg", withType: P4QualifiedType(P4Int())), atIndex: 0),
Argument(P4Value(P4BooleanValue(withValue: true)), atIndex: 1),
])))
@@ -124,24 +124,24 @@ import TreeSitterP4
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
// Pull the control out of the compiled program.
let controls = program.InstancesWithTypes() { (tipe: P4Type) -> Bool in
switch tipe.dataType() {
let controls = program.InstancesWithTypes() { (tipe: P4QualifiedType) -> Bool in
switch tipe.baseType() {
case let c as Control: c.name == "simple"
default: false
}
}
var control = ((controls[0].dataType() as P4DataType) as! Control)
var control = ((controls[0].baseType() as P4DataType) as! Control)
// Add entries to the table.
control = control.updateTable(
addEntry: (
P4Value(P4BooleanValue(withValue: true)),
TypedIdentifier(name: "a", withType: P4Type(Action()))
TypedIdentifier(name: "a", withType: P4QualifiedType(Action()))
)
).updateTable(
addEntry: (
P4Value(P4BooleanValue(withValue: false)),
TypedIdentifier(name: "b", withType: P4Type(Action()))
TypedIdentifier(name: "b", withType: P4QualifiedType(Action()))
))
// Set a variable in the global scope for the inout first parameter.
@@ -150,14 +150,14 @@ import TreeSitterP4
identifier: Identifier(name: "result_arg"),
withValue: P4Value(
P4IntValue(withValue: 0),
P4Type(P4Int())))
P4QualifiedType(P4Int())))
let runtime = try #UseOkResult(
P4Runtime.Runtime<P4TableHitMissValue, Control>.create(control: control, withGlobalValues: global_values))
let (hit_miss, updated_execution) = try #UseOkResult(runtime.run(
withArguments: ArgumentList([
Argument(TypedIdentifier(name: "result_arg", withType: P4Type(P4Int())), atIndex: 0),
Argument(TypedIdentifier(name: "result_arg", withType: P4QualifiedType(P4Int())), atIndex: 0),
Argument(P4Value(P4BooleanValue(withValue: false)), atIndex: 1),
])))
@@ -195,24 +195,24 @@ import TreeSitterP4
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
// Pull the control out of the compiled program.
let controls = program.InstancesWithTypes() { (tipe: P4Type) -> Bool in
switch tipe.dataType() {
let controls = program.InstancesWithTypes() { (tipe: P4QualifiedType) -> Bool in
switch tipe.baseType() {
case let c as Control: c.name == "simple"
default: false
}
}
var control = ((controls[0].dataType() as P4DataType) as! Control)
var control = ((controls[0].baseType() as P4DataType) as! Control)
// Add entries to the table.
control = control.updateTable(
addEntry: (
P4Value(P4IntValue(withValue: 5)),
TypedIdentifier(name: "a", withType: P4Type(Action()))
TypedIdentifier(name: "a", withType: P4QualifiedType(Action()))
)
).updateTable(
addEntry: (
P4Value(P4IntValue(withValue: 2)),
TypedIdentifier(name: "b", withType: P4Type(Action()))
TypedIdentifier(name: "b", withType: P4QualifiedType(Action()))
))
// Set a variable in the global scope for the inout first parameter.
@@ -221,14 +221,14 @@ import TreeSitterP4
identifier: Identifier(name: "result_arg"),
withValue: P4Value(
P4IntValue(withValue: 0),
P4Type(P4Int())))
P4QualifiedType(P4Int())))
let runtime = try #UseOkResult(
P4Runtime.Runtime<P4TableHitMissValue, Control>.create(control: control, withGlobalValues: global_values))
let (hit_miss, updated_execution) = try #UseOkResult(runtime.run(
withArguments: ArgumentList([
Argument(TypedIdentifier(name: "result_arg", withType: P4Type(P4Int())), atIndex: 0),
Argument(TypedIdentifier(name: "result_arg", withType: P4QualifiedType(P4Int())), atIndex: 0),
Argument(P4Value(P4IntValue(withValue: 5)), atIndex: 1),
])))
@@ -265,24 +265,24 @@ import TreeSitterP4
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
// Pull the control out of the compiled program.
let controls = program.InstancesWithTypes() { (tipe: P4Type) -> Bool in
switch tipe.dataType() {
let controls = program.InstancesWithTypes() { (tipe: P4QualifiedType) -> Bool in
switch tipe.baseType() {
case let c as Control: c.name == "simple"
default: false
}
}
var control = ((controls[0].dataType() as P4DataType) as! Control)
var control = ((controls[0].baseType() as P4DataType) as! Control)
// Add entries to the table.
control = control.updateTable(
addEntry: (
P4Value(P4IntValue(withValue: 1)),
TypedIdentifier(name: "a", withType: P4Type(Action()))
TypedIdentifier(name: "a", withType: P4QualifiedType(Action()))
)
).updateTable(
addEntry: (
P4Value(P4IntValue(withValue: 2)),
TypedIdentifier(name: "b", withType: P4Type(Action()))
TypedIdentifier(name: "b", withType: P4QualifiedType(Action()))
))
// Set a variable in the global scope for the inout first parameter.
@@ -291,14 +291,14 @@ import TreeSitterP4
identifier: Identifier(name: "result_arg"),
withValue: P4Value(
P4IntValue(withValue: 0),
P4Type(P4Int())))
P4QualifiedType(P4Int())))
let runtime = try #UseOkResult(
P4Runtime.Runtime<P4TableHitMissValue, Control>.create(control: control, withGlobalValues: global_values))
let (hit_miss, updated_execution) = try #UseOkResult(runtime.run(
withArguments: ArgumentList([
Argument(TypedIdentifier(name: "result_arg", withType: P4Type(P4Int())), atIndex: 0),
Argument(TypedIdentifier(name: "result_arg", withType: P4QualifiedType(P4Int())), atIndex: 0),
Argument(P4Value(P4IntValue(withValue: 3)), atIndex: 1),
])))
@@ -336,24 +336,24 @@ import TreeSitterP4
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
// Pull the control out of the compiled program.
let controls = program.InstancesWithTypes() { (tipe: P4Type) -> Bool in
switch tipe.dataType() {
let controls = program.InstancesWithTypes() { (tipe: P4QualifiedType) -> Bool in
switch tipe.baseType() {
case let c as Control: c.name == "simple"
default: false
}
}
var control = ((controls[0].dataType() as P4DataType) as! Control)
var control = ((controls[0].baseType() as P4DataType) as! Control)
// Add entries to the table.
control = control.updateTable(
addEntry: (
P4Value(P4BooleanValue(withValue: true)),
TypedIdentifier(name: "a", withType: P4Type(Action()))
TypedIdentifier(name: "a", withType: P4QualifiedType(Action()))
)
).updateTable(
addEntry: (
P4Value(P4IntValue(withValue: 5)),
TypedIdentifier(name: "b", withType: P4Type(Action()))
TypedIdentifier(name: "b", withType: P4QualifiedType(Action()))
))
// Set a variable in the global scope for the inout first parameter.
@@ -362,14 +362,14 @@ import TreeSitterP4
identifier: Identifier(name: "result_arg"),
withValue: P4Value(
P4IntValue(withValue: 0),
P4Type(P4Int())))
P4QualifiedType(P4Int())))
let runtime = try #UseOkResult(
P4Runtime.Runtime<P4TableHitMissValue, Control>.create(control: control, withGlobalValues: global_values))
let (hit_miss, updated_execution) = try #UseOkResult(runtime.run(
withArguments: ArgumentList([
Argument(TypedIdentifier(name: "result_arg", withType: P4Type(P4Int())), atIndex: 0),
Argument(TypedIdentifier(name: "result_arg", withType: P4QualifiedType(P4Int())), atIndex: 0),
Argument(P4Value(P4BooleanValue(withValue: false)), atIndex: 1),
Argument(P4Value(P4IntValue(withValue: 5)), atIndex: 2),
])))
+6 -6
View File
@@ -38,10 +38,10 @@ public struct Return5: P4FFI {
return ParameterList()
}
public func type() -> Common.P4Type {
return P4Type(
public func type() -> Common.P4QualifiedType {
return P4QualifiedType(
FunctionDeclaration(
named: Identifier(name: "externally"), ofType: P4Type(P4Int()),
named: Identifier(name: "externally"), ofType: P4QualifiedType(P4Int()),
withParameters: ParameterList(), withBody: .none?))
}
@@ -59,10 +59,10 @@ public struct Return6: P4FFI {
return ParameterList()
}
public func type() -> Common.P4Type {
return P4Type(
public func type() -> Common.P4QualifiedType {
return P4QualifiedType(
FunctionDeclaration(
named: Identifier(name: "externally"), ofType: P4Type(P4Int()),
named: Identifier(name: "externally"), ofType: P4QualifiedType(P4Int()),
withParameters: ParameterList(), withBody: .none?))
}
+6 -6
View File
@@ -118,7 +118,7 @@ import P4Lang
#expect(parameters.parameters.count == 1)
#expect(parameters.parameters[0].name == Identifier(name: "pmtr"))
#expect(parameters.parameters[0].type.dataType().eq(rhs: P4Boolean()))
#expect(parameters.parameters[0].type.baseType().eq(rhs: P4Boolean()))
}
@Test func test_simple_compiler_parser_with_multiple_parameters() async throws {
@@ -138,10 +138,10 @@ import P4Lang
#expect(parameters.parameters.count == 2)
#expect(parameters.parameters[0].name == Identifier(name: "pmtr"))
#expect(parameters.parameters[0].type.dataType().eq(rhs: P4Boolean()))
#expect(parameters.parameters[0].type.baseType().eq(rhs: P4Boolean()))
#expect(parameters.parameters[1].name == Identifier(name: "smtr"))
#expect(parameters.parameters[1].type.dataType().eq(rhs: P4String()))
#expect(parameters.parameters[1].type.baseType().eq(rhs: P4String()))
}
@Test func test_simple_compiler_parser_with_multiple_parameters2() async throws {
@@ -161,13 +161,13 @@ import P4Lang
#expect(parameters.parameters.count == 3)
#expect(parameters.parameters[0].name == Identifier(name: "pmtr"))
#expect(parameters.parameters[0].type.dataType().eq(rhs: P4Boolean()))
#expect(parameters.parameters[0].type.baseType().eq(rhs: P4Boolean()))
#expect(parameters.parameters[1].name == Identifier(name: "smtr"))
#expect(parameters.parameters[1].type.dataType().eq(rhs: P4String()))
#expect(parameters.parameters[1].type.baseType().eq(rhs: P4String()))
#expect(parameters.parameters[2].name == Identifier(name: "imtr"))
#expect(parameters.parameters[2].type.dataType().eq(rhs: P4Int()))
#expect(parameters.parameters[2].type.baseType().eq(rhs: P4Int()))
}
@Test func test_simple_compiler_parser_use_parameters() async throws {
+10 -10
View File
@@ -28,39 +28,39 @@ import TreeSitterP4
@Test func test_scope() async throws {
let s = VarTypeScope()
let s2 = s.declare(identifier: Identifier(name: "first"), withValue: P4Type(P4Int()))
let s2 = s.declare(identifier: Identifier(name: "first"), withValue: P4QualifiedType(P4Int()))
let found_first = try! #require(s2.lookup(identifier: Identifier(name: "first")))
#expect(found_first.dataType().eq(rhs: P4Int()))
#expect(found_first.baseType().eq(rhs: P4Int()))
#expect(s2.count == 1)
}
@Test func test_scope_no_set() async throws {
var ss = VarTypeScopes().enter()
ss = ss.declare(identifier: Identifier(name: "first"), withValue: P4Type(P4Int()))
ss = ss.declare(identifier: Identifier(name: "first"), withValue: P4QualifiedType(P4Int()))
ss = ss.enter()
ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4Type(P4Boolean()))
ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4QualifiedType(P4Boolean()))
let found_first = try! #UseOkResult(ss.lookup(identifier: Identifier(name: "first")))
let found_second = try! #UseOkResult(ss.lookup(identifier: Identifier(name: "second")))
#expect(found_first.dataType().eq(rhs: P4Int()))
#expect(found_second.dataType().eq(rhs: P4Boolean()))
#expect(found_first.baseType().eq(rhs: P4Int()))
#expect(found_second.baseType().eq(rhs: P4Boolean()))
}
@Test func test_scope_set() async throws {
var ss = VarTypeScopes().enter()
let id = Identifier(name: "first")
let id_type = P4Type(P4Int())
let id_type = P4QualifiedType(P4Int())
ss = ss.declare(identifier: id, withValue: id_type)
ss = ss.enter()
ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4Type(P4Boolean()))
ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4QualifiedType(P4Boolean()))
// Change the value of `first`.
ss = ss.set(identifier: id, withValue: P4Type(P4String()))
ss = ss.set(identifier: id, withValue: P4QualifiedType(P4String()))
// Verify the change!
let found = try! #UseOkResult(ss.lookup(identifier: id))
#expect(found.dataType().eq(rhs: P4String()))
#expect(found.baseType().eq(rhs: P4String()))
}
+38 -38
View File
@@ -41,11 +41,11 @@ import TreeSitterP4
"""
var test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
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: P4Type(struct_type))
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
@@ -78,8 +78,8 @@ import TreeSitterP4
"""
var test_types = TypeTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
])
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
@@ -108,8 +108,8 @@ import TreeSitterP4
"""
var test_types = TypeTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
])
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
@@ -135,11 +135,11 @@ import TreeSitterP4
"""
var test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
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: P4Type(struct_type))
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
@@ -170,11 +170,11 @@ import TreeSitterP4
"""
var test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
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: P4Type(struct_type))
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
@@ -204,11 +204,11 @@ import TreeSitterP4
"""
var test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
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: P4Type(struct_type))
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
@@ -240,15 +240,15 @@ import TreeSitterP4
var test_declarations = VarTypeScopes().enter()
let ty_fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
])
let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields)
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4Type(ty_struct_type))])
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4QualifiedType(ty_struct_type))])
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(ts_struct_type))
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(ts_struct_type))
var test_values = VarValueScopes().enter()
@@ -286,11 +286,11 @@ import TreeSitterP4
"""
var test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
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: P4Type(struct_type))
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
@@ -318,11 +318,11 @@ import TreeSitterP4
"""
var test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
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: P4Type(struct_type))
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type))
#expect(
#RequireErrorResult(
@@ -349,15 +349,15 @@ import TreeSitterP4
var test_declarations = VarTypeScopes().enter()
let ty_fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
])
let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields)
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4Type(ty_struct_type))])
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4QualifiedType(ty_struct_type))])
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(ts_struct_type))
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(ts_struct_type))
var test_values = VarValueScopes().enter()
@@ -397,15 +397,15 @@ import TreeSitterP4
var test_declarations = VarTypeScopes().enter()
let ty_fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
])
let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields)
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4Type(ty_struct_type))])
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4QualifiedType(ty_struct_type))])
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(ts_struct_type))
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(ts_struct_type))
var test_values = VarValueScopes().enter()
@@ -444,15 +444,15 @@ import TreeSitterP4
var test_declarations = VarTypeScopes().enter()
let ty_fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
])
let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields)
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4Type(ty_struct_type))])
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4QualifiedType(ty_struct_type))])
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(ts_struct_type))
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(ts_struct_type))
#expect(
#RequireErrorResult(
+2 -2
View File
@@ -26,8 +26,8 @@ import TreeSitterP4
@Test func test_simple_struct() async throws {
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
])
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
+1 -1
View File
@@ -253,7 +253,7 @@ import TreeSitterP4
};
"""
var test_types = VarTypeScopes().enter()
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Int()))))
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
#expect(
#RequireErrorResult(
Error(