Refactor Names of Scope Types
Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -15,5 +15,15 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
public typealias TypeScope = Scope<P4Type>
|
/// A scope that resolves variable identifiers to their types.
|
||||||
public typealias TypeScopes = Scopes<P4Type>
|
public typealias VarTypeScope = Scope<P4Type>
|
||||||
|
|
||||||
|
/// Scopes that resolve variable identifiers to their types.
|
||||||
|
public typealias VarTypeScopes = Scopes<P4Type>
|
||||||
|
|
||||||
|
/// A scope that resolves type identifiers to their types.
|
||||||
|
public typealias TypeTypeScope = Scope<P4Type>
|
||||||
|
|
||||||
|
/// Scopes that resolve type identifiers to their types.
|
||||||
|
public typealias TypeTypeScopes = Scopes<P4Type>
|
||||||
|
|
||||||
|
|||||||
@@ -16,8 +16,8 @@
|
|||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
open class ProgramExecution: CustomStringConvertible {
|
open class ProgramExecution: CustomStringConvertible {
|
||||||
public var scopes: ValueScopes = ValueScopes()
|
public var scopes: VarValueScopes = VarValueScopes()
|
||||||
let initialValues: ValueScopes?
|
let initialValues: VarValueScopes?
|
||||||
var error: Error?
|
var error: Error?
|
||||||
var debug: DebugLevel = DebugLevel.Error
|
var debug: DebugLevel = DebugLevel.Error
|
||||||
|
|
||||||
@@ -25,7 +25,7 @@ open class ProgramExecution: CustomStringConvertible {
|
|||||||
initialValues = .none
|
initialValues = .none
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(withGlobalValues values: ValueScopes) {
|
public init(withGlobalValues values: VarValueScopes) {
|
||||||
initialValues = values
|
initialValues = values
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,10 +88,13 @@ open class ProgramExecution: CustomStringConvertible {
|
|||||||
return new_pe
|
return new_pe
|
||||||
}
|
}
|
||||||
|
|
||||||
public func initial_values() -> ValueScopes? {
|
public func initial_values() -> VarValueScopes? {
|
||||||
return self.initialValues
|
return self.initialValues
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public typealias ValueScope = Scope<P4Value>
|
/// A scope that resolves variable identifiers to their values.
|
||||||
public typealias ValueScopes = Scopes<P4Value>
|
public typealias VarValueScope = Scope<P4Value>
|
||||||
|
|
||||||
|
/// Scopes that resolves variable identifiers to their values.
|
||||||
|
public typealias VarValueScopes = Scopes<P4Value>
|
||||||
|
|||||||
@@ -42,15 +42,15 @@ public func ErrorOnNode(node: Node, withError error: String) -> Error {
|
|||||||
|
|
||||||
/// Context for compilation.
|
/// Context for compilation.
|
||||||
public struct CompilerContext {
|
public struct CompilerContext {
|
||||||
let names: LexicalScopes
|
let names: VarTypeScopes
|
||||||
let types: LexicalScopes
|
let types: TypeTypeScopes
|
||||||
|
|
||||||
public init(withNames _names: LexicalScopes) {
|
public init(withNames _names: VarTypeScopes) {
|
||||||
names = _names
|
names = _names
|
||||||
types = LexicalScopes()
|
types = TypeTypeScopes()
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(withNames _names: LexicalScopes, withTypes _types: LexicalScopes) {
|
public init(withNames _names: VarTypeScopes, withTypes _types: TypeTypeScopes) {
|
||||||
names = _names
|
names = _names
|
||||||
types = _types
|
types = _types
|
||||||
}
|
}
|
||||||
@@ -59,9 +59,9 @@ public struct CompilerContext {
|
|||||||
///
|
///
|
||||||
/// Create a new compiler context based on the current with the same types and new names.
|
/// Create a new compiler context based on the current with the same types and new names.
|
||||||
///
|
///
|
||||||
/// - Parameter names: a ``LexicalScopes`` with the updated names for the newly created compiler context.
|
/// - Parameter names: a ``TypeScopes`` with the updated names for the newly created compiler context.
|
||||||
/// - Returns: A new compiler context based on the current with the same types and new names.
|
/// - Returns: A new compiler context based on the current with the same types and new names.
|
||||||
public func update(newNames names: LexicalScopes) -> CompilerContext {
|
public func update(newNames names: VarTypeScopes) -> CompilerContext {
|
||||||
return CompilerContext(withNames: names, withTypes: self.types)
|
return CompilerContext(withNames: names, withTypes: self.types)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,9 +69,9 @@ public struct CompilerContext {
|
|||||||
///
|
///
|
||||||
/// Create a new compiler context based on the current with the same names and new types.
|
/// Create a new compiler context based on the current with the same names and new types.
|
||||||
///
|
///
|
||||||
/// - Parameter types: a ``LexicalScopes`` with the updated types for the newly created compiler context.
|
/// - Parameter types: a ``TypeScopes`` with the updated types for the newly created compiler context.
|
||||||
/// - Returns: A new compiler context based on the current with the same names and new types.
|
/// - Returns: A new compiler context based on the current with the same names and new types.
|
||||||
public func update(newTypes types: LexicalScopes) -> CompilerContext {
|
public func update(newTypes types: TypeTypeScopes) -> CompilerContext {
|
||||||
return CompilerContext(withNames: self.names, withTypes: types)
|
return CompilerContext(withNames: self.names, withTypes: types)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
// p4rse, Copyright 2026, Will Hawkins
|
|
||||||
//
|
|
||||||
// This file is part of p4rse.
|
|
||||||
//
|
|
||||||
// This file is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// This program is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU General Public License
|
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
import Common
|
|
||||||
|
|
||||||
public typealias LexicalScope = Scope<P4Type>
|
|
||||||
public typealias LexicalScopes = Scopes<P4Type>
|
|
||||||
@@ -24,11 +24,15 @@ import TreeSitterP4
|
|||||||
|
|
||||||
public struct Program {
|
public struct Program {
|
||||||
public static func Compile(_ source: String) -> Result<P4Lang.Program> {
|
public static func Compile(_ source: String) -> Result<P4Lang.Program> {
|
||||||
return Program.Compile(source, withGlobalTypes: .none)
|
return Program.Compile(source, withGlobalInstances: .none, withGlobalTypes: .none)
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func Compile(_ source: String, withGlobalInstances globalInstances: VarTypeScopes) -> Result<P4Lang.Program> {
|
||||||
|
return Program.Compile(source, withGlobalInstances: globalInstances, withGlobalTypes: .none)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func Compile(
|
public static func Compile(
|
||||||
_ source: String, withGlobalTypes globalTypes: LexicalScopes?
|
_ source: String, withGlobalInstances globalInstances: VarTypeScopes?, withGlobalTypes globalTypes: TypeTypeScopes?
|
||||||
) -> Result<P4Lang.Program> {
|
) -> Result<P4Lang.Program> {
|
||||||
|
|
||||||
let maybe_parser = ConfigureP4Parser()
|
let maybe_parser = ConfigureP4Parser()
|
||||||
@@ -47,13 +51,18 @@ public struct Program {
|
|||||||
var program = P4Lang.Program()
|
var program = P4Lang.Program()
|
||||||
|
|
||||||
// Set up a context for parsing.
|
// Set up a context for parsing.
|
||||||
var compilation_context = CompilerContext(withNames: LexicalScopes().enter())
|
var compilation_context = CompilerContext(withNames: VarTypeScopes().enter())
|
||||||
|
|
||||||
var errors: [Error] = Array()
|
var errors: [Error] = Array()
|
||||||
|
|
||||||
|
// If the caller gave any global instances, add them here.
|
||||||
|
if let globalInstances = globalInstances {
|
||||||
|
compilation_context = compilation_context.update(newNames: globalInstances)
|
||||||
|
}
|
||||||
|
|
||||||
// If the caller gave any global types, add them here.
|
// If the caller gave any global types, add them here.
|
||||||
if let globalTypes = globalTypes {
|
if let globalTypes = globalTypes {
|
||||||
compilation_context = compilation_context.update(newNames: globalTypes)
|
compilation_context = compilation_context.update(newTypes: globalTypes)
|
||||||
}
|
}
|
||||||
|
|
||||||
result?.rootNode?.enumerateNamedChildren { declaration_node in
|
result?.rootNode?.enumerateNamedChildren { declaration_node in
|
||||||
|
|||||||
@@ -52,36 +52,6 @@ extension SelectExpression: EvaluatableExpression {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension P4StringValue: EvaluatableExpression {
|
|
||||||
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
|
|
||||||
return .Ok(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension P4BooleanValue: EvaluatableExpression {
|
|
||||||
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
|
|
||||||
return .Ok(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension P4StructValue: EvaluatableExpression {
|
|
||||||
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
|
|
||||||
return .Ok(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension P4IntValue: EvaluatableExpression {
|
|
||||||
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
|
|
||||||
return .Ok(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension P4ArrayValue: EvaluatableExpression {
|
|
||||||
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
|
|
||||||
return .Ok(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Variables are evaluatable because they can be looked up by identifiers.
|
// Variables are evaluatable because they can be looked up by identifiers.
|
||||||
extension TypedIdentifier: EvaluatableExpression {
|
extension TypedIdentifier: EvaluatableExpression {
|
||||||
public func type() -> any Common.P4Type {
|
public func type() -> any Common.P4Type {
|
||||||
@@ -96,8 +66,8 @@ extension TypedIdentifier: EvaluatableExpression {
|
|||||||
// Variables are evaluatable because they can be looked up by identifiers.
|
// Variables are evaluatable because they can be looked up by identifiers.
|
||||||
extension TypedIdentifier: EvaluatableLValueExpression {
|
extension TypedIdentifier: EvaluatableLValueExpression {
|
||||||
public func set(
|
public func set(
|
||||||
to: any Common.P4Value, inScopes scopes: Common.ValueScopes, duringExecution execution: ProgramExecution
|
to: any Common.P4Value, inScopes scopes: Common.VarValueScopes, duringExecution execution: ProgramExecution
|
||||||
) -> Common.Result<(Common.ValueScopes, P4Value)> {
|
) -> Common.Result<(Common.VarValueScopes, P4Value)> {
|
||||||
if case .Error(let e) = scopes.lookup(identifier: self) {
|
if case .Error(let e) = scopes.lookup(identifier: self) {
|
||||||
return .Error(e)
|
return .Error(e)
|
||||||
}
|
}
|
||||||
@@ -105,7 +75,7 @@ extension TypedIdentifier: EvaluatableLValueExpression {
|
|||||||
return .Ok((scopes.set(identifier: self, withValue: to), to))
|
return .Ok((scopes.set(identifier: self, withValue: to), to))
|
||||||
}
|
}
|
||||||
|
|
||||||
public func check(to: any Common.EvaluatableExpression, inScopes scopes: Common.TypeScopes) -> Result<()> {
|
public func check(to: any Common.EvaluatableExpression, inScopes scopes: Common.VarTypeScopes) -> Result<()> {
|
||||||
guard case .Ok(let type) = scopes.lookup(identifier: self) else {
|
guard case .Ok(let type) = scopes.lookup(identifier: self) else {
|
||||||
return .Error(Error(withMessage: "Cannot assign to identifier not in scope"))
|
return .Error(Error(withMessage: "Cannot assign to identifier not in scope"))
|
||||||
}
|
}
|
||||||
@@ -175,8 +145,8 @@ extension ArrayAccessExpression: EvaluatableExpression {
|
|||||||
|
|
||||||
extension ArrayAccessExpression: EvaluatableLValueExpression {
|
extension ArrayAccessExpression: EvaluatableLValueExpression {
|
||||||
public func set(
|
public func set(
|
||||||
to: any Common.P4Value, inScopes scopes: Common.ValueScopes, duringExecution execution: ProgramExecution
|
to: any Common.P4Value, inScopes scopes: Common.VarValueScopes, duringExecution execution: ProgramExecution
|
||||||
) -> Common.Result<(Common.ValueScopes, P4Value)> {
|
) -> Common.Result<(Common.VarValueScopes, P4Value)> {
|
||||||
// For purposes of documentation, assume the field access expression we are evaluating is
|
// For purposes of documentation, assume the field access expression we are evaluating is
|
||||||
// (strct_id)[indexor] = new_value
|
// (strct_id)[indexor] = new_value
|
||||||
// where strct_id expands to
|
// where strct_id expands to
|
||||||
@@ -213,7 +183,7 @@ extension ArrayAccessExpression: EvaluatableLValueExpression {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func check(
|
public func check(
|
||||||
to: any Common.EvaluatableExpression, inScopes scopes: Common.TypeScopes
|
to: any Common.EvaluatableExpression, inScopes scopes: Common.VarTypeScopes
|
||||||
) -> Common.Result<()> {
|
) -> Common.Result<()> {
|
||||||
|
|
||||||
if !self.type.value_type().eq(rhs: to.type()) {
|
if !self.type.value_type().eq(rhs: to.type()) {
|
||||||
@@ -249,8 +219,8 @@ extension FieldAccessExpression: EvaluatableExpression {
|
|||||||
|
|
||||||
extension FieldAccessExpression: EvaluatableLValueExpression {
|
extension FieldAccessExpression: EvaluatableLValueExpression {
|
||||||
public func set(
|
public func set(
|
||||||
to: any Common.P4Value, inScopes scopes: Common.ValueScopes, duringExecution execution: ProgramExecution
|
to: any Common.P4Value, inScopes scopes: Common.VarValueScopes, duringExecution execution: ProgramExecution
|
||||||
) -> Common.Result<(Common.ValueScopes, P4Value)> {
|
) -> Common.Result<(Common.VarValueScopes, P4Value)> {
|
||||||
// For purposes of documentation, assume the field access expression we are evaluating is
|
// For purposes of documentation, assume the field access expression we are evaluating is
|
||||||
// (strct_id).field_id = new_field_value
|
// (strct_id).field_id = new_field_value
|
||||||
// where strct_id expands to
|
// where strct_id expands to
|
||||||
@@ -282,7 +252,7 @@ extension FieldAccessExpression: EvaluatableLValueExpression {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func check(
|
public func check(
|
||||||
to: any Common.EvaluatableExpression, inScopes scopes: Common.TypeScopes
|
to: any Common.EvaluatableExpression, inScopes scopes: Common.VarTypeScopes
|
||||||
) -> Common.Result<()> {
|
) -> Common.Result<()> {
|
||||||
|
|
||||||
if !self.field.type.eq(rhs:to.type()) {
|
if !self.field.type.eq(rhs:to.type()) {
|
||||||
|
|||||||
@@ -22,14 +22,14 @@ import P4Lang
|
|||||||
public struct ParserRuntime: CustomStringConvertible {
|
public struct ParserRuntime: CustomStringConvertible {
|
||||||
public var parser: Parser
|
public var parser: Parser
|
||||||
|
|
||||||
let initialValues: ValueScopes?
|
let initialValues: VarValueScopes?
|
||||||
|
|
||||||
init(parser: Parser) {
|
init(parser: Parser) {
|
||||||
self.parser = parser
|
self.parser = parser
|
||||||
self.initialValues = .none
|
self.initialValues = .none
|
||||||
}
|
}
|
||||||
|
|
||||||
init(parser: Parser, withInitialValues initial: ValueScopes?) {
|
init(parser: Parser, withInitialValues initial: VarValueScopes?) {
|
||||||
self.parser = parser
|
self.parser = parser
|
||||||
self.initialValues = initial
|
self.initialValues = initial
|
||||||
}
|
}
|
||||||
@@ -40,7 +40,7 @@ public struct ParserRuntime: CustomStringConvertible {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static func create(
|
public static func create(
|
||||||
program: P4Lang.Program, withInitialValues initial: ValueScopes?
|
program: P4Lang.Program, withInitialValues initial: VarValueScopes?
|
||||||
) -> Result<ParserRuntime> {
|
) -> Result<ParserRuntime> {
|
||||||
return switch program.starting_parser() {
|
return switch program.starting_parser() {
|
||||||
case .Ok(let parser):
|
case .Ok(let parser):
|
||||||
|
|||||||
@@ -39,16 +39,16 @@ import TreeSitterP4
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_types = LexicalScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
|
||||||
var test_values = ValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
identifier: Identifier(name: "ta"),
|
identifier: Identifier(name: "ta"),
|
||||||
withValue: P4ArrayValue(withType: P4Int(), withValue: [
|
withValue: P4ArrayValue(withType: P4Int(), withValue: [
|
||||||
P4IntValue(withValue: 1), P4IntValue(withValue: 2), P4IntValue(withValue: 3),
|
P4IntValue(withValue: 1), P4IntValue(withValue: 2), P4IntValue(withValue: 3),
|
||||||
]))
|
]))
|
||||||
let program = try #UseOkResult(
|
let program = try #UseOkResult(
|
||||||
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
|
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
||||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||||
|
|
||||||
@@ -67,14 +67,14 @@ import TreeSitterP4
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_types = LexicalScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4Int())
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Int())
|
||||||
#expect(
|
#expect(
|
||||||
#RequireErrorResult(
|
#RequireErrorResult(
|
||||||
Error(
|
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, withGlobalTypes: test_types))
|
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,16 +90,16 @@ import TreeSitterP4
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_types = LexicalScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
|
||||||
var test_values = ValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
identifier: Identifier(name: "ta"),
|
identifier: Identifier(name: "ta"),
|
||||||
withValue: P4ArrayValue(withType: P4Int(), withValue: [
|
withValue: P4ArrayValue(withType: P4Int(), withValue: [
|
||||||
P4IntValue(withValue: 1), P4IntValue(withValue: 2), P4IntValue(withValue: 3),
|
P4IntValue(withValue: 1), P4IntValue(withValue: 2), P4IntValue(withValue: 3),
|
||||||
]))
|
]))
|
||||||
let program = try #UseOkResult(
|
let program = try #UseOkResult(
|
||||||
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
|
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
||||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||||
|
|
||||||
@@ -117,16 +117,16 @@ import TreeSitterP4
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_types = LexicalScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
|
||||||
var test_values = ValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
identifier: Identifier(name: "ta"),
|
identifier: Identifier(name: "ta"),
|
||||||
withValue: P4ArrayValue(withType: P4Int(), withValue: [
|
withValue: P4ArrayValue(withType: P4Int(), withValue: [
|
||||||
P4IntValue(withValue: 1), P4IntValue(withValue: 2), P4IntValue(withValue: 3),
|
P4IntValue(withValue: 1), P4IntValue(withValue: 2), P4IntValue(withValue: 3),
|
||||||
]))
|
]))
|
||||||
let program = try #UseOkResult(
|
let program = try #UseOkResult(
|
||||||
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
|
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
||||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||||
|
|
||||||
@@ -144,16 +144,16 @@ import TreeSitterP4
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_types = LexicalScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
|
||||||
var test_values = ValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
identifier: Identifier(name: "ta"),
|
identifier: Identifier(name: "ta"),
|
||||||
withValue: P4ArrayValue(withType: P4Int(), withValue: [
|
withValue: P4ArrayValue(withType: P4Int(), withValue: [
|
||||||
P4IntValue(withValue: 1), P4IntValue(withValue: 2), P4IntValue(withValue: 3),
|
P4IntValue(withValue: 1), P4IntValue(withValue: 2), P4IntValue(withValue: 3),
|
||||||
]))
|
]))
|
||||||
let program = try #UseOkResult(
|
let program = try #UseOkResult(
|
||||||
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
|
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
||||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||||
|
|
||||||
@@ -172,9 +172,9 @@ import TreeSitterP4
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_types = LexicalScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Array(withValueType: P4Int())))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Array(withValueType: P4Int())))
|
||||||
var test_values = ValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
|
|
||||||
let nested = P4ArrayValue(
|
let nested = P4ArrayValue(
|
||||||
withType: P4Int(),
|
withType: P4Int(),
|
||||||
@@ -185,7 +185,7 @@ import TreeSitterP4
|
|||||||
withValue: P4ArrayValue(withType: P4Array(withValueType: P4Int()), withValue: [nested]))
|
withValue: P4ArrayValue(withType: P4Array(withValueType: P4Int()), withValue: [nested]))
|
||||||
|
|
||||||
let program = try #UseOkResult(
|
let program = try #UseOkResult(
|
||||||
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
|
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
||||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||||
|
|
||||||
@@ -205,16 +205,16 @@ import TreeSitterP4
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_types = LexicalScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
|
||||||
var test_values = ValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
identifier: Identifier(name: "ta"),
|
identifier: Identifier(name: "ta"),
|
||||||
withValue: P4ArrayValue(withType: P4Int(), withValue: [
|
withValue: P4ArrayValue(withType: P4Int(), withValue: [
|
||||||
P4IntValue(withValue: 1), P4IntValue(withValue: 2), P4IntValue(withValue: 3),
|
P4IntValue(withValue: 1), P4IntValue(withValue: 2), P4IntValue(withValue: 3),
|
||||||
]))
|
]))
|
||||||
let program = try #UseOkResult(
|
let program = try #UseOkResult(
|
||||||
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
|
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
||||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||||
|
|
||||||
@@ -234,9 +234,9 @@ import TreeSitterP4
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_types = LexicalScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Array(withValueType: P4Int())))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Array(withValueType: P4Int())))
|
||||||
var test_values = ValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
|
|
||||||
let nested = P4ArrayValue(
|
let nested = P4ArrayValue(
|
||||||
withType: P4Int(),
|
withType: P4Int(),
|
||||||
@@ -247,7 +247,7 @@ import TreeSitterP4
|
|||||||
withValue: P4ArrayValue(withType: P4Array(withValueType: P4Int()), withValue: [nested]))
|
withValue: P4ArrayValue(withType: P4Array(withValueType: P4Int()), withValue: [nested]))
|
||||||
|
|
||||||
let program = try #UseOkResult(
|
let program = try #UseOkResult(
|
||||||
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
|
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
||||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||||
|
|
||||||
|
|||||||
@@ -114,5 +114,5 @@ import P4Lang
|
|||||||
#RequireErrorResult<(EvaluatableStatement, CompilerContext)>(
|
#RequireErrorResult<(EvaluatableStatement, CompilerContext)>(
|
||||||
Error(withMessage: "{2, 154}: Did not find assignment statement"),
|
Error(withMessage: "{2, 154}: Did not find assignment statement"),
|
||||||
ParserAssignmentStatement.Compile( // Note: Calling ParserAssignmentStatement compilation directly.
|
ParserAssignmentStatement.Compile( // Note: Calling ParserAssignmentStatement compilation directly.
|
||||||
node: result.rootNode!, withContext: CompilerContext(withNames: LexicalScopes()))))
|
node: result.rootNode!, withContext: CompilerContext(withNames: VarTypeScopes()))))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ import TreeSitterP4
|
|||||||
@testable import P4Compiler
|
@testable import P4Compiler
|
||||||
|
|
||||||
@Test func test_scope() async throws {
|
@Test func test_scope() async throws {
|
||||||
let s = LexicalScope()
|
let s = VarTypeScope()
|
||||||
let s2 = s.declare(identifier: Identifier(name: "first"), withValue: P4Int())
|
let s2 = s.declare(identifier: Identifier(name: "first"), withValue: P4Int())
|
||||||
let found_first = try! #require(s2.lookup(identifier: Identifier(name: "first")))
|
let found_first = try! #require(s2.lookup(identifier: Identifier(name: "first")))
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ import TreeSitterP4
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test func test_scope_no_set() async throws {
|
@Test func test_scope_no_set() async throws {
|
||||||
var ss = LexicalScopes().enter()
|
var ss = VarTypeScopes().enter()
|
||||||
ss = ss.declare(identifier: Identifier(name: "first"), withValue: P4Int())
|
ss = ss.declare(identifier: Identifier(name: "first"), withValue: P4Int())
|
||||||
ss = ss.enter()
|
ss = ss.enter()
|
||||||
ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4Boolean())
|
ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4Boolean())
|
||||||
@@ -49,7 +49,7 @@ import TreeSitterP4
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test func test_scope_set() async throws {
|
@Test func test_scope_set() async throws {
|
||||||
var ss = LexicalScopes().enter()
|
var ss = VarTypeScopes().enter()
|
||||||
let id = Identifier(name: "first")
|
let id = Identifier(name: "first")
|
||||||
let id_type = P4Int()
|
let id_type = P4Int()
|
||||||
|
|
||||||
|
|||||||
@@ -39,15 +39,15 @@ import TreeSitterP4
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_types = LexicalScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
let fields = P4StructFields([
|
let fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
||||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||||
])
|
])
|
||||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||||
test_types = test_types.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
|
||||||
|
|
||||||
var test_values = ValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
identifier: Identifier(name: "ts"),
|
identifier: Identifier(name: "ts"),
|
||||||
withValue: P4StructValue(withType: struct_type, andInitializers: [
|
withValue: P4StructValue(withType: struct_type, andInitializers: [
|
||||||
@@ -56,7 +56,7 @@ import TreeSitterP4
|
|||||||
]))
|
]))
|
||||||
|
|
||||||
let program = try #UseOkResult(
|
let program = try #UseOkResult(
|
||||||
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
|
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
||||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||||
#expect(state_result == P4Lang.accept)
|
#expect(state_result == P4Lang.accept)
|
||||||
@@ -74,15 +74,15 @@ import TreeSitterP4
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_types = LexicalScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
let fields = P4StructFields([
|
let fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
||||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||||
])
|
])
|
||||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||||
test_types = test_types.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
|
||||||
|
|
||||||
var test_values = ValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
identifier: Identifier(name: "ts"),
|
identifier: Identifier(name: "ts"),
|
||||||
withValue: P4StructValue(withType: struct_type, andInitializers: [
|
withValue: P4StructValue(withType: struct_type, andInitializers: [
|
||||||
@@ -91,7 +91,7 @@ import TreeSitterP4
|
|||||||
]))
|
]))
|
||||||
|
|
||||||
let program = try #UseOkResult(
|
let program = try #UseOkResult(
|
||||||
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
|
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
||||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||||
#expect(state_result == P4Lang.reject)
|
#expect(state_result == P4Lang.reject)
|
||||||
@@ -109,15 +109,15 @@ import TreeSitterP4
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_types = LexicalScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
let fields = P4StructFields([
|
let fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
||||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||||
])
|
])
|
||||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||||
test_types = test_types.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
|
||||||
|
|
||||||
var test_values = ValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
identifier: Identifier(name: "ts"),
|
identifier: Identifier(name: "ts"),
|
||||||
withValue: P4StructValue(withType: struct_type, andInitializers: [
|
withValue: P4StructValue(withType: struct_type, andInitializers: [
|
||||||
@@ -126,7 +126,7 @@ import TreeSitterP4
|
|||||||
]))
|
]))
|
||||||
|
|
||||||
let program = try #UseOkResult(
|
let program = try #UseOkResult(
|
||||||
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
|
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
||||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||||
#expect(state_result == P4Lang.accept)
|
#expect(state_result == P4Lang.accept)
|
||||||
@@ -143,15 +143,15 @@ import TreeSitterP4
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_types = LexicalScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
let fields = P4StructFields([
|
let fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
||||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||||
])
|
])
|
||||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||||
test_types = test_types.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
|
||||||
|
|
||||||
var test_values = ValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
identifier: Identifier(name: "ts"),
|
identifier: Identifier(name: "ts"),
|
||||||
withValue: P4StructValue(withType: struct_type, andInitializers: [
|
withValue: P4StructValue(withType: struct_type, andInitializers: [
|
||||||
@@ -160,7 +160,7 @@ import TreeSitterP4
|
|||||||
]))
|
]))
|
||||||
|
|
||||||
let program = try #UseOkResult(
|
let program = try #UseOkResult(
|
||||||
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
|
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
||||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||||
#expect(state_result == P4Lang.reject)
|
#expect(state_result == P4Lang.reject)
|
||||||
@@ -178,7 +178,7 @@ import TreeSitterP4
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_types = LexicalScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
|
|
||||||
let ty_fields = P4StructFields([
|
let ty_fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
||||||
@@ -189,9 +189,9 @@ import TreeSitterP4
|
|||||||
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: ty_struct_type)])
|
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: ty_struct_type)])
|
||||||
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
|
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
|
||||||
|
|
||||||
test_types = test_types.declare(identifier: Identifier(name: "ts"), withValue: ts_struct_type)
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: ts_struct_type)
|
||||||
|
|
||||||
var test_values = ValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
|
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
identifier: Identifier(name: "ts"),
|
identifier: Identifier(name: "ts"),
|
||||||
@@ -206,7 +206,7 @@ import TreeSitterP4
|
|||||||
])
|
])
|
||||||
]))
|
]))
|
||||||
let program = try #UseOkResult(
|
let program = try #UseOkResult(
|
||||||
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
|
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
||||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||||
#expect(state_result == P4Lang.accept)
|
#expect(state_result == P4Lang.accept)
|
||||||
@@ -225,15 +225,15 @@ import TreeSitterP4
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_types = LexicalScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
let fields = P4StructFields([
|
let fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
||||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||||
])
|
])
|
||||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||||
test_types = test_types.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
|
||||||
|
|
||||||
var test_values = ValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
identifier: Identifier(name: "ts"),
|
identifier: Identifier(name: "ts"),
|
||||||
withValue: P4StructValue(withType: struct_type, andInitializers: [
|
withValue: P4StructValue(withType: struct_type, andInitializers: [
|
||||||
@@ -242,7 +242,7 @@ import TreeSitterP4
|
|||||||
]))
|
]))
|
||||||
|
|
||||||
let program = try #UseOkResult(
|
let program = try #UseOkResult(
|
||||||
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
|
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
||||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||||
#expect(state_result == P4Lang.accept)
|
#expect(state_result == P4Lang.accept)
|
||||||
@@ -257,20 +257,20 @@ import TreeSitterP4
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_types = LexicalScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
let fields = P4StructFields([
|
let fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
||||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||||
])
|
])
|
||||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||||
test_types = test_types.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
|
||||||
|
|
||||||
#expect(
|
#expect(
|
||||||
#RequireErrorResult(
|
#RequireErrorResult(
|
||||||
Error(
|
Error(
|
||||||
withMessage: "{49, 13}: Failed to parse a statement element: {49, 8}: Cannot assign value of type Int to field with type Boolean"
|
withMessage: "{49, 13}: Failed to parse a statement element: {49, 8}: Cannot assign value of type Int to field with type Boolean"
|
||||||
),
|
),
|
||||||
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
|
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -287,7 +287,7 @@ import TreeSitterP4
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_types = LexicalScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
|
|
||||||
let ty_fields = P4StructFields([
|
let ty_fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
||||||
@@ -298,9 +298,9 @@ import TreeSitterP4
|
|||||||
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: ty_struct_type)])
|
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: ty_struct_type)])
|
||||||
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
|
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
|
||||||
|
|
||||||
test_types = test_types.declare(identifier: Identifier(name: "ts"), withValue: ts_struct_type)
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: ts_struct_type)
|
||||||
|
|
||||||
var test_values = ValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
|
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
identifier: Identifier(name: "ts"),
|
identifier: Identifier(name: "ts"),
|
||||||
@@ -315,7 +315,7 @@ import TreeSitterP4
|
|||||||
])
|
])
|
||||||
]))
|
]))
|
||||||
let program = try #UseOkResult(
|
let program = try #UseOkResult(
|
||||||
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
|
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
||||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||||
#expect(state_result == P4Lang.accept)
|
#expect(state_result == P4Lang.accept)
|
||||||
@@ -335,7 +335,7 @@ import TreeSitterP4
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_types = LexicalScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
|
|
||||||
let ty_fields = P4StructFields([
|
let ty_fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
||||||
@@ -346,9 +346,9 @@ import TreeSitterP4
|
|||||||
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: ty_struct_type)])
|
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: ty_struct_type)])
|
||||||
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
|
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
|
||||||
|
|
||||||
test_types = test_types.declare(identifier: Identifier(name: "ts"), withValue: ts_struct_type)
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: ts_struct_type)
|
||||||
|
|
||||||
var test_values = ValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
|
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
identifier: Identifier(name: "ts"),
|
identifier: Identifier(name: "ts"),
|
||||||
@@ -363,7 +363,7 @@ import TreeSitterP4
|
|||||||
])
|
])
|
||||||
]))
|
]))
|
||||||
let program = try #UseOkResult(
|
let program = try #UseOkResult(
|
||||||
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
|
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||||
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
|
||||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||||
#expect(state_result == P4Lang.accept)
|
#expect(state_result == P4Lang.accept)
|
||||||
@@ -382,7 +382,7 @@ import TreeSitterP4
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_types = LexicalScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
|
|
||||||
let ty_fields = P4StructFields([
|
let ty_fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
|
||||||
@@ -393,14 +393,14 @@ import TreeSitterP4
|
|||||||
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: ty_struct_type)])
|
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: ty_struct_type)])
|
||||||
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
|
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
|
||||||
|
|
||||||
test_types = test_types.declare(identifier: Identifier(name: "ts"), withValue: ts_struct_type)
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: ts_struct_type)
|
||||||
|
|
||||||
#expect(
|
#expect(
|
||||||
#RequireErrorResult(
|
#RequireErrorResult(
|
||||||
Error(
|
Error(
|
||||||
withMessage: "{49, 20}: Failed to parse a statement element: {49, 11}: Cannot assign value of type Boolean to field with type Int"
|
withMessage: "{49, 20}: Failed to parse a statement element: {49, 11}: Cannot assign value of type Boolean to field with type Int"
|
||||||
),
|
),
|
||||||
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
|
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||||
)
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,10 +29,20 @@ import TreeSitterP4
|
|||||||
|
|
||||||
@Test func test_invalid_types() async throws {
|
@Test func test_invalid_types() async throws {
|
||||||
for invalid_type_name in ["boo", "str", "in"] {
|
for invalid_type_name in ["boo", "str", "in"] {
|
||||||
#expect(
|
let simple_parser_declaration = """
|
||||||
#RequireErrorResult(
|
parser main_parser() {
|
||||||
Error(withMessage: "Type name not recognized"),
|
state start {
|
||||||
Types.CompileBasicType(type: invalid_type_name)))
|
\(invalid_type_name) v = 1;
|
||||||
|
transition reject;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
""";
|
||||||
|
|
||||||
|
let err = Program.Compile(simple_parser_declaration)
|
||||||
|
guard case Result.Error(let e) = err else {
|
||||||
|
assert(false, "Expected an error, but had success")
|
||||||
|
}
|
||||||
|
#expect(e.msg.contains("Failed to parse a statement element: Could not parse a P4 type from \(invalid_type_name)"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -242,7 +252,7 @@ import TreeSitterP4
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_types = LexicalScopes().enter()
|
var test_types = VarTypeScopes().enter()
|
||||||
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
|
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
|
||||||
#expect(
|
#expect(
|
||||||
#RequireErrorResult(
|
#RequireErrorResult(
|
||||||
@@ -250,5 +260,5 @@ import TreeSitterP4
|
|||||||
withMessage:
|
withMessage:
|
||||||
"{49, 22}: Failed to parse a statement element: Cannot initialize where_to (with type Boolean) from rvalue with type Int"
|
"{49, 22}: Failed to parse a statement element: Cannot initialize where_to (with type Boolean) from rvalue with type Int"
|
||||||
),
|
),
|
||||||
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types)))
|
Program.Compile(simple_parser_declaration, withGlobalInstances: test_types)))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user