1 #ifndef FUNC_SYNTAX_HPP 2 #define FUNC_SYNTAX_HPP 43 std::map<std::string, Expr*> variables;
47 std::vector<Expr*>
ctx;
50 :
GCObj(gc), lexer{lexer}, parent{parent}, variables() {}
55 bool contains(
const std::string &name)
const noexcept;
59 const Expr *
get(
const std::string &name)
const noexcept;
66 virtual void mark(
GCMain &gc) noexcept
override;
68 std::map<std::string, Expr*> &getVariables() noexcept
76 std::vector<Expr*>::iterator find(std::vector<Expr*> &vec,
Expr *expr) noexcept;
90 : env{env}, expr{expr} {
92 if (this->expr) env.
ctx.push_back(dynamic_cast<Expr*>(expr));
97 auto it = find(env.
ctx, dynamic_cast<Expr*>(this->expr));
98 if (it != env.
ctx.end()) env.
ctx.erase(it);
104 T* operator ->()
const noexcept {
118 return this->expr == expr;
125 return this->expr == *obj;
132 return this->expr != expr;
139 return this->expr != *obj;
147 if (expr) env.
ctx.push_back(dynamic_cast<Expr*>(expr));
150 auto it = find(env.
ctx, dynamic_cast<Expr*>(this->expr));
151 if (it != env.
ctx.end()) env.
ctx.erase(it);
154 this->expr = expr;
return *
this;
169 operator bool()
const {
170 return expr !=
nullptr;
184 Expr *lastEval =
nullptr;
187 :
GCObj(gc), type{type}, pos(pos) {}
193 std::size_t getDepth()
const noexcept {
return depth; }
207 virtual std::string
toString() const noexcept {
return std::string(); }
240 return const_cast<Expr*>(
this);
249 virtual bool equals(
const Expr *expr,
bool exact =
false) const noexcept
250 {
return this == expr; }
255 return std::vector<std::string>();
258 virtual void mark(
GCMain &gc) noexcept
override;
274 op(op), lhs{lhs}, rhs{rhs} {
275 depth = 1 + lhs->getDepth() + rhs->getDepth();
280 op(op), lhs{lhs}, rhs{rhs} {
281 depth = 1 + lhs->getDepth() + rhs->getDepth();
295 virtual std::string
toString() const noexcept
override {
300 +
" " + std::to_string(op) +
" " + rhs->
toString() +
")";
309 if (lastEval) lastEval->
mark(gc);
317 virtual bool equals(
const Expr *expr,
bool exact =
false) const noexcept override;
325 bool isFunctionConstructor() const noexcept;
335 result.push_back(
id);
355 depth = 1 + expr->getDepth();
358 Operator getOperator()
const noexcept {
return op; }
359 const Expr &getExpression()
const noexcept {
return *expr; }
361 virtual void mark(
GCMain &gc) noexcept
override;
364 virtual bool equals(
const Expr *expr,
bool exact =
false)
const noexcept
override;
371 const Expr *thisExpr,
372 const Expr *lhs,
const Expr *rhs) noexcept;
386 double getNumber()
const noexcept {
return num; }
388 virtual std::string
toString() const noexcept
override {
389 return std::to_string(num);
392 virtual bool equals(
const Expr *expr,
bool exact =
false) const noexcept override;
407 std::int64_t getNumber()
const noexcept {
return num; }
409 virtual std::string
toString() const noexcept
override {
410 return std::to_string(num);
413 virtual bool equals(
const Expr *expr,
bool exact =
false) const noexcept override;
429 const std::string &getName()
const noexcept {
return id; }
431 virtual std::string
toString() const noexcept
override {
438 virtual bool equals(
const Expr *expr,
bool exact =
false) const noexcept override;
441 std::vector<std::string> result;
442 result.push_back(
id);
456 name(name), expr(std::move(expr)) {
457 depth = 1 + expr->getDepth();
462 const std::string &getName()
const noexcept {
return name; }
463 const Expr& getExpression()
const noexcept {
return *expr; }
465 virtual std::string
toString() const noexcept
override {
466 return "\\" + name +
" = " 476 if (lastEval) lastEval->mark(gc);
487 virtual bool equals(
const Expr *expr,
bool exact =
false) const noexcept override;
491 result.push_back(name);
511 const std::string &getName()
const noexcept {
return id; }
513 virtual std::string
toString() const noexcept
override {
517 virtual bool equals(
const Expr *expr,
bool exact =
false) const noexcept override;
525 Expr *condition, *exprTrue, *exprFalse;
529 condition{condition}, exprTrue{exprTrue}, exprFalse{exprFalse} {
530 depth = 1 + condition->getDepth()
531 + exprTrue->getDepth() + exprFalse->getDepth();
549 virtual std::string
toString() const noexcept
override {
550 return "if " + condition->
toString() +
" then " 560 if (lastEval) lastEval->mark(gc);
569 virtual bool equals(
const Expr *expr,
bool exact =
false) const noexcept override;
574 result.push_back(
id);
576 result.push_back(
id);
597 virtual bool equals(
const Expr *expr,
bool exact =
false)
const noexcept
override;
599 virtual std::string
toString() const noexcept
override {
600 return std::string(
"_");
607 std::vector<BiOpExpr*> assignments;
617 assignments(assignments), body{body} {
618 depth = 1 + body->getDepth();
619 for (
BiOpExpr *expr : assignments) depth += expr->getDepth();
627 {
return assignments; }
638 if(lastEval) lastEval->mark(gc);
652 result.push_back(
id);
657 virtual bool equals(
const Expr *expr,
bool exact =
false) const noexcept override;
659 virtual std::
string toString() const noexcept
override {
660 std::string result =
"let ";
661 auto &asgs =
const_cast<std::vector<BiOpExpr*>&
>(assignments);
662 for (
auto it = asgs.begin(); it != assignments.end(); ++it) {
663 if (it != assignments.begin())
666 result += (*it)->toString();
669 return result +
" in " + body->
toString();
680 std::vector<std::pair<std::vector<Expr*>,
Expr*>> fncases;
683 const std::string &name,
684 std::pair<std::vector<Expr*>,
Expr*> fncase) noexcept;
688 void calcDepth() noexcept;
696 bool addCase(std::pair<std::vector<Expr*>,
Expr*> fncase) noexcept;
700 const std::string &
getName() const noexcept {
return name; }
702 std::vector<std::pair<std::vector<Expr*>,
Expr*>> &getFunctionCases()
703 noexcept {
return fncases; }
704 const std::vector<std::pair<std::vector<Expr*>,
Expr*>> &getFunctionCases()
705 const noexcept {
return fncases; }
707 virtual void mark(
GCMain &gc) noexcept
override;
711 virtual std::string
toString() const noexcept
override {
716 Expr *reportSyntaxError(
Lexer &lexer,
const std::string &msg,
LetExpr(GCMain &gc, const TokenPos &pos, const std::vector< BiOpExpr * > &assignments, Expr *body)
Initialize let expression.
Definition: syntax.hpp:615
virtual Expr * eval(GCMain &gc, Environment &env) noexcept override
Evaluates expression one time.
const Expr & getLHS() const noexcept
Definition: syntax.hpp:293
const Expr * get(const std::string &name) const noexcept
virtual std::string toString() const noexcept override
Definition: syntax.hpp:711
const std::string & getName() const noexcept
Definition: syntax.hpp:700
T * operator *() const noexcept
Definition: syntax.hpp:110
virtual void mark(GCMain &gc) noexcept override
If not marked mark itself and children.
virtual Expr * optimize(GCMain &gc) noexcept override
void breadthEval(GCMain &gc, Environment &env, StackFrameObj< Expr > &lhs, StackFrameObj< Expr > &rhs) noexcept
Executes eval function for given expressions as long as differenct expr returned. Breadth first execu...
virtual std::string toString() const noexcept override
Definition: syntax.hpp:599
virtual Expr * replace(GCMain &gc, const std::string &name, Expr *expr) const noexcept
Replace all identifiers equal to name with expr.
Definition: syntax.hpp:239
GCObj(GCMain &main) noexcept
Initialize as not marked.
Lexical Analysis/Tokenizer.
bool isAtomConstructor() const noexcept
Lambda function expression.
Definition: syntax.hpp:449
Integer number.
Definition: syntax.hpp:30
virtual Expr * eval(GCMain &gc, Environment &env) noexcept override
Evaluates expression one time.
virtual bool equals(const Expr *expr, bool exact=false) const noexcept override
Checks if this expression and expr have the same structure, Every expression has the same structure a...
virtual std::string toString() const noexcept override
Definition: syntax.hpp:295
const Expr & getTrue() const noexcept
Definition: syntax.hpp:544
Operator getOperator() const noexcept
Definition: syntax.hpp:287
Represents a named function.
Definition: syntax.hpp:678
const TokenPos & getTokenPos() const noexcept
Definition: syntax.hpp:202
Floating point number expression.
Definition: syntax.hpp:376
virtual Expr * eval(GCMain &gc, Environment &env) noexcept
Evaluates expression one time.
Definition: syntax.hpp:219
const Expr & getRHS() const noexcept
Definition: syntax.hpp:290
Implements a tracing garbage collector.
Definition: gc.hpp:35
virtual void mark(GCMain &gc) noexcept override
Mark self and expr.
Definition: syntax.hpp:471
bool operator !=(T *expr) const noexcept
Definition: syntax.hpp:131
Atom expression.
Definition: syntax.hpp:501
Lambda function.
Definition: syntax.hpp:32
Environment * getParent() const noexcept
Definition: syntax.hpp:73
ExprType getExpressionType() const noexcept
Definition: syntax.hpp:212
virtual void mark(GCMain &gc) noexcept
If not marked mark itself and children.
Definition: syntax.hpp:633
virtual std::string toString() const noexcept override
Definition: syntax.hpp:431
Not a real operator (functional substition)
Definition: lexer.hpp:63
Any '_'.
Definition: syntax.hpp:35
Let expression.
Definition: syntax.hpp:606
virtual Expr * replace(GCMain &gc, const std::string &name, Expr *expr) const noexcept override
Replace all identifiers equal to name with expr.
ExprType
Types of expressions.
Definition: syntax.hpp:26
virtual std::string toString() const noexcept
Definition: syntax.hpp:207
Class for adding Expr's to stack frame of Environment.
Definition: syntax.hpp:85
virtual void mark(GCMain &gc) noexcept override
Mark self, rhs and lhs.
Definition: syntax.hpp:304
Operator
Operator type.
Definition: lexer.hpp:45
virtual void mark(GCMain &gc) noexcept override
If not marked mark itself and children.
bool operator==(T *expr) const noexcept
Definition: syntax.hpp:117
Expr * evalWithLookup(GCMain &gc, Environment &env) noexcept
std::vector< Expr * > ctx
Context to store e.g. stack variables.
Definition: syntax.hpp:47
bool contains(const std::string &name) const noexcept
const std::vector< BiOpExpr * > & getAssignments() const noexcept
Definition: syntax.hpp:626
const Expr & getFalse() const noexcept
Evaluated if condition evaluated to the atom .true.
Definition: syntax.hpp:547
any expression
Definition: syntax.hpp:589
Binary operator.
Definition: syntax.hpp:27
virtual std::vector< std::string > getIdentifiers() const noexcept override
Definition: syntax.hpp:648
Binary operator expression.
Definition: syntax.hpp:268
Floating-point Number.
Definition: syntax.hpp:29
virtual std::string toString() const noexcept override
Definition: syntax.hpp:465
virtual std::string toString() const noexcept override
Definition: syntax.hpp:549
Atom.
Definition: syntax.hpp:33
Tracing garbage collection.
bool operator !=(StackFrameObj< T > &obj) const noexcept
Definition: syntax.hpp:138
const Expr & getCondition() const noexcept
Condition of the if-then-else expression.
Definition: syntax.hpp:540
Unary left-operator.
Definition: syntax.hpp:28
Let statement.
Definition: syntax.hpp:36
virtual void markSelf(GCMain &main)
Marks itself.
StackFrameObj< T > & operator=(StackFrameObj< T > &obj) noexcept
Assigns **this = *obj. Removes old expression from stack frame and adds *obj to stack frame.
Definition: syntax.hpp:162
Garbage collection object (objects to collect).
Definition: gc.hpp:15
If-then-else expression.
Definition: syntax.hpp:524
virtual bool isMarked(GCMain &main) const noexcept
File for managing external headers.
StackFrameObj< T > & operator=(T *expr) noexcept
Definition: syntax.hpp:146
Identifier.
Definition: syntax.hpp:31
virtual std::string toString() const noexcept override
Definition: syntax.hpp:388
Definition: syntax.hpp:348
virtual bool equals(const Expr *expr, bool exact=false) const noexcept
Checks if this expression and expr have the same structure, Every expression has the same structure a...
Definition: syntax.hpp:249
Main expression handle (should only be used as parent class).
Definition: syntax.hpp:179
virtual std::vector< std::string > getIdentifiers() const noexcept
Definition: syntax.hpp:254
virtual bool equals(const Expr *expr, bool exact=false) const noexcept override
Checks if this expression and expr have the same structure, Every expression has the same structure a...
const AtomExpr * getAtomConstructor() const noexcept
virtual std::string toString() const noexcept override
Definition: syntax.hpp:409
Environment for accessing variables.
Definition: syntax.hpp:42
virtual Expr * optimize(GCMain &gc) noexcept
bool hasLastEval() const noexcept
Definition: syntax.hpp:198
virtual std::vector< std::string > getIdentifiers() const noexcept override
Definition: syntax.hpp:332
const Expr * currentGet(const std::string &name) const noexcept
Integer number expression.
Definition: syntax.hpp:397
const Expr & getBody() const noexcept
Definition: syntax.hpp:631
Intern statement for named functions.
Definition: syntax.hpp:37
virtual Expr * optimize(GCMain &gc) noexcept override
Identifier expression.
Definition: syntax.hpp:419
virtual bool equals(const Expr *expr, bool exact=false) const noexcept override
Checks if this expression and expr have the same structure, Every expression has the same structure a...
virtual std::string toString() const noexcept override
Definition: syntax.hpp:513
virtual void mark(GCMain &gc) noexcept override
If not marked mark itself and children.
If-then-else.
Definition: syntax.hpp:34
virtual void mark(GCMain &gc) noexcept override
Mark self, condition, exprTrue and exprFalse.
Definition: syntax.hpp:555