6dba17c97a
Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
207 lines
6.2 KiB
Swift
207 lines
6.2 KiB
Swift
// p4rse, Copyright 2026, Will Hawkins
|
|
//
|
|
// This file is part of p4rse.
|
|
//
|
|
// This file is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
import Common
|
|
import P4Lang
|
|
import SwiftTreeSitter
|
|
import TreeSitterP4
|
|
|
|
protocol CompilableExpression {
|
|
static func compile(
|
|
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
|
|
) -> Result<EvaluatableExpression?>
|
|
}
|
|
|
|
extension TypedIdentifier: CompilableExpression {
|
|
static func compile(
|
|
node: SwiftTreeSitter.Node, inTree tree: SwiftTreeSitter.MutableTree,
|
|
withScopes scopes: LexicalScopes
|
|
) -> Result<EvaluatableExpression?> {
|
|
|
|
guard
|
|
let parser_statement_query = try? SwiftTreeSitter.Query(
|
|
language: p4lang,
|
|
data: String(
|
|
"(expression (identifier) @identifier)"
|
|
).data(using: String.Encoding.utf8)!)
|
|
else {
|
|
return Result.Error(Error(withMessage: "Could not compile the tree sitter query"))
|
|
}
|
|
|
|
let qr = parser_statement_query.execute(node: node, in: tree)
|
|
|
|
guard let result = qr.next() else {
|
|
return .Ok(.none)
|
|
}
|
|
|
|
let value_capture = result.captures(named: "identifier")
|
|
guard
|
|
case Result.Ok(let type) = scopes.lookup(
|
|
identifier: Common.Identifier(name: value_capture[0].node.text!))
|
|
else {
|
|
return .Error(Error(withMessage: "Cannot find \(result.captures[0].node.text!) in scope"))
|
|
}
|
|
|
|
return .Ok(TypedIdentifier(name: result.captures[0].node.text!, withType: type))
|
|
}
|
|
}
|
|
|
|
extension P4BooleanValue: CompilableExpression {
|
|
static func compile(
|
|
node: SwiftTreeSitter.Node, inTree tree: SwiftTreeSitter.MutableTree,
|
|
withScopes scopes: LexicalScopes
|
|
) -> Result<EvaluatableExpression?> {
|
|
|
|
guard
|
|
let true_query = try? SwiftTreeSitter.Query(
|
|
language: p4lang,
|
|
data: String(
|
|
"(expression (true))"
|
|
).data(using: String.Encoding.utf8)!)
|
|
else {
|
|
return Result.Error(Error(withMessage: "Could not compile the tree sitter query"))
|
|
}
|
|
|
|
let true_qr = true_query.execute(node: node, in: tree)
|
|
|
|
if true_qr.next() != nil {
|
|
return .Ok(P4BooleanValue(withValue: true))
|
|
}
|
|
|
|
guard
|
|
let false_query = try? SwiftTreeSitter.Query(
|
|
language: p4lang,
|
|
data: String(
|
|
"(expression (false))"
|
|
).data(using: String.Encoding.utf8)!)
|
|
else {
|
|
return Result.Error(Error(withMessage: "Could not compile the tree sitter query"))
|
|
}
|
|
|
|
let false_qr = false_query.execute(node: node, in: tree)
|
|
|
|
if false_qr.next() != nil {
|
|
return .Ok(P4BooleanValue(withValue: false))
|
|
}
|
|
return .Ok(.none)
|
|
}
|
|
}
|
|
|
|
extension P4IntValue: CompilableExpression {
|
|
static func compile(
|
|
node: SwiftTreeSitter.Node, inTree tree: SwiftTreeSitter.MutableTree,
|
|
withScopes scopes: LexicalScopes
|
|
) -> Result<EvaluatableExpression?> {
|
|
|
|
guard
|
|
let integer_query = try? SwiftTreeSitter.Query(
|
|
language: p4lang,
|
|
data: String(
|
|
"(expression (integer) @value)"
|
|
).data(using: String.Encoding.utf8)!)
|
|
else {
|
|
return Result.Error(Error(withMessage: "Could not compile the tree sitter query"))
|
|
}
|
|
|
|
let integer_qr = integer_query.execute(node: node, in: tree)
|
|
|
|
guard let result = integer_qr.next() else {
|
|
return .Ok(.none)
|
|
}
|
|
|
|
let value_capture = result.captures(named: "value")
|
|
if let parsed_int = Int(value_capture[0].node.text!) {
|
|
return .Ok(P4IntValue(withValue: parsed_int))
|
|
} else {
|
|
print("HERE!!")
|
|
return .Error(Error(withMessage: "Failed to parse integer: \(result.captures[0].node.text!)"))
|
|
}
|
|
}
|
|
}
|
|
|
|
extension P4StringValue: CompilableExpression {
|
|
static func compile(
|
|
node: SwiftTreeSitter.Node, inTree tree: SwiftTreeSitter.MutableTree,
|
|
withScopes scopes: LexicalScopes
|
|
) -> Result<EvaluatableExpression?> {
|
|
|
|
guard
|
|
let string_query = try? SwiftTreeSitter.Query(
|
|
language: p4lang,
|
|
data: String(
|
|
"(expression (string_literal) @value)"
|
|
).data(using: String.Encoding.utf8)!)
|
|
else {
|
|
return Result.Error(Error(withMessage: "Could not compile the tree sitter query"))
|
|
}
|
|
|
|
let string_qr = string_query.execute(node: node, in: tree)
|
|
|
|
guard let result = string_qr.next() else {
|
|
return .Ok(.none)
|
|
}
|
|
|
|
return .Ok(P4StringValue(withValue: result.captures[0].node.text!))
|
|
}
|
|
}
|
|
|
|
struct Expression {
|
|
public static func Compile(
|
|
node: Node, inTree: MutableTree, withScopes scopes: LexicalScopes
|
|
) -> Result<EvaluatableExpression> {
|
|
let localElementsParsers: [CompilableExpression.Type] = [
|
|
P4BooleanValue.self, P4StringValue.self, P4IntValue.self, TypedIdentifier.self,
|
|
]
|
|
|
|
for le_parser in localElementsParsers {
|
|
switch le_parser.compile(
|
|
node: node, inTree: inTree, withScopes: scopes)
|
|
{
|
|
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"))
|
|
}
|
|
}
|
|
|
|
struct LValue {
|
|
public static func Compile(
|
|
node: Node, inTree: MutableTree, withScopes scopes: LexicalScopes
|
|
) -> 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"))
|
|
}
|
|
}
|
|
}
|
|
|
|
struct Identifier {
|
|
public static func Compile(
|
|
node: Node, inTree: MutableTree, withScopes scopes: LexicalScopes
|
|
) -> 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 from \(node)"))
|
|
}
|
|
}
|
|
}
|