Skip to content

Commit

Permalink
[#133] Added vartype nodes for parenCast expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
dmed256 committed Jul 4, 2018
1 parent 4bee423 commit fd248c6
Show file tree
Hide file tree
Showing 11 changed files with 292 additions and 86 deletions.
33 changes: 33 additions & 0 deletions include/occa/lang/exprNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ namespace occa {
extern const udim_t string;
extern const udim_t identifier;
extern const udim_t type;
extern const udim_t vartype;
extern const udim_t variable;
extern const udim_t function;
extern const udim_t value;
Expand Down Expand Up @@ -275,6 +276,7 @@ namespace occa {
virtual void debugPrint(const std::string &prefix) const;
};

// |---[ Type ]--------------------
class typeNode : public exprNode {
public:
type_t &value;
Expand All @@ -298,7 +300,35 @@ namespace occa {

virtual void debugPrint(const std::string &prefix) const;
};
// |===============================

// |---[ Vartype ]-----------------
class vartypeNode : public exprNode {
public:
vartype_t value;

vartypeNode(token_t *token_,
const vartype_t &value_);

vartypeNode(const vartypeNode& node);

virtual ~vartypeNode();

virtual udim_t type() const;

virtual exprNode* clone() const;

virtual void setChildren(exprNodeRefVector &children);

virtual bool hasAttribute(const std::string &attr) const;

virtual void print(printer &pout) const;

virtual void debugPrint(const std::string &prefix) const;
};
// |===============================

// |---[ Variable ]----------------
class variableNode : public exprNode {
public:
variable_t &value;
Expand All @@ -324,7 +354,9 @@ namespace occa {

virtual void debugPrint(const std::string &prefix) const;
};
// |===============================

// |---[ Function ]----------------
class functionNode : public exprNode {
public:
function_t &value;
Expand All @@ -348,6 +380,7 @@ namespace occa {

virtual void debugPrint(const std::string &prefix) const;
};
// |===============================
//==================================

//---[ Operators ]------------------
Expand Down
2 changes: 1 addition & 1 deletion include/occa/lang/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ namespace occa {
void loadDeclarationAssignment(variableDeclaration &decl);
void loadDeclarationBraceInitializer(variableDeclaration &decl);

vartype_t preloadType();
vartype_t loadType();

void loadBaseType(vartype_t &vartype);

Expand Down
25 changes: 22 additions & 3 deletions include/occa/lang/token.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@

#include <occa/lang/errorHandler.hpp>
#include <occa/lang/file.hpp>
#include <occa/lang/type.hpp>

namespace occa {
namespace lang {
class operator_t;
class token_t;
class qualifier_t;
class type_t;
class variable_t;
class function_t;

typedef std::vector<token_t*> tokenVector;

Expand Down Expand Up @@ -75,6 +74,7 @@ namespace occa {

extern const int qualifier;
extern const int type;
extern const int vartype;
extern const int variable;
extern const int function;

Expand Down Expand Up @@ -138,7 +138,8 @@ namespace occa {
void debugPrint() const;
};

std::ostream& operator << (std::ostream &out, token_t &token);
std::ostream& operator << (std::ostream &out,
token_t &token);

//---[ Unknown ]--------------------
class unknownToken : public token_t {
Expand Down Expand Up @@ -262,6 +263,24 @@ namespace occa {
};
//==================================

//---[ Vartype ]--------------------
class vartypeToken : public token_t {
public:
vartype_t value;

vartypeToken(const fileOrigin &origin_,
const vartype_t &value_);

virtual ~vartypeToken();

virtual int type() const;

virtual token_t* clone() const;

virtual void print(std::ostream &out) const;
};
//==================================

//---[ Variable ]-------------------
class variableToken : public token_t {
public:
Expand Down
4 changes: 0 additions & 4 deletions include/occa/lang/tokenContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ namespace occa {

int getNextOperator(const opType_t &opType);

exprNode* getExpression();
exprNode* getExpression(const int start,
const int end);

void debugPrint();
};
}
Expand Down
10 changes: 10 additions & 0 deletions include/occa/lang/type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ namespace occa {
void printError(const std::string &message) const;
};

std::ostream& operator << (std::ostream &out,
const type_t &type);
printer& operator << (printer &pout,
const type_t &type);
//==================================
Expand All @@ -153,6 +155,8 @@ namespace occa {
void add(const qualifierWithSource &qualifier);
};

std::ostream& operator << (std::ostream &out,
const pointer_t &pointer);
printer& operator << (printer &pout,
const pointer_t &pointer);
//==================================
Expand Down Expand Up @@ -181,6 +185,8 @@ namespace occa {
void printError(const std::string &message) const;
};

std::ostream& operator << (std::ostream &out,
const array_t &array);
printer& operator << (printer &pout,
const array_t &array);
//==================================
Expand Down Expand Up @@ -245,6 +251,8 @@ namespace occa {
vartype_t& operator += (const array_t &array);
vartype_t& operator += (const arrayVector &arrays_);

bool hasAttribute(const std::string &attr) const;

vartype_t declarationType() const;

vartype_t flatten() const;
Expand All @@ -260,6 +268,8 @@ namespace occa {
void printError(const std::string &message) const;
};

std::ostream& operator << (std::ostream &out,
const vartype_t &type);
printer& operator << (printer &pout,
const vartype_t &type);
//==================================
Expand Down
105 changes: 73 additions & 32 deletions src/lang/exprNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,54 +32,56 @@ namespace occa {
const udim_t string = (1L << 3);
const udim_t identifier = (1L << 4);
const udim_t type = (1L << 5);
const udim_t variable = (1L << 6);
const udim_t function = (1L << 7);
const udim_t vartype = (1L << 6);
const udim_t variable = (1L << 7);
const udim_t function = (1L << 8);

const udim_t value = (primitive |
type |
vartype |
variable |
function);

const udim_t rawOp = (1L << 8);
const udim_t leftUnary = (1L << 9);
const udim_t rightUnary = (1L << 10);
const udim_t binary = (1L << 11);
const udim_t ternary = (1L << 12);
const udim_t rawOp = (1L << 9);
const udim_t leftUnary = (1L << 10);
const udim_t rightUnary = (1L << 11);
const udim_t binary = (1L << 12);
const udim_t ternary = (1L << 13);
const udim_t op = (leftUnary |
rightUnary |
binary |
ternary);

const udim_t pair = (1L << 13);
const udim_t pair = (1L << 14);

const udim_t subscript = (1L << 14);
const udim_t call = (1L << 15);
const udim_t subscript = (1L << 15);
const udim_t call = (1L << 16);

const udim_t sizeof_ = (1L << 16);
const udim_t sizeof_pack_ = (1L << 17);
const udim_t new_ = (1L << 18);
const udim_t delete_ = (1L << 19);
const udim_t throw_ = (1L << 20);
const udim_t sizeof_ = (1L << 17);
const udim_t sizeof_pack_ = (1L << 18);
const udim_t new_ = (1L << 19);
const udim_t delete_ = (1L << 20);
const udim_t throw_ = (1L << 21);

const udim_t typeid_ = (1L << 21);
const udim_t noexcept_ = (1L << 22);
const udim_t alignof_ = (1L << 23);
const udim_t typeid_ = (1L << 22);
const udim_t noexcept_ = (1L << 23);
const udim_t alignof_ = (1L << 24);

const udim_t const_cast_ = (1L << 24);
const udim_t dynamic_cast_ = (1L << 25);
const udim_t static_cast_ = (1L << 26);
const udim_t reinterpret_cast_ = (1L << 27);
const udim_t const_cast_ = (1L << 25);
const udim_t dynamic_cast_ = (1L << 26);
const udim_t static_cast_ = (1L << 27);
const udim_t reinterpret_cast_ = (1L << 28);

const udim_t funcCast = (1L << 28);
const udim_t parenCast = (1L << 29);
const udim_t constCast = (1L << 30);
const udim_t staticCast = (1L << 31);
const udim_t reinterpretCast = (1L << 32);
const udim_t dynamicCast = (1L << 33);
const udim_t funcCast = (1L << 29);
const udim_t parenCast = (1L << 30);
const udim_t constCast = (1L << 31);
const udim_t staticCast = (1L << 32);
const udim_t reinterpretCast = (1L << 33);
const udim_t dynamicCast = (1L << 34);

const udim_t parentheses = (1L << 34);
const udim_t tuple = (1L << 35);
const udim_t cudaCall = (1L << 36);
const udim_t parentheses = (1L << 35);
const udim_t tuple = (1L << 36);
const udim_t cudaCall = (1L << 37);
}

exprNode::exprNode(token_t *token_) :
Expand Down Expand Up @@ -348,7 +350,7 @@ namespace occa {
}
// |===============================

// |---[ Type ]----------------
// |---[ Type ]--------------------
typeNode::typeNode(token_t *token_,
type_t &value_) :
exprNode(token_),
Expand Down Expand Up @@ -387,6 +389,45 @@ namespace occa {
}
// |===============================

// |---[ Vartype ]-----------------
vartypeNode::vartypeNode(token_t *token_,
const vartype_t &value_) :
exprNode(token_),
value(value_) {}

vartypeNode::vartypeNode(const vartypeNode &node) :
exprNode(node.token),
value(node.value) {}

vartypeNode::~vartypeNode() {}

udim_t vartypeNode::type() const {
return exprNodeType::vartype;
}

exprNode* vartypeNode::clone() const {
return new vartypeNode(token, value);
}

void vartypeNode::setChildren(exprNodeRefVector &children) {}

bool vartypeNode::hasAttribute(const std::string &attr) const {
return value.hasAttribute(attr);
}

void vartypeNode::print(printer &pout) const {
pout << value;
}

void vartypeNode::debugPrint(const std::string &prefix) const {
printer pout(std::cerr);
std::cerr << prefix << "|\n"
<< prefix << "|---[";
pout << (*this);
std::cerr << "] (vartype)\n";
}
// |===============================

// |---[ Variable ]----------------
variableNode::variableNode(token_t *token_,
variable_t &value_) :
Expand Down
28 changes: 21 additions & 7 deletions src/lang/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace occa {
namespace lang {
static const int outputTokenType = (tokenType::identifier |
tokenType::type |
tokenType::vartype |
tokenType::variable |
tokenType::function |
tokenType::primitive |
Expand Down Expand Up @@ -287,6 +288,10 @@ namespace occa {
typeToken &t = token->to<typeToken>();
state.pushOutput(new typeNode(token, t.value));
}
else if (tokenType & tokenType::vartype) {
vartypeToken &t = token->to<vartypeToken>();
state.pushOutput(new vartypeNode(token, t.value));
}
else if (tokenType & tokenType::primitive) {
primitiveToken &t = token->to<primitiveToken>();
state.pushOutput(new primitiveNode(token, t.value));
Expand Down Expand Up @@ -388,7 +393,8 @@ namespace occa {
}

if (pair.opType() & operatorType::parentheses) {
if (pair.value->type() & exprNodeType::type) {
if (pair.value->type() & (exprNodeType::type |
exprNodeType::vartype)) {
state.pushOperator(
new leftUnaryOpNode(&opToken,
op::parenCast,
Expand Down Expand Up @@ -747,12 +753,20 @@ namespace occa {

if (opType & operatorType::parenCast) {
leftUnaryOpNode &parenOpNode = (leftUnaryOpNode&) opNode;
type_t &type = ((typeNode*) parenOpNode.value)->value;
state.pushOutput(
new parenCastNode(parenOpNode.token,
type,
value)
);
exprNode *valueNode = parenOpNode.value;
if (valueNode->type() & exprNodeType::type) {
state.pushOutput(
new parenCastNode(parenOpNode.token,
((typeNode*) valueNode)->value,
value)
);
} else {
state.pushOutput(
new parenCastNode(parenOpNode.token,
((vartypeNode*) valueNode)->value,
value)
);
}
}
else if (opType & operatorType::sizeof_) {
state.pushOutput(
Expand Down
Loading

0 comments on commit fd248c6

Please sign in to comment.