Commit 74aab7d4 authored by Léo Grange's avatar Léo Grange
Browse files

replace raw pointers with unique_ptr in all AST nodes and improve print readibility

parent 165ecd5d
Loading
Loading
Loading
Loading
+34 −4
Original line number Diff line number Diff line
@@ -5,6 +5,9 @@

#include "node.hpp"

// for token values
#include "parser.hpp"


void NRegItem::printNode(std::ostream &o) {
	o << "reg ";
@@ -113,14 +116,41 @@ void NBitfieldVariable::printNode(std::ostream &o) {
void NBinaryOperator::printNode(std::ostream &o) {
	o << "(";
	lexp->printNode(o);
	o << ") <token" << op << "> (";
	o << ") " << tokenToString(op) << " (";
	rexp->printNode(o);
	o << ") ";
}


void NUnaryOperator::printNode(std::ostream &o) {
	o << "<token" << op << "> (";
	exp->printNode(o);
	o << ") ";
	o << tokenToString(op) << " ";
}


#define TOK(name, str) case TOK_##name: return str;
std::string tokenToString(int tok) {
	switch(tok) {
		TOK(GT, ">");
		TOK(GE, ">=");
		TOK(LT, "<");
		TOK(LE, "<=");
		TOK(EQ, "=");
		TOK(NE, "<>");

		TOK(PLUS, "+");
		TOK(MINUS, "-");
		TOK(MUL, "*");
		TOK(DIV, "/");
		TOK(LSHIFT, "<<");
		TOK(RSHIFT, ">>");
		TOK(AND, "&");
		TOK(OR, "|");

		TOK(NOT, "!");
		TOK(INV, "~");

		default:
			return std::string("<token ") + std::to_string(tok) + ">";
	}
}
+98 −61
Original line number Diff line number Diff line
@@ -7,6 +7,9 @@
#include <string>
#include <vector>

#include <memory>
#include <utility>

class NState;
class NItem;
class NStatement;
@@ -32,19 +35,20 @@ public:
template<typename NodeT>
class NAnySequence : public Node {
public:
	std::vector<NodeT *> items;
	std::vector<std::unique_ptr<NodeT>> items;

	NAnySequence(NodeT *first = NULL) {
	NAnySequence(std::unique_ptr<NodeT> first = nullptr) {
		if(first)
			items.push_back(first);
			items.push_back(std::move(first));
	}

	void addItem(NodeT *item) {
		items.push_back(item);
	void addItem(std::unique_ptr<NodeT> item) {
		items.push_back(std::move(item));
	}

	void printNode(std::ostream &o) {
		for(auto item : items) {
		// use reference to avoid unique_ptr copy!
		for(auto& item : items) {
			item->printNode(o);
			o << " ;" << std::endl;
		}
@@ -62,10 +66,12 @@ class NItem : public Node {
// assign address to a symbolic register
class NRegItem : public NItem {
public:
	NIdentifier *ident;
	NInteger *address;
	std::unique_ptr<NIdentifier> ident;
	std::unique_ptr<NInteger> address;

	NRegItem(NIdentifier *id, NInteger *addr) : ident(id), address(addr) {}
	NRegItem(std::unique_ptr<NIdentifier> id, std::unique_ptr<NInteger> addr)
		: ident(std::move(id)), address(std::move(addr))
	{}

	void printNode(std::ostream &o);
};
@@ -73,12 +79,14 @@ public:

class NAutoItem : public NItem {
public:
	NIdentifier *ident;
	NStatementSequence *stmts;
	NStateSequence *states;

	NAutoItem(NIdentifier *id, NStatementSequence *stmts, NStateSequence *states) :
		ident(id), stmts(stmts), states(states)
	std::unique_ptr<NIdentifier> ident;
	std::unique_ptr<NStatementSequence> stmts;
	std::unique_ptr<NStateSequence> states;

	NAutoItem(std::unique_ptr<NIdentifier> id,
			std::unique_ptr<NStatementSequence> stmts,
			std::unique_ptr<NStateSequence> states)
		: ident(std::move(id)), stmts(std::move(stmts)), states(std::move(states))
	{}

	void printNode(std::ostream &o);
@@ -87,33 +95,42 @@ public:

class NVarItem : public NItem {
public:
	NIdentifier *ident;
	std::unique_ptr<NIdentifier> ident;

	NVarItem(std::unique_ptr<NIdentifier> ident)
		: ident(std::move(ident))
	{}

	NVarItem(NIdentifier *ident) : ident(ident) {}
	void printNode(std::ostream &o);
};


class NConstItem : public NItem {
public:
	NIdentifier *ident;
	NInteger *value;
	std::unique_ptr<NIdentifier> ident;
	std::unique_ptr<NInteger> value;

	NConstItem(std::unique_ptr<NIdentifier> ident, std::unique_ptr<NInteger> value)
		: ident(std::move(ident)), value(std::move(value))
	{}

	NConstItem(NIdentifier *ident, NInteger *value) : ident(ident), value(value) {}
	void printNode(std::ostream &o);
};


class NSignalItem : public NItem {
public:
	NIdentifier *ident;
	NIdentifier *where;
	NExpression *bit;
	std::unique_ptr<NIdentifier> ident;
	std::unique_ptr<NIdentifier> where;
	std::unique_ptr<NExpression> bit;
	bool reverse;

	NSignalItem(NIdentifier *ident, NIdentifier *where, NExpression *bit,
			bool reverse) :
		ident(ident), where(where), bit(bit), reverse(reverse)
	NSignalItem(std::unique_ptr<NIdentifier> ident,
			std::unique_ptr<NIdentifier> where,
			std::unique_ptr<NExpression> bit,
			bool reverse)
		: ident(std::move(ident)), where(std::move(where)), bit(std::move(bit)),
		reverse(std::move(reverse))
	{}

	void printNode(std::ostream &o);
@@ -128,12 +145,14 @@ class NItemSequence : public NAnySequence<NItem> {

class NState : public Node {
public:
	NIdentifier *ident;
	NStatementSequence *stmts;
	NWhenSequence *whens;
	
	NState(NIdentifier *id, NStatementSequence *stmts, NWhenSequence *whens) :
		ident(id), stmts(stmts), whens(whens)
	std::unique_ptr<NIdentifier> ident;
	std::unique_ptr<NStatementSequence> stmts;
	std::unique_ptr<NWhenSequence> whens;
	
	NState(std::unique_ptr<NIdentifier> id,
			std::unique_ptr<NStatementSequence> stmts,
			std::unique_ptr<NWhenSequence> whens)
		: ident(std::move(id)), stmts(std::move(stmts)), whens(std::move(whens))
	{}

	void printNode(std::ostream &o);
@@ -156,11 +175,12 @@ class NStatementSequence : public NAnySequence<NStatement> {

class NAssignStatement : public NStatement {
public:
	NBitfieldVariable *variable;
	NExpression *assign;
	std::unique_ptr<NBitfieldVariable> variable;
	std::unique_ptr<NExpression> assign;

	NAssignStatement(NBitfieldVariable *variable, NExpression *assign) :
		variable(variable), assign(assign)
	NAssignStatement(std::unique_ptr<NBitfieldVariable> variable,
			std::unique_ptr<NExpression> assign)
		: variable(std::move(variable)), assign(std::move(assign))
	{}

	void printNode(std::ostream &o);
@@ -169,9 +189,12 @@ public:

class NGotoStatement : public NStatement {
public:
	NIdentifier *ident;
	std::unique_ptr<NIdentifier> ident;

	NGotoStatement(std::unique_ptr<NIdentifier> id)
		: ident(std::move(id))
	{}

	NGotoStatement(NIdentifier *id) : ident(id) {}
	void printNode(std::ostream &o);
};

@@ -184,13 +207,15 @@ public:

class NIfStatement : public NStatement {
public:
	NBinaryOperator *cond;
	NStatementSequence *stmtsTrue;
	NStatementSequence *stmtsFalse;

	NIfStatement(NBinaryOperator *cond, NStatementSequence *stmtsTrue,
			NStatementSequence *stmtsFalse = NULL) :
		cond(cond), stmtsTrue(stmtsTrue), stmtsFalse(stmtsFalse)
	std::unique_ptr<NBinaryOperator> cond;
	std::unique_ptr<NStatementSequence> stmtsTrue;
	std::unique_ptr<NStatementSequence> stmtsFalse;

	NIfStatement(std::unique_ptr<NBinaryOperator> cond,
			std::unique_ptr<NStatementSequence> stmtsTrue,
			std::unique_ptr<NStatementSequence> stmtsFalse = nullptr)
		: cond(std::move(cond)), stmtsTrue(std::move(stmtsTrue)),
		stmtsFalse(std::move(stmtsFalse))
	{}

	void printNode(std::ostream &o);
@@ -198,10 +223,12 @@ public:

class NWhen : public Node {
public:
	NIdentifier *ident;
	NStatementSequence *stmts;
	std::unique_ptr<NIdentifier> ident;
	std::unique_ptr<NStatementSequence> stmts;

	NWhen(NIdentifier *id, NStatementSequence *stmts) : ident(id), stmts(stmts) {}
	NWhen(std::unique_ptr<NIdentifier> id, std::unique_ptr<NStatementSequence> stmts)
		: ident(std::move(id)), stmts(std::move(stmts))
	{}

	void printNode(std::ostream &o);
};
@@ -230,13 +257,15 @@ public:
 */
class NBitfieldVariable : public NExpression {
public:
	NIdentifier *ident;
	NExpression *bitFrom;
	NExpression *bitTo;

	NBitfieldVariable(NIdentifier *ident, NExpression *bitFrom = NULL,
			NExpression *bitTo = NULL) :
		ident(ident), bitFrom(bitFrom), bitTo(bitTo)
	std::unique_ptr<NIdentifier> ident;
	std::unique_ptr<NExpression> bitFrom;
	std::unique_ptr<NExpression> bitTo;

	NBitfieldVariable(std::unique_ptr<NIdentifier> ident,
			std::unique_ptr<NExpression> bitFrom = nullptr,
			std::unique_ptr<NExpression> bitTo = nullptr)
		: ident(std::move(ident)), bitFrom(std::move(bitFrom)),
		bitTo(std::move(bitTo))
	{}

	void printNode(std::ostream &o);
@@ -261,11 +290,12 @@ class NBinaryOperator : public NExpression {
public:
	// token value of the operator used...
	int op;
	NExpression *lexp;
	NExpression *rexp;
	std::unique_ptr<NExpression> lexp;
	std::unique_ptr<NExpression> rexp;

	NBinaryOperator(int o, NExpression *l, NExpression *r) :
		op(o), lexp(l), rexp(r)
	NBinaryOperator(int o, std::unique_ptr<NExpression> l,
			std::unique_ptr<NExpression> r)
		: op(o), lexp(std::move(l)), rexp(std::move(r))
	{}

	void printNode(std::ostream &o);
@@ -275,11 +305,18 @@ class NUnaryOperator : public NExpression {
public:
	// token value of the operator used...
	int op;
	NExpression *exp;
	std::unique_ptr<NExpression> exp;

	NUnaryOperator(int o, NExpression *e) :
		op(o), exp(e)
	NUnaryOperator(int o, std::unique_ptr<NExpression> e)
		: op(o), exp(std::move(e))
	{}

	void printNode(std::ostream &o);
};



/**
 * Simple utility function to provide displayable operators from token value.
 */
std::string tokenToString(int tok);
+33 −29
Original line number Diff line number Diff line
@@ -11,6 +11,10 @@ void yyerror(const char *s) {
// our root node ;)
Node *rootNode;

// a very dirty hack for the sake of readibility...
template<typename T>
std::unique_ptr<T> UNIQUE(T *ptr) { return std::unique_ptr<T>(ptr); }

%}


@@ -91,62 +95,62 @@ Node *rootNode;
program : items { rootNode = $1; }
	 ;

items : items item { $1->addItem($2); $$ = $1; }
	 | item { $$ = new NItemSequence($1); }
items : items item { $1->addItem(UNIQUE($2)); $$ = $1; }
	 | item { $$ = new NItemSequence(UNIQUE($1)); }
	 ;

item : TOK_REG ident TOK_EQ literal { $$ = new NRegItem($2, $4); }
item : TOK_REG ident TOK_EQ literal { $$ = new NRegItem(UNIQUE($2), UNIQUE($4)); }
	/* TODO var const signal*/
	| TOK_VAR ident { $$ = new NVarItem($2); }
	| TOK_CONST ident TOK_EQ literal { $$ = new NConstItem($2, $4); }
	| TOK_VAR ident { $$ = new NVarItem(UNIQUE($2)); }
	| TOK_CONST ident TOK_EQ literal { $$ = new NConstItem(UNIQUE($2), UNIQUE($4)); }
	| TOK_SIGNAL ident TOK_EQ ident TOK_LBRAC expr TOK_RBRAC
		{ $$ = new NSignalItem($2, $4, $6, false); }
		{ $$ = new NSignalItem(UNIQUE($2), UNIQUE($4), UNIQUE($6), false); }
	| TOK_SIGNAL ident TOK_EQ TOK_NOT ident TOK_LBRAC expr TOK_RBRAC
		{ $$ = new NSignalItem($2, $5, $7, true); }
		{ $$ = new NSignalItem(UNIQUE($2), UNIQUE($5), UNIQUE($7), true); }
	| TOK_AUTO ident stmts states state {
		$4->addItem($5);
		$$ = new NAutoItem($2, $3, $4);
		$4->addItem(UNIQUE($5));
		$$ = new NAutoItem(UNIQUE($2), UNIQUE($3), UNIQUE($4));
	}
	;

/* zero or more statements */
stmts : stmts stmt { $1->addItem($2); }
stmts : stmts stmt { $1->addItem(UNIQUE($2)); }
	  | { $$ = new NStatementSequence(); }
	  ;

stmt : variable TOK_EQ expr { $$ = new NAssignStatement($1, $3); }
	 | TOK_GOTO ident { $$ = new NGotoStatement($2); }
stmt : variable TOK_EQ expr { $$ = new NAssignStatement(UNIQUE($1), UNIQUE($3)); }
	 | TOK_GOTO ident { $$ = new NGotoStatement(UNIQUE($2)); }
	 | TOK_EXIT { $$ = new NExitStatement(); }
	 | TOK_IF cond TOK_THEN stmts stmt TOK_END {
			$4->addItem($5);
			$$ = new NIfStatement($2, $4);
			$4->addItem(UNIQUE($5));
			$$ = new NIfStatement(UNIQUE($2), UNIQUE($4));
		}
	 | TOK_IF cond TOK_THEN stmts stmt TOK_ELSE stmts stmt TOK_END {
			$4->addItem($5);
			$7->addItem($8);
			$$ = new NIfStatement($2, $4, $7);
			$4->addItem(UNIQUE($5));
			$7->addItem(UNIQUE($8));
			$$ = new NIfStatement(UNIQUE($2), UNIQUE($4), UNIQUE($7));
		}
	 ;

/* zero or more states */
states : states state { $1->addItem($2); }
states : states state { $1->addItem(UNIQUE($2)); }
	   | { $$ = new NStateSequence(); }
	   ;

state : TOK_STATE ident stmts whens when {
	  		$4->addItem($5);
	  		$$ = new NState($2, $3, $4);
	  		$4->addItem(UNIQUE($5));
	  		$$ = new NState(UNIQUE($2), UNIQUE($3), UNIQUE($4));
		}
	  ;

/* zero or more whens */
whens : whens when { $1->addItem($2); }
whens : whens when { $1->addItem(UNIQUE($2)); }
	  | { $$ = new NWhenSequence(); }
	  ;

when : TOK_WHEN ident stmts stmt {
	 		$3->addItem($4);
			$$ = new NWhen($2, $3);
	 		$3->addItem(UNIQUE($4));
			$$ = new NWhen(UNIQUE($2), UNIQUE($3));
	 }
	 ;

@@ -162,21 +166,21 @@ compop : TOK_EQ | TOK_NE | TOK_GT | TOK_GE | TOK_LT | TOK_LE ;

unop : TOK_MINUS | TOK_INV ;

variable : ident { $$ = new NBitfieldVariable($1); }
variable : ident { $$ = new NBitfieldVariable(UNIQUE($1)); }
	 	 | ident TOK_LBRAC expr TOK_RBRAC
			{ $$ = new NBitfieldVariable($1, $3); }
			{ $$ = new NBitfieldVariable(UNIQUE($1), UNIQUE($3)); }
	 	 | ident TOK_LBRAC expr TOK_COLUMN expr TOK_RBRAC
		 	{ $$ = new NBitfieldVariable($1, $3, $5); }
		 	{ $$ = new NBitfieldVariable(UNIQUE($1), UNIQUE($3), UNIQUE($5)); }
		 ;

expr : variable { $$ = $1; }
	 | literal { $$ = $1; }
	 | unop expr { $$ = new NUnaryOperator($1, $2); }
	 | expr binop expr { $$ = new NBinaryOperator($2, $1, $3); }
	 | unop expr { $$ = new NUnaryOperator($1, UNIQUE($2)); }
	 | expr binop expr { $$ = new NBinaryOperator($2, UNIQUE($1), UNIQUE($3)); }
	 | TOK_LPAR expr TOK_RPAR { $$ = $2; }
	 ;

cond : expr compop expr { $$ = new NBinaryOperator($2, $1, $3); }
cond : expr compop expr { $$ = new NBinaryOperator($2, UNIQUE($1), UNIQUE($3)); }
	 ;