Support Calling Parsers With Parameters
Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -38,6 +38,10 @@ public struct Parameter: CustomStringConvertible {
|
||||
public struct ParameterList: CustomStringConvertible {
|
||||
public var parameters: [Parameter]
|
||||
|
||||
public init() {
|
||||
self.parameters = Array()
|
||||
}
|
||||
|
||||
public init(_ parameters: [Parameter]) {
|
||||
self.parameters = parameters
|
||||
}
|
||||
|
||||
@@ -200,3 +200,34 @@ public struct FieldAccessExpression {
|
||||
self.field = field
|
||||
}
|
||||
}
|
||||
|
||||
public struct ArgumentList {
|
||||
public let arguments: [(Int, EvaluatableExpression)]
|
||||
public init(_ arguments: [EvaluatableExpression]) {
|
||||
self.arguments = zip(1..., arguments).map { (idx, argument) in
|
||||
(idx, argument)
|
||||
}
|
||||
}
|
||||
|
||||
public func compatible(_ parameters: ParameterList) -> Result<()> {
|
||||
if self.arguments.count != parameters.parameters.count {
|
||||
return .Error(
|
||||
Error(
|
||||
withMessage:
|
||||
"\(self.arguments.count) arguments found but \(parameters.parameters.count) required"))
|
||||
}
|
||||
|
||||
for (arg, param) in zip(self.arguments, parameters.parameters) {
|
||||
let arg_index = arg.0
|
||||
let arg_type = arg.1.type()
|
||||
if !arg_type.eq(rhs: param.type) {
|
||||
return .Error(
|
||||
Error(
|
||||
withMessage:
|
||||
"Argument \(arg_index)'s type (\(arg_type)) is incompatible with the parameter type (\(param.type))"
|
||||
))
|
||||
}
|
||||
}
|
||||
return .Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -319,10 +319,11 @@ public struct Parser: P4Type, P4Value {
|
||||
public var states: ParserStates
|
||||
|
||||
public var name: Identifier
|
||||
public var parameters: ParameterList?
|
||||
public var parameters: ParameterList
|
||||
|
||||
public init(withName name: Identifier) {
|
||||
self.states = ParserStates()
|
||||
self.parameters = ParameterList()
|
||||
self.name = name
|
||||
}
|
||||
|
||||
@@ -349,7 +350,6 @@ public struct Parser: P4Type, P4Value {
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
let parameters = self.parameters?.description ?? "N/A"
|
||||
return "Parser \(self.name) with parameters: \(parameters) and states: \(self.states)"
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user