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
+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 {