From 6022898e2b775f6138d74ec0a550270d8451ad57 Mon Sep 17 00:00:00 2001 From: Brandon Rozek Date: Fri, 28 Sep 2018 12:05:06 -0400 Subject: [PATCH] Replaced make_node with a Node constructor --- src/operations/node.cpp | 20 ----------------- src/operations/node.hpp | 14 +++++++++--- src/parser/lexer.l | 12 +++++------ src/parser/parser.y | 48 ++++++++++++++++++++--------------------- 4 files changed, 41 insertions(+), 53 deletions(-) diff --git a/src/operations/node.cpp b/src/operations/node.cpp index a0ea5d5..d6b2661 100644 --- a/src/operations/node.cpp +++ b/src/operations/node.cpp @@ -7,26 +7,6 @@ #include "../variables/value.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 */ void attach_node(struct Node* parent, struct Node* child) { /* connect it */ diff --git a/src/operations/node.hpp b/src/operations/node.hpp index 7c7a3d9..8035ac3 100644 --- a/src/operations/node.hpp +++ b/src/operations/node.hpp @@ -6,7 +6,6 @@ #include "../variables/value.hpp" #include "../variables/environment.hpp" -#define ID_SIZE 100 #define MAX_CHILDREN 3 /* a tree node definition */ @@ -15,15 +14,24 @@ struct Node { struct Value* value; /* the id of the node (used for identifiers only) */ - char id[ID_SIZE]; + std::string id; /* at most three children nodes */ int num_children; std::array 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 -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 print_tree(struct Node* node, int tabs); void delete_tree(struct Node* node); diff --git a/src/parser/lexer.l b/src/parser/lexer.l index 1114940..fa43e3d 100644 --- a/src/parser/lexer.l +++ b/src/parser/lexer.l @@ -43,12 +43,12 @@ DIGIT [0-9] "input" {return INPUT;} "lambda" {return LAMBDA;} ":" {return COLON;} -"true" {yylval.value = make_node(VALUE, make_true(), ""); return VALUE;} -"false" {yylval.value = make_node(VALUE, make_false(), ""); return VALUE;} -\".*\" {yylval.value = make_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}*"."?{DIGIT}+ {yylval.value = make_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;} +"true" {yylval.value = new Node(VALUE, make_true(), ""); return VALUE;} +"false" {yylval.value = new Node(VALUE, make_false(), ""); return VALUE;} +\".*\" {yylval.value = new Node(VALUE, make_string(substring(yytext, 1, strlen(yytext) - 1)), ""); return VALUE; } +{DIGIT} {yylval.value = new Node(VALUE, make_long(atoi(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 = new Node(IDENTIFIER, nullptr, strdup(yytext)); return IDENTIFIER;} [\n] {linenum++;} [ \t\r]+ {} . {printf("Error: invlaid lexeme '%s'.\n", yytext); return 0;} diff --git a/src/parser/parser.y b/src/parser/parser.y index c164f56..a8d5e15 100644 --- a/src/parser/parser.y +++ b/src/parser/parser.y @@ -65,32 +65,32 @@ statement: assignment { $$ = $1; } | statements { $$ = $1; } assignment: ident ASSIGN exprlambda SEMICOLON { - $$ = make_node(ASSIGN, NULL, ""); + $$ = new Node(ASSIGN, NULL, ""); attach_node($$, $1); attach_node($$, $3); } if-statement: IF expression THEN statement { - $$ = make_node(IF, NULL, ""); + $$ = new Node(IF, NULL, ""); attach_node($$, $2); attach_node($$, $4); } if-else-statement: IF expression THEN statement ELSE statement { - $$ = make_node(IF, NULL, ""); + $$ = new Node(IF, NULL, ""); attach_node($$, $2); attach_node($$, $4); attach_node($$, $6); } while: WHILE expression DO statement { - $$ = make_node(WHILE, NULL, ""); + $$ = new Node(WHILE, NULL, ""); attach_node($$, $2); attach_node($$, $4); } print: PRINT exprlambda SEMICOLON { - $$ = make_node(PRINT, NULL, ""); + $$ = new Node(PRINT, NULL, ""); attach_node($$, $2); } @@ -98,41 +98,41 @@ print: PRINT exprlambda SEMICOLON { statements: BEGINTOK substatements END { $$ = $2; } | BEGINTOK END {} -substatements: statement substatements {$$ = make_node(STATEMENT, NULL, ""); attach_node($$, $1); attach_node($$, $2); } - | statement {$$ = make_node(STATEMENT, NULL, ""); attach_node($$, $1); } +substatements: statement substatements {$$ = new Node(STATEMENT, NULL, ""); attach_node($$, $1); attach_node($$, $2); } + | statement {$$ = new Node(STATEMENT, NULL, ""); attach_node($$, $1); } exprlambda: LAMBDATAG ident COLON expression { // Only supports one argument functions for now - $$ = make_node(LAMBDATAG, NULL, ""); + $$ = new Node(LAMBDATAG, NULL, ""); attach_node($$, $2); attach_node($$, $4); } | expression { $$ = $1; } -expression: expression OR subexpression { $$ = make_node(OR, NULL, ""); attach_node($$, $1); attach_node($$, $3);} - | expression AND subexpression { $$ = make_node(AND, NULL, ""); attach_node($$, $1); attach_node($$, $3);} +expression: expression OR subexpression { $$ = new Node(OR, NULL, ""); attach_node($$, $1); attach_node($$, $3);} + | expression AND subexpression { $$ = new Node(AND, NULL, ""); attach_node($$, $1); attach_node($$, $3);} | subexpression { $$ = $1; } -subexpression: subexpression LESS term { $$ = make_node(LESS, NULL, ""); attach_node($$, $1); attach_node($$, $3);} - | subexpression LESSEQ term { $$ = make_node(LESSEQ, NULL, ""); attach_node($$, $1); attach_node($$, $3);} - | subexpression GREATER term { $$ = make_node(GREATER, NULL, ""); attach_node($$, $1); attach_node($$, $3);} - | subexpression GREATEREQ term { $$ = make_node(GREATEREQ, NULL, ""); attach_node($$, $1); attach_node($$, $3);} - | subexpression EQUALS term { $$ = make_node(EQUALS, NULL, ""); attach_node($$, $1); attach_node($$, $3);} - | subexpression NEQUALS term { $$ = make_node(NEQUALS, NULL, ""); attach_node($$, $1); attach_node($$, $3);} +subexpression: subexpression LESS term { $$ = new Node(LESS, NULL, ""); attach_node($$, $1); attach_node($$, $3);} + | subexpression LESSEQ term { $$ = new Node(LESSEQ, NULL, ""); attach_node($$, $1); attach_node($$, $3);} + | subexpression GREATER term { $$ = new Node(GREATER, NULL, ""); attach_node($$, $1); attach_node($$, $3);} + | subexpression GREATEREQ term { $$ = new Node(GREATEREQ, NULL, ""); attach_node($$, $1); attach_node($$, $3);} + | subexpression EQUALS term { $$ = new Node(EQUALS, NULL, ""); attach_node($$, $1); attach_node($$, $3);} + | subexpression NEQUALS term { $$ = new Node(NEQUALS, NULL, ""); attach_node($$, $1); attach_node($$, $3);} | term { $$ = $1; } -term : term PLUS subterm { $$ = make_node(PLUS, NULL, ""); attach_node($$, $1); attach_node($$, $3);} - | term MINUS subterm { $$ = make_node(MINUS, NULL, ""); attach_node($$, $1); attach_node($$, $3);} +term : term PLUS subterm { $$ = new Node(PLUS, NULL, ""); attach_node($$, $1); attach_node($$, $3);} + | term MINUS subterm { $$ = new Node(MINUS, NULL, ""); attach_node($$, $1); attach_node($$, $3);} | subterm { $$ = $1; } -subterm: subterm TIMES factor { $$ = make_node(TIMES, NULL, ""); attach_node($$, $1); attach_node($$, $3);} - | subterm DIVIDE factor { $$ = make_node(DIVIDE, NULL, ""); attach_node($$, $1); attach_node($$, $3);} +subterm: subterm TIMES factor { $$ = new Node(TIMES, NULL, ""); attach_node($$, $1); attach_node($$, $3);} + | subterm DIVIDE factor { $$ = new Node(DIVIDE, NULL, ""); attach_node($$, $1); attach_node($$, $3);} | factor { $$ = $1; } -factor : MINUS factor { $$ = make_node(MINUS, NULL, ""); attach_node($$, $2); } - | NOT factor { $$ = make_node(NOT, NULL, ""); attach_node($$, $2); } +factor : MINUS factor { $$ = new Node(MINUS, NULL, ""); attach_node($$, $2); } + | NOT factor { $$ = new Node(NOT, NULL, ""); attach_node($$, $2); } | 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; } | callfunc { $$ = $1; } @@ -142,7 +142,7 @@ ident: IDENTIFIER { $$ = $1; } identvalue: ident { $$ = $1; } | VALUE { $$ = $1; } - | INPUT { $$ = make_node(INPUT, NULL , ""); } + | INPUT { $$ = new Node(INPUT, NULL , ""); }