compiler, language, runtime: Separate Parser Type From Instances
Continuous Integration / Grammar Tests (push) Successful in 4m2s
Continuous Integration / Library Format Tests (push) Successful in 5m0s
Continuous Integration / Library Tests (push) Successful in 8m1s

In P4, parsers are considered types. Those parsers are instantiated.
The instantiated parsers are values. Previously, gp4 treated a parser
type and a parser value as identical. This PR makes that difference
clear _and_ sets the stage for the future.

TODO: Make the same distinction between control and action types and
values.

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-05-27 05:41:23 -04:00
parent 925f20a13b
commit 61d8f601e8
36 changed files with 1058 additions and 796 deletions
+6
View File
@@ -257,6 +257,12 @@ public typealias VarValueScope = Scope<P4Value>
/// Scopes that resolves variable identifiers to their values.
public typealias VarValueScopes = Scopes<P4Value>
/// A scope that resolves variable identifiers to their values.
public typealias StaticVarValueScope = Scope<(P4QualifiedType, P4Value?)>
/// Scopes that resolves variable identifiers to their values.
public typealias StaticVarValueScopes = Scopes<(P4QualifiedType, P4Value?)>
/// Indicate the control flow result of a particular statement.
public enum ControlFlow {
case Next
+1 -1
View File
@@ -52,7 +52,7 @@ public protocol EvaluatableLValueExpression: EvaluatableExpression {
func set(
to: P4Value, inScopes scopes: VarValueScopes, duringExecution execution: ProgramExecution
) -> Result<(VarValueScopes, P4Value)>
func check(to: EvaluatableExpression, inScopes scopes: VarTypeScopes) -> Result<()>
func check(to: EvaluatableExpression, inScopes scopes: StaticVarValueScopes) -> Result<()>
}
public protocol ProgramExecutionEvaluator {
+5 -5
View File
@@ -46,7 +46,7 @@ public func ConfigureP4Parser() -> Result<SwiftTreeSitter.Parser> {
/// knowledge of an expected type. For instance, when compiling a return statement, the
/// compiler must know the return type of the function to type check.
public struct CompilerContext {
let instances: VarTypeScopes
let instances: StaticVarValueScopes
let types: TypeTypeScopes
let externs: TypeTypeScopes
let ffis: [P4FFI]
@@ -54,7 +54,7 @@ public struct CompilerContext {
let extern_context: Bool
public init() {
instances = VarTypeScopes().enter()
instances = StaticVarValueScopes().enter()
types = TypeTypeScopes().enter()
externs = TypeTypeScopes().enter()
expected_type = .none
@@ -62,7 +62,7 @@ public struct CompilerContext {
ffis = Array()
}
public init(withInstances _instances: VarTypeScopes, withTypes _types: TypeTypeScopes) {
public init(withInstances _instances: StaticVarValueScopes, withTypes _types: TypeTypeScopes) {
instances = _instances
types = _types
externs = TypeTypeScopes().enter()
@@ -72,7 +72,7 @@ public struct CompilerContext {
}
public init(
withInstances _instances: VarTypeScopes, withTypes _types: TypeTypeScopes,
withInstances _instances: StaticVarValueScopes, withTypes _types: TypeTypeScopes,
withExpectation expectation: P4QualifiedType?, withExtern extern: Bool,
withExterns externs: TypeTypeScopes, withFFIs foreigns: [P4FFI]
) {
@@ -90,7 +90,7 @@ public struct CompilerContext {
///
/// - Parameter instances: a ``VarTypeScopes`` with the updated instances for the newly created compiler context.
/// - Returns: A new compiler context based on the current but new instances.
public func update(newInstances instances: VarTypeScopes) -> CompilerContext {
public func update(newInstances instances: StaticVarValueScopes) -> CompilerContext {
return CompilerContext(
withInstances: instances, withTypes: self.types, withExpectation: self.expected_type,
withExtern: self.extern_context, withExterns: self.externs, withFFIs: self.ffis)
+20 -15
View File
@@ -105,7 +105,7 @@ extension FunctionDeclaration: CompilableDeclaration {
var function_scope = context.instances.enter()
for parameter in function_parameters.parameters {
function_scope = function_scope.declare(
identifier: parameter.name, withValue: parameter.type)
identifier: parameter.name, withValue: (parameter.type, .none))
}
let maybe_function_body = BlockStatement.Compile(
@@ -342,7 +342,7 @@ extension P4Lang.Parser: CompilableDeclaration {
for parameter in parameter_list.parameters {
current_context = current_context.update(
newInstances: current_context.instances.declare(
identifier: parameter.name, withValue: parameter.type))
identifier: parameter.name, withValue: (parameter.type, .none)))
}
walker.next()
@@ -380,6 +380,7 @@ extension P4Lang.Parser: CompilableDeclaration {
return .Error(Error(withMessage: "Missing parser states in parser declaration"))
}
/// TODO: Handle extern parsers.
switch Parser.Compile(
withName: parser_name!, withParameters: parameter_list, node: current_node!,
withContext: current_context)
@@ -392,10 +393,11 @@ extension P4Lang.Parser: CompilableDeclaration {
(
parser_declaration,
context.extern_context
? context
: context.update(
newInstances: updated_context.instances.declare(
identifier: parser.name, withValue: parser_declaration.identifier.type))
? updated_context.update(
newExterns: updated_context.externs.declare(
identifier: parser.name, withValue: parser_declaration))
: updated_context.update(
newTypes: updated_context.types.declare(identifier: parser.name, withValue: parser))
))
case Result.Error(let error): return .Error(error)
}
@@ -450,7 +452,7 @@ extension Control: CompilableDeclaration {
var control_scope = local_context.instances.enter()
for parameter in control_parameters.parameters {
control_scope = control_scope.declare(
identifier: parameter.name, withValue: parameter.type)
identifier: parameter.name, withValue: (parameter.type, .none))
}
local_context = local_context.update(newInstances: control_scope)
@@ -543,14 +545,15 @@ extension Control: CompilableDeclaration {
))
}
let control = Control(
named: control_name, withParameters: control_parameters, withTable: tables[0],
withActions: Actions(withActions: actions), withApply: apply)
let declared_control =
Declaration(
TypedIdentifier(
id: control_name,
withType: P4QualifiedType(
Control(
named: control_name, withParameters: control_parameters, withTable: tables[0],
withActions: Actions(withActions: actions), withApply: apply))))
withType: P4QualifiedType(control)))
// Don't forget to add the newly declared Control to the instance that we were given
// (and not the one that we entered to do the parsing of this Control).
@@ -558,10 +561,12 @@ extension Control: CompilableDeclaration {
(
declared_control,
context.extern_context
? context
? context.update(
newTypes: context.externs.declare(
identifier: control_name, withValue: declared_control))
: context.update(
newInstances: context.instances.declare(
identifier: control_name, withValue: declared_control.identifier.type))
newTypes: context.types.declare(
identifier: control_name, withValue: control))
))
}
}
@@ -638,7 +643,7 @@ extension Action: Compilable {
var function_scope = context.instances.enter()
for parameter in action_parameters.parameters {
function_scope = function_scope.declare(
identifier: parameter.name, withValue: parameter.type)
identifier: parameter.name, withValue: (parameter.type, .none))
}
let maybe_action_body = BlockStatement.Compile(
+2 -1
View File
@@ -51,7 +51,8 @@ extension TypedIdentifier: CompilableExpression {
sourceLocation: node.toSourceLocation(), withError: "Cannot find \(node.text!) in scope"))
}
return .Ok(TypedIdentifier(name: node.text!, withType: type))
/// TODO: If there is a value here, then we can make this a compile-time constant!
return .Ok(TypedIdentifier(name: node.text!, withType: type.0))
}
}
+38 -16
View File
@@ -90,7 +90,7 @@ public struct Parser {
static func Compile(
node: Node, forState state_identifier: Common.Identifier,
withStatements stmts: [EvaluatableStatement], withContext context: CompilerContext
) -> Result<(InstantiatedParserState, CompilerContext)> {
) -> Result<(ParserState, CompilerContext)> {
#RequireNodeType<Node, (EvaluatableStatement, CompilerContext)>(
node: node, type: "parserTransitionStatement", nice_type_name: "parser transition statement"
@@ -114,19 +114,40 @@ public struct Parser {
// If the next node is an identifier, we have the simple form ...
if next_node.nodeType == "identifier" {
let maybe_parsed_next_state = Identifier.Compile(
let maybe_parsed_next_state_id = Identifier.Compile(
node: next_node, withContext: context)
if case .Ok(let next_state) = maybe_parsed_next_state {
return .Ok(
(
ParserStateDirectTransition(
name: state_identifier, withStatements: stmts, withNextState: next_state), context
))
if case .Ok(let next_state_id) = maybe_parsed_next_state_id {
if case .Ok(let next_state) = context.instances.lookup(identifier: next_state_id) {
switch next_state {
case (_, .some(let instance)):
return .Ok(
(
ParserStateDirectTransition(
name: state_identifier,
withNextState: instance.dataValue() as! InstantiatedParserState,
withStatements: stmts), context
))
case (_, .none):
return .Ok(
(
ParserStateDirectTransition(
name: state_identifier,
withNextStateIdentifier: next_state_id, withStatements: stmts), context
))
}
} else {
return .Error(
Error(
withMessage:
"\(next_state_id) does not name a parser state in scope"
))
}
} else {
return .Error(
Error(
withMessage:
"Could not parse the next state in a transition statement: \(maybe_parsed_next_state.error()!)"
"Could not parse the next state in a transition statement: \(maybe_parsed_next_state_id.error()!)"
))
}
}
@@ -139,8 +160,9 @@ public struct Parser {
.Ok(
(
ParserStateSelectTransition(
name: state_identifier, withStatements: stmts,
withTransitioniExpression: tse as! SelectExpression), context
name: state_identifier, withTransitionExpression: tse as! SelectExpression,
withStatements: stmts,
), context
))
case .Error(let e): .Error(e)
}
@@ -189,7 +211,7 @@ public struct Parser {
public struct State {
static func Compile(
node: Node, withContext context: CompilerContext
) -> Result<(InstantiatedParserState, CompilerContext)> {
) -> Result<(ParserState, CompilerContext)> {
var walker = Walker(node: node)
var current_node: Node? = .none
@@ -205,7 +227,7 @@ public struct Parser {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(InstantiatedParserState, CompilerContext)>.Error(
or: Result<(ParserState, CompilerContext)>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Missing elements in parser state declaration")))
@@ -223,7 +245,7 @@ public struct Parser {
walker.next()
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(InstantiatedParserState, CompilerContext)>.Error(
or: Result<(ParserState, CompilerContext)>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Missing elements in parser state declaration")))
@@ -239,7 +261,7 @@ public struct Parser {
walker.next()
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(InstantiatedParserState, CompilerContext)>.Error(
or: Result<(ParserState, CompilerContext)>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing body of state declaration")
))
@@ -272,7 +294,7 @@ public struct Parser {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(InstantiatedParserState, CompilerContext)>.Error(
or: Result<(ParserState, CompilerContext)>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Missing transition statement of state declaration")))
+19 -5
View File
@@ -24,18 +24,30 @@ import TreeSitterP4
public struct Program {
public static func Compile(_ source: String) -> Result<P4Lang.Program> {
return Program.Compile(source, withGlobalInstances: .none, withGlobalTypes: .none, withFFIs: [])
// Certain names are always in scope during compilation.
var globals = StaticVarValueScopes().enter()
globals = globals.declare(
identifier: accept.state().getName(),
withValue: (P4QualifiedType(accept.type()), P4Value(accept))
)
.declare(
identifier: reject.state.getName(),
withValue: (P4QualifiedType(reject.type()), P4Value(reject)))
return Program.Compile(
source, withGlobalInstances: globals, withGlobalTypes: .none, withFFIs: [])
}
public static func Compile(
_ source: String, withGlobalInstances globalInstances: VarTypeScopes
_ source: String, withGlobalInstances globalInstances: StaticVarValueScopes
) -> Result<P4Lang.Program> {
return Program.Compile(
source, withGlobalInstances: globalInstances, withGlobalTypes: .none, withFFIs: [])
}
public static func Compile(
_ source: String, withGlobalInstances globalInstances: VarTypeScopes?,
_ source: String, withGlobalInstances globalInstances: StaticVarValueScopes?,
withGlobalTypes globalTypes: TypeTypeScopes?, withFFIs ffis: [P4FFI] = Array()
) -> Result<P4Lang.Program> {
@@ -122,8 +134,10 @@ public struct Program {
// Any of the instances that are in the top-level scope should go into the program!
program.instances = Array(
compilation_context.instances.map { (_, v) in
v
compilation_context.instances.filter { (_, v) in
v.1 != nil
}.map { (_, v) in
v.1!
})
// Any of the types that are in the top-level scope should go into the program!
+1 -1
View File
@@ -255,7 +255,7 @@ extension VariableDeclarationStatement: CompilableStatement {
// Context with updated names to include the newly declared name.
context.update(
newInstances: context.instances.declare(
identifier: parsed_variablename, withValue: declaration_p4_type))
identifier: parsed_variablename, withValue: (declaration_p4_type, .none)))
)
)
}
+229 -193
View File
@@ -41,209 +41,234 @@ public struct ParserAssignmentStatement {
///
/// Note: A P4 Parser State is both a type and a value.
/// This "bare" parser state represents the state more as a type than a value.
public class ParserState: P4Type, P4DataValue, Equatable, CustomStringConvertible {
public class ParserState: P4Type, Equatable, CustomStringConvertible {
let name: Identifier
public let statements: [EvaluatableStatement]
public static func == (lhs: ParserState, rhs: ParserState) -> Bool {
// Two "bare" parser states are always equal.
return true
return lhs.eq(rhs: rhs)
}
public func eq(rhs: any Common.P4Type) -> Bool {
return switch rhs {
case is ParserState: true
default: false
}
}
public func type() -> any Common.P4Type {
return self
}
// Any operation between two "bare" parser states is always true.
public func eq(rhs: any Common.P4DataValue) -> Bool {
return switch rhs {
case is ParserState: true
default: false
}
}
public func lt(rhs: any Common.P4DataValue) -> Bool {
return switch rhs {
case is ParserState: true
default: false
}
}
public func lte(rhs: any Common.P4DataValue) -> Bool {
return switch rhs {
case is ParserState: true
default: false
}
}
public func gt(rhs: any Common.P4DataValue) -> Bool {
return switch rhs {
case is ParserState: true
default: false
}
}
public func gte(rhs: any Common.P4DataValue) -> Bool {
return switch rhs {
case is ParserState: true
case let rrhs as ParserState: self.name == rrhs.name
default: false
}
}
public var description: String {
return "Bare Parser State"
return "Parser State named \(self.name)"
}
public func getName() -> Identifier {
return self.name
}
public func getStatements() -> [EvaluatableStatement] {
return self.statements
}
/// Construct a ParserState
public init() {}
public init(_ name: Identifier, _ statements: [EvaluatableStatement] = Array()) {
self.name = name
self.statements = statements
}
public func def() -> P4DataValue? {
return .none
}
public func instantiate(_ name: Identifier) -> InstantiatedParserState? {
return .none
}
}
/// Instantiated Parser State
///
/// A parser state is both a type and a value. The Instantiated
/// Parser State is the base class for parser states that act more
/// as a value than a type.
public class InstantiatedParserState: ParserState {
public static func == (lhs: InstantiatedParserState, rhs: InstantiatedParserState) -> Bool {
return lhs.state == rhs.state
}
public class _AnyParserState: ParserState {
public override func eq(rhs: any Common.P4Type) -> Bool {
return switch rhs {
case is ParserState: true
default: false
}
}
}
public override func type() -> any Common.P4Type {
return self
nonisolated(unsafe) public let AnyParserState = _AnyParserState(Identifier(name: "AnyParserState"))
public class ParserStateDirectTransition: ParserState {
public let next_state: InstantiatedParserState?
public let next_state_identifier: Identifier?
/// Construct a ParserState
public init(
name: Identifier, withNextState next_state: InstantiatedParserState,
withStatements stmts: [EvaluatableStatement] = Array(),
) {
self.next_state = next_state
self.next_state_identifier = .none
super.init(name, stmts)
}
public override func eq(rhs: any Common.P4DataValue) -> Bool {
public init(
name: Identifier, withNextStateIdentifier next_state_id: Identifier,
withStatements stmts: [EvaluatableStatement] = Array(),
) {
self.next_state = .none
self.next_state_identifier = next_state_id
super.init(name, stmts)
}
public override func instantiate(_ name: Identifier) -> InstantiatedParserState? {
return if let next_state = self.next_state {
ParserStateDirectTransitionValue(name: name, withState: self, withNextState: next_state)
} else {
ParserStateDirectTransitionValue(
name: name, withState: self, withNextStateIdentifier: self.next_state_identifier!)
}
}
}
public class ParserStateNoTransition: ParserState {
/// Construct a ParserState
public init(
name: Identifier, withStatements stmts: [EvaluatableStatement] = Array(),
) {
super.init(name, stmts)
}
public override func instantiate(_ name: Identifier) -> InstantiatedParserState? {
return ParserStateNoTransitionValue(name: name, withState: self)
}
}
public class ParserStateSelectTransition: ParserState {
public let te: SelectExpression
public init(
name: Identifier, withTransitionExpression te: SelectExpression,
withStatements stmts: [EvaluatableStatement] = Array()
) {
self.te = te
super.init(name, stmts)
}
public override func instantiate(_ name: Identifier) -> InstantiatedParserState? {
return ParserStateSelectTransitionValue(name: name, withState: self, withSelectExpression: te)
}
}
/// Instantiated Parser State
///
public class InstantiatedParserState: P4DataValue {
public static func == (lhs: InstantiatedParserState, rhs: InstantiatedParserState) -> Bool {
return lhs.state == rhs.state
}
public func type() -> any Common.P4Type {
return self.state
}
public func eq(rhs: any Common.P4DataValue) -> Bool {
return switch rhs {
case let other as InstantiatedParserState: self.state == other.state
default: false
}
}
public override func lt(rhs: any Common.P4DataValue) -> Bool {
public func lt(rhs: any Common.P4DataValue) -> Bool {
return switch rhs {
case let other as InstantiatedParserState: self.state < other.state
case let other as InstantiatedParserState: self.state.getName() < other.state.getName()
default: false
}
}
public override func lte(rhs: any Common.P4DataValue) -> Bool {
public func lte(rhs: any Common.P4DataValue) -> Bool {
return switch rhs {
case let other as InstantiatedParserState: self.state <= other.state
case let other as InstantiatedParserState: self.state.getName() <= other.state.getName()
default: false
}
}
public override func gt(rhs: any Common.P4DataValue) -> Bool {
public func gt(rhs: any Common.P4DataValue) -> Bool {
return switch rhs {
case let other as InstantiatedParserState: self.state > other.state
case let other as InstantiatedParserState: self.state.getName() > other.state.getName()
default: false
}
}
public override func gte(rhs: any Common.P4DataValue) -> Bool {
public func gte(rhs: any Common.P4DataValue) -> Bool {
return switch rhs {
case let other as InstantiatedParserState: self.state >= other.state
case let other as InstantiatedParserState: self.state.getName() >= other.state.getName()
default: false
}
}
public private(set) var state: Identifier
public private(set) var statements: [EvaluatableStatement]
public private(set) var state: ParserState
public private(set) var name: Identifier
public override var description: String {
return "Name: \(state)"
public var description: String {
return "Instance of state of type \(state) named \(name)"
}
/// Construct a ParserState
public init(
name: Identifier, withStatements stmts: [EvaluatableStatement],
) {
state = name
statements = stmts
}
/// (private) constructor (no transition)
///
/// accept and reject are the only final states and they are constructed internally.
private init(name: Identifier) {
state = name
statements = Array()
}
public override func def() -> any P4DataValue {
return InstantiatedParserState(name: Identifier(name: ""))
public init(_ name: Identifier, _ state: ParserState) {
self.name = name
self.state = state
}
}
public class ParserStateDirectTransition: InstantiatedParserState {
private let next_state: Identifier
public class ParserStateDirectTransitionValue: InstantiatedParserState {
public let next_state: InstantiatedParserState?
public let next_state_identifier: Identifier?
public init(
name: Identifier, withStatements stmts: [EvaluatableStatement],
withNextState next_state: Identifier
name: Identifier, withState state: ParserStateDirectTransition,
withNextState next_state: InstantiatedParserState
) {
self.next_state = next_state
super.init(name: name, withStatements: stmts)
}
public override var description: String {
return "State (Name: \(super.state) (direct transition))"
}
public func get_next_state() -> Identifier {
return self.next_state
}
}
public class ParserStateNoTransition: InstantiatedParserState {
public override init(name: Identifier, withStatements stmts: [any EvaluatableStatement]) {
super.init(name: name, withStatements: stmts)
}
public override var description: String {
return "State (Name: \(super.state) (no transition))"
}
}
public class ParserStateSelectTransition: InstantiatedParserState {
public let selectExpression: SelectExpression
public override var description: String {
return "State (Name: \(super.state) (select transition))"
self.next_state_identifier = .none
super.init(name, state)
}
public init(
name: Identifier, withStatements stmts: [any EvaluatableStatement],
withTransitioniExpression te: SelectExpression
name: Identifier, withState state: ParserStateDirectTransition,
withNextStateIdentifier next_state_id: Identifier
) {
self.selectExpression = te
super.init(name: name, withStatements: stmts)
self.next_state = .none
self.next_state_identifier = next_state_id
super.init(name, state)
}
}
nonisolated(unsafe) public let accept = ParserStateNoTransition(
public class ParserStateNoTransitionValue: InstantiatedParserState {
public init(name: Identifier, withState state: ParserStateNoTransition) {
super.init(name, state)
}
}
public class ParserStateSelectTransitionValue: InstantiatedParserState {
public let te: SelectExpression
public init(
name: Identifier, withState state: ParserStateSelectTransition,
withSelectExpression te: SelectExpression
) {
self.te = te
super.init(name, state)
}
}
nonisolated(unsafe) private let accept_type = ParserStateNoTransition(
name: Identifier(name: "accept"), withStatements: [])
nonisolated(unsafe) public let reject = ParserStateNoTransition(
nonisolated(unsafe) private let reject_type = ParserStateNoTransition(
name: Identifier(name: "reject"), withStatements: [])
nonisolated(unsafe) public let accept = ParserStateNoTransitionValue(
name: Identifier(name: "accept"), withState: accept_type)
nonisolated(unsafe) public let reject = ParserStateNoTransitionValue(
name: Identifier(name: "reject"), withState: reject_type)
public struct ParserStates {
public var states: [InstantiatedParserState] = Array()
public var states: [ParserState] = Array()
public func count() -> Int {
return states.count
@@ -251,71 +276,28 @@ public struct ParserStates {
public func find(withIdentifier id: Identifier) -> ParserState? {
for state in states {
if state.state == id {
if state.getName() == id {
return .some(state)
}
}
return .none
}
public init() {
self.states = Array()
}
private init(withStates states: [InstantiatedParserState]) {
public init(_ states: [ParserState] = Array()) {
self.states = states
}
public func append(state: InstantiatedParserState) -> ParserStates {
public func append(state: ParserState) -> ParserStates {
var new_states = self.states
new_states.append(state)
return ParserStates(withStates: new_states)
return ParserStates(new_states)
}
}
/// A P4 Parser
///
/// Note: A Parser is both a type _and_ a value.
public struct Parser: P4Type, P4DataValue {
public func type() -> any Common.P4Type {
return self
}
public func eq(rhs: any Common.P4Type) -> Bool {
return switch rhs {
case let parser_rhs as Parser: self.name == parser_rhs.name
default: false
}
}
public func lt(rhs: any Common.P4DataValue) -> Bool {
return switch rhs {
case let parser_rhs as Parser: self.name < parser_rhs.name
default: false
}
}
public func lte(rhs: any Common.P4DataValue) -> Bool {
return switch rhs {
case let parser_rhs as Parser: self.name <= parser_rhs.name
default: false
}
}
public func gt(rhs: any Common.P4DataValue) -> Bool {
return switch rhs {
case let parser_rhs as Parser: self.name > parser_rhs.name
default: false
}
}
public func gte(rhs: any Common.P4DataValue) -> Bool {
return switch rhs {
case let parser_rhs as Parser: self.name >= parser_rhs.name
default: false
}
}
/// Note: A Parser is a type
public struct Parser: P4Type {
public var states: ParserStates
public var name: Identifier
@@ -334,19 +316,7 @@ public struct Parser: P4Type, P4DataValue {
}
public func findStartState() -> ParserState? {
for state in states.states {
if state.state == Identifier(name: "start") {
return state
}
}
return .none
}
public func eq(rhs: any P4DataValue) -> Bool {
return switch rhs {
case let other as Parser: self.name == other.name
default: false
}
return self.states.find(withIdentifier: Identifier(name: "start"))
}
public var description: String {
@@ -356,9 +326,75 @@ public struct Parser: P4Type, P4DataValue {
public func def() -> P4DataValue? {
return .none
}
public func eq(rhs: any Common.P4Type) -> Bool {
return switch rhs {
case let parser_rhs as Parser: self.name == parser_rhs.name
default: false
}
}
public func instantiate(_ name: Identifier) -> ParserValue {
return ParserValue(self, name)
}
}
/// Launder a parser state into an instantiated parser state.
public func AsInstantiatedParserState(_ state: ParserState) -> InstantiatedParserState {
return state as! InstantiatedParserState
/// A instance of a P4 Parser
///
public struct ParserValue: P4DataValue {
public func type() -> P4Type {
return self.tipe
}
public func eq(rhs: any Common.P4DataValue) -> Bool {
return switch rhs {
case let rrhs as ParserValue: rrhs.type().eq(rhs: self.type()) && self.name == rrhs.name
default: false
}
}
public func lt(rhs: any Common.P4DataValue) -> Bool {
return switch rhs {
case let rrhs as ParserValue: rrhs.type().eq(rhs: self.type()) && self.name < rrhs.name
default: false
}
}
public func lte(rhs: any Common.P4DataValue) -> Bool {
return switch rhs {
case let rrhs as ParserValue: rrhs.type().eq(rhs: self.type()) && self.name <= rrhs.name
default: false
}
}
public func gt(rhs: any Common.P4DataValue) -> Bool {
return switch rhs {
case let rrhs as ParserValue: rrhs.type().eq(rhs: self.type()) && self.name > rrhs.name
default: false
}
}
public func gte(rhs: any Common.P4DataValue) -> Bool {
return switch rhs {
case let rrhs as ParserValue: rrhs.type().eq(rhs: self.type()) && self.name >= rrhs.name
default: false
}
}
public var tipe: Parser
public var name: Identifier
public init(_ tipe: Parser, _ name: Identifier) {
self.tipe = tipe
self.name = name
}
public var description: String {
return "Parser \(self.name) of type \(self.tipe)"
}
public func def() -> P4DataValue? {
return self.tipe.def()
}
}
+6 -6
View File
@@ -28,7 +28,7 @@ public struct ExpressionStatement {
public struct Program {
public var types: [P4Type] = Array()
public var externs: [P4Type] = Array()
public var instances: [P4QualifiedType] = Array()
public var instances: [P4Value] = Array()
/// Type of closure for filtering results from ``Program/InstancesWithTypes(_:)``
public typealias TypeFilter = (P4QualifiedType) -> Bool
@@ -36,7 +36,7 @@ public struct Program {
public typealias DataTypeFilter = (P4Type) -> Bool
/// Retrieve global instances in the compiled P4 program.
public func InstancesWithTypes() -> [P4QualifiedType] {
public func InstancesWithTypes() -> [P4Value] {
return self.instances
}
@@ -52,9 +52,9 @@ public struct Program {
///
/// @Snippet(path: "use-program-instanceswithtypes", slice: "include")
///
public func InstancesWithTypes(_ filter: TypeFilter) -> [P4QualifiedType] {
public func InstancesWithTypes(_ filter: TypeFilter) -> [P4Value] {
return self.instances.filter { instance in
filter(instance)
filter(instance.type())
}
}
@@ -112,8 +112,8 @@ public struct Program {
}
public func find_parser(withName name: Identifier) -> Result<Parser> {
for instance in self.instances {
guard let parser = instance.baseType() as? Parser else {
for instance in self.types {
guard let parser = instance as? Parser else {
continue
}
if parser.name == name {
+12 -5
View File
@@ -72,14 +72,16 @@ public struct CodeGenerator: LanguageVisitor {
}
}
/// TODO: Handle instances.
/*
result = Fold(
input: v.instances, initial: result
) { (current, acc) in
return switch acc {
case .Ok(let acc): acc.getVisitorDriver().visit(current.baseType(), context: acc)
case .Ok(let acc): acc.getVisitorDriver().visit(current, context: acc)
case .Error(let e): .Error(e)
}
}
}*/
result = result.map {
.Ok($0.next(uc: $0.getUserContext().append("]")))
@@ -115,7 +117,7 @@ public struct CodeGenerator: LanguageVisitor {
}
public func visit(
_ v: InstantiatedParserState, _ c: VisitorContext<Generated>
_ v: ParserState, _ c: VisitorContext<Generated>
) -> Result<VisitorContext<Generated>> {
let direct_transition_codegen = {
(
@@ -135,14 +137,14 @@ public struct CodeGenerator: LanguageVisitor {
(
state: ParserStateSelectTransition, c: VisitorContext<Generated>
) -> Result<VisitorContext<Generated>> in
return switch self.visit(state.selectExpression, c.next(uc: c.getUserContext().append("["))) {
return switch self.visit(state.te, c.next(uc: c.getUserContext().append("["))) {
case .Ok(let res): .Ok(res.next(uc: res.getUserContext().append("]")))
case .Error(let e): .Error(e)
}
}
var initial = "{"
initial += "name: \"\(v.state)\","
initial += "name: \"\(v.getName())\","
initial += "transitions: "
let result: Result<VisitorContext<Generated>> =
@@ -281,4 +283,9 @@ public struct CodeGenerator: LanguageVisitor {
) -> Result<VisitorContext<Generated>> {
return .Ok(c)
}
public func visit(
_ parser_state: P4Lang.InstantiatedParserState, _ c: P4Lang.VisitorContext<Generated>
) -> Common.Result<P4Lang.VisitorContext<Generated>> {
return .Ok(c)
}
}
+7 -7
View File
@@ -24,7 +24,7 @@ extension SelectCaseExpression: EvaluatableExpression {
}
public func type() -> P4QualifiedType {
return P4QualifiedType(ParserState())
return P4QualifiedType(AnyParserState)
}
}
@@ -50,7 +50,7 @@ extension SelectExpression: EvaluatableExpression {
}
public func type() -> P4QualifiedType {
return P4QualifiedType(ParserState())
return P4QualifiedType(AnyParserState)
}
}
@@ -79,18 +79,18 @@ extension TypedIdentifier: EvaluatableLValueExpression {
}
public func check(
to: any Common.EvaluatableExpression, inScopes scopes: Common.VarTypeScopes
to: any Common.EvaluatableExpression, inScopes scopes: Common.StaticVarValueScopes
) -> Result<()> {
guard case .Ok(let type) = scopes.lookup(identifier: self) else {
return .Error(Error(withMessage: "Cannot assign to identifier not in scope"))
}
return switch type.assignableFromType(to.type()) {
return switch type.0.assignableFromType(to.type()) {
case TypeCheckResults.IncompatibleTypes:
.Error(
Error(
withMessage:
"Cannot assign value with type \(to.type()) to identifier \(self) with type \(type)"))
"Cannot assign value with type \(to.type()) to identifier \(self) with type \(type.0)"))
case TypeCheckResults.ReadOnly:
.Error(
Error(
@@ -318,7 +318,7 @@ extension ArrayAccessExpression: EvaluatableLValueExpression {
}
public func check(
to: any Common.EvaluatableExpression, inScopes scopes: Common.VarTypeScopes
to: any Common.EvaluatableExpression, inScopes scopes: Common.StaticVarValueScopes
) -> Common.Result<()> {
return switch self.type.value_type().assignableFromType(to.type()) {
@@ -426,7 +426,7 @@ extension FieldAccessExpression: EvaluatableLValueExpression {
}
public func check(
to: any Common.EvaluatableExpression, inScopes scopes: Common.VarTypeScopes
to: any Common.EvaluatableExpression, inScopes scopes: Common.StaticVarValueScopes
) -> Common.Result<()> {
return switch self.field.type().assignableFromType(to.type()) {
case TypeCheckResults.IncompatibleTypes:
+27 -31
View File
@@ -39,14 +39,14 @@ extension ParserAssignmentStatement: EvaluatableStatement {
}
}
extension ParserStateDirectTransition: EvaluatableParserState {
extension ParserStateDirectTransitionValue: EvaluatableParserState {
public func execute(
program: Common.ProgramExecution
) -> (any EvaluatableParserState, Common.ProgramExecution) {
var program = program.enter_scope()
let (control_flow, next_execution) = program.evaluator.ExecuteStatements(
statements, inExecution: program)
self.state.statements, inExecution: program)
switch control_flow {
case .Next: program = next_execution
@@ -59,17 +59,7 @@ extension ParserStateDirectTransition: EvaluatableParserState {
)
}
let res = program.scopes.lookup(identifier: get_next_state())
if case .Ok(let value) = res {
if value.type().baseType().eq(rhs: self) {
return (value.dataValue() as! EvaluatableParserState, program.exit_scope())
}
}
program = program.setError(error: res.error()!).exit_scope()
return (self, program.exit_scope())
return (self.next_state as! EvaluatableParserState, program.exit_scope())
}
public func done() -> Bool {
@@ -77,11 +67,11 @@ extension ParserStateDirectTransition: EvaluatableParserState {
}
public func state() -> P4Lang.ParserState {
return self
return self.state
}
}
extension ParserStateNoTransition: EvaluatableParserState {
extension ParserStateNoTransitionValue: EvaluatableParserState {
public func execute(
program: Common.ProgramExecution
) -> (any EvaluatableParserState, Common.ProgramExecution) {
@@ -93,18 +83,18 @@ extension ParserStateNoTransition: EvaluatableParserState {
}
public func state() -> P4Lang.ParserState {
return self
return self.state
}
}
extension ParserStateSelectTransition: EvaluatableParserState {
extension ParserStateSelectTransitionValue: EvaluatableParserState {
public func execute(
program: Common.ProgramExecution
) -> (any EvaluatableParserState, Common.ProgramExecution) {
var program = program.enter_scope()
let (control_flow, next_execution) = program.evaluator.ExecuteStatements(
statements, inExecution: program)
self.state.statements, inExecution: program)
switch control_flow {
case .Next: program = next_execution
case .Error: return (reject, next_execution.exit_scope())
@@ -116,10 +106,9 @@ extension ParserStateSelectTransition: EvaluatableParserState {
)
}
//switch self.selectExpression.evaluate(execution: program) {
switch program.evaluator.EvaluateExpression(self.selectExpression, inExecution: program) {
switch program.evaluator.EvaluateExpression(self.te, inExecution: program) {
case (.Ok(let value), let program):
if value.type().baseType().eq(rhs: self) {
if AnyParserState.eq(rhs: value.type().baseType()) {
return (value.dataValue() as! EvaluatableParserState, program.exit_scope())
} else {
return (
@@ -137,11 +126,11 @@ extension ParserStateSelectTransition: EvaluatableParserState {
}
public func state() -> P4Lang.ParserState {
return self
return self.state
}
}
extension Parser: LibraryCallable {
extension ParserValue: LibraryCallable {
public typealias T = InstantiatedParserState
public func call(
execution: Common.ProgramExecution, arguments: ArgumentList
@@ -149,10 +138,10 @@ extension Parser: LibraryCallable {
var execution = execution.enter_scope()
execution = execution.declare(
identifier: AsInstantiatedParserState(accept.state()).state,
identifier: accept.state().getName(),
withValue: P4Value(accept, P4QualifiedType.ReadOnly(accept.type())))
execution = execution.declare(
identifier: AsInstantiatedParserState(reject.state()).state,
identifier: reject.state().getName(),
withValue: P4Value(reject, P4QualifiedType.ReadOnly(reject.type())))
// Add initial values to the global scope
@@ -161,13 +150,20 @@ extension Parser: LibraryCallable {
}
// First, add every state to the scope!
for state in self.states.states {
for state in self.tipe.states.states {
guard let instantiated_state = state.instantiate(state.getName()) else {
return (
reject,
execution.setError(error: Error(withMessage: "Could not instantiate \(state.getName())"))
)
}
execution = execution.declare(
identifier: state.state, withValue: P4Value(state))
identifier: state.getName(), withValue: P4Value(instantiated_state))
}
guard let _current_state = self.findStartState(),
var current_state = _current_state as? EvaluatableParserState
guard let _current_state = self.tipe.findStartState(),
var current_state = _current_state.instantiate(Identifier(name: "start"))
as? EvaluatableParserState
else {
return (
reject, execution.setError(error: Error(withMessage: "Could not find the start state"))
@@ -181,12 +177,12 @@ extension Parser: LibraryCallable {
while !current_state.done() && !current_execution.hasError() {
(current_state, current_execution) = current_state.execute(program: current_execution)
}
return (.Ok(P4Value(AsInstantiatedParserState(current_state.state()))), current_execution)
return (.Ok(P4Value((current_state))), current_execution)
}
return
switch Call(
body: call_body, withArguments: arguments, withParameters: parameters,
body: call_body, withArguments: arguments, withParameters: self.tipe.parameters,
inExecution: execution)
{
case (.Ok(let value), let updated_execution):
+5 -4
View File
@@ -37,18 +37,19 @@ public struct Runtime<U, T: LibraryCallable<U>>: CustomStringConvertible {
/// Create a parser runtime from a P4 program
public static func create(
program: P4Lang.Program
) -> Result<Runtime<InstantiatedParserState, Parser>> {
) -> Result<Runtime<InstantiatedParserState, ParserValue>> {
return Runtime.create(program: program, withGlobalValues: .none)
}
public static func create(
program: P4Lang.Program, withGlobalValues initial: VarValueScopes?
) -> Result<Runtime<InstantiatedParserState, Parser>> {
) -> Result<Runtime<InstantiatedParserState, ParserValue>> {
return switch program.starting_parser() {
case .Ok(let parser):
.Ok(
P4Runtime.Runtime<InstantiatedParserState, Parser>(
callable: parser, withGlobalValues: initial))
P4Runtime.Runtime<InstantiatedParserState, ParserValue>(
callable: parser.instantiate(Identifier(name: "starting_parser")),
withGlobalValues: initial))
case .Error(let error): .Error(error)
}
}
+134 -55
View File
@@ -18,8 +18,8 @@
import Common
import Foundation
import Macros
import P4Runtime
import P4Lang
import P4Runtime
import SwiftTreeSitter
import Testing
import TreeSitter
@@ -39,20 +39,30 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
var test_declarations = StaticVarValueScopes().enter()
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ta"),
withValue: (P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))), .none))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ta"),
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))
])))
withValue: P4Value(
P4ArrayValue(
withType: P4QualifiedType(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.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_array_access_invalid_type() async throws {
@@ -67,12 +77,15 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Int()))
var test_declarations = StaticVarValueScopes().enter()
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ta"), withValue: (P4QualifiedType(P4Int()), .none))
#expect(
#RequireErrorResult(
Error(
withMessage: "{49, 22}: Failed to parse a statement element: {65, 2}: ta does not name an array type"
withMessage:
"{49, 22}: Failed to parse a statement element: {65, 2}: ta does not name an array type"
),
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
)
@@ -90,21 +103,29 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
var test_declarations = StaticVarValueScopes().enter()
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ta"),
withValue: (P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))), .none))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ta"),
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))
])))
withValue: P4Value(
P4ArrayValue(
withType: P4QualifiedType(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.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_array_access3() async throws {
@@ -118,20 +139,29 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
var test_declarations = StaticVarValueScopes().enter()
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ta"),
withValue: (P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))), .none))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ta"),
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3)),
])))
withValue: P4Value(
P4ArrayValue(
withType: P4QualifiedType(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.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_array_access4() async throws {
@@ -145,20 +175,28 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
var test_declarations = StaticVarValueScopes().enter()
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ta"),
withValue: (P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))), .none))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ta"),
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3)),
])))
withValue: P4Value(
P4ArrayValue(
withType: P4QualifiedType(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.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_array_access_nested() async throws {
@@ -173,24 +211,39 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))))
var test_declarations = StaticVarValueScopes().enter()
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ta"),
withValue: (
P4QualifiedType(
P4Array(withValueType: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))),
.none
))
var test_values = VarValueScopes().enter()
let nested = P4Value(P4ArrayValue(
withType: P4QualifiedType(P4Int()),
withValue: [P4Value(P4IntValue(withValue: 5)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))]))
let nested = P4Value(
P4ArrayValue(
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: P4QualifiedType(P4Array(withValueType: P4QualifiedType(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))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_array_set() async throws {
@@ -206,20 +259,30 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
var test_declarations = StaticVarValueScopes().enter()
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ta"),
withValue: (P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))), .none))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ta"),
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3)),
])))
withValue: P4Value(
P4ArrayValue(
withType: P4QualifiedType(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.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_array_set_nested() async throws {
@@ -235,22 +298,38 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))))
var test_declarations = StaticVarValueScopes().enter()
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ta"),
withValue: (
P4QualifiedType(
P4Array(withValueType: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))),
.none
))
var test_values = VarValueScopes().enter()
let nested = P4Value(P4ArrayValue(
withType: P4QualifiedType(P4Int()),
withValue: [P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))]))
let nested = P4Value(
P4ArrayValue(
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: P4QualifiedType(P4Array(withValueType: P4QualifiedType(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))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@@ -42,10 +42,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_and2() async throws {
@@ -61,10 +61,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_parser_binary_operator_and3() async throws {
@@ -80,10 +80,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_parser_binary_operator_and4() async throws {
@@ -99,10 +99,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_parser_binary_operator_or() async throws {
@@ -118,10 +118,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_or2() async throws {
@@ -137,10 +137,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_or3() async throws {
@@ -156,10 +156,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_or4() async throws {
@@ -175,10 +175,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_parser_binary_operator_grouped() async throws {
@@ -194,10 +194,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_grouped2() async throws {
@@ -213,8 +213,8 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
+28 -28
View File
@@ -42,10 +42,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_equal_not_equal_bool() async throws {
@@ -61,10 +61,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_parser_binary_operator_less_than_bool() async throws {
@@ -80,10 +80,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_less_than_equal_bool() async throws {
@@ -99,10 +99,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_less_than_equal_bool2() async throws {
@@ -118,10 +118,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_greater_than_bool() async throws {
@@ -137,10 +137,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_greater_than_equal_bool() async throws {
@@ -156,10 +156,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_greater_than_equal_bool2() async throws {
@@ -175,10 +175,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_less_than_bool_not() async throws {
@@ -194,10 +194,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_parser_binary_operator_less_than_bool_not2() async throws {
@@ -213,10 +213,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_parser_binary_operator_less_than_equal_bool_not() async throws {
@@ -232,10 +232,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_parser_binary_operator_greater_than_bool_not() async throws {
@@ -251,10 +251,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_parser_binary_operator_greater_than_bool_not2() async throws {
@@ -270,10 +270,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_parser_binary_operator_greater_than_equal_bool_not() async throws {
@@ -289,8 +289,8 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@@ -42,10 +42,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_equal_not_equal_integer() async throws {
@@ -61,10 +61,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_parser_binary_operator_less_than_integer() async throws {
@@ -80,10 +80,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_less_than_equal_integer() async throws {
@@ -99,10 +99,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_less_than_equal_integer2() async throws {
@@ -118,10 +118,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_greater_than_integer() async throws {
@@ -137,10 +137,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_greater_than_equal_integer() async throws {
@@ -156,10 +156,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_greater_than_equal_integer2() async throws {
@@ -175,10 +175,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_less_than_integer_not() async throws {
@@ -194,10 +194,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_parser_binary_operator_less_than_integer_not2() async throws {
@@ -213,10 +213,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_parser_binary_operator_less_than_equal_integer_not() async throws {
@@ -232,10 +232,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_parser_binary_operator_greater_than_integer_not() async throws {
@@ -251,10 +251,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_parser_binary_operator_greater_than_integer_not2() async throws {
@@ -270,10 +270,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_parser_binary_operator_greater_than_equal_integer_not() async throws {
@@ -289,10 +289,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@@ -311,10 +311,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_add_non_integer() async throws {
@@ -374,10 +374,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_subtract_non_integer() async throws {
@@ -437,10 +437,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_multiply_non_integer() async throws {
@@ -500,10 +500,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_divide_non_integer() async throws {
@@ -42,10 +42,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_equal_not_equal_string() async throws {
@@ -61,10 +61,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_parser_binary_operator_less_than_string() async throws {
@@ -80,10 +80,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_less_than_equal_string() async throws {
@@ -99,10 +99,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_greater_than_string() async throws {
@@ -118,10 +118,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_greater_than_equal_string() async throws {
@@ -137,10 +137,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_binary_operator_less_than_string_not() async throws {
@@ -156,10 +156,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_parser_binary_operator_less_than_equal_string_not() async throws {
@@ -175,10 +175,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_parser_binary_operator_greater_than_string_not() async throws {
@@ -194,10 +194,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_parser_binary_operator_greater_than_equal_string_not() async throws {
@@ -213,8 +213,8 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@@ -40,7 +40,6 @@ import TreeSitterP4
}
};
"""
let test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
@@ -50,11 +49,11 @@ import TreeSitterP4
test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_struct_equality_one_empty() async throws {
@@ -73,7 +72,6 @@ import TreeSitterP4
}
};
"""
let test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
@@ -83,11 +81,11 @@ import TreeSitterP4
test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_struct_equality_neither_empty() async throws {
@@ -107,7 +105,6 @@ import TreeSitterP4
}
};
"""
let test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
@@ -117,11 +114,11 @@ import TreeSitterP4
test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_struct_equality_neither_empty2() async throws {
@@ -144,7 +141,6 @@ import TreeSitterP4
}
};
"""
let test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
@@ -154,11 +150,11 @@ import TreeSitterP4
test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_struct_equality_neither_empty3() async throws {
@@ -181,7 +177,6 @@ import TreeSitterP4
}
};
"""
let test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
@@ -191,11 +186,11 @@ import TreeSitterP4
test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
+1 -2
View File
@@ -37,7 +37,7 @@ func shrink(_ from: String) -> String {
parser main_parser() {
state start {
true;
transition start;
transition accept;
}
};
"""
@@ -59,7 +59,6 @@ func shrink(_ from: String) -> String {
}
]
""")
#expect(expected == cg.getGeneratedCode())
}
+4 -4
View File
@@ -46,10 +46,10 @@ 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.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_parser_with_conditional_statement_and_else() async throws {
@@ -74,9 +74,9 @@ 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.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
+12 -12
View File
@@ -41,14 +41,14 @@ import TreeSitterP4
}
};
"""
let x = { (tipe: P4QualifiedType) -> Bool in
switch tipe.baseType() {
let x = { (tipe: P4Type) -> Bool in
switch tipe {
case let c as Control: c.name == "simple"
default: false
}
}
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
#expect(program.InstancesWithTypes(x).count == 1)
#expect(program.TypesWithTypes(x).count == 1)
}
@Test func test_simple_control_declaration2() async throws {
@@ -79,14 +79,14 @@ import TreeSitterP4
};
"""
let filter = { (tipe: P4QualifiedType) -> Bool in
switch tipe.baseType() {
let filter = { (tipe: P4Type) -> Bool in
switch tipe {
case let c as Control: c.name == "simple" || c.name == "complex"
default: false
}
}
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
#expect(program.InstancesWithTypes(filter).count == 2)
#expect(program.TypesWithTypes(filter).count == 2)
}
@Test func test_simple_control_declaration_with_actions() async throws {
@@ -106,14 +106,14 @@ import TreeSitterP4
}
};
"""
let x = { (tipe: P4QualifiedType) -> Bool in
switch tipe.baseType() {
let x = { (tipe: P4Type) -> Bool in
switch tipe {
case let c as Control: c.name == "simple"
default: false
}
}
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
#expect(program.InstancesWithTypes(x).count == 1)
#expect(program.TypesWithTypes(x).count == 1)
}
@Test func test_simple_control_declaration_with_misnamed_actions() async throws {
@@ -341,14 +341,14 @@ import TreeSitterP4
};
"""
let x = { (tipe: P4QualifiedType) -> Bool in
switch tipe.baseType() {
let x = { (tipe: P4Type) -> Bool in
switch tipe {
case let c as Control: c.name == "simple"
default: false
}
}
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
#expect(program.InstancesWithTypes(x).count == 1)
#expect(program.TypesWithTypes(x).count == 1)
}
@Test func test_simple_control_declaration_with_action_with_params_wrong_order() async throws {
+145 -108
View File
@@ -53,13 +53,13 @@ import TreeSitterP4
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
// Pull the control out of the compiled program.
let controls = program.InstancesWithTypes() { (tipe: P4QualifiedType) -> Bool in
switch tipe.baseType() {
let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in
return switch tipe {
case let c as Control: c.name == "simple"
default: false
}
}
var control = ((controls[0].baseType() as P4Type) as! Control)
var control = ((controls[0]) as! Control)
// Add entries to the table.
control = control.updateTable(
@@ -78,23 +78,27 @@ import TreeSitterP4
global_values = global_values.declare(
identifier: Identifier(name: "result_arg"),
withValue: P4Value(
P4IntValue(withValue: 0),
P4QualifiedType(P4Int())))
P4IntValue(withValue: 0),
P4QualifiedType(P4Int())))
let runtime = try #UseOkResult(
P4Runtime.Runtime<P4TableHitMissValue, Control>.create(control: control, withGlobalValues: global_values))
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: P4QualifiedType(P4Int())), atIndex: 0),
Argument(P4Value(P4BooleanValue(withValue: true)), atIndex: 1),
])))
let (hit_miss, updated_execution) = try #UseOkResult(
runtime.run(
withArguments: ArgumentList([
Argument(
TypedIdentifier(name: "result_arg", withType: P4QualifiedType(P4Int())), atIndex: 0),
Argument(P4Value(P4BooleanValue(withValue: true)), atIndex: 1),
])))
// We expect there to be a hit.
#expect(hit_miss == P4TableHitMissValue.Hit)
// And that the proper action was invoked.
let result_arg = try #UseOkResult(updated_execution.scopes.lookup(identifier: Identifier(name: "result_arg")))
let result_arg = try #UseOkResult(
updated_execution.scopes.lookup(identifier: Identifier(name: "result_arg")))
#expect(result_arg.eq(P4Value(P4IntValue(withValue: 5))))
}
@@ -124,13 +128,13 @@ import TreeSitterP4
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
// Pull the control out of the compiled program.
let controls = program.InstancesWithTypes() { (tipe: P4QualifiedType) -> Bool in
switch tipe.baseType() {
let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in
switch tipe {
case let c as Control: c.name == "simple"
default: false
}
}
var control = ((controls[0].baseType() as P4Type) as! Control)
var control = ((controls[0]) as! Control)
// Add entries to the table.
control = control.updateTable(
@@ -149,23 +153,27 @@ import TreeSitterP4
global_values = global_values.declare(
identifier: Identifier(name: "result_arg"),
withValue: P4Value(
P4IntValue(withValue: 0),
P4QualifiedType(P4Int())))
P4IntValue(withValue: 0),
P4QualifiedType(P4Int())))
let runtime = try #UseOkResult(
P4Runtime.Runtime<P4TableHitMissValue, Control>.create(control: control, withGlobalValues: global_values))
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: P4QualifiedType(P4Int())), atIndex: 0),
Argument(P4Value(P4BooleanValue(withValue: false)), atIndex: 1),
])))
let (hit_miss, updated_execution) = try #UseOkResult(
runtime.run(
withArguments: ArgumentList([
Argument(
TypedIdentifier(name: "result_arg", withType: P4QualifiedType(P4Int())), atIndex: 0),
Argument(P4Value(P4BooleanValue(withValue: false)), atIndex: 1),
])))
// We expect there to be a hit.
#expect(hit_miss == P4TableHitMissValue.Hit)
// And that the proper action was invoked.
let result_arg = try #UseOkResult(updated_execution.scopes.lookup(identifier: Identifier(name: "result_arg")))
let result_arg = try #UseOkResult(
updated_execution.scopes.lookup(identifier: Identifier(name: "result_arg")))
#expect(result_arg.eq(P4Value(P4IntValue(withValue: 7))))
}
@@ -195,13 +203,13 @@ import TreeSitterP4
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
// Pull the control out of the compiled program.
let controls = program.InstancesWithTypes() { (tipe: P4QualifiedType) -> Bool in
switch tipe.baseType() {
let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in
switch tipe {
case let c as Control: c.name == "simple"
default: false
}
}
var control = ((controls[0].baseType() as P4Type) as! Control)
var control = ((controls[0]) as! Control)
// Add entries to the table.
control = control.updateTable(
@@ -220,22 +228,26 @@ import TreeSitterP4
global_values = global_values.declare(
identifier: Identifier(name: "result_arg"),
withValue: P4Value(
P4IntValue(withValue: 0),
P4QualifiedType(P4Int())))
P4IntValue(withValue: 0),
P4QualifiedType(P4Int())))
let runtime = try #UseOkResult(
P4Runtime.Runtime<P4TableHitMissValue, Control>.create(control: control, withGlobalValues: global_values))
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: P4QualifiedType(P4Int())), atIndex: 0),
Argument(P4Value(P4IntValue(withValue: 5)), atIndex: 1),
])))
let (hit_miss, updated_execution) = try #UseOkResult(
runtime.run(
withArguments: ArgumentList([
Argument(
TypedIdentifier(name: "result_arg", withType: P4QualifiedType(P4Int())), atIndex: 0),
Argument(P4Value(P4IntValue(withValue: 5)), atIndex: 1),
])))
// We expect there to be a hit.
#expect(hit_miss == P4TableHitMissValue.Hit)
let result_arg = try #UseOkResult(updated_execution.scopes.lookup(identifier: Identifier(name: "result_arg")))
let result_arg = try #UseOkResult(
updated_execution.scopes.lookup(identifier: Identifier(name: "result_arg")))
#expect(result_arg.eq(P4Value(P4IntValue(withValue: 5))))
}
@@ -265,13 +277,13 @@ import TreeSitterP4
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
// Pull the control out of the compiled program.
let controls = program.InstancesWithTypes() { (tipe: P4QualifiedType) -> Bool in
switch tipe.baseType() {
let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in
return switch tipe {
case let c as Control: c.name == "simple"
default: false
}
}
var control = ((controls[0].baseType() as P4Type) as! Control)
var control = ((controls[0]) as! Control)
// Add entries to the table.
control = control.updateTable(
@@ -290,22 +302,26 @@ import TreeSitterP4
global_values = global_values.declare(
identifier: Identifier(name: "result_arg"),
withValue: P4Value(
P4IntValue(withValue: 0),
P4QualifiedType(P4Int())))
P4IntValue(withValue: 0),
P4QualifiedType(P4Int())))
let runtime = try #UseOkResult(
P4Runtime.Runtime<P4TableHitMissValue, Control>.create(control: control, withGlobalValues: global_values))
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: P4QualifiedType(P4Int())), atIndex: 0),
Argument(P4Value(P4IntValue(withValue: 3)), atIndex: 1),
])))
let (hit_miss, updated_execution) = try #UseOkResult(
runtime.run(
withArguments: ArgumentList([
Argument(
TypedIdentifier(name: "result_arg", withType: P4QualifiedType(P4Int())), atIndex: 0),
Argument(P4Value(P4IntValue(withValue: 3)), atIndex: 1),
])))
// We expect there to be a hit.
#expect(hit_miss == P4TableHitMissValue.Miss)
let result_arg = try #UseOkResult(updated_execution.scopes.lookup(identifier: Identifier(name: "result_arg")))
let result_arg = try #UseOkResult(
updated_execution.scopes.lookup(identifier: Identifier(name: "result_arg")))
#expect(result_arg.eq(P4Value(P4IntValue(withValue: 0))))
}
@@ -336,13 +352,13 @@ import TreeSitterP4
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
// Pull the control out of the compiled program.
let controls = program.InstancesWithTypes() { (tipe: P4QualifiedType) -> Bool in
switch tipe.baseType() {
let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in
return switch tipe {
case let c as Control: c.name == "simple"
default: false
}
}
var control = ((controls[0].baseType() as P4Type) as! Control)
var control = ((controls[0]) as! Control)
// Add entries to the table.
control = control.updateTable(
@@ -361,24 +377,28 @@ import TreeSitterP4
global_values = global_values.declare(
identifier: Identifier(name: "result_arg"),
withValue: P4Value(
P4IntValue(withValue: 0),
P4QualifiedType(P4Int())))
P4IntValue(withValue: 0),
P4QualifiedType(P4Int())))
let runtime = try #UseOkResult(
P4Runtime.Runtime<P4TableHitMissValue, Control>.create(control: control, withGlobalValues: global_values))
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: P4QualifiedType(P4Int())), atIndex: 0),
Argument(P4Value(P4BooleanValue(withValue: false)), atIndex: 1), // false will make the x key miss.
Argument(P4Value(P4IntValue(withValue: 5)), atIndex: 2),
])))
let (hit_miss, updated_execution) = try #UseOkResult(
runtime.run(
withArguments: ArgumentList([
Argument(
TypedIdentifier(name: "result_arg", withType: P4QualifiedType(P4Int())), atIndex: 0),
Argument(P4Value(P4BooleanValue(withValue: false)), atIndex: 1), // false will make the x key miss.
Argument(P4Value(P4IntValue(withValue: 5)), atIndex: 2),
])))
// We expect there to be a hit -- but from the second key!
#expect(hit_miss == P4TableHitMissValue.Hit)
// And that the proper action was invoked.
let result_arg = try #UseOkResult(updated_execution.scopes.lookup(identifier: Identifier(name: "result_arg")))
let result_arg = try #UseOkResult(
updated_execution.scopes.lookup(identifier: Identifier(name: "result_arg")))
#expect(result_arg.eq(P4Value(P4IntValue(withValue: 7))))
}
@@ -412,13 +432,13 @@ import TreeSitterP4
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
// Pull the control out of the compiled program.
let controls = program.InstancesWithTypes() { (tipe: P4QualifiedType) -> Bool in
switch tipe.baseType() {
let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in
return switch tipe {
case let c as Control: c.name == "simple"
default: false
}
}
var control = ((controls[0].baseType() as P4Type) as! Control)
var control = ((controls[0]) as! Control)
let k_fields = P4StructFields([
P4StructFieldIdentifier(name: "i", withType: P4QualifiedType(P4Int())),
@@ -426,10 +446,12 @@ import TreeSitterP4
])
let k_type = P4Struct(withName: Identifier(name: "K"), andFields: k_fields)
let k_instance = P4StructValue(withType: k_type, andInitializers: [
P4Value(P4IntValue(withValue: 5)),
P4Value(P4IntValue(withValue: 1)),
])
let k_instance = P4StructValue(
withType: k_type,
andInitializers: [
P4Value(P4IntValue(withValue: 5)),
P4Value(P4IntValue(withValue: 1)),
])
// Add entries to the table.
control = control.updateTable(
@@ -448,54 +470,63 @@ import TreeSitterP4
global_values = global_values.declare(
identifier: Identifier(name: "result_arg"),
withValue: P4Value(
P4IntValue(withValue: 0),
P4QualifiedType(P4Int())))
P4IntValue(withValue: 0),
P4QualifiedType(P4Int())))
let runtime = try #UseOkResult(
P4Runtime.Runtime<P4TableHitMissValue, Control>.create(control: control, withGlobalValues: global_values))
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: P4QualifiedType(P4Int())), atIndex: 0),
Argument(P4Value(k_instance), atIndex: 1),
])))
let (hit_miss, updated_execution) = try #UseOkResult(
runtime.run(
withArguments: ArgumentList([
Argument(
TypedIdentifier(name: "result_arg", withType: P4QualifiedType(P4Int())), atIndex: 0),
Argument(P4Value(k_instance), atIndex: 1),
])))
// We expect there to be a hit.
#expect(hit_miss == P4TableHitMissValue.Hit)
// And that the proper action was invoked.
let result_arg = try #UseOkResult(updated_execution.scopes.lookup(identifier: Identifier(name: "result_arg")))
let result_arg = try #UseOkResult(
updated_execution.scopes.lookup(identifier: Identifier(name: "result_arg")))
#expect(result_arg.eq(P4Value(P4IntValue(withValue: 5))))
// Now, check whether the b action can be invoked.
let k_instance2 = P4StructValue(withType: k_type, andInitializers: [
P4Value(P4IntValue(withValue: 4)),
P4Value(P4IntValue(withValue: 1)),
])
let k_instance2 = P4StructValue(
withType: k_type,
andInitializers: [
P4Value(P4IntValue(withValue: 4)),
P4Value(P4IntValue(withValue: 1)),
])
// Set a variable in the global scope for the inout first parameter.
var next_global_values = VarValueScopes().enter()
next_global_values = global_values.declare(
identifier: Identifier(name: "result_arg"),
withValue: P4Value(
P4IntValue(withValue: 0),
P4QualifiedType(P4Int())))
P4IntValue(withValue: 0),
P4QualifiedType(P4Int())))
let runtime2 = try #UseOkResult(
P4Runtime.Runtime<P4TableHitMissValue, Control>.create(control: control, withGlobalValues: next_global_values))
P4Runtime.Runtime<P4TableHitMissValue, Control>.create(
control: control, withGlobalValues: next_global_values))
let (hit_miss2, updated_execution2) = try #UseOkResult(runtime2.run(
withArguments: ArgumentList([
Argument(TypedIdentifier(name: "result_arg", withType: P4QualifiedType(P4Int())), atIndex: 0),
Argument(P4Value(k_instance2), atIndex: 1),
])))
let (hit_miss2, updated_execution2) = try #UseOkResult(
runtime2.run(
withArguments: ArgumentList([
Argument(
TypedIdentifier(name: "result_arg", withType: P4QualifiedType(P4Int())), atIndex: 0),
Argument(P4Value(k_instance2), atIndex: 1),
])))
// We expect there to be a hit.
#expect(hit_miss2 == P4TableHitMissValue.Hit)
// And that the proper action was invoked.
let result_arg2 = try #UseOkResult(updated_execution2.scopes.lookup(identifier: Identifier(name: "result_arg")))
let result_arg2 = try #UseOkResult(
updated_execution2.scopes.lookup(identifier: Identifier(name: "result_arg")))
#expect(result_arg2.eq(P4Value(P4IntValue(withValue: 7))))
}
@@ -529,13 +560,13 @@ import TreeSitterP4
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
// Pull the control out of the compiled program.
let controls = program.InstancesWithTypes() { (tipe: P4QualifiedType) -> Bool in
switch tipe.baseType() {
let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in
return switch tipe {
case let c as Control: c.name == "simple"
default: false
}
}
var control = ((controls[0].baseType() as P4Type) as! Control)
var control = ((controls[0]) as! Control)
let k_fields = P4StructFields([
P4StructFieldIdentifier(name: "i", withType: P4QualifiedType(P4Int())),
@@ -543,10 +574,12 @@ import TreeSitterP4
])
let k_type = P4Struct(withName: Identifier(name: "K"), andFields: k_fields)
let k_instance = P4StructValue(withType: k_type, andInitializers: [
P4Value(P4IntValue(withValue: 8)),
P4Value(P4IntValue(withValue: 1)),
])
let k_instance = P4StructValue(
withType: k_type,
andInitializers: [
P4Value(P4IntValue(withValue: 8)),
P4Value(P4IntValue(withValue: 1)),
])
// Add entries to the table.
control = control.updateTable(
@@ -565,22 +598,26 @@ import TreeSitterP4
global_values = global_values.declare(
identifier: Identifier(name: "result_arg"),
withValue: P4Value(
P4IntValue(withValue: 0),
P4QualifiedType(P4Int())))
P4IntValue(withValue: 0),
P4QualifiedType(P4Int())))
let runtime = try #UseOkResult(
P4Runtime.Runtime<P4TableHitMissValue, Control>.create(control: control, withGlobalValues: global_values))
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: P4QualifiedType(P4Int())), atIndex: 0),
Argument(P4Value(k_instance), atIndex: 1),
])))
let (hit_miss, updated_execution) = try #UseOkResult(
runtime.run(
withArguments: ArgumentList([
Argument(
TypedIdentifier(name: "result_arg", withType: P4QualifiedType(P4Int())), atIndex: 0),
Argument(P4Value(k_instance), atIndex: 1),
])))
// We expect there to be a hit.
#expect(hit_miss == P4TableHitMissValue.Miss)
// And that the proper action was invoked.
let result_arg = try #UseOkResult(updated_execution.scopes.lookup(identifier: Identifier(name: "result_arg")))
let result_arg = try #UseOkResult(
updated_execution.scopes.lookup(identifier: Identifier(name: "result_arg")))
#expect(result_arg.eq(P4Value(P4IntValue(withValue: 0))))
}
}
+10 -10
View File
@@ -47,9 +47,9 @@ 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.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_struct_declaration_and_field_write_field_read() async throws {
@@ -72,9 +72,9 @@ 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.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_struct_declaration_and_field_read_defaults() async throws {
@@ -95,9 +95,9 @@ 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.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_struct_declaration_and_field_read_defaults_sc() async throws {
@@ -118,9 +118,9 @@ 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.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_struct_declaration_and_field_read_defaults_sc2() async throws {
@@ -142,9 +142,9 @@ 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.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_function_declaration() async throws {
+6 -6
View File
@@ -41,10 +41,10 @@ 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.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_expression_grouped_or() async throws {
@@ -61,10 +61,10 @@ 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.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_expression_grouped_and() async throws {
@@ -81,8 +81,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.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@@ -46,12 +46,12 @@ import TreeSitterP4
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(parser.states.count() == 1)
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_function_call_scoped_name_collision2() async throws {
@@ -75,12 +75,12 @@ import TreeSitterP4
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(parser.states.count() == 1)
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_function_call_scoped_name_collision_inout() async throws {
@@ -103,12 +103,12 @@ import TreeSitterP4
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(parser.states.count() == 1)
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@@ -130,12 +130,12 @@ import TreeSitterP4
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(parser.states.count() == 1)
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_function_call_integer_return_value2() async throws {
@@ -156,12 +156,12 @@ import TreeSitterP4
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(parser.states.count() == 1)
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_function_call_invalid_return_type() async throws {
@@ -41,12 +41,12 @@ import TreeSitterP4
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(parser.states.count() == 1)
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_with_transition_select_case_default_expression() async throws {
@@ -63,12 +63,12 @@ import TreeSitterP4
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(parser.states.count() == 1)
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_parser_with_transition_select_case_default_expression2() async throws {
@@ -85,12 +85,12 @@ import TreeSitterP4
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(parser.states.count() == 1)
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_with_transition_select_case_default_expression3() async throws {
@@ -107,12 +107,12 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(parser.states.count() == 1)
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_parser_with_transition_select_case_invalid_type() async throws {
@@ -151,12 +151,12 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(parser.states.count() == 1)
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@@ -172,13 +172,13 @@ 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.ParserValue>.create(program: program))
let args = ArgumentList([
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)
#expect(state_result == P4Lang.reject)
}
@Test func test_select_expression_from_parser_parameters2() async throws {
@@ -193,11 +193,11 @@ 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.ParserValue>.create(program: program))
let args = ArgumentList([
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)
#expect(state_result == P4Lang.accept)
}
+4 -4
View File
@@ -89,10 +89,10 @@ public struct Return6: P4FFI {
simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: .none,
withFFIs: [externally]))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@@ -117,8 +117,8 @@ public struct Return6: P4FFI {
simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: .none,
withFFIs: [externally]))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
+6 -4
View File
@@ -47,7 +47,7 @@ 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.ParserValue>.create(program: program))
var statements_executed: [String] = Array()
@@ -57,7 +57,9 @@ import TreeSitterP4
let (state_result, _) = try! #UseOkResult(runtime.run(withArguments: ArgumentList(), inExecution: ProgramExecution(ev)))
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
print("statements_executed: \(statements_executed)")
#expect(statements_executed[0].hasPrefix("VariableDeclarationStatement"))
#expect(statements_executed[1].hasPrefix("ParserAssignmentStatement"))
@@ -86,7 +88,7 @@ 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.ParserValue>.create(program: program))
var expressions_evaluated: [String] = Array()
@@ -96,7 +98,7 @@ import TreeSitterP4
let (state_result, _) = try! #UseOkResult(runtime.run(withArguments: ArgumentList(), inExecution: ProgramExecution(ev)))
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
#expect(expressions_evaluated[0].hasPrefix("Value: true of Boolean"))
#expect(expressions_evaluated[1].hasPrefix("Value: true of Boolean"))
+3 -3
View File
@@ -46,7 +46,7 @@ import P4Lang
parser main_parser() {
state start {
true;
transition start;
transition accept;
}
};
"""
@@ -56,8 +56,8 @@ import P4Lang
#expect(parser.states.count() == 1)
let state = AsInstantiatedParserState((try! #require(parser.states.find(withIdentifier: Identifier(name: "start")))))
#expect(state.state == Identifier(name: "start"))
let state = (try! #require(parser.states.find(withIdentifier: Identifier(name: "start"))))
#expect(state.getName() == Identifier(name: "start"))
#expect(state.statements.count == 1)
}
+10 -10
View File
@@ -38,11 +38,11 @@ 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.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
// We should be in the accept state.
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_runtime_to_accept() async throws {
@@ -56,10 +56,10 @@ 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.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
// We should be in the accept state.
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_runtime_no_start_state() async throws {
@@ -73,7 +73,7 @@ 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.ParserValue>.create(program: program))
#expect(
#RequireErrorResult<(InstantiatedParserState, ProgramExecution)>(
@@ -93,14 +93,14 @@ 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.ParserValue>.create(program: program))
let args = ArgumentList([
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.
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_runtime_parser_with_mismatched_parameter_types() async throws {
@@ -115,7 +115,7 @@ 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.ParserValue>.create(program: program))
let args = ArgumentList([
Argument(P4Value(P4BooleanValue(withValue: true)), atIndex: 1), Argument(P4Value(P4BooleanValue(withValue: false)), atIndex: 2), Argument(P4Value(P4IntValue(withValue: 5)), atIndex: 3),
@@ -139,7 +139,7 @@ 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.ParserValue>.create(program: program))
let args = ArgumentList([
Argument(P4Value(P4IntValue(withValue: 5)), atIndex: 1), Argument(P4Value(P4StringValue(withValue: "Testing")), atIndex: 2), Argument(P4Value(P4IntValue(withValue: 5)), atIndex: 3),
@@ -163,7 +163,7 @@ 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.ParserValue>.create(program: program))
let args = ArgumentList([Argument(P4Value(P4BooleanValue(withValue: true)), atIndex: 0)])
#expect(
+10 -10
View File
@@ -43,10 +43,10 @@ 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.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_scope() async throws {
@@ -69,10 +69,10 @@ 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.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@@ -97,10 +97,10 @@ 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.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_simple_assignment() async throws {
@@ -121,10 +121,10 @@ 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.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@@ -152,8 +152,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.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
+170 -108
View File
@@ -18,8 +18,8 @@
import Common
import Foundation
import Macros
import P4Runtime
import P4Lang
import P4Runtime
import SwiftTreeSitter
import Testing
import TreeSitter
@@ -39,27 +39,33 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
var test_declarations = StaticVarValueScopes().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))
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ts"), withValue: (P4QualifiedType(struct_type), .none))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ts"),
withValue: P4Value(P4StructValue(withType: struct_type, andInitializers: [
P4Value(P4BooleanValue(withValue: true)),
P4Value(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))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_field_access_declared() async throws {
@@ -84,11 +90,13 @@ import TreeSitterP4
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let program = try #UseOkResult(
Program.Compile(
simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_field_access_declared2() async throws {
@@ -106,19 +114,22 @@ import TreeSitterP4
}
};
"""
var test_types = TypeTypeScopes().enter()
var test_declarations = TypeTypeScopes().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_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
test_declarations = test_declarations.declare(
identifier: Identifier(name: "Testing"), withValue: struct_type)
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_declarations))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_field_access_opp() async throws {
@@ -133,30 +144,35 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
var test_declarations = StaticVarValueScopes().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))
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ts"), withValue: (P4QualifiedType(struct_type), .none))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ts"),
withValue: P4Value(P4StructValue(withType: struct_type, andInitializers: [
P4Value(P4BooleanValue(withValue: false)),
P4Value(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))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_field_access2() async throws {
let simple_parser_declaration = """
parser main_parser() {
@@ -168,27 +184,33 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
var test_declarations = StaticVarValueScopes().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))
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ts"), withValue: (P4QualifiedType(struct_type), .none))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ts"),
withValue: P4Value(P4StructValue(withType: struct_type, andInitializers: [
P4Value(P4BooleanValue(withValue: true)),
P4Value(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))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_field_access2_opp() async throws {
@@ -202,27 +224,33 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
var test_declarations = StaticVarValueScopes().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))
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ts"), withValue: (P4QualifiedType(struct_type), .none))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ts"),
withValue: P4Value(P4StructValue(withType: struct_type, andInitializers: [
P4Value(P4BooleanValue(withValue: true)),
P4Value(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))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_field_access_nested() async throws {
@@ -237,7 +265,7 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
var test_declarations = StaticVarValueScopes().enter()
let ty_fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
@@ -245,30 +273,37 @@ import TreeSitterP4
])
let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields)
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4QualifiedType(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: P4QualifiedType(ts_struct_type))
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ts"), withValue: (P4QualifiedType(ts_struct_type), .none))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ts"),
withValue: P4Value(P4StructValue(
withType: ts_struct_type,
andInitializers: [
P4Value(P4StructValue(
withType: ty_struct_type,
andInitializers: [
P4Value(P4BooleanValue(withValue: true)),
P4Value(P4IntValue(withValue: 5)),
]))
])))
withValue: P4Value(
P4StructValue(
withType: ts_struct_type,
andInitializers: [
P4Value(
P4StructValue(
withType: ty_struct_type,
andInitializers: [
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.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_field_write() async throws {
@@ -284,27 +319,33 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
var test_declarations = StaticVarValueScopes().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))
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ts"), withValue: (P4QualifiedType(struct_type), .none))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ts"),
withValue: P4Value(P4StructValue(withType: struct_type, andInitializers: [
P4Value(P4BooleanValue(withValue: false)),
P4Value(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))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_field_write_invalid_type() async throws {
@@ -316,18 +357,20 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
var test_declarations = StaticVarValueScopes().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))
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ts"), withValue: (P4QualifiedType(struct_type), .none))
#expect(
#RequireErrorResult(
Error(
withMessage: "{49, 13}: Failed to parse a statement element: {49, 8}: Cannot assign value of type Int (width: Infinite) to field yesno of type Boolean"
withMessage:
"{49, 13}: Failed to parse a statement element: {49, 8}: Cannot assign value of type Int (width: Infinite) to field yesno of type Boolean"
),
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
)
@@ -346,7 +389,7 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
var test_declarations = StaticVarValueScopes().enter()
let ty_fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
@@ -354,30 +397,37 @@ import TreeSitterP4
])
let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields)
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4QualifiedType(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: P4QualifiedType(ts_struct_type))
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ts"), withValue: (P4QualifiedType(ts_struct_type), .none))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ts"),
withValue: P4Value(P4StructValue(
withType: ts_struct_type,
andInitializers: [
P4Value(P4StructValue(
withType: ty_struct_type,
andInitializers: [
P4Value(P4BooleanValue(withValue: true)),
P4Value(P4IntValue(withValue: 7)),
]))
])))
withValue: P4Value(
P4StructValue(
withType: ts_struct_type,
andInitializers: [
P4Value(
P4StructValue(
withType: ty_struct_type,
andInitializers: [
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.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_field_write_nested2() async throws {
@@ -394,7 +444,7 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
var test_declarations = StaticVarValueScopes().enter()
let ty_fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
@@ -402,30 +452,37 @@ import TreeSitterP4
])
let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields)
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4QualifiedType(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: P4QualifiedType(ts_struct_type))
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ts"), withValue: (P4QualifiedType(ts_struct_type), .none))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ts"),
withValue: P4Value(P4StructValue(
withType: ts_struct_type,
andInitializers: [
P4Value(P4StructValue(
withType: ty_struct_type,
andInitializers: [
P4Value(P4BooleanValue(withValue: true)),
P4Value(P4IntValue(withValue: 7)),
]))
])))
withValue: P4Value(
P4StructValue(
withType: ts_struct_type,
andInitializers: [
P4Value(
P4StructValue(
withType: ty_struct_type,
andInitializers: [
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.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_field_write_nested_invalid_type() async throws {
@@ -441,7 +498,7 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
var test_declarations = StaticVarValueScopes().enter()
let ty_fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
@@ -449,15 +506,19 @@ import TreeSitterP4
])
let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields)
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4QualifiedType(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: P4QualifiedType(ts_struct_type))
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ts"), withValue: (P4QualifiedType(ts_struct_type), .none))
#expect(
#RequireErrorResult(
Error(
withMessage: "{49, 20}: Failed to parse a statement element: {49, 11}: Cannot assign value of type Boolean to field count of type Int (width: Infinite)"
withMessage:
"{49, 20}: Failed to parse a statement element: {49, 11}: Cannot assign value of type Boolean to field count of type Int (width: Infinite)"
),
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
)
@@ -475,20 +536,21 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
var test_declarations = StaticVarValueScopes().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))
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ts"), withValue: (P4QualifiedType(struct_type), .none))
#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"
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))
)
}
+5 -5
View File
@@ -41,12 +41,12 @@ import TreeSitterP4
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(parser.states.count() == 1)
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_with_transition_select_expression_to_reject() async throws {
@@ -63,11 +63,11 @@ import TreeSitterP4
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(parser.states.count() == 1)
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_no_matching_key_transition() async throws {
@@ -82,7 +82,7 @@ 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.ParserValue>.create(program: program))
#expect(
#RequireErrorResult<(InstantiatedParserState, ProgramExecution)>(
+17 -17
View File
@@ -179,13 +179,13 @@ import TreeSitterP4
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
// 5w5 == specific_width_int == true
// true == true
// true
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_expression_in_declaration_initializer_specific_width_int_type2() async throws {
@@ -204,16 +204,15 @@ import TreeSitterP4
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
// 5w6 == specific_width_int == false
// false == false
// true
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_expression_in_declaration_initializer() async throws {
let simple_parser_declaration = """
parser main_parser() {
@@ -228,13 +227,13 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
// 5 == 5 == true
// true == true
// true
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_expression_in_declaration_initializer2() async throws {
@@ -251,13 +250,13 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
// 5 == 5 == true
// true == false
// false
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_expression_in_declaration_initializer_false() async throws {
@@ -274,13 +273,13 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
// 6 == 5 == true
// false == true
// false
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_expression_in_declaration_initializer_false2() async throws {
@@ -297,13 +296,13 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
// 6 == 5 == false
// false == false
// true
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_expression_in_declaration_initializer_invalid_types() async throws {
@@ -341,17 +340,18 @@ import TreeSitterP4
}
};
"""
var test_types = VarTypeScopes().enter()
test_types = test_types.declare(
var test_declarations = StaticVarValueScopes().enter()
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ta"),
withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
withValue: (P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))), .none))
#expect(
#RequireErrorResult(
Error(
withMessage:
"{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)))
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations)))
}
@Test func test_simple_compiler_parser_parameters_invalid_types() async throws {