diff --git a/src/Makefile b/src/Makefile index 889833a..30b7fe8 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,6 +1,7 @@ .PHONY: all clean all: deelang CC = g++ +CPPFLAGS = -g lexer.o lexer.h: parser.h lexer.l flex -o lexer.cpp --header-file=lexer.h lexer.l @@ -13,7 +14,7 @@ parser.h parser.o: parser.y rm parser.cpp deelang: deelang.cpp lexer.o parser.o syntax.o - $(CC) -o $@ $^ + $(CC) $(CPPFLAGS) -o $@ $^ clean: rm -rf *.o parser.h lexer.h deelang diff --git a/src/deelang.cpp b/src/deelang.cpp index f9e3219..04a11ab 100644 --- a/src/deelang.cpp +++ b/src/deelang.cpp @@ -5,7 +5,6 @@ using namespace std; int main() { - yydebug = 1; yyparse(); return 0; diff --git a/src/parser.y b/src/parser.y index 58b58f0..0dcc3bb 100644 --- a/src/parser.y +++ b/src/parser.y @@ -1,6 +1,8 @@ %{ -#include +#include + #include "syntax.h" + using namespace std; int yylex(); @@ -12,7 +14,8 @@ void yyerror(const char* p) { fprintf(stderr, p); } %union { float num; char *sym; - Node *node; + Node *expr; + NodeList *exprlist; } %token ID @@ -26,10 +29,11 @@ void yyerror(const char* p) { fprintf(stderr, p); } %right GETS %left CAT %left '+' '-' -%left '/' '*' +%left '/' '*'n %left MAPS -%nterm expr +%nterm expr +%nterm exprlist %% program: statements | statements statement; @@ -55,8 +59,10 @@ expr: funcdef funcdef: param MAPS expr | param MAPS block; param: ID | '_' | // null production; -funcall: ID '(' exprlist ')' -exprlist: exprlist ',' expr | expr | // null production; +funcall: ID '(' exprlist ')' { print_expression_list($3); } +exprlist: exprlist ',' expr { $1 -> push_back($3); $$ = $1; } + | expr { $$ = new NodeList(); $$ -> push_back($1); } + | { $$ = NULL; } // null production; conditional: IF expr block elifs | IF expr block elifs ELSE block; diff --git a/src/syntax.cpp b/src/syntax.cpp index f79afcb..2272fb2 100644 --- a/src/syntax.cpp +++ b/src/syntax.cpp @@ -1,18 +1,33 @@ -#include -#include +#include +#include +#include #include "syntax.h" +using namespace std; + IdNode::IdNode(char * id) { - this->id = id; + this->id = string(id); +} + +void IdNode::display() { + cout << id; } NumNode::NumNode(float num) { this->num = num; } +void NumNode::display() { + +} + StringNode::StringNode(char *str) { - this->string = string; + this->stringVal = str; +} + +void StringNode::display() { + } OpNode::OpNode(int op, Node *first, Node *second) { @@ -20,3 +35,13 @@ OpNode::OpNode(int op, Node *first, Node *second) { this->first = first; this->second = second; } + +void OpNode::display() { + +} + +void print_expression_list(NodeList *list) { + for (Node *expr : *list) { + expr->display(); + } +} diff --git a/src/syntax.h b/src/syntax.h index 55a57af..fffa45a 100644 --- a/src/syntax.h +++ b/src/syntax.h @@ -1,13 +1,20 @@ #ifndef SYNTAX_H #define SYNTAX_H -class Node {}; +#include +#include + +class Node { +public: + virtual void display() = 0; +}; class IdNode : public Node { private: - char* id; + std::string id; public: IdNode(char*); + virtual void display(); }; class NumNode : public Node { @@ -15,13 +22,15 @@ private: float num; public: NumNode(float); + virtual void display(); }; class StringNode : public Node { private: - char* string; + char* stringVal; public: StringNode(char*); + virtual void display(); }; class OpNode : public Node { @@ -31,7 +40,11 @@ private: Node *second; public: OpNode(int, Node*, Node*); + virtual void display(); }; +class NodeList : public std::vector{}; + +void print_expression_list(NodeList *); #endif /* SYNTAX_H */