compiler, runtime, common, testing: Support Directions on Parameters and Attributed Types
Add support for directions on parameters and attributed types. The latter is necessary to support the former, but is not limited to the direction use case. An attributed type is a P4 type with attributes (like its direction, whether it is read only, etc.) Other attributes will be added in the future. Changes necessary to support attributed types include: 1. Global instances tracked during compilation are not attributed types and not simply types. 2. (future) Type checking will have to make sure that a types attributes do not affect type compatibility. Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -40,7 +40,7 @@ import TreeSitterP4
|
||||
};
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4TypeAttributed.Attributeless(P4Array(withValueType: P4Int())))
|
||||
var test_values = VarValueScopes().enter()
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
@@ -68,7 +68,7 @@ import TreeSitterP4
|
||||
};
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Int())
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4TypeAttributed.Attributeless(P4Int()))
|
||||
#expect(
|
||||
#RequireErrorResult(
|
||||
Error(
|
||||
@@ -91,7 +91,7 @@ import TreeSitterP4
|
||||
};
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4TypeAttributed.Attributeless(P4Array(withValueType: P4Int())))
|
||||
var test_values = VarValueScopes().enter()
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
@@ -118,7 +118,7 @@ import TreeSitterP4
|
||||
};
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4TypeAttributed.Attributeless(P4Array(withValueType: P4Int())))
|
||||
var test_values = VarValueScopes().enter()
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
@@ -145,7 +145,7 @@ import TreeSitterP4
|
||||
};
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4TypeAttributed.Attributeless(P4Array(withValueType: P4Int())))
|
||||
var test_values = VarValueScopes().enter()
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
@@ -173,7 +173,7 @@ import TreeSitterP4
|
||||
};
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Array(withValueType: P4Int())))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4TypeAttributed.Attributeless(P4Array(withValueType: P4Array(withValueType: P4Int()))))
|
||||
var test_values = VarValueScopes().enter()
|
||||
|
||||
let nested = P4ArrayValue(
|
||||
@@ -206,7 +206,7 @@ import TreeSitterP4
|
||||
};
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4TypeAttributed.Attributeless(P4Array(withValueType: P4Int())))
|
||||
var test_values = VarValueScopes().enter()
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
@@ -235,7 +235,7 @@ import TreeSitterP4
|
||||
};
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Array(withValueType: P4Int())))
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4TypeAttributed.Attributeless(P4Array(withValueType: P4Array(withValueType: P4Int()))))
|
||||
var test_values = VarValueScopes().enter()
|
||||
|
||||
let nested = P4ArrayValue(
|
||||
|
||||
@@ -39,8 +39,8 @@ import P4Lang
|
||||
}
|
||||
};
|
||||
"""
|
||||
let x = { (tipe: P4Type) -> Bool in
|
||||
switch tipe {
|
||||
let x = { (tipe: P4TypeAttributed) -> Bool in
|
||||
switch tipe.type {
|
||||
case let c as Control: c.name == "simple"
|
||||
default: false
|
||||
}
|
||||
@@ -73,8 +73,8 @@ import P4Lang
|
||||
};
|
||||
"""
|
||||
|
||||
let filter = { (tipe: P4Type) -> Bool in
|
||||
switch tipe {
|
||||
let filter = { (tipe: P4TypeAttributed) -> Bool in
|
||||
switch tipe.type {
|
||||
case let c as Control: c.name == "simple" || c.name == "complex"
|
||||
default: false
|
||||
}
|
||||
|
||||
@@ -166,3 +166,12 @@ import TreeSitterP4
|
||||
#expect(#RequireOkResult(Program.Compile(simple_parser_declaration)))
|
||||
}
|
||||
|
||||
@Test func test_function_declaration_with_parameters_and_direction() async throws {
|
||||
let simple_parser_declaration = """
|
||||
bool functionb(in bool x, out string y, inout int z) {
|
||||
x = true;
|
||||
};
|
||||
"""
|
||||
#expect(#RequireOkResult(Program.Compile(simple_parser_declaration)))
|
||||
}
|
||||
|
||||
|
||||
@@ -28,39 +28,39 @@ import TreeSitterP4
|
||||
|
||||
@Test func test_scope() async throws {
|
||||
let s = VarTypeScope()
|
||||
let s2 = s.declare(identifier: Identifier(name: "first"), withValue: P4Int())
|
||||
let s2 = s.declare(identifier: Identifier(name: "first"), withValue: P4TypeAttributed.Attributeless(P4Int()))
|
||||
let found_first = try! #require(s2.lookup(identifier: Identifier(name: "first")))
|
||||
|
||||
#expect(found_first.eq(rhs: P4Int()))
|
||||
#expect(found_first.type.eq(rhs: P4Int()))
|
||||
#expect(s2.count == 1)
|
||||
}
|
||||
|
||||
@Test func test_scope_no_set() async throws {
|
||||
var ss = VarTypeScopes().enter()
|
||||
ss = ss.declare(identifier: Identifier(name: "first"), withValue: P4Int())
|
||||
ss = ss.declare(identifier: Identifier(name: "first"), withValue: P4TypeAttributed.Attributeless(P4Int()))
|
||||
ss = ss.enter()
|
||||
ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4Boolean())
|
||||
ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4TypeAttributed.Attributeless(P4Boolean()))
|
||||
|
||||
let found_first = try! #UseOkResult(ss.lookup(identifier: Identifier(name: "first")))
|
||||
let found_second = try! #UseOkResult(ss.lookup(identifier: Identifier(name: "second")))
|
||||
|
||||
#expect(found_first.eq(rhs: P4Int()))
|
||||
#expect(found_second.eq(rhs: P4Boolean()))
|
||||
#expect(found_first.type.eq(rhs: P4Int()))
|
||||
#expect(found_second.type.eq(rhs: P4Boolean()))
|
||||
}
|
||||
|
||||
@Test func test_scope_set() async throws {
|
||||
var ss = VarTypeScopes().enter()
|
||||
let id = Identifier(name: "first")
|
||||
let id_type = P4Int()
|
||||
let id_type = P4TypeAttributed.Attributeless(P4Int())
|
||||
|
||||
ss = ss.declare(identifier: id, withValue: id_type)
|
||||
ss = ss.enter()
|
||||
ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4Boolean())
|
||||
ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4TypeAttributed.Attributeless(P4Boolean()))
|
||||
// Change the value of `first`.
|
||||
ss = ss.set(identifier: id, withValue: P4String())
|
||||
ss = ss.set(identifier: id, withValue: P4TypeAttributed.Attributeless(P4String()))
|
||||
|
||||
// Verify the change!
|
||||
let found = try! #UseOkResult(ss.lookup(identifier: id))
|
||||
|
||||
#expect(found.eq(rhs: P4String()))
|
||||
#expect(found.type.eq(rhs: P4String()))
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ import TreeSitterP4
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||
])
|
||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4TypeAttributed.Attributeless(struct_type))
|
||||
|
||||
var test_values = VarValueScopes().enter()
|
||||
test_values = test_values.declare(
|
||||
@@ -139,7 +139,7 @@ import TreeSitterP4
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||
])
|
||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4TypeAttributed.Attributeless(struct_type))
|
||||
|
||||
var test_values = VarValueScopes().enter()
|
||||
test_values = test_values.declare(
|
||||
@@ -174,7 +174,7 @@ import TreeSitterP4
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||
])
|
||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4TypeAttributed.Attributeless(struct_type))
|
||||
|
||||
var test_values = VarValueScopes().enter()
|
||||
test_values = test_values.declare(
|
||||
@@ -208,7 +208,7 @@ import TreeSitterP4
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||
])
|
||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4TypeAttributed.Attributeless(struct_type))
|
||||
|
||||
var test_values = VarValueScopes().enter()
|
||||
test_values = test_values.declare(
|
||||
@@ -248,7 +248,7 @@ import TreeSitterP4
|
||||
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: ty_struct_type)])
|
||||
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
|
||||
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: ts_struct_type)
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4TypeAttributed.Attributeless(ts_struct_type))
|
||||
|
||||
var test_values = VarValueScopes().enter()
|
||||
|
||||
@@ -290,7 +290,7 @@ import TreeSitterP4
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||
])
|
||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4TypeAttributed.Attributeless(struct_type))
|
||||
|
||||
var test_values = VarValueScopes().enter()
|
||||
test_values = test_values.declare(
|
||||
@@ -322,7 +322,7 @@ import TreeSitterP4
|
||||
P4StructFieldIdentifier(name: "count", withType: P4Int()),
|
||||
])
|
||||
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4TypeAttributed.Attributeless(struct_type))
|
||||
|
||||
#expect(
|
||||
#RequireErrorResult(
|
||||
@@ -357,7 +357,7 @@ import TreeSitterP4
|
||||
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: ty_struct_type)])
|
||||
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
|
||||
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: ts_struct_type)
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4TypeAttributed.Attributeless(ts_struct_type))
|
||||
|
||||
var test_values = VarValueScopes().enter()
|
||||
|
||||
@@ -405,7 +405,7 @@ import TreeSitterP4
|
||||
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: ty_struct_type)])
|
||||
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
|
||||
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: ts_struct_type)
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4TypeAttributed.Attributeless(ts_struct_type))
|
||||
|
||||
var test_values = VarValueScopes().enter()
|
||||
|
||||
@@ -452,7 +452,7 @@ import TreeSitterP4
|
||||
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: ty_struct_type)])
|
||||
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
|
||||
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: ts_struct_type)
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4TypeAttributed.Attributeless(ts_struct_type))
|
||||
|
||||
#expect(
|
||||
#RequireErrorResult(
|
||||
|
||||
@@ -253,7 +253,7 @@ import TreeSitterP4
|
||||
};
|
||||
"""
|
||||
var test_types = VarTypeScopes().enter()
|
||||
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
|
||||
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4TypeAttributed.Attributeless(P4Array(withValueType: P4Int())))
|
||||
#expect(
|
||||
#RequireErrorResult(
|
||||
Error(
|
||||
|
||||
Reference in New Issue
Block a user