Compare commits
5 Commits
a84e778e79
...
3d4d26d07b
| Author | SHA1 | Date | |
|---|---|---|---|
| 3d4d26d07b | |||
| 168d48fa7c | |||
| b49ec104e9 | |||
| 5cfe5532a2 | |||
| 7c660b2b0c |
@@ -39,8 +39,8 @@ let p4_program_with_control_decl = """
|
|||||||
|
|
||||||
|
|
||||||
// snippet.include
|
// snippet.include
|
||||||
let flter = { (tipe: P4Type) -> Bool in
|
let flter = { (tipe: P4QualifiedType) -> Bool in
|
||||||
switch tipe.dataType(){
|
switch tipe.baseType(){
|
||||||
case let c as Control: c.name == "simple"
|
case let c as Control: c.name == "simple"
|
||||||
default: false
|
default: false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ let p4_program_with_struct_decl = """
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
// snippet.include
|
// snippet.include
|
||||||
let flter = { (tipe: P4DataType) -> Bool in
|
let flter = { (tipe: P4Type) -> Bool in
|
||||||
switch tipe {
|
switch tipe {
|
||||||
case let c as P4Struct: c.name == "agg"
|
case let c as P4Struct: c.name == "agg"
|
||||||
default: false
|
default: false
|
||||||
|
|||||||
@@ -21,10 +21,10 @@ public struct Parameter: CustomStringConvertible, Equatable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public var name: Identifier
|
public var name: Identifier
|
||||||
public var type: P4Type
|
public var type: P4QualifiedType
|
||||||
|
|
||||||
public init(
|
public init(
|
||||||
identifier: Identifier, withType type: P4Type
|
identifier: Identifier, withType type: P4QualifiedType
|
||||||
) {
|
) {
|
||||||
self.name = identifier
|
self.name = identifier
|
||||||
self.type = type
|
self.type = type
|
||||||
@@ -46,7 +46,7 @@ public struct Parameter: CustomStringConvertible, Equatable {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return arg_type.dataType().eq(rhs: self.type.dataType())
|
return arg_type.baseType().eq(rhs: self.type.baseType())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,13 +16,13 @@
|
|||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
/// A scope that resolves variable identifiers to their types.
|
/// A scope that resolves variable identifiers to their types.
|
||||||
public typealias VarTypeScope = Scope<P4Type>
|
public typealias VarTypeScope = Scope<P4QualifiedType>
|
||||||
|
|
||||||
/// Scopes that resolve variable identifiers to their types.
|
/// Scopes that resolve variable identifiers to their types.
|
||||||
public typealias VarTypeScopes = Scopes<P4Type>
|
public typealias VarTypeScopes = Scopes<P4QualifiedType>
|
||||||
|
|
||||||
/// A scope that resolves type identifiers to their types.
|
/// A scope that resolves type identifiers to their types.
|
||||||
public typealias TypeTypeScope = Scope<P4DataType>
|
public typealias TypeTypeScope = Scope<P4Type>
|
||||||
|
|
||||||
/// Scopes that resolve type identifiers to their types.
|
/// Scopes that resolve type identifiers to their types.
|
||||||
public typealias TypeTypeScopes = Scopes<P4DataType>
|
public typealias TypeTypeScopes = Scopes<P4Type>
|
||||||
|
|||||||
@@ -50,14 +50,14 @@ public class Identifier: CustomStringConvertible, Comparable, Hashable {
|
|||||||
|
|
||||||
/// A P4 identifier
|
/// A P4 identifier
|
||||||
public class TypedIdentifier: Identifier {
|
public class TypedIdentifier: Identifier {
|
||||||
public var type: P4Type
|
public var type: P4QualifiedType
|
||||||
|
|
||||||
public init(name: String, withType type: P4Type) {
|
public init(name: String, withType type: P4QualifiedType) {
|
||||||
self.type = type
|
self.type = type
|
||||||
super.init(name: name)
|
super.init(name: name)
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(id: Identifier, withType type: P4Type) {
|
public init(id: Identifier, withType type: P4QualifiedType) {
|
||||||
self.type = type
|
self.type = type
|
||||||
super.init(id: id)
|
super.init(id: id)
|
||||||
}
|
}
|
||||||
@@ -107,7 +107,7 @@ public struct P4StructFields: Sequence, CustomStringConvertible, Equatable {
|
|||||||
}.joined(separator: ",")
|
}.joined(separator: ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
public func get_field_type(_ field: Identifier) -> P4Type? {
|
public func get_field_type(_ field: Identifier) -> P4QualifiedType? {
|
||||||
if let found_field = self.fields.makeIterator().first(where: { current in
|
if let found_field = self.fields.makeIterator().first(where: { current in
|
||||||
return current.name == field.name
|
return current.name == field.name
|
||||||
}) {
|
}) {
|
||||||
@@ -135,7 +135,7 @@ public struct P4StructFields: Sequence, CustomStringConvertible, Equatable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// The type for a P4 struct
|
/// The type for a P4 struct
|
||||||
public struct P4Struct: P4DataType {
|
public struct P4Struct: P4Type {
|
||||||
|
|
||||||
public let name: Identifier
|
public let name: Identifier
|
||||||
public let fields: P4StructFields
|
public let fields: P4StructFields
|
||||||
@@ -154,7 +154,7 @@ public struct P4Struct: P4DataType {
|
|||||||
return "Struct \(self.name) with fields: \(self.fields)"
|
return "Struct \(self.name) with fields: \(self.fields)"
|
||||||
}
|
}
|
||||||
|
|
||||||
public func eq(rhs: P4DataType) -> Bool {
|
public func eq(rhs: P4Type) -> Bool {
|
||||||
return if let struct_rhs = rhs as? P4Struct {
|
return if let struct_rhs = rhs as? P4Struct {
|
||||||
struct_rhs.name == self.name
|
struct_rhs.name == self.name
|
||||||
} else {
|
} else {
|
||||||
@@ -162,14 +162,14 @@ public struct P4Struct: P4DataType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func def() -> any P4DataValue {
|
public func def() -> P4DataValue? {
|
||||||
return P4StructValue(withType: self)
|
return P4StructValue(withType: self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An instance of a P4 struct
|
/// An instance of a P4 struct
|
||||||
public class P4StructValue: P4DataValue {
|
public class P4StructValue: P4DataValue {
|
||||||
public func type() -> P4DataType {
|
public func type() -> P4Type {
|
||||||
return self.stype
|
return self.stype
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,7 +203,7 @@ public class P4StructValue: P4DataValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Now that we know that the field names match, do the values match?
|
// Now that we know that the field names match, do the values match?
|
||||||
if !op(left_field_value.dataValue(), right_field_value.dataValue()) {
|
if !op(left_field_value?.dataValue(), right_field_value?.dataValue()) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -297,19 +297,22 @@ public class P4StructValue: P4DataValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public let stype: P4Struct
|
public let stype: P4Struct
|
||||||
public let values: [P4Value]
|
public let values: [P4Value?]
|
||||||
|
|
||||||
public convenience init(withType type: P4Struct) {
|
public convenience init(withType type: P4Struct) {
|
||||||
self.init(withType: type, andInitializers: [])
|
self.init(withType: type, andInitializers: [])
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(withType type: P4Struct, andInitializers initializers: [P4Value?]) {
|
public init(withType type: P4Struct, andInitializers initializers: [P4Value?]) {
|
||||||
let values = zip(0..<type.fields.count(), type.fields.fields).map { (index, field) in
|
let values: [P4Value?] = zip(0..<type.fields.count(), type.fields.fields).map {
|
||||||
|
(index, field) in
|
||||||
// If there is an initializer for the field, then use it.
|
// If there is an initializer for the field, then use it.
|
||||||
if index < initializers.count, let initializer = initializers[index] {
|
if index < initializers.count, let initializer = initializers[index] {
|
||||||
initializer
|
initializer
|
||||||
} else {
|
} else {
|
||||||
// Otherwise, set a default!
|
// Otherwise, try to set a default!
|
||||||
|
// Note: If the field type does not have a default, then the value
|
||||||
|
// will be a none. Pretty cool!
|
||||||
field.type.def()
|
field.type.def()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -350,25 +353,25 @@ public class P4StructValue: P4DataValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A P4 boolean type
|
/// A P4 boolean type
|
||||||
public struct P4Boolean: P4DataType {
|
public struct P4Boolean: P4Type {
|
||||||
public init() {}
|
public init() {}
|
||||||
public var description: String {
|
public var description: String {
|
||||||
return "Boolean"
|
return "Boolean"
|
||||||
}
|
}
|
||||||
public func eq(rhs: P4DataType) -> Bool {
|
public func eq(rhs: P4Type) -> Bool {
|
||||||
return switch rhs {
|
return switch rhs {
|
||||||
case is P4Boolean: true
|
case is P4Boolean: true
|
||||||
default: false
|
default: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public func def() -> any P4DataValue {
|
public func def() -> P4DataValue? {
|
||||||
return P4BooleanValue(withValue: false)
|
return P4BooleanValue(withValue: false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An instance of a P4 boolean
|
/// An instance of a P4 boolean
|
||||||
public class P4BooleanValue: P4DataValue {
|
public class P4BooleanValue: P4DataValue {
|
||||||
public func type() -> any P4DataType {
|
public func type() -> any P4Type {
|
||||||
return P4Boolean()
|
return P4Boolean()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -422,26 +425,26 @@ public class P4BooleanValue: P4DataValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A P4 int type
|
/// A P4 int type
|
||||||
public struct P4Int: P4DataType {
|
public struct P4Int: P4Type {
|
||||||
public init() {}
|
public init() {}
|
||||||
|
|
||||||
public var description: String {
|
public var description: String {
|
||||||
return "Int"
|
return "Int"
|
||||||
}
|
}
|
||||||
public func eq(rhs: P4DataType) -> Bool {
|
public func eq(rhs: P4Type) -> Bool {
|
||||||
return switch rhs {
|
return switch rhs {
|
||||||
case is P4Int: true
|
case is P4Int: true
|
||||||
default: false
|
default: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public func def() -> any P4DataValue {
|
public func def() -> P4DataValue? {
|
||||||
return P4IntValue(withValue: 0)
|
return P4IntValue(withValue: 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An instance of a P4 integer
|
/// An instance of a P4 integer
|
||||||
public class P4IntValue: P4DataValue {
|
public class P4IntValue: P4DataValue {
|
||||||
public func type() -> P4DataType {
|
public func type() -> P4Type {
|
||||||
return P4Int()
|
return P4Int()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -495,24 +498,24 @@ public class P4IntValue: P4DataValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A P4 string type
|
/// A P4 string type
|
||||||
public struct P4String: P4DataType {
|
public struct P4String: P4Type {
|
||||||
public init() {}
|
public init() {}
|
||||||
public var description: String {
|
public var description: String {
|
||||||
return "String"
|
return "String"
|
||||||
}
|
}
|
||||||
public func eq(rhs: any P4DataType) -> Bool {
|
public func eq(rhs: any P4Type) -> Bool {
|
||||||
return switch rhs {
|
return switch rhs {
|
||||||
case is P4String: true
|
case is P4String: true
|
||||||
default: false
|
default: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public func def() -> any P4DataValue {
|
public func def() -> P4DataValue? {
|
||||||
return P4StringValue(withValue: "")
|
return P4StringValue(withValue: "")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// An instance of a P4 string
|
/// An instance of a P4 string
|
||||||
public class P4StringValue: P4DataValue {
|
public class P4StringValue: P4DataValue {
|
||||||
public func type() -> any P4DataType {
|
public func type() -> any P4Type {
|
||||||
return P4String()
|
return P4String()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -565,14 +568,14 @@ public class Packet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A P4 array type
|
/// A P4 array type
|
||||||
public struct P4Array: P4DataType {
|
public struct P4Array: P4Type {
|
||||||
public init(withValueType vtype: P4Type) {
|
public init(withValueType vtype: P4QualifiedType) {
|
||||||
self.vtype = vtype
|
self.vtype = vtype
|
||||||
}
|
}
|
||||||
|
|
||||||
let vtype: P4Type
|
let vtype: P4QualifiedType
|
||||||
|
|
||||||
public func value_type() -> P4Type {
|
public func value_type() -> P4QualifiedType {
|
||||||
return self.vtype
|
return self.vtype
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -580,28 +583,28 @@ public struct P4Array: P4DataType {
|
|||||||
return "Array"
|
return "Array"
|
||||||
}
|
}
|
||||||
|
|
||||||
public func eq(rhs: any P4DataType) -> Bool {
|
public func eq(rhs: any P4Type) -> Bool {
|
||||||
return switch rhs {
|
return switch rhs {
|
||||||
case is P4Array: true
|
case is P4Array: true
|
||||||
default: false
|
default: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func def() -> P4DataValue {
|
public func def() -> P4DataValue? {
|
||||||
return P4ArrayValue(withType: self.vtype, withValue: [])
|
return P4ArrayValue(withType: self.vtype, withValue: [])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An instance of a P4 array
|
/// An instance of a P4 array
|
||||||
public class P4ArrayValue: P4DataValue {
|
public class P4ArrayValue: P4DataValue {
|
||||||
public func type() -> any P4DataType {
|
public func type() -> any P4Type {
|
||||||
return P4Array(withValueType: self.vtype)
|
return P4Array(withValueType: self.vtype)
|
||||||
}
|
}
|
||||||
|
|
||||||
let value: [P4Value]
|
let value: [P4Value]
|
||||||
let vtype: P4Type
|
let vtype: P4QualifiedType
|
||||||
|
|
||||||
public init(withType type: P4Type, withValue value: [P4Value]) {
|
public init(withType type: P4QualifiedType, withValue value: [P4Value]) {
|
||||||
self.vtype = type
|
self.vtype = type
|
||||||
self.value = value
|
self.value = value
|
||||||
}
|
}
|
||||||
@@ -663,14 +666,14 @@ public class P4ArrayValue: P4DataValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A P4 set type
|
/// A P4 set type
|
||||||
public struct P4Set: P4DataType {
|
public struct P4Set: P4Type {
|
||||||
public init(withSetType stype: P4Type) {
|
public init(withSetType stype: P4QualifiedType) {
|
||||||
self.stype = stype
|
self.stype = stype
|
||||||
}
|
}
|
||||||
|
|
||||||
let stype: P4Type
|
let stype: P4QualifiedType
|
||||||
|
|
||||||
public func set_type() -> P4Type {
|
public func set_type() -> P4QualifiedType {
|
||||||
return self.stype
|
return self.stype
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -678,22 +681,25 @@ public struct P4Set: P4DataType {
|
|||||||
return "P4Set"
|
return "P4Set"
|
||||||
}
|
}
|
||||||
|
|
||||||
public func eq(rhs: any P4DataType) -> Bool {
|
public func eq(rhs: any P4Type) -> Bool {
|
||||||
return switch rhs {
|
return switch rhs {
|
||||||
// If rhs is a set type, then they are the same if the types in the set are the same.
|
// If rhs is a set type, then they are the same if the types in the set are the same.
|
||||||
case let srhs as P4Set: srhs.eq(rhs: self.stype.dataType())
|
case let srhs as P4Set: srhs.eq(rhs: self.stype.baseType())
|
||||||
default: false
|
default: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func def() -> P4DataValue {
|
public func def() -> P4DataValue? {
|
||||||
return P4SetValue(withValue: P4Value(self.stype.dataType().def(), self.stype))
|
if let base_type_default = self.stype.baseType().def() {
|
||||||
|
return P4SetValue(withValue: P4Value(base_type_default, self.stype))
|
||||||
|
}
|
||||||
|
return .none
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An instance of a P4 set
|
/// An instance of a P4 set
|
||||||
public class P4SetValue: P4DataValue {
|
public class P4SetValue: P4DataValue {
|
||||||
public func type() -> any P4DataType {
|
public func type() -> any P4Type {
|
||||||
return P4Set(withSetType: self.value.type())
|
return P4Set(withSetType: self.value.type())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -744,13 +750,13 @@ public class P4SetValue: P4DataValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public class P4SetDefaultValue: P4DataValue {
|
public class P4SetDefaultValue: P4DataValue {
|
||||||
public func type() -> P4DataType {
|
public func type() -> P4Type {
|
||||||
return P4Set(withSetType: self.stype)
|
return P4Set(withSetType: self.stype)
|
||||||
}
|
}
|
||||||
|
|
||||||
let stype: P4Type
|
let stype: P4QualifiedType
|
||||||
|
|
||||||
public init(withType type: P4Type) {
|
public init(withType type: P4QualifiedType) {
|
||||||
self.stype = type
|
self.stype = type
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -776,15 +782,15 @@ public class P4SetDefaultValue: P4DataValue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct P4HitMiss: P4DataType {
|
public struct P4HitMiss: P4Type {
|
||||||
public func eq(rhs: any P4DataType) -> Bool {
|
public func eq(rhs: any P4Type) -> Bool {
|
||||||
return switch rhs {
|
return switch rhs {
|
||||||
case is P4HitMiss: true
|
case is P4HitMiss: true
|
||||||
default: false
|
default: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func def() -> any P4DataValue {
|
public func def() -> P4DataValue? {
|
||||||
return P4TableHitMissValue.Miss
|
return P4TableHitMissValue.Miss
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -794,7 +800,7 @@ public struct P4HitMiss: P4DataType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public enum P4TableHitMissValue: P4DataValue, Equatable, Comparable, CustomStringConvertible {
|
public enum P4TableHitMissValue: P4DataValue, Equatable, Comparable, CustomStringConvertible {
|
||||||
public func type() -> any P4DataType {
|
public func type() -> any P4Type {
|
||||||
return P4HitMiss()
|
return P4HitMiss()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -152,7 +152,7 @@ public typealias ExpressionInterloper = (EvaluatableExpression, Result<P4Value>,
|
|||||||
open class ProgramExecution: CustomStringConvertible {
|
open class ProgramExecution: CustomStringConvertible {
|
||||||
public var scopes: VarValueScopes = VarValueScopes()
|
public var scopes: VarValueScopes = VarValueScopes()
|
||||||
var globalValues: VarValueScopes?
|
var globalValues: VarValueScopes?
|
||||||
var error: Error?
|
var error: (any Errorable)?
|
||||||
var debug: DebugLevel = DebugLevel.Error
|
var debug: DebugLevel = DebugLevel.Error
|
||||||
public let evaluator: ProgramExecutionEvaluator
|
public let evaluator: ProgramExecutionEvaluator
|
||||||
|
|
||||||
@@ -182,11 +182,11 @@ open class ProgramExecution: CustomStringConvertible {
|
|||||||
return self.error != nil
|
return self.error != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
public func getError() -> Error? {
|
public func getError() -> (any Errorable)? {
|
||||||
return self.error
|
return self.error
|
||||||
}
|
}
|
||||||
|
|
||||||
public func setError(error: Error) -> ProgramExecution {
|
public func setError(error: any Errorable) -> ProgramExecution {
|
||||||
let npe = ProgramExecution(copy: self)
|
let npe = ProgramExecution(copy: self)
|
||||||
npe.error = error
|
npe.error = error
|
||||||
return npe
|
return npe
|
||||||
|
|||||||
@@ -17,6 +17,6 @@
|
|||||||
|
|
||||||
public protocol P4FFI {
|
public protocol P4FFI {
|
||||||
func execute(execution: ProgramExecution) -> (ControlFlow, ProgramExecution)
|
func execute(execution: ProgramExecution) -> (ControlFlow, ProgramExecution)
|
||||||
func type() -> P4Type
|
func type() -> P4QualifiedType
|
||||||
func parameters() -> ParameterList
|
func parameters() -> ParameterList
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,27 +52,27 @@ public enum Direction: Equatable, CustomStringConvertible {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum P4TypeAttribute: Equatable {
|
public enum P4TypeQualifier: Equatable {
|
||||||
case Direction(Direction)
|
case Direction(Direction)
|
||||||
case Readonly // Not yet used -- here to keep Swift warnings at bay
|
case Readonly // Not yet used -- here to keep Swift warnings at bay
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct P4TypeAttributes: CustomStringConvertible {
|
public struct P4TypeQualifiers: CustomStringConvertible {
|
||||||
let _attributes: [P4TypeAttribute]
|
let _qualifiers: [P4TypeQualifier]
|
||||||
|
|
||||||
public init(_ attributes: [P4TypeAttribute]) {
|
public init(_ qualifiers: [P4TypeQualifier]) {
|
||||||
self._attributes = attributes
|
self._qualifiers = qualifiers
|
||||||
}
|
}
|
||||||
|
|
||||||
public func direction() -> Direction? {
|
public func direction() -> Direction? {
|
||||||
let result = _attributes.firstIndex { attribute in
|
let result = _qualifiers.firstIndex { attribute in
|
||||||
return switch attribute {
|
return switch attribute {
|
||||||
case .Direction(_): true
|
case .Direction(_): true
|
||||||
default: false
|
default: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result.flatMap { index in
|
return result.flatMap { index in
|
||||||
return switch _attributes[index] {
|
return switch _qualifiers[index] {
|
||||||
case .Direction(let d): d
|
case .Direction(let d): d
|
||||||
default: Optional<Direction>.none
|
default: Optional<Direction>.none
|
||||||
}
|
}
|
||||||
@@ -80,7 +80,7 @@ public struct P4TypeAttributes: CustomStringConvertible {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func readOnly() -> Bool {
|
public func readOnly() -> Bool {
|
||||||
return _attributes.contains { attribute in
|
return _qualifiers.contains { attribute in
|
||||||
return switch attribute {
|
return switch attribute {
|
||||||
case .Readonly: true
|
case .Readonly: true
|
||||||
default: false
|
default: false
|
||||||
@@ -88,45 +88,45 @@ public struct P4TypeAttributes: CustomStringConvertible {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func update(removeAttribute attributeToRemove: P4TypeAttribute) -> P4TypeAttributes {
|
public func update(removeAttribute attributeToRemove: P4TypeQualifier) -> P4TypeQualifiers {
|
||||||
var new_attributes = self._attributes
|
var new_attributes = self._qualifiers
|
||||||
new_attributes.removeAll { item in
|
new_attributes.removeAll { item in
|
||||||
return item == attributeToRemove
|
return item == attributeToRemove
|
||||||
}
|
}
|
||||||
return P4TypeAttributes(new_attributes)
|
return P4TypeQualifiers(new_attributes)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func update(addAttribute attributeToAdd: P4TypeAttribute) -> P4TypeAttributes {
|
public func update(addAttribute attributeToAdd: P4TypeQualifier) -> P4TypeQualifiers {
|
||||||
return P4TypeAttributes(self._attributes + [attributeToAdd])
|
return P4TypeQualifiers(self._qualifiers + [attributeToAdd])
|
||||||
}
|
}
|
||||||
|
|
||||||
public var description: String {
|
public var description: String {
|
||||||
return self._attributes.map { attribute in
|
return self._qualifiers.map { qualifier in
|
||||||
return "\(attribute)"
|
return "\(qualifier)"
|
||||||
}.joined(separator: ",")
|
}.joined(separator: ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func ReadOnly() -> P4TypeAttributes {
|
public static func ReadOnly() -> P4TypeQualifiers {
|
||||||
return P4TypeAttributes([P4TypeAttribute.Readonly])
|
return P4TypeQualifiers([P4TypeQualifier.Readonly])
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct P4Type: CustomStringConvertible {
|
public struct P4QualifiedType: CustomStringConvertible {
|
||||||
let _attributes: P4TypeAttributes
|
let _attributes: P4TypeQualifiers
|
||||||
let _data_type: P4DataType
|
let base_type: P4Type
|
||||||
|
|
||||||
public init(_ type: P4DataType, _ attributes: P4TypeAttributes = P4TypeAttributes([])) {
|
public init(_ base_type: P4Type, _ attributes: P4TypeQualifiers = P4TypeQualifiers([])) {
|
||||||
self._attributes = attributes
|
self._attributes = attributes
|
||||||
self._data_type = type
|
self.base_type = base_type
|
||||||
}
|
}
|
||||||
|
|
||||||
public func update(removeAttribute attribute: P4TypeAttribute) -> P4Type {
|
public func update(removeAttribute attribute: P4TypeQualifier) -> P4QualifiedType {
|
||||||
return P4Type(self._data_type, self._attributes.update(removeAttribute: attribute))
|
return P4QualifiedType(self.base_type, self._attributes.update(removeAttribute: attribute))
|
||||||
}
|
}
|
||||||
|
|
||||||
public func update(addAttribute attribute: P4TypeAttribute) -> P4Type {
|
public func update(addAttribute attribute: P4TypeQualifier) -> P4QualifiedType {
|
||||||
return P4Type(self._data_type, self._attributes.update(addAttribute: attribute))
|
return P4QualifiedType(self.base_type, self._attributes.update(addAttribute: attribute))
|
||||||
}
|
}
|
||||||
|
|
||||||
public func direction() -> Direction? {
|
public func direction() -> Direction? {
|
||||||
@@ -137,17 +137,20 @@ public struct P4Type: CustomStringConvertible {
|
|||||||
return self._attributes.readOnly()
|
return self._attributes.readOnly()
|
||||||
}
|
}
|
||||||
|
|
||||||
public func dataType() -> P4DataType {
|
public func baseType() -> P4Type {
|
||||||
return self._data_type
|
return self.base_type
|
||||||
}
|
}
|
||||||
|
|
||||||
public func def() -> P4Value {
|
public func def() -> P4Value? {
|
||||||
return P4Value(self._data_type.def(), self)
|
if let default_value = self.base_type.def() {
|
||||||
|
return P4Value(default_value, self)
|
||||||
|
}
|
||||||
|
return .none
|
||||||
}
|
}
|
||||||
|
|
||||||
public func eq(_ rhs: P4Type) -> Bool {
|
public func eq(_ rhs: P4QualifiedType) -> Bool {
|
||||||
return self.direction() == rhs.direction() && self.readOnly() == self.readOnly()
|
return self.direction() == rhs.direction() && self.readOnly() == self.readOnly()
|
||||||
&& self.dataType().eq(rhs: rhs.dataType())
|
&& self.baseType().eq(rhs: rhs.baseType())
|
||||||
}
|
}
|
||||||
|
|
||||||
public func assignable() -> TypeCheckResults {
|
public func assignable() -> TypeCheckResults {
|
||||||
@@ -163,8 +166,8 @@ public struct P4Type: CustomStringConvertible {
|
|||||||
return TypeCheckResults.Ok
|
return TypeCheckResults.Ok
|
||||||
}
|
}
|
||||||
|
|
||||||
public func assignableFromType(_ rhs: P4Type) -> TypeCheckResults {
|
public func assignableFromType(_ rhs: P4QualifiedType) -> TypeCheckResults {
|
||||||
if !self.dataType().eq(rhs: rhs.dataType()) {
|
if !self.baseType().eq(rhs: rhs.baseType()) {
|
||||||
return TypeCheckResults.IncompatibleTypes
|
return TypeCheckResults.IncompatibleTypes
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,8 +184,8 @@ public struct P4Type: CustomStringConvertible {
|
|||||||
return TypeCheckResults.Ok
|
return TypeCheckResults.Ok
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func ReadOnly(_ type: P4DataType) -> P4Type {
|
public static func ReadOnly(_ type: P4Type) -> P4QualifiedType {
|
||||||
return P4Type(type, P4TypeAttributes.ReadOnly())
|
return P4QualifiedType(type, P4TypeQualifiers.ReadOnly())
|
||||||
}
|
}
|
||||||
|
|
||||||
public var description: String {
|
public var description: String {
|
||||||
@@ -190,17 +193,17 @@ public struct P4Type: CustomStringConvertible {
|
|||||||
if !attributes_description.isEmpty {
|
if !attributes_description.isEmpty {
|
||||||
attributes_description += " "
|
attributes_description += " "
|
||||||
}
|
}
|
||||||
return "\(attributes_description)\(self._data_type)"
|
return "\(attributes_description)\(self.base_type)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct P4Value: CustomStringConvertible {
|
public struct P4Value: CustomStringConvertible {
|
||||||
let _value: P4DataValue
|
let _value: P4DataValue
|
||||||
let _type: P4Type
|
let _type: P4QualifiedType
|
||||||
|
|
||||||
public init(_ value: P4DataValue, _ type: P4Type? = .none) {
|
public init(_ value: P4DataValue, _ type: P4QualifiedType? = .none) {
|
||||||
self._value = value
|
self._value = value
|
||||||
self._type = type != nil ? type! : P4Type(value.type())
|
self._type = type != nil ? type! : P4QualifiedType(value.type())
|
||||||
}
|
}
|
||||||
|
|
||||||
public func update(withNewValue value: P4DataValue) -> Result<P4Value> {
|
public func update(withNewValue value: P4DataValue) -> Result<P4Value> {
|
||||||
@@ -212,7 +215,7 @@ public struct P4Value: CustomStringConvertible {
|
|||||||
return "Value: \(self._value) of type \(self._type)"
|
return "Value: \(self._value) of type \(self._type)"
|
||||||
}
|
}
|
||||||
|
|
||||||
public func type() -> P4Type {
|
public func type() -> P4QualifiedType {
|
||||||
return self._type
|
return self._type
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ public protocol EvaluatableExpression {
|
|||||||
/// - execution: The execution context in which to evaluate the expression
|
/// - execution: The execution context in which to evaluate the expression
|
||||||
/// - Returns: The value of expression
|
/// - Returns: The value of expression
|
||||||
func evaluate(execution: ProgramExecution) -> (Result<P4Value>, ProgramExecution)
|
func evaluate(execution: ProgramExecution) -> (Result<P4Value>, ProgramExecution)
|
||||||
func type() -> P4Type
|
func type() -> P4QualifiedType
|
||||||
}
|
}
|
||||||
|
|
||||||
public protocol EvaluatableStatement {
|
public protocol EvaluatableStatement {
|
||||||
@@ -34,13 +34,13 @@ public protocol EvaluatableStatement {
|
|||||||
func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution)
|
func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution)
|
||||||
}
|
}
|
||||||
|
|
||||||
public protocol P4DataType: CustomStringConvertible {
|
public protocol P4Type: CustomStringConvertible {
|
||||||
func eq(rhs: any P4DataType) -> Bool
|
func eq(rhs: any P4Type) -> Bool
|
||||||
func def() -> P4DataValue
|
func def() -> P4DataValue?
|
||||||
}
|
}
|
||||||
|
|
||||||
public protocol P4DataValue: CustomStringConvertible {
|
public protocol P4DataValue: CustomStringConvertible {
|
||||||
func type() -> any P4DataType
|
func type() -> any P4Type
|
||||||
func eq(rhs: P4DataValue) -> Bool
|
func eq(rhs: P4DataValue) -> Bool
|
||||||
func lt(rhs: P4DataValue) -> Bool
|
func lt(rhs: P4DataValue) -> Bool
|
||||||
func lte(rhs: P4DataValue) -> Bool
|
func lte(rhs: P4DataValue) -> Bool
|
||||||
@@ -70,6 +70,19 @@ public protocol ProgramExecutionEvaluator {
|
|||||||
) -> (Result<P4Value>, ProgramExecution)
|
) -> (Result<P4Value>, ProgramExecution)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public protocol Errorable: CustomStringConvertible {
|
||||||
|
func format() -> String
|
||||||
|
func msg() -> String
|
||||||
|
func append(error: any Errorable) -> any Errorable
|
||||||
|
func eq(_ rhs: any Errorable) -> Bool
|
||||||
|
}
|
||||||
|
|
||||||
|
extension Errorable {
|
||||||
|
public func eq(_ rhs: any Errorable) -> Bool {
|
||||||
|
return self.msg() == rhs.msg()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
extension ProgramExecutionEvaluator {
|
extension ProgramExecutionEvaluator {
|
||||||
public func ExecuteStatements(
|
public func ExecuteStatements(
|
||||||
_ statements: [EvaluatableStatement], inExecution execution: ProgramExecution
|
_ statements: [EvaluatableStatement], inExecution execution: ProgramExecution
|
||||||
|
|||||||
@@ -0,0 +1,178 @@
|
|||||||
|
// 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/>.
|
||||||
|
|
||||||
|
public struct Error: Errorable, Equatable, CustomStringConvertible {
|
||||||
|
public func format() -> String {
|
||||||
|
return self.description
|
||||||
|
}
|
||||||
|
|
||||||
|
public func msg() -> String {
|
||||||
|
return self._msg
|
||||||
|
}
|
||||||
|
|
||||||
|
public func append(error: any Errorable) -> any Errorable {
|
||||||
|
return Errors(self, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
let _msg: String
|
||||||
|
|
||||||
|
public init(withMessage msg: String) {
|
||||||
|
self._msg = msg
|
||||||
|
}
|
||||||
|
|
||||||
|
public var description: String {
|
||||||
|
return self._msg
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct ErrorWithLocation: Errorable, Equatable, CustomStringConvertible {
|
||||||
|
|
||||||
|
public func format() -> String {
|
||||||
|
return self.description
|
||||||
|
}
|
||||||
|
|
||||||
|
public func msg() -> String {
|
||||||
|
return self.description
|
||||||
|
}
|
||||||
|
|
||||||
|
public func append(error: any Errorable) -> any Errorable {
|
||||||
|
return Errors(self, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
let _msg: String
|
||||||
|
|
||||||
|
let location: SourceLocation
|
||||||
|
|
||||||
|
public init(sourceLocation location: SourceLocation, withError msg: String) {
|
||||||
|
self._msg = msg
|
||||||
|
self.location = location
|
||||||
|
}
|
||||||
|
|
||||||
|
public var description: String {
|
||||||
|
return "\(self.location): \(self._msg)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct Errors: Errorable, CustomStringConvertible {
|
||||||
|
public func format() -> String {
|
||||||
|
return self.description
|
||||||
|
}
|
||||||
|
|
||||||
|
public func msg() -> String {
|
||||||
|
return self.description
|
||||||
|
}
|
||||||
|
|
||||||
|
public func append(error: any Errorable) -> any Errorable {
|
||||||
|
return Errors(self.errors + [error])
|
||||||
|
}
|
||||||
|
|
||||||
|
public var description: String {
|
||||||
|
return self.errors.map { error in
|
||||||
|
return error.msg()
|
||||||
|
}.joined(separator: ";")
|
||||||
|
}
|
||||||
|
|
||||||
|
public let errors: [any Errorable]
|
||||||
|
|
||||||
|
init(_ errors: [any Errorable]) {
|
||||||
|
self.errors = errors
|
||||||
|
}
|
||||||
|
|
||||||
|
public init(_ e1: any Errorable, _ e2: any Errorable) {
|
||||||
|
self.errors = [e1, e2]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct ErrorWithLabel: Errorable {
|
||||||
|
let label: String
|
||||||
|
let error: any Errorable
|
||||||
|
|
||||||
|
public init(_ label: String, _ error: any Errorable) {
|
||||||
|
self.label = label
|
||||||
|
self.error = error
|
||||||
|
}
|
||||||
|
|
||||||
|
public func format() -> String {
|
||||||
|
return self.description
|
||||||
|
}
|
||||||
|
|
||||||
|
public func msg() -> String {
|
||||||
|
return self.description
|
||||||
|
}
|
||||||
|
|
||||||
|
public func append(error: any Errorable) -> any Errorable {
|
||||||
|
return Errors(self, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
public var description: String {
|
||||||
|
return "\(self.label): \(self.error.msg())"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum Result<OKT>: Equatable {
|
||||||
|
case Ok(OKT)
|
||||||
|
case Error(any Errorable)
|
||||||
|
|
||||||
|
public static func == (lhs: Result, rhs: Result) -> Bool {
|
||||||
|
switch (lhs, rhs) {
|
||||||
|
case (Ok, Ok):
|
||||||
|
return true
|
||||||
|
case (Error(let le), Error(let re)):
|
||||||
|
return le.eq(re)
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public func ok() -> Bool {
|
||||||
|
switch self {
|
||||||
|
case .Ok(_): true
|
||||||
|
case .Error(_): false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public func error() -> (any Errorable)? {
|
||||||
|
if case Result.Error(let e) = self {
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
public func map<T>(block: (OKT) -> Result<T>) -> Result<T> {
|
||||||
|
switch self {
|
||||||
|
case .Ok(let ok): return block(ok)
|
||||||
|
case .Error(let e): return .Error(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public func map_err(block: (any Errorable) -> Result) -> Result {
|
||||||
|
switch self {
|
||||||
|
case .Ok(let ok): return .Ok(ok)
|
||||||
|
case .Error(let e): return block(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension Result: CustomStringConvertible where OKT: CustomStringConvertible {
|
||||||
|
public var description: String {
|
||||||
|
switch self {
|
||||||
|
case Result.Error(let e):
|
||||||
|
return e.format()
|
||||||
|
case Result.Ok(let o):
|
||||||
|
return "Ok: \(o)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,6 +15,20 @@
|
|||||||
// 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 struct SourceLocation: Equatable, CustomStringConvertible {
|
||||||
|
public let start: Int
|
||||||
|
public let extent: Int
|
||||||
|
|
||||||
|
public init(_ start: Int, _ extent: Int) {
|
||||||
|
self.start = start
|
||||||
|
self.extent = extent
|
||||||
|
}
|
||||||
|
|
||||||
|
public var description: String {
|
||||||
|
return "{\(self.start), \(self.extent)}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public enum DebugLevel {
|
public enum DebugLevel {
|
||||||
case Trace
|
case Trace
|
||||||
case Verbose
|
case Verbose
|
||||||
@@ -61,18 +75,6 @@ public enum DebugLevel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct Error: Equatable, CustomStringConvertible {
|
|
||||||
public private(set) var msg: String
|
|
||||||
|
|
||||||
public init(withMessage msg: String) {
|
|
||||||
self.msg = msg
|
|
||||||
}
|
|
||||||
|
|
||||||
public var description: String {
|
|
||||||
return self.msg
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public struct Nothing: CustomStringConvertible {
|
public struct Nothing: CustomStringConvertible {
|
||||||
public var description: String {
|
public var description: String {
|
||||||
return "Nothing"
|
return "Nothing"
|
||||||
@@ -81,64 +83,19 @@ public struct Nothing: CustomStringConvertible {
|
|||||||
public init() {}
|
public init() {}
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum Result<OKT>: Equatable {
|
|
||||||
case Ok(OKT)
|
|
||||||
case Error(Error)
|
|
||||||
|
|
||||||
public static func == (lhs: Result, rhs: Result) -> Bool {
|
|
||||||
switch (lhs, rhs) {
|
|
||||||
case (Ok, Ok):
|
|
||||||
return true
|
|
||||||
case (Error(let le), Error(let re)):
|
|
||||||
return le.msg == re.msg
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public func ok() -> Bool {
|
|
||||||
switch self {
|
|
||||||
case .Ok(_): true
|
|
||||||
case .Error(_): false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public func error() -> Error? {
|
|
||||||
if case Result.Error(let e) = self {
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
public func map<T>(block: (OKT) -> Result<T>) -> Result<T> {
|
|
||||||
switch self {
|
|
||||||
case .Ok(let ok): return block(ok)
|
|
||||||
case .Error(let e): return .Error(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension Result: CustomStringConvertible where OKT: CustomStringConvertible {
|
|
||||||
public var description: String {
|
|
||||||
switch self {
|
|
||||||
case Result.Error(let e):
|
|
||||||
return e.msg
|
|
||||||
case Result.Ok(let o):
|
|
||||||
return "Ok: \(o)"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public func Map<T, U>(input: T, block: (T) -> U) -> U {
|
public func Map<T, U>(input: T, block: (T) -> U) -> U {
|
||||||
return block(input)
|
return block(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
@freestanding(expression) public macro RequireOkResult<T>(_: Result<T>) -> Bool =
|
@freestanding(expression) public macro RequireOkResult<T>(_: Result<T>) -> Bool =
|
||||||
#externalMacro(module: "Macros", type: "RequireResult")
|
#externalMacro(module: "Macros", type: "RequireResult")
|
||||||
@freestanding(expression) public macro RequireErrorResult<T>(_: Error, _: Result<T>) -> Bool =
|
@freestanding(expression) public macro RequireErrorResult<T>(
|
||||||
|
_: any Errorable, _: Result<T>
|
||||||
|
) -> Bool =
|
||||||
#externalMacro(module: "Macros", type: "RequireErrorResult")
|
#externalMacro(module: "Macros", type: "RequireErrorResult")
|
||||||
@freestanding(expression) public macro UseOkResult<T>(_: Result<T>) -> T =
|
@freestanding(expression) public macro UseOkResult<T>(_: Result<T>) -> T =
|
||||||
#externalMacro(module: "Macros", type: "UseOkResult")
|
#externalMacro(module: "Macros", type: "UseOkResult")
|
||||||
@freestanding(expression) public macro UseErrorResult<T>(_: Result<T>) -> Error =
|
@freestanding(expression) public macro UseErrorResult<T>(_: Result<T>) -> any Errorable =
|
||||||
#externalMacro(module: "Macros", type: "UseErrorResult")
|
#externalMacro(module: "Macros", type: "UseErrorResult")
|
||||||
@freestanding(codeItem) public macro RequireNodeType<N, T>(
|
@freestanding(codeItem) public macro RequireNodeType<N, T>(
|
||||||
node: N, type: String, nice_type_name: String
|
node: N, type: String, nice_type_name: String
|
||||||
|
|||||||
@@ -125,10 +125,14 @@ public struct RequireErrorResult: ExpressionMacro {
|
|||||||
{
|
{
|
||||||
let __expected_error = \(expected_error)
|
let __expected_error = \(expected_error)
|
||||||
let __actual_error = \(error_producer)
|
let __actual_error = \(error_producer)
|
||||||
if case Result.Error(__expected_error) = __actual_error {
|
if case Result.Error(let __found_error) = __actual_error {
|
||||||
|
if !__expected_error.eq(__found_error) {
|
||||||
|
print("Expected Error: \\(__expected_error) but got Error: \\(__found_error)")
|
||||||
|
return false
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
print("Expected Error: \\(__expected_error) but got Error: \\(__actual_error)")
|
print("Expected error, but got Ok")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@@ -156,7 +160,7 @@ public struct RequireNodeType: CodeItemMacro {
|
|||||||
"""
|
"""
|
||||||
if \(node_to_check).nodeType != \(expected_type) {
|
if \(node_to_check).nodeType != \(expected_type) {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
ErrorOnNode(node: \(node_to_check), withError: "\(raw: error_message)"))
|
ErrorWithLocation(sourceLocation: \(node_to_check).toSourceLocation(), withError: "\(raw: error_message)"))
|
||||||
}
|
}
|
||||||
""")
|
""")
|
||||||
]
|
]
|
||||||
@@ -198,7 +202,7 @@ public struct RequireNodesType: CodeItemMacro {
|
|||||||
"""
|
"""
|
||||||
if \(raw: ifs) {
|
if \(raw: ifs) {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
ErrorOnNode(node: \(node_to_check), withError: "\(raw: error_message)"))
|
ErrorWithLocation(sourceLocation: \(node_to_check).toSourceLocation(), withError: "\(raw: error_message)"))
|
||||||
}
|
}
|
||||||
""")
|
""")
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -42,8 +42,8 @@ func parameter_list_compiler(
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(ParameterList, CompilerContext)>.Error(
|
or: Result<(ParameterList, CompilerContext)>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing parameter list component")))
|
sourceLocation: node.toSourceLocation(), withError: "Missing parameter list component")))
|
||||||
|
|
||||||
if current_node?.nodeType == "parameter_list" {
|
if current_node?.nodeType == "parameter_list" {
|
||||||
switch parameter_list_compiler(node: current_node!, withContext: context) {
|
switch parameter_list_compiler(node: current_node!, withContext: context) {
|
||||||
@@ -57,8 +57,8 @@ func parameter_list_compiler(
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(ParameterList, CompilerContext)>.Error(
|
or: Result<(ParameterList, CompilerContext)>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing parameter list component")))
|
sourceLocation: node.toSourceLocation(), withError: "Missing parameter list component")))
|
||||||
|
|
||||||
// If this is a ')', we are done.
|
// If this is a ')', we are done.
|
||||||
if current_node?.text == ")" {
|
if current_node?.text == ")" {
|
||||||
@@ -73,8 +73,8 @@ func parameter_list_compiler(
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(ParameterList, CompilerContext)>.Error(
|
or: Result<(ParameterList, CompilerContext)>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing parameter list component")))
|
sourceLocation: node.toSourceLocation(), withError: "Missing parameter list component")))
|
||||||
|
|
||||||
// Otherwise, there should be one parameter left!
|
// Otherwise, there should be one parameter left!
|
||||||
switch Parameter.Compile(node: current_node!, withContext: context) {
|
switch Parameter.Compile(node: current_node!, withContext: context) {
|
||||||
@@ -101,15 +101,16 @@ extension ParameterList: Compilable {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(ParameterList, CompilerContext)>.Error(
|
or: Result<(ParameterList, CompilerContext)>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing '(' in parameter list component")))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Missing '(' in parameter list component")))
|
||||||
|
|
||||||
walker.next()
|
walker.next()
|
||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(ParameterList, CompilerContext)>.Error(
|
or: Result<(ParameterList, CompilerContext)>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing parameter list component")))
|
sourceLocation: node.toSourceLocation(), withError: "Missing parameter list component")))
|
||||||
|
|
||||||
return parameter_list_compiler(node: current_node!, withContext: context)
|
return parameter_list_compiler(node: current_node!, withContext: context)
|
||||||
}
|
}
|
||||||
@@ -131,8 +132,9 @@ extension Direction: Compilable {
|
|||||||
|
|
||||||
guard let parsed_direction = directions[direction_node.text!] else {
|
guard let parsed_direction = directions[direction_node.text!] else {
|
||||||
return .Error(
|
return .Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: direction_node, withError: "\(direction_node.text!) is not a valid direction"))
|
sourceLocation: direction_node.toSourceLocation(),
|
||||||
|
withError: "\(direction_node.text!) is not a valid direction"))
|
||||||
}
|
}
|
||||||
|
|
||||||
return .Ok((parsed_direction, context))
|
return .Ok((parsed_direction, context))
|
||||||
@@ -154,14 +156,15 @@ extension Parameter: Compilable {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(Parameter, CompilerContext)>.Error(
|
or: Result<(Parameter, CompilerContext)>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing parameter declaration component")))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Missing parameter declaration component")))
|
||||||
|
|
||||||
// Annotation?
|
// Annotation?
|
||||||
if current_node!.nodeType == "annotations" {
|
if current_node!.nodeType == "annotations" {
|
||||||
return .Error(
|
return .Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: current_node!,
|
sourceLocation: current_node!.toSourceLocation(),
|
||||||
withError: "Annotations in parameter declarations are not yet handled"))
|
withError: "Annotations in parameter declarations are not yet handled"))
|
||||||
// Will increment indexes here.
|
// Will increment indexes here.
|
||||||
}
|
}
|
||||||
@@ -169,8 +172,9 @@ extension Parameter: Compilable {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(Parameter, CompilerContext)>.Error(
|
or: Result<(Parameter, CompilerContext)>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing parameter declaration component")))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Missing parameter declaration component")))
|
||||||
|
|
||||||
var direction: Direction? = .none
|
var direction: Direction? = .none
|
||||||
// Direction?
|
// Direction?
|
||||||
@@ -188,13 +192,15 @@ extension Parameter: Compilable {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(Parameter, CompilerContext)>.Error(
|
or: Result<(Parameter, CompilerContext)>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing parameter declaration component")))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Missing parameter declaration component")))
|
||||||
|
|
||||||
if current_node!.nodeType != "typeRef" {
|
if current_node!.nodeType != "typeRef" {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Did not find type name for parameter declaration"))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Did not find type name for parameter declaration"))
|
||||||
}
|
}
|
||||||
|
|
||||||
guard
|
guard
|
||||||
@@ -208,13 +214,15 @@ extension Parameter: Compilable {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(Parameter, CompilerContext)>.Error(
|
or: Result<(Parameter, CompilerContext)>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing parameter declaration component")))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Missing parameter declaration component")))
|
||||||
|
|
||||||
if current_node!.nodeType != "identifier" {
|
if current_node!.nodeType != "identifier" {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Did not find identifier for parameter statement"))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Did not find identifier for parameter statement"))
|
||||||
}
|
}
|
||||||
|
|
||||||
guard
|
guard
|
||||||
@@ -229,7 +237,7 @@ extension Parameter: Compilable {
|
|||||||
Parameter(
|
Parameter(
|
||||||
identifier: parameter_name,
|
identifier: parameter_name,
|
||||||
withType: direction != nil
|
withType: direction != nil
|
||||||
? parameter_type.update(addAttribute: P4TypeAttribute.Direction(direction!))
|
? parameter_type.update(addAttribute: P4TypeQualifier.Direction(direction!))
|
||||||
: parameter_type),
|
: parameter_type),
|
||||||
context
|
context
|
||||||
))
|
))
|
||||||
@@ -256,8 +264,8 @@ func argument_list_compiler(
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(ArgumentList, CompilerContext)>.Error(
|
or: Result<(ArgumentList, CompilerContext)>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing argument list component")))
|
sourceLocation: node.toSourceLocation(), withError: "Missing argument list component")))
|
||||||
|
|
||||||
if current_node?.nodeType == "argument_list" {
|
if current_node?.nodeType == "argument_list" {
|
||||||
switch argument_list_compiler(node: current_node!, withContext: context) {
|
switch argument_list_compiler(node: current_node!, withContext: context) {
|
||||||
@@ -273,8 +281,8 @@ func argument_list_compiler(
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(ArgumentList, CompilerContext)>.Error(
|
or: Result<(ArgumentList, CompilerContext)>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing argument list component")))
|
sourceLocation: node.toSourceLocation(), withError: "Missing argument list component")))
|
||||||
|
|
||||||
// If this is a ')', we are done.
|
// If this is a ')', we are done.
|
||||||
if current_node?.text == ")" {
|
if current_node?.text == ")" {
|
||||||
@@ -289,8 +297,8 @@ func argument_list_compiler(
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(ArgumentList, CompilerContext)>.Error(
|
or: Result<(ArgumentList, CompilerContext)>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing argument list component")))
|
sourceLocation: node.toSourceLocation(), withError: "Missing argument list component")))
|
||||||
|
|
||||||
// Otherwise, there should be one argument left!
|
// Otherwise, there should be one argument left!
|
||||||
switch Argument.Compile(node: current_node!, withContext: context) {
|
switch Argument.Compile(node: current_node!, withContext: context) {
|
||||||
@@ -317,16 +325,17 @@ extension ArgumentList: Compilable {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(ArgumentList, CompilerContext)>.Error(
|
or: Result<(ArgumentList, CompilerContext)>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing '(' in argument list component")))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Missing '(' in argument list component")))
|
||||||
|
|
||||||
walker.next()
|
walker.next()
|
||||||
|
|
||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(ArgumentList, CompilerContext)>.Error(
|
or: Result<(ArgumentList, CompilerContext)>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing argument list component")))
|
sourceLocation: node.toSourceLocation(), withError: "Missing argument list component")))
|
||||||
|
|
||||||
return argument_list_compiler(node: current_node!, withContext: context)
|
return argument_list_compiler(node: current_node!, withContext: context)
|
||||||
}
|
}
|
||||||
@@ -372,3 +381,9 @@ func ContainsInvalidStatements(block: BlockStatement, invalids: [EvaluatableStat
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension Node {
|
||||||
|
public func toSourceLocation() -> SourceLocation {
|
||||||
|
return SourceLocation(self.range.location, self.range.length)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -36,10 +36,6 @@ public func ConfigureP4Parser() -> Result<SwiftTreeSitter.Parser> {
|
|||||||
return .Ok(p)
|
return .Ok(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func ErrorOnNode(node: Node, withError error: String) -> Error {
|
|
||||||
return Error(withMessage: "\(node.range): \(error)")
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Context for compilation
|
/// Context for compilation
|
||||||
///
|
///
|
||||||
/// It contains (at least) three important pieces of information:
|
/// It contains (at least) three important pieces of information:
|
||||||
@@ -54,7 +50,7 @@ public struct CompilerContext {
|
|||||||
let types: TypeTypeScopes
|
let types: TypeTypeScopes
|
||||||
let externs: TypeTypeScopes
|
let externs: TypeTypeScopes
|
||||||
let ffis: [P4FFI]
|
let ffis: [P4FFI]
|
||||||
let expected_type: P4Type?
|
let expected_type: P4QualifiedType?
|
||||||
let extern_context: Bool
|
let extern_context: Bool
|
||||||
|
|
||||||
public init() {
|
public init() {
|
||||||
@@ -77,7 +73,7 @@ public struct CompilerContext {
|
|||||||
|
|
||||||
public init(
|
public init(
|
||||||
withInstances _instances: VarTypeScopes, withTypes _types: TypeTypeScopes,
|
withInstances _instances: VarTypeScopes, withTypes _types: TypeTypeScopes,
|
||||||
withExpectation expectation: P4Type?, withExtern extern: Bool,
|
withExpectation expectation: P4QualifiedType?, withExtern extern: Bool,
|
||||||
withExterns externs: TypeTypeScopes, withFFIs foreigns: [P4FFI]
|
withExterns externs: TypeTypeScopes, withFFIs foreigns: [P4FFI]
|
||||||
) {
|
) {
|
||||||
instances = _instances
|
instances = _instances
|
||||||
@@ -118,7 +114,7 @@ public struct CompilerContext {
|
|||||||
///
|
///
|
||||||
/// - Parameter expectation: a ``P4Type?`` to (re)set the type the compiler is expecting.
|
/// - Parameter expectation: a ``P4Type?`` to (re)set the type the compiler is expecting.
|
||||||
/// - Returns: A new compiler context based on the current but new expected type.
|
/// - Returns: A new compiler context based on the current but new expected type.
|
||||||
public func update(newExpectation expectation: P4Type?) -> CompilerContext {
|
public func update(newExpectation expectation: P4QualifiedType?) -> CompilerContext {
|
||||||
return CompilerContext(
|
return CompilerContext(
|
||||||
withInstances: self.instances, withTypes: self.types, withExpectation: expectation,
|
withInstances: self.instances, withTypes: self.types, withExpectation: expectation,
|
||||||
withExtern: self.extern_context, withExterns: self.externs, withFFIs: self.ffis)
|
withExtern: self.extern_context, withExterns: self.externs, withFFIs: self.ffis)
|
||||||
|
|||||||
@@ -60,8 +60,9 @@ extension FunctionDeclaration: CompilableDeclaration {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: function_declaration_node, withError: "Missing function declaration component")))
|
sourceLocation: function_declaration_node.toSourceLocation(),
|
||||||
|
withError: "Missing function declaration component")))
|
||||||
|
|
||||||
let maybe_function_type = Types.CompileType(type: current_node!, withContext: context)
|
let maybe_function_type = Types.CompileType(type: current_node!, withContext: context)
|
||||||
guard case .Ok(let function_type) = maybe_function_type else {
|
guard case .Ok(let function_type) = maybe_function_type else {
|
||||||
@@ -72,8 +73,9 @@ extension FunctionDeclaration: CompilableDeclaration {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: function_declaration_node, withError: "Missing function declaration component")))
|
sourceLocation: function_declaration_node.toSourceLocation(),
|
||||||
|
withError: "Missing function declaration component")))
|
||||||
|
|
||||||
let maybe_function_name = Identifier.Compile(node: current_node!, withContext: context)
|
let maybe_function_name = Identifier.Compile(node: current_node!, withContext: context)
|
||||||
guard case .Ok(let function_name) = maybe_function_name else {
|
guard case .Ok(let function_name) = maybe_function_name else {
|
||||||
@@ -84,8 +86,9 @@ extension FunctionDeclaration: CompilableDeclaration {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: function_declaration_node, withError: "Missing function declaration component")))
|
sourceLocation: function_declaration_node.toSourceLocation(),
|
||||||
|
withError: "Missing function declaration component")))
|
||||||
|
|
||||||
let maybe_function_parameters = ParameterList.Compile(node: current_node!, withContext: context)
|
let maybe_function_parameters = ParameterList.Compile(node: current_node!, withContext: context)
|
||||||
guard case .Ok((let function_parameters, let updated_context)) = maybe_function_parameters
|
guard case .Ok((let function_parameters, let updated_context)) = maybe_function_parameters
|
||||||
@@ -120,15 +123,16 @@ extension FunctionDeclaration: CompilableDeclaration {
|
|||||||
|
|
||||||
if !context.extern_context {
|
if !context.extern_context {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: function_declaration_node, withError: "Missing function declaration component"))
|
sourceLocation: function_declaration_node.toSourceLocation(),
|
||||||
|
withError: "Missing function declaration component"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let function_declaration = Declaration(
|
let function_declaration = Declaration(
|
||||||
TypedIdentifier(
|
TypedIdentifier(
|
||||||
id: function_name,
|
id: function_name,
|
||||||
withType: P4Type(
|
withType: P4QualifiedType(
|
||||||
FunctionDeclaration(
|
FunctionDeclaration(
|
||||||
named: function_name, ofType: function_type, withParameters: function_parameters,
|
named: function_name, ofType: function_type, withParameters: function_parameters,
|
||||||
withBody: function_body))))
|
withBody: function_body))))
|
||||||
@@ -144,7 +148,7 @@ extension FunctionDeclaration: CompilableDeclaration {
|
|||||||
? context
|
? context
|
||||||
: context.update(
|
: context.update(
|
||||||
newTypes: context.types.declare(
|
newTypes: context.types.declare(
|
||||||
identifier: function_name, withValue: function_declaration.identifier.type.dataType())
|
identifier: function_name, withValue: function_declaration.identifier.type.baseType())
|
||||||
)
|
)
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
@@ -165,8 +169,9 @@ extension P4Struct: CompilableDeclaration {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: currentNode, thing: walker.getNext(),
|
result: currentNode, thing: walker.getNext(),
|
||||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: struct_declaration_node, withError: "Missing function declaration component")))
|
sourceLocation: struct_declaration_node.toSourceLocation(),
|
||||||
|
withError: "Missing function declaration component")))
|
||||||
|
|
||||||
// The name of the struct type.
|
// The name of the struct type.
|
||||||
let maybe_struct_identifier = Identifier.Compile(
|
let maybe_struct_identifier = Identifier.Compile(
|
||||||
@@ -181,15 +186,17 @@ extension P4Struct: CompilableDeclaration {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: currentNode, thing: walker.getNext(),
|
result: currentNode, thing: walker.getNext(),
|
||||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: struct_declaration_node, withError: "Missing function declaration component")))
|
sourceLocation: struct_declaration_node.toSourceLocation(),
|
||||||
|
withError: "Missing function declaration component")))
|
||||||
|
|
||||||
// If there are no fields, it will be a "}"
|
// If there are no fields, it will be a "}"
|
||||||
if currentNode!.nodeType == "}" {
|
if currentNode!.nodeType == "}" {
|
||||||
let struc = Declaration(
|
let struc = Declaration(
|
||||||
TypedIdentifier(
|
TypedIdentifier(
|
||||||
id: struct_identifier,
|
id: struct_identifier,
|
||||||
withType: P4Type(P4Struct(withName: struct_identifier, andFields: P4StructFields([])))))
|
withType: P4QualifiedType(
|
||||||
|
P4Struct(withName: struct_identifier, andFields: P4StructFields([])))))
|
||||||
return Result.Ok(
|
return Result.Ok(
|
||||||
(
|
(
|
||||||
struc,
|
struc,
|
||||||
@@ -197,11 +204,11 @@ extension P4Struct: CompilableDeclaration {
|
|||||||
? context
|
? context
|
||||||
: context.update(
|
: context.update(
|
||||||
newTypes: context.types.declare(
|
newTypes: context.types.declare(
|
||||||
identifier: struct_identifier, withValue: struc.identifier.type.dataType()))
|
identifier: struct_identifier, withValue: struc.identifier.type.baseType()))
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
var parse_errs: [Error] = Array()
|
var parse_errs: (any Errorable)? = .none
|
||||||
var current_context = context
|
var current_context = context
|
||||||
var parsed_fields: [P4StructFieldIdentifier] = Array()
|
var parsed_fields: [P4StructFieldIdentifier] = Array()
|
||||||
|
|
||||||
@@ -217,24 +224,25 @@ extension P4Struct: CompilableDeclaration {
|
|||||||
id: variable_declaration.identifier, withType: variable_declaration.initializer.type()
|
id: variable_declaration.identifier, withType: variable_declaration.initializer.type()
|
||||||
))
|
))
|
||||||
current_context = updated_context
|
current_context = updated_context
|
||||||
case .Error(let e): parse_errs.append(e)
|
case .Error(let e):
|
||||||
|
parse_errs =
|
||||||
|
if let e = parse_errs {
|
||||||
|
parse_errs?.append(error: e)
|
||||||
|
} else {
|
||||||
|
e
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !parse_errs.isEmpty {
|
if let parse_errs = parse_errs {
|
||||||
return .Error(
|
return .Error(ErrorWithLabel("Error(s) parsing select cases", parse_errs))
|
||||||
Error(
|
|
||||||
withMessage: "Error(s) parsing select cases: "
|
|
||||||
+ (parse_errs.map { error in
|
|
||||||
return "\(error.msg)"
|
|
||||||
}.joined(separator: ";"))))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let declared_struct = Declaration(
|
let declared_struct = Declaration(
|
||||||
TypedIdentifier(
|
TypedIdentifier(
|
||||||
id: struct_identifier,
|
id: struct_identifier,
|
||||||
withType: P4Type(
|
withType: P4QualifiedType(
|
||||||
P4Struct(
|
P4Struct(
|
||||||
withName: struct_identifier, andFields: P4StructFields(parsed_fields)))))
|
withName: struct_identifier, andFields: P4StructFields(parsed_fields)))))
|
||||||
return .Ok(
|
return .Ok(
|
||||||
@@ -244,7 +252,7 @@ extension P4Struct: CompilableDeclaration {
|
|||||||
? current_context
|
? current_context
|
||||||
: current_context.update(
|
: current_context.update(
|
||||||
newTypes: current_context.types.declare(
|
newTypes: current_context.types.declare(
|
||||||
identifier: struct_identifier, withValue: declared_struct.identifier.type.dataType()))
|
identifier: struct_identifier, withValue: declared_struct.identifier.type.baseType()))
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -263,12 +271,15 @@ extension P4Lang.Parser: CompilableDeclaration {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: parser_node, withError: "Missing elements of parser declaration")))
|
sourceLocation: parser_node.toSourceLocation(),
|
||||||
|
withError: "Missing elements of parser declaration")))
|
||||||
|
|
||||||
if current_node!.nodeType != "parserType" {
|
if current_node!.nodeType != "parserType" {
|
||||||
return .Error(
|
return .Error(
|
||||||
ErrorOnNode(node: current_node!, withError: "Missing type for parser declaration"))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: current_node!.toSourceLocation(),
|
||||||
|
withError: "Missing type for parser declaration"))
|
||||||
}
|
}
|
||||||
|
|
||||||
let type_node = current_node
|
let type_node = current_node
|
||||||
@@ -284,13 +295,15 @@ extension P4Lang.Parser: CompilableDeclaration {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: type_node_child, thing: type_node_walker.getNext(),
|
result: type_node_child, thing: type_node_walker.getNext(),
|
||||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: parser_node, withError: "Missing elements of parser type in parser declaration")))
|
sourceLocation: parser_node.toSourceLocation(),
|
||||||
|
withError: "Missing elements of parser type in parser declaration")))
|
||||||
|
|
||||||
if type_node_child!.nodeType == "annotations" {
|
if type_node_child!.nodeType == "annotations" {
|
||||||
return .Error(
|
return .Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: type_node_child!, withError: "Annotations in parser type are not yet handled."))
|
sourceLocation: type_node_child!.toSourceLocation(),
|
||||||
|
withError: "Annotations in parser type are not yet handled."))
|
||||||
// Will increment indexes here.
|
// Will increment indexes here.
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -298,8 +311,9 @@ extension P4Lang.Parser: CompilableDeclaration {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: type_node_child, thing: type_node_walker.getNext(),
|
result: type_node_child, thing: type_node_walker.getNext(),
|
||||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: type_node_child!, withError: "Missing name in parser type declaration")))
|
sourceLocation: type_node_child!.toSourceLocation(),
|
||||||
|
withError: "Missing name in parser type declaration")))
|
||||||
|
|
||||||
switch Identifier.Compile(node: type_node_child!, withContext: current_context) {
|
switch Identifier.Compile(node: type_node_child!, withContext: current_context) {
|
||||||
case .Ok(let id): parser_name = id
|
case .Ok(let id): parser_name = id
|
||||||
@@ -311,8 +325,9 @@ extension P4Lang.Parser: CompilableDeclaration {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: type_node_child, thing: type_node_walker.getNext(),
|
result: type_node_child, thing: type_node_walker.getNext(),
|
||||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: type_node_child!, withError: "Missing parser parameters")))
|
sourceLocation: type_node_child!.toSourceLocation(),
|
||||||
|
withError: "Missing parser parameters")))
|
||||||
|
|
||||||
switch ParameterList.Compile(node: type_node_child!, withContext: current_context) {
|
switch ParameterList.Compile(node: type_node_child!, withContext: current_context) {
|
||||||
case .Ok(let (parsed_parameter_list, updated_context)):
|
case .Ok(let (parsed_parameter_list, updated_context)):
|
||||||
@@ -334,27 +349,32 @@ extension P4Lang.Parser: CompilableDeclaration {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: parser_node, withError: "Missing parser declaration component")))
|
sourceLocation: parser_node.toSourceLocation(),
|
||||||
|
withError: "Missing parser declaration component")))
|
||||||
|
|
||||||
walker.next()
|
walker.next()
|
||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: parser_node, withError: "Missing elements of parser declaration")))
|
sourceLocation: parser_node.toSourceLocation(),
|
||||||
|
withError: "Missing elements of parser declaration")))
|
||||||
|
|
||||||
if current_node!.nodeType == "parserLocalElements" {
|
if current_node!.nodeType == "parserLocalElements" {
|
||||||
return .Error(
|
return .Error(
|
||||||
ErrorOnNode(node: current_node!, withError: "Parser Local Elements are not yet handled."))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: current_node!.toSourceLocation(),
|
||||||
|
withError: "Parser Local Elements are not yet handled."))
|
||||||
// Will increment indexes here.
|
// Will increment indexes here.
|
||||||
}
|
}
|
||||||
|
|
||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: parser_node, withError: "Missing body of parser declaration")))
|
sourceLocation: parser_node.toSourceLocation(),
|
||||||
|
withError: "Missing body of parser declaration")))
|
||||||
|
|
||||||
if current_node!.nodeType != "parserStates" {
|
if current_node!.nodeType != "parserStates" {
|
||||||
return .Error(Error(withMessage: "Missing parser states in parser declaration"))
|
return .Error(Error(withMessage: "Missing parser states in parser declaration"))
|
||||||
@@ -366,7 +386,7 @@ extension P4Lang.Parser: CompilableDeclaration {
|
|||||||
{
|
{
|
||||||
case Result.Ok((let parser, let updated_context)):
|
case Result.Ok((let parser, let updated_context)):
|
||||||
let parser_declaration = Declaration(
|
let parser_declaration = Declaration(
|
||||||
TypedIdentifier(id: parser.name, withType: P4Type(parser)))
|
TypedIdentifier(id: parser.name, withType: P4QualifiedType(parser)))
|
||||||
// Create a new context with the name of the parser that was just compiled in scope.
|
// Create a new context with the name of the parser that was just compiled in scope.
|
||||||
return .Ok(
|
return .Ok(
|
||||||
(
|
(
|
||||||
@@ -398,8 +418,9 @@ extension Control: CompilableDeclaration {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing control declaration component")))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Missing control declaration component")))
|
||||||
|
|
||||||
guard
|
guard
|
||||||
case .Ok(let control_name) = Identifier.Compile(
|
case .Ok(let control_name) = Identifier.Compile(
|
||||||
@@ -413,8 +434,9 @@ extension Control: CompilableDeclaration {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing control declaration component")))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Missing control declaration component")))
|
||||||
|
|
||||||
let maybe_control_parameters = ParameterList.Compile(
|
let maybe_control_parameters = ParameterList.Compile(
|
||||||
node: current_node!, withContext: local_context)
|
node: current_node!, withContext: local_context)
|
||||||
@@ -438,8 +460,9 @@ extension Control: CompilableDeclaration {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing control declaration component")))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Missing control declaration component")))
|
||||||
|
|
||||||
var actions: [Action] = Array()
|
var actions: [Action] = Array()
|
||||||
var tables: [Table] = Array()
|
var tables: [Table] = Array()
|
||||||
@@ -489,7 +512,9 @@ extension Control: CompilableDeclaration {
|
|||||||
// But, that is handled by the compiler.
|
// But, that is handled by the compiler.
|
||||||
} else {
|
} else {
|
||||||
return .Error(
|
return .Error(
|
||||||
ErrorOnNode(node: current_node, withError: "Uknown node type in control declaration"))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: current_node.toSourceLocation(),
|
||||||
|
withError: "Uknown node type in control declaration"))
|
||||||
}
|
}
|
||||||
return .Ok(())
|
return .Ok(())
|
||||||
}
|
}
|
||||||
@@ -505,20 +530,24 @@ extension Control: CompilableDeclaration {
|
|||||||
/// IDEA: Add a "compilation context" for the error message into the `CompilationContext`
|
/// IDEA: Add a "compilation context" for the error message into the `CompilationContext`
|
||||||
// that can be retrieved to make the error messages nicer.
|
// that can be retrieved to make the error messages nicer.
|
||||||
return .Error(
|
return .Error(
|
||||||
ErrorOnNode(node: node, withError: "More than one table in control declaration"))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "More than one table in control declaration"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check to make sure that there is an apply.
|
// Check to make sure that there is an apply.
|
||||||
guard let apply = apply else {
|
guard let apply = apply else {
|
||||||
return .Error(
|
return .Error(
|
||||||
ErrorOnNode(node: node, withError: "Missing apply in control declaration"))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(), withError: "Missing apply in control declaration"
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
let declared_control =
|
let declared_control =
|
||||||
Declaration(
|
Declaration(
|
||||||
TypedIdentifier(
|
TypedIdentifier(
|
||||||
id: control_name,
|
id: control_name,
|
||||||
withType: P4Type(
|
withType: P4QualifiedType(
|
||||||
Control(
|
Control(
|
||||||
named: control_name, withParameters: control_parameters, withTable: tables[0],
|
named: control_name, withParameters: control_parameters, withTable: tables[0],
|
||||||
withActions: Actions(withActions: actions), withApply: apply))))
|
withActions: Actions(withActions: actions), withApply: apply))))
|
||||||
@@ -542,7 +571,7 @@ extension Action: Compilable {
|
|||||||
public static func Compile(
|
public static func Compile(
|
||||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||||
) -> Common.Result<(P4Lang.Action, CompilerContext)> {
|
) -> Common.Result<(P4Lang.Action, CompilerContext)> {
|
||||||
#RequireNodeType<Node, (P4DataType, CompilerContext)>(
|
#RequireNodeType<Node, (P4Type, CompilerContext)>(
|
||||||
node: node, type: "action_declaration", nice_type_name: "Action Declaration")
|
node: node, type: "action_declaration", nice_type_name: "Action Declaration")
|
||||||
|
|
||||||
var walker = Walker(node: node)
|
var walker = Walker(node: node)
|
||||||
@@ -555,7 +584,9 @@ extension Action: Compilable {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(P4Lang.Action, CompilerContext)>.Error(
|
or: Result<(P4Lang.Action, CompilerContext)>.Error(
|
||||||
ErrorOnNode(node: node, withError: "Missing action declaration component"))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(), withError: "Missing action declaration component"
|
||||||
|
))
|
||||||
)
|
)
|
||||||
|
|
||||||
guard
|
guard
|
||||||
@@ -570,7 +601,9 @@ extension Action: Compilable {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(P4Lang.Action, CompilerContext)>.Error(
|
or: Result<(P4Lang.Action, CompilerContext)>.Error(
|
||||||
ErrorOnNode(node: node, withError: "Missing action declaration component"))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(), withError: "Missing action declaration component"
|
||||||
|
))
|
||||||
)
|
)
|
||||||
|
|
||||||
let maybe_action_parameters = ParameterList.Compile(
|
let maybe_action_parameters = ParameterList.Compile(
|
||||||
@@ -585,7 +618,9 @@ extension Action: Compilable {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(P4Lang.Action, CompilerContext)>.Error(
|
or: Result<(P4Lang.Action, CompilerContext)>.Error(
|
||||||
ErrorOnNode(node: node, withError: "Missing action declaration component"))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(), withError: "Missing action declaration component"
|
||||||
|
))
|
||||||
)
|
)
|
||||||
|
|
||||||
// Add the parameters into scope.
|
// Add the parameters into scope.
|
||||||
@@ -619,7 +654,7 @@ extension TableKeyEntry: Compilable {
|
|||||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||||
) -> Common.Result<(P4Lang.TableKeyEntry, CompilerContext)> {
|
) -> Common.Result<(P4Lang.TableKeyEntry, CompilerContext)> {
|
||||||
|
|
||||||
#RequireNodeType<Node, (P4DataType, CompilerContext)>(
|
#RequireNodeType<Node, (P4Type, CompilerContext)>(
|
||||||
node: node, type: "table_key_entry", nice_type_name: "Table Key Entry")
|
node: node, type: "table_key_entry", nice_type_name: "Table Key Entry")
|
||||||
|
|
||||||
var walker = Walker(node: node)
|
var walker = Walker(node: node)
|
||||||
@@ -631,8 +666,9 @@ extension TableKeyEntry: Compilable {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(P4Lang.TableKeyEntry, CompilerContext)>.Error(
|
or: Result<(P4Lang.TableKeyEntry, CompilerContext)>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing table key entry declaration component")))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Missing table key entry declaration component")))
|
||||||
|
|
||||||
let maybe_keyset_expression = KeysetExpression.compile(
|
let maybe_keyset_expression = KeysetExpression.compile(
|
||||||
node: current_node!, withContext: current_context)
|
node: current_node!, withContext: current_context)
|
||||||
@@ -646,8 +682,9 @@ extension TableKeyEntry: Compilable {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(P4Lang.TableKeyEntry, CompilerContext)>.Error(
|
or: Result<(P4Lang.TableKeyEntry, CompilerContext)>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing table key entry declaration component")))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Missing table key entry declaration component")))
|
||||||
|
|
||||||
let maybe_match_type = TableKeyMatchType.Compile(
|
let maybe_match_type = TableKeyMatchType.Compile(
|
||||||
node: current_node!, withContext: current_context)
|
node: current_node!, withContext: current_context)
|
||||||
@@ -670,7 +707,10 @@ extension TableKeyMatchType: Compilable {
|
|||||||
if node.text! == "exact" {
|
if node.text! == "exact" {
|
||||||
return .Ok((TableKeyMatchType.Exact, context))
|
return .Ok((TableKeyMatchType.Exact, context))
|
||||||
}
|
}
|
||||||
return .Error(ErrorOnNode(node: node, withError: "\(node.text!) is not a valid match type)"))
|
return .Error(
|
||||||
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "\(node.text!) is not a valid match type)"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -695,8 +735,9 @@ extension TableKeys: Compilable {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(P4Lang.TableKeys, CompilerContext)>.Error(
|
or: Result<(P4Lang.TableKeys, CompilerContext)>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing table keys declaration component in control declaration"))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Missing table keys declaration component in control declaration"))
|
||||||
)
|
)
|
||||||
|
|
||||||
let (keys, errors) = walker.try_map(n: node.childCount - 1, onlyNamed: true) { current_node in
|
let (keys, errors) = walker.try_map(n: node.childCount - 1, onlyNamed: true) { current_node in
|
||||||
@@ -708,11 +749,11 @@ extension TableKeys: Compilable {
|
|||||||
|
|
||||||
if !errors.isEmpty {
|
if !errors.isEmpty {
|
||||||
return .Error(
|
return .Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node,
|
sourceLocation: node.toSourceLocation(),
|
||||||
withError: "Error(s) parsing table key: "
|
withError: "Error(s) parsing table key: "
|
||||||
+ (errors.map { error in
|
+ (errors.map { error in
|
||||||
return "\(error.msg)"
|
return "\(error.msg())"
|
||||||
}.joined(separator: ";"))))
|
}.joined(separator: ";"))))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -741,8 +782,8 @@ extension TableActionsProperty: Compilable {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(TableActionsProperty, CompilerContext)>.Error(
|
or: Result<(TableActionsProperty, CompilerContext)>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node,
|
sourceLocation: node.toSourceLocation(),
|
||||||
withError: "Missing table actions declaration component in control declaration"))
|
withError: "Missing table actions declaration component in control declaration"))
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -753,10 +794,12 @@ extension TableActionsProperty: Compilable {
|
|||||||
switch context.types.lookup(identifier: listed_action) {
|
switch context.types.lookup(identifier: listed_action) {
|
||||||
case .Ok(let maybe_action):
|
case .Ok(let maybe_action):
|
||||||
if maybe_action.eq(rhs: Action()) {
|
if maybe_action.eq(rhs: Action()) {
|
||||||
return .Ok(TypedIdentifier(id: listed_action, withType: P4Type(maybe_action)))
|
return .Ok(TypedIdentifier(id: listed_action, withType: P4QualifiedType(maybe_action)))
|
||||||
}
|
}
|
||||||
return .Error(
|
return .Error(
|
||||||
ErrorOnNode(node: node, withError: "\(listed_action) does not name an action"))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "\(listed_action) does not name an action"))
|
||||||
case .Error(let e): return .Error(e)
|
case .Error(let e): return .Error(e)
|
||||||
}
|
}
|
||||||
case .Error(let e): return .Error(e)
|
case .Error(let e): return .Error(e)
|
||||||
@@ -765,11 +808,11 @@ extension TableActionsProperty: Compilable {
|
|||||||
|
|
||||||
if !errors.isEmpty {
|
if !errors.isEmpty {
|
||||||
return .Error(
|
return .Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node,
|
sourceLocation: node.toSourceLocation(),
|
||||||
withError: "Error(s) parsing table actions: "
|
withError: "Error(s) parsing table actions: "
|
||||||
+ (errors.map { error in
|
+ (errors.map { error in
|
||||||
return "\(error.msg)"
|
return "\(error.msg())"
|
||||||
}.joined(separator: ";"))))
|
}.joined(separator: ";"))))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -782,14 +825,14 @@ extension TablePropertyList: Compilable {
|
|||||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||||
) -> Common.Result<(P4Lang.TablePropertyList, CompilerContext)> {
|
) -> Common.Result<(P4Lang.TablePropertyList, CompilerContext)> {
|
||||||
|
|
||||||
#RequireNodeType<Node, (P4DataType, CompilerContext)>(
|
#RequireNodeType<Node, (P4Type, CompilerContext)>(
|
||||||
node: node, type: "table_property_list", nice_type_name: "Table Property List")
|
node: node, type: "table_property_list", nice_type_name: "Table Property List")
|
||||||
|
|
||||||
var current_context = context
|
var current_context = context
|
||||||
|
|
||||||
var keys: [TableKeys] = Array()
|
var keys: [TableKeys] = Array()
|
||||||
var actions: [TableActionsProperty] = Array()
|
var actions: [TableActionsProperty] = Array()
|
||||||
var errors: [Error] = Array()
|
var errors: [any Errorable] = Array()
|
||||||
|
|
||||||
node.enumerateNamedChildren { child in
|
node.enumerateNamedChildren { child in
|
||||||
if child.nodeType == "table_keys" {
|
if child.nodeType == "table_keys" {
|
||||||
@@ -808,17 +851,19 @@ extension TablePropertyList: Compilable {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
errors.append(
|
errors.append(
|
||||||
ErrorOnNode(node: child, withError: "Uknown node type in control declaration"))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: child.toSourceLocation(),
|
||||||
|
withError: "Uknown node type in control declaration"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !errors.isEmpty {
|
if !errors.isEmpty {
|
||||||
return .Error(
|
return .Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node,
|
sourceLocation: node.toSourceLocation(),
|
||||||
withError: "Error(s) parsing property list: "
|
withError: "Error(s) parsing property list: "
|
||||||
+ (errors.map { error in
|
+ (errors.map { error in
|
||||||
return "\(error.msg)"
|
return "\(error.msg())"
|
||||||
}.joined(separator: ";"))))
|
}.joined(separator: ";"))))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -826,14 +871,18 @@ extension TablePropertyList: Compilable {
|
|||||||
if keys.count > 1 {
|
if keys.count > 1 {
|
||||||
// Todo: Make this error message better.
|
// Todo: Make this error message better.
|
||||||
return .Error(
|
return .Error(
|
||||||
ErrorOnNode(node: node, withError: "More than one key set in table property list"))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "More than one key set in table property list"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// There should be only one table actions!
|
// There should be only one table actions!
|
||||||
if actions.count > 1 {
|
if actions.count > 1 {
|
||||||
// Todo: Make this error message better.
|
// Todo: Make this error message better.
|
||||||
return .Error(
|
return .Error(
|
||||||
ErrorOnNode(node: node, withError: "More than one actions in table property list"))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "More than one actions in table property list"))
|
||||||
}
|
}
|
||||||
if actions.isEmpty {
|
if actions.isEmpty {
|
||||||
actions.append(TableActionsProperty())
|
actions.append(TableActionsProperty())
|
||||||
@@ -851,7 +900,7 @@ extension Table: Compilable {
|
|||||||
) -> Common.Result<(P4Lang.Table, CompilerContext)> {
|
) -> Common.Result<(P4Lang.Table, CompilerContext)> {
|
||||||
|
|
||||||
let table_declaration_node = node
|
let table_declaration_node = node
|
||||||
#RequireNodeType<Node, (P4DataType, CompilerContext)>(
|
#RequireNodeType<Node, (P4Type, CompilerContext)>(
|
||||||
node: table_declaration_node, type: "table_declaration", nice_type_name: "Table Declaration")
|
node: table_declaration_node, type: "table_declaration", nice_type_name: "Table Declaration")
|
||||||
|
|
||||||
var walker = Walker(node: table_declaration_node)
|
var walker = Walker(node: table_declaration_node)
|
||||||
@@ -864,8 +913,9 @@ extension Table: Compilable {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(P4Lang.Table, CompilerContext)>.Error(
|
or: Result<(P4Lang.Table, CompilerContext)>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing table declaration component")))
|
sourceLocation: node.toSourceLocation(), withError: "Missing table declaration component")
|
||||||
|
))
|
||||||
|
|
||||||
guard
|
guard
|
||||||
case .Ok(let table_name) = Identifier.Compile(
|
case .Ok(let table_name) = Identifier.Compile(
|
||||||
@@ -881,8 +931,9 @@ extension Table: Compilable {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(P4Lang.Table, CompilerContext)>.Error(
|
or: Result<(P4Lang.Table, CompilerContext)>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing table declaration component")))
|
sourceLocation: node.toSourceLocation(), withError: "Missing table declaration component")
|
||||||
|
))
|
||||||
|
|
||||||
let maybe_table_property_list = TablePropertyList.Compile(
|
let maybe_table_property_list = TablePropertyList.Compile(
|
||||||
node: current_node!, withContext: current_context)
|
node: current_node!, withContext: current_context)
|
||||||
@@ -923,13 +974,13 @@ extension ExternDeclaration: CompilableDeclaration {
|
|||||||
// with the matching "stuff".
|
// with the matching "stuff".
|
||||||
|
|
||||||
let found_ffi = context.ffis.first { ffi in
|
let found_ffi = context.ffis.first { ffi in
|
||||||
ffi.type().dataType().eq(rhs: declared.identifier.type.dataType())
|
ffi.type().baseType().eq(rhs: declared.identifier.type.baseType())
|
||||||
}
|
}
|
||||||
|
|
||||||
guard let found_ffi = found_ffi else {
|
guard let found_ffi = found_ffi else {
|
||||||
return .Error(
|
return .Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: declarationed_node,
|
sourceLocation: declarationed_node.toSourceLocation(),
|
||||||
withError:
|
withError:
|
||||||
"Could not find a foreign function that matches the extern declaration (\(declared))"))
|
"Could not find a foreign function that matches the extern declaration (\(declared))"))
|
||||||
}
|
}
|
||||||
|
|||||||
+141
-107
@@ -46,7 +46,9 @@ extension TypedIdentifier: CompilableExpression {
|
|||||||
case Result.Ok(let type) = context.instances.lookup(
|
case Result.Ok(let type) = context.instances.lookup(
|
||||||
identifier: Common.Identifier(name: node.text!))
|
identifier: Common.Identifier(name: node.text!))
|
||||||
else {
|
else {
|
||||||
return .Error(ErrorOnNode(node: node, withError: "Cannot find \(node.text!) in scope"))
|
return .Error(
|
||||||
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(), withError: "Cannot find \(node.text!) in scope"))
|
||||||
}
|
}
|
||||||
|
|
||||||
return .Ok(TypedIdentifier(name: node.text!, withType: type))
|
return .Ok(TypedIdentifier(name: node.text!, withType: type))
|
||||||
@@ -88,7 +90,9 @@ extension P4BooleanValue: CompilableExpression {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return .Error(
|
return .Error(
|
||||||
ErrorOnNode(node: node, withError: "Failed to parse boolean literal: \(node.text!)"))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Failed to parse boolean literal: \(node.text!)"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,7 +105,10 @@ extension P4IntValue: CompilableExpression {
|
|||||||
if let parsed_int = Int(node.text!) {
|
if let parsed_int = Int(node.text!) {
|
||||||
return .Ok(P4Value(P4IntValue(withValue: parsed_int)))
|
return .Ok(P4Value(P4IntValue(withValue: parsed_int)))
|
||||||
} else {
|
} else {
|
||||||
return .Error(ErrorOnNode(node: node, withError: "Failed to parse integer: \(node.text!)"))
|
return .Error(
|
||||||
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Failed to parse integer: \(node.text!)"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -237,46 +244,52 @@ extension SelectExpression: CompilableExpression {
|
|||||||
guard let selector_node = node.child(at: 2),
|
guard let selector_node = node.child(at: 2),
|
||||||
selector_node.nodeType == "expression"
|
selector_node.nodeType == "expression"
|
||||||
else {
|
else {
|
||||||
return .Error(ErrorOnNode(node: node, withError: "Could not find selector expression"))
|
return .Error(
|
||||||
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(), withError: "Could not find selector expression"))
|
||||||
}
|
}
|
||||||
|
|
||||||
guard let select_body_node = node.child(at: 5),
|
guard let select_body_node = node.child(at: 5),
|
||||||
select_body_node.nodeType == "selectBody"
|
select_body_node.nodeType == "selectBody"
|
||||||
else {
|
else {
|
||||||
return .Error(ErrorOnNode(node: node, withError: "Could not find select expression body"))
|
return .Error(
|
||||||
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Could not find select expression body"))
|
||||||
}
|
}
|
||||||
|
|
||||||
let maybe_selector = Expression.Compile(node: selector_node, withContext: context)
|
let maybe_selector = Expression.Compile(node: selector_node, withContext: context)
|
||||||
guard case .Ok(let selector) = maybe_selector else {
|
guard case .Ok(let selector) = maybe_selector else {
|
||||||
return .Error(
|
return .Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: selector_node,
|
sourceLocation: selector_node.toSourceLocation(),
|
||||||
withError:
|
withError:
|
||||||
"Could not parse transition select expression selector expression: \(maybe_selector.error()!)"
|
"Could not parse transition select expression selector expression: \(maybe_selector.error()!)"
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
var sces: [SelectCaseExpression] = Array()
|
var sces: [SelectCaseExpression] = Array()
|
||||||
var sces_errors: [Error] = Array()
|
var sces_errors: (any Errorable)? = .none
|
||||||
|
|
||||||
select_body_node.enumerateNamedChildren { current_node in
|
select_body_node.enumerateNamedChildren { current_node in
|
||||||
let maybe_parsed_cse = SelectCaseExpression.compile(
|
let maybe_parsed_cse = SelectCaseExpression.compile(
|
||||||
node: current_node, withContext: context.update(newExpectation: selector.type()))
|
node: current_node, withContext: context.update(newExpectation: selector.type()))
|
||||||
if case .Ok(let parsed_cse) = maybe_parsed_cse {
|
switch maybe_parsed_cse {
|
||||||
sces.append(parsed_cse as! SelectCaseExpression)
|
case .Ok(let parsed_cse): sces.append(parsed_cse as! SelectCaseExpression)
|
||||||
} else {
|
case .Error(let e):
|
||||||
sces_errors.append(Error(withMessage: "\(maybe_parsed_cse.error()!)"))
|
sces_errors =
|
||||||
|
if let sces_errors = sces_errors {
|
||||||
|
sces_errors.append(error: Error(withMessage: "\(maybe_parsed_cse.error()!)"))
|
||||||
|
} else {
|
||||||
|
e
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !sces_errors.isEmpty {
|
if let sces_errors = sces_errors {
|
||||||
return .Error(
|
return .Error(ErrorWithLabel("Error(s) parsing select cases", sces_errors))
|
||||||
Error(
|
|
||||||
withMessage: "Error(s) parsing select cases: "
|
|
||||||
+ (sces_errors.map { error in
|
|
||||||
return "\(error.msg)"
|
|
||||||
}.joined(separator: ";"))))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return .Ok(
|
return .Ok(
|
||||||
SelectExpression(withSelector: selector, withSelectCaseExpressions: sces),
|
SelectExpression(withSelector: selector, withSelectCaseExpressions: sces),
|
||||||
)
|
)
|
||||||
@@ -311,13 +324,17 @@ extension SelectCaseExpression: CompilableExpression {
|
|||||||
|
|
||||||
guard let maybe_keysetexpression = maybe_keysetexpression else {
|
guard let maybe_keysetexpression = maybe_keysetexpression else {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
ErrorOnNode(node: keysetexpression_node, withError: "Missing expected keyset expression"))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: keysetexpression_node.toSourceLocation(),
|
||||||
|
withError: "Missing expected keyset expression"))
|
||||||
}
|
}
|
||||||
|
|
||||||
let keysetexpression = maybe_keysetexpression as! KeysetExpression
|
let keysetexpression = maybe_keysetexpression as! KeysetExpression
|
||||||
|
|
||||||
if case .Error(let e) = keysetexpression.compatible(type: context.expected_type!) {
|
if case .Error(let e) = keysetexpression.compatible(type: context.expected_type!) {
|
||||||
return .Error(ErrorOnNode(node: keysetexpression_node, withError: e.msg))
|
return .Error(
|
||||||
|
ErrorWithLocation(
|
||||||
|
sourceLocation: keysetexpression_node.toSourceLocation(), withError: e.msg()))
|
||||||
}
|
}
|
||||||
|
|
||||||
let maybe_parsed_targetstate = Identifier.Compile(
|
let maybe_parsed_targetstate = Identifier.Compile(
|
||||||
@@ -349,8 +366,9 @@ extension BinaryOperatorExpression: CompilableExpression {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<EvaluatableExpression?>.Error(
|
or: Result<EvaluatableExpression?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Malformed binary operator expression")))
|
sourceLocation: node.toSourceLocation(), withError: "Malformed binary operator expression"
|
||||||
|
)))
|
||||||
|
|
||||||
/// TODO: This macro cannot handle new lines in the arrays
|
/// TODO: This macro cannot handle new lines in the arrays
|
||||||
// swift-format-ignore
|
// swift-format-ignore
|
||||||
@@ -361,8 +379,9 @@ extension BinaryOperatorExpression: CompilableExpression {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<EvaluatableExpression?>.Error(
|
or: Result<EvaluatableExpression?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing LHS for binary operator expression")))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Missing LHS for binary operator expression")))
|
||||||
|
|
||||||
let left_hand_side_raw = current_node!
|
let left_hand_side_raw = current_node!
|
||||||
|
|
||||||
@@ -370,15 +389,17 @@ extension BinaryOperatorExpression: CompilableExpression {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<EvaluatableExpression?>.Error(
|
or: Result<EvaluatableExpression?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing binary operator for binary operator expression")))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Missing binary operator for binary operator expression")))
|
||||||
|
|
||||||
walker.next()
|
walker.next()
|
||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<EvaluatableExpression?>.Error(
|
or: Result<EvaluatableExpression?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing RHS for binary operator expression")))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Missing RHS for binary operator expression")))
|
||||||
|
|
||||||
let right_hand_side_raw = current_node!
|
let right_hand_side_raw = current_node!
|
||||||
|
|
||||||
@@ -392,52 +413,55 @@ extension BinaryOperatorExpression: CompilableExpression {
|
|||||||
return Result.Error(maybe_right_hand_side.error()!)
|
return Result.Error(maybe_right_hand_side.error()!)
|
||||||
}
|
}
|
||||||
|
|
||||||
let evaluators: [String: (String, P4Type, BinaryOperatorChecker?, BinaryOperatorEvaluator)] = [
|
let evaluators:
|
||||||
"binaryEqualOperatorExpression": (
|
[String: (String, P4QualifiedType, BinaryOperatorChecker?, BinaryOperatorEvaluator)] = [
|
||||||
"Binary Equal", P4Type(P4Boolean()), Optional<BinaryOperatorChecker>.none,
|
"binaryEqualOperatorExpression": (
|
||||||
binary_equal_operator_evaluator
|
"Binary Equal", P4QualifiedType(P4Boolean()), Optional<BinaryOperatorChecker>.none,
|
||||||
),
|
binary_equal_operator_evaluator
|
||||||
"binaryLessThanOperatorExpression": (
|
),
|
||||||
"Binary Less Than", P4Type(P4Boolean()), Optional<BinaryOperatorChecker>.none,
|
"binaryLessThanOperatorExpression": (
|
||||||
binary_lt_operator_evaluator
|
"Binary Less Than", P4QualifiedType(P4Boolean()), Optional<BinaryOperatorChecker>.none,
|
||||||
),
|
binary_lt_operator_evaluator
|
||||||
"binaryLessThanEqualOperatorExpression": (
|
),
|
||||||
"Binary Less Than Or Equal", P4Type(P4Boolean()), Optional<BinaryOperatorChecker>.none,
|
"binaryLessThanEqualOperatorExpression": (
|
||||||
binary_lte_operator_evaluator
|
"Binary Less Than Or Equal", P4QualifiedType(P4Boolean()),
|
||||||
),
|
Optional<BinaryOperatorChecker>.none,
|
||||||
"binaryGreaterThanOperatorExpression": (
|
binary_lte_operator_evaluator
|
||||||
"Binary Greater Than", P4Type(P4Boolean()), Optional<BinaryOperatorChecker>.none,
|
),
|
||||||
binary_gt_operator_evaluator
|
"binaryGreaterThanOperatorExpression": (
|
||||||
),
|
"Binary Greater Than", P4QualifiedType(P4Boolean()), Optional<BinaryOperatorChecker>.none,
|
||||||
"binaryGreaterThanEqualOperatorExpression": (
|
binary_gt_operator_evaluator
|
||||||
"Binary Greater Than Or Equal", P4Type(P4Boolean()), Optional<BinaryOperatorChecker>.none,
|
),
|
||||||
binary_gte_operator_evaluator
|
"binaryGreaterThanEqualOperatorExpression": (
|
||||||
),
|
"Binary Greater Than Or Equal", P4QualifiedType(P4Boolean()),
|
||||||
"binaryAndOperatorExpression": (
|
Optional<BinaryOperatorChecker>.none,
|
||||||
"Binary Or", P4Type(P4Boolean()), binary_and_or_operator_checker,
|
binary_gte_operator_evaluator
|
||||||
binary_and_operator_evaluator
|
),
|
||||||
),
|
"binaryAndOperatorExpression": (
|
||||||
"binaryOrOperatorExpression": (
|
"Binary Or", P4QualifiedType(P4Boolean()), binary_and_or_operator_checker,
|
||||||
"Binary And", P4Type(P4Boolean()), binary_and_or_operator_checker,
|
binary_and_operator_evaluator
|
||||||
binary_or_operator_evaluator
|
),
|
||||||
),
|
"binaryOrOperatorExpression": (
|
||||||
"binaryAddOperatorExpression": (
|
"Binary And", P4QualifiedType(P4Boolean()), binary_and_or_operator_checker,
|
||||||
"Binary Add", P4Type(P4Int()), binary_int_math_operator_checker,
|
binary_or_operator_evaluator
|
||||||
binary_add_operator_evaluator
|
),
|
||||||
),
|
"binaryAddOperatorExpression": (
|
||||||
"binarySubtractOperatorExpression": (
|
"Binary Add", P4QualifiedType(P4Int()), binary_int_math_operator_checker,
|
||||||
"Binary Subtract", P4Type(P4Int()), binary_int_math_operator_checker,
|
binary_add_operator_evaluator
|
||||||
binary_subtract_operator_evaluator
|
),
|
||||||
),
|
"binarySubtractOperatorExpression": (
|
||||||
"binaryMultiplyOperatorExpression": (
|
"Binary Subtract", P4QualifiedType(P4Int()), binary_int_math_operator_checker,
|
||||||
"Binary Multiply", P4Type(P4Int()), binary_int_math_operator_checker,
|
binary_subtract_operator_evaluator
|
||||||
binary_multiply_operator_evaluator
|
),
|
||||||
),
|
"binaryMultiplyOperatorExpression": (
|
||||||
"binaryDivideOperatorExpression": (
|
"Binary Multiply", P4QualifiedType(P4Int()), binary_int_math_operator_checker,
|
||||||
"Binary Divide", P4Type(P4Int()), binary_int_math_operator_checker,
|
binary_multiply_operator_evaluator
|
||||||
binary_divide_operator_evaluator
|
),
|
||||||
),
|
"binaryDivideOperatorExpression": (
|
||||||
]
|
"Binary Divide", P4QualifiedType(P4Int()), binary_int_math_operator_checker,
|
||||||
|
binary_divide_operator_evaluator
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
guard let selected_evaluator = evaluators[binary_operator_expression_node.nodeType!] else {
|
guard let selected_evaluator = evaluators[binary_operator_expression_node.nodeType!] else {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
@@ -474,8 +498,8 @@ extension ArrayAccessExpression: CompilableExpression {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<EvaluatableExpression?>.Error(
|
or: Result<EvaluatableExpression?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Malformed array access expression")))
|
sourceLocation: node.toSourceLocation(), withError: "Malformed array access expression")))
|
||||||
|
|
||||||
#RequireNodeType<Node, EvaluatableExpression?>(
|
#RequireNodeType<Node, EvaluatableExpression?>(
|
||||||
node: current_node!, type: "expression",
|
node: current_node!, type: "expression",
|
||||||
@@ -486,16 +510,18 @@ extension ArrayAccessExpression: CompilableExpression {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<EvaluatableExpression?>.Error(
|
or: Result<EvaluatableExpression?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing [ for array access expression")))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Missing [ for array access expression")))
|
||||||
|
|
||||||
walker.next()
|
walker.next()
|
||||||
|
|
||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<EvaluatableExpression?>.Error(
|
or: Result<EvaluatableExpression?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing indexor expression for array access expression")))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Missing indexor expression for array access expression")))
|
||||||
|
|
||||||
#RequireNodeType<Node, EvaluatableExpression?>(
|
#RequireNodeType<Node, EvaluatableExpression?>(
|
||||||
node: current_node!, type: "expression",
|
node: current_node!, type: "expression",
|
||||||
@@ -510,10 +536,10 @@ extension ArrayAccessExpression: CompilableExpression {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let maybe_array_type = array_identifier.type()
|
let maybe_array_type = array_identifier.type()
|
||||||
guard let array_type = maybe_array_type.dataType() as? P4Array else {
|
guard let array_type = maybe_array_type.baseType() as? P4Array else {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: array_access_identifier_node,
|
sourceLocation: array_access_identifier_node.toSourceLocation(),
|
||||||
withError: "\(array_identifier) does not name an array type")
|
withError: "\(array_identifier) does not name an array type")
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -547,8 +573,8 @@ extension FieldAccessExpression: CompilableExpression {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<EvaluatableExpression?>.Error(
|
or: Result<EvaluatableExpression?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Malformed field access expression")))
|
sourceLocation: node.toSourceLocation(), withError: "Malformed field access expression")))
|
||||||
|
|
||||||
#RequireNodeType<Node, EvaluatableExpression?>(
|
#RequireNodeType<Node, EvaluatableExpression?>(
|
||||||
node: current_node!, type: "expression",
|
node: current_node!, type: "expression",
|
||||||
@@ -559,15 +585,17 @@ extension FieldAccessExpression: CompilableExpression {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<EvaluatableExpression?>.Error(
|
or: Result<EvaluatableExpression?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing . for field access expression")))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Missing . for field access expression")))
|
||||||
|
|
||||||
walker.next()
|
walker.next()
|
||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<EvaluatableExpression?>.Error(
|
or: Result<EvaluatableExpression?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing field name for field access expression")))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Missing field name for field access expression")))
|
||||||
|
|
||||||
#RequireNodeType<Node, EvaluatableExpression?>(
|
#RequireNodeType<Node, EvaluatableExpression?>(
|
||||||
node: current_node!, type: "identifier",
|
node: current_node!, type: "identifier",
|
||||||
@@ -581,10 +609,10 @@ extension FieldAccessExpression: CompilableExpression {
|
|||||||
guard case Result.Ok(let struct_identifier) = maybe_struct_identifier else {
|
guard case Result.Ok(let struct_identifier) = maybe_struct_identifier else {
|
||||||
return Result.Error(maybe_struct_identifier.error()!)
|
return Result.Error(maybe_struct_identifier.error()!)
|
||||||
}
|
}
|
||||||
guard let struct_type = struct_identifier.type().dataType() as? P4Struct else {
|
guard let struct_type = struct_identifier.type().baseType() as? P4Struct else {
|
||||||
return .Error(
|
return .Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: struct_identifier_node,
|
sourceLocation: struct_identifier_node.toSourceLocation(),
|
||||||
withError: "\(struct_identifier_node.text!) does not have struct type"))
|
withError: "\(struct_identifier_node.text!) does not have struct type"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -598,8 +626,8 @@ extension FieldAccessExpression: CompilableExpression {
|
|||||||
let maybe_field_type = struct_type.fields.get_field_type(field_name)
|
let maybe_field_type = struct_type.fields.get_field_type(field_name)
|
||||||
guard let field_type = maybe_field_type else {
|
guard let field_type = maybe_field_type else {
|
||||||
return .Error(
|
return .Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: field_name_node,
|
sourceLocation: field_name_node.toSourceLocation(),
|
||||||
withError: "\(field_name) is not a valid field for struct with type \(struct_type)"))
|
withError: "\(field_name) is not a valid field for struct with type \(struct_type)"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -663,8 +691,8 @@ extension FunctionCall: CompilableExpression {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<EvaluatableExpression?>.Error(
|
or: Result<EvaluatableExpression?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing function call component")))
|
sourceLocation: node.toSourceLocation(), withError: "Missing function call component")))
|
||||||
|
|
||||||
let maybe_callee_name = Identifier.Compile(
|
let maybe_callee_name = Identifier.Compile(
|
||||||
node: current_node!, withContext: context)
|
node: current_node!, withContext: context)
|
||||||
@@ -680,7 +708,9 @@ extension FunctionCall: CompilableExpression {
|
|||||||
Result<(FunctionDeclaration?, Declaration?)>.Ok((callee, .none)) // What we found is actually a function declaration
|
Result<(FunctionDeclaration?, Declaration?)>.Ok((callee, .none)) // What we found is actually a function declaration
|
||||||
default:
|
default:
|
||||||
Result<(FunctionDeclaration?, Declaration?)>.Error(
|
Result<(FunctionDeclaration?, Declaration?)>.Error(
|
||||||
ErrorOnNode(node: current_node!, withError: "\(callee_name) is not a function"))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: current_node!.toSourceLocation(),
|
||||||
|
withError: "\(callee_name) is not a function"))
|
||||||
}
|
}
|
||||||
case .Error(let e): Result<(FunctionDeclaration?, Declaration?)>.Error(e)
|
case .Error(let e): Result<(FunctionDeclaration?, Declaration?)>.Error(e)
|
||||||
}
|
}
|
||||||
@@ -690,10 +720,13 @@ extension FunctionCall: CompilableExpression {
|
|||||||
switch context.externs.lookup(identifier: callee_name) {
|
switch context.externs.lookup(identifier: callee_name) {
|
||||||
case .Ok(let callee as Declaration):
|
case .Ok(let callee as Declaration):
|
||||||
// Now, make sure that it is a function declaration!
|
// Now, make sure that it is a function declaration!
|
||||||
switch callee.identifier.type.dataType() {
|
switch callee.identifier.type.baseType() {
|
||||||
case is FunctionDeclaration: Result.Ok((.none, callee))
|
case is FunctionDeclaration: Result.Ok((.none, callee))
|
||||||
default:
|
default:
|
||||||
.Error(ErrorOnNode(node: current_node!, withError: "\(callee_name) is not a function"))
|
.Error(
|
||||||
|
ErrorWithLocation(
|
||||||
|
sourceLocation: current_node!.toSourceLocation(),
|
||||||
|
withError: "\(callee_name) is not a function"))
|
||||||
}
|
}
|
||||||
default: .Error(e)
|
default: .Error(e)
|
||||||
}
|
}
|
||||||
@@ -710,8 +743,8 @@ extension FunctionCall: CompilableExpression {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<EvaluatableExpression?>.Error(
|
or: Result<EvaluatableExpression?>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing function call component")))
|
sourceLocation: node.toSourceLocation(), withError: "Missing function call component")))
|
||||||
|
|
||||||
let maybe_argument_list = ArgumentList.Compile(node: current_node!, withContext: context)
|
let maybe_argument_list = ArgumentList.Compile(node: current_node!, withContext: context)
|
||||||
|
|
||||||
@@ -725,14 +758,14 @@ extension FunctionCall: CompilableExpression {
|
|||||||
switch callee {
|
switch callee {
|
||||||
case (.some(let callee), .none): Optional<ParameterList>.some(callee.params)
|
case (.some(let callee), .none): Optional<ParameterList>.some(callee.params)
|
||||||
case (.none, .some(let callee)):
|
case (.none, .some(let callee)):
|
||||||
Optional<ParameterList>.some((callee.ffi!.type().dataType() as! FunctionDeclaration).params)
|
Optional<ParameterList>.some((callee.ffi!.type().baseType() as! FunctionDeclaration).params)
|
||||||
default: Optional<ParameterList>.none
|
default: Optional<ParameterList>.none
|
||||||
}
|
}
|
||||||
|
|
||||||
guard case .some(let params) = params else {
|
guard case .some(let params) = params else {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node,
|
sourceLocation: node.toSourceLocation(),
|
||||||
withError: "Could not lookup the parameters for the called function (\(callee_name))"))
|
withError: "Could not lookup the parameters for the called function (\(callee_name))"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -747,8 +780,9 @@ extension FunctionCall: CompilableExpression {
|
|||||||
case (.none, .some(let callee)): .Ok(FunctionCall(callee.ffi!, withArguments: arguments))
|
case (.none, .some(let callee)): .Ok(FunctionCall(callee.ffi!, withArguments: arguments))
|
||||||
default:
|
default:
|
||||||
Result.Error(
|
Result.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Unexpected error occurred calling function named (\(callee_name))"
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Unexpected error occurred calling function named (\(callee_name))"
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,8 +33,8 @@ public struct Parser {
|
|||||||
|
|
||||||
guard let parser = localElementsParsers[node.nodeType ?? ""] else {
|
guard let parser = localElementsParsers[node.nodeType ?? ""] else {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node,
|
sourceLocation: node.toSourceLocation(),
|
||||||
withError: "Unparseable statement type (\(node.nodeType ?? "Unknown Statement Type"))"))
|
withError: "Unparseable statement type (\(node.nodeType ?? "Unknown Statement Type"))"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,7 +52,10 @@ public struct Parser {
|
|||||||
node: Node, withContext context: CompilerContext
|
node: Node, withContext context: CompilerContext
|
||||||
) -> Result<(EvaluatableStatement, CompilerContext)> {
|
) -> Result<(EvaluatableStatement, CompilerContext)> {
|
||||||
if node.nodeType != "parserStatement" && node.nodeType != "statement" {
|
if node.nodeType != "parserStatement" && node.nodeType != "statement" {
|
||||||
return Result.Error(ErrorOnNode(node: node, withError: "Missing expected parser statement"))
|
return Result.Error(
|
||||||
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(), withError: "Missing expected parser statement")
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
let statement = node.child(at: 0)!
|
let statement = node.child(at: 0)!
|
||||||
@@ -66,8 +69,8 @@ public struct Parser {
|
|||||||
]
|
]
|
||||||
guard let parser = statementParsers[statement.nodeType ?? ""] else {
|
guard let parser = statementParsers[statement.nodeType ?? ""] else {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: statement,
|
sourceLocation: statement.toSourceLocation(),
|
||||||
withError:
|
withError:
|
||||||
"Unparseable statement type (\(statement.nodeType ?? "Unknown Statement Type"))"))
|
"Unparseable statement type (\(statement.nodeType ?? "Unknown Statement Type"))"))
|
||||||
}
|
}
|
||||||
@@ -76,7 +79,9 @@ public struct Parser {
|
|||||||
return .Ok((parsed, updated_context))
|
return .Ok((parsed, updated_context))
|
||||||
case Result.Error(let e):
|
case Result.Error(let e):
|
||||||
return .Error(
|
return .Error(
|
||||||
ErrorOnNode(node: node, withError: "Failed to parse a statement element: \(e)"))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Failed to parse a statement element: \(e)"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -95,13 +100,15 @@ public struct Parser {
|
|||||||
tse_node.nodeType! == "transitionSelectionExpression"
|
tse_node.nodeType! == "transitionSelectionExpression"
|
||||||
else {
|
else {
|
||||||
return .Error(
|
return .Error(
|
||||||
ErrorOnNode(node: node, withError: "Could not find transition select expression"))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Could not find transition select expression"))
|
||||||
}
|
}
|
||||||
|
|
||||||
guard let next_node = tse_node.child(at: 0) else {
|
guard let next_node = tse_node.child(at: 0) else {
|
||||||
return .Error(
|
return .Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node,
|
sourceLocation: node.toSourceLocation(),
|
||||||
withError: "Could not find the next token in a transition selection expression"))
|
withError: "Could not find the next token in a transition selection expression"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,10 +152,12 @@ public struct Parser {
|
|||||||
node: Node, withContext context: CompilerContext
|
node: Node, withContext context: CompilerContext
|
||||||
) -> Result<([EvaluatableStatement], CompilerContext)> {
|
) -> Result<([EvaluatableStatement], CompilerContext)> {
|
||||||
if node.nodeType != "statements" && node.nodeType != "parserStatements" {
|
if node.nodeType != "statements" && node.nodeType != "parserStatements" {
|
||||||
return Result.Error(ErrorOnNode(node: node, withError: "Did not find expected statements"))
|
return Result.Error(
|
||||||
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(), withError: "Did not find expected statements"))
|
||||||
}
|
}
|
||||||
|
|
||||||
var parse_errs: [Error] = Array()
|
var parse_errs: [any Errorable] = Array()
|
||||||
var current_context = context
|
var current_context = context
|
||||||
var parsed_s: [EvaluatableStatement] = Array()
|
var parsed_s: [EvaluatableStatement] = Array()
|
||||||
|
|
||||||
@@ -168,7 +177,7 @@ public struct Parser {
|
|||||||
return Result.Error(
|
return Result.Error(
|
||||||
Error(
|
Error(
|
||||||
withMessage: parse_errs.map { err in
|
withMessage: parse_errs.map { err in
|
||||||
return String(err.msg)
|
return String(err.format())
|
||||||
}.joined(separator: ";")))
|
}.joined(separator: ";")))
|
||||||
}
|
}
|
||||||
return Result.Ok((parsed_s, current_context))
|
return Result.Ok((parsed_s, current_context))
|
||||||
@@ -187,19 +196,23 @@ public struct Parser {
|
|||||||
node_type == "parserState"
|
node_type == "parserState"
|
||||||
else {
|
else {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
ErrorOnNode(node: node, withError: "Did not find a parser state declaration"))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Did not find a parser state declaration"))
|
||||||
}
|
}
|
||||||
|
|
||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(InstantiatedParserState, CompilerContext)>.Error(
|
or: Result<(InstantiatedParserState, CompilerContext)>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing elements in parser state declaration")))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Missing elements in parser state declaration")))
|
||||||
|
|
||||||
if current_node!.nodeType == "annotations" {
|
if current_node!.nodeType == "annotations" {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: current_node!, withError: "Annotations in parser state are not yet handled."))
|
sourceLocation: current_node!.toSourceLocation(),
|
||||||
|
withError: "Annotations in parser state are not yet handled."))
|
||||||
|
|
||||||
// Would increment here.
|
// Would increment here.
|
||||||
}
|
}
|
||||||
@@ -209,8 +222,9 @@ public struct Parser {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(InstantiatedParserState, CompilerContext)>.Error(
|
or: Result<(InstantiatedParserState, CompilerContext)>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing elements in parser state declaration")))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Missing elements in parser state declaration")))
|
||||||
|
|
||||||
let maybe_state_identifier = Identifier.Compile(
|
let maybe_state_identifier = Identifier.Compile(
|
||||||
node: current_node!, withContext: context)
|
node: current_node!, withContext: context)
|
||||||
@@ -224,10 +238,11 @@ public struct Parser {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(InstantiatedParserState, CompilerContext)>.Error(
|
or: Result<(InstantiatedParserState, CompilerContext)>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing body of state declaration")))
|
sourceLocation: node.toSourceLocation(), withError: "Missing body of state declaration")
|
||||||
|
))
|
||||||
|
|
||||||
var parse_errs: [Error] = Array()
|
var parse_errs: [any Errorable] = Array()
|
||||||
var current_context = context
|
var current_context = context
|
||||||
var parsed_s: [EvaluatableStatement] = Array()
|
var parsed_s: [EvaluatableStatement] = Array()
|
||||||
|
|
||||||
@@ -248,15 +263,16 @@ public struct Parser {
|
|||||||
return Result.Error(
|
return Result.Error(
|
||||||
Error(
|
Error(
|
||||||
withMessage: parse_errs.map { err in
|
withMessage: parse_errs.map { err in
|
||||||
return String(err.msg)
|
return String(err.format())
|
||||||
}.joined(separator: ";")))
|
}.joined(separator: ";")))
|
||||||
}
|
}
|
||||||
|
|
||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(InstantiatedParserState, CompilerContext)>.Error(
|
or: Result<(InstantiatedParserState, CompilerContext)>.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Missing transition statement of state declaration")))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Missing transition statement of state declaration")))
|
||||||
|
|
||||||
return TransitionStatement.Compile(
|
return TransitionStatement.Compile(
|
||||||
node: current_node!, forState: state_identifier, withStatements: parsed_s,
|
node: current_node!, forState: state_identifier, withStatements: parsed_s,
|
||||||
@@ -272,7 +288,7 @@ public struct Parser {
|
|||||||
var parser = P4Lang.Parser(withName: name, withParameters: parameters)
|
var parser = P4Lang.Parser(withName: name, withParameters: parameters)
|
||||||
|
|
||||||
// Build a state from each one listed.
|
// Build a state from each one listed.
|
||||||
var error: Error? = .none
|
var error: (any Errorable)? = .none
|
||||||
|
|
||||||
var current_context = context
|
var current_context = context
|
||||||
/// TODO: Assert that there is only one.
|
/// TODO: Assert that there is only one.
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ public struct Program {
|
|||||||
// Add our FFIs
|
// Add our FFIs
|
||||||
compilation_context = compilation_context.update(newFFIs: ffis)
|
compilation_context = compilation_context.update(newFFIs: ffis)
|
||||||
|
|
||||||
var errors: [Error] = Array()
|
var errors: [any Errorable] = Array()
|
||||||
|
|
||||||
// If the caller gave any global instances, add them here.
|
// If the caller gave any global instances, add them here.
|
||||||
if let globalInstances = globalInstances {
|
if let globalInstances = globalInstances {
|
||||||
@@ -98,8 +98,9 @@ public struct Program {
|
|||||||
// If none of the declaration parsers chose to parse, that's an error, too!
|
// If none of the declaration parsers chose to parse, that's an error, too!
|
||||||
if !found_parser {
|
if !found_parser {
|
||||||
errors.append(
|
errors.append(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: specific_declaration_node, withError: "Could not find parser for declaration node"
|
sourceLocation: specific_declaration_node.toSourceLocation(),
|
||||||
|
withError: "Could not find parser for declaration node"
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -108,7 +109,7 @@ public struct Program {
|
|||||||
return Result.Error(
|
return Result.Error(
|
||||||
Error(
|
Error(
|
||||||
withMessage: errors.map { error in
|
withMessage: errors.map { error in
|
||||||
return error.msg
|
return error.format()
|
||||||
}.joined(separator: ";")))
|
}.joined(separator: ";")))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ public protocol CompilableValue {
|
|||||||
public protocol CompilableType {
|
public protocol CompilableType {
|
||||||
static func CompileType(
|
static func CompileType(
|
||||||
type: SwiftTreeSitter.Node, withContext: CompilerContext
|
type: SwiftTreeSitter.Node, withContext: CompilerContext
|
||||||
) -> Result<P4DataType?>
|
) -> Result<P4Type?>
|
||||||
}
|
}
|
||||||
|
|
||||||
public protocol CompilableDeclaration {
|
public protocol CompilableDeclaration {
|
||||||
|
|||||||
@@ -35,22 +35,26 @@ extension BlockStatement: CompilableStatement {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(EvaluatableStatement, CompilerContext)>.Error(
|
or: Result<(EvaluatableStatement, CompilerContext)>.Error(
|
||||||
ErrorOnNode(node: node, withError: "Malformed block statement")))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(), withError: "Malformed block statement")))
|
||||||
|
|
||||||
if current_node!.nodeType != "{" {
|
if current_node!.nodeType != "{" {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
ErrorOnNode(node: current_node!, withError: "Missing { on block statement"))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: current_node!.toSourceLocation(),
|
||||||
|
withError: "Missing { on block statement"))
|
||||||
}
|
}
|
||||||
|
|
||||||
var statements: [EvaluatableStatement] = Array()
|
var statements: [EvaluatableStatement] = Array()
|
||||||
var parse_err: Error? = .none
|
var parse_err: (any Errorable)? = .none
|
||||||
var current_context = context
|
var current_context = context
|
||||||
|
|
||||||
walker.next()
|
walker.next()
|
||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(EvaluatableStatement, CompilerContext)>.Error(
|
or: Result<(EvaluatableStatement, CompilerContext)>.Error(
|
||||||
ErrorOnNode(node: node, withError: "Malformed block statement")))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(), withError: "Malformed block statement")))
|
||||||
|
|
||||||
if current_node!.nodeType == "statements" {
|
if current_node!.nodeType == "statements" {
|
||||||
switch Parser.Statements.Compile(
|
switch Parser.Statements.Compile(
|
||||||
@@ -73,11 +77,14 @@ extension BlockStatement: CompilableStatement {
|
|||||||
#MustOr(
|
#MustOr(
|
||||||
result: current_node, thing: walker.getNext(),
|
result: current_node, thing: walker.getNext(),
|
||||||
or: Result<(EvaluatableStatement, CompilerContext)>.Error(
|
or: Result<(EvaluatableStatement, CompilerContext)>.Error(
|
||||||
ErrorOnNode(node: node, withError: "Malformed block statement")))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(), withError: "Malformed block statement")))
|
||||||
|
|
||||||
if current_node!.nodeType != "}" {
|
if current_node!.nodeType != "}" {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
ErrorOnNode(node: current_node!, withError: "Missing } on block statement"))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: current_node!.toSourceLocation(),
|
||||||
|
withError: "Missing } on block statement"))
|
||||||
}
|
}
|
||||||
|
|
||||||
return .Ok((BlockStatement(statements), current_context))
|
return .Ok((BlockStatement(statements), current_context))
|
||||||
@@ -97,7 +104,9 @@ extension ConditionalStatement: CompilableStatement {
|
|||||||
condition_expression.nodeType == "expression"
|
condition_expression.nodeType == "expression"
|
||||||
else {
|
else {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
ErrorOnNode(node: node, withError: "Did not find condition for conditional statement"))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Did not find condition for conditional statement"))
|
||||||
}
|
}
|
||||||
|
|
||||||
let maybe_thens = node.child(at: 4)
|
let maybe_thens = node.child(at: 4)
|
||||||
@@ -105,8 +114,9 @@ extension ConditionalStatement: CompilableStatement {
|
|||||||
thens.nodeType == "statement"
|
thens.nodeType == "statement"
|
||||||
else {
|
else {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Did not find then statement block for conditional statement"))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Did not find then statement block for conditional statement"))
|
||||||
}
|
}
|
||||||
|
|
||||||
guard
|
guard
|
||||||
@@ -165,8 +175,9 @@ extension VariableDeclarationStatement: CompilableStatement {
|
|||||||
typeref.nodeType == "typeRef"
|
typeref.nodeType == "typeRef"
|
||||||
else {
|
else {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Did not find type name for variable declaration statement"))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Did not find type name for variable declaration statement"))
|
||||||
}
|
}
|
||||||
|
|
||||||
let maybe_variablename = node.child(at: 1)
|
let maybe_variablename = node.child(at: 1)
|
||||||
@@ -174,8 +185,9 @@ extension VariableDeclarationStatement: CompilableStatement {
|
|||||||
variablename.nodeType == "identifier"
|
variablename.nodeType == "identifier"
|
||||||
else {
|
else {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node, withError: "Did not find identifier name for variable declaration statement"))
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Did not find identifier name for variable declaration statement"))
|
||||||
}
|
}
|
||||||
|
|
||||||
let maybe_rvalue = node.childCount > 3 ? node.child(at: 3) : .none
|
let maybe_rvalue = node.childCount > 3 ? node.child(at: 3) : .none
|
||||||
@@ -194,17 +206,19 @@ extension VariableDeclarationStatement: CompilableStatement {
|
|||||||
Error(withMessage: "Could not parse a P4 type from \(typeref.text!)"))
|
Error(withMessage: "Could not parse a P4 type from \(typeref.text!)"))
|
||||||
}
|
}
|
||||||
|
|
||||||
var initializer: EvaluatableExpression = declaration_p4_type.def()
|
var initializer: EvaluatableExpression? = .none
|
||||||
|
|
||||||
// If there is an initializer, it must be an expression.
|
// If there is an initializer, it must be an expression.
|
||||||
if let rvalue = maybe_rvalue {
|
if let initializer_expression = maybe_rvalue {
|
||||||
guard rvalue.nodeType == "expression" else {
|
guard initializer_expression.nodeType == "expression" else {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node,
|
sourceLocation: node.toSourceLocation(),
|
||||||
withError: "initial value for declaration statement is not an expression"))
|
withError: "initial value for declaration statement is not an expression"))
|
||||||
}
|
}
|
||||||
|
|
||||||
let maybe_parsed_rvalue = Expression.Compile(node: rvalue, withContext: context)
|
let maybe_parsed_rvalue = Expression.Compile(
|
||||||
|
node: initializer_expression, withContext: context)
|
||||||
guard
|
guard
|
||||||
case .Ok(let parsed_rvalue) = maybe_parsed_rvalue
|
case .Ok(let parsed_rvalue) = maybe_parsed_rvalue
|
||||||
else {
|
else {
|
||||||
@@ -217,10 +231,23 @@ extension VariableDeclarationStatement: CompilableStatement {
|
|||||||
return Result.Error(
|
return Result.Error(
|
||||||
Error(
|
Error(
|
||||||
withMessage:
|
withMessage:
|
||||||
"Cannot initialize \(parsed_variablename) (with type \(declaration_p4_type)) from rvalue with type \(parsed_rvalue.type())"
|
"Cannot initialize \(parsed_variablename) (with type \(declaration_p4_type)) from expression with type \(parsed_rvalue.type())"
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If there is no initializer, then it must be defaultable.
|
||||||
|
|
||||||
|
if initializer == nil {
|
||||||
|
initializer = declaration_p4_type.def()
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let initializer = initializer else {
|
||||||
|
return Result.Error(
|
||||||
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(), withError: "No initializer for declaration"))
|
||||||
|
}
|
||||||
|
|
||||||
return Result.Ok(
|
return Result.Ok(
|
||||||
(
|
(
|
||||||
VariableDeclarationStatement(
|
VariableDeclarationStatement(
|
||||||
@@ -262,14 +289,18 @@ extension ParserAssignmentStatement: CompilableStatement {
|
|||||||
lvalue_node.nodeType == "expression"
|
lvalue_node.nodeType == "expression"
|
||||||
else {
|
else {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
ErrorOnNode(node: node, withError: "Missing lvalue in assignment statement"))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Missing lvalue in assignment statement"))
|
||||||
}
|
}
|
||||||
|
|
||||||
guard let rvalue_node = node.child(at: 2),
|
guard let rvalue_node = node.child(at: 2),
|
||||||
rvalue_node.nodeType == "expression"
|
rvalue_node.nodeType == "expression"
|
||||||
else {
|
else {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
ErrorOnNode(node: node, withError: "Missing rvalue in assignment statement"))
|
ErrorWithLocation(
|
||||||
|
sourceLocation: node.toSourceLocation(),
|
||||||
|
withError: "Missing rvalue in assignment statement"))
|
||||||
}
|
}
|
||||||
|
|
||||||
let maybe_parsed_rvalue = Expression.Compile(
|
let maybe_parsed_rvalue = Expression.Compile(
|
||||||
@@ -286,8 +317,8 @@ extension ParserAssignmentStatement: CompilableStatement {
|
|||||||
let check_result = lvalue_identifier.check(to: rvalue, inScopes: context.instances)
|
let check_result = lvalue_identifier.check(to: rvalue, inScopes: context.instances)
|
||||||
guard case .Ok(_) = check_result else {
|
guard case .Ok(_) = check_result else {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: lvalue_node,
|
sourceLocation: lvalue_node.toSourceLocation(),
|
||||||
withError: "\(check_result.error()!)"))
|
withError: "\(check_result.error()!)"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -312,12 +343,12 @@ extension ReturnStatement: CompilableStatement {
|
|||||||
|
|
||||||
return switch Expression.Compile(node: expression_node, withContext: context) {
|
return switch Expression.Compile(node: expression_node, withContext: context) {
|
||||||
case .Ok(let result):
|
case .Ok(let result):
|
||||||
if result.type().dataType().eq(rhs: context.expected_type!.dataType()) {
|
if result.type().baseType().eq(rhs: context.expected_type!.baseType()) {
|
||||||
.Ok((ReturnStatement(result), context))
|
.Ok((ReturnStatement(result), context))
|
||||||
} else {
|
} else {
|
||||||
.Error(
|
.Error(
|
||||||
ErrorOnNode(
|
ErrorWithLocation(
|
||||||
node: node,
|
sourceLocation: node.toSourceLocation(),
|
||||||
withError:
|
withError:
|
||||||
"Type of expression in return statement (\(result.type())) is not compatible with function return type (\(context.expected_type!))"
|
"Type of expression in return statement (\(result.type())) is not compatible with function return type (\(context.expected_type!))"
|
||||||
))
|
))
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ import TreeSitterP4
|
|||||||
extension P4Boolean: CompilableType {
|
extension P4Boolean: CompilableType {
|
||||||
public static func CompileType(
|
public static func CompileType(
|
||||||
type: SwiftTreeSitter.Node, withContext: CompilerContext
|
type: SwiftTreeSitter.Node, withContext: CompilerContext
|
||||||
) -> Common.Result<(any Common.P4DataType)?> {
|
) -> Common.Result<(any Common.P4Type)?> {
|
||||||
return type.text == "bool" ? .Ok(P4Boolean()) : .Ok(.none)
|
return type.text == "bool" ? .Ok(P4Boolean()) : .Ok(.none)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -33,7 +33,7 @@ extension P4Boolean: CompilableType {
|
|||||||
extension P4Int: CompilableType {
|
extension P4Int: CompilableType {
|
||||||
public static func CompileType(
|
public static func CompileType(
|
||||||
type: SwiftTreeSitter.Node, withContext: CompilerContext
|
type: SwiftTreeSitter.Node, withContext: CompilerContext
|
||||||
) -> Common.Result<(any Common.P4DataType)?> {
|
) -> Common.Result<(any Common.P4Type)?> {
|
||||||
return type.text == "int" ? .Ok(P4Int()) : .Ok(.none)
|
return type.text == "int" ? .Ok(P4Int()) : .Ok(.none)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -41,7 +41,7 @@ extension P4Int: CompilableType {
|
|||||||
extension P4String: CompilableType {
|
extension P4String: CompilableType {
|
||||||
public static func CompileType(
|
public static func CompileType(
|
||||||
type: SwiftTreeSitter.Node, withContext: CompilerContext
|
type: SwiftTreeSitter.Node, withContext: CompilerContext
|
||||||
) -> Common.Result<(any Common.P4DataType)?> {
|
) -> Common.Result<(any Common.P4Type)?> {
|
||||||
return type.text == "string" ? .Ok(P4String()) : .Ok(.none)
|
return type.text == "string" ? .Ok(P4String()) : .Ok(.none)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -49,7 +49,7 @@ extension P4String: CompilableType {
|
|||||||
extension P4Struct: CompilableType {
|
extension P4Struct: CompilableType {
|
||||||
public static func CompileType(
|
public static func CompileType(
|
||||||
type: SwiftTreeSitter.Node, withContext context: CompilerContext
|
type: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||||
) -> Common.Result<(any Common.P4DataType)?> {
|
) -> Common.Result<(any Common.P4Type)?> {
|
||||||
|
|
||||||
let maybe_parsed_type_id = Identifier.Compile(node: type, withContext: context)
|
let maybe_parsed_type_id = Identifier.Compile(node: type, withContext: context)
|
||||||
guard case .Ok(let parsed_type_id) = maybe_parsed_type_id else {
|
guard case .Ok(let parsed_type_id) = maybe_parsed_type_id else {
|
||||||
@@ -68,13 +68,13 @@ extension P4Struct: CompilableType {
|
|||||||
public struct Types {
|
public struct Types {
|
||||||
static func CompileType(
|
static func CompileType(
|
||||||
type: SwiftTreeSitter.Node, withContext context: CompilerContext
|
type: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||||
) -> Result<P4Type> {
|
) -> Result<P4QualifiedType> {
|
||||||
let type_parsers: [CompilableType.Type] = [
|
let type_parsers: [CompilableType.Type] = [
|
||||||
P4Boolean.self, P4Int.self, P4String.self, P4Struct.self,
|
P4Boolean.self, P4Int.self, P4String.self, P4Struct.self,
|
||||||
]
|
]
|
||||||
for type_parser in type_parsers {
|
for type_parser in type_parsers {
|
||||||
switch type_parser.CompileType(type: type, withContext: context) {
|
switch type_parser.CompileType(type: type, withContext: context) {
|
||||||
case .Ok(.some(let type)): return .Ok(P4Type(type))
|
case .Ok(.some(let type)): return .Ok(P4QualifiedType(type))
|
||||||
case .Ok(.none): continue
|
case .Ok(.none): continue
|
||||||
case .Error(let e): return .Error(e)
|
case .Error(let e): return .Error(e)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,8 +54,8 @@ public struct Walker {
|
|||||||
|
|
||||||
public func try_map<T>(
|
public func try_map<T>(
|
||||||
n: Int, onlyNamed: Bool = false, todo: (Node) -> Result<T>
|
n: Int, onlyNamed: Bool = false, todo: (Node) -> Result<T>
|
||||||
) -> ([T], [Error]) {
|
) -> ([T], [any Errorable]) {
|
||||||
var errors: [Error] = Array()
|
var errors: [any Errorable] = Array()
|
||||||
var results: [T] = Array()
|
var results: [T] = Array()
|
||||||
|
|
||||||
for currentChildIdx in currentChildIdx..<n {
|
for currentChildIdx in currentChildIdx..<n {
|
||||||
|
|||||||
@@ -17,8 +17,8 @@
|
|||||||
|
|
||||||
import Common
|
import Common
|
||||||
|
|
||||||
public struct Action: CustomStringConvertible, P4DataType, P4DataValue {
|
public struct Action: CustomStringConvertible, P4Type, P4DataValue {
|
||||||
public func type() -> any Common.P4DataType {
|
public func type() -> any P4Type {
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,7 +29,7 @@ public struct Action: CustomStringConvertible, P4DataType, P4DataValue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func eq(rhs: any Common.P4DataType) -> Bool {
|
public func eq(rhs: any P4Type) -> Bool {
|
||||||
return switch rhs {
|
return switch rhs {
|
||||||
case is Action: true
|
case is Action: true
|
||||||
default: false
|
default: false
|
||||||
@@ -64,8 +64,8 @@ public struct Action: CustomStringConvertible, P4DataType, P4DataValue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func def() -> any Common.P4DataValue {
|
public func def() -> P4DataValue? {
|
||||||
return Action()
|
return .none
|
||||||
}
|
}
|
||||||
|
|
||||||
public var description: String {
|
public var description: String {
|
||||||
@@ -194,20 +194,20 @@ public struct Table: CustomStringConvertible {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct Control: P4DataType, P4DataValue, Equatable, CustomStringConvertible {
|
public struct Control: P4Type, P4DataValue, Equatable, CustomStringConvertible {
|
||||||
public static func == (lhs: Control, rhs: Control) -> Bool {
|
public static func == (lhs: Control, rhs: Control) -> Bool {
|
||||||
// Two "bare" controls are always equal.
|
// Two "bare" controls are always equal.
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
public func eq(rhs: any Common.P4DataType) -> Bool {
|
public func eq(rhs: any P4Type) -> Bool {
|
||||||
return switch rhs {
|
return switch rhs {
|
||||||
case is Control: true
|
case is Control: true
|
||||||
default: false
|
default: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func type() -> any Common.P4DataType {
|
public func type() -> any P4Type {
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -284,15 +284,8 @@ public struct Control: P4DataType, P4DataValue, Equatable, CustomStringConvertib
|
|||||||
withActions: self.actions, withApply: self.apply)
|
withActions: self.actions, withApply: self.apply)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func def() -> any P4DataValue {
|
public func def() -> P4DataValue? {
|
||||||
return Control(
|
return .none
|
||||||
named: Identifier(name: ""),
|
|
||||||
withParameters: ParameterList(),
|
|
||||||
withTable: Table(
|
|
||||||
withName: Identifier(name: "empty"),
|
|
||||||
withPropertyList: TablePropertyList(
|
|
||||||
withActions: TableActionsProperty(), withKeys: TableKeys())),
|
|
||||||
withActions: Actions(withActions: []), withApply: ApplyStatement())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
import Common
|
import Common
|
||||||
|
|
||||||
public struct Declaration: P4DataType {
|
public struct Declaration: P4Type {
|
||||||
public let identifier: TypedIdentifier
|
public let identifier: TypedIdentifier
|
||||||
public let extern: Bool
|
public let extern: Bool
|
||||||
public let ffi: P4FFI?
|
public let ffi: P4FFI?
|
||||||
@@ -34,21 +34,21 @@ public struct Declaration: P4DataType {
|
|||||||
self.extern = true
|
self.extern = true
|
||||||
}
|
}
|
||||||
|
|
||||||
public func eq(rhs: any Common.P4DataType) -> Bool {
|
public func eq(rhs: any Common.P4Type) -> Bool {
|
||||||
return switch rhs {
|
return switch rhs {
|
||||||
case let rrhs as Declaration:
|
case let rrhs as Declaration:
|
||||||
self.identifier.type.dataType().eq(rhs: rrhs.identifier.type.dataType())
|
self.identifier.type.baseType().eq(rhs: rrhs.identifier.type.baseType())
|
||||||
&& self.extern == rrhs.extern
|
&& self.extern == rrhs.extern
|
||||||
default: false
|
default: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func def() -> any Common.P4DataValue {
|
public func def() -> P4DataValue? {
|
||||||
/// TODO: Is a default of the extern'd type the right way to go?
|
/// TODO: Is a default of the extern'd type the right way to go?
|
||||||
return self.identifier.type.dataType().def()
|
return self.identifier.type.baseType().def()
|
||||||
}
|
}
|
||||||
|
|
||||||
public func type() -> any Common.P4DataType {
|
public func type() -> any Common.P4Type {
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
public var description: String {
|
public var description: String {
|
||||||
@@ -58,12 +58,12 @@ public struct Declaration: P4DataType {
|
|||||||
|
|
||||||
public struct ExternDeclaration {}
|
public struct ExternDeclaration {}
|
||||||
|
|
||||||
public struct FunctionDeclaration: P4DataType, P4DataValue {
|
public struct FunctionDeclaration: P4Type, P4DataValue {
|
||||||
public func type() -> any Common.P4DataType {
|
public func type() -> any Common.P4Type {
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
public func eq(rhs: any Common.P4DataType) -> Bool {
|
public func eq(rhs: any Common.P4Type) -> Bool {
|
||||||
switch rhs {
|
switch rhs {
|
||||||
case let frhs as FunctionDeclaration:
|
case let frhs as FunctionDeclaration:
|
||||||
return frhs.tipe.eq(self.tipe) && frhs.params == self.params
|
return frhs.tipe.eq(self.tipe) && frhs.params == self.params
|
||||||
@@ -73,7 +73,7 @@ public struct FunctionDeclaration: P4DataType, P4DataValue {
|
|||||||
|
|
||||||
public func eq(rhs: any Common.P4DataValue) -> Bool {
|
public func eq(rhs: any Common.P4DataValue) -> Bool {
|
||||||
switch rhs {
|
switch rhs {
|
||||||
case let frhs as FunctionDeclaration: return self.eq(rhs: frhs as P4DataType)
|
case let frhs as FunctionDeclaration: return self.eq(rhs: frhs as P4Type)
|
||||||
default: return false
|
default: return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -107,11 +107,8 @@ public struct FunctionDeclaration: P4DataType, P4DataValue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func def() -> any Common.P4DataValue {
|
public func def() -> P4DataValue? {
|
||||||
return FunctionDeclaration(
|
return .none
|
||||||
named: Identifier(name: ""), ofType: P4Type(P4Boolean()),
|
|
||||||
withParameters: ParameterList([]),
|
|
||||||
withBody: .none)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public var description: String {
|
public var description: String {
|
||||||
@@ -121,10 +118,10 @@ public struct FunctionDeclaration: P4DataType, P4DataValue {
|
|||||||
public var body: BlockStatement?
|
public var body: BlockStatement?
|
||||||
public var params: ParameterList
|
public var params: ParameterList
|
||||||
public var name: Identifier
|
public var name: Identifier
|
||||||
public var tipe: P4Type
|
public var tipe: P4QualifiedType
|
||||||
|
|
||||||
public init(
|
public init(
|
||||||
named name: Identifier, ofType type: P4Type, withParameters parameters: ParameterList,
|
named name: Identifier, ofType type: P4QualifiedType, withParameters parameters: ParameterList,
|
||||||
withBody body: BlockStatement?
|
withBody body: BlockStatement?
|
||||||
) {
|
) {
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|||||||
@@ -24,8 +24,8 @@ public struct KeysetExpression {
|
|||||||
self.key = key
|
self.key = key
|
||||||
}
|
}
|
||||||
|
|
||||||
public func compatible(type: P4Type) -> Result<()> {
|
public func compatible(type: P4QualifiedType) -> Result<()> {
|
||||||
if let key_type = self.key.type().dataType() as? P4Set {
|
if let key_type = self.key.type().baseType() as? P4Set {
|
||||||
if !key_type.set_type().eq(type) {
|
if !key_type.set_type().eq(type) {
|
||||||
return .Error(
|
return .Error(
|
||||||
Error(
|
Error(
|
||||||
@@ -84,7 +84,9 @@ public struct SelectExpression {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public typealias NamedBinaryOperatorEvaluator = (String, P4Type, (P4Value, P4Value) -> P4DataValue)
|
public typealias NamedBinaryOperatorEvaluator = (
|
||||||
|
String, P4QualifiedType, (P4Value, P4Value) -> P4DataValue
|
||||||
|
)
|
||||||
public typealias BinaryOperatorEvaluator = (P4Value, P4Value) -> P4DataValue
|
public typealias BinaryOperatorEvaluator = (P4Value, P4Value) -> P4DataValue
|
||||||
|
|
||||||
public struct BinaryOperatorExpression {
|
public struct BinaryOperatorExpression {
|
||||||
@@ -130,18 +132,18 @@ public struct FieldAccessExpression {
|
|||||||
public struct FunctionCall {
|
public struct FunctionCall {
|
||||||
public let callee: (FunctionDeclaration?, P4FFI?)
|
public let callee: (FunctionDeclaration?, P4FFI?)
|
||||||
public let arguments: ArgumentList
|
public let arguments: ArgumentList
|
||||||
public let return_type: P4DataType
|
public let return_type: P4Type
|
||||||
|
|
||||||
public init(_ callee: FunctionDeclaration, withArguments arguments: ArgumentList) {
|
public init(_ callee: FunctionDeclaration, withArguments arguments: ArgumentList) {
|
||||||
self.callee = (callee, .none)
|
self.callee = (callee, .none)
|
||||||
self.arguments = arguments
|
self.arguments = arguments
|
||||||
self.return_type = callee.tipe.dataType()
|
self.return_type = callee.tipe.baseType()
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(_ callee: P4FFI, withArguments arguments: ArgumentList) {
|
public init(_ callee: P4FFI, withArguments arguments: ArgumentList) {
|
||||||
self.callee = (.none, callee)
|
self.callee = (.none, callee)
|
||||||
self.arguments = arguments
|
self.arguments = arguments
|
||||||
/// ASSUME: That the FFI has been checked and the type is always a function declaration.
|
/// ASSUME: That the FFI has been checked and the type is always a function declaration.
|
||||||
self.return_type = (callee.type().dataType() as! FunctionDeclaration).tipe.dataType()
|
self.return_type = (callee.type().baseType() as! FunctionDeclaration).tipe.baseType()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-12
@@ -41,20 +41,20 @@ public struct ParserAssignmentStatement {
|
|||||||
///
|
///
|
||||||
/// Note: A P4 Parser State is both a type and a value.
|
/// 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.
|
/// This "bare" parser state represents the state more as a type than a value.
|
||||||
public class ParserState: P4DataType, P4DataValue, Equatable, CustomStringConvertible {
|
public class ParserState: P4Type, P4DataValue, Equatable, CustomStringConvertible {
|
||||||
public static func == (lhs: ParserState, rhs: ParserState) -> Bool {
|
public static func == (lhs: ParserState, rhs: ParserState) -> Bool {
|
||||||
// Two "bare" parser states are always equal.
|
// Two "bare" parser states are always equal.
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
public func eq(rhs: any Common.P4DataType) -> Bool {
|
public func eq(rhs: any Common.P4Type) -> Bool {
|
||||||
return switch rhs {
|
return switch rhs {
|
||||||
case is ParserState: true
|
case is ParserState: true
|
||||||
default: false
|
default: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func type() -> any Common.P4DataType {
|
public func type() -> any Common.P4Type {
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,8 +101,8 @@ public class ParserState: P4DataType, P4DataValue, Equatable, CustomStringConver
|
|||||||
/// Construct a ParserState
|
/// Construct a ParserState
|
||||||
public init() {}
|
public init() {}
|
||||||
|
|
||||||
public func def() -> any P4DataValue {
|
public func def() -> P4DataValue? {
|
||||||
return ParserState()
|
return .none
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,14 +116,14 @@ public class InstantiatedParserState: ParserState {
|
|||||||
return lhs.state == rhs.state
|
return lhs.state == rhs.state
|
||||||
}
|
}
|
||||||
|
|
||||||
public override func eq(rhs: any Common.P4DataType) -> Bool {
|
public override func eq(rhs: any Common.P4Type) -> Bool {
|
||||||
return switch rhs {
|
return switch rhs {
|
||||||
case is ParserState: true
|
case is ParserState: true
|
||||||
default: false
|
default: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override func type() -> any Common.P4DataType {
|
public override func type() -> any Common.P4Type {
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -276,12 +276,12 @@ public struct ParserStates {
|
|||||||
/// A P4 Parser
|
/// A P4 Parser
|
||||||
///
|
///
|
||||||
/// Note: A Parser is both a type _and_ a value.
|
/// Note: A Parser is both a type _and_ a value.
|
||||||
public struct Parser: P4DataType, P4DataValue {
|
public struct Parser: P4Type, P4DataValue {
|
||||||
public func type() -> any Common.P4DataType {
|
public func type() -> any Common.P4Type {
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
public func eq(rhs: any Common.P4DataType) -> Bool {
|
public func eq(rhs: any Common.P4Type) -> Bool {
|
||||||
return switch rhs {
|
return switch rhs {
|
||||||
case let parser_rhs as Parser: self.name == parser_rhs.name
|
case let parser_rhs as Parser: self.name == parser_rhs.name
|
||||||
default: false
|
default: false
|
||||||
@@ -353,8 +353,8 @@ public struct Parser: P4DataType, P4DataValue {
|
|||||||
return "Parser \(self.name) with parameters: \(parameters) and states: \(self.states)"
|
return "Parser \(self.name) with parameters: \(parameters) and states: \(self.states)"
|
||||||
}
|
}
|
||||||
|
|
||||||
public func def() -> any P4DataValue {
|
public func def() -> P4DataValue? {
|
||||||
return Parser(withName: Identifier(name: ""))
|
return .none
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,17 +26,17 @@ public struct ExpressionStatement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public struct Program {
|
public struct Program {
|
||||||
public var types: [P4DataType] = Array()
|
public var types: [P4Type] = Array()
|
||||||
public var externs: [P4DataType] = Array()
|
public var externs: [P4Type] = Array()
|
||||||
public var instances: [P4Type] = Array()
|
public var instances: [P4QualifiedType] = Array()
|
||||||
|
|
||||||
/// Type of closure for filtering results from ``Program/InstancesWithTypes(_:)``
|
/// Type of closure for filtering results from ``Program/InstancesWithTypes(_:)``
|
||||||
public typealias TypeFilter = (P4Type) -> Bool
|
public typealias TypeFilter = (P4QualifiedType) -> Bool
|
||||||
/// Type of closure for filtering results from ``Program/TypesWithTypes(_:)``
|
/// Type of closure for filtering results from ``Program/TypesWithTypes(_:)``
|
||||||
public typealias DataTypeFilter = (P4DataType) -> Bool
|
public typealias DataTypeFilter = (P4Type) -> Bool
|
||||||
|
|
||||||
/// Retrieve global instances in the compiled P4 program.
|
/// Retrieve global instances in the compiled P4 program.
|
||||||
public func InstancesWithTypes() -> [P4Type] {
|
public func InstancesWithTypes() -> [P4QualifiedType] {
|
||||||
return self.instances
|
return self.instances
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,14 +52,14 @@ public struct Program {
|
|||||||
///
|
///
|
||||||
/// @Snippet(path: "use-program-instanceswithtypes", slice: "include")
|
/// @Snippet(path: "use-program-instanceswithtypes", slice: "include")
|
||||||
///
|
///
|
||||||
public func InstancesWithTypes(_ filter: TypeFilter) -> [P4Type] {
|
public func InstancesWithTypes(_ filter: TypeFilter) -> [P4QualifiedType] {
|
||||||
return self.instances.filter { instance in
|
return self.instances.filter { instance in
|
||||||
filter(instance)
|
filter(instance)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieve global types in the compiled P4 program.
|
/// Retrieve global types in the compiled P4 program.
|
||||||
public func TypesWithTypes() -> [P4DataType] {
|
public func TypesWithTypes() -> [P4Type] {
|
||||||
return self.types
|
return self.types
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,14 +75,14 @@ public struct Program {
|
|||||||
///
|
///
|
||||||
/// @Snippet(path: "use-program-typeswithtypes", slice: "include")
|
/// @Snippet(path: "use-program-typeswithtypes", slice: "include")
|
||||||
///
|
///
|
||||||
public func TypesWithTypes(_ filter: DataTypeFilter) -> [P4DataType] {
|
public func TypesWithTypes(_ filter: DataTypeFilter) -> [P4Type] {
|
||||||
return self.types.filter { instance in
|
return self.types.filter { instance in
|
||||||
filter(instance)
|
filter(instance)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieve extern types in the compiled P4 program.
|
/// Retrieve extern types in the compiled P4 program.
|
||||||
public func Externs() -> [P4DataType] {
|
public func Externs() -> [P4Type] {
|
||||||
return self.externs
|
return self.externs
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,7 +98,7 @@ public struct Program {
|
|||||||
///
|
///
|
||||||
/// @Snippet(path: "use-program-typeswithtypes", slice: "include")
|
/// @Snippet(path: "use-program-typeswithtypes", slice: "include")
|
||||||
///
|
///
|
||||||
public func Externs(_ filter: DataTypeFilter) -> [P4DataType] {
|
public func Externs(_ filter: DataTypeFilter) -> [P4Type] {
|
||||||
return self.externs.filter { instance in
|
return self.externs.filter { instance in
|
||||||
filter(instance)
|
filter(instance)
|
||||||
}
|
}
|
||||||
@@ -113,7 +113,7 @@ public struct Program {
|
|||||||
|
|
||||||
public func find_parser(withName name: Identifier) -> Result<Parser> {
|
public func find_parser(withName name: Identifier) -> Result<Parser> {
|
||||||
for instance in self.instances {
|
for instance in self.instances {
|
||||||
guard let parser = instance.dataType() as? Parser else {
|
guard let parser = instance.baseType() as? Parser else {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if parser.name == name {
|
if parser.name == name {
|
||||||
|
|||||||
@@ -18,10 +18,10 @@
|
|||||||
import Common
|
import Common
|
||||||
|
|
||||||
public struct AttributedP4Type {
|
public struct AttributedP4Type {
|
||||||
public let type: P4DataType
|
public let type: P4Type
|
||||||
public let attributes: P4Type
|
public let attributes: P4QualifiedType
|
||||||
|
|
||||||
public init(_ type: P4DataType, _ attributes: P4Type) {
|
public init(_ type: P4Type, _ attributes: P4QualifiedType) {
|
||||||
self.type = type
|
self.type = type
|
||||||
self.attributes = attributes
|
self.attributes = attributes
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ extension SelectCaseExpression: EvaluatableExpression {
|
|||||||
return (execution.scopes.lookup(identifier: next_state_identifier), execution)
|
return (execution.scopes.lookup(identifier: next_state_identifier), execution)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func type() -> P4Type {
|
public func type() -> P4QualifiedType {
|
||||||
return P4Type(ParserState())
|
return P4QualifiedType(ParserState())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,14 +49,14 @@ extension SelectExpression: EvaluatableExpression {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func type() -> P4Type {
|
public func type() -> P4QualifiedType {
|
||||||
return P4Type(ParserState())
|
return P4QualifiedType(ParserState())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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() -> P4Type {
|
public func type() -> P4QualifiedType {
|
||||||
return self.type
|
return self.type
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,7 +192,7 @@ public func binary_and_or_operator_checker(
|
|||||||
left: EvaluatableExpression, right: EvaluatableExpression
|
left: EvaluatableExpression, right: EvaluatableExpression
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
// Check that both are Boolean-typed things!
|
// Check that both are Boolean-typed things!
|
||||||
if !(left.type().dataType().eq(rhs: P4Boolean()) && right.type().dataType().eq(rhs: P4Boolean()))
|
if !(left.type().baseType().eq(rhs: P4Boolean()) && right.type().baseType().eq(rhs: P4Boolean()))
|
||||||
{
|
{
|
||||||
return .Error(Error(withMessage: "And/Or on operands with non-bool type is not allowed"))
|
return .Error(Error(withMessage: "And/Or on operands with non-bool type is not allowed"))
|
||||||
}
|
}
|
||||||
@@ -203,7 +203,7 @@ public func binary_int_math_operator_checker(
|
|||||||
left: EvaluatableExpression, right: EvaluatableExpression
|
left: EvaluatableExpression, right: EvaluatableExpression
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
// Check that both are int-typed things!
|
// Check that both are int-typed things!
|
||||||
if !(left.type().dataType().eq(rhs: P4Int()) && right.type().dataType().eq(rhs: P4Int())) {
|
if !(left.type().baseType().eq(rhs: P4Int()) && right.type().baseType().eq(rhs: P4Int())) {
|
||||||
return .Error(
|
return .Error(
|
||||||
Error(withMessage: "Mathematical operation on operands with non-int type is not allowed"))
|
Error(withMessage: "Mathematical operation on operands with non-int type is not allowed"))
|
||||||
}
|
}
|
||||||
@@ -230,7 +230,7 @@ extension BinaryOperatorExpression: EvaluatableExpression {
|
|||||||
return (.Ok(P4Value(self.evaluator.2(evaluated_left, evaluated_right))), updated_execution)
|
return (.Ok(P4Value(self.evaluator.2(evaluated_left, evaluated_right))), updated_execution)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func type() -> P4Type {
|
public func type() -> P4QualifiedType {
|
||||||
return self.evaluator.1
|
return self.evaluator.1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -264,7 +264,7 @@ extension ArrayAccessExpression: EvaluatableExpression {
|
|||||||
return (.Ok(accessed), updated_execution)
|
return (.Ok(accessed), updated_execution)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func type() -> P4Type {
|
public func type() -> P4QualifiedType {
|
||||||
return self.type.value_type()
|
return self.type.value_type()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -375,7 +375,7 @@ extension FieldAccessExpression: EvaluatableExpression {
|
|||||||
return (.Ok(value), updated_execution)
|
return (.Ok(value), updated_execution)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func type() -> P4Type {
|
public func type() -> P4QualifiedType {
|
||||||
return self.field.type
|
return self.field.type
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -467,7 +467,7 @@ extension KeysetExpression: EvaluatableExpression {
|
|||||||
return execution.evaluator.EvaluateExpression(self.key, inExecution: execution)
|
return execution.evaluator.EvaluateExpression(self.key, inExecution: execution)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func type() -> P4Type {
|
public func type() -> P4QualifiedType {
|
||||||
return self.key.type()
|
return self.key.type()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -524,8 +524,8 @@ extension FunctionCall: EvaluatableExpression {
|
|||||||
inExecution: execution)
|
inExecution: execution)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func type() -> P4Type {
|
public func type() -> P4QualifiedType {
|
||||||
return P4Type(self.return_type)
|
return P4QualifiedType(self.return_type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ extension ParserStateDirectTransition: EvaluatableParserState {
|
|||||||
let res = program.scopes.lookup(identifier: get_next_state())
|
let res = program.scopes.lookup(identifier: get_next_state())
|
||||||
|
|
||||||
if case .Ok(let value) = res {
|
if case .Ok(let value) = res {
|
||||||
if value.type().dataType().eq(rhs: self) {
|
if value.type().baseType().eq(rhs: self) {
|
||||||
return (value.dataValue() as! EvaluatableParserState, program.exit_scope())
|
return (value.dataValue() as! EvaluatableParserState, program.exit_scope())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -119,7 +119,7 @@ extension ParserStateSelectTransition: EvaluatableParserState {
|
|||||||
//switch self.selectExpression.evaluate(execution: program) {
|
//switch self.selectExpression.evaluate(execution: program) {
|
||||||
switch program.evaluator.EvaluateExpression(self.selectExpression, inExecution: program) {
|
switch program.evaluator.EvaluateExpression(self.selectExpression, inExecution: program) {
|
||||||
case (.Ok(let value), let program):
|
case (.Ok(let value), let program):
|
||||||
if value.type().dataType().eq(rhs: self) {
|
if value.type().baseType().eq(rhs: self) {
|
||||||
return (value.dataValue() as! EvaluatableParserState, program.exit_scope())
|
return (value.dataValue() as! EvaluatableParserState, program.exit_scope())
|
||||||
} else {
|
} else {
|
||||||
return (
|
return (
|
||||||
@@ -150,10 +150,10 @@ extension Parser: LibraryCallable {
|
|||||||
|
|
||||||
execution = execution.declare(
|
execution = execution.declare(
|
||||||
identifier: AsInstantiatedParserState(accept.state()).state,
|
identifier: AsInstantiatedParserState(accept.state()).state,
|
||||||
withValue: P4Value(accept, P4Type.ReadOnly(accept.type())))
|
withValue: P4Value(accept, P4QualifiedType.ReadOnly(accept.type())))
|
||||||
execution = execution.declare(
|
execution = execution.declare(
|
||||||
identifier: AsInstantiatedParserState(reject.state()).state,
|
identifier: AsInstantiatedParserState(reject.state()).state,
|
||||||
withValue: P4Value(reject, P4Type.ReadOnly(reject.type())))
|
withValue: P4Value(reject, P4QualifiedType.ReadOnly(reject.type())))
|
||||||
|
|
||||||
// Add initial values to the global scope
|
// Add initial values to the global scope
|
||||||
for (name, value) in execution.getGlobalValues() {
|
for (name, value) in execution.getGlobalValues() {
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ extension ConditionalStatement: EvaluatableStatement {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !evaluated_condition.type().dataType().eq(rhs: P4Boolean()) {
|
if !evaluated_condition.type().baseType().eq(rhs: P4Boolean()) {
|
||||||
return (
|
return (
|
||||||
ControlFlow.Error,
|
ControlFlow.Error,
|
||||||
execution.setError(error: Error(withMessage: "Condition expression is not a Boolean"))
|
execution.setError(error: Error(withMessage: "Condition expression is not a Boolean"))
|
||||||
|
|||||||
@@ -40,11 +40,11 @@ import TreeSitterP4
|
|||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_declarations = VarTypeScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Int()))))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
|
||||||
var test_values = VarValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
identifier: Identifier(name: "ta"),
|
identifier: Identifier(name: "ta"),
|
||||||
withValue: P4Value(P4ArrayValue(withType: P4Type(P4Int()), withValue: [
|
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [
|
||||||
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))
|
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))
|
||||||
])))
|
])))
|
||||||
let program = try #UseOkResult(
|
let program = try #UseOkResult(
|
||||||
@@ -68,7 +68,7 @@ import TreeSitterP4
|
|||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_declarations = VarTypeScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Int()))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Int()))
|
||||||
#expect(
|
#expect(
|
||||||
#RequireErrorResult(
|
#RequireErrorResult(
|
||||||
Error(
|
Error(
|
||||||
@@ -91,12 +91,12 @@ import TreeSitterP4
|
|||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_declarations = VarTypeScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Int()))))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
|
||||||
var test_values = VarValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
|
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
identifier: Identifier(name: "ta"),
|
identifier: Identifier(name: "ta"),
|
||||||
withValue: P4Value(P4ArrayValue(withType: P4Type(P4Int()), withValue: [
|
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [
|
||||||
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))
|
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))
|
||||||
])))
|
])))
|
||||||
let program = try #UseOkResult(
|
let program = try #UseOkResult(
|
||||||
@@ -119,11 +119,11 @@ import TreeSitterP4
|
|||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_declarations = VarTypeScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Int()))))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
|
||||||
var test_values = VarValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
identifier: Identifier(name: "ta"),
|
identifier: Identifier(name: "ta"),
|
||||||
withValue: P4Value(P4ArrayValue(withType: P4Type(P4Int()), withValue: [
|
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [
|
||||||
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3)),
|
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3)),
|
||||||
])))
|
])))
|
||||||
let program = try #UseOkResult(
|
let program = try #UseOkResult(
|
||||||
@@ -146,11 +146,11 @@ import TreeSitterP4
|
|||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_declarations = VarTypeScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Int()))))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
|
||||||
var test_values = VarValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
identifier: Identifier(name: "ta"),
|
identifier: Identifier(name: "ta"),
|
||||||
withValue: P4Value(P4ArrayValue(withType: P4Type(P4Int()), withValue: [
|
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [
|
||||||
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3)),
|
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3)),
|
||||||
])))
|
])))
|
||||||
let program = try #UseOkResult(
|
let program = try #UseOkResult(
|
||||||
@@ -174,16 +174,16 @@ import TreeSitterP4
|
|||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_declarations = VarTypeScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Array(withValueType: P4Type(P4Int()))))))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))))
|
||||||
var test_values = VarValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
|
|
||||||
let nested = P4Value(P4ArrayValue(
|
let nested = P4Value(P4ArrayValue(
|
||||||
withType: P4Type(P4Int()),
|
withType: P4QualifiedType(P4Int()),
|
||||||
withValue: [P4Value(P4IntValue(withValue: 5)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))]))
|
withValue: [P4Value(P4IntValue(withValue: 5)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))]))
|
||||||
|
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
identifier: Identifier(name: "ta"),
|
identifier: Identifier(name: "ta"),
|
||||||
withValue: P4Value(P4ArrayValue(withType: P4Type(P4Array(withValueType: P4Type(P4Int()))), withValue: [nested])))
|
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))), withValue: [nested])))
|
||||||
|
|
||||||
let program = try #UseOkResult(
|
let program = try #UseOkResult(
|
||||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||||
@@ -207,11 +207,11 @@ import TreeSitterP4
|
|||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_declarations = VarTypeScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Int()))))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
|
||||||
var test_values = VarValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
identifier: Identifier(name: "ta"),
|
identifier: Identifier(name: "ta"),
|
||||||
withValue: P4Value(P4ArrayValue(withType: P4Type(P4Int()), withValue: [
|
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [
|
||||||
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3)),
|
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3)),
|
||||||
])))
|
])))
|
||||||
let program = try #UseOkResult(
|
let program = try #UseOkResult(
|
||||||
@@ -236,16 +236,16 @@ import TreeSitterP4
|
|||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_declarations = VarTypeScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Array(withValueType: P4Type(P4Int()))))))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))))
|
||||||
var test_values = VarValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
|
|
||||||
let nested = P4Value(P4ArrayValue(
|
let nested = P4Value(P4ArrayValue(
|
||||||
withType: P4Type(P4Int()),
|
withType: P4QualifiedType(P4Int()),
|
||||||
withValue: [P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))]))
|
withValue: [P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))]))
|
||||||
|
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
identifier: Identifier(name: "ta"),
|
identifier: Identifier(name: "ta"),
|
||||||
withValue: P4Value(P4ArrayValue(withType: P4Type(P4Array(withValueType: P4Type(P4Int()))), withValue: [nested])))
|
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))), withValue: [nested])))
|
||||||
|
|
||||||
let program = try #UseOkResult(
|
let program = try #UseOkResult(
|
||||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||||
|
|||||||
@@ -42,8 +42,8 @@ import TreeSitterP4
|
|||||||
"""
|
"""
|
||||||
let test_declarations = VarTypeScopes().enter()
|
let test_declarations = VarTypeScopes().enter()
|
||||||
let fields = P4StructFields([
|
let fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
|
||||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
|
||||||
])
|
])
|
||||||
var test_types = TypeTypeScopes().enter()
|
var test_types = TypeTypeScopes().enter()
|
||||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||||
@@ -75,8 +75,8 @@ import TreeSitterP4
|
|||||||
"""
|
"""
|
||||||
let test_declarations = VarTypeScopes().enter()
|
let test_declarations = VarTypeScopes().enter()
|
||||||
let fields = P4StructFields([
|
let fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
|
||||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
|
||||||
])
|
])
|
||||||
var test_types = TypeTypeScopes().enter()
|
var test_types = TypeTypeScopes().enter()
|
||||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||||
@@ -109,8 +109,8 @@ import TreeSitterP4
|
|||||||
"""
|
"""
|
||||||
let test_declarations = VarTypeScopes().enter()
|
let test_declarations = VarTypeScopes().enter()
|
||||||
let fields = P4StructFields([
|
let fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
|
||||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
|
||||||
])
|
])
|
||||||
var test_types = TypeTypeScopes().enter()
|
var test_types = TypeTypeScopes().enter()
|
||||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||||
@@ -146,8 +146,8 @@ import TreeSitterP4
|
|||||||
"""
|
"""
|
||||||
let test_declarations = VarTypeScopes().enter()
|
let test_declarations = VarTypeScopes().enter()
|
||||||
let fields = P4StructFields([
|
let fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
|
||||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
|
||||||
])
|
])
|
||||||
var test_types = TypeTypeScopes().enter()
|
var test_types = TypeTypeScopes().enter()
|
||||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||||
@@ -183,8 +183,8 @@ import TreeSitterP4
|
|||||||
"""
|
"""
|
||||||
let test_declarations = VarTypeScopes().enter()
|
let test_declarations = VarTypeScopes().enter()
|
||||||
let fields = P4StructFields([
|
let fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
|
||||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
|
||||||
])
|
])
|
||||||
var test_types = TypeTypeScopes().enter()
|
var test_types = TypeTypeScopes().enter()
|
||||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||||
|
|||||||
@@ -41,8 +41,8 @@ import P4Lang
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
let x = { (tipe: P4Type) -> Bool in
|
let x = { (tipe: P4QualifiedType) -> Bool in
|
||||||
switch tipe.dataType() {
|
switch tipe.baseType() {
|
||||||
case let c as Control: c.name == "simple"
|
case let c as Control: c.name == "simple"
|
||||||
default: false
|
default: false
|
||||||
}
|
}
|
||||||
@@ -79,8 +79,8 @@ import P4Lang
|
|||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
|
|
||||||
let filter = { (tipe: P4Type) -> Bool in
|
let filter = { (tipe: P4QualifiedType) -> Bool in
|
||||||
switch tipe.dataType() {
|
switch tipe.baseType() {
|
||||||
case let c as Control: c.name == "simple" || c.name == "complex"
|
case let c as Control: c.name == "simple" || c.name == "complex"
|
||||||
default: false
|
default: false
|
||||||
}
|
}
|
||||||
@@ -106,8 +106,8 @@ import P4Lang
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
let x = { (tipe: P4Type) -> Bool in
|
let x = { (tipe: P4QualifiedType) -> Bool in
|
||||||
switch tipe.dataType() {
|
switch tipe.baseType() {
|
||||||
case let c as Control: c.name == "simple"
|
case let c as Control: c.name == "simple"
|
||||||
default: false
|
default: false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,24 +53,24 @@ import TreeSitterP4
|
|||||||
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
|
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
|
||||||
|
|
||||||
// Pull the control out of the compiled program.
|
// Pull the control out of the compiled program.
|
||||||
let controls = program.InstancesWithTypes() { (tipe: P4Type) -> Bool in
|
let controls = program.InstancesWithTypes() { (tipe: P4QualifiedType) -> Bool in
|
||||||
switch tipe.dataType() {
|
switch tipe.baseType() {
|
||||||
case let c as Control: c.name == "simple"
|
case let c as Control: c.name == "simple"
|
||||||
default: false
|
default: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var control = ((controls[0].dataType() as P4DataType) as! Control)
|
var control = ((controls[0].baseType() as P4Type) as! Control)
|
||||||
|
|
||||||
// Add entries to the table.
|
// Add entries to the table.
|
||||||
control = control.updateTable(
|
control = control.updateTable(
|
||||||
addEntry: (
|
addEntry: (
|
||||||
P4Value(P4BooleanValue(withValue: true)),
|
P4Value(P4BooleanValue(withValue: true)),
|
||||||
TypedIdentifier(name: "a", withType: P4Type(Action()))
|
TypedIdentifier(name: "a", withType: P4QualifiedType(Action()))
|
||||||
)
|
)
|
||||||
).updateTable(
|
).updateTable(
|
||||||
addEntry: (
|
addEntry: (
|
||||||
P4Value(P4BooleanValue(withValue: false)),
|
P4Value(P4BooleanValue(withValue: false)),
|
||||||
TypedIdentifier(name: "b", withType: P4Type(Action()))
|
TypedIdentifier(name: "b", withType: P4QualifiedType(Action()))
|
||||||
))
|
))
|
||||||
|
|
||||||
// Set a variable in the global scope for the inout first parameter.
|
// Set a variable in the global scope for the inout first parameter.
|
||||||
@@ -79,14 +79,14 @@ import TreeSitterP4
|
|||||||
identifier: Identifier(name: "result_arg"),
|
identifier: Identifier(name: "result_arg"),
|
||||||
withValue: P4Value(
|
withValue: P4Value(
|
||||||
P4IntValue(withValue: 0),
|
P4IntValue(withValue: 0),
|
||||||
P4Type(P4Int())))
|
P4QualifiedType(P4Int())))
|
||||||
|
|
||||||
let runtime = try #UseOkResult(
|
let runtime = try #UseOkResult(
|
||||||
P4Runtime.Runtime<P4TableHitMissValue, Control>.create(control: control, withGlobalValues: global_values))
|
P4Runtime.Runtime<P4TableHitMissValue, Control>.create(control: control, withGlobalValues: global_values))
|
||||||
|
|
||||||
let (hit_miss, updated_execution) = try #UseOkResult(runtime.run(
|
let (hit_miss, updated_execution) = try #UseOkResult(runtime.run(
|
||||||
withArguments: ArgumentList([
|
withArguments: ArgumentList([
|
||||||
Argument(TypedIdentifier(name: "result_arg", withType: P4Type(P4Int())), atIndex: 0),
|
Argument(TypedIdentifier(name: "result_arg", withType: P4QualifiedType(P4Int())), atIndex: 0),
|
||||||
Argument(P4Value(P4BooleanValue(withValue: true)), atIndex: 1),
|
Argument(P4Value(P4BooleanValue(withValue: true)), atIndex: 1),
|
||||||
])))
|
])))
|
||||||
|
|
||||||
@@ -124,24 +124,24 @@ import TreeSitterP4
|
|||||||
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
|
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
|
||||||
|
|
||||||
// Pull the control out of the compiled program.
|
// Pull the control out of the compiled program.
|
||||||
let controls = program.InstancesWithTypes() { (tipe: P4Type) -> Bool in
|
let controls = program.InstancesWithTypes() { (tipe: P4QualifiedType) -> Bool in
|
||||||
switch tipe.dataType() {
|
switch tipe.baseType() {
|
||||||
case let c as Control: c.name == "simple"
|
case let c as Control: c.name == "simple"
|
||||||
default: false
|
default: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var control = ((controls[0].dataType() as P4DataType) as! Control)
|
var control = ((controls[0].baseType() as P4Type) as! Control)
|
||||||
|
|
||||||
// Add entries to the table.
|
// Add entries to the table.
|
||||||
control = control.updateTable(
|
control = control.updateTable(
|
||||||
addEntry: (
|
addEntry: (
|
||||||
P4Value(P4BooleanValue(withValue: true)),
|
P4Value(P4BooleanValue(withValue: true)),
|
||||||
TypedIdentifier(name: "a", withType: P4Type(Action()))
|
TypedIdentifier(name: "a", withType: P4QualifiedType(Action()))
|
||||||
)
|
)
|
||||||
).updateTable(
|
).updateTable(
|
||||||
addEntry: (
|
addEntry: (
|
||||||
P4Value(P4BooleanValue(withValue: false)),
|
P4Value(P4BooleanValue(withValue: false)),
|
||||||
TypedIdentifier(name: "b", withType: P4Type(Action()))
|
TypedIdentifier(name: "b", withType: P4QualifiedType(Action()))
|
||||||
))
|
))
|
||||||
|
|
||||||
// Set a variable in the global scope for the inout first parameter.
|
// Set a variable in the global scope for the inout first parameter.
|
||||||
@@ -150,14 +150,14 @@ import TreeSitterP4
|
|||||||
identifier: Identifier(name: "result_arg"),
|
identifier: Identifier(name: "result_arg"),
|
||||||
withValue: P4Value(
|
withValue: P4Value(
|
||||||
P4IntValue(withValue: 0),
|
P4IntValue(withValue: 0),
|
||||||
P4Type(P4Int())))
|
P4QualifiedType(P4Int())))
|
||||||
|
|
||||||
let runtime = try #UseOkResult(
|
let runtime = try #UseOkResult(
|
||||||
P4Runtime.Runtime<P4TableHitMissValue, Control>.create(control: control, withGlobalValues: global_values))
|
P4Runtime.Runtime<P4TableHitMissValue, Control>.create(control: control, withGlobalValues: global_values))
|
||||||
|
|
||||||
let (hit_miss, updated_execution) = try #UseOkResult(runtime.run(
|
let (hit_miss, updated_execution) = try #UseOkResult(runtime.run(
|
||||||
withArguments: ArgumentList([
|
withArguments: ArgumentList([
|
||||||
Argument(TypedIdentifier(name: "result_arg", withType: P4Type(P4Int())), atIndex: 0),
|
Argument(TypedIdentifier(name: "result_arg", withType: P4QualifiedType(P4Int())), atIndex: 0),
|
||||||
Argument(P4Value(P4BooleanValue(withValue: false)), atIndex: 1),
|
Argument(P4Value(P4BooleanValue(withValue: false)), atIndex: 1),
|
||||||
])))
|
])))
|
||||||
|
|
||||||
@@ -195,24 +195,24 @@ import TreeSitterP4
|
|||||||
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
|
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
|
||||||
|
|
||||||
// Pull the control out of the compiled program.
|
// Pull the control out of the compiled program.
|
||||||
let controls = program.InstancesWithTypes() { (tipe: P4Type) -> Bool in
|
let controls = program.InstancesWithTypes() { (tipe: P4QualifiedType) -> Bool in
|
||||||
switch tipe.dataType() {
|
switch tipe.baseType() {
|
||||||
case let c as Control: c.name == "simple"
|
case let c as Control: c.name == "simple"
|
||||||
default: false
|
default: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var control = ((controls[0].dataType() as P4DataType) as! Control)
|
var control = ((controls[0].baseType() as P4Type) as! Control)
|
||||||
|
|
||||||
// Add entries to the table.
|
// Add entries to the table.
|
||||||
control = control.updateTable(
|
control = control.updateTable(
|
||||||
addEntry: (
|
addEntry: (
|
||||||
P4Value(P4IntValue(withValue: 5)),
|
P4Value(P4IntValue(withValue: 5)),
|
||||||
TypedIdentifier(name: "a", withType: P4Type(Action()))
|
TypedIdentifier(name: "a", withType: P4QualifiedType(Action()))
|
||||||
)
|
)
|
||||||
).updateTable(
|
).updateTable(
|
||||||
addEntry: (
|
addEntry: (
|
||||||
P4Value(P4IntValue(withValue: 2)),
|
P4Value(P4IntValue(withValue: 2)),
|
||||||
TypedIdentifier(name: "b", withType: P4Type(Action()))
|
TypedIdentifier(name: "b", withType: P4QualifiedType(Action()))
|
||||||
))
|
))
|
||||||
|
|
||||||
// Set a variable in the global scope for the inout first parameter.
|
// Set a variable in the global scope for the inout first parameter.
|
||||||
@@ -221,14 +221,14 @@ import TreeSitterP4
|
|||||||
identifier: Identifier(name: "result_arg"),
|
identifier: Identifier(name: "result_arg"),
|
||||||
withValue: P4Value(
|
withValue: P4Value(
|
||||||
P4IntValue(withValue: 0),
|
P4IntValue(withValue: 0),
|
||||||
P4Type(P4Int())))
|
P4QualifiedType(P4Int())))
|
||||||
|
|
||||||
let runtime = try #UseOkResult(
|
let runtime = try #UseOkResult(
|
||||||
P4Runtime.Runtime<P4TableHitMissValue, Control>.create(control: control, withGlobalValues: global_values))
|
P4Runtime.Runtime<P4TableHitMissValue, Control>.create(control: control, withGlobalValues: global_values))
|
||||||
|
|
||||||
let (hit_miss, updated_execution) = try #UseOkResult(runtime.run(
|
let (hit_miss, updated_execution) = try #UseOkResult(runtime.run(
|
||||||
withArguments: ArgumentList([
|
withArguments: ArgumentList([
|
||||||
Argument(TypedIdentifier(name: "result_arg", withType: P4Type(P4Int())), atIndex: 0),
|
Argument(TypedIdentifier(name: "result_arg", withType: P4QualifiedType(P4Int())), atIndex: 0),
|
||||||
Argument(P4Value(P4IntValue(withValue: 5)), atIndex: 1),
|
Argument(P4Value(P4IntValue(withValue: 5)), atIndex: 1),
|
||||||
])))
|
])))
|
||||||
|
|
||||||
@@ -265,24 +265,24 @@ import TreeSitterP4
|
|||||||
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
|
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
|
||||||
|
|
||||||
// Pull the control out of the compiled program.
|
// Pull the control out of the compiled program.
|
||||||
let controls = program.InstancesWithTypes() { (tipe: P4Type) -> Bool in
|
let controls = program.InstancesWithTypes() { (tipe: P4QualifiedType) -> Bool in
|
||||||
switch tipe.dataType() {
|
switch tipe.baseType() {
|
||||||
case let c as Control: c.name == "simple"
|
case let c as Control: c.name == "simple"
|
||||||
default: false
|
default: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var control = ((controls[0].dataType() as P4DataType) as! Control)
|
var control = ((controls[0].baseType() as P4Type) as! Control)
|
||||||
|
|
||||||
// Add entries to the table.
|
// Add entries to the table.
|
||||||
control = control.updateTable(
|
control = control.updateTable(
|
||||||
addEntry: (
|
addEntry: (
|
||||||
P4Value(P4IntValue(withValue: 1)),
|
P4Value(P4IntValue(withValue: 1)),
|
||||||
TypedIdentifier(name: "a", withType: P4Type(Action()))
|
TypedIdentifier(name: "a", withType: P4QualifiedType(Action()))
|
||||||
)
|
)
|
||||||
).updateTable(
|
).updateTable(
|
||||||
addEntry: (
|
addEntry: (
|
||||||
P4Value(P4IntValue(withValue: 2)),
|
P4Value(P4IntValue(withValue: 2)),
|
||||||
TypedIdentifier(name: "b", withType: P4Type(Action()))
|
TypedIdentifier(name: "b", withType: P4QualifiedType(Action()))
|
||||||
))
|
))
|
||||||
|
|
||||||
// Set a variable in the global scope for the inout first parameter.
|
// Set a variable in the global scope for the inout first parameter.
|
||||||
@@ -291,14 +291,14 @@ import TreeSitterP4
|
|||||||
identifier: Identifier(name: "result_arg"),
|
identifier: Identifier(name: "result_arg"),
|
||||||
withValue: P4Value(
|
withValue: P4Value(
|
||||||
P4IntValue(withValue: 0),
|
P4IntValue(withValue: 0),
|
||||||
P4Type(P4Int())))
|
P4QualifiedType(P4Int())))
|
||||||
|
|
||||||
let runtime = try #UseOkResult(
|
let runtime = try #UseOkResult(
|
||||||
P4Runtime.Runtime<P4TableHitMissValue, Control>.create(control: control, withGlobalValues: global_values))
|
P4Runtime.Runtime<P4TableHitMissValue, Control>.create(control: control, withGlobalValues: global_values))
|
||||||
|
|
||||||
let (hit_miss, updated_execution) = try #UseOkResult(runtime.run(
|
let (hit_miss, updated_execution) = try #UseOkResult(runtime.run(
|
||||||
withArguments: ArgumentList([
|
withArguments: ArgumentList([
|
||||||
Argument(TypedIdentifier(name: "result_arg", withType: P4Type(P4Int())), atIndex: 0),
|
Argument(TypedIdentifier(name: "result_arg", withType: P4QualifiedType(P4Int())), atIndex: 0),
|
||||||
Argument(P4Value(P4IntValue(withValue: 3)), atIndex: 1),
|
Argument(P4Value(P4IntValue(withValue: 3)), atIndex: 1),
|
||||||
])))
|
])))
|
||||||
|
|
||||||
@@ -336,24 +336,24 @@ import TreeSitterP4
|
|||||||
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
|
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
|
||||||
|
|
||||||
// Pull the control out of the compiled program.
|
// Pull the control out of the compiled program.
|
||||||
let controls = program.InstancesWithTypes() { (tipe: P4Type) -> Bool in
|
let controls = program.InstancesWithTypes() { (tipe: P4QualifiedType) -> Bool in
|
||||||
switch tipe.dataType() {
|
switch tipe.baseType() {
|
||||||
case let c as Control: c.name == "simple"
|
case let c as Control: c.name == "simple"
|
||||||
default: false
|
default: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var control = ((controls[0].dataType() as P4DataType) as! Control)
|
var control = ((controls[0].baseType() as P4Type) as! Control)
|
||||||
|
|
||||||
// Add entries to the table.
|
// Add entries to the table.
|
||||||
control = control.updateTable(
|
control = control.updateTable(
|
||||||
addEntry: (
|
addEntry: (
|
||||||
P4Value(P4BooleanValue(withValue: true)),
|
P4Value(P4BooleanValue(withValue: true)),
|
||||||
TypedIdentifier(name: "a", withType: P4Type(Action()))
|
TypedIdentifier(name: "a", withType: P4QualifiedType(Action()))
|
||||||
)
|
)
|
||||||
).updateTable(
|
).updateTable(
|
||||||
addEntry: (
|
addEntry: (
|
||||||
P4Value(P4IntValue(withValue: 5)),
|
P4Value(P4IntValue(withValue: 5)),
|
||||||
TypedIdentifier(name: "b", withType: P4Type(Action()))
|
TypedIdentifier(name: "b", withType: P4QualifiedType(Action()))
|
||||||
))
|
))
|
||||||
|
|
||||||
// Set a variable in the global scope for the inout first parameter.
|
// Set a variable in the global scope for the inout first parameter.
|
||||||
@@ -362,14 +362,14 @@ import TreeSitterP4
|
|||||||
identifier: Identifier(name: "result_arg"),
|
identifier: Identifier(name: "result_arg"),
|
||||||
withValue: P4Value(
|
withValue: P4Value(
|
||||||
P4IntValue(withValue: 0),
|
P4IntValue(withValue: 0),
|
||||||
P4Type(P4Int())))
|
P4QualifiedType(P4Int())))
|
||||||
|
|
||||||
let runtime = try #UseOkResult(
|
let runtime = try #UseOkResult(
|
||||||
P4Runtime.Runtime<P4TableHitMissValue, Control>.create(control: control, withGlobalValues: global_values))
|
P4Runtime.Runtime<P4TableHitMissValue, Control>.create(control: control, withGlobalValues: global_values))
|
||||||
|
|
||||||
let (hit_miss, updated_execution) = try #UseOkResult(runtime.run(
|
let (hit_miss, updated_execution) = try #UseOkResult(runtime.run(
|
||||||
withArguments: ArgumentList([
|
withArguments: ArgumentList([
|
||||||
Argument(TypedIdentifier(name: "result_arg", withType: P4Type(P4Int())), atIndex: 0),
|
Argument(TypedIdentifier(name: "result_arg", withType: P4QualifiedType(P4Int())), atIndex: 0),
|
||||||
Argument(P4Value(P4BooleanValue(withValue: false)), atIndex: 1),
|
Argument(P4Value(P4BooleanValue(withValue: false)), atIndex: 1),
|
||||||
Argument(P4Value(P4IntValue(withValue: 5)), atIndex: 2),
|
Argument(P4Value(P4IntValue(withValue: 5)), atIndex: 2),
|
||||||
])))
|
])))
|
||||||
|
|||||||
@@ -182,5 +182,5 @@ import TreeSitterP4
|
|||||||
|
|
||||||
let error = try #UseErrorResult(Program.Compile(simple_parser_declaration))
|
let error = try #UseErrorResult(Program.Compile(simple_parser_declaration))
|
||||||
|
|
||||||
#expect(error.msg.contains("{29, 12}: Type of expression in return statement (Boolean) is not compatible with function return type (Int)"))
|
#expect(error.msg().contains("{29, 12}: Type of expression in return statement (Boolean) is not compatible with function return type (Int)"))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,10 +38,10 @@ public struct Return5: P4FFI {
|
|||||||
return ParameterList()
|
return ParameterList()
|
||||||
}
|
}
|
||||||
|
|
||||||
public func type() -> Common.P4Type {
|
public func type() -> Common.P4QualifiedType {
|
||||||
return P4Type(
|
return P4QualifiedType(
|
||||||
FunctionDeclaration(
|
FunctionDeclaration(
|
||||||
named: Identifier(name: "externally"), ofType: P4Type(P4Int()),
|
named: Identifier(name: "externally"), ofType: P4QualifiedType(P4Int()),
|
||||||
withParameters: ParameterList(), withBody: .none?))
|
withParameters: ParameterList(), withBody: .none?))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,10 +59,10 @@ public struct Return6: P4FFI {
|
|||||||
return ParameterList()
|
return ParameterList()
|
||||||
}
|
}
|
||||||
|
|
||||||
public func type() -> Common.P4Type {
|
public func type() -> Common.P4QualifiedType {
|
||||||
return P4Type(
|
return P4QualifiedType(
|
||||||
FunctionDeclaration(
|
FunctionDeclaration(
|
||||||
named: Identifier(name: "externally"), ofType: P4Type(P4Int()),
|
named: Identifier(name: "externally"), ofType: P4QualifiedType(P4Int()),
|
||||||
withParameters: ParameterList(), withBody: .none?))
|
withParameters: ParameterList(), withBody: .none?))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -75,8 +75,8 @@ import P4Lang
|
|||||||
|
|
||||||
let compilation_error = try #UseErrorResult(Program.Compile(simple_parser_declaration))
|
let compilation_error = try #UseErrorResult(Program.Compile(simple_parser_declaration))
|
||||||
|
|
||||||
#expect(compilation_error.msg.contains("asde"))
|
#expect(compilation_error.msg().contains("asde"))
|
||||||
#expect(compilation_error.msg.contains("asdf"))
|
#expect(compilation_error.msg().contains("asdf"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test func test_simple_compiler_macro_nodetype_test() async throws {
|
@Test func test_simple_compiler_macro_nodetype_test() async throws {
|
||||||
@@ -96,7 +96,7 @@ import P4Lang
|
|||||||
|
|
||||||
#expect(
|
#expect(
|
||||||
#RequireErrorResult<(EvaluatableStatement, CompilerContext)>(
|
#RequireErrorResult<(EvaluatableStatement, CompilerContext)>(
|
||||||
Error(withMessage: "{2, 154}: Did not find assignment statement"),
|
ErrorWithLocation(sourceLocation: SourceLocation(2, 154), withError: "Did not find assignment statement"),
|
||||||
ParserAssignmentStatement.Compile( // Note: Calling ParserAssignmentStatement compilation directly.
|
ParserAssignmentStatement.Compile( // Note: Calling ParserAssignmentStatement compilation directly.
|
||||||
node: result.rootNode!, withContext: CompilerContext())))
|
node: result.rootNode!, withContext: CompilerContext())))
|
||||||
}
|
}
|
||||||
@@ -118,7 +118,7 @@ import P4Lang
|
|||||||
#expect(parameters.parameters.count == 1)
|
#expect(parameters.parameters.count == 1)
|
||||||
|
|
||||||
#expect(parameters.parameters[0].name == Identifier(name: "pmtr"))
|
#expect(parameters.parameters[0].name == Identifier(name: "pmtr"))
|
||||||
#expect(parameters.parameters[0].type.dataType().eq(rhs: P4Boolean()))
|
#expect(parameters.parameters[0].type.baseType().eq(rhs: P4Boolean()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test func test_simple_compiler_parser_with_multiple_parameters() async throws {
|
@Test func test_simple_compiler_parser_with_multiple_parameters() async throws {
|
||||||
@@ -138,10 +138,10 @@ import P4Lang
|
|||||||
#expect(parameters.parameters.count == 2)
|
#expect(parameters.parameters.count == 2)
|
||||||
|
|
||||||
#expect(parameters.parameters[0].name == Identifier(name: "pmtr"))
|
#expect(parameters.parameters[0].name == Identifier(name: "pmtr"))
|
||||||
#expect(parameters.parameters[0].type.dataType().eq(rhs: P4Boolean()))
|
#expect(parameters.parameters[0].type.baseType().eq(rhs: P4Boolean()))
|
||||||
|
|
||||||
#expect(parameters.parameters[1].name == Identifier(name: "smtr"))
|
#expect(parameters.parameters[1].name == Identifier(name: "smtr"))
|
||||||
#expect(parameters.parameters[1].type.dataType().eq(rhs: P4String()))
|
#expect(parameters.parameters[1].type.baseType().eq(rhs: P4String()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test func test_simple_compiler_parser_with_multiple_parameters2() async throws {
|
@Test func test_simple_compiler_parser_with_multiple_parameters2() async throws {
|
||||||
@@ -161,13 +161,13 @@ import P4Lang
|
|||||||
#expect(parameters.parameters.count == 3)
|
#expect(parameters.parameters.count == 3)
|
||||||
|
|
||||||
#expect(parameters.parameters[0].name == Identifier(name: "pmtr"))
|
#expect(parameters.parameters[0].name == Identifier(name: "pmtr"))
|
||||||
#expect(parameters.parameters[0].type.dataType().eq(rhs: P4Boolean()))
|
#expect(parameters.parameters[0].type.baseType().eq(rhs: P4Boolean()))
|
||||||
|
|
||||||
#expect(parameters.parameters[1].name == Identifier(name: "smtr"))
|
#expect(parameters.parameters[1].name == Identifier(name: "smtr"))
|
||||||
#expect(parameters.parameters[1].type.dataType().eq(rhs: P4String()))
|
#expect(parameters.parameters[1].type.baseType().eq(rhs: P4String()))
|
||||||
|
|
||||||
#expect(parameters.parameters[2].name == Identifier(name: "imtr"))
|
#expect(parameters.parameters[2].name == Identifier(name: "imtr"))
|
||||||
#expect(parameters.parameters[2].type.dataType().eq(rhs: P4Int()))
|
#expect(parameters.parameters[2].type.baseType().eq(rhs: P4Int()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test func test_simple_compiler_parser_use_parameters() async throws {
|
@Test func test_simple_compiler_parser_use_parameters() async throws {
|
||||||
|
|||||||
@@ -28,39 +28,39 @@ import TreeSitterP4
|
|||||||
|
|
||||||
@Test func test_scope() async throws {
|
@Test func test_scope() async throws {
|
||||||
let s = VarTypeScope()
|
let s = VarTypeScope()
|
||||||
let s2 = s.declare(identifier: Identifier(name: "first"), withValue: P4Type(P4Int()))
|
let s2 = s.declare(identifier: Identifier(name: "first"), withValue: P4QualifiedType(P4Int()))
|
||||||
let found_first = try! #require(s2.lookup(identifier: Identifier(name: "first")))
|
let found_first = try! #require(s2.lookup(identifier: Identifier(name: "first")))
|
||||||
|
|
||||||
#expect(found_first.dataType().eq(rhs: P4Int()))
|
#expect(found_first.baseType().eq(rhs: P4Int()))
|
||||||
#expect(s2.count == 1)
|
#expect(s2.count == 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test func test_scope_no_set() async throws {
|
@Test func test_scope_no_set() async throws {
|
||||||
var ss = VarTypeScopes().enter()
|
var ss = VarTypeScopes().enter()
|
||||||
ss = ss.declare(identifier: Identifier(name: "first"), withValue: P4Type(P4Int()))
|
ss = ss.declare(identifier: Identifier(name: "first"), withValue: P4QualifiedType(P4Int()))
|
||||||
ss = ss.enter()
|
ss = ss.enter()
|
||||||
ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4Type(P4Boolean()))
|
ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4QualifiedType(P4Boolean()))
|
||||||
|
|
||||||
let found_first = try! #UseOkResult(ss.lookup(identifier: Identifier(name: "first")))
|
let found_first = try! #UseOkResult(ss.lookup(identifier: Identifier(name: "first")))
|
||||||
let found_second = try! #UseOkResult(ss.lookup(identifier: Identifier(name: "second")))
|
let found_second = try! #UseOkResult(ss.lookup(identifier: Identifier(name: "second")))
|
||||||
|
|
||||||
#expect(found_first.dataType().eq(rhs: P4Int()))
|
#expect(found_first.baseType().eq(rhs: P4Int()))
|
||||||
#expect(found_second.dataType().eq(rhs: P4Boolean()))
|
#expect(found_second.baseType().eq(rhs: P4Boolean()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test func test_scope_set() async throws {
|
@Test func test_scope_set() async throws {
|
||||||
var ss = VarTypeScopes().enter()
|
var ss = VarTypeScopes().enter()
|
||||||
let id = Identifier(name: "first")
|
let id = Identifier(name: "first")
|
||||||
let id_type = P4Type(P4Int())
|
let id_type = P4QualifiedType(P4Int())
|
||||||
|
|
||||||
ss = ss.declare(identifier: id, withValue: id_type)
|
ss = ss.declare(identifier: id, withValue: id_type)
|
||||||
ss = ss.enter()
|
ss = ss.enter()
|
||||||
ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4Type(P4Boolean()))
|
ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4QualifiedType(P4Boolean()))
|
||||||
// Change the value of `first`.
|
// Change the value of `first`.
|
||||||
ss = ss.set(identifier: id, withValue: P4Type(P4String()))
|
ss = ss.set(identifier: id, withValue: P4QualifiedType(P4String()))
|
||||||
|
|
||||||
// Verify the change!
|
// Verify the change!
|
||||||
let found = try! #UseOkResult(ss.lookup(identifier: id))
|
let found = try! #UseOkResult(ss.lookup(identifier: id))
|
||||||
|
|
||||||
#expect(found.dataType().eq(rhs: P4String()))
|
#expect(found.baseType().eq(rhs: P4String()))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,11 +41,11 @@ import TreeSitterP4
|
|||||||
"""
|
"""
|
||||||
var test_declarations = VarTypeScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
let fields = P4StructFields([
|
let fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
|
||||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
|
||||||
])
|
])
|
||||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(struct_type))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type))
|
||||||
|
|
||||||
var test_values = VarValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
@@ -78,8 +78,8 @@ import TreeSitterP4
|
|||||||
"""
|
"""
|
||||||
var test_types = TypeTypeScopes().enter()
|
var test_types = TypeTypeScopes().enter()
|
||||||
let fields = P4StructFields([
|
let fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
|
||||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(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: "Testing"), withValue: struct_type)
|
test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
|
||||||
@@ -108,8 +108,8 @@ import TreeSitterP4
|
|||||||
"""
|
"""
|
||||||
var test_types = TypeTypeScopes().enter()
|
var test_types = TypeTypeScopes().enter()
|
||||||
let fields = P4StructFields([
|
let fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
|
||||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(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: "Testing"), withValue: struct_type)
|
test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
|
||||||
@@ -135,11 +135,11 @@ import TreeSitterP4
|
|||||||
"""
|
"""
|
||||||
var test_declarations = VarTypeScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
let fields = P4StructFields([
|
let fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
|
||||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
|
||||||
])
|
])
|
||||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(struct_type))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type))
|
||||||
|
|
||||||
var test_values = VarValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
@@ -170,11 +170,11 @@ import TreeSitterP4
|
|||||||
"""
|
"""
|
||||||
var test_declarations = VarTypeScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
let fields = P4StructFields([
|
let fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
|
||||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
|
||||||
])
|
])
|
||||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(struct_type))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type))
|
||||||
|
|
||||||
var test_values = VarValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
@@ -204,11 +204,11 @@ import TreeSitterP4
|
|||||||
"""
|
"""
|
||||||
var test_declarations = VarTypeScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
let fields = P4StructFields([
|
let fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
|
||||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
|
||||||
])
|
])
|
||||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(struct_type))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type))
|
||||||
|
|
||||||
var test_values = VarValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
@@ -240,15 +240,15 @@ import TreeSitterP4
|
|||||||
var test_declarations = VarTypeScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
|
|
||||||
let ty_fields = P4StructFields([
|
let ty_fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
|
||||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
|
||||||
])
|
])
|
||||||
let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields)
|
let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields)
|
||||||
|
|
||||||
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4Type(ty_struct_type))])
|
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4QualifiedType(ty_struct_type))])
|
||||||
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
|
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
|
||||||
|
|
||||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(ts_struct_type))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(ts_struct_type))
|
||||||
|
|
||||||
var test_values = VarValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
|
|
||||||
@@ -286,11 +286,11 @@ import TreeSitterP4
|
|||||||
"""
|
"""
|
||||||
var test_declarations = VarTypeScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
let fields = P4StructFields([
|
let fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
|
||||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
|
||||||
])
|
])
|
||||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(struct_type))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type))
|
||||||
|
|
||||||
var test_values = VarValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
test_values = test_values.declare(
|
test_values = test_values.declare(
|
||||||
@@ -318,11 +318,11 @@ import TreeSitterP4
|
|||||||
"""
|
"""
|
||||||
var test_declarations = VarTypeScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
let fields = P4StructFields([
|
let fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
|
||||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
|
||||||
])
|
])
|
||||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(struct_type))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type))
|
||||||
|
|
||||||
#expect(
|
#expect(
|
||||||
#RequireErrorResult(
|
#RequireErrorResult(
|
||||||
@@ -349,15 +349,15 @@ import TreeSitterP4
|
|||||||
var test_declarations = VarTypeScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
|
|
||||||
let ty_fields = P4StructFields([
|
let ty_fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
|
||||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
|
||||||
])
|
])
|
||||||
let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields)
|
let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields)
|
||||||
|
|
||||||
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4Type(ty_struct_type))])
|
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4QualifiedType(ty_struct_type))])
|
||||||
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
|
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
|
||||||
|
|
||||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(ts_struct_type))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(ts_struct_type))
|
||||||
|
|
||||||
var test_values = VarValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
|
|
||||||
@@ -397,15 +397,15 @@ import TreeSitterP4
|
|||||||
var test_declarations = VarTypeScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
|
|
||||||
let ty_fields = P4StructFields([
|
let ty_fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
|
||||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
|
||||||
])
|
])
|
||||||
let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields)
|
let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields)
|
||||||
|
|
||||||
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4Type(ty_struct_type))])
|
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4QualifiedType(ty_struct_type))])
|
||||||
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
|
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
|
||||||
|
|
||||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(ts_struct_type))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(ts_struct_type))
|
||||||
|
|
||||||
var test_values = VarValueScopes().enter()
|
var test_values = VarValueScopes().enter()
|
||||||
|
|
||||||
@@ -444,15 +444,15 @@ import TreeSitterP4
|
|||||||
var test_declarations = VarTypeScopes().enter()
|
var test_declarations = VarTypeScopes().enter()
|
||||||
|
|
||||||
let ty_fields = P4StructFields([
|
let ty_fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
|
||||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
|
||||||
])
|
])
|
||||||
let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields)
|
let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields)
|
||||||
|
|
||||||
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4Type(ty_struct_type))])
|
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4QualifiedType(ty_struct_type))])
|
||||||
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
|
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
|
||||||
|
|
||||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(ts_struct_type))
|
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(ts_struct_type))
|
||||||
|
|
||||||
#expect(
|
#expect(
|
||||||
#RequireErrorResult(
|
#RequireErrorResult(
|
||||||
|
|||||||
@@ -26,8 +26,8 @@ import TreeSitterP4
|
|||||||
|
|
||||||
@Test func test_simple_struct() async throws {
|
@Test func test_simple_struct() async throws {
|
||||||
let fields = P4StructFields([
|
let fields = P4StructFields([
|
||||||
P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())),
|
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
|
||||||
P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())),
|
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
|
||||||
])
|
])
|
||||||
|
|
||||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ import TreeSitterP4
|
|||||||
guard case Result.Error(let e) = err else {
|
guard case Result.Error(let e) = err else {
|
||||||
assert(false, "Expected an error, but had success")
|
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)"))
|
#expect(e.format().contains("Failed to parse a statement element: Could not parse a P4 type from \(invalid_type_name)"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,7 +103,7 @@ import TreeSitterP4
|
|||||||
#RequireErrorResult(
|
#RequireErrorResult(
|
||||||
Error(
|
Error(
|
||||||
withMessage:
|
withMessage:
|
||||||
"{86, 27}: Failed to parse a statement element: Cannot initialize where_to (with type Boolean) from rvalue with type String"
|
"{86, 27}: Failed to parse a statement element: Cannot initialize where_to (with type Boolean) from expression with type String"
|
||||||
),
|
),
|
||||||
Program.Compile(simple_parser_declaration)))
|
Program.Compile(simple_parser_declaration)))
|
||||||
}
|
}
|
||||||
@@ -123,7 +123,7 @@ import TreeSitterP4
|
|||||||
#RequireErrorResult(
|
#RequireErrorResult(
|
||||||
Error(
|
Error(
|
||||||
withMessage:
|
withMessage:
|
||||||
"{77, 29}: Failed to parse a statement element: Cannot initialize where_from (with type String) from rvalue with type Boolean"
|
"{77, 29}: Failed to parse a statement element: Cannot initialize where_from (with type String) from expression with type Boolean"
|
||||||
),
|
),
|
||||||
Program.Compile(simple_parser_declaration)))
|
Program.Compile(simple_parser_declaration)))
|
||||||
}
|
}
|
||||||
@@ -253,12 +253,12 @@ import TreeSitterP4
|
|||||||
};
|
};
|
||||||
"""
|
"""
|
||||||
var test_types = VarTypeScopes().enter()
|
var test_types = VarTypeScopes().enter()
|
||||||
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Int()))))
|
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
|
||||||
#expect(
|
#expect(
|
||||||
#RequireErrorResult(
|
#RequireErrorResult(
|
||||||
Error(
|
Error(
|
||||||
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 expression with type Int"
|
||||||
),
|
),
|
||||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_types)))
|
Program.Compile(simple_parser_declaration, withGlobalInstances: test_types)))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user