1 #include "Pegmatite/ast.hh" 4 #include <unordered_set> 19 using pegmatite::ASTPtr;
20 using pegmatite::ASTChild;
21 using pegmatite::ASTList;
22 using pegmatite::ErrorReporter;
44 virtual void collectVarUses(std::unordered_set<std::string> &decls,
45 std::unordered_set<std::string> &uses) = 0;
112 void collectVarUses(std::unordered_set<std::string> &decls,
113 std::unordered_set<std::string> &uses);
118 ASTList<Statement> statements;
135 pegmatite::ASTStack &st,
const ErrorReporter&)
override 137 pegmatite::constructValue(r, value);
151 return reinterpret_cast<Obj>((value << 3) | 1);
161 std::unordered_set<std::string> &uses)
override 176 pegmatite::ASTStack &st,
const ErrorReporter &er)
override 178 pegmatite::ASTString::construct(r, st, er);
179 std::string::size_type newline;
180 while ((newline = find(
"\\n")) != std::string::npos)
182 replace(newline, 2,
"\n");
203 std::unordered_set<std::string> &uses)
override 236 return lhs->isConstantExpression() && rhs->isConstantExpression();
250 llvm::Value *RHS) = 0;
257 return compileBinOp(c,
258 lhs->compileExpression(c),
259 rhs->compileExpression(c));
265 virtual const char *methodName() = 0;
272 std::unordered_set<std::string> &uses)
override 274 lhs->collectVarUses(decls, uses);
275 rhs->collectVarUses(decls, uses);
281 virtual intptr_t evaluateWithIntegers(intptr_t lhs, intptr_t rhs) = 0;
316 llvm::Value *RHS)
override;
333 llvm::Value *RHS)
override;
350 llvm::Value *RHS)
override;
375 llvm::Value *RHS)
override;
399 llvm::Value *RHS)
override;
408 llvm::Value *RHS)
override;
417 llvm::Value *RHS)
override;
426 llvm::Value *RHS)
override;
435 llvm::Value *RHS)
override;
444 llvm::Value *RHS)
override;
527 static const int compileThreshold = 10;
532 int executionCount = 0;
542 bool checked =
false;
552 std::unordered_set<std::string> boundVars;
556 std::unordered_set<std::string> decls;
570 void collectVarUses(std::unordered_set<std::string> &decls,
571 std::unordered_set<std::string> &uses)
override;
586 std::unordered_set<std::string> &uses)
override 629 std::unordered_set<std::string> &uses)
override 631 target->collectVarUses(decls, uses);
632 expr->collectVarUses(decls, uses);
639 struct ArgList :
public pegmatite::ASTContainer
678 std::unordered_set<std::string> &uses)
override 680 callee->collectVarUses(decls, uses);
681 for (
auto &arg : arguments->arguments)
683 arg->collectVarUses(decls, uses);
712 std::unordered_set<std::string> &uses)
override 739 std::unordered_set<std::string> &uses)
override 741 expr->collectVarUses(decls, uses);
771 std::unordered_set<std::string> &uses)
override 773 condition->collectVarUses(decls, uses);
774 body->collectVarUses(decls, uses);
786 ASTPtr<Expression> condition;
790 ASTPtr<Statements> body;
800 std::unordered_set<std::string> &uses)
override 802 condition->collectVarUses(decls, uses);
803 body->collectVarUses(decls, uses);
848 std::unordered_set<std::string> &uses)
override {}
858 ASTChild<Identifier> className;
872 void collectVarUses(std::unordered_set<std::string> &decls,
873 std::unordered_set<std::string> &uses)
override {}
ASTPtr< Expression > rhs
The right-hand side of the operation.
ASTPtr< ParamList > parameters
The parameter list.
Abstract superclass for binary operators.
Abstract superclass for expressions (statements that evaluate to something).
const char * methodName() override
Add operations become add() method invocations on non-integer objects.
Object *(* CompiledMethod)(Object *, Selector,...)
A compiled method is a function that takes an object (the receiver) and the selector as implicit argu...
ASTPtr< Statements > body
The body of the if statement.
ASTChild< Identifier > superclassName
The superclass name (or class name if there is no superclass).
Object * Obj
Object pointer.
bool construct(const pegmatite::InputRange &r, pegmatite::ASTStack &st, const ErrorReporter &er) override
Construct the string from the source text.
bool isConstantExpression() override
A binary operation is a constant expression if both of its operands are binary expressions.
virtual bool isConstantExpression()
Returns true if the expression is constant and therefore doesn't need interpreting every time...
uint32_t Selector
Selectors are unique identifiers for methods.
ASTPtr< Statements > body
The statements that make up the body of the closure.
Superclass for comparison operations.
A parameter list for a closure declaration.
ASTPtr< Expression > expr
The expression being assigned.
bool isComparison() override
All comparisons are (obviously) comparisons.
Interpreter::Value cache
Cached result for constant expression.
A new expression, which constructs a new instance of a class.
std::unordered_map< std::string, Obj * > SymbolTable
A symbol table stores the address of each allocation.
Assignment statements, setting the value of a variable.
void collectVarUses(std::unordered_set< std::string > &decls, std::unordered_set< std::string > &uses) override
Classes are not allowed to be declared inside closures, so there is never a need to collect their dec...
const char * methodName() override
Divide operations become div() method invocations on non-integer objects.
Argument list for a call expression.
ASTPtr< Identifier, true > method
The name of the method, if this is a method invocation.
Object *(* ClosureInvoke)(Closure *,...)
A compiled closure invoke function.
void collectVarUses(std::unordered_set< std::string > &decls, std::unordered_set< std::string > &uses) override
Collect the uses and declarations in this expression.
bool isConstantExpression() override
Literals are constant expressions.
intptr_t evaluateWithIntegers(intptr_t lhs, intptr_t rhs) override
Evaluate on integer values by subtracting the right side from the left.
void interpret(Interpreter::Context &c) override final
Interpret this expression as if it were a statement.
ASTChild< Identifier > name
The name of the referenced variable.
ASTPtr< Expression > expr
The expression that is returned.
Template superclass for binary operators.
int64_t value
The value of this literal.
bool isConstantExpression() override
All literals are constant expressions.
ASTPtr< Expression, true > init
The initialiser for this variable, if there is one.
const char * methodName() override
Comparisons don't map to any method name.
void collectVarUses(std::unordered_set< std::string > &decls, std::unordered_set< std::string > &uses) override
Collect any variables use in this expression.
Value wraps an object pointer.
ASTPtr< Expression > callee
The callee, if this is calling a closure, or the object that is having a method invoked on it if it i...
void compile(Compiler::Context &c) override final
Compile this expression as if it were a statement.
void collectVarUses(std::unordered_set< std::string > &decls, std::unordered_set< std::string > &uses) override
Add this variable to the set of referenced variables.
ASTPtr< Expression > lhs
The left-hand side of the operation.
void collectVarUses(std::unordered_set< std::string > &decls, std::unordered_set< std::string > &uses) override
Collect any variables that are referenced.
virtual bool isComparison()
Returns whether this operation is a comparison.
void collectVarUses(std::unordered_set< std::string > &decls, std::unordered_set< std::string > &uses) override
Collect all of the variables used and defined in this statement.
const char * methodName() override
Subtract operations become sub() method invocations on non-integer objects.
Any identifier in the source.
void collectVarUses(std::unordered_set< std::string > &decls, std::unordered_set< std::string > &uses) override
Adds this variable to the set that are defined.
const char * methodName() override
Multiply operations become the mul() method invocations on non-integer objects.
ASTPtr< VarRef > target
The variable being assigned to.
ASTList< Decl > ivars
The instance variables declared in this class.
void collectVarUses(std::unordered_set< std::string > &decls, std::unordered_set< std::string > &uses) override
Collect the variables used and declared in the loop.
ASTList< Expression > arguments
The expressions that will be evaluated to give the arguments to the called function.
ASTChild< Identifier > name
The name of the variable.
llvm::Value * compileExpression(Compiler::Context &c) override
Compile the expression by compiling the two sides and then calling compileBinOp (implemented in subcl...
void collectVarUses(std::unordered_set< std::string > &decls, std::unordered_set< std::string > &uses) override
Collect the variables referenced by this call.
Less-than-or-equal-to comparison.
Greater-than-or-equal-to comparison.
bool construct(const pegmatite::InputRange &r, pegmatite::ASTStack &st, const ErrorReporter &) override
Constructs the class from the source range.
The abstract superclass for all statements.
void collectVarUses(std::unordered_set< std::string > &decls, std::unordered_set< std::string > &uses) override
Literals do not define or use any values.
ASTPtr< Expression > condition
The condition.
virtual void compile(Compiler::Context &c)
Compile this statement to LLVM IR.
intptr_t evaluateWithIntegers(intptr_t lhs, intptr_t rhs) override
Evaluate (interpret) this expression, having already determined that the two sides are integer values...
void collectVarUses(std::unordered_set< std::string > &decls, std::unordered_set< std::string > &uses) override
Literals do not define or use any values.
Struct holding metadata about a class.
ASTList< Identifier > arguments
The arguments in this parameter list.
Obj evaluateExpr(Interpreter::Context &c) override
Construct the small integer (integer in a pointer with the low bit set to 1) value corresponding to t...
ASTPtr< ArgList > arguments
The arguments to this call.
Methods in a class's method list.
ASTPtr< Identifier, true > name
The name of the class.
ASTChild< Identifier > name
The name of this closure.
The layout of all closures in MysoreScript.
ASTList< ClosureDecl > methods
The methods declared in this class.