From: Mike D. Lowis Date: Sun, 26 Feb 2012 21:26:25 +0000 (-0500) Subject: DLLexer was not properly handling single character operators X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=311315063a2a5efc91eff6d458b88002ccc47cb6;p=archive%2Fdlang.git DLLexer was not properly handling single character operators --- diff --git a/example.dl b/example.dl index 0b8ff13..869719f 100644 --- a/example.dl +++ b/example.dl @@ -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 ) diff --git a/source/dllexer/dllexer.cpp b/source/dllexer/dllexer.cpp index 28d10d6..a2e4e3b 100644 --- a/source/dllexer/dllexer.cpp +++ b/source/dllexer/dllexer.cpp @@ -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) diff --git a/source/dlparser/dlparser.cpp b/source/dlparser/dlparser.cpp index dd7886b..7692b8d 100644 --- a/source/dlparser/dlparser.cpp +++ b/source/dlparser/dlparser.cpp @@ -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(); diff --git a/source/main.cpp b/source/main.cpp index 507ef35..2a412a3 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -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 { diff --git a/source/parse_utils/lexer/ilexer.h b/source/parse_utils/lexer/ilexer.h index 530c079..d21768d 100644 --- a/source/parse_utils/lexer/ilexer.h +++ b/source/parse_utils/lexer/ilexer.h @@ -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; }; diff --git a/source/visitors/sexp/sexp.cpp b/source/visitors/sexp/sexp.cpp index f49d787..d0eb266 100644 --- a/source/visitors/sexp/sexp.cpp +++ b/source/visitors/sexp/sexp.cpp @@ -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)