compiler, runtime: Begin Runtime Refactor
Ultimately, the goal is to completely separate the compilation from the runtime to make it possible to have the interpreter/evaluator be "just another" entity that can perform meaningful work when given a parsed GP4 program. Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -15,28 +15,16 @@
|
||||
// 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 protocol EvaluatableExpression {
|
||||
/// Evaluate an expression for a given execution
|
||||
/// - Parameters
|
||||
/// - execution: The execution context in which to evaluate the expression
|
||||
/// - Returns: The value of expression
|
||||
func evaluate(execution: ProgramExecution) -> (Result<P4Value>, ProgramExecution)
|
||||
func type() -> P4QualifiedType
|
||||
}
|
||||
|
||||
public protocol EvaluatableStatement {
|
||||
/// Evaluate a statement for a given execution
|
||||
/// - Parameters
|
||||
/// - execution: The execution context in which to evaluate the parser statement
|
||||
/// - Returns: A tuple of
|
||||
/// 1. Whether this statement affects control flow.
|
||||
/// 2. An updated execution after evaluating the parser statement
|
||||
func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution)
|
||||
}
|
||||
|
||||
public protocol P4Type: CustomStringConvertible {
|
||||
func eq(rhs: any P4Type) -> Bool
|
||||
func def() -> P4DataValue?
|
||||
func instantiable() -> Bool
|
||||
}
|
||||
|
||||
extension P4Type {
|
||||
public func instantiable() -> Bool {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
public protocol P4DataValue: CustomStringConvertible {
|
||||
@@ -48,28 +36,6 @@ public protocol P4DataValue: CustomStringConvertible {
|
||||
func gte(rhs: P4DataValue) -> Bool
|
||||
}
|
||||
|
||||
public protocol EvaluatableLValueExpression: EvaluatableExpression {
|
||||
func set(
|
||||
to: P4Value, inScopes scopes: VarValueScopes, duringExecution execution: ProgramExecution
|
||||
) -> Result<(VarValueScopes, P4Value)>
|
||||
func check(to: EvaluatableExpression, inScopes scopes: StaticVarValueScopes) -> Result<()>
|
||||
}
|
||||
|
||||
public protocol ProgramExecutionEvaluator {
|
||||
func ExecuteStatements(
|
||||
_ statements: [EvaluatableStatement], inExecution execution: ProgramExecution,
|
||||
_ handler: ExecuteStatementResultHandlerT?
|
||||
) -> (ControlFlow, ProgramExecution)
|
||||
|
||||
func ExecuteStatements(
|
||||
_ statements: [EvaluatableStatement], inExecution execution: ProgramExecution
|
||||
) -> (ControlFlow, ProgramExecution)
|
||||
|
||||
func EvaluateExpression(
|
||||
_ expression: EvaluatableExpression, inExecution execution: ProgramExecution,
|
||||
) -> (Result<P4Value>, ProgramExecution)
|
||||
}
|
||||
|
||||
public protocol Errorable: CustomStringConvertible {
|
||||
func format(_ formatter: Formattable) -> String
|
||||
func format(_ formatter: Formattable, _ sc: SourceCode) -> String
|
||||
@@ -91,6 +57,64 @@ public protocol Formattable {
|
||||
func formatWithStyle(_ value: String, _ style: Style) -> String
|
||||
}
|
||||
|
||||
public protocol P4Statement {
|
||||
/// Evaluate a statement for a given execution
|
||||
/// - Parameters
|
||||
/// - execution: The execution context in which to evaluate the parser statement
|
||||
/// - Returns: A tuple of
|
||||
/// 1. Whether this statement affects control flow.
|
||||
/// 2. An updated execution after evaluating the parser statement
|
||||
func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution)
|
||||
|
||||
func effect(context: CompilerContext) -> CompilerContext
|
||||
}
|
||||
|
||||
public protocol P4Expression {
|
||||
func effect(context: CompilerContext) -> CompilerContext
|
||||
func type() -> P4QualifiedType
|
||||
/// Evaluate an expression for a given execution
|
||||
/// - Parameters
|
||||
/// - execution: The execution context in which to evaluate the expression
|
||||
/// - Returns: The value of expression
|
||||
func evaluate(execution: ProgramExecution) -> (Result<P4Value>, ProgramExecution)
|
||||
}
|
||||
|
||||
extension P4Expression {
|
||||
func effect(context: CompilerContext) -> CompilerContext {
|
||||
return context
|
||||
}
|
||||
}
|
||||
|
||||
public protocol P4LValueExpression {
|
||||
func effect(context: CompilerContext) -> CompilerContext
|
||||
func type() -> P4QualifiedType
|
||||
func check(to: P4Expression, inScopes scopes: StaticVarValueScopes) -> Result<()>
|
||||
func set(
|
||||
to: P4Value, inScopes scopes: VarValueScopes, duringExecution execution: ProgramExecution
|
||||
) -> Result<(VarValueScopes, P4Value)>
|
||||
|
||||
}
|
||||
|
||||
/// TODO: Only generate these after going through the codegen!
|
||||
public typealias EvaluatableStatement = P4Statement
|
||||
public typealias EvaluatableLValueExpression = P4LValueExpression
|
||||
public typealias EvaluatableExpression = P4Expression
|
||||
|
||||
public protocol ProgramExecutionEvaluator {
|
||||
func ExecuteStatements(
|
||||
_ statements: [EvaluatableStatement], inExecution execution: ProgramExecution,
|
||||
_ handler: ExecuteStatementResultHandlerT?
|
||||
) -> (ControlFlow, ProgramExecution)
|
||||
|
||||
func ExecuteStatements(
|
||||
_ statements: [EvaluatableStatement], inExecution execution: ProgramExecution
|
||||
) -> (ControlFlow, ProgramExecution)
|
||||
|
||||
func EvaluateExpression(
|
||||
_ expression: EvaluatableExpression, inExecution execution: ProgramExecution,
|
||||
) -> (Result<P4Value>, ProgramExecution)
|
||||
}
|
||||
|
||||
extension ProgramExecutionEvaluator {
|
||||
public func ExecuteStatements(
|
||||
_ statements: [EvaluatableStatement], inExecution execution: ProgramExecution
|
||||
|
||||
Reference in New Issue
Block a user