Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * parsetree.h
- */
- #ifndef PARSETREE_H_
- #define PARSETREE_H_
- #include "value.h"
- #include <vector>
- #include <map>
- #include <string>
- using std::vector;
- using std::map;
- // NodeType represents all possible types
- enum NodeType { ERRTYPE, INTTYPE, STRTYPE, BOOLTYPE, IDENTTYPE};
- // a "forward declaration" for a class to hold values
- class Value;
- class ParseTree {
- public:
- int linenum;
- ParseTree *left;
- ParseTree *right;
- public:
- ParseTree(int linenum, ParseTree *l = 0, ParseTree *r = 0)
- : linenum(linenum), left(l), right(r) {}
- virtual ~ParseTree() {
- delete left;
- delete right;
- }
- int GetLinenum() const { return linenum; }
- virtual string GetID() const { return "NO WORKING"; }
- virtual NodeType GetType() const { return ERRTYPE; }
- int LeafCount() const {
- int lc = 0;
- if( left ) lc += left->LeafCount();
- if( right ) lc += right->LeafCount();
- if( left == 0 && right == 0 )
- lc++;
- return lc;
- }
- virtual bool IsIdent() const { return false; }
- virtual bool IsString() const { return false; }
- virtual string GetId() const { return ""; }
- virtual Value Eval(map<string, Value> *symbolMap) {
- return Value();
- }
- int IdentCount() const {
- int cnt = 0;
- if( left ) cnt += left->IdentCount();
- if( right ) cnt += right->IdentCount();
- if( IsIdent() )
- cnt++;
- return cnt;
- }
- int StringCount() const {
- int cnt = 0;
- if( left ) cnt += left->StringCount();
- if( right ) cnt += right->StringCount();
- if( IsString() )
- cnt++;
- return cnt;
- }
- void GetVars(map<string,int>& var) {
- if( left ) left->GetVars(var);
- if( right ) right->GetVars(var);
- if( IsIdent() )
- var[ this->GetId() ]++;
- }
- };
- class StmtList : public ParseTree {
- public:
- StmtList(ParseTree *l, ParseTree *r) : ParseTree(0, l, r) {}
- Value Eval(map<string, Value> *symbolMap) {
- Value leftVal = left->Eval(symbolMap);
- if(leftVal.hasMessage())
- {
- cout<<std::to_string(GetLinenum())+": RUNTIME ERROR " + leftVal.getMessage()<<endl;
- }
- else if(right!=0)
- {
- Value rightVal = right->Eval(symbolMap);
- if(rightVal.hasMessage()){
- cout<<std::to_string(GetLinenum())+": RUNTIME ERROR " + rightVal.getMessage()<<endl;
- }
- }
- return Value();
- }
- };
- class IfStatement : public ParseTree {
- public:
- IfStatement(int line, ParseTree *ex, ParseTree *stmt) : ParseTree(line, ex, stmt) {}
- Value Eval(map<string, Value> *symbolMap) {
- Value expr = left->Eval(symbolMap);
- if(expr.isBoolType())
- {
- if(expr.getBoolean()){
- Value stmt = right->Eval(symbolMap);
- return stmt;
- }
- return Value();
- }
- int line = GetLinenum();
- string err = ": RUNTIME ERROR if does not have boolean value";
- return Value(std::to_string(line) + err, true);
- }
- };
- class Assignment : public ParseTree {
- public:
- Assignment(int line, ParseTree *lhs, ParseTree *rhs) : ParseTree(line, lhs, rhs) {}
- Value Eval(map<string, Value> *symbolMap) {
- Value assignValue = right->Eval(symbolMap);
- if(left->GetType()==IDENTTYPE)
- {
- (*symbolMap)[left->GetID()]=assignValue;
- return Value();
- }
- else{
- int line = GetLinenum();
- string err = ": RUNTIME ERROR only identifiers can be assigned a value";
- return Value(std::to_string(line) + err, true);
- }
- }
- };
- class PrintStatement : public ParseTree {
- public:
- PrintStatement(int line, ParseTree *e) : ParseTree(line, e) {}
- Value Eval(map<string, Value> *symbolMap) {
- Value printMsg = left->Eval(symbolMap);
- if(left->GetType()==IDENTTYPE){
- map<string, Value>::iterator it = symbolMap->find(left->GetID());
- if(it == symbolMap->end()){
- int line = GetLinenum();
- string err = ": RUNTIME ERROR cannot print an unassigned identifier";
- return Value(std::to_string(line) + err, true);
- }
- }
- if(printMsg.isError()){
- return Value(printMsg.getMessage(), true);
- }
- cout<<printMsg<<endl;
- return Value();
- }
- };
- class PlusExpr : public ParseTree {
- public:
- PlusExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
- Value Eval(map<string, Value> *symbolMap) {
- return left->Eval(symbolMap)+right->Eval(symbolMap);
- }
- };
- class MinusExpr : public ParseTree {
- public:
- MinusExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
- Value Eval(map<string, Value> *symbolMap) {
- return left->Eval(symbolMap)-right->Eval(symbolMap);
- }
- };
- class TimesExpr : public ParseTree {
- public:
- TimesExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
- Value Eval(map<string, Value> *symbolMap) {
- return left->Eval(symbolMap)*right->Eval(symbolMap);
- }
- };
- class DivideExpr : public ParseTree {
- public:
- DivideExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
- Value Eval(map<string, Value> *symbolMap) {
- return left->Eval(symbolMap) / right->Eval(symbolMap);
- }
- };
- class LogicAndExpr : public ParseTree {
- public:
- LogicAndExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
- Value Eval(map<string, Value> *symbolMap) {
- return left->Eval(symbolMap) && right->Eval(symbolMap);
- }
- NodeType GetType() const { return BOOLTYPE; }
- };
- class LogicOrExpr : public ParseTree {
- public:
- LogicOrExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
- Value Eval(map<string, Value> *symbolMap) {
- return left->Eval(symbolMap) || right->Eval(symbolMap);
- }
- NodeType GetType() const { return BOOLTYPE; }
- };
- class EqExpr : public ParseTree {
- public:
- EqExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
- Value Eval(map<string, Value> *symbolMap) {
- return left->Eval(symbolMap) == right->Eval(symbolMap);
- }
- NodeType GetType() const { return BOOLTYPE; }
- };
- class NEqExpr : public ParseTree {
- public:
- NEqExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
- Value Eval(map<string, Value> *symbolMap) {
- return left->Eval(symbolMap) != right->Eval(symbolMap);
- }
- NodeType GetType() const { return BOOLTYPE; }
- };
- class LtExpr : public ParseTree {
- public:
- LtExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
- Value Eval(map<string, Value> *symbolMap) {
- return left->Eval(symbolMap) < right->Eval(symbolMap);
- }
- NodeType GetType() const { return BOOLTYPE; }
- };
- class LEqExpr : public ParseTree {
- public:
- LEqExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
- Value Eval(map<string, Value> *symbolMap) {
- return left->Eval(symbolMap) <= right->Eval(symbolMap);
- }
- NodeType GetType() const { return BOOLTYPE; }
- };
- class GtExpr : public ParseTree {
- public:
- GtExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
- Value Eval(map<string, Value> *symbolMap) {
- return left->Eval(symbolMap) > right->Eval(symbolMap);
- }
- NodeType GetType() const { return BOOLTYPE; }
- };
- class GEqExpr : public ParseTree {
- public:
- GEqExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
- Value Eval(map<string, Value> *symbolMap) {
- return left->Eval(symbolMap) >= right->Eval(symbolMap);
- }
- NodeType GetType() const { return BOOLTYPE; }
- };
- class IConst : public ParseTree {
- int val;
- public:
- IConst(int l, int i) : ParseTree(l), val(i) {}
- IConst(Token& t) : ParseTree(t.GetLinenum()) {
- val = stoi(t.GetLexeme());
- }
- Value Eval(map<string, Value> *symbolMap) {
- return Value(val);
- }
- NodeType GetType() const { return INTTYPE; }
- };
- class BoolConst : public ParseTree {
- bool val;
- public:
- BoolConst(Token& t, bool val) : ParseTree(t.GetLinenum()), val(val) {}
- Value Eval(map<string, Value> *symbolMap) {
- return Value(val);
- }
- NodeType GetType() const { return BOOLTYPE; }
- };
- class SConst : public ParseTree {
- string val;
- public:
- SConst(Token& t) : ParseTree(t.GetLinenum()) {
- val = t.GetLexeme();
- }
- NodeType GetType() const { return STRTYPE; }
- bool IsString() const { return true; }
- Value Eval(map<string, Value> *symbolMap) {
- return Value(val);
- }
- };
- class Ident : public ParseTree {
- string id;
- public:
- Ident(Token& t) : ParseTree(t.GetLinenum()), id(t.GetLexeme()) {}
- NodeType GetType() const { return IDENTTYPE; }
- Value Eval(map<string, Value> *symbolMap) {
- if(symbolMap->count(id))
- return symbolMap->at(id);
- else
- return Value("An unassigned identifier cant be used", true);
- }
- bool IsIdent() const { return true; }
- string GetID() const { return id; }
- };
- #endif /* PARSETREE_H_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement