diff --git a/Sources/Common/ProgramTypes.swift b/Sources/Common/ProgramTypes.swift index bc9dbd2..faf45ca 100644 --- a/Sources/Common/ProgramTypes.swift +++ b/Sources/Common/ProgramTypes.swift @@ -158,6 +158,10 @@ public struct P4Struct: P4Type { false } } + + public func def() -> any P4Value { + return P4StructValue(withType: self) + } } /// An instance of a P4 struct @@ -231,6 +235,9 @@ public struct P4Boolean: P4Type { default: false } } + public func def() -> any P4Value { + return P4BooleanValue(withValue: false) + } } /// An instance of a P4 boolean @@ -269,6 +276,9 @@ public struct P4Int: P4Type { default: false } } + public func def() -> any P4Value { + return P4IntValue(withValue: 0) + } } /// An instance of a P4 integer @@ -309,6 +319,9 @@ public struct P4String: P4Type { default: false } } + public func def() -> any P4Value { + return P4StringValue(withValue: "") + } } /// An instance of a P4 string public class P4StringValue: P4Value { @@ -358,6 +371,10 @@ public struct P4Array: P4Type { default: false } } + + public func def() -> P4Value { + return P4ArrayValue(withType: self, withValue: []) + } } /// An instance of a P4 array diff --git a/Sources/Common/Protocols.swift b/Sources/Common/Protocols.swift index affbe3e..89acb0f 100644 --- a/Sources/Common/Protocols.swift +++ b/Sources/Common/Protocols.swift @@ -34,14 +34,21 @@ public protocol EvaluatableStatement { public protocol P4Type: CustomStringConvertible { func eq(rhs: any P4Type) -> Bool + func def() -> P4Value } -public protocol P4Value: CustomStringConvertible { +public protocol P4Value: EvaluatableExpression, 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<()> +public extension P4Value { + func evaluate(execution: ProgramExecution) -> Result { + return Result.Ok(self) + } +} + +public protocol EvaluatableLValueExpression: EvaluatableExpression { + func set(to: P4Value, inScopes scopes: VarValueScopes, duringExecution execution: ProgramExecution) -> Result<(VarValueScopes, P4Value)> + func check(to: EvaluatableExpression, inScopes scopes: VarTypeScopes) -> Result<()> } diff --git a/Sources/P4Lang/Parser.swift b/Sources/P4Lang/Parser.swift index 99101e6..d252cc9 100644 --- a/Sources/P4Lang/Parser.swift +++ b/Sources/P4Lang/Parser.swift @@ -83,6 +83,10 @@ public class ParserState: P4Type, P4Value, Equatable, CustomStringConvertible { state = name statements = Array() } + + public func def() -> any P4Value { + return ParserState(name: Identifier(name: "")) + } } public class ParserStateDirectTransition: ParserState { @@ -211,4 +215,8 @@ public struct Parser: P4Type, P4Value { public var description: String { return "Parser" } + + public func def() -> any P4Value { + return Parser(withName: Identifier(name: "")) + } }