Support Setting Arrays/Fields

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-03-20 02:32:15 -04:00
parent d7696e822c
commit 04627f20e2
9 changed files with 326 additions and 74 deletions
+19
View File
@@ -0,0 +1,19 @@
// 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 typealias TypeScope = Scope<P4Type>
public typealias TypeScopes = Scopes<P4Type>
+41 -5
View File
@@ -117,6 +117,18 @@ public struct P4StructFields: Sequence, CustomStringConvertible, Equatable {
public func count() -> Int {
return self.fields.count
}
public func describe_with_values(values: [P4Value?]) -> String {
assert(values.count == self.count())
return zip(self.fields, values).map() { (field, value) in
let actual_value = if let v = value {
v.description
} else {
"Unset"
}
return String("\(field): \(actual_value)")
}.joined(separator: "; ")
}
}
/// The type for a P4 struct
@@ -159,7 +171,7 @@ public class P4StructValue: P4Value {
}
public var description: String {
return "Struct"
return "Struct: \(self.stype.fields.describe_with_values(values: self.values))"
}
public let stype: P4Struct
@@ -169,7 +181,7 @@ public class P4StructValue: P4Value {
self.init(withType: type, andInitializers: [])
}
public init(withType type: P4Struct, andInitializers initializers: [P4Value]) {
public init(withType type: P4Struct, andInitializers initializers: [P4Value?]) {
var values: [P4Value?] = Array(repeating: .none, count: type.fields.count())
for i in 0..<initializers.count {
@@ -188,6 +200,23 @@ public class P4StructValue: P4Value {
}
return .none
}
public func set(field: P4StructFieldIdentifier, to: P4Value) -> Result<P4StructValue> {
var updated_values = self.values
for field_idx in 0..<stype.fields.count() {
if stype.fields.fields[field_idx] == field {
if !stype.fields.fields[field_idx].type.eq(rhs: to.type()) {
return .Error(Error(withMessage: "Cannot assign value with type \(to.type()) to field with type \(stype.fields.fields[field_idx].type))"))
}
updated_values[field_idx] = to
break
}
}
return .Ok(P4StructValue(withType: self.stype, andInitializers: updated_values))
}
}
/// A P4 boolean type
@@ -337,18 +366,25 @@ public class P4ArrayValue: P4Value {
return P4Array(withValueType: self.vtype)
}
let value: [EvaluatableExpression]
let value: [P4Value]
let vtype: P4Type
public init(withType type: P4Type, withValue value: [EvaluatableExpression]) {
public init(withType type: P4Type, withValue value: [P4Value]) {
self.vtype = type
self.value = value
}
public func access(_ index: Int) -> EvaluatableExpression {
public func access(_ index: Int) -> P4Value {
return self.value[index]
}
public func set(index: Int, to: P4Value) -> Result<P4ArrayValue> {
// TODO: Check for OOB
var updated_values = self.value
updated_values[index] = to
return Result.Ok(P4ArrayValue(withType: self.vtype, withValue: updated_values))
}
public func eq(rhs: P4Value) -> Bool {
guard rhs as? P4ArrayValue != nil else {
return false
+5
View File
@@ -40,3 +40,8 @@ public protocol P4Value: CustomStringConvertible {
func type() -> any P4Type
func eq(rhs: P4Value) -> Bool
}
public protocol EvaluatableLValueExpression: EvaluatableExpression {
func set(to: P4Value, inScopes scopes: ValueScopes, duringExecution execution: ProgramExecution) -> Result<(ValueScopes, P4Value)>
func check(to: EvaluatableExpression, inScopes scopes: TypeScopes) -> Result<()>
}
+82 -5
View File
@@ -27,6 +27,12 @@ protocol CompilableExpression {
) -> Result<EvaluatableExpression?>
}
protocol CompilableLValueExpression {
static func compile_as_lvalue(
node: Node, withContext context: CompilerContext
) -> Result<EvaluatableLValueExpression?>
}
extension TypedIdentifier: CompilableExpression {
static func compile(
node: SwiftTreeSitter.Node, withContext context: CompilerContext
@@ -47,6 +53,26 @@ extension TypedIdentifier: CompilableExpression {
}
}
extension TypedIdentifier: CompilableLValueExpression {
static func compile_as_lvalue(
node: SwiftTreeSitter.Node, withContext context: CompilerContext
) -> Result<EvaluatableLValueExpression?> {
let expression = node.child(at: 0)!
#SkipUnlessNodeType<SwiftTreeSitter.Node, EvaluatableExpression?>(
node: expression, type: "identifier")
let maybe_parsed_expression = TypedIdentifier.compile(node: node, withContext: context)
guard case .Ok(let maybe_typed_identifier) = maybe_parsed_expression else {
return .Error(maybe_parsed_expression.error()!)
}
let typed_identifier_expression = maybe_typed_identifier as! TypedIdentifier
return Result.Ok(typed_identifier_expression)
}
}
extension P4BooleanValue: CompilableExpression {
static func compile(
node: SwiftTreeSitter.Node, withContext context: CompilerContext
@@ -130,13 +156,26 @@ struct Expression {
struct LValue {
public static func Compile(
node: Node, withContext: CompilerContext
) -> Result<Common.Identifier> {
return if let node_text_value = node.text {
.Ok(Common.Identifier(name: node_text_value))
} else {
.Error(Error(withMessage: "Could not parse an identifier for an LValue"))
) -> Result<EvaluatableLValueExpression> {
// Try to compile all the expressions that are LValuable!
let lvalueParsers: [CompilableLValueExpression.Type] = [
TypedIdentifier.self, FieldAccessExpression.self, ArrayAccessExpression.self,
]
for lvalue_parser in lvalueParsers {
switch lvalue_parser.compile_as_lvalue(
node: node, withContext: withContext)
{
case .Ok(.some(let parsed)): return .Ok(parsed)
case .Error(let e): return .Error(e)
default: continue
}
}
return Result.Error(Error(withMessage: "\(node.range): Could not parse into expression"))
}
}
struct Identifier {
@@ -477,3 +516,41 @@ extension FieldAccessExpression: CompilableExpression {
withField: P4StructFieldIdentifier(id: field_name, withType: field_type)))
}
}
extension FieldAccessExpression: CompilableLValueExpression {
static func compile_as_lvalue(
node: SwiftTreeSitter.Node, withContext context: CompilerContext
) -> Result<EvaluatableLValueExpression?> {
let expression = node.child(at: 0)!
#SkipUnlessNodeType<Node, EvaluatableExpression?>(
node: expression, type: "fieldAccessExpression")
let maybe_parsed_expression = FieldAccessExpression.compile(node: node, withContext: context)
guard case .Ok(let maybe_field_access_expression) = maybe_parsed_expression else {
return .Error(maybe_parsed_expression.error()!)
}
let field_access_expression = maybe_field_access_expression as! FieldAccessExpression
return Result.Ok(field_access_expression)
}
}
extension ArrayAccessExpression: CompilableLValueExpression {
static func compile_as_lvalue(
node: SwiftTreeSitter.Node, withContext context: CompilerContext
) -> Result<EvaluatableLValueExpression?> {
let expression = node.child(at: 0)!
#SkipUnlessNodeType<Node, EvaluatableExpression?>(
node: expression, type: "arrayAccessExpression")
let maybe_parsed_expression = ArrayAccessExpression.compile(node: node, withContext: context)
guard case .Ok(let maybe_array_access_expression) = maybe_parsed_expression else {
return .Error(maybe_parsed_expression.error()!)
}
let array_access_expression = maybe_array_access_expression as! ArrayAccessExpression
return Result.Ok(array_access_expression)
}
}
-59
View File
@@ -22,65 +22,6 @@ import SwiftTreeSitter
import TreeSitterExtensions
import TreeSitterP4
extension ParserAssignmentStatement: CompilableStatement {
public static func Compile(
node: Node, withContext context: CompilerContext
) -> Result<(EvaluatableStatement, CompilerContext)> {
#RequireNodeType<Node, (EvaluatableStatement, CompilerContext)>(
node: node, type: "assignmentStatement", nice_type_name: "assignment statement")
guard let lvalue_node = node.child(at: 0),
lvalue_node.nodeType == "expression"
else {
return Result.Error(
ErrorOnNode(node: node, 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"))
}
let maybe_parsed_rvalue = Expression.Compile(
node: rvalue_node, withContext: context)
guard case Result.Ok(let rvalue) = maybe_parsed_rvalue else {
return Result.Error(maybe_parsed_rvalue.error()!)
}
let maybe_parsed_lvalue = LValue.Compile(node: lvalue_node, withContext: context)
guard case .Ok(let lvalue_identifier) = maybe_parsed_lvalue else {
return Result.Error(maybe_parsed_lvalue.error()!)
}
guard case Result.Ok(let lvalue_type) = context.names.lookup(identifier: lvalue_identifier)
else {
return Result.Error(
ErrorOnNode(
node: lvalue_node,
withError: "Cannot assign to variable \(lvalue_identifier) not in scope"))
}
if rvalue.type().eq(rhs: lvalue_type) {
return Result.Ok(
(
ParserAssignmentStatement(
withLValue: TypedIdentifier(name: lvalue_node.text!, withType: lvalue_type),
withValue: rvalue
), context
))
} else {
return Result.Error(
ErrorOnNode(
node: node,
withError:
"Cannot assign value of type \(rvalue.type()) to \(lvalue_identifier) (with type \(lvalue_type))"
))
}
}
}
public struct Parser {
public struct LocalElements {
+51
View File
@@ -246,3 +246,54 @@ extension ExpressionStatement: CompilableStatement {
return Result.Ok((ExpressionStatement(), context))
}
}
extension ParserAssignmentStatement: CompilableStatement {
public static func Compile(
node: Node, withContext context: CompilerContext
) -> Result<(EvaluatableStatement, CompilerContext)> {
#RequireNodeType<Node, (EvaluatableStatement, CompilerContext)>(
node: node, type: "assignmentStatement", nice_type_name: "assignment statement")
guard let lvalue_node = node.child(at: 0),
lvalue_node.nodeType == "expression"
else {
return Result.Error(
ErrorOnNode(node: node, 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"))
}
let maybe_parsed_rvalue = Expression.Compile(
node: rvalue_node, withContext: context)
guard case Result.Ok(let rvalue) = maybe_parsed_rvalue else {
return Result.Error(maybe_parsed_rvalue.error()!)
}
let maybe_parsed_lvalue = LValue.Compile(node: lvalue_node, withContext: context)
guard case .Ok(let lvalue_identifier) = maybe_parsed_lvalue else {
return Result.Error(maybe_parsed_lvalue.error()!)
}
let check_result = lvalue_identifier.check(to: rvalue, inScopes: context.names)
guard case .Ok(_) = check_result else {
return Result.Error(
ErrorOnNode(
node: lvalue_node,
withError: "\(check_result.error()!)"))
}
return Result.Ok(
(
ParserAssignmentStatement(
withLValue: lvalue_identifier,
withValue: rvalue
), context
))
}
}
+2 -2
View File
@@ -26,10 +26,10 @@ public struct LocalElement {
}
public struct ParserAssignmentStatement {
public let lvalue: TypedIdentifier
public let lvalue: EvaluatableLValueExpression
public let value: EvaluatableExpression
public init(withLValue lvalue: TypedIdentifier, withValue value: EvaluatableExpression) {
public init(withLValue lvalue: EvaluatableLValueExpression, withValue value: EvaluatableExpression) {
self.lvalue = lvalue
self.value = value
}
+120 -1
View File
@@ -93,6 +93,30 @@ extension TypedIdentifier: EvaluatableExpression {
}
}
// Variables are evaluatable because they can be looked up by identifiers.
extension TypedIdentifier: EvaluatableLValueExpression {
public func set(
to: any Common.P4Value, inScopes scopes: Common.ValueScopes, duringExecution execution: ProgramExecution
) -> Common.Result<(Common.ValueScopes, P4Value)> {
if case .Error(let e) = scopes.lookup(identifier: self) {
return .Error(e)
}
return .Ok((scopes.set(identifier: self, withValue: to), to))
}
public func check(to: any Common.EvaluatableExpression, inScopes scopes: Common.TypeScopes) -> Result<()> {
guard case .Ok(let type) = scopes.lookup(identifier: self) else {
return .Error(Error(withMessage: "Cannot assign to identifier not in scope"))
}
if !type.eq(rhs: to.type()) {
return .Error(Error(withMessage: "Cannot assign value with type \(to.type()) to identifier \(self) with type \(type)"))
}
return .Ok(())
}
}
public func binary_equal_operator_evaluator(left: P4Value, right: P4Value) -> P4Value {
if left.eq(rhs: right) {
return P4BooleanValue(withValue: true)
@@ -141,7 +165,7 @@ extension ArrayAccessExpression: EvaluatableExpression {
}
let accessed = array.access(indexor_int.access())
return accessed.evaluate(execution: execution)
return .Ok(accessed)
}
public func type() -> any Common.P4Type {
@@ -149,6 +173,56 @@ extension ArrayAccessExpression: EvaluatableExpression {
}
}
extension ArrayAccessExpression: EvaluatableLValueExpression {
public func set(
to: any Common.P4Value, inScopes scopes: Common.ValueScopes, duringExecution execution: ProgramExecution
) -> Common.Result<(Common.ValueScopes, P4Value)> {
// For purposes of documentation, assume the field access expression we are evaluating is
// (strct_id)[indexor] = new_value
// where strct_id expands to
// (identifier.field_id1.field_id2...).field_id = new_field_value
// First, evaluate strct_id and make sure that it names a struct.
let maybe_value = self.name.evaluate(execution: execution)
guard case .Ok(let value) = maybe_value else {
return Result.Error(Error(withMessage: "\(self.name) cannot be evaluated: \(maybe_value.error()!)"))
}
guard let array_value = value as? P4ArrayValue else {
return Result.Error(Error(withMessage: "\(self.name) does not identify a struct"))
}
// Now, get the indexor!
let maybe_indexor_value = self.indexor.evaluate(execution: execution)
guard case .Ok(let indexor_value) = maybe_indexor_value else {
return Result.Error(Error(withMessage: "\(self.indexor) cannot be evaluated: \(maybe_indexor_value.error()!)"))
}
guard let indexor_int = indexor_value as? P4IntValue else {
return Result.Error(Error(withMessage: "\(self.indexor) cannot be used to index an array"))
}
// Now we have an array and an index!
// Update field_id of that structure and get the new structure value.
let set_result = array_value.set(index: indexor_int.access(), to: to)
guard case .Ok(let new_array_value) = set_result else {
return .Error(set_result.error()!)
}
let array_lvalue = self.name as! EvaluatableLValueExpression
return array_lvalue.set(to: new_array_value, inScopes: scopes, duringExecution: execution)
}
public func check(
to: any Common.EvaluatableExpression, inScopes scopes: Common.TypeScopes
) -> Common.Result<()> {
if !self.type.value_type().eq(rhs: to.type()) {
return .Error(Error(withMessage: "Cannot assign value of type \(to.type()) to array with values of type \(self.name.type())"))
}
return .Ok(())
}
}
extension FieldAccessExpression: EvaluatableExpression {
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
let maybe_struct = self.strct.evaluate(execution: execution)
@@ -172,3 +246,48 @@ extension FieldAccessExpression: EvaluatableExpression {
return self.field.type
}
}
extension FieldAccessExpression: EvaluatableLValueExpression {
public func set(
to: any Common.P4Value, inScopes scopes: Common.ValueScopes, duringExecution execution: ProgramExecution
) -> Common.Result<(Common.ValueScopes, P4Value)> {
// For purposes of documentation, assume the field access expression we are evaluating is
// (strct_id).field_id = new_field_value
// where strct_id expands to
// (identifier.field_id1.field_id2...).field_id = new_field_value
// First, evaluate strct_id and make sure that it names a struct.
let maybe_value = self.strct.evaluate(execution: execution)
guard case .Ok(let value) = maybe_value else {
return Result.Error(Error(withMessage: "\(self.strct) cannot be evaluated: \(maybe_value.error()!)"))
}
guard let struct_value = value as? P4StructValue else {
return Result.Error(Error(withMessage: "\(self.strct) does not identify a struct"))
}
// Now we know that struct_id identifies a structure value.
// Update field_id of that structure and get the new structure value.
let set_result = struct_value.set(field: self.field, to: to)
guard case .Ok(let new_struct_value) = set_result else {
return .Error(set_result.error()!)
}
// That new structure value should be assignable to the lvalue that is strct_id.
// We use recursion here -- ultimately finding our way to a TypedIdentifier that
// will update the scope. Pretty cool!
let struct_lvalue = self.strct as! EvaluatableLValueExpression
return struct_lvalue.set(to: new_struct_value, inScopes: scopes, duringExecution: execution)
}
public func check(
to: any Common.EvaluatableExpression, inScopes scopes: Common.TypeScopes
) -> Common.Result<()> {
if !self.field.type.eq(rhs:to.type()) {
return .Error(Error(withMessage: "Cannot assign value of type \(to.type()) to field with type \(self.field.type)"))
}
return .Ok(())
}
}
+6 -2
View File
@@ -24,9 +24,13 @@ extension ParserAssignmentStatement: EvaluatableStatement {
guard case Result.Ok(let value) = result else {
return execution.setError(error: result.error()!)
}
let updated_scopes = execution.scopes.set(identifier: self.lvalue, withValue: value)
execution.scopes = updated_scopes
let maybe_updated_scopes = self.lvalue.set(
to: value, inScopes: execution.scopes, duringExecution: execution)
guard case Result.Ok(let updated_scopes) = maybe_updated_scopes else {
return execution.setError(error: maybe_updated_scopes.error()!)
}
execution.scopes = updated_scopes.0
return execution
}