compiler, runtime, common, documentation: Refactor Type System
The type system (and the value system) now include attributes for each type (things like direction, const-ness). Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -40,13 +40,13 @@ import TreeSitterP4
|
||||
};
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4TypeAttributed.Attributeless(P4Array(withValueType: P4Int())))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Int()))))
|
||||
var test_values = VarValueScopes().enter()
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
withValue: P4ArrayValue(withType: P4Int(), withValue: [
|
||||
P4IntValue(withValue: 1), P4IntValue(withValue: 2), P4IntValue(withValue: 3),
|
||||
]))
|
||||
withValue: P4Value(P4ArrayValue(withType: P4Type(P4Int()), withValue: [
|
||||
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))
|
||||
])))
|
||||
let program = try #UseOkResult(
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
||||
@@ -68,7 +68,7 @@ import TreeSitterP4
|
||||
};
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4TypeAttributed.Attributeless(P4Int()))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Int()))
|
||||
#expect(
|
||||
#RequireErrorResult(
|
||||
Error(
|
||||
@@ -91,13 +91,14 @@ import TreeSitterP4
|
||||
};
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4TypeAttributed.Attributeless(P4Array(withValueType: P4Int())))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Int()))))
|
||||
var test_values = VarValueScopes().enter()
|
||||
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
withValue: P4ArrayValue(withType: P4Int(), withValue: [
|
||||
P4IntValue(withValue: 1), P4IntValue(withValue: 2), P4IntValue(withValue: 3),
|
||||
]))
|
||||
withValue: P4Value(P4ArrayValue(withType: P4Type(P4Int()), withValue: [
|
||||
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))
|
||||
])))
|
||||
let program = try #UseOkResult(
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
||||
@@ -118,13 +119,13 @@ import TreeSitterP4
|
||||
};
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4TypeAttributed.Attributeless(P4Array(withValueType: P4Int())))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Int()))))
|
||||
var test_values = VarValueScopes().enter()
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
withValue: P4ArrayValue(withType: P4Int(), withValue: [
|
||||
P4IntValue(withValue: 1), P4IntValue(withValue: 2), P4IntValue(withValue: 3),
|
||||
]))
|
||||
withValue: P4Value(P4ArrayValue(withType: P4Type(P4Int()), withValue: [
|
||||
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3)),
|
||||
])))
|
||||
let program = try #UseOkResult(
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
||||
@@ -145,13 +146,13 @@ import TreeSitterP4
|
||||
};
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4TypeAttributed.Attributeless(P4Array(withValueType: P4Int())))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Int()))))
|
||||
var test_values = VarValueScopes().enter()
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
withValue: P4ArrayValue(withType: P4Int(), withValue: [
|
||||
P4IntValue(withValue: 1), P4IntValue(withValue: 2), P4IntValue(withValue: 3),
|
||||
]))
|
||||
withValue: P4Value(P4ArrayValue(withType: P4Type(P4Int()), withValue: [
|
||||
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3)),
|
||||
])))
|
||||
let program = try #UseOkResult(
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
||||
@@ -173,16 +174,16 @@ import TreeSitterP4
|
||||
};
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4TypeAttributed.Attributeless(P4Array(withValueType: P4Array(withValueType: P4Int()))))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Array(withValueType: P4Type(P4Int()))))))
|
||||
var test_values = VarValueScopes().enter()
|
||||
|
||||
let nested = P4ArrayValue(
|
||||
withType: P4Int(),
|
||||
withValue: [P4IntValue(withValue: 5), P4IntValue(withValue: 2), P4IntValue(withValue: 3)])
|
||||
let nested = P4Value(P4ArrayValue(
|
||||
withType: P4Type(P4Int()),
|
||||
withValue: [P4Value(P4IntValue(withValue: 5)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))]))
|
||||
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
withValue: P4ArrayValue(withType: P4Array(withValueType: P4Int()), withValue: [nested]))
|
||||
withValue: P4Value(P4ArrayValue(withType: P4Type(P4Array(withValueType: P4Type(P4Int()))), withValue: [nested])))
|
||||
|
||||
let program = try #UseOkResult(
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||
@@ -206,13 +207,13 @@ import TreeSitterP4
|
||||
};
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4TypeAttributed.Attributeless(P4Array(withValueType: P4Int())))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Int()))))
|
||||
var test_values = VarValueScopes().enter()
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
withValue: P4ArrayValue(withType: P4Int(), withValue: [
|
||||
P4IntValue(withValue: 1), P4IntValue(withValue: 2), P4IntValue(withValue: 3),
|
||||
]))
|
||||
withValue: P4Value(P4ArrayValue(withType: P4Type(P4Int()), withValue: [
|
||||
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3)),
|
||||
])))
|
||||
let program = try #UseOkResult(
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
||||
@@ -235,16 +236,16 @@ import TreeSitterP4
|
||||
};
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4TypeAttributed.Attributeless(P4Array(withValueType: P4Array(withValueType: P4Int()))))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Array(withValueType: P4Type(P4Int()))))))
|
||||
var test_values = VarValueScopes().enter()
|
||||
|
||||
let nested = P4ArrayValue(
|
||||
withType: P4Int(),
|
||||
withValue: [P4IntValue(withValue: 1), P4IntValue(withValue: 2), P4IntValue(withValue: 3)])
|
||||
let nested = P4Value(P4ArrayValue(
|
||||
withType: P4Type(P4Int()),
|
||||
withValue: [P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))]))
|
||||
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
withValue: P4ArrayValue(withType: P4Array(withValueType: P4Int()), withValue: [nested]))
|
||||
withValue: P4Value(P4ArrayValue(withType: P4Type(P4Array(withValueType: P4Type(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: P4Boolean()),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Type(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: P4Boolean()),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Type(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: P4Boolean()),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Type(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: P4Boolean()),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Type(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: P4Boolean()),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
||||
])
|
||||
var test_types = TypeTypeScopes().enter()
|
||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||
|
||||
@@ -39,8 +39,8 @@ import P4Lang
|
||||
}
|
||||
};
|
||||
"""
|
||||
let x = { (tipe: P4TypeAttributed) -> Bool in
|
||||
switch tipe.type {
|
||||
let x = { (tipe: P4Type) -> Bool in
|
||||
switch tipe.dataType() {
|
||||
case let c as Control: c.name == "simple"
|
||||
default: false
|
||||
}
|
||||
@@ -73,8 +73,8 @@ import P4Lang
|
||||
};
|
||||
"""
|
||||
|
||||
let filter = { (tipe: P4TypeAttributed) -> Bool in
|
||||
switch tipe.type {
|
||||
let filter = { (tipe: P4Type) -> Bool in
|
||||
switch tipe.dataType() {
|
||||
case let c as Control: c.name == "simple" || c.name == "complex"
|
||||
default: false
|
||||
}
|
||||
|
||||
@@ -175,7 +175,7 @@ import TreeSitterP4
|
||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program))
|
||||
|
||||
let args = ArgumentList([
|
||||
Argument(P4BooleanValue(withValue: false), atIndex: 1), Argument(P4StringValue(withValue: "Testing"), atIndex: 2), Argument(P4IntValue(withValue: 5), atIndex: 3),
|
||||
Argument(P4Value(P4BooleanValue(withValue: false)), atIndex: 1), Argument(P4Value(P4StringValue(withValue: "Testing")), atIndex: 2), Argument(P4Value(P4IntValue(withValue: 5)), atIndex: 3),
|
||||
])
|
||||
let (state_result, _) = try! #UseOkResult(runtime.run(withArguments: args))
|
||||
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
|
||||
@@ -196,7 +196,7 @@ import TreeSitterP4
|
||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program))
|
||||
|
||||
let args = ArgumentList([
|
||||
Argument(P4BooleanValue(withValue: false), atIndex: 1), Argument(P4StringValue(withValue: "Testing"), atIndex: 2), Argument(P4IntValue(withValue: 5), atIndex: 3),
|
||||
Argument(P4Value(P4BooleanValue(withValue: false)), atIndex: 1), Argument(P4Value(P4StringValue(withValue: "Testing")), atIndex: 2), Argument(P4Value(P4IntValue(withValue: 5)), atIndex: 3),
|
||||
])
|
||||
let (state_result, _) = try! #UseOkResult(runtime.run(withArguments: args))
|
||||
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
|
||||
|
||||
@@ -118,7 +118,7 @@ import P4Lang
|
||||
#expect(parameters.parameters.count == 1)
|
||||
|
||||
#expect(parameters.parameters[0].name == Identifier(name: "pmtr"))
|
||||
#expect(parameters.parameters[0].type.eq(rhs: P4Boolean()))
|
||||
#expect(parameters.parameters[0].type.dataType().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.eq(rhs: P4Boolean()))
|
||||
#expect(parameters.parameters[0].type.dataType().eq(rhs: P4Boolean()))
|
||||
|
||||
#expect(parameters.parameters[1].name == Identifier(name: "smtr"))
|
||||
#expect(parameters.parameters[1].type.eq(rhs: P4String()))
|
||||
#expect(parameters.parameters[1].type.dataType().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.eq(rhs: P4Boolean()))
|
||||
#expect(parameters.parameters[0].type.dataType().eq(rhs: P4Boolean()))
|
||||
|
||||
#expect(parameters.parameters[1].name == Identifier(name: "smtr"))
|
||||
#expect(parameters.parameters[1].type.eq(rhs: P4String()))
|
||||
#expect(parameters.parameters[1].type.dataType().eq(rhs: P4String()))
|
||||
|
||||
#expect(parameters.parameters[2].name == Identifier(name: "imtr"))
|
||||
#expect(parameters.parameters[2].type.eq(rhs: P4Int()))
|
||||
#expect(parameters.parameters[2].type.dataType().eq(rhs: P4Int()))
|
||||
}
|
||||
|
||||
@Test func test_simple_compiler_parser_use_parameters() async throws {
|
||||
|
||||
@@ -96,7 +96,7 @@ import TreeSitterP4
|
||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program))
|
||||
|
||||
let args = ArgumentList([
|
||||
Argument(P4BooleanValue(withValue: true), atIndex: 1), Argument(P4StringValue(withValue: "Testing"), atIndex: 2), Argument(P4IntValue(withValue: 5), atIndex: 3),
|
||||
Argument(P4Value(P4BooleanValue(withValue: true)), atIndex: 1), Argument(P4Value(P4StringValue(withValue: "Testing")), atIndex: 2), Argument(P4Value(P4IntValue(withValue: 5)), atIndex: 3),
|
||||
])
|
||||
let (state_result, _) = try! #UseOkResult(runtime.run(withArguments: args))
|
||||
// We should be in the accept state.
|
||||
@@ -118,7 +118,7 @@ import TreeSitterP4
|
||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program))
|
||||
|
||||
let args = ArgumentList([
|
||||
Argument(P4BooleanValue(withValue: true), atIndex: 1), Argument(P4BooleanValue(withValue: false), atIndex: 2), Argument(P4IntValue(withValue: 5), atIndex: 3),
|
||||
Argument(P4Value(P4BooleanValue(withValue: true)), atIndex: 1), Argument(P4Value(P4BooleanValue(withValue: false)), atIndex: 2), Argument(P4Value(P4IntValue(withValue: 5)), atIndex: 3),
|
||||
])
|
||||
|
||||
#expect(
|
||||
@@ -142,7 +142,7 @@ import TreeSitterP4
|
||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program))
|
||||
|
||||
let args = ArgumentList([
|
||||
Argument(P4IntValue(withValue: 5), atIndex: 1), Argument(P4StringValue(withValue: "Testing"), atIndex: 2), Argument(P4IntValue(withValue: 5), atIndex: 3),
|
||||
Argument(P4Value(P4IntValue(withValue: 5)), atIndex: 1), Argument(P4Value(P4StringValue(withValue: "Testing")), atIndex: 2), Argument(P4Value(P4IntValue(withValue: 5)), atIndex: 3),
|
||||
])
|
||||
|
||||
#expect(
|
||||
@@ -164,7 +164,7 @@ import TreeSitterP4
|
||||
"""
|
||||
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
|
||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program))
|
||||
let args = ArgumentList([Argument(P4BooleanValue(withValue: true), atIndex: 0)])
|
||||
let args = ArgumentList([Argument(P4Value(P4BooleanValue(withValue: true)), atIndex: 0)])
|
||||
|
||||
#expect(
|
||||
#RequireErrorResult<(ParserState, ProgramExecution)>(
|
||||
|
||||
@@ -28,39 +28,39 @@ import TreeSitterP4
|
||||
|
||||
@Test func test_scope() async throws {
|
||||
let s = VarTypeScope()
|
||||
let s2 = s.declare(identifier: Identifier(name: "first"), withValue: P4TypeAttributed.Attributeless(P4Int()))
|
||||
let s2 = s.declare(identifier: Identifier(name: "first"), withValue: P4Type(P4Int()))
|
||||
let found_first = try! #require(s2.lookup(identifier: Identifier(name: "first")))
|
||||
|
||||
#expect(found_first.type.eq(rhs: P4Int()))
|
||||
#expect(found_first.dataType().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: P4TypeAttributed.Attributeless(P4Int()))
|
||||
ss = ss.declare(identifier: Identifier(name: "first"), withValue: P4Type(P4Int()))
|
||||
ss = ss.enter()
|
||||
ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4TypeAttributed.Attributeless(P4Boolean()))
|
||||
ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4Type(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.type.eq(rhs: P4Int()))
|
||||
#expect(found_second.type.eq(rhs: P4Boolean()))
|
||||
#expect(found_first.dataType().eq(rhs: P4Int()))
|
||||
#expect(found_second.dataType().eq(rhs: P4Boolean()))
|
||||
}
|
||||
|
||||
@Test func test_scope_set() async throws {
|
||||
var ss = VarTypeScopes().enter()
|
||||
let id = Identifier(name: "first")
|
||||
let id_type = P4TypeAttributed.Attributeless(P4Int())
|
||||
let id_type = P4Type(P4Int())
|
||||
|
||||
ss = ss.declare(identifier: id, withValue: id_type)
|
||||
ss = ss.enter()
|
||||
ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4TypeAttributed.Attributeless(P4Boolean()))
|
||||
ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4Type(P4Boolean()))
|
||||
// Change the value of `first`.
|
||||
ss = ss.set(identifier: id, withValue: P4TypeAttributed.Attributeless(P4String()))
|
||||
ss = ss.set(identifier: id, withValue: P4Type(P4String()))
|
||||
|
||||
// Verify the change!
|
||||
let found = try! #UseOkResult(ss.lookup(identifier: id))
|
||||
|
||||
#expect(found.type.eq(rhs: P4String()))
|
||||
#expect(found.dataType().eq(rhs: P4String()))
|
||||
}
|
||||
|
||||
@@ -41,19 +41,19 @@ import TreeSitterP4
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
let fields = P4StructFields([
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
||||
])
|
||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4TypeAttributed.Attributeless(struct_type))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(struct_type))
|
||||
|
||||
var test_values = VarValueScopes().enter()
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ts"),
|
||||
withValue: P4StructValue(withType: struct_type, andInitializers: [
|
||||
P4BooleanValue(withValue: true),
|
||||
P4IntValue(withValue: 5),
|
||||
]))
|
||||
withValue: P4Value(P4StructValue(withType: struct_type, andInitializers: [
|
||||
P4Value(P4BooleanValue(withValue: true)),
|
||||
P4Value(P4IntValue(withValue: 5)),
|
||||
])))
|
||||
|
||||
let program = try #UseOkResult(
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||
@@ -78,8 +78,8 @@ import TreeSitterP4
|
||||
"""
|
||||
var test_types = TypeTypeScopes().enter()
|
||||
let fields = P4StructFields([
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Type(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: P4Boolean()),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
||||
])
|
||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||
test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
|
||||
@@ -135,19 +135,19 @@ import TreeSitterP4
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
let fields = P4StructFields([
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
||||
])
|
||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4TypeAttributed.Attributeless(struct_type))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(struct_type))
|
||||
|
||||
var test_values = VarValueScopes().enter()
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ts"),
|
||||
withValue: P4StructValue(withType: struct_type, andInitializers: [
|
||||
P4BooleanValue(withValue: false),
|
||||
P4IntValue(withValue: 5),
|
||||
]))
|
||||
withValue: P4Value(P4StructValue(withType: struct_type, andInitializers: [
|
||||
P4Value(P4BooleanValue(withValue: false)),
|
||||
P4Value(P4IntValue(withValue: 5)),
|
||||
])))
|
||||
|
||||
let program = try #UseOkResult(
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||
@@ -170,19 +170,19 @@ import TreeSitterP4
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
let fields = P4StructFields([
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
||||
])
|
||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4TypeAttributed.Attributeless(struct_type))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(struct_type))
|
||||
|
||||
var test_values = VarValueScopes().enter()
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ts"),
|
||||
withValue: P4StructValue(withType: struct_type, andInitializers: [
|
||||
P4BooleanValue(withValue: true),
|
||||
P4IntValue(withValue: 5),
|
||||
]))
|
||||
withValue: P4Value(P4StructValue(withType: struct_type, andInitializers: [
|
||||
P4Value(P4BooleanValue(withValue: true)),
|
||||
P4Value(P4IntValue(withValue: 5)),
|
||||
])))
|
||||
|
||||
let program = try #UseOkResult(
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||
@@ -204,19 +204,19 @@ import TreeSitterP4
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
let fields = P4StructFields([
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
||||
])
|
||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4TypeAttributed.Attributeless(struct_type))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(struct_type))
|
||||
|
||||
var test_values = VarValueScopes().enter()
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ts"),
|
||||
withValue: P4StructValue(withType: struct_type, andInitializers: [
|
||||
P4BooleanValue(withValue: true),
|
||||
P4IntValue(withValue: 8),
|
||||
]))
|
||||
withValue: P4Value(P4StructValue(withType: struct_type, andInitializers: [
|
||||
P4Value(P4BooleanValue(withValue: true)),
|
||||
P4Value(P4IntValue(withValue: 8)),
|
||||
])))
|
||||
|
||||
let program = try #UseOkResult(
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||
@@ -240,30 +240,30 @@ import TreeSitterP4
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
|
||||
let ty_fields = P4StructFields([
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
||||
])
|
||||
let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields)
|
||||
|
||||
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: ty_struct_type)])
|
||||
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4Type(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: P4TypeAttributed.Attributeless(ts_struct_type))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(ts_struct_type))
|
||||
|
||||
var test_values = VarValueScopes().enter()
|
||||
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ts"),
|
||||
withValue: P4StructValue(
|
||||
withValue: P4Value(P4StructValue(
|
||||
withType: ts_struct_type,
|
||||
andInitializers: [
|
||||
P4StructValue(
|
||||
P4Value(P4StructValue(
|
||||
withType: ty_struct_type,
|
||||
andInitializers: [
|
||||
P4BooleanValue(withValue: true),
|
||||
P4IntValue(withValue: 5),
|
||||
])
|
||||
]))
|
||||
P4Value(P4BooleanValue(withValue: true)),
|
||||
P4Value(P4IntValue(withValue: 5)),
|
||||
]))
|
||||
])))
|
||||
let program = try #UseOkResult(
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
||||
@@ -286,19 +286,19 @@ import TreeSitterP4
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
let fields = P4StructFields([
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
||||
])
|
||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4TypeAttributed.Attributeless(struct_type))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(struct_type))
|
||||
|
||||
var test_values = VarValueScopes().enter()
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ts"),
|
||||
withValue: P4StructValue(withType: struct_type, andInitializers: [
|
||||
P4BooleanValue(withValue: false),
|
||||
P4IntValue(withValue: 5),
|
||||
]))
|
||||
withValue: P4Value(P4StructValue(withType: struct_type, andInitializers: [
|
||||
P4Value(P4BooleanValue(withValue: false)),
|
||||
P4Value(P4IntValue(withValue: 5)),
|
||||
])))
|
||||
|
||||
let program = try #UseOkResult(
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||
@@ -318,11 +318,11 @@ import TreeSitterP4
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
let fields = P4StructFields([
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
||||
])
|
||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4TypeAttributed.Attributeless(struct_type))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(struct_type))
|
||||
|
||||
#expect(
|
||||
#RequireErrorResult(
|
||||
@@ -349,30 +349,30 @@ import TreeSitterP4
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
|
||||
let ty_fields = P4StructFields([
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
||||
])
|
||||
let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields)
|
||||
|
||||
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: ty_struct_type)])
|
||||
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4Type(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: P4TypeAttributed.Attributeless(ts_struct_type))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(ts_struct_type))
|
||||
|
||||
var test_values = VarValueScopes().enter()
|
||||
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ts"),
|
||||
withValue: P4StructValue(
|
||||
withValue: P4Value(P4StructValue(
|
||||
withType: ts_struct_type,
|
||||
andInitializers: [
|
||||
P4StructValue(
|
||||
P4Value(P4StructValue(
|
||||
withType: ty_struct_type,
|
||||
andInitializers: [
|
||||
P4BooleanValue(withValue: true),
|
||||
P4IntValue(withValue: 7),
|
||||
])
|
||||
]))
|
||||
P4Value(P4BooleanValue(withValue: true)),
|
||||
P4Value(P4IntValue(withValue: 7)),
|
||||
]))
|
||||
])))
|
||||
let program = try #UseOkResult(
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
||||
@@ -397,30 +397,30 @@ import TreeSitterP4
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
|
||||
let ty_fields = P4StructFields([
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
||||
])
|
||||
let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields)
|
||||
|
||||
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: ty_struct_type)])
|
||||
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4Type(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: P4TypeAttributed.Attributeless(ts_struct_type))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(ts_struct_type))
|
||||
|
||||
var test_values = VarValueScopes().enter()
|
||||
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ts"),
|
||||
withValue: P4StructValue(
|
||||
withValue: P4Value(P4StructValue(
|
||||
withType: ts_struct_type,
|
||||
andInitializers: [
|
||||
P4StructValue(
|
||||
P4Value(P4StructValue(
|
||||
withType: ty_struct_type,
|
||||
andInitializers: [
|
||||
P4BooleanValue(withValue: true),
|
||||
P4IntValue(withValue: 7),
|
||||
])
|
||||
]))
|
||||
P4Value(P4BooleanValue(withValue: true)),
|
||||
P4Value(P4IntValue(withValue: 7)),
|
||||
]))
|
||||
])))
|
||||
let program = try #UseOkResult(
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
||||
@@ -444,15 +444,15 @@ import TreeSitterP4
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
|
||||
let ty_fields = P4StructFields([
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
||||
])
|
||||
let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields)
|
||||
|
||||
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: ty_struct_type)])
|
||||
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4Type(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: P4TypeAttributed.Attributeless(ts_struct_type))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(ts_struct_type))
|
||||
|
||||
#expect(
|
||||
#RequireErrorResult(
|
||||
|
||||
@@ -26,8 +26,8 @@ import TreeSitterP4
|
||||
|
||||
@Test func test_simple_struct() async throws {
|
||||
let fields = P4StructFields([
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
||||
])
|
||||
|
||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||
|
||||
@@ -253,7 +253,7 @@ import TreeSitterP4
|
||||
};
|
||||
"""
|
||||
var test_types = VarTypeScopes().enter()
|
||||
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4TypeAttributed.Attributeless(P4Array(withValueType: P4Int())))
|
||||
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Int()))))
|
||||
#expect(
|
||||
#RequireErrorResult(
|
||||
Error(
|
||||
|
||||
Reference in New Issue
Block a user