Archived
1
0
Fork 0

Replaced make_node with a Node constructor

This commit is contained in:
Brandon Rozek 2018-09-28 12:05:06 -04:00
parent 4c2afd779d
commit 6022898e2b
4 changed files with 41 additions and 53 deletions

View file

@ -7,26 +7,6 @@
#include "../variables/value.hpp" #include "../variables/value.hpp"
#include "../variables/variable.hpp" #include "../variables/variable.hpp"
/* creates a new node and returns it */
struct Node* make_node(int type, struct Value* value, std::string id) {
int i;
/* allocate space */
struct Node* node = new Node();
/* set properties */
node->type = type;
node->value = value;
id.copy(node->id, id.length(), 0);
node->num_children = 0;
for(i = 0; i < MAX_CHILDREN; i++) {
node->children[i] = nullptr;
}
/* return new node */
return node;
}
/* attach an existing node onto a parent */ /* attach an existing node onto a parent */
void attach_node(struct Node* parent, struct Node* child) { void attach_node(struct Node* parent, struct Node* child) {
/* connect it */ /* connect it */

View file

@ -6,7 +6,6 @@
#include "../variables/value.hpp" #include "../variables/value.hpp"
#include "../variables/environment.hpp" #include "../variables/environment.hpp"
#define ID_SIZE 100
#define MAX_CHILDREN 3 #define MAX_CHILDREN 3
/* a tree node definition */ /* a tree node definition */
@ -15,15 +14,24 @@ struct Node {
struct Value* value; struct Value* value;
/* the id of the node (used for identifiers only) */ /* the id of the node (used for identifiers only) */
char id[ID_SIZE]; std::string id;
/* at most three children nodes */ /* at most three children nodes */
int num_children; int num_children;
std::array<struct Node*, MAX_CHILDREN> children; std::array<struct Node*, MAX_CHILDREN> children;
Node(int t, struct Value* v, std::string s) {
type = t;
value = v;
id = s;
for (int i = 0; i < MAX_CHILDREN; i++) {
children[i] = nullptr;
}
}
}; };
// Abstract Syntax Tree Functions // Abstract Syntax Tree Functions
struct Node* make_node(int type, struct Value* value, std::string id); // struct Node* make_node(int type, struct Value* value, std::string id);
void attach_node(struct Node* parent, struct Node* child); void attach_node(struct Node* parent, struct Node* child);
void print_tree(struct Node* node, int tabs); void print_tree(struct Node* node, int tabs);
void delete_tree(struct Node* node); void delete_tree(struct Node* node);

View file

@ -43,12 +43,12 @@ DIGIT [0-9]
"input" {return INPUT;} "input" {return INPUT;}
"lambda" {return LAMBDA;} "lambda" {return LAMBDA;}
":" {return COLON;} ":" {return COLON;}
"true" {yylval.value = make_node(VALUE, make_true(), ""); return VALUE;} "true" {yylval.value = new Node(VALUE, make_true(), ""); return VALUE;}
"false" {yylval.value = make_node(VALUE, make_false(), ""); return VALUE;} "false" {yylval.value = new Node(VALUE, make_false(), ""); return VALUE;}
\".*\" {yylval.value = make_node(VALUE, make_string(substring(yytext, 1, strlen(yytext) - 1)), ""); return VALUE; } \".*\" {yylval.value = new Node(VALUE, make_string(substring(yytext, 1, strlen(yytext) - 1)), ""); return VALUE; }
{DIGIT} {yylval.value = make_node(VALUE, make_long(atoi(yytext)), ""); return VALUE;} {DIGIT} {yylval.value = new Node(VALUE, make_long(atoi(yytext)), ""); return VALUE;}
{DIGIT}*"."?{DIGIT}+ {yylval.value = make_node(VALUE, make_double(atof(yytext)), ""); return VALUE;} {DIGIT}*"."?{DIGIT}+ {yylval.value = new Node(VALUE, make_double(atof(yytext)), ""); return VALUE;}
[_a-zA-Z][_a-zA-Z0-9]* {yylval.value = make_node(IDENTIFIER, nullptr, strdup(yytext)); return IDENTIFIER;} [_a-zA-Z][_a-zA-Z0-9]* {yylval.value = new Node(IDENTIFIER, nullptr, strdup(yytext)); return IDENTIFIER;}
[\n] {linenum++;} [\n] {linenum++;}
[ \t\r]+ {} [ \t\r]+ {}
. {printf("Error: invlaid lexeme '%s'.\n", yytext); return 0;} . {printf("Error: invlaid lexeme '%s'.\n", yytext); return 0;}

View file

@ -65,32 +65,32 @@ statement: assignment { $$ = $1; }
| statements { $$ = $1; } | statements { $$ = $1; }
assignment: ident ASSIGN exprlambda SEMICOLON { assignment: ident ASSIGN exprlambda SEMICOLON {
$$ = make_node(ASSIGN, NULL, ""); $$ = new Node(ASSIGN, NULL, "");
attach_node($$, $1); attach_node($$, $1);
attach_node($$, $3); attach_node($$, $3);
} }
if-statement: IF expression THEN statement { if-statement: IF expression THEN statement {
$$ = make_node(IF, NULL, ""); $$ = new Node(IF, NULL, "");
attach_node($$, $2); attach_node($$, $2);
attach_node($$, $4); attach_node($$, $4);
} }
if-else-statement: IF expression THEN statement ELSE statement { if-else-statement: IF expression THEN statement ELSE statement {
$$ = make_node(IF, NULL, ""); $$ = new Node(IF, NULL, "");
attach_node($$, $2); attach_node($$, $2);
attach_node($$, $4); attach_node($$, $4);
attach_node($$, $6); attach_node($$, $6);
} }
while: WHILE expression DO statement { while: WHILE expression DO statement {
$$ = make_node(WHILE, NULL, ""); $$ = new Node(WHILE, NULL, "");
attach_node($$, $2); attach_node($$, $2);
attach_node($$, $4); attach_node($$, $4);
} }
print: PRINT exprlambda SEMICOLON { print: PRINT exprlambda SEMICOLON {
$$ = make_node(PRINT, NULL, ""); $$ = new Node(PRINT, NULL, "");
attach_node($$, $2); attach_node($$, $2);
} }
@ -98,41 +98,41 @@ print: PRINT exprlambda SEMICOLON {
statements: BEGINTOK substatements END { $$ = $2; } statements: BEGINTOK substatements END { $$ = $2; }
| BEGINTOK END {} | BEGINTOK END {}
substatements: statement substatements {$$ = make_node(STATEMENT, NULL, ""); attach_node($$, $1); attach_node($$, $2); } substatements: statement substatements {$$ = new Node(STATEMENT, NULL, ""); attach_node($$, $1); attach_node($$, $2); }
| statement {$$ = make_node(STATEMENT, NULL, ""); attach_node($$, $1); } | statement {$$ = new Node(STATEMENT, NULL, ""); attach_node($$, $1); }
exprlambda: LAMBDATAG ident COLON expression { exprlambda: LAMBDATAG ident COLON expression {
// Only supports one argument functions for now // Only supports one argument functions for now
$$ = make_node(LAMBDATAG, NULL, ""); $$ = new Node(LAMBDATAG, NULL, "");
attach_node($$, $2); attach_node($$, $2);
attach_node($$, $4); } attach_node($$, $4); }
| expression { $$ = $1; } | expression { $$ = $1; }
expression: expression OR subexpression { $$ = make_node(OR, NULL, ""); attach_node($$, $1); attach_node($$, $3);} expression: expression OR subexpression { $$ = new Node(OR, NULL, ""); attach_node($$, $1); attach_node($$, $3);}
| expression AND subexpression { $$ = make_node(AND, NULL, ""); attach_node($$, $1); attach_node($$, $3);} | expression AND subexpression { $$ = new Node(AND, NULL, ""); attach_node($$, $1); attach_node($$, $3);}
| subexpression { $$ = $1; } | subexpression { $$ = $1; }
subexpression: subexpression LESS term { $$ = make_node(LESS, NULL, ""); attach_node($$, $1); attach_node($$, $3);} subexpression: subexpression LESS term { $$ = new Node(LESS, NULL, ""); attach_node($$, $1); attach_node($$, $3);}
| subexpression LESSEQ term { $$ = make_node(LESSEQ, NULL, ""); attach_node($$, $1); attach_node($$, $3);} | subexpression LESSEQ term { $$ = new Node(LESSEQ, NULL, ""); attach_node($$, $1); attach_node($$, $3);}
| subexpression GREATER term { $$ = make_node(GREATER, NULL, ""); attach_node($$, $1); attach_node($$, $3);} | subexpression GREATER term { $$ = new Node(GREATER, NULL, ""); attach_node($$, $1); attach_node($$, $3);}
| subexpression GREATEREQ term { $$ = make_node(GREATEREQ, NULL, ""); attach_node($$, $1); attach_node($$, $3);} | subexpression GREATEREQ term { $$ = new Node(GREATEREQ, NULL, ""); attach_node($$, $1); attach_node($$, $3);}
| subexpression EQUALS term { $$ = make_node(EQUALS, NULL, ""); attach_node($$, $1); attach_node($$, $3);} | subexpression EQUALS term { $$ = new Node(EQUALS, NULL, ""); attach_node($$, $1); attach_node($$, $3);}
| subexpression NEQUALS term { $$ = make_node(NEQUALS, NULL, ""); attach_node($$, $1); attach_node($$, $3);} | subexpression NEQUALS term { $$ = new Node(NEQUALS, NULL, ""); attach_node($$, $1); attach_node($$, $3);}
| term { $$ = $1; } | term { $$ = $1; }
term : term PLUS subterm { $$ = make_node(PLUS, NULL, ""); attach_node($$, $1); attach_node($$, $3);} term : term PLUS subterm { $$ = new Node(PLUS, NULL, ""); attach_node($$, $1); attach_node($$, $3);}
| term MINUS subterm { $$ = make_node(MINUS, NULL, ""); attach_node($$, $1); attach_node($$, $3);} | term MINUS subterm { $$ = new Node(MINUS, NULL, ""); attach_node($$, $1); attach_node($$, $3);}
| subterm { $$ = $1; } | subterm { $$ = $1; }
subterm: subterm TIMES factor { $$ = make_node(TIMES, NULL, ""); attach_node($$, $1); attach_node($$, $3);} subterm: subterm TIMES factor { $$ = new Node(TIMES, NULL, ""); attach_node($$, $1); attach_node($$, $3);}
| subterm DIVIDE factor { $$ = make_node(DIVIDE, NULL, ""); attach_node($$, $1); attach_node($$, $3);} | subterm DIVIDE factor { $$ = new Node(DIVIDE, NULL, ""); attach_node($$, $1); attach_node($$, $3);}
| factor { $$ = $1; } | factor { $$ = $1; }
factor : MINUS factor { $$ = make_node(MINUS, NULL, ""); attach_node($$, $2); } factor : MINUS factor { $$ = new Node(MINUS, NULL, ""); attach_node($$, $2); }
| NOT factor { $$ = make_node(NOT, NULL, ""); attach_node($$, $2); } | NOT factor { $$ = new Node(NOT, NULL, ""); attach_node($$, $2); }
| atom { $$ = $1; } | atom { $$ = $1; }
callfunc: ident OPENPAREM expression ENDPAREM { $$ = make_node(CALLFUNC, NULL, ""); attach_node($$, $1); attach_node($$, $3); } callfunc: ident OPENPAREM expression ENDPAREM { $$ = new Node(CALLFUNC, NULL, ""); attach_node($$, $1); attach_node($$, $3); }
atom: OPENPAREM expression ENDPAREM { $$ = $2; } atom: OPENPAREM expression ENDPAREM { $$ = $2; }
| callfunc { $$ = $1; } | callfunc { $$ = $1; }
@ -142,7 +142,7 @@ ident: IDENTIFIER { $$ = $1; }
identvalue: ident { $$ = $1; } identvalue: ident { $$ = $1; }
| VALUE { $$ = $1; } | VALUE { $$ = $1; }
| INPUT { $$ = make_node(INPUT, NULL , ""); } | INPUT { $$ = new Node(INPUT, NULL , ""); }