]> git.mdlowis.com Git - archive/dlang.git/commitdiff
Refactored and fixed some memory leaks
authorMike D. Lowis <mike@mdlowis.com>
Sun, 26 Feb 2012 19:29:04 +0000 (14:29 -0500)
committerMike D. Lowis <mike@mdlowis.com>
Sun, 26 Feb 2012 19:29:04 +0000 (14:29 -0500)
19 files changed:
deps/cork
example.dl
rakefile.rb
source/cork/cork.cpp [deleted file]
source/cork/cork.h [deleted file]
source/dllexer/dllexer.cpp
source/dllexer/dllexer.h
source/dlparser/dlparser.cpp
source/dlparser/dlparser.h
source/dlparser/macro/macro.c
source/main.cpp
source/parse_utils/lexer/ilexer.h
source/parse_utils/lexer/token/token.cpp
source/parse_utils/lexer/token/token.h
source/parse_utils/parser/btparser/btparser.cpp
source/parse_utils/parser/btparser/btparser.h
source/parse_utils/parser/iparser.h
source/parse_utils/parser/llkparser/llkparser.cpp
source/parse_utils/parser/llkparser/llkparser.h

index 244613ffd2288f8d51ecab2c6743f1a1c28d6537..dbd1bb9634cee2c271477bb32b8556f017be5559 160000 (submodule)
--- a/deps/cork
+++ b/deps/cork
@@ -1 +1 @@
-Subproject commit 244613ffd2288f8d51ecab2c6743f1a1c28d6537
+Subproject commit dbd1bb9634cee2c271477bb32b8556f017be5559
index cb0140047ddee0862472125f55b45ca721656c7e..785218fe8fdc143f838ba1bea4418f75071fdfea 100644 (file)
@@ -2,81 +2,81 @@
 # Macro Tests
 #------------------------------------------------------------------------------
 # Macro Definitions
-% if ( cond, branch1:Block ) {
-       exec_if( cond, branch1 )
-}
-
-% ifelse ( cond, branch1:Block, branch2:Block ) {
-       exec_if( cond, branch1, branch2 )
-}
-
-% foreach ( lst, fn:Block ) {
-       for_each(fn, lst)
-}
+#% if ( cond, branch1:Block ) {
+#    exec_if( cond, branch1 )
+#}
+#
+#% ifelse ( cond, branch1:Block, branch2:Block ) {
+#    exec_if( cond, branch1, branch2 )
+#}
+#
+#% foreach ( lst, fn:Block ) {
+#    for_each(fn, lst)
+#}
 
 # Macro Uses
-if (1 == 1)
-{
-       print("1 Hello, World!")
-}
-
-if (1 == 0)
-{
-       print("Hello, World!")
-}
-
-ifelse (1 == 1)
-{
-       print("2 Hello, World!")
-}{
-       error("Incorrect branch was taken.")
-}
-
-ifelse (1 == 0)
-{
-       error("Incorrect branch was taken.")
-}{
-       print("3 Hello, World!")
-}
+#if (1 == 1)
+#{
+#    print("1 Hello, World!")
+#}
+#
+#if (1 == 0)
+#{
+#    print("Hello, World!")
+#}
+#
+#ifelse (1 == 1)
+#{
+#    print("2 Hello, World!")
+#}{
+#    error("Incorrect branch was taken.")
+#}
+#
+#ifelse (1 == 0)
+#{
+#    error("Incorrect branch was taken.")
+#}{
+#    print("3 Hello, World!")
+#}
 
 #------------------------------------------------------------------------------
 # Collection Access and Iteration Tests
 #------------------------------------------------------------------------------
 # Accessing elements of lists, vectors, and strings
-lst1 = `(4,5,6)
-print(lst1[0], " Hello, World!")
-
-vec1 = [4,5,6]
-print(vec1[1], " Hello, World!")
-
-str1 = "456"
-print(str1[2], " Hello, World!")
-
-# Iterating over lists, vectors, and strings
-lst_foo = `(7,8,9,10)
-foreach lst_foo { |a|
-       print(a," Hello, World!")
-}
-
-vec_foo = [11,12,13,14,15]
-foreach vec_foo { |a,b|
-       print(b," Hello, World!")
-}
-
-str_foo = "6789"
-foreach str_foo { |a|
-       print(1,a," Hello, World!")
-}
+#lst1 = `(4,5,6)
+#print(lst1[0], " Hello, World!")
+#
+#vec1 = [4,5,6]
+#print(vec1[1], " Hello, World!")
+#
+#str1 = "456"
+#print(str1[2], " Hello, World!")
+#
+## Iterating over lists, vectors, and strings
+#lst_foo = `(7,8,9,10)
+#foreach lst_foo { |a|
+#    print(a," Hello, World!")
+#}
+#
+#vec_foo = [11,12,13,14,15]
+#foreach vec_foo { |a,b|
+#    print(b," Hello, World!")
+#}
+#
+#str_foo = "6789"
+#foreach str_foo { |a|
+#    print(1,a," Hello, World!")
+#}
 
 #------------------------------------------------------------------------------
 # Delayed Evaluation
 #------------------------------------------------------------------------------
 % delay ( exp ) {
-       make_promise({ exp })
+    make_promise({ exp })
 }
 
 % force ( prom ) {
-       force( prom )
+    force( prom )
 }
 
 foo = delay nonexistent_var + 1
index 0b374aa3c8b2499b753608f6ba2ef787f1454c5f..4348f0af38c04570fb39db622918f47f5df9b560 100644 (file)
@@ -10,9 +10,13 @@ DLangParser = Binary.new({
     :name => 'dlang',
     :output_dir => 'build/parser',
     :compiler_options => [ '-c', '-Wall', '-Werror', '-o' ],
+    :static_libs => [ './deps/cork/build/static/bin/libcork.a' ],
     :source_files => [ 'source/**/*.c*' ],
-    :include_dirs => [ 'source/**/' ],
-    :preprocessor_defines => [ 'DETECT_MEM_LEAKS' ]
+    :include_dirs => [
+        'source/**/',
+        'deps/cork/source/**/'
+    ],
+    #:preprocessor_defines => [ 'DETECT_MEM_LEAKS' ]
 })
 DLangParser.setup_default_rake_tasks()
 
diff --git a/source/cork/cork.cpp b/source/cork/cork.cpp
deleted file mode 100644 (file)
index 0a298fa..0000000
+++ /dev/null
@@ -1,183 +0,0 @@
-#include "cork.h"
-
-#ifdef DETECT_MEM_LEAKS
-
-// We want to use the real malloc and free in this file
-#undef malloc
-#undef free
-
-#include <iostream>
-#include <exception> // for std::bad_alloc
-#include <cstdlib> // for malloc() and free()
-#include <string.h>
-
-// Set the namespace
-using namespace std;
-/******************************************************************************
- * Typedefs
- *****************************************************************************/
-typedef struct BlockTableEntry
-{
-    void * ptr;
-    unsigned int size;
-    const char* file;
-    int line;
-    void * next;
-} BlockTableEntry_T;
-
-typedef struct BlockTable
-{
-    unsigned int size;
-    BlockTableEntry_T* blocks[TBL_SIZE];
-} BlockTable_T;
-
-/******************************************************************************
- * Prototypes
- *****************************************************************************/
-void insert_record(void * ptr, BlockTable_T* entry);
-void erase_record(void * ptr);
-
-/******************************************************************************
- * Globals
- *****************************************************************************/
-unsigned int allocated;
-static BlockTable_T Block_Table = { 0, {0} };
-
-/******************************************************************************
- * Function Definitions
- *****************************************************************************/
-void insert_record(void * ptr, BlockTableEntry_T* entry)
-{
-    unsigned int index = ((unsigned int)ptr) % TBL_SIZE;
-    BlockTableEntry_T* last  = Block_Table.blocks[ index ];
-    BlockTableEntry_T* curr  = last;
-
-    while (curr != NULL)
-    {
-        if ( curr->ptr == ptr )
-        {
-            curr->size = entry->size;
-            free(entry);
-            break;
-        }
-        last = curr;
-        curr = (BlockTableEntry_T*)curr->next;
-    }
-
-    if(curr == NULL)
-    {
-        if (last != NULL)
-        {
-            last->next = entry;
-        }
-        else
-        {
-            Block_Table.blocks[index] = entry;
-        }
-        Block_Table.size++;
-    }
-}
-
-void erase_record(void * ptr)
-{
-    int depth = 0;
-    unsigned int index = ((unsigned int)ptr) % TBL_SIZE;
-    BlockTableEntry_T* last = Block_Table.blocks[ index ];
-    BlockTableEntry_T* curr = last;
-
-    while( curr != NULL )
-    {
-        depth = 1;
-        if( curr->ptr == ptr )
-        {
-            depth = 2;
-            if(last == curr)
-            {
-                depth = 3;
-                Block_Table.blocks[ index ] = (BlockTableEntry_T*)curr->next;
-            }
-            else
-            {
-                depth = 4;
-                last->next = curr->next;
-            }
-            free(curr);
-            Block_Table.size--;
-            break;
-        }
-        last = curr;
-        curr = (BlockTableEntry_T*)curr->next;
-    }
-}
-
-void Cork_ReportMemoryLeaks(void)
-{
-    unsigned int index = 0;
-    cout << "-----------------------------------------------------------------" << endl;
-    cout << "Cork: Memory Allocation Analysis" << endl;
-    cout << "-----------------------------------------------------------------" << endl;
-    cout << "You have " << Block_Table.size << " Unclaimed objects." << endl;
-
-    for(; index < TBL_SIZE; index++)
-    {
-        BlockTableEntry_T* entry = Block_Table.blocks[ index ];
-        while(entry != NULL)
-        {
-            cout << "\t" << entry->size << "\t" << entry->ptr;
-            if( entry->file != NULL )
-            {
-                cout << "\t" << entry->line << "\t" << entry->file;
-            }
-            cout << endl;
-            entry = (BlockTableEntry_T*)entry->next;
-        }
-    }
-}
-
-void * operator new (size_t size, string file, unsigned int line)
-{
-    void * ptr = malloc(size);
-    char * fname = (char*)malloc(file.length());
-    if(ptr == NULL)
-    {
-        throw bad_alloc();
-    }
-    else
-    {
-        BlockTableEntry_T* entry = (BlockTableEntry_T*)malloc(sizeof(BlockTableEntry_T));
-        strcpy( fname, file.c_str() );
-        entry->ptr = ptr;
-        entry->size = size;
-        entry->file = fname;
-        entry->line = line;
-        entry->next = NULL;
-        insert_record(ptr,entry);
-    }
-    return ptr;
-}
-
-void * operator new(size_t size) throw(bad_alloc) {
-    void * ptr = malloc(size);
-    if(ptr == NULL)
-    {
-        throw bad_alloc();
-    }
-    else
-    {
-        BlockTableEntry_T* entry = (BlockTableEntry_T*)malloc(sizeof(BlockTableEntry_T));
-        entry->ptr = ptr;
-        entry->size = size;
-        entry->file = NULL;
-        entry->line = 0;
-        entry->next = NULL;
-        insert_record(ptr,entry);
-    }
-    return ptr;
-}
-
-void operator delete(void * p) {
-    free(p);
-    erase_record(p);
-}
-
-#endif
diff --git a/source/cork/cork.h b/source/cork/cork.h
deleted file mode 100644 (file)
index 6af4f20..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-#ifndef CORK_H
-#define CORK_H 
-
-#ifdef DETECT_MEM_LEAKS
-    #include <string>
-    typedef unsigned int size_t;
-
-    void Cork_ReportMemoryLeaks(void);
-    void * operator new (size_t size, std::string file, unsigned int line);
-    #define TBL_SIZE       512
-    #define REPORT_LEAKS() Cork_ReportMemoryLeaks()
-    #define _new           new (__FILE__,__LINE__)
-#else 
-    #define REPORT_LEAKS()
-    #define _new new
-#endif
-
-#endif
index 48f0773bb80c25686274e2b5a48dab9126b12538..83c9b3acd51b1c8ff5de3d73b3d984194eadfffa 100644 (file)
@@ -58,10 +58,11 @@ bool DLLexer::isStringChar(void)
              && (current != '\n'));
 }
 
-Token* DLLexer::next(void)
+Token DLLexer::next(void)
 {
-    Token* ret = NULL;
-    while ( (!input->eof()) && (ret == NULL) )
+    Token ret;
+    Token* temp = NULL;
+    while ( (!input->eof()) && (temp == NULL) )
     {
         if (isWhiteSpace())
         {
@@ -73,33 +74,40 @@ Token* DLLexer::next(void)
         }
         else if (isLetter())
         {
-            ret = Id();
+            temp = Id();
         }
         else if( isOperator() )
         {
-            ret = MultiCharOp();
+            temp = MultiCharOp();
         }
         else if (isDigit())
         {
-            ret = Number();
+            temp = Number();
         }
         else if(current == '\'')
         {
-            ret = Char();
+            temp = Char();
         }
         else if(current == '"')
         {
-            ret = String();
+            temp = String();
         }
         else if(current == '$')
         {
-            ret = Symbol();
+            temp = Symbol();
         }
         else
         {
-            ret = SingleCharOp();
+            temp = SingleCharOp();
         }
     }
+
+    if(temp !=  NULL)
+    {
+        ret = *(temp);
+        delete temp;
+    }
+
     return ret;
 }
 
index e27bfe85967a35803bd17bb8f704426d4675d605..e93e7ad40a31741261b57b535b0cadd7858f5f4b 100644 (file)
@@ -67,7 +67,7 @@ class DLLexer : public ILexer {
         void WS(void);
         void COMMENT(void);
 
-        Token* next(void);
+        Token next(void);
         Token* Id(void);
         Token* Number(void);
         Token* Decimal(std::ostringstream& oss);
index f7f547d1a30129bb538ef12c4da04c0b90efe28f..7692b8db38a9e64769f5434782860bcb4b663dc0 100644 (file)
@@ -2,24 +2,27 @@
 #include "exception.h"
 #include "cork.h"
 
-DLParser::DLParser() : BTParser(_new DLLexer()), ast(NULL)
+DLParser::DLParser() : BTParser(_new DLLexer())
 {
 }
 
-void DLParser::parse(void)
+DLParser::~DLParser()
 {
-    ast = Program();
+    std::map<std::string,Macro*>::iterator iter;
+    for (iter = macros.begin(); iter != macros.end(); ++iter) {
+        delete (iter->second);
+    }
 }
 
-AST* DLParser::getAST(void)
+AST* DLParser::parse(void)
 {
-    return ast;
+    return Program();
 }
 
-bool DLParser::isMacro( Token* token )
+bool DLParser::isMacro( Token& token )
 {
     bool ret = false;
-    if( (token->type() == ID) && (macros.find(token->text()) != macros.end()) )
+    if( (token.type() == ID) && (macros.find(token.text()) != macros.end()) )
     {
         ret = true;
     }
@@ -40,10 +43,10 @@ AST* DLParser::parseMacroParam(Param* param)
             break;
 
         default:
-            Token* tok = lookaheadToken(1);
+            Token& tok = lookaheadToken(1);
             ostringstream oss;
-            oss << "Expected macro parameter type. Expected " << param->type() << ", received " << tok->type() << ".";
-            Exception ex( tok->line(), tok->column() );
+            oss << "Expected macro parameter type. Expected " << param->type() << ", received " << tok.type() << ".";
+            Exception ex( tok.line(), tok.column() );
             ex.setMessage(oss.str());
             break;
     }
@@ -65,7 +68,7 @@ AST* DLParser::Expression(void)
     AST* ret = NULL;
     if((lookaheadType(1) == ID) && (lookaheadType(2) == ASSIGN))
     {
-        AST* id_node = _new AST( ID,(char*)(lookaheadToken(1)->text().c_str()) );
+        AST* id_node = _new AST( ID,(char*)(lookaheadToken(1).text().c_str()) );
         consume();
         match(ASSIGN);
         ret = _new AST( ASSIGN, 2, id_node, Expression());
@@ -200,39 +203,39 @@ AST* DLParser::Literal(void)
 
         // Literal = ID
         case ID:
-            node = _new AST( ID, lookaheadToken(1)->text() );
+            node = _new AST( ID, lookaheadToken(1).text() );
             consume();
             break;
 
         // Literal = NUM
         case NUM:
-            node = _new AST( NUM, lookaheadToken(1)->text() );
+            node = _new AST( NUM, lookaheadToken(1).text() );
             consume();
             break;
 
         // Literal = CHAR
         case CHAR:
-            node = _new AST( CHAR, lookaheadToken(1)->text() );
+            node = _new AST( CHAR, lookaheadToken(1).text() );
             consume();
             break;
 
         // Literal = STRING
         case STRING:
-            node = _new AST( STRING, lookaheadToken(1)->text() );
+            node = _new AST( STRING, lookaheadToken(1).text() );
             consume();
             break;
 
         // Literal = SYMBOL
         case SYMBOL:
-            node = _new AST( SYMBOL, lookaheadToken(1)->text() );
+            node = _new AST( SYMBOL, lookaheadToken(1).text() );
             consume();
             break;
 
         default:
-            Token* tok = lookaheadToken(1);
+            Token& tok = lookaheadToken(1);
             ostringstream oss;
-            oss << "Expected literal type, recieved type " << tok->type() << ".";
-            Exception ex( tok->line(), tok->column() );
+            oss << "Expected literal type, recieved type " << tok.type() << ".";
+            Exception ex( tok.line(), tok.column() );
             ex.setMessage(oss.str());
             throw ex;
     }
@@ -288,7 +291,7 @@ AST* DLParser::MacroDefinition(void)
     Macro* macro = NULL;
 
     match(MACRO);
-    id = _new AST( ID, lookaheadToken(1)->text() );
+    id = _new AST( ID, lookaheadToken(1).text() );
     consume();
     match(LPAR);
     params = MacroParamList();
@@ -307,7 +310,7 @@ AST* DLParser::MacroDefinition(void)
 AST* DLParser::MacroExpansion(void)
 {
     AST* ret = NULL;
-    Macro* cur_macro = macros[ lookaheadToken(1)->text() ];
+    Macro* cur_macro = macros[ lookaheadToken(1).text() ];
     list<Param*>::const_iterator it = cur_macro->params().begin();
 
     consume();
@@ -336,12 +339,12 @@ AST* DLParser::MacroParamList(void)
 // MacroParam = ID (':' ID)?
 AST* DLParser::MacroParam(void)
 {
-    AST* ret = _new AST( ID, lookaheadToken(1)->text() );
+    AST* ret = _new AST( ID, lookaheadToken(1).text() );
     consume();
     if( lookaheadType(1) == SEP )
     {
         match(SEP);
-        AST* type = _new AST( ID, lookaheadToken(1)->text() );
+        AST* type = _new AST( ID, lookaheadToken(1).text() );
         consume();
         ret = _new AST(SEP, 2, ret, type);
     }
index d00d1ddc1cbb5e603cdb4ddc01ab150c97757613..5ad2487847a45861e09659c0d7ec341fd2ddcdbb 100644 (file)
@@ -1,5 +1,5 @@
 #ifndef DLPARSER_H
-#define DLPARSER_H 
+#define DLPARSER_H
 
 #include <map>
 #include "btparser.h"
@@ -8,85 +8,84 @@
 
 class DLParser : public BTParser
 {
-       private:
-               AST* ast;
-               std::map<std::string,Macro*> macros;
-       public:
-               DLParser();
-               void parse(void);
-               AST* getAST(void);
-               bool isMacro(Token* token);
-               AST* parseMacroParam(Param* param);
-               
-               /**********************************************************************
-                * EBNF Syntax Grammar
-                *********************************************************************/
-               // Program    = Expression*
-               //
-               // Expression = ID '=' LogicalExpr
-               //            | MacroDefinition
-               //            | $MacroExpansion$
-               //            | LogicalExpr
-               //
-               // LogicalExpr = CompExpr (('&&' | '||') CompExpr)*
-               //
-               // CompExpr = AddSubExpr (('==' | '!=' | '<' | '>' | '<=' | '>=') AddSubExpr)*
-               //
-               // AddSubExpr = MulDivExpr (('+' | '-') MulDivExpr)*
-               //
-               // MulDivExpr = UnaryExpr (('*' | '/') UnaryExpr)*
-               //
-               // UnaryExpr = '!' GroupExpr
-               //           | GroupExpr
-               //            
-               // GroupExpr = '(' LogicalExpr ')'
-               //           | Literal '(' ExpList ')'
-               //           | Literal
-               //
+    private:
+        std::map<std::string,Macro*> macros;
+    public:
+        DLParser();
+        ~DLParser();
+        AST* parse(void);
+        bool isMacro(Token& token);
+        AST* parseMacroParam(Param* param);
+
+        /**********************************************************************
+         * EBNF Syntax Grammar
+         *********************************************************************/
+        // Program    = Expression*
+        //
+        // Expression = ID '=' LogicalExpr
+        //            | MacroDefinition
+        //            | $MacroExpansion$
+        //            | LogicalExpr
+        //
+        // LogicalExpr = CompExpr (('&&' | '||') CompExpr)*
+        //
+        // CompExpr = AddSubExpr (('==' | '!=' | '<' | '>' | '<=' | '>=') AddSubExpr)*
+        //
+        // AddSubExpr = MulDivExpr (('+' | '-') MulDivExpr)*
+        //
+        // MulDivExpr = UnaryExpr (('*' | '/') UnaryExpr)*
+        //
+        // UnaryExpr = '!' GroupExpr
+        //           | GroupExpr
+        //
+        // GroupExpr = '(' LogicalExpr ')'
+        //           | Literal '(' ExpList ')'
+        //           | Literal
+        //
         // Literal    = VectorLiteral
-               //            | ListLiteral
-               //            | FuncLiteral
-               //            | ID
-               //            | NUM
-               //            | CHAR
-               //            | STRING
-               //            | SYMBOL
-               //
-               // VectorLiteral =  '[' ExpList ']'
-               //
-               // ListLiteral = '`' '(' ExpList ')'
-               //
-               // FuncLiteral = '{' ExpBlock '}'
-               //             |  '{' '|' ExpList '|' ExpBlock '}'
-               //
-               // MacroDefinition = '%' ID '(' MacroParamList ')' '{' Expression '}'
-               //
-               // MacroParamList = MacroParam (',' MacroParam)* 
-               //
-               // MacroParam = ID (':' ID)?
-               //
-               // ExpList    = (GroupExpr (',' GroupExpr)*)?
-               //
-               // ExpBlock   = Expression*
-       private:
-               AST* Program(void);
-               AST* Expression(void);
-               AST* LogicalExpr(void);
-               AST* CompExpr(void);
-               AST* AddSubExpr(void);
-               AST* MulDivExpr(void);
-               AST* UnaryExpr(void);
-               AST* GroupExpr(void);
-               AST* Literal(void);
-               AST* VectorLiteral(void);
-               AST* ListLiteral(void);
-               AST* FuncLiteral(void);
-               AST* MacroDefinition(void);
-               AST* MacroExpansion(void);
-               AST* MacroParamList(void);
-               AST* MacroParam(void);
-               AST* ExpList(TokenType_T node_type, TokenType_T terminator);
-               AST* ExpBlock(TokenType_T node_type, TokenType_T terminator);
+        //            | ListLiteral
+        //            | FuncLiteral
+        //            | ID
+        //            | NUM
+        //            | CHAR
+        //            | STRING
+        //            | SYMBOL
+        //
+        // VectorLiteral =  '[' ExpList ']'
+        //
+        // ListLiteral = '`' '(' ExpList ')'
+        //
+        // FuncLiteral = '{' ExpBlock '}'
+        //             |  '{' '|' ExpList '|' ExpBlock '}'
+        //
+        // MacroDefinition = '%' ID '(' MacroParamList ')' '{' Expression '}'
+        //
+        // MacroParamList = MacroParam (',' MacroParam)*
+        //
+        // MacroParam = ID (':' ID)?
+        //
+        // ExpList    = (GroupExpr (',' GroupExpr)*)?
+        //
+        // ExpBlock   = Expression*
+    private:
+        AST* Program(void);
+        AST* Expression(void);
+        AST* LogicalExpr(void);
+        AST* CompExpr(void);
+        AST* AddSubExpr(void);
+        AST* MulDivExpr(void);
+        AST* UnaryExpr(void);
+        AST* GroupExpr(void);
+        AST* Literal(void);
+        AST* VectorLiteral(void);
+        AST* ListLiteral(void);
+        AST* FuncLiteral(void);
+        AST* MacroDefinition(void);
+        AST* MacroExpansion(void);
+        AST* MacroParamList(void);
+        AST* MacroParam(void);
+        AST* ExpList(TokenType_T node_type, TokenType_T terminator);
+        AST* ExpBlock(TokenType_T node_type, TokenType_T terminator);
 };
 
 #endif
index 0808661a294d4ec89e8bafcfffa30027b5ae1bdc..a01f8044f45c66ec96c53dc16871edd3da102570 100644 (file)
@@ -21,6 +21,11 @@ Macro::Macro(AST* macro_def)
 
 Macro::~Macro()
 {
+
+    std::list<Param*>::iterator iter;
+    for (iter = macro_params.begin(); iter != macro_params.end(); ++iter) {
+        delete *iter;
+    }
     delete macro_body;
 }
 
index 240e11573a129051d027b65704af6527d7170433..507ef355343839118cfdf2116d66eda4fe678135 100644 (file)
@@ -22,15 +22,19 @@ int main(int argc, char** argv)
         //string temp_fname = createTempFileName( input_fname );
         //ifstream input(input_fname.c_str());
         //DLLexer lexer;
+        //Token tok;
         //lexer.setInput(&input);
-        //lexer.next();
+        //while( tok.type() != EOF )
+        //{
+        //    tok = lexer.next();
+        //}
         //(void)temp_fname;
 
         string input_fname(argv[1]);
         string temp_fname = createTempFileName( input_fname );
         (void)temp_fname;
         DLParser parser;
-        Scheme* visitor = NULL;
+        //Scheme* visitor = NULL;
         AST* parse_tree = NULL;
 
         // Open the input and output files
@@ -39,11 +43,10 @@ int main(int argc, char** argv)
 
         // Parse the file
         parser.setInput(&input);
-        parser.parse();
-        parse_tree = parser.getAST();
+        parse_tree = parser.parse();
 
         // Translate the AST
-        visitor = _new Scheme( parse_tree );
+        //visitor = _new Scheme( parse_tree );
         //visitor->visit();
 
         // Write to a temp file
@@ -71,7 +74,7 @@ int main(int argc, char** argv)
         cerr << "Error: No input files." << endl;
     }
 
-    REPORT_LEAKS();
+    Cork_ReportMemoryLeaks();
 
     return ret;
 }
index 3b0494306879bd6f2c9cbf78c0d29926c0d2bb2a..530c07959c4be3621499e91b568c13f2d6c20deb 100644 (file)
@@ -26,7 +26,7 @@ class ILexer
         void setInput(std::string& in);
         void setInput(std::istream* in);
 
-        virtual Token* next(void) = 0;
+        virtual Token next(void) = 0;
 };
 
 #endif
index dab99b75b14d964d83a1200d287e2f53bd157418..ae8c2459299bd6a765f60e47bdf6dc6395d918ab 100644 (file)
@@ -1,5 +1,10 @@
+#include <stdio.h>
 #include "token.h"
 
+Token::Token() : tok_type(EOF), tok_text(""), tok_line(-1), tok_col(-1)
+{
+}
+
 Token::Token(TokenType_T ttype, std::string ttext, int line, int col) : tok_type(ttype), tok_text(ttext), tok_line(line), tok_col(col)
 {
 }
@@ -8,21 +13,41 @@ Token::Token(TokenType_T ttype, int line, int col) : tok_type(ttype), tok_line(l
 {
 }
 
+void Token::type(TokenType_T typ)
+{
+    tok_type = typ;
+}
+
 TokenType_T Token::type()
 {
     return tok_type;
 }
 
+void Token::text(std::string txt)
+{
+    tok_text = txt;
+}
+
 std::string Token::text()
 {
     return tok_text;
 }
 
+void Token::line(int ln)
+{
+    tok_line = ln;
+}
+
 int Token::line()
 {
     return tok_line;
 }
 
+void Token::column(int col)
+{
+    tok_col = col;
+}
+
 int Token::column()
 {
     return tok_col;
index 75f8a85dabbcaba5646b09d06a4516cf581f742d..8c5f0b697d643cd6a64f149c1d1f0837b3afd075 100644 (file)
@@ -13,11 +13,20 @@ class Token
         int tok_line;
         int tok_col;
     public:
+        Token();
         Token(TokenType_T ttype, int line, int col);
         Token(TokenType_T ttype, std::string ttext, int line, int col);
+
+        void type(TokenType_T typ);
         TokenType_T type();
+
+        void text(std::string txt);
         std::string text();
+
+        void line(int ln);
         int line();
+
+        void column(int col);
         int column();
 };
 
index 6aacba3f181c79e5577cefd3d20501984ea0e77a..9a01d439553c785c22c4aed08de722c27e66ebb3 100644 (file)
@@ -71,15 +71,15 @@ void BTParser::match(TokenType_T type)
     }
     else
     {
-        Token* tok = lookaheadToken(1);
+        Token& tok = lookaheadToken(1);
         ostringstream oss;
-        oss << "Expected token type. Expected " << type << ", received " << tok->type() << ".";
-        Exception ex( tok->line(), tok->column() );
+        oss << "Expected token type. Expected " << type << ", received " << tok.type() << ".";
+        Exception ex( tok.line(), tok.column() );
         ex.setMessage(oss.str());
     }
 }
 
-Token* BTParser::lookaheadToken(unsigned int i)
+Token& BTParser::lookaheadToken(unsigned int i)
 {
     sync(i);
     return lookahead.at( current + i - 1 );
@@ -87,9 +87,7 @@ Token* BTParser::lookaheadToken(unsigned int i)
 
 TokenType_T BTParser::lookaheadType(unsigned int i)
 {
-
-    Token* tok = lookaheadToken(i);
-    return (tok != NULL) ? tok->type() : EOF;
+    return lookaheadToken(i).type();
 }
 
 unsigned int BTParser::mark(void)
index ea77c39807304c845be436bd55604a4da4cbba63..5d94cb3832d182f9d7f675847eb25f79ed9213b8 100644 (file)
@@ -13,7 +13,7 @@ class BTParser : public IParser
         ILexer* lexer;
         unsigned int current;
         std::vector<unsigned int> markers;
-        std::vector<Token*> lookahead;
+        std::vector<Token> lookahead;
     public:
         BTParser(ILexer* lxer);
         ~BTParser();
@@ -26,14 +26,14 @@ class BTParser : public IParser
         void sync(unsigned int i);
         void fill(unsigned int n);
         void match(TokenType_T type);
-        Token* lookaheadToken(unsigned int i);
+        Token& lookaheadToken(unsigned int i);
         TokenType_T lookaheadType(unsigned int i);
         unsigned int mark(void);
         void release(void);
         void seek(unsigned int index);
         bool isSpeculating(void);
 
-        virtual void parse(void) = 0;
+        virtual AST* parse(void) = 0;
 };
 
 #endif
index 0c4d86c94f88f978b99e22b6e79c200612861c40..47b75c1fb33c1ac6510d8a632b5d66bc27735948 100644 (file)
@@ -30,7 +30,7 @@ class IParser {
     public:
         IParser();
         virtual ~IParser();
-        virtual void parse() = 0;
+        virtual AST* parse() = 0;
 
         void setInput(char* in);
         void setInput(string& in);
index 3568726487706842160635927dac30f272cd0fa2..4569dece833883eb69d000e02f770dbc46051a2f 100644 (file)
@@ -6,7 +6,7 @@ LLKParser::LLKParser(int k_val, ILexer* lxer) : k(k_val), next(0), lexer(lxer)
 {
     if ( lexer != NULL )
     {
-        lookahead = new Token*[k];
+        lookahead = new Token[k];
     }
     else
     {
@@ -79,13 +79,9 @@ void LLKParser::match(TokenType_T type)
     }
 }
 
-Token* LLKParser::lookaheadToken(int i)
+Token& LLKParser::lookaheadToken(int i)
 {
-    Token* ret = NULL;
-    if( lookahead != NULL )
-    {
-        ret = lookahead[(next + i - 1) % k];
-    }
+    Token& ret = lookahead[(next + i - 1) % k];
     return ret;
 }
 
@@ -94,8 +90,7 @@ TokenType_T LLKParser::lookaheadType(int i)
     TokenType_T ret = EOF;
     if( lookahead != NULL )
     {
-        Token* tok = lookaheadToken(i);
-        ret = (tok != NULL) ? tok->type() : EOF;
+        ret = lookaheadToken(i).type();
     }
     return ret;
 }
index 2e403b72e56d4e98f861d431fbccc3f6d9c565bc..8971cfa5a2b276f2370bfe1f5dfb57f145ed4ed1 100644 (file)
@@ -12,7 +12,7 @@ class LLKParser : public IParser
         int k;
         int next;
         ILexer*  lexer;
-        Token** lookahead;
+        Token* lookahead;
     public:
         LLKParser(int k_val, ILexer* lxer);
         ~LLKParser();
@@ -23,9 +23,9 @@ class LLKParser : public IParser
 
         void         consume(void);
         void         match(TokenType_T type);
-        Token*       lookaheadToken(int i);
+        Token&       lookaheadToken(int i);
         TokenType_T  lookaheadType(int i);
-        virtual void parse(void) = 0;
+        virtual AST* parse(void) = 0;
 };
 
 #endif