]> git.mdlowis.com Git - archive/dlang.git/commitdiff
DLLexer was not properly handling single character operators
authorMike D. Lowis <mike@mdlowis.com>
Sun, 26 Feb 2012 21:26:25 +0000 (16:26 -0500)
committerMike D. Lowis <mike@mdlowis.com>
Sun, 26 Feb 2012 21:26:25 +0000 (16:26 -0500)
example.dl
source/dllexer/dllexer.cpp
source/dlparser/dlparser.cpp
source/main.cpp
source/parse_utils/lexer/ilexer.h
source/visitors/sexp/sexp.cpp

index 0b8ff13b0a43db9954e6b74e8cb00974d9005af3..869719f5180ad94d6ce7c636c5561dc492fec565 100644 (file)
@@ -2,71 +2,71 @@
 # Macro Tests
 #------------------------------------------------------------------------------
 # Macro Definitions
-#% if ( cond, branch1:Block ) {
-#    exec_if( cond, branch1 )
-#}
+% if ( cond, branch1:Block ) {
+    exec_if( cond, branch1 )
+}
 
-#% ifelse ( cond, branch1:Block, branch2:Block ) {
-#    exec_if( cond, branch1, branch2 )
-#}
+% ifelse ( cond, branch1:Block, branch2:Block ) {
+    exec_if( cond, branch1, branch2 )
+}
 
-#% foreach ( lst, fn:Block ) {
-#    for_each(fn, lst)
-#}
+% 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!")
-#}
+# 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!")
+}
 
 #------------------------------------------------------------------------------
 # Delayed Evaluation
@@ -78,7 +78,7 @@
 #% force ( prom ) {
 #    force( prom )
 #}
-
+#
 #foo = delay nonexistent_var + 1
 #nonexistent_var = 19
 #assert( typeof(foo) == Block )
index 28d10d6b625f7469aa412b89cebed010ec359e37..a2e4e3b9a6972d964e6fdd478bc24515031f7ab2 100644 (file)
@@ -61,7 +61,7 @@ bool DLLexer::isStringChar(void)
 Token DLLexer::next(void)
 {
     Token ret;
-    while ( (!input->eof()) && (ret.type() != EOF) )
+    while ( (!input->eof()) && (ret.type() == EOF) )
     {
         if (isWhiteSpace())
         {
@@ -100,7 +100,6 @@ Token DLLexer::next(void)
             SingleCharOp(ret);
         }
     }
-
     return ret;
 }
 
@@ -234,7 +233,11 @@ void DLLexer::SingleCharOp(Token& tok)
             tok = Token( Single_Character_Matches[i].type, line, column );
         }
     }
-    throw Exception(line,column);
+
+    if( tok.type() == EOF)
+    {
+        throw Exception(line,column);
+    }
 }
 
 void DLLexer::MultiCharOp(Token& tok)
index dd7886ba82de249e4445c2718e42b48f888dd379..7692b8db38a9e64769f5434782860bcb4b663dc0 100644 (file)
@@ -73,14 +73,14 @@ AST* DLParser::Expression(void)
         match(ASSIGN);
         ret = _new AST( ASSIGN, 2, id_node, Expression());
     }
-    //else if( (lookaheadType(1) == MACRO) && (lookaheadType(2) == ID))
-    //{
-    //    ret = MacroDefinition();
-    //}
-    //else if( isMacro( lookaheadToken(1) ) )
-    //{
-    //    ret = MacroExpansion();
-    //}
+    else if( (lookaheadType(1) == MACRO) && (lookaheadType(2) == ID))
+    {
+        ret = MacroDefinition();
+    }
+    else if( isMacro( lookaheadToken(1) ) )
+    {
+        ret = MacroExpansion();
+    }
     else
     {
         ret = LogicalExpr();
index 507ef355343839118cfdf2116d66eda4fe678135..2a412a3b78696d391311711670d3c80cc658980b 100644 (file)
@@ -18,49 +18,33 @@ int main(int argc, char** argv)
 
     if( (argc == 2) && fileExists( argv[1] ) )
     {
-        //string input_fname(argv[1]);
-        //string temp_fname = createTempFileName( input_fname );
-        //ifstream input(input_fname.c_str());
-        //DLLexer lexer;
-        //Token tok;
-        //lexer.setInput(&input);
-        //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;
-        AST* parse_tree = NULL;
+        Scheme* visitor = NULL;
 
         // Open the input and output files
         ifstream input(input_fname.c_str());
-        //ofstream output(temp_fname.c_str());
+        ofstream output(temp_fname.c_str());
 
         // Parse the file
         parser.setInput(&input);
-        parse_tree = parser.parse();
 
         // Translate the AST
-        //visitor = _new Scheme( parse_tree );
-        //visitor->visit();
+        visitor = _new Scheme( parser.parse() );
+        visitor->visit();
 
         // Write to a temp file
-        //output << visitor->str();
-        //cout << visitor->str();
-        //output.close();
+        output << visitor->str();
+        cout << visitor->str();
+        output.close();
 
         // Compile temp file
-        //system(("csc " + temp_fname).c_str());
+        system(("csc " + temp_fname).c_str());
 
-        //// delete temp file
-        //remove(temp_fname.c_str());
-        //delete visitor;
-        delete parse_tree;
+        // delete temp file
+        remove(temp_fname.c_str());
+        delete visitor;
     }
     else
     {
index 530c07959c4be3621499e91b568c13f2d6c20deb..d21768d57a921ced2edd0e4c0569ee062c6a13bf 100644 (file)
@@ -14,10 +14,6 @@ class ILexer
         char current;
         std::istream* input;
 
-        void consume(void);
-        void match(char x);
-        bool eof(void);
-
     public:
         ILexer();
         virtual ~ILexer();
@@ -26,6 +22,10 @@ class ILexer
         void setInput(std::string& in);
         void setInput(std::istream* in);
 
+        void consume(void);
+        void match(char x);
+        bool eof(void);
+
         virtual Token next(void) = 0;
 };
 
index f49d7872cf2b93faec2d0e49676582112df61292..d0eb2665c5737fe06dc2aab0a213d0f6929991e6 100644 (file)
@@ -4,7 +4,7 @@ using namespace std;
 
 string SEXP::str()
 {
-       return stream.str();
+    return stream.str();
 }
 
 void SEXP::beforeVisit(AST* cur, int depth)
@@ -13,26 +13,26 @@ void SEXP::beforeVisit(AST* cur, int depth)
 
 void SEXP::afterVisit(AST* cur, int depth)
 {
-       stream << endl;
+    stream << endl;
 }
 
 void SEXP::beforeChildren(AST* cur, int depth)
 {
-       stream << "(" << cur->type() << " " << cur->text();
+    stream << "(" << cur->type() << " " << cur->text();
 }
 
 void SEXP::afterChildren(AST* cur, int depth)
 {
-       stream << ")";
+    stream << ")";
 }
 
 void SEXP::beforeChild(AST* cur, int depth)
 {
-       stream << endl;
-       for(int i = 0; i< depth; i++)
-       {
-               stream << "  ";
-       }
+    stream << endl;
+    for(int i = 0; i< depth; i++)
+    {
+        stream << "  ";
+    }
 }
 
 void SEXP::afterChild(AST* cur, int depth)