From 9dfa6993fbd364ad9357a7e5b0cd4e88aaeaf1df Mon Sep 17 00:00:00 2001 From: Philippe Tillet Date: Mon, 17 Dec 2018 10:43:49 -0500 Subject: [PATCH] TDL [codegen]: added basic structure --- CMakeLists.txt | 2 +- examples/matrix.cpp | 2 +- include/ast.h | 58 ++++++++++++++++++++++++++++++--------------- include/parser.y | 7 +++--- include/scanner.l | 2 -- lib/codegen.cpp | 14 +++++++++++ 6 files changed, 59 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 256bbcf9c..b20429946 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,7 +18,7 @@ if(NOT CMAKE_BUILD_TYPE) endif() # Gather headers for cmake-based IDEs -file( GLOB_RECURSE ALL_SRC *.cpp *.hpp *.h *.py) +file( GLOB_RECURSE ALL_SRC *.cpp *.hpp *.h *.py *.y *.l) add_custom_target( ALL SOURCES ${ALL_SRC} ) # Compiler flags diff --git a/examples/matrix.cpp b/examples/matrix.cpp index cd46cc008..8d74b27b6 100644 --- a/examples/matrix.cpp +++ b/examples/matrix.cpp @@ -6,7 +6,7 @@ typedef struct yy_buffer_state * YY_BUFFER_STATE; extern int yyparse(); extern YY_BUFFER_STATE yy_scan_string(const char * str); extern void yy_delete_buffer(YY_BUFFER_STATE buffer); -using ast::translation_unit; +using tdl::ast::translation_unit; extern translation_unit *ast_root; const char src[] = diff --git a/include/ast.h b/include/ast.h index 3e1544264..48e2f7852 100644 --- a/include/ast.h +++ b/include/ast.h @@ -1,8 +1,15 @@ +#ifndef TDL_INCLUDE_AST_H +#define TDL_INCLUDE_AST_H + #include "parser.hpp" #include #include #include +namespace tdl{ + +class module; + namespace ast{ // Enumerations @@ -41,13 +48,18 @@ enum TYPE_T{ }; // AST -class node { }; +class node { +public: + virtual void codegen(module*) { } +}; template class list: public node { public: list(const T& x): values_{x} {} node* append(const T& x) { values_.push_back(x); return this;} + void codegen(module* mod) { for(T x: values_){ x->codegen(mod); } } + const std::list &values() const { return values_; } private: std::list values_; @@ -224,10 +236,10 @@ public: class function_declarator: public declarator{ public: function_declarator(node *args) - : args_((list)args) { } + : args_((list*)args) { } public: - const list args_; + const list* args_; }; class compound_declarator: public declarator{ @@ -261,28 +273,36 @@ public: const node *decl_; }; -class translation_unit: public node{ -public: - translation_unit(node *item) - : decls_(item) { } - - translation_unit *add(node *item) { - decls_.append(item); - return this; - } - -private: - list decls_; -}; - +/* Function definition */ class function_definition: public node{ public: function_definition(node *header, node *body) - : header_((declarator *)header), body_((compound_statement*)body) { } + : header_((function_declarator *)header), body_((compound_statement*)body) { } public: - const declarator *header_; + const function_declarator *header_; const compound_statement *body_; }; +/* Translation Unit */ +class translation_unit: public node{ +public: + translation_unit(node *item) + : decls_((list*)item) { } + + translation_unit *add(node *item) { + decls_->append(item); + return this; + } + + void codegen(module* mod); + +private: + list* decls_; +}; + } + +} + +#endif diff --git a/include/parser.y b/include/parser.y index 40153c9c4..73ef31ad6 100644 --- a/include/parser.y +++ b/include/parser.y @@ -1,11 +1,12 @@ %{ +namespace tdl{ namespace ast{ class node; } -using namespace ast; +} +using namespace tdl::ast; #define YYSTYPE node* #include "../include/ast.h" -using namespace ast; extern char* yytext; void yyerror(const char *s); @@ -111,7 +112,7 @@ primary_expression : identifier { $$ = $1; } | constant { $$ = $1; } | STRING_LITERAL { $$ = new string_literal(yytext); } - | '(' unary_expression ')' { $$ = $1; } + | '(' expression ')' { $$ = $1; } ; unary_expression diff --git a/include/scanner.l b/include/scanner.l index 394cca7c4..df730aec8 100644 --- a/include/scanner.l +++ b/include/scanner.l @@ -113,8 +113,6 @@ void count() column += 8 - (column % 8); else column++; - - ECHO; } void yyerror (const char *s) /* Called by yyparse on error */ diff --git a/lib/codegen.cpp b/lib/codegen.cpp index e69de29bb..f3d61d59c 100644 --- a/lib/codegen.cpp +++ b/lib/codegen.cpp @@ -0,0 +1,14 @@ +#include "codegen.h" +#include "ast.h" + +namespace tdl{ + +namespace ast{ + +void translation_unit::codegen(module *mod) +{ decls_->codegen(mod); } + + +} + +}