WIP formulas and expressions

Joint work with shati-patel.
This commit is contained in:
Taus
2021-05-26 16:21:01 +00:00
committed by GitHub
parent 28968163e0
commit 94c1321e43
2 changed files with 86 additions and 1 deletions

View File

@@ -171,3 +171,68 @@ class Body extends TBody, AstNode {
override string getAPrimaryQlClass() { result = "Body" }
// TODO: Children.
}
/** A formula, such as `x = 6 and y < 5`. */
abstract class Formula extends AstNode { }
/** An `and` formula, with 2 or more operands. */
class Conjunction extends TConjunction, AstNode, Formula {
Generated::Conjunction conj;
Conjunction() { this = TConjunction(conj) }
override string getAPrimaryQlClass() { result = "Conjunction" }
/** Gets an operand to this formula. */
Formula getAnOperand() { toGenerated(result) in [conj.getLeft(), conj.getRight()] }
}
/** An `or` formula, with 2 or more operands. */
class Disjunction extends TDisjunction, AstNode {
Generated::Disjunction disj;
Disjunction() { this = TDisjunction(disj) }
override string getAPrimaryQlClass() { result = "Disjunction" }
/** Gets an operand to this formula. */
Formula getAnOperand() { toGenerated(result) in [disj.getLeft(), disj.getRight()] }
}
class ComparisonOp extends TComparisonOp, AstNode {
Generated::Compop op;
ComparisonOp() {this = TComparisonOp(op)}
}
class ComparisonFormula extends TComparisonFormula, Formula {
Expr getLeftOperand() {none()}
Expr getRightOperand() {none()}
Expr getAnOperand() {none()}
ComparisonOp getOperator() {none()}
//ComparisonSymbol getSymbol() {none()}
}
/** An expression, such as `x+4`. */
abstract class Expr extends AstNode {}
/** A function symbol, such as `+` or `*`. */
class FunctionSymbol extends string {
FunctionSymbol() { this = "+" or this = "-" or this = "*" or this = "/" or this = "%" }
}
/** A binary operation, such as `x+3` or `y/2` */
abstract class BinOpExpr extends Expr {}
class AddExpr extends TAddExpr, BinOpExpr {
Generated::AddExpr addexpr;
AddExpr() {this = TAddExpr(addexpr)}
Expr getLeftOperand() { toGenerated(result) = addexpr.getLeft() }
Expr getRightOperand() { toGenerated(result) = addexpr.getRight() }
Expr getAnOperand() { result = getLeftOperand() or result = getRightOperand() }
FunctionSymbol getOperator() { result = addexpr.getChild().getValue() }
}

View File

@@ -10,12 +10,32 @@ newtype TAstNode =
TBody(Generated::Body body) or
TClass(Generated::Dataclass dc) or
TCharPred(Generated::Charpred pred) or
TClassPredicate(Generated::MemberPredicate pred)
TClassPredicate(Generated::MemberPredicate pred) or
TDisjunction(Generated::Disjunction disj) or
TConjunction(Generated::Conjunction conj) or
TComparisonFormula(Generated::CompTerm comp) or
TComparisonOp(Generated::Compop op) or
TAddExpr(Generated::AddExpr addexp)
class TBinOpExpr = TAddExpr; // Can this work?
Generated::AstNode toGeneratedFormula(AST::AstNode n) {
n = TConjunction(result) or
n = TDisjunction(result) or
n = TComparisonFormula(result)
}
Generated::AstNode toGeneratedExpr(AST::AstNode n) { n = TAddExpr(result) }
/**
* Gets the underlying TreeSitter entity for a given AST node.
*/
Generated::AstNode toGenerated(AST::AstNode n) {
result = toGeneratedExpr(n)
or
result = toGeneratedFormula(n)
or
n = TClasslessPredicate(_, result)
or
n = TVarDecl(result)