compiler, runtime, common: Support (in)out Parameters

When a function is called, if there is an (in)out parameter,
make sure that updated values are propogated to the calling
function.

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-04-16 06:58:45 -04:00
parent 94086c8e17
commit 82c125e4d1
14 changed files with 322 additions and 139 deletions
+6
View File
@@ -87,6 +87,12 @@ open class ProgramExecution: CustomStringConvertible {
return new_pe
}
public func replaceScopes(_ new_scopes: VarValueScopes) -> ProgramExecution {
let new_pe = ProgramExecution(copy: self)
new_pe.scopes = new_scopes
return new_pe
}
public func declare(identifier: Identifier, withValue value: P4Value) -> ProgramExecution {
let new_pe = ProgramExecution(copy: self)
let new_scopes = new_pe.scopes.declare(identifier: identifier, withValue: value)
+40 -2
View File
@@ -15,6 +15,13 @@
// 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 TypeCheckResults: Equatable {
case Ok
case ReadOnly
case WrongDirection
case IncompatibleTypes
}
public enum Direction: Equatable, CustomStringConvertible {
case In
case Out
@@ -115,11 +122,11 @@ public struct P4Type: CustomStringConvertible {
}
public func update(removeAttribute attribute: P4TypeAttribute) -> P4Type {
return P4Type(self._data_type, self._attributes.update(addAttribute: attribute))
return P4Type(self._data_type, self._attributes.update(removeAttribute: attribute))
}
public func update(addAttribute attribute: P4TypeAttribute) -> P4Type {
return P4Type(self._data_type, self._attributes.update(removeAttribute: attribute))
return P4Type(self._data_type, self._attributes.update(addAttribute: attribute))
}
public func direction() -> Direction? {
@@ -143,6 +150,37 @@ public struct P4Type: CustomStringConvertible {
&& self.dataType().eq(rhs: rhs.dataType())
}
public func assignable() -> TypeCheckResults {
if self.readOnly() {
return TypeCheckResults.ReadOnly
}
if let direction = direction(),
direction == Direction.In
{
return TypeCheckResults.WrongDirection
}
return TypeCheckResults.Ok
}
public func assignableFromType(_ rhs: P4Type) -> TypeCheckResults {
if !self.dataType().eq(rhs: rhs.dataType()) {
return TypeCheckResults.IncompatibleTypes
}
if self.readOnly() {
return TypeCheckResults.ReadOnly
}
if let direction = direction(),
direction == Direction.In
{
return TypeCheckResults.WrongDirection
}
return TypeCheckResults.Ok
}
public static func ReadOnly(_ type: P4DataType) -> P4Type {
return P4Type(type, P4TypeAttributes.ReadOnly())
}
+1 -1
View File
@@ -20,7 +20,7 @@ public protocol EvaluatableExpression {
/// - Parameters
/// - execution: The execution context in which to evaluate the expression
/// - Returns: The value of expression
func evaluate(execution: ProgramExecution) -> Result<P4Value>
func evaluate(execution: ProgramExecution) -> (Result<P4Value>, ProgramExecution)
func type() -> P4Type
}