@@ -108,9 +108,9 @@ public typealias VarValueScopes = Scopes<P4Value>
|
||||
|
||||
/// Indicate the control flow result of a particular statement.
|
||||
public enum ControlFlow {
|
||||
case Next
|
||||
case Continue
|
||||
case Break
|
||||
case Return(P4Value?)
|
||||
case Error
|
||||
case Next
|
||||
case Continue
|
||||
case Break
|
||||
case Return(P4Value?)
|
||||
case Error
|
||||
}
|
||||
|
||||
@@ -263,7 +263,8 @@ func argument_list_compiler(
|
||||
// Otherwise, there should be one argument left!
|
||||
switch Argument.Compile(node: currentChild!, withContext: context) {
|
||||
case .Ok(let (ce, updated_context)):
|
||||
return Result.Ok((arguments.addArgument(Argument(ce, atIndex: arguments.count() + 1)), updated_context))
|
||||
return Result.Ok(
|
||||
(arguments.addArgument(Argument(ce, atIndex: arguments.count() + 1)), updated_context))
|
||||
case .Error(let e): return Result.Error(e)
|
||||
}
|
||||
}
|
||||
@@ -311,8 +312,8 @@ extension Argument: Compilable {
|
||||
let expression_node = node.child(at: 0)!
|
||||
|
||||
return switch Expression.Compile(node: expression_node, withContext: context) {
|
||||
case .Ok(let compiled_expression): .Ok((compiled_expression, context))
|
||||
case .Error(let e): .Error(e)
|
||||
case .Ok(let compiled_expression): .Ok((compiled_expression, context))
|
||||
case .Error(let e): .Error(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,7 +163,7 @@ struct Expression {
|
||||
let expression_parsers: [CompilableExpression.Type] = [
|
||||
P4BooleanValue.self, P4StringValue.self, P4IntValue.self, TypedIdentifier.self,
|
||||
BinaryOperatorExpression.self, ArrayAccessExpression.self, FieldAccessExpression.self,
|
||||
FunctionCall.self
|
||||
FunctionCall.self,
|
||||
]
|
||||
|
||||
for candidate_expression_parser in expression_parsers {
|
||||
@@ -680,19 +680,22 @@ extension FunctionCall: CompilableExpression {
|
||||
return Result.Error(maybe_callee_name.error()!)
|
||||
}
|
||||
|
||||
let maybe_callee = switch context.types.lookup(identifier: callee_name) {
|
||||
case .Ok(let looked_up): switch looked_up {
|
||||
case let callee as FunctionDeclaration: Result.Ok(callee) // What we found is actually a function declaration
|
||||
default: Result<FunctionDeclaration>.Error(ErrorOnNode(node: currentChild!, withError: "\(callee_name) is not a function"))
|
||||
}
|
||||
let maybe_callee =
|
||||
switch context.types.lookup(identifier: callee_name) {
|
||||
case .Ok(let looked_up):
|
||||
switch looked_up {
|
||||
case let callee as FunctionDeclaration: Result.Ok(callee) // What we found is actually a function declaration
|
||||
default:
|
||||
Result<FunctionDeclaration>.Error(
|
||||
ErrorOnNode(node: currentChild!, withError: "\(callee_name) is not a function"))
|
||||
}
|
||||
case .Error(let e): Result<FunctionDeclaration>.Error(e)
|
||||
}
|
||||
}
|
||||
|
||||
guard case .Ok(let callee) = maybe_callee else {
|
||||
return .Error(maybe_callee.error()!)
|
||||
}
|
||||
|
||||
|
||||
currentChildIdx += 1
|
||||
currentChildIdxSafe += 1
|
||||
if expression.childCount < currentChildIdxSafe {
|
||||
@@ -702,8 +705,8 @@ extension FunctionCall: CompilableExpression {
|
||||
currentChild = expression.child(at: currentChildIdx)
|
||||
|
||||
let maybe_argument_list = ArgumentList.Compile(node: currentChild!, withContext: context)
|
||||
|
||||
guard case .Ok((let arguments, _)) = maybe_argument_list else {
|
||||
|
||||
guard case .Ok((let arguments, _)) = maybe_argument_list else {
|
||||
return .Error(maybe_argument_list.error()!)
|
||||
}
|
||||
|
||||
|
||||
@@ -314,4 +314,4 @@ extension ReturnStatement: CompilableStatement {
|
||||
case .Error(let e): .Error(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,4 +117,4 @@ public struct Argument {
|
||||
self.argument = argument
|
||||
self.index = index
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,4 +209,4 @@ public struct FunctionCall {
|
||||
self.callee = callee
|
||||
self.arguments = arguments
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,4 +62,4 @@ public struct ReturnStatement {
|
||||
public init(_ value: EvaluatableExpression) {
|
||||
self.value = value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,4 +16,4 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import Common
|
||||
import P4Lang
|
||||
import P4Lang
|
||||
|
||||
@@ -400,18 +400,21 @@ extension FunctionCall: EvaluatableExpression {
|
||||
guard case .Ok(let argument_value) = maybe_argument_value else {
|
||||
return .Error(Error(withMessage: "Cannot evaluate argument \(arg_idx): \(argument)"))
|
||||
}
|
||||
called_execution = called_execution.declare(identifier: parameter.name, withValue: argument_value)
|
||||
called_execution = called_execution.declare(
|
||||
identifier: parameter.name, withValue: argument_value)
|
||||
}
|
||||
|
||||
let (control_flow, _) = body.evaluate(execution: called_execution)
|
||||
|
||||
return switch control_flow {
|
||||
case ControlFlow.Return(let value): if let value = value {
|
||||
.Ok(value)
|
||||
case ControlFlow.Return(let value):
|
||||
if let value = value {
|
||||
.Ok(value)
|
||||
} else {
|
||||
.Error(Error(withMessage: "No value returned from called function (\(self.callee.name))"))
|
||||
}
|
||||
default: .Error(Error(withMessage: "No value returned from called function (\(self.callee.name))"))
|
||||
default:
|
||||
.Error(Error(withMessage: "No value returned from called function (\(self.callee.name))"))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -45,9 +45,14 @@ extension ParserStateDirectTransition: EvaluatableParserState {
|
||||
for statement in statements {
|
||||
let (control_flow, next_program) = statement.evaluate(execution: program)
|
||||
switch control_flow {
|
||||
case .Next: program = next_program // Ok!
|
||||
case .Error: return (reject, next_program)
|
||||
default: return (reject, next_program.setError(error: Error(withMessage: "Invalid control flow (\(control_flow) in parser)")))
|
||||
case .Next: program = next_program // Ok!
|
||||
case .Error: return (reject, next_program)
|
||||
default:
|
||||
return (
|
||||
reject,
|
||||
next_program.setError(
|
||||
error: Error(withMessage: "Invalid control flow (\(control_flow) in parser)"))
|
||||
)
|
||||
}
|
||||
}
|
||||
let res = program.scopes.lookup(identifier: get_next_state())
|
||||
@@ -100,9 +105,14 @@ extension ParserStateSelectTransition: EvaluatableParserState {
|
||||
for statement in statements {
|
||||
let (control_flow, next_program) = statement.evaluate(execution: program)
|
||||
switch control_flow {
|
||||
case .Next: program = next_program // Ok!
|
||||
case .Error: return (reject, next_program)
|
||||
default: return (reject, next_program.setError(error: Error(withMessage: "Invalid control flow (\(control_flow) in parser)")))
|
||||
case .Next: program = next_program // Ok!
|
||||
case .Error: return (reject, next_program)
|
||||
default:
|
||||
return (
|
||||
reject,
|
||||
next_program.setError(
|
||||
error: Error(withMessage: "Invalid control flow (\(control_flow) in parser)"))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -108,10 +108,8 @@ extension ExpressionStatement: EvaluatableStatement {
|
||||
extension ReturnStatement: EvaluatableStatement {
|
||||
public func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution) {
|
||||
return switch self.value.evaluate(execution: execution) {
|
||||
case .Ok(let v): (ControlFlow.Return(v), execution)
|
||||
case .Error(let e): (ControlFlow.Error, execution.setError(error: e))
|
||||
case .Ok(let v): (ControlFlow.Return(v), execution)
|
||||
case .Error(let e): (ControlFlow.Error, execution.setError(error: e))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user