Improve Parsing of P4 Parsers And Continue Runtime Implementation

1. Parsing of P4 Parsers was broken: Multiple local elements and
statements were not allowed.
2. Support more functionality on scopes.
3. Refactor variable/identifier.
4. Add more tests.

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-01-30 08:31:42 -05:00
parent 9b307bcd66
commit 1203c0c90a
8 changed files with 410 additions and 281 deletions
+4 -6
View File
@@ -65,15 +65,13 @@ public struct ParserTransitionStatement: ParserStatement {
}
public struct VariableDeclarationStatement: ParserStatement {
public var id: Identifier
public init(withIdentifier id: Identifier) {
self.id = id
public var variable: Variable
public init(withVariable variable: Variable) {
self.variable = variable
}
public func evaluate(execution: ParserExecution) -> ParserExecution {
print("Evaluating!")
execution.scopes.scopes[0].variables.append(Variable(name: id.name, withValue: id.value, isConstant: false))
print("Execution: \(execution)")
execution.scopes.scopes[0].variables.append(self.variable)
return execution
}
}
+47 -10
View File
@@ -15,30 +15,40 @@
// 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 {
public class Identifier: CustomStringConvertible, Equatable {
var name: String
var value: Value
public init(name: String, withValue value: Value) {
public init(name: String) {
self.name = name
self.value = value
}
public var description: String {
return "\(name) = \(value)"
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: Value, isConstant constant: Bool) {
public init(name: String, withValue value: ValueType, isConstant constant: Bool) {
self.constant = constant
super.init(name: name, withValue: value)
self.value = value
super.init(name: name)
}
public override var description: String {
return "\(super.description) \(constant ? "(constant)" : "")"
return "\(super.description) = \(value) \(constant ? "(constant)" : "")"
}
public var value_type: ValueType {
get {
value
}
}
}
@@ -49,10 +59,25 @@ public struct Scope: CustomStringConvertible{
public var description: String {
var result = String()
for v in variables {
result += "\(v)"
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 struct Scopes: CustomStringConvertible {
@@ -71,11 +96,23 @@ public struct Scopes: CustomStringConvertible {
public var description: String {
var result = String()
for s in scopes {
result += "Scope: \(s)\n"
result += "Scope:\n\(s)\n"
}
return result
}
public var current: Scope? {
get {
scopes.last
}
}
public var count: Int {
get {
scopes.count
}
}
}
public struct Program {
+2 -4
View File
@@ -41,14 +41,12 @@ public class ParserRuntime: CustomStringConvertible {
return Result.Ok(P4.ParserRuntime(execution: P4.ParserExecution(start_state)))
}
public func run(input: P4.Packet) -> Result<Nothing> {
public func run(input: P4.Packet) -> Result<ParserExecution> {
execution.scopes.enter()
print("Execution: \(execution)")
while execution.state != P4.accept && execution.state != P4.reject {
execution = execution.state.evaluate(execution: execution)
print("Execution: \(execution)")
}
return .Ok(Nothing())
return .Ok(execution)
}
public var description: String {
+20 -1
View File
@@ -15,15 +15,34 @@
// 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 enum ValueType: CustomStringConvertible {
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 {