Support Setting Arrays/Fields
Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -27,6 +27,12 @@ protocol CompilableExpression {
|
||||
) -> Result<EvaluatableExpression?>
|
||||
}
|
||||
|
||||
protocol CompilableLValueExpression {
|
||||
static func compile_as_lvalue(
|
||||
node: Node, withContext context: CompilerContext
|
||||
) -> Result<EvaluatableLValueExpression?>
|
||||
}
|
||||
|
||||
extension TypedIdentifier: CompilableExpression {
|
||||
static func compile(
|
||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
@@ -47,6 +53,26 @@ extension TypedIdentifier: CompilableExpression {
|
||||
}
|
||||
}
|
||||
|
||||
extension TypedIdentifier: CompilableLValueExpression {
|
||||
static func compile_as_lvalue(
|
||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Result<EvaluatableLValueExpression?> {
|
||||
|
||||
let expression = node.child(at: 0)!
|
||||
#SkipUnlessNodeType<SwiftTreeSitter.Node, EvaluatableExpression?>(
|
||||
node: expression, type: "identifier")
|
||||
|
||||
let maybe_parsed_expression = TypedIdentifier.compile(node: node, withContext: context)
|
||||
guard case .Ok(let maybe_typed_identifier) = maybe_parsed_expression else {
|
||||
return .Error(maybe_parsed_expression.error()!)
|
||||
}
|
||||
|
||||
let typed_identifier_expression = maybe_typed_identifier as! TypedIdentifier
|
||||
|
||||
return Result.Ok(typed_identifier_expression)
|
||||
}
|
||||
}
|
||||
|
||||
extension P4BooleanValue: CompilableExpression {
|
||||
static func compile(
|
||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
@@ -130,12 +156,25 @@ struct Expression {
|
||||
struct LValue {
|
||||
public static func Compile(
|
||||
node: Node, withContext: CompilerContext
|
||||
) -> Result<Common.Identifier> {
|
||||
return if let node_text_value = node.text {
|
||||
.Ok(Common.Identifier(name: node_text_value))
|
||||
} else {
|
||||
.Error(Error(withMessage: "Could not parse an identifier for an LValue"))
|
||||
) -> Result<EvaluatableLValueExpression> {
|
||||
|
||||
// Try to compile all the expressions that are LValuable!
|
||||
|
||||
let lvalueParsers: [CompilableLValueExpression.Type] = [
|
||||
TypedIdentifier.self, FieldAccessExpression.self, ArrayAccessExpression.self,
|
||||
]
|
||||
|
||||
for lvalue_parser in lvalueParsers {
|
||||
switch lvalue_parser.compile_as_lvalue(
|
||||
node: node, withContext: withContext)
|
||||
{
|
||||
case .Ok(.some(let parsed)): return .Ok(parsed)
|
||||
case .Error(let e): return .Error(e)
|
||||
default: continue
|
||||
}
|
||||
}
|
||||
|
||||
return Result.Error(Error(withMessage: "\(node.range): Could not parse into expression"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -477,3 +516,41 @@ extension FieldAccessExpression: CompilableExpression {
|
||||
withField: P4StructFieldIdentifier(id: field_name, withType: field_type)))
|
||||
}
|
||||
}
|
||||
|
||||
extension FieldAccessExpression: CompilableLValueExpression {
|
||||
static func compile_as_lvalue(
|
||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Result<EvaluatableLValueExpression?> {
|
||||
let expression = node.child(at: 0)!
|
||||
#SkipUnlessNodeType<Node, EvaluatableExpression?>(
|
||||
node: expression, type: "fieldAccessExpression")
|
||||
|
||||
let maybe_parsed_expression = FieldAccessExpression.compile(node: node, withContext: context)
|
||||
guard case .Ok(let maybe_field_access_expression) = maybe_parsed_expression else {
|
||||
return .Error(maybe_parsed_expression.error()!)
|
||||
}
|
||||
|
||||
let field_access_expression = maybe_field_access_expression as! FieldAccessExpression
|
||||
|
||||
return Result.Ok(field_access_expression)
|
||||
}
|
||||
}
|
||||
|
||||
extension ArrayAccessExpression: CompilableLValueExpression {
|
||||
static func compile_as_lvalue(
|
||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Result<EvaluatableLValueExpression?> {
|
||||
let expression = node.child(at: 0)!
|
||||
#SkipUnlessNodeType<Node, EvaluatableExpression?>(
|
||||
node: expression, type: "arrayAccessExpression")
|
||||
|
||||
let maybe_parsed_expression = ArrayAccessExpression.compile(node: node, withContext: context)
|
||||
guard case .Ok(let maybe_array_access_expression) = maybe_parsed_expression else {
|
||||
return .Error(maybe_parsed_expression.error()!)
|
||||
}
|
||||
|
||||
let array_access_expression = maybe_array_access_expression as! ArrayAccessExpression
|
||||
|
||||
return Result.Ok(array_access_expression)
|
||||
}
|
||||
}
|
||||
@@ -22,65 +22,6 @@ import SwiftTreeSitter
|
||||
import TreeSitterExtensions
|
||||
import TreeSitterP4
|
||||
|
||||
extension ParserAssignmentStatement: CompilableStatement {
|
||||
public static func Compile(
|
||||
node: Node, withContext context: CompilerContext
|
||||
) -> Result<(EvaluatableStatement, CompilerContext)> {
|
||||
|
||||
#RequireNodeType<Node, (EvaluatableStatement, CompilerContext)>(
|
||||
node: node, type: "assignmentStatement", nice_type_name: "assignment statement")
|
||||
|
||||
guard let lvalue_node = node.child(at: 0),
|
||||
lvalue_node.nodeType == "expression"
|
||||
else {
|
||||
return Result.Error(
|
||||
ErrorOnNode(node: node, withError: "Missing lvalue in assignment statement"))
|
||||
}
|
||||
|
||||
guard let rvalue_node = node.child(at: 2),
|
||||
rvalue_node.nodeType == "expression"
|
||||
else {
|
||||
return Result.Error(
|
||||
ErrorOnNode(node: node, withError: "Missing rvalue in assignment statement"))
|
||||
}
|
||||
|
||||
let maybe_parsed_rvalue = Expression.Compile(
|
||||
node: rvalue_node, withContext: context)
|
||||
guard case Result.Ok(let rvalue) = maybe_parsed_rvalue else {
|
||||
return Result.Error(maybe_parsed_rvalue.error()!)
|
||||
}
|
||||
|
||||
let maybe_parsed_lvalue = LValue.Compile(node: lvalue_node, withContext: context)
|
||||
guard case .Ok(let lvalue_identifier) = maybe_parsed_lvalue else {
|
||||
return Result.Error(maybe_parsed_lvalue.error()!)
|
||||
}
|
||||
guard case Result.Ok(let lvalue_type) = context.names.lookup(identifier: lvalue_identifier)
|
||||
else {
|
||||
return Result.Error(
|
||||
ErrorOnNode(
|
||||
node: lvalue_node,
|
||||
withError: "Cannot assign to variable \(lvalue_identifier) not in scope"))
|
||||
}
|
||||
|
||||
if rvalue.type().eq(rhs: lvalue_type) {
|
||||
return Result.Ok(
|
||||
(
|
||||
ParserAssignmentStatement(
|
||||
withLValue: TypedIdentifier(name: lvalue_node.text!, withType: lvalue_type),
|
||||
withValue: rvalue
|
||||
), context
|
||||
))
|
||||
|
||||
} else {
|
||||
return Result.Error(
|
||||
ErrorOnNode(
|
||||
node: node,
|
||||
withError:
|
||||
"Cannot assign value of type \(rvalue.type()) to \(lvalue_identifier) (with type \(lvalue_type))"
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public struct Parser {
|
||||
public struct LocalElements {
|
||||
|
||||
@@ -246,3 +246,54 @@ extension ExpressionStatement: CompilableStatement {
|
||||
return Result.Ok((ExpressionStatement(), context))
|
||||
}
|
||||
}
|
||||
|
||||
extension ParserAssignmentStatement: CompilableStatement {
|
||||
public static func Compile(
|
||||
node: Node, withContext context: CompilerContext
|
||||
) -> Result<(EvaluatableStatement, CompilerContext)> {
|
||||
|
||||
#RequireNodeType<Node, (EvaluatableStatement, CompilerContext)>(
|
||||
node: node, type: "assignmentStatement", nice_type_name: "assignment statement")
|
||||
|
||||
guard let lvalue_node = node.child(at: 0),
|
||||
lvalue_node.nodeType == "expression"
|
||||
else {
|
||||
return Result.Error(
|
||||
ErrorOnNode(node: node, withError: "Missing lvalue in assignment statement"))
|
||||
}
|
||||
|
||||
guard let rvalue_node = node.child(at: 2),
|
||||
rvalue_node.nodeType == "expression"
|
||||
else {
|
||||
return Result.Error(
|
||||
ErrorOnNode(node: node, withError: "Missing rvalue in assignment statement"))
|
||||
}
|
||||
|
||||
let maybe_parsed_rvalue = Expression.Compile(
|
||||
node: rvalue_node, withContext: context)
|
||||
guard case Result.Ok(let rvalue) = maybe_parsed_rvalue else {
|
||||
return Result.Error(maybe_parsed_rvalue.error()!)
|
||||
}
|
||||
|
||||
let maybe_parsed_lvalue = LValue.Compile(node: lvalue_node, withContext: context)
|
||||
guard case .Ok(let lvalue_identifier) = maybe_parsed_lvalue else {
|
||||
return Result.Error(maybe_parsed_lvalue.error()!)
|
||||
}
|
||||
|
||||
let check_result = lvalue_identifier.check(to: rvalue, inScopes: context.names)
|
||||
guard case .Ok(_) = check_result else {
|
||||
return Result.Error(
|
||||
ErrorOnNode(
|
||||
node: lvalue_node,
|
||||
withError: "\(check_result.error()!)"))
|
||||
}
|
||||
|
||||
return Result.Ok(
|
||||
(
|
||||
ParserAssignmentStatement(
|
||||
withLValue: lvalue_identifier,
|
||||
withValue: rvalue
|
||||
), context
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user