From 61d8f601e8c580e4f35e5b63113c2e09f0f96936 Mon Sep 17 00:00:00 2001 From: Will Hawkins Date: Wed, 27 May 2026 05:41:23 -0400 Subject: [PATCH] compiler, language, runtime: Separate Parser Type From Instances 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 --- Sources/Common/Execution.swift | 6 + Sources/Common/Protocols.swift | 2 +- Sources/P4Compiler/Compiler.swift | 10 +- Sources/P4Compiler/Declarations.swift | 35 +- Sources/P4Compiler/Expression.swift | 3 +- Sources/P4Compiler/Parser.swift | 54 ++- Sources/P4Compiler/Program.swift | 24 +- Sources/P4Compiler/Statement.swift | 2 +- Sources/P4Lang/Parser.swift | 422 ++++++++++-------- Sources/P4Lang/Program.swift | 12 +- Sources/P4Runtime/CodeGen.swift | 17 +- Sources/P4Runtime/Expressions.swift | 14 +- Sources/P4Runtime/Parser.swift | 58 ++- Sources/P4Runtime/Runtime.swift | 9 +- Tests/p4rseTests/ArrayTests.swift | 189 +++++--- .../BinaryOperatorTests/AndOr.swift | 40 +- .../p4rseTests/BinaryOperatorTests/Bool.swift | 56 +-- .../BinaryOperatorTests/Integer.swift | 72 +-- .../BinaryOperatorTests/String.swift | 40 +- .../BinaryOperatorTests/Struct.swift | 35 +- Tests/p4rseTests/CodeGenTests/CodeGen.swift | 3 +- Tests/p4rseTests/ConditionalTests.swift | 8 +- Tests/p4rseTests/ControlTests/Compile.swift | 24 +- Tests/p4rseTests/ControlTests/Runtime.swift | 253 ++++++----- Tests/p4rseTests/Declarations.swift | 20 +- Tests/p4rseTests/ExpressionTests.swift | 12 +- .../ExpressionTests/FunctionCall.swift | 20 +- .../ExpressionTests/SelectExpression.swift | 28 +- Tests/p4rseTests/ExternDeclarations.swift | 8 +- Tests/p4rseTests/InterloperTests.swift | 10 +- Tests/p4rseTests/ParserCompilerTests.swift | 6 +- Tests/p4rseTests/RuntimeTests.swift | 20 +- Tests/p4rseTests/ScopeRuntimeTests.swift | 20 +- Tests/p4rseTests/StructTests.swift | 278 +++++++----- Tests/p4rseTests/TransitionTests.swift | 10 +- Tests/p4rseTests/ValueTypeParserTests.swift | 34 +- 36 files changed, 1058 insertions(+), 796 deletions(-) diff --git a/Sources/Common/Execution.swift b/Sources/Common/Execution.swift index a38b5f0..c985d09 100644 --- a/Sources/Common/Execution.swift +++ b/Sources/Common/Execution.swift @@ -257,6 +257,12 @@ public typealias VarValueScope = Scope /// Scopes that resolves variable identifiers to their values. public typealias VarValueScopes = Scopes +/// 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 diff --git a/Sources/Common/Protocols.swift b/Sources/Common/Protocols.swift index 9c25bf6..b9eb9d6 100644 --- a/Sources/Common/Protocols.swift +++ b/Sources/Common/Protocols.swift @@ -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 { diff --git a/Sources/P4Compiler/Compiler.swift b/Sources/P4Compiler/Compiler.swift index 4378cc2..a65786a 100644 --- a/Sources/P4Compiler/Compiler.swift +++ b/Sources/P4Compiler/Compiler.swift @@ -46,7 +46,7 @@ public func ConfigureP4Parser() -> Result { /// 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) diff --git a/Sources/P4Compiler/Declarations.swift b/Sources/P4Compiler/Declarations.swift index dc223cf..693d4c2 100644 --- a/Sources/P4Compiler/Declarations.swift +++ b/Sources/P4Compiler/Declarations.swift @@ -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( diff --git a/Sources/P4Compiler/Expression.swift b/Sources/P4Compiler/Expression.swift index ab98151..ae87c5a 100644 --- a/Sources/P4Compiler/Expression.swift +++ b/Sources/P4Compiler/Expression.swift @@ -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)) } } diff --git a/Sources/P4Compiler/Parser.swift b/Sources/P4Compiler/Parser.swift index 10f1e3d..6d59b75 100644 --- a/Sources/P4Compiler/Parser.swift +++ b/Sources/P4Compiler/Parser.swift @@ -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: 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"))) diff --git a/Sources/P4Compiler/Program.swift b/Sources/P4Compiler/Program.swift index d84d324..3584906 100644 --- a/Sources/P4Compiler/Program.swift +++ b/Sources/P4Compiler/Program.swift @@ -24,18 +24,30 @@ import TreeSitterP4 public struct Program { public static func Compile(_ source: String) -> Result { - 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 { 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 { @@ -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! diff --git a/Sources/P4Compiler/Statement.swift b/Sources/P4Compiler/Statement.swift index fda1d63..e7da607 100644 --- a/Sources/P4Compiler/Statement.swift +++ b/Sources/P4Compiler/Statement.swift @@ -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))) ) ) } diff --git a/Sources/P4Lang/Parser.swift b/Sources/P4Lang/Parser.swift index e56b353..7daef25 100644 --- a/Sources/P4Lang/Parser.swift +++ b/Sources/P4Lang/Parser.swift @@ -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() + } + } diff --git a/Sources/P4Lang/Program.swift b/Sources/P4Lang/Program.swift index 26f43df..55b9930 100644 --- a/Sources/P4Lang/Program.swift +++ b/Sources/P4Lang/Program.swift @@ -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 { - 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 { diff --git a/Sources/P4Runtime/CodeGen.swift b/Sources/P4Runtime/CodeGen.swift index 2e6e010..6cf17b7 100644 --- a/Sources/P4Runtime/CodeGen.swift +++ b/Sources/P4Runtime/CodeGen.swift @@ -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 + _ v: ParserState, _ c: VisitorContext ) -> Result> { let direct_transition_codegen = { ( @@ -135,14 +137,14 @@ public struct CodeGenerator: LanguageVisitor { ( state: ParserStateSelectTransition, c: VisitorContext ) -> Result> 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> = @@ -281,4 +283,9 @@ public struct CodeGenerator: LanguageVisitor { ) -> Result> { return .Ok(c) } + public func visit( + _ parser_state: P4Lang.InstantiatedParserState, _ c: P4Lang.VisitorContext + ) -> Common.Result> { + return .Ok(c) + } } diff --git a/Sources/P4Runtime/Expressions.swift b/Sources/P4Runtime/Expressions.swift index 8909ba9..7f2b83c 100644 --- a/Sources/P4Runtime/Expressions.swift +++ b/Sources/P4Runtime/Expressions.swift @@ -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: diff --git a/Sources/P4Runtime/Parser.swift b/Sources/P4Runtime/Parser.swift index 9dbc763..3263908 100644 --- a/Sources/P4Runtime/Parser.swift +++ b/Sources/P4Runtime/Parser.swift @@ -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): diff --git a/Sources/P4Runtime/Runtime.swift b/Sources/P4Runtime/Runtime.swift index 1c4573e..3d9129f 100644 --- a/Sources/P4Runtime/Runtime.swift +++ b/Sources/P4Runtime/Runtime.swift @@ -37,18 +37,19 @@ public struct Runtime>: CustomStringConvertible { /// Create a parser runtime from a P4 program public static func create( program: P4Lang.Program - ) -> Result> { + ) -> Result> { return Runtime.create(program: program, withGlobalValues: .none) } public static func create( program: P4Lang.Program, withGlobalValues initial: VarValueScopes? - ) -> Result> { + ) -> Result> { return switch program.starting_parser() { case .Ok(let parser): .Ok( - P4Runtime.Runtime( - callable: parser, withGlobalValues: initial)) + P4Runtime.Runtime( + callable: parser.instantiate(Identifier(name: "starting_parser")), + withGlobalValues: initial)) case .Error(let error): .Error(error) } } diff --git a/Tests/p4rseTests/ArrayTests.swift b/Tests/p4rseTests/ArrayTests.swift index 45ac2ff..42f4c7f 100644 --- a/Tests/p4rseTests/ArrayTests.swift +++ b/Tests/p4rseTests/ArrayTests.swift @@ -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.create(program: program, withGlobalValues: test_values)) + let runtime = try #UseOkResult( + P4Runtime.Runtime.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.create(program: program, withGlobalValues: test_values)) + let runtime = try #UseOkResult( + P4Runtime.Runtime.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.create(program: program, withGlobalValues: test_values)) + let runtime = try #UseOkResult( + P4Runtime.Runtime.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.create(program: program, withGlobalValues: test_values)) + let runtime = try #UseOkResult( + P4Runtime.Runtime.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.create(program: program, withGlobalValues: test_values)) + let runtime = try #UseOkResult( + P4Runtime.Runtime.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.create(program: program, withGlobalValues: test_values)) + let runtime = try #UseOkResult( + P4Runtime.Runtime.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.create(program: program, withGlobalValues: test_values)) + let runtime = try #UseOkResult( + P4Runtime.Runtime.create( + program: program, withGlobalValues: test_values)) let (state_result, _) = try! #UseOkResult(runtime.run()) - #expect(AsInstantiatedParserState(state_result) == P4Lang.accept) + #expect(state_result == P4Lang.accept) } diff --git a/Tests/p4rseTests/BinaryOperatorTests/AndOr.swift b/Tests/p4rseTests/BinaryOperatorTests/AndOr.swift index ecf610e..fc6a69f 100644 --- a/Tests/p4rseTests/BinaryOperatorTests/AndOr.swift +++ b/Tests/p4rseTests/BinaryOperatorTests/AndOr.swift @@ -42,10 +42,10 @@ import TreeSitterP4 """ let program = try #UseOkResult(Program.Compile(simple)) - let runtime = try #UseOkResult(P4Runtime.Runtime.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.create(program: program)) let (state_result, _) = try! #UseOkResult(runtime.run()) - #expect(AsInstantiatedParserState(state_result) == P4Lang.reject) + #expect(state_result == P4Lang.reject) } diff --git a/Tests/p4rseTests/BinaryOperatorTests/Bool.swift b/Tests/p4rseTests/BinaryOperatorTests/Bool.swift index 77ffe2b..4b9bc58 100644 --- a/Tests/p4rseTests/BinaryOperatorTests/Bool.swift +++ b/Tests/p4rseTests/BinaryOperatorTests/Bool.swift @@ -42,10 +42,10 @@ import TreeSitterP4 """ let program = try #UseOkResult(Program.Compile(simple)) - let runtime = try #UseOkResult(P4Runtime.Runtime.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.create(program: program)) let (state_result, _) = try! #UseOkResult(runtime.run()) - #expect(AsInstantiatedParserState(state_result) == P4Lang.reject) + #expect(state_result == P4Lang.reject) } diff --git a/Tests/p4rseTests/BinaryOperatorTests/Integer.swift b/Tests/p4rseTests/BinaryOperatorTests/Integer.swift index 6dff8c7..4869f83 100644 --- a/Tests/p4rseTests/BinaryOperatorTests/Integer.swift +++ b/Tests/p4rseTests/BinaryOperatorTests/Integer.swift @@ -42,10 +42,10 @@ import TreeSitterP4 """ let program = try #UseOkResult(Program.Compile(simple)) - let runtime = try #UseOkResult(P4Runtime.Runtime.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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 { diff --git a/Tests/p4rseTests/BinaryOperatorTests/String.swift b/Tests/p4rseTests/BinaryOperatorTests/String.swift index a8160ba..aa9a61d 100644 --- a/Tests/p4rseTests/BinaryOperatorTests/String.swift +++ b/Tests/p4rseTests/BinaryOperatorTests/String.swift @@ -42,10 +42,10 @@ import TreeSitterP4 """ let program = try #UseOkResult(Program.Compile(simple)) - let runtime = try #UseOkResult(P4Runtime.Runtime.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.create(program: program)) let (state_result, _) = try! #UseOkResult(runtime.run()) - #expect(AsInstantiatedParserState(state_result) == P4Lang.reject) + #expect(state_result == P4Lang.reject) } diff --git a/Tests/p4rseTests/BinaryOperatorTests/Struct.swift b/Tests/p4rseTests/BinaryOperatorTests/Struct.swift index 4dcf9b0..f6901be 100644 --- a/Tests/p4rseTests/BinaryOperatorTests/Struct.swift +++ b/Tests/p4rseTests/BinaryOperatorTests/Struct.swift @@ -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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.create(program: program)) let (state_result, _) = try! #UseOkResult(runtime.run()) - #expect(AsInstantiatedParserState(state_result) == P4Lang.reject) + #expect(state_result == P4Lang.reject) } diff --git a/Tests/p4rseTests/CodeGenTests/CodeGen.swift b/Tests/p4rseTests/CodeGenTests/CodeGen.swift index 134fb82..33f3596 100644 --- a/Tests/p4rseTests/CodeGenTests/CodeGen.swift +++ b/Tests/p4rseTests/CodeGenTests/CodeGen.swift @@ -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()) } diff --git a/Tests/p4rseTests/ConditionalTests.swift b/Tests/p4rseTests/ConditionalTests.swift index cb2b68a..73b2308 100644 --- a/Tests/p4rseTests/ConditionalTests.swift +++ b/Tests/p4rseTests/ConditionalTests.swift @@ -46,10 +46,10 @@ import TreeSitterP4 """ let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) - let runtime = try #UseOkResult(P4Runtime.Runtime.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.create(program: program)) let (state_result, _) = try! #UseOkResult(runtime.run()) - #expect(AsInstantiatedParserState(state_result) == P4Lang.accept) + #expect(state_result == P4Lang.accept) } diff --git a/Tests/p4rseTests/ControlTests/Compile.swift b/Tests/p4rseTests/ControlTests/Compile.swift index cfd206a..251cbfb 100644 --- a/Tests/p4rseTests/ControlTests/Compile.swift +++ b/Tests/p4rseTests/ControlTests/Compile.swift @@ -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 { diff --git a/Tests/p4rseTests/ControlTests/Runtime.swift b/Tests/p4rseTests/ControlTests/Runtime.swift index 6418a83..1dbd318 100644 --- a/Tests/p4rseTests/ControlTests/Runtime.swift +++ b/Tests/p4rseTests/ControlTests/Runtime.swift @@ -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.create(control: control, withGlobalValues: global_values)) + P4Runtime.Runtime.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.create(control: control, withGlobalValues: global_values)) + P4Runtime.Runtime.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.create(control: control, withGlobalValues: global_values)) + P4Runtime.Runtime.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.create(control: control, withGlobalValues: global_values)) + P4Runtime.Runtime.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.create(control: control, withGlobalValues: global_values)) + P4Runtime.Runtime.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.create(control: control, withGlobalValues: global_values)) + P4Runtime.Runtime.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.create(control: control, withGlobalValues: next_global_values)) + P4Runtime.Runtime.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.create(control: control, withGlobalValues: global_values)) + P4Runtime.Runtime.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)))) -} \ No newline at end of file +} diff --git a/Tests/p4rseTests/Declarations.swift b/Tests/p4rseTests/Declarations.swift index 2dd9ca8..2e1be41 100644 --- a/Tests/p4rseTests/Declarations.swift +++ b/Tests/p4rseTests/Declarations.swift @@ -47,9 +47,9 @@ import TreeSitterP4 """ let program = try #UseOkResult( Program.Compile(simple_parser_declaration)) - let runtime = try #UseOkResult(P4Runtime.Runtime.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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 { diff --git a/Tests/p4rseTests/ExpressionTests.swift b/Tests/p4rseTests/ExpressionTests.swift index 7ac037b..fff30aa 100644 --- a/Tests/p4rseTests/ExpressionTests.swift +++ b/Tests/p4rseTests/ExpressionTests.swift @@ -41,10 +41,10 @@ import TreeSitterP4 """ let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) - let runtime = try #UseOkResult(P4Runtime.Runtime.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.create(program: program)) let (state_result, _) = try! #UseOkResult(runtime.run()) - #expect(AsInstantiatedParserState(state_result) == P4Lang.reject) + #expect(state_result == P4Lang.reject) } diff --git a/Tests/p4rseTests/ExpressionTests/FunctionCall.swift b/Tests/p4rseTests/ExpressionTests/FunctionCall.swift index 81b4249..9d8f867 100644 --- a/Tests/p4rseTests/ExpressionTests/FunctionCall.swift +++ b/Tests/p4rseTests/ExpressionTests/FunctionCall.swift @@ -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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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 { diff --git a/Tests/p4rseTests/ExpressionTests/SelectExpression.swift b/Tests/p4rseTests/ExpressionTests/SelectExpression.swift index 7d10304..f10b158 100644 --- a/Tests/p4rseTests/ExpressionTests/SelectExpression.swift +++ b/Tests/p4rseTests/ExpressionTests/SelectExpression.swift @@ -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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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) } \ No newline at end of file diff --git a/Tests/p4rseTests/ExternDeclarations.swift b/Tests/p4rseTests/ExternDeclarations.swift index 9830570..3f16d0d 100644 --- a/Tests/p4rseTests/ExternDeclarations.swift +++ b/Tests/p4rseTests/ExternDeclarations.swift @@ -89,10 +89,10 @@ public struct Return6: P4FFI { simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: .none, withFFIs: [externally])) - let runtime = try #UseOkResult(P4Runtime.Runtime.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.create(program: program)) let (state_result, _) = try! #UseOkResult(runtime.run()) - #expect(AsInstantiatedParserState(state_result) == P4Lang.reject) + #expect(state_result == P4Lang.reject) } diff --git a/Tests/p4rseTests/InterloperTests.swift b/Tests/p4rseTests/InterloperTests.swift index 4fd8dc9..6dbf4bb 100644 --- a/Tests/p4rseTests/InterloperTests.swift +++ b/Tests/p4rseTests/InterloperTests.swift @@ -47,7 +47,7 @@ import TreeSitterP4 """ let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) - let runtime = try #UseOkResult(P4Runtime.Runtime.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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")) diff --git a/Tests/p4rseTests/ParserCompilerTests.swift b/Tests/p4rseTests/ParserCompilerTests.swift index 631bade..d328f1a 100644 --- a/Tests/p4rseTests/ParserCompilerTests.swift +++ b/Tests/p4rseTests/ParserCompilerTests.swift @@ -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) } diff --git a/Tests/p4rseTests/RuntimeTests.swift b/Tests/p4rseTests/RuntimeTests.swift index 63ba3a2..4984216 100644 --- a/Tests/p4rseTests/RuntimeTests.swift +++ b/Tests/p4rseTests/RuntimeTests.swift @@ -38,11 +38,11 @@ import TreeSitterP4 """ let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) - let runtime = try #UseOkResult(P4Runtime.Runtime.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.create(program: program)) let args = ArgumentList([Argument(P4Value(P4BooleanValue(withValue: true)), atIndex: 0)]) #expect( diff --git a/Tests/p4rseTests/ScopeRuntimeTests.swift b/Tests/p4rseTests/ScopeRuntimeTests.swift index f0f2d7f..99f36b8 100644 --- a/Tests/p4rseTests/ScopeRuntimeTests.swift +++ b/Tests/p4rseTests/ScopeRuntimeTests.swift @@ -43,10 +43,10 @@ import TreeSitterP4 let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) - let runtime = try #UseOkResult(P4Runtime.Runtime.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.create(program: program)) let (state_result, _) = try! #UseOkResult(runtime.run()) - #expect(AsInstantiatedParserState(state_result) == P4Lang.reject) + #expect(state_result == P4Lang.reject) } \ No newline at end of file diff --git a/Tests/p4rseTests/StructTests.swift b/Tests/p4rseTests/StructTests.swift index e7f78fc..931d6fd 100644 --- a/Tests/p4rseTests/StructTests.swift +++ b/Tests/p4rseTests/StructTests.swift @@ -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.create(program: program, withGlobalValues: test_values)) + let runtime = try #UseOkResult( + P4Runtime.Runtime.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.create(program: program)) + let program = try #UseOkResult( + Program.Compile( + simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types)) + let runtime = try #UseOkResult( + P4Runtime.Runtime.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.create(program: program)) + let program = try #UseOkResult( + Program.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_declarations)) + + let runtime = try #UseOkResult( + P4Runtime.Runtime.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.create(program: program, withGlobalValues: test_values)) + let runtime = try #UseOkResult( + P4Runtime.Runtime.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.create(program: program, withGlobalValues: test_values)) + let runtime = try #UseOkResult( + P4Runtime.Runtime.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.create(program: program, withGlobalValues: test_values)) + let runtime = try #UseOkResult( + P4Runtime.Runtime.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.create(program: program, withGlobalValues: test_values)) + let runtime = try #UseOkResult( + P4Runtime.Runtime.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.create(program: program, withGlobalValues: test_values)) + let runtime = try #UseOkResult( + P4Runtime.Runtime.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.create(program: program, withGlobalValues: test_values)) + let runtime = try #UseOkResult( + P4Runtime.Runtime.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.create(program: program, withGlobalValues: test_values)) + let runtime = try #UseOkResult( + P4Runtime.Runtime.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)) ) } - diff --git a/Tests/p4rseTests/TransitionTests.swift b/Tests/p4rseTests/TransitionTests.swift index d22ded7..ec9faee 100644 --- a/Tests/p4rseTests/TransitionTests.swift +++ b/Tests/p4rseTests/TransitionTests.swift @@ -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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.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.create(program: program)) + let runtime = try #UseOkResult(P4Runtime.Runtime.create(program: program)) #expect( #RequireErrorResult<(InstantiatedParserState, ProgramExecution)>( diff --git a/Tests/p4rseTests/ValueTypeParserTests.swift b/Tests/p4rseTests/ValueTypeParserTests.swift index 14820d8..12437b7 100644 --- a/Tests/p4rseTests/ValueTypeParserTests.swift +++ b/Tests/p4rseTests/ValueTypeParserTests.swift @@ -179,13 +179,13 @@ import TreeSitterP4 let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let runtime = try #UseOkResult( - P4Runtime.Runtime.create(program: program)) + P4Runtime.Runtime.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.create(program: program)) + P4Runtime.Runtime.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.create(program: program)) + P4Runtime.Runtime.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.create(program: program)) + P4Runtime.Runtime.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.create(program: program)) + P4Runtime.Runtime.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.create(program: program)) + P4Runtime.Runtime.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 {