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