Refactor into four major components:

1. Common: Things shared among all other components.
2. Lang: P4-language-related components.
3. Runtime: Components required to run a P4 program.
4. Parser: Components for parsing a P4 program from source.

Other components:

1. Macros
2. Tests
3. TreeSitterExtensions: Extra tree sitter functionality

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-02-03 08:22:58 -05:00
parent 1203c0c90a
commit 989c9b1212
20 changed files with 513 additions and 320 deletions
+104
View File
@@ -0,0 +1,104 @@
// 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/>.
open class ProgramExecution: CustomStringConvertible {
public var scopes: Scopes = Scopes()
public init() {}
open var description: String {
return "Runtime:\nScopes: \(scopes)"
}
}
public struct Scope: CustomStringConvertible{
var variables: [Variable] = Array()
public init() {}
public var description: String {
var result = String()
for v in variables {
result += "\(v)\n"
}
return result
}
public var count: Int {
get {
variables.count
}
}
public func lookup(identifier: Identifier) -> Variable? {
for v in variables {
if v == identifier {
return v
}
}
return .none
}
public mutating func declare(variable: Variable) -> Scope {
var s = self
s.variables.append(variable)
return s
}
}
public struct Scopes: CustomStringConvertible {
var scopes: [Scope] = Array()
public init() {}
public mutating func enter() {
scopes.append(Scope())
}
public mutating func exit() {
let _ = scopes.popLast()
}
public var description: String {
var result = String()
for s in scopes {
result += "Scope:\n\(s)\n"
}
return result
}
public var current: Scope? {
get {
scopes.last
}
}
public func declare(variable: Variable) -> Scopes {
var s = self
if var scope = s.scopes.popLast() {
s.scopes.append(scope.declare(variable: variable))
}
return s
}
public var count: Int {
get {
scopes.count
}
}
}
+99
View File
@@ -0,0 +1,99 @@
// 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 class Identifier: CustomStringConvertible, Equatable {
var name: String
public init(name: String) {
self.name = name
}
public var description: String {
return "\(name)"
}
public static func ==(lhs: Identifier, rhs: Identifier) -> Bool {
return lhs.name == rhs.name
}
}
public class Variable: Identifier {
var constant: Bool
var value: ValueType
public init(name: String, withValue value: ValueType, isConstant constant: Bool) {
self.constant = constant
self.value = value
super.init(name: name)
}
public override var description: String {
return "\(super.description) = \(value) \(constant ? "(constant)" : "")"
}
public var value_type: ValueType {
get {
value
}
}
}
public enum ValueType: CustomStringConvertible, Equatable {
case Boolean(Bool)
case Int(Int)
case String(String)
public var description: String {
switch self {
case ValueType.Boolean(let b):
return "\(b) of Boolean"
case ValueType.Int(let i):
return "\(i) of Int"
case ValueType.String(let s):
return "\(s) of String"
}
}
public static func==(lhs: ValueType, rhs: ValueType) -> Bool {
switch (lhs,rhs) {
case (ValueType.Boolean(let lhsb), ValueType.Boolean(let rhsb)):
return lhsb == rhsb
case (ValueType.String(let lhsb), ValueType.String(let rhsb)):
return lhsb == rhsb
case (ValueType.Int(let lhsb), ValueType.Int(let rhsb)):
return lhsb == rhsb
default: return false
}
}
}
public struct Value: CustomStringConvertible {
public var value_type: ValueType
public init(withValue value: ValueType) {
self.value_type = value
}
public var description: String {
return "\(value_type)"
}
}
public class Packet {
public init() {}
}
+33
View File
@@ -0,0 +1,33 @@
// 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 protocol EvaluatableExpression {
/// Evaluate an expression for a given execution
/// - Parameters
/// - execution: The execution context in which to evaluate the expression
/// - Returns: The value of expression
func evaluate(execution: ProgramExecution) -> ValueType
}
public protocol EvaluatableParserStatement {
/// Evaluate a statement for a given execution
/// - Parameters
/// - execution: The execution context in which to evaluate the parser statement
/// - Returns: An updated execution after evaluating the parser statement
func evaluate(execution: ProgramExecution) -> ProgramExecution
}
+85
View File
@@ -0,0 +1,85 @@
// 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: Equatable {
public private(set) var msg: String
public init(withMessage msg: String) {
self.msg = msg
}
}
public struct Nothing: CustomStringConvertible {
public var description: String {
return "Nothing"
}
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 error() -> Error? {
if case Result.Error(let e) = self {
return e
}
return nil
}
}
extension Result where OKT: CustomStringConvertible {
public var description: String {
switch self {
case Result.Error(let e):
return e.msg
case Result.Ok(let o):
return "\(o)"
}
}
}
extension Result {
public var description: String {
switch self {
case Result.Error(let e):
return e.msg
case Result.Ok(_):
return "Ok"
}
}
}
@freestanding(expression) public macro RequireOkResult<T>(_: Result<T>) -> Bool =
#externalMacro(module: "Macros", type: "RequireResult")
@freestanding(expression) public macro RequireErrorResult<T>(_: Error, _: Result<T>) -> Bool =
#externalMacro(module: "Macros", type: "RequireErrorResult")
@freestanding(expression) public macro UseOkResult<T>(_: Result<T>) -> T =
#externalMacro(module: "Macros", type: "UseOkResult")