@@ -279,9 +279,9 @@ public class P4ArrayValue: P4Value {
|
|||||||
return P4Array()
|
return P4Array()
|
||||||
}
|
}
|
||||||
|
|
||||||
let value: Array<EvaluatableExpression>
|
let value: [EvaluatableExpression]
|
||||||
|
|
||||||
public init(withValue value: Array<EvaluatableExpression>) {
|
public init(withValue value: [EvaluatableExpression]) {
|
||||||
self.value = value
|
self.value = value
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -290,7 +290,7 @@ public class P4ArrayValue: P4Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func eq(rhs: P4Value) -> Bool {
|
public func eq(rhs: P4Value) -> Bool {
|
||||||
guard let _ = rhs as? P4ArrayValue else {
|
guard rhs as? P4ArrayValue != nil else {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
// TODO!!
|
// TODO!!
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ struct Expression {
|
|||||||
|
|
||||||
let localElementsParsers: [CompilableExpression.Type] = [
|
let localElementsParsers: [CompilableExpression.Type] = [
|
||||||
P4BooleanValue.self, P4StringValue.self, P4IntValue.self, TypedIdentifier.self,
|
P4BooleanValue.self, P4StringValue.self, P4IntValue.self, TypedIdentifier.self,
|
||||||
BinaryOperatorExpression.self, ArrayAccessExpression.self
|
BinaryOperatorExpression.self, ArrayAccessExpression.self,
|
||||||
]
|
]
|
||||||
|
|
||||||
for le_parser in localElementsParsers {
|
for le_parser in localElementsParsers {
|
||||||
@@ -313,7 +313,9 @@ extension BinaryOperatorExpression: CompilableExpression {
|
|||||||
}
|
}
|
||||||
|
|
||||||
extension ArrayAccessExpression: CompilableExpression {
|
extension ArrayAccessExpression: CompilableExpression {
|
||||||
static func compile(node: SwiftTreeSitter.Node, withContext context: CompilerContext) -> Common.Result<(any Common.EvaluatableExpression)?> {
|
static func compile(
|
||||||
|
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||||
|
) -> Common.Result<(any Common.EvaluatableExpression)?> {
|
||||||
let expression = node.child(at: 0)!
|
let expression = node.child(at: 0)!
|
||||||
|
|
||||||
#SkipUnlessNodeType<Node, EvaluatableExpression?>(
|
#SkipUnlessNodeType<Node, EvaluatableExpression?>(
|
||||||
@@ -362,12 +364,14 @@ extension ArrayAccessExpression: CompilableExpression {
|
|||||||
|
|
||||||
let array_access_indexor_node = currentChild!
|
let array_access_indexor_node = currentChild!
|
||||||
|
|
||||||
let maybe_array_identifier = Expression.Compile(node: array_access_identifier_node, withContext: context)
|
let maybe_array_identifier = Expression.Compile(
|
||||||
|
node: array_access_identifier_node, withContext: context)
|
||||||
guard case Result.Ok(let array_identifier) = maybe_array_identifier else {
|
guard case Result.Ok(let array_identifier) = maybe_array_identifier else {
|
||||||
return Result.Error(maybe_array_identifier.error()!)
|
return Result.Error(maybe_array_identifier.error()!)
|
||||||
}
|
}
|
||||||
|
|
||||||
let maybe_array_indexor = Expression.Compile(node: array_access_indexor_node, withContext: context)
|
let maybe_array_indexor = Expression.Compile(
|
||||||
|
node: array_access_indexor_node, withContext: context)
|
||||||
guard case Result.Ok(let array_indexor) = maybe_array_indexor else {
|
guard case Result.Ok(let array_indexor) = maybe_array_indexor else {
|
||||||
return Result.Error(maybe_array_indexor.error()!)
|
return Result.Error(maybe_array_indexor.error()!)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,9 @@ public struct Program {
|
|||||||
return Program.Compile(source, withGlobalTypes: .none)
|
return Program.Compile(source, withGlobalTypes: .none)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func Compile(_ source: String, withGlobalTypes globalTypes: LexicalScopes?) -> Result<P4Lang.Program> {
|
public static func Compile(
|
||||||
|
_ source: String, withGlobalTypes globalTypes: LexicalScopes?
|
||||||
|
) -> Result<P4Lang.Program> {
|
||||||
|
|
||||||
let maybe_parser = ConfigureP4Parser()
|
let maybe_parser = ConfigureP4Parser()
|
||||||
guard case .Ok(let p) = maybe_parser else {
|
guard case .Ok(let p) = maybe_parser else {
|
||||||
@@ -194,9 +196,10 @@ public struct Program {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Any of the types that are in the top-level scope should go into the program!
|
// Any of the types that are in the top-level scope should go into the program!
|
||||||
program.types = Array(compilation_context.names.map() { (_, v) in
|
program.types = Array(
|
||||||
v
|
compilation_context.names.map { (_, v) in
|
||||||
})
|
v
|
||||||
|
})
|
||||||
return Result.Ok(program)
|
return Result.Ok(program)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,9 @@ public struct ParserRuntime: CustomStringConvertible {
|
|||||||
return ParserRuntime.create(program: program, withInitialValues: .none)
|
return ParserRuntime.create(program: program, withInitialValues: .none)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func create(program: P4Lang.Program, withInitialValues initial: ValueScopes?) -> Result<ParserRuntime> {
|
public static func create(
|
||||||
|
program: P4Lang.Program, withInitialValues initial: ValueScopes?
|
||||||
|
) -> Result<ParserRuntime> {
|
||||||
return switch program.starting_parser() {
|
return switch program.starting_parser() {
|
||||||
case .Ok(let parser):
|
case .Ok(let parser):
|
||||||
.Ok(P4Runtime.ParserRuntime(parser: parser, withInitialValues: initial))
|
.Ok(P4Runtime.ParserRuntime(parser: parser, withInitialValues: initial))
|
||||||
@@ -50,11 +52,12 @@ public struct ParserRuntime: CustomStringConvertible {
|
|||||||
/// Run the P4 parser on a given packet
|
/// Run the P4 parser on a given packet
|
||||||
public func run() -> Result<(ParserState, ProgramExecution)> {
|
public func run() -> Result<(ParserState, ProgramExecution)> {
|
||||||
|
|
||||||
let pe = if let initial = initialValues {
|
let pe =
|
||||||
ProgramExecution(withGlobalValues: initial)
|
if let initial = initialValues {
|
||||||
} else {
|
ProgramExecution(withGlobalValues: initial)
|
||||||
ProgramExecution()
|
} else {
|
||||||
}
|
ProgramExecution()
|
||||||
|
}
|
||||||
|
|
||||||
let (end_state, execution) = parser.execute(execution: pe)
|
let (end_state, execution) = parser.execute(execution: pe)
|
||||||
if let error = execution.getError() {
|
if let error = execution.getError() {
|
||||||
|
|||||||
Reference in New Issue
Block a user