compiler, runtime: Refactor P4Type to P4QualifiedType
Also, refer to the different pieces of the qualified type as qualifiers and not attributes. Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -229,7 +229,7 @@ extension Parameter: Compilable {
|
||||
Parameter(
|
||||
identifier: parameter_name,
|
||||
withType: direction != nil
|
||||
? parameter_type.update(addAttribute: P4TypeAttribute.Direction(direction!))
|
||||
? parameter_type.update(addAttribute: P4TypeQualifier.Direction(direction!))
|
||||
: parameter_type),
|
||||
context
|
||||
))
|
||||
|
||||
@@ -54,7 +54,7 @@ public struct CompilerContext {
|
||||
let types: TypeTypeScopes
|
||||
let externs: TypeTypeScopes
|
||||
let ffis: [P4FFI]
|
||||
let expected_type: P4Type?
|
||||
let expected_type: P4QualifiedType?
|
||||
let extern_context: Bool
|
||||
|
||||
public init() {
|
||||
@@ -77,7 +77,7 @@ public struct CompilerContext {
|
||||
|
||||
public init(
|
||||
withInstances _instances: VarTypeScopes, withTypes _types: TypeTypeScopes,
|
||||
withExpectation expectation: P4Type?, withExtern extern: Bool,
|
||||
withExpectation expectation: P4QualifiedType?, withExtern extern: Bool,
|
||||
withExterns externs: TypeTypeScopes, withFFIs foreigns: [P4FFI]
|
||||
) {
|
||||
instances = _instances
|
||||
@@ -118,7 +118,7 @@ public struct CompilerContext {
|
||||
///
|
||||
/// - Parameter expectation: a ``P4Type?`` to (re)set the type the compiler is expecting.
|
||||
/// - Returns: A new compiler context based on the current but new expected type.
|
||||
public func update(newExpectation expectation: P4Type?) -> CompilerContext {
|
||||
public func update(newExpectation expectation: P4QualifiedType?) -> CompilerContext {
|
||||
return CompilerContext(
|
||||
withInstances: self.instances, withTypes: self.types, withExpectation: expectation,
|
||||
withExtern: self.extern_context, withExterns: self.externs, withFFIs: self.ffis)
|
||||
|
||||
@@ -128,7 +128,7 @@ extension FunctionDeclaration: CompilableDeclaration {
|
||||
let function_declaration = Declaration(
|
||||
TypedIdentifier(
|
||||
id: function_name,
|
||||
withType: P4Type(
|
||||
withType: P4QualifiedType(
|
||||
FunctionDeclaration(
|
||||
named: function_name, ofType: function_type, withParameters: function_parameters,
|
||||
withBody: function_body))))
|
||||
@@ -144,7 +144,7 @@ extension FunctionDeclaration: CompilableDeclaration {
|
||||
? context
|
||||
: context.update(
|
||||
newTypes: context.types.declare(
|
||||
identifier: function_name, withValue: function_declaration.identifier.type.dataType())
|
||||
identifier: function_name, withValue: function_declaration.identifier.type.baseType())
|
||||
)
|
||||
))
|
||||
}
|
||||
@@ -189,7 +189,7 @@ extension P4Struct: CompilableDeclaration {
|
||||
let struc = Declaration(
|
||||
TypedIdentifier(
|
||||
id: struct_identifier,
|
||||
withType: P4Type(P4Struct(withName: struct_identifier, andFields: P4StructFields([])))))
|
||||
withType: P4QualifiedType(P4Struct(withName: struct_identifier, andFields: P4StructFields([])))))
|
||||
return Result.Ok(
|
||||
(
|
||||
struc,
|
||||
@@ -197,7 +197,7 @@ extension P4Struct: CompilableDeclaration {
|
||||
? context
|
||||
: context.update(
|
||||
newTypes: context.types.declare(
|
||||
identifier: struct_identifier, withValue: struc.identifier.type.dataType()))
|
||||
identifier: struct_identifier, withValue: struc.identifier.type.baseType()))
|
||||
))
|
||||
}
|
||||
|
||||
@@ -234,7 +234,7 @@ extension P4Struct: CompilableDeclaration {
|
||||
let declared_struct = Declaration(
|
||||
TypedIdentifier(
|
||||
id: struct_identifier,
|
||||
withType: P4Type(
|
||||
withType: P4QualifiedType(
|
||||
P4Struct(
|
||||
withName: struct_identifier, andFields: P4StructFields(parsed_fields)))))
|
||||
return .Ok(
|
||||
@@ -244,7 +244,7 @@ extension P4Struct: CompilableDeclaration {
|
||||
? current_context
|
||||
: current_context.update(
|
||||
newTypes: current_context.types.declare(
|
||||
identifier: struct_identifier, withValue: declared_struct.identifier.type.dataType()))
|
||||
identifier: struct_identifier, withValue: declared_struct.identifier.type.baseType()))
|
||||
))
|
||||
}
|
||||
}
|
||||
@@ -366,7 +366,7 @@ extension P4Lang.Parser: CompilableDeclaration {
|
||||
{
|
||||
case Result.Ok((let parser, let updated_context)):
|
||||
let parser_declaration = Declaration(
|
||||
TypedIdentifier(id: parser.name, withType: P4Type(parser)))
|
||||
TypedIdentifier(id: parser.name, withType: P4QualifiedType(parser)))
|
||||
// Create a new context with the name of the parser that was just compiled in scope.
|
||||
return .Ok(
|
||||
(
|
||||
@@ -518,7 +518,7 @@ extension Control: CompilableDeclaration {
|
||||
Declaration(
|
||||
TypedIdentifier(
|
||||
id: control_name,
|
||||
withType: P4Type(
|
||||
withType: P4QualifiedType(
|
||||
Control(
|
||||
named: control_name, withParameters: control_parameters, withTable: tables[0],
|
||||
withActions: Actions(withActions: actions), withApply: apply))))
|
||||
@@ -753,7 +753,7 @@ extension TableActionsProperty: Compilable {
|
||||
switch context.types.lookup(identifier: listed_action) {
|
||||
case .Ok(let maybe_action):
|
||||
if maybe_action.eq(rhs: Action()) {
|
||||
return .Ok(TypedIdentifier(id: listed_action, withType: P4Type(maybe_action)))
|
||||
return .Ok(TypedIdentifier(id: listed_action, withType: P4QualifiedType(maybe_action)))
|
||||
}
|
||||
return .Error(
|
||||
ErrorOnNode(node: node, withError: "\(listed_action) does not name an action"))
|
||||
@@ -923,7 +923,7 @@ extension ExternDeclaration: CompilableDeclaration {
|
||||
// with the matching "stuff".
|
||||
|
||||
let found_ffi = context.ffis.first { ffi in
|
||||
ffi.type().dataType().eq(rhs: declared.identifier.type.dataType())
|
||||
ffi.type().baseType().eq(rhs: declared.identifier.type.baseType())
|
||||
}
|
||||
|
||||
guard let found_ffi = found_ffi else {
|
||||
|
||||
@@ -392,49 +392,49 @@ extension BinaryOperatorExpression: CompilableExpression {
|
||||
return Result.Error(maybe_right_hand_side.error()!)
|
||||
}
|
||||
|
||||
let evaluators: [String: (String, P4Type, BinaryOperatorChecker?, BinaryOperatorEvaluator)] = [
|
||||
let evaluators: [String: (String, P4QualifiedType, BinaryOperatorChecker?, BinaryOperatorEvaluator)] = [
|
||||
"binaryEqualOperatorExpression": (
|
||||
"Binary Equal", P4Type(P4Boolean()), Optional<BinaryOperatorChecker>.none,
|
||||
"Binary Equal", P4QualifiedType(P4Boolean()), Optional<BinaryOperatorChecker>.none,
|
||||
binary_equal_operator_evaluator
|
||||
),
|
||||
"binaryLessThanOperatorExpression": (
|
||||
"Binary Less Than", P4Type(P4Boolean()), Optional<BinaryOperatorChecker>.none,
|
||||
"Binary Less Than", P4QualifiedType(P4Boolean()), Optional<BinaryOperatorChecker>.none,
|
||||
binary_lt_operator_evaluator
|
||||
),
|
||||
"binaryLessThanEqualOperatorExpression": (
|
||||
"Binary Less Than Or Equal", P4Type(P4Boolean()), Optional<BinaryOperatorChecker>.none,
|
||||
"Binary Less Than Or Equal", P4QualifiedType(P4Boolean()), Optional<BinaryOperatorChecker>.none,
|
||||
binary_lte_operator_evaluator
|
||||
),
|
||||
"binaryGreaterThanOperatorExpression": (
|
||||
"Binary Greater Than", P4Type(P4Boolean()), Optional<BinaryOperatorChecker>.none,
|
||||
"Binary Greater Than", P4QualifiedType(P4Boolean()), Optional<BinaryOperatorChecker>.none,
|
||||
binary_gt_operator_evaluator
|
||||
),
|
||||
"binaryGreaterThanEqualOperatorExpression": (
|
||||
"Binary Greater Than Or Equal", P4Type(P4Boolean()), Optional<BinaryOperatorChecker>.none,
|
||||
"Binary Greater Than Or Equal", P4QualifiedType(P4Boolean()), Optional<BinaryOperatorChecker>.none,
|
||||
binary_gte_operator_evaluator
|
||||
),
|
||||
"binaryAndOperatorExpression": (
|
||||
"Binary Or", P4Type(P4Boolean()), binary_and_or_operator_checker,
|
||||
"Binary Or", P4QualifiedType(P4Boolean()), binary_and_or_operator_checker,
|
||||
binary_and_operator_evaluator
|
||||
),
|
||||
"binaryOrOperatorExpression": (
|
||||
"Binary And", P4Type(P4Boolean()), binary_and_or_operator_checker,
|
||||
"Binary And", P4QualifiedType(P4Boolean()), binary_and_or_operator_checker,
|
||||
binary_or_operator_evaluator
|
||||
),
|
||||
"binaryAddOperatorExpression": (
|
||||
"Binary Add", P4Type(P4Int()), binary_int_math_operator_checker,
|
||||
"Binary Add", P4QualifiedType(P4Int()), binary_int_math_operator_checker,
|
||||
binary_add_operator_evaluator
|
||||
),
|
||||
"binarySubtractOperatorExpression": (
|
||||
"Binary Subtract", P4Type(P4Int()), binary_int_math_operator_checker,
|
||||
"Binary Subtract", P4QualifiedType(P4Int()), binary_int_math_operator_checker,
|
||||
binary_subtract_operator_evaluator
|
||||
),
|
||||
"binaryMultiplyOperatorExpression": (
|
||||
"Binary Multiply", P4Type(P4Int()), binary_int_math_operator_checker,
|
||||
"Binary Multiply", P4QualifiedType(P4Int()), binary_int_math_operator_checker,
|
||||
binary_multiply_operator_evaluator
|
||||
),
|
||||
"binaryDivideOperatorExpression": (
|
||||
"Binary Divide", P4Type(P4Int()), binary_int_math_operator_checker,
|
||||
"Binary Divide", P4QualifiedType(P4Int()), binary_int_math_operator_checker,
|
||||
binary_divide_operator_evaluator
|
||||
),
|
||||
]
|
||||
@@ -510,7 +510,7 @@ extension ArrayAccessExpression: CompilableExpression {
|
||||
}
|
||||
|
||||
let maybe_array_type = array_identifier.type()
|
||||
guard let array_type = maybe_array_type.dataType() as? P4Array else {
|
||||
guard let array_type = maybe_array_type.baseType() as? P4Array else {
|
||||
return Result.Error(
|
||||
ErrorOnNode(
|
||||
node: array_access_identifier_node,
|
||||
@@ -581,7 +581,7 @@ extension FieldAccessExpression: CompilableExpression {
|
||||
guard case Result.Ok(let struct_identifier) = maybe_struct_identifier else {
|
||||
return Result.Error(maybe_struct_identifier.error()!)
|
||||
}
|
||||
guard let struct_type = struct_identifier.type().dataType() as? P4Struct else {
|
||||
guard let struct_type = struct_identifier.type().baseType() as? P4Struct else {
|
||||
return .Error(
|
||||
ErrorOnNode(
|
||||
node: struct_identifier_node,
|
||||
@@ -690,7 +690,7 @@ extension FunctionCall: CompilableExpression {
|
||||
switch context.externs.lookup(identifier: callee_name) {
|
||||
case .Ok(let callee as Declaration):
|
||||
// Now, make sure that it is a function declaration!
|
||||
switch callee.identifier.type.dataType() {
|
||||
switch callee.identifier.type.baseType() {
|
||||
case is FunctionDeclaration: Result.Ok((.none, callee))
|
||||
default:
|
||||
.Error(ErrorOnNode(node: current_node!, withError: "\(callee_name) is not a function"))
|
||||
@@ -725,7 +725,7 @@ extension FunctionCall: CompilableExpression {
|
||||
switch callee {
|
||||
case (.some(let callee), .none): Optional<ParameterList>.some(callee.params)
|
||||
case (.none, .some(let callee)):
|
||||
Optional<ParameterList>.some((callee.ffi!.type().dataType() as! FunctionDeclaration).params)
|
||||
Optional<ParameterList>.some((callee.ffi!.type().baseType() as! FunctionDeclaration).params)
|
||||
default: Optional<ParameterList>.none
|
||||
}
|
||||
|
||||
|
||||
@@ -312,7 +312,7 @@ extension ReturnStatement: CompilableStatement {
|
||||
|
||||
return switch Expression.Compile(node: expression_node, withContext: context) {
|
||||
case .Ok(let result):
|
||||
if result.type().dataType().eq(rhs: context.expected_type!.dataType()) {
|
||||
if result.type().baseType().eq(rhs: context.expected_type!.baseType()) {
|
||||
.Ok((ReturnStatement(result), context))
|
||||
} else {
|
||||
.Error(
|
||||
|
||||
@@ -68,13 +68,13 @@ extension P4Struct: CompilableType {
|
||||
public struct Types {
|
||||
static func CompileType(
|
||||
type: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Result<P4Type> {
|
||||
) -> Result<P4QualifiedType> {
|
||||
let type_parsers: [CompilableType.Type] = [
|
||||
P4Boolean.self, P4Int.self, P4String.self, P4Struct.self,
|
||||
]
|
||||
for type_parser in type_parsers {
|
||||
switch type_parser.CompileType(type: type, withContext: context) {
|
||||
case .Ok(.some(let type)): return .Ok(P4Type(type))
|
||||
case .Ok(.some(let type)): return .Ok(P4QualifiedType(type))
|
||||
case .Ok(.none): continue
|
||||
case .Error(let e): return .Error(e)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user