Skip to content

Commit

Permalink
Removed C support references. Fixed wrong indentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
miromannino committed May 6, 2015
1 parent cce10c0 commit 3ad4e9b
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 418 deletions.
370 changes: 192 additions & 178 deletions include/MExprAST.h
Expand Up @@ -35,187 +35,201 @@
#include <MExprEnvironment.h>
#include <MExprInstruction.h>

#ifdef __cplusplus

namespace MExpr {

/*-- Node Abstract class ---------------------*/
/**
* An abstract class that represents a generic node in the abstract syntax tree.
* With the countChildren and getChild methods you can navigate the tree.
*/
class ASTNode {
public:
virtual ~ASTNode() {}

/**
* get the tree representation in a string
*
*/
std::string* getExprTreeString();

/**
* Counts the nodes of the tree/subtree that have this node as root
*
* @return the nodes count (greater or equal to zero)
*/
virtual unsigned int countNodes() = 0;

/**
* Counts the number of children.
* For example the '+' has two children but the 'log' has only one child.
*
* @return the children count (greater or equal to zero)
*/
virtual unsigned int countChildren() = 0;

/**
* Get a child.
* For example the '+' has two children and the getChild(0) returns the first child, the getChild(1) returns the second.
*
* @param c the number of the child (c >= 0).
*
* @return the children or NULL if it doesn't exists
*/
virtual ASTNode* getChild(unsigned int c) throw(Error) = 0;

/**
* Set a child.
*
* @param c the number of the child (c >= 0).
* @param node the value of the child.
*
*/
virtual void setChild(unsigned int c, ASTNode* node) throw(Error) = 0;

/**
* Returns the result of the tree/subtree that have this node as root
*
* @param env the Environment class to evalutate the expression (for variables and functions)
* @return the result
*/
virtual ValueType evaluate(Environment* env) throw(Error) = 0;

/**
* Deallocate the tree/subtree that have this node as root. This function deallocate this node too.
*/
virtual void deleteTree() = 0;

/**
* Returns he Instruction that represent this node. This is used to build the Code.
*/
virtual MExpr::Instruction getMExprInstr() = 0;


virtual void getExprTreeString_rec(std::stringstream* ris, std::string* tabs, bool sameLine) = 0;

};
/*-------------------------------------------*/

/*-- Primitive Operations -------------------*/
/**
* A class that represents a generic primitive operation, for examplel +, -, *, ...
*/
class ASTPrimitiveOp: public ASTNode {
public:
enum Type {
ADD, // '+'
SUB, // '-'
MUL, // '*'
DIV, // '/'
POW // '^'
};
protected:
ASTNode** children; //array of numChildren elements
unsigned int numChildren; //depends on the PrimitiveOp::Type
Type type;
public:
ASTPrimitiveOp(ASTPrimitiveOp::Type type) throw(Error);
~ASTPrimitiveOp();
unsigned int countNodes();
unsigned int countChildren();
ASTNode* getChild(unsigned int c) throw(Error);
void setChild(unsigned int c, ASTNode* node) throw(Error);
ValueType evaluate(Environment* env) throw(Error);
void deleteTree();
MExpr::Instruction getMExprInstr();
protected:
void getExprTreeString_rec(std::stringstream* ris, std::string* tabs, bool sameLine);
};
/*-------------------------------------------*/

/*-- Function -------------------------------*/
/* A class that represents a call to a function.
* */
class ASTFunction: public ASTNode {
private:
std::string funcName;
ASTNode** children; //array of numChildren elements
unsigned int numChildren;

public:
ASTFunction(std::string funcName, unsigned int numArgs);
~ASTFunction();
unsigned int countNodes();
unsigned int countChildren();
ASTNode* getChild(unsigned int c) throw(Error);
void setChild(unsigned int c, ASTNode* node) throw(Error);
ValueType evaluate(Environment* env) throw(Error);
void deleteTree();
MExpr::Instruction getMExprInstr();
protected:
void getExprTreeString_rec(std::stringstream* ris, std::string* tabs, bool sameLine);
};
/*-------------------------------------------*/

/*-- Value ----------------------------------*/
/* A class that represents a value.
* The countNodes returns always 1, countChildren returns always 0 and getChild returns always NULL.
* setChild doesn't set anything because this node haven't children.
* */
class ASTValue: public ASTNode {
private:
ValueType value;
public:
ASTValue(ValueType value);
~ASTValue();
unsigned int countNodes();
unsigned int countChildren();
ASTNode* getChild(unsigned int c) throw(Error);
void setChild(unsigned int c, ASTNode* node) throw(Error);
ValueType evaluate(Environment* env) throw(Error);
void deleteTree();
MExpr::Instruction getMExprInstr();
protected:
void getExprTreeString_rec(std::stringstream* ris, std::string* tabs, bool sameLine);
};
/*-------------------------------------------*/

/*-- Variable -------------------------------*/
/* A class that represents a variable.
* The countNodes returns always 1, countChildren returns always 0 and getChild returns always NULL.
* setChild doesn't set anything because this node haven't children.
* */
class ASTVariable: public ASTNode {
private:
char var;
public:
ASTVariable(char var);
~ASTVariable();
unsigned int countNodes();
unsigned int countChildren();
ASTNode* getChild(unsigned int c) throw(Error);
void setChild(unsigned int c, ASTNode* node) throw(Error);
ValueType evaluate(Environment* env) throw(Error);
void deleteTree();
MExpr::Instruction getMExprInstr();
protected:
void getExprTreeString_rec(std::stringstream* ris, std::string* tabs, bool sameLine);
};
/*-------------------------------------------*/
/*-- Node Abstract class ---------------------*/
/**
* An abstract class that represents a generic node in the abstract syntax tree.
* With the countChildren and getChild methods you can navigate the tree.
*/
class ASTNode {

public:
virtual ~ASTNode() {
}

/**
* get the tree representation in a string
*
*/
std::string* getExprTreeString();

/**
* Counts the nodes of the tree/subtree that have this node as root
*
* @return the nodes count (greater or equal to zero)
*/
virtual unsigned int countNodes() = 0;

/**
* Counts the number of children.
* For example the '+' has two children but the 'log' has only one child.
*
* @return the children count (greater or equal to zero)
*/
virtual unsigned int countChildren() = 0;

/**
* Get a child.
* For example the '+' has two children and the getChild(0) returns the first child, the getChild(1) returns the second.
*
* @param c the number of the child (c >= 0).
*
* @return the children or NULL if it doesn't exists
*/
virtual ASTNode* getChild(unsigned int c) throw (Error) = 0;

/**
* Set a child.
*
* @param c the number of the child (c >= 0).
* @param node the value of the child.
*
*/
virtual void setChild(unsigned int c, ASTNode* node) throw (Error) = 0;

/**
* Returns the result of the tree/subtree that have this node as root
*
* @param env the Environment class to evalutate the expression (for variables and functions)
* @return the result
*/
virtual ValueType evaluate(Environment* env) throw (Error) = 0;

/**
* Deallocate the tree/subtree that have this node as root. This function deallocate this node too.
*/
virtual void deleteTree() = 0;

/**
* Returns he Instruction that represent this node. This is used to build the Code.
*/
virtual MExpr::Instruction getMExprInstr() = 0;

virtual void getExprTreeString_rec(std::stringstream* ris, std::string* tabs, bool sameLine) = 0;

};


/*-- Primitive Operations -------------------*/

/**
* A class that represents a generic primitive operation, for examplel +, -, *, ...
*/
class ASTPrimitiveOp: public ASTNode {

public:
enum Type {
ADD, // '+'
SUB, // '-'
MUL, // '*'
DIV, // '/'
POW // '^'
};

protected:
ASTNode** children; //array of numChildren elements
unsigned int numChildren; //depends on the PrimitiveOp::Type
Type type;

public:
ASTPrimitiveOp(ASTPrimitiveOp::Type type) throw (Error);
~ASTPrimitiveOp();
unsigned int countNodes();
unsigned int countChildren();
ASTNode* getChild(unsigned int c) throw (Error);
void setChild(unsigned int c, ASTNode* node) throw (Error);
ValueType evaluate(Environment* env) throw (Error);
void deleteTree();
MExpr::Instruction getMExprInstr();

protected:
void getExprTreeString_rec(std::stringstream* ris, std::string* tabs, bool sameLine);

};
/*-------------------------------------------*/

/*-- Function -------------------------------*/
/* A class that represents a call to a function.
* */
class ASTFunction: public ASTNode {

private:
std::string funcName;
ASTNode** children; //array of numChildren elements
unsigned int numChildren;

public:
ASTFunction(std::string funcName, unsigned int numArgs);
~ASTFunction();
unsigned int countNodes();
unsigned int countChildren();
ASTNode* getChild(unsigned int c) throw (Error);
void setChild(unsigned int c, ASTNode* node) throw (Error);
ValueType evaluate(Environment* env) throw (Error);
void deleteTree();
MExpr::Instruction getMExprInstr();

protected:
void getExprTreeString_rec(std::stringstream* ris, std::string* tabs, bool sameLine);

};
/*-------------------------------------------*/

/*-- Value ----------------------------------*/
/* A class that represents a value.
* The countNodes returns always 1, countChildren returns always 0 and getChild returns always NULL.
* setChild doesn't set anything because this node haven't children.
* */
class ASTValue: public ASTNode {

private:
ValueType value;

public:
ASTValue(ValueType value);
~ASTValue();
unsigned int countNodes();
unsigned int countChildren();
ASTNode* getChild(unsigned int c) throw (Error);
void setChild(unsigned int c, ASTNode* node) throw (Error);
ValueType evaluate(Environment* env) throw (Error);
void deleteTree();
MExpr::Instruction getMExprInstr();

protected:
void getExprTreeString_rec(std::stringstream* ris, std::string* tabs, bool sameLine);

};
/*-------------------------------------------*/

/*-- Variable -------------------------------*/
/* A class that represents a variable.
* The countNodes returns always 1, countChildren returns always 0 and getChild returns always NULL.
* setChild doesn't set anything because this node haven't children.
* */
class ASTVariable: public ASTNode {

private:
char var;

public:
ASTVariable(char var);
~ASTVariable();
unsigned int countNodes();
unsigned int countChildren();
ASTNode* getChild(unsigned int c) throw (Error);
void setChild(unsigned int c, ASTNode* node) throw (Error);
ValueType evaluate(Environment* env) throw (Error);
void deleteTree();
MExpr::Instruction getMExprInstr();

protected:
void getExprTreeString_rec(std::stringstream* ris, std::string* tabs, bool sameLine);

};
/*-------------------------------------------*/

} //end of namespace MExpr

#endif //of #ifdef __cplusplus

#endif

0 comments on commit 3ad4e9b

Please sign in to comment.