Files
gp4/Sources/P4Lang/Declarations.swift
T
Will Hawkins b49ec104e9 compiler, runtime: Make Default Values Optional
For types, make it optional to return a default value. While default
values for some types are meaningful, they are not meaningful for all
types (e.g., Action, Control, Parser, Parser state, etc.).

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
2026-05-04 08:35:33 -04:00

133 lines
3.5 KiB
Swift

// p4rse, Copyright 2026, Will Hawkins
//
// This file is part of p4rse.
//
// This file is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import Common
public struct Declaration: P4Type {
public let identifier: TypedIdentifier
public let extern: Bool
public let ffi: P4FFI?
public init(_ id: TypedIdentifier) {
identifier = id
ffi = .none
self.extern = false
}
public init(extern: Declaration, ffi: P4FFI) {
identifier = extern.identifier
self.ffi = ffi
self.extern = true
}
public func eq(rhs: any Common.P4Type) -> Bool {
return switch rhs {
case let rrhs as Declaration:
self.identifier.type.baseType().eq(rhs: rrhs.identifier.type.baseType())
&& self.extern == rrhs.extern
default: false
}
}
public func def() -> P4DataValue? {
/// TODO: Is a default of the extern'd type the right way to go?
return self.identifier.type.baseType().def()
}
public func type() -> any Common.P4Type {
return self
}
public var description: String {
return "Extern \(self.identifier)"
}
}
public struct ExternDeclaration {}
public struct FunctionDeclaration: P4Type, P4DataValue {
public func type() -> any Common.P4Type {
return self
}
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
default: return false
}
}
public func eq(rhs: any Common.P4DataValue) -> Bool {
switch rhs {
case let frhs as FunctionDeclaration: return self.eq(rhs: frhs as P4Type)
default: return false
}
}
public func lt(rhs: any Common.P4DataValue) -> Bool {
switch rhs {
case let frhs as FunctionDeclaration: return self.name < frhs.name
default: return false
}
}
public func lte(rhs: any Common.P4DataValue) -> Bool {
switch rhs {
case let frhs as FunctionDeclaration: return self.name <= frhs.name
default: return false
}
}
public func gt(rhs: any Common.P4DataValue) -> Bool {
switch rhs {
case let frhs as FunctionDeclaration: return self.name > frhs.name
default: return false
}
}
public func gte(rhs: any Common.P4DataValue) -> Bool {
switch rhs {
case let frhs as FunctionDeclaration: return self.name >= frhs.name
default: return false
}
}
public func def() -> P4DataValue? {
return .none
}
public var description: String {
return "Function named \(self.name) that returns \(self.tipe) with parameters \(self.params)"
}
public var body: BlockStatement?
public var params: ParameterList
public var name: Identifier
public var tipe: P4QualifiedType
public init(
named name: Identifier, ofType type: P4QualifiedType, withParameters parameters: ParameterList,
withBody body: BlockStatement?
) {
self.name = name
self.tipe = type
self.params = parameters
self.body = body
}
}