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