fuphyl-commits Mailing List for Functional/Procedural Hybrid Language
Status: Planning
Brought to you by:
me22
You can subscribe to this list here.
2006 |
Jan
|
Feb
|
Mar
|
Apr
|
May
(7) |
Jun
(1) |
Jul
|
Aug
(5) |
Sep
(49) |
Oct
(6) |
Nov
|
Dec
|
---|
From: <me...@us...> - 2006-10-08 04:06:27
|
Revision: 96 http://svn.sourceforge.net/fuphyl/?rev=96&view=rev Author: me22 Date: 2006-10-07 21:06:22 -0700 (Sat, 07 Oct 2006) Log Message: ----------- Flex/Bison input files Added Paths: ----------- trunk/interpreter/fuphyl.l trunk/interpreter/fuphyl.y Removed Paths: ------------- trunk/interpreter/symbols.txt trunk/interpreter/symbols_gen.cpp Added: trunk/interpreter/fuphyl.l =================================================================== --- trunk/interpreter/fuphyl.l (rev 0) +++ trunk/interpreter/fuphyl.l 2006-10-08 04:06:22 UTC (rev 96) @@ -0,0 +1,261 @@ +%{ + +/* Get Token IDs from Bison Output */ +#include "fuphyl.tab.h" + + +#include "stream_yy_input.h" + +/* +#define YY_DECL int yylex(void *stream) + +#define YY_INPUT(buf,result,max_size) \ + \ + result = YY_NULL; \ + stream_yy_input(stream, buf, &result, max_size); \ + +} +*/ + +YYLTYPE yylloc = {1,0,1,0}; +YYSTYPE yylval; + +int nestlevel = 0; + +#define UPDATE_LVAL \ + ++yylloc.first_column; \ + yylloc.last_column = yylloc.first_column + yyleng + +#define YY_USER_ACTION \ + UPDATE_LVAL; + +#define LOC_NEXT_LINE \ + ++yylloc.first_line; \ + ++yylloc.last_line; \ + yylloc.first_column = 0; \ + yylloc.last_column = 0 + +// %option never-interactive +// %option prefix="fuphyl" +%} + +%option stack + +%option noyywrap + +%option case-sensitive + +%x nestedcomment +%x string +%x string_done +%x atom +%x atom_done +%x sep_eater + +%% + +":" { + return SYN_TYPE; +} +"?" { + yy_push_state(sep_eater); + return SYN_COND; +} +";" { + yy_push_state(sep_eater); + return SYN_END; +} +"->" { + return SYN_RESULT; +} +"=" { + yy_push_state(sep_eater); + return SYN_ASSIGN; +} +<INITIAL>"," { +ECHO; + yy_push_state(sep_eater); + return SYN_SEP; +} +<sep_eater>"," { + /* do nothing */ +} + +"(" { + yy_push_state(sep_eater); + return TUPLE_BEGIN; +} +")" { + return TUPLE_END; +} +"{" { + yy_push_state(sep_eater); + return ARRAY_BEGIN; +} +"}" { + return ARRAY_END; +} +"[" { + yy_push_state(sep_eater); + return LIST_BEGIN; +} +"]" { + return LIST_END; +} + +"+" { + return OP_ADD; +} +"-" { + return OP_SUBTRACT; +} +"*" { + return OP_MULTIPLY; +} +"/" { + return OP_DIVIDE; +} +"**" { + return OP_POWER; +} + +"&&" { + return LOG_AND; +} +"&&&" { + return LOG_ANDALSO; +} +"||" { + return LOG_OR; +} +"|||" { + return LOG_ORELSE; +} +"^^" { + return LOG_XOR; +} +"!" { + return LOG_NOT; +} + +"<" { + return REL_LT; +} +"<=" { + return REL_LTE; +} +">" { + return REL_GT; +} +">=" { + return REL_GTE; +} +"==" { + return REL_EQ; +} +"!=" { + return REL_NEQ; +} + +if { + yy_push_state(sep_eater); + return KEY_IF; +} +else { + yy_push_state(sep_eater); + return KEY_ELSE; +} + +[[:digit:]][[:alnum:]_.]* { + return LIT_NUMBER; +} + +"\"" { + BEGIN(string); +} +<string,string_done>"\"" { + BEGIN(INITIAL); +} +<string>([^\\"[:cntrl:]]|("\\".))* { + BEGIN(string_done); + return LIT_STRING; +} +<string>. { + BEGIN(string_done); + return ERROR_STRING; +} +<string,string_done>"\n" { + LOC_NEXT_LINE; + return ERROR_STRING; +} +<string_done>. { + return ERROR_STRING; +} + +"'" { + BEGIN(atom); +} +<atom>"'" { + BEGIN(INITIAL); + return ERROR_ATOM; +} +<atom_done>"'" { + BEGIN(INITIAL); +} +<atom,atom_done>"\n" { + LOC_NEXT_LINE; + return ERROR_ATOM; +} +<atom>[[:print:]]+ { + BEGIN(atom_done); + return LIT_ATOM; +} +<atom,atom_done>. { + return ERROR_ATOM; +} + + +[[:alpha:]_][[:alnum:]_]* { +ECHO; + return LIT_IDENTIFIER; +} + +<*>"\n" { + LOC_NEXT_LINE; + ECHO; +} + +<*>[[:space:]] { + /* do nothing */ +} + +<INITIAL,sep_eater>"//".*\n { + /* do nothing */ +} +<INITIAL,nestedcomment,sep_eater>"/*" { + ++nestlevel; + if ( nestlevel == 1 ) { + yy_push_state(nestedcomment); + } +} +<nestedcomment>[^*/\n]+ {} +<nestedcomment>"*"+ {} +<nestedcomment>"*/" { + --nestlevel; + if ( !nestlevel ) { + yy_pop_state(); + } +} + +<sep_eater>. { + yyless( 0 ); + yy_pop_state(); +} + +<INITIAL>. { + printf("unmatched"); ECHO; +} + + +%% + Added: trunk/interpreter/fuphyl.y =================================================================== --- trunk/interpreter/fuphyl.y (rev 0) +++ trunk/interpreter/fuphyl.y 2006-10-08 04:06:22 UTC (rev 96) @@ -0,0 +1,154 @@ +// %lex-param {void *stream} +// %parse-param {void *stream} + +%{ + +#define TESTING + +%} + +%defines +%locations + +%token SYN_TYPE +%token SYN_COND +%token SYN_END +%token SYN_RESULT +%token SYN_ASSIGN +%token SYN_SEP + +%token TUPLE_BEGIN +%token TUPLE_END +%token ARRAY_BEGIN +%token ARRAY_END +%token LIST_BEGIN +%token LIST_END + +%token KEY_IF +%token KEY_ELSE + +%token LIT_NUMBER +%token LIT_STRING +%token LIT_ATOM +%token LIT_IDENTIFIER + +%left LOG_OR LOG_ORELSE LOG_XOR +%left LOG_AND LOG_ANDALSO +%nonassoc REL_LT REL_LTE REL_GT REL_GTE REL_EQ REL_NEQ +%left OP_ADD OP_SUBTRACT +%left OP_MULTIPLY OP_DIVIDE +%right OP_POWER + +/* unary ops */ +%left OP_POSITIVE OP_NEGATIVE LOG_NOT + +%left OP_LITERAL + +%token ERROR_STRING +%token ERROR_ATOM + +%token DUMMY + +%start expr_seq + +%% + +else_cond : KEY_ELSE SYN_COND + | KEY_ELSE + ; + +conditional : KEY_IF expr_seq SYN_COND + statement + else_cond + statement + ; + +argument_tuple : '1' ; +type_expr : '2' ; + +statement : SYN_END + | simple_expr_seq SYN_END + | conditional + | simple_expr_seq SYN_SEP conditional + ; + +type_specifier : /* empty */ + | SYN_TYPE type_expr + ; + +visibility_modifier : /* empty */ + ; + +function : visibility_modifier LIT_IDENTIFIER argument_tuple + type_specifier SYN_ASSIGN + statement + ; +variable : visibility_modifier LIT_IDENTIFIER + type_specifier SYN_ASSIGN + statement + ; + +definition : function + | variable + ; + +lit_tuple : TUPLE_BEGIN TUPLE_END + | TUPLE_BEGIN SYN_SEP TUPLE_END + | TUPLE_BEGIN simple_expr_seq TUPLE_END + | TUPLE_BEGIN simple_expr_seq SYN_SEP TUPLE_END + ; +lit_array : ARRAY_BEGIN ARRAY_END + | ARRAY_BEGIN SYN_SEP ARRAY_END + | ARRAY_BEGIN simple_expr_seq ARRAY_END + | ARRAY_BEGIN simple_expr_seq SYN_SEP ARRAY_END + ; +lit_list : LIST_BEGIN LIST_END + | LIST_BEGIN SYN_SEP LIST_END + | LIST_BEGIN simple_expr_seq LIST_END + | LIST_BEGIN simple_expr_seq SYN_SEP LIST_END + ; + +expr : LIT_NUMBER + | LIT_STRING + | LIT_ATOM + + | expr LOG_AND expr + | expr LOG_ANDALSO expr + | expr LOG_OR expr + | expr LOG_ORELSE expr + | expr LOG_XOR expr + | LOG_NOT expr + + | expr REL_LT expr + | expr REL_LTE expr + | expr REL_GT expr + | expr REL_GTE expr + | expr REL_EQ expr + | expr REL_NEQ expr + + | expr OP_ADD expr + | expr OP_SUBTRACT expr + | expr OP_MULTIPLY expr + | expr OP_DIVIDE expr + | expr OP_POWER expr + + | lit_tuple %prec OP_LITERAL + | lit_array %prec OP_LITERAL + | lit_list %prec OP_LITERAL + + | OP_SUBTRACT expr %prec OP_NEGATIVE + | OP_ADD expr %prec OP_POSITIVE + ; + +simple_expr_seq : expr + | simple_expr_seq SYN_SEP expr + ; + +fancy_expr : expr SYN_SEP + | definition + ; + +expr_seq : /* empty */ + | SYN_SEP + | expr_seq fancy_expr + ; Deleted: trunk/interpreter/symbols.txt =================================================================== --- trunk/interpreter/symbols.txt 2006-10-08 03:50:28 UTC (rev 95) +++ trunk/interpreter/symbols.txt 2006-10-08 04:06:22 UTC (rev 96) @@ -1,83 +0,0 @@ - -/* - * symbols.txt - * - * Copyright (c) 2006 Scott McMurray - * - * Licensed under the Open Software License version 3.0 - * ( See http://opensource.org/licenses/osl-3.0.php ) - * - */ - -// UNUSED_1 $ -// UNUSED_2 # -// UNUSED_3 { -// UNUSED_4 } - -// SPECIAL_FUNCCALL SPECIAL_FUNCCALL -// SPECIAL_ADJACENT SPECIAL_ADJACENT - -OP_COMMAND ` -OP_TYPE : -OP_COND ? -OP_END ; -OP_RESULT -> -OP_OOCALL . -OP_SCOPE :: -OP_ANY _ -OP_ASSIGN = -OP_ATOM ' -OP_STRING " -OP_DELAY \ -OP_UNIT () -OP_NIL nil -OP_SEP , - -LIST_BEGIN [ -LIST_END ] -LIST_JOIN ++ -//LIST_SPLIT * -LIST_PREP >> -LIST_AP << - -TUPLE_BEGIN ( -TUPLE_END ) -TUPLE_REP $ -TUPLE_GET @ -TUPLE_SET @= - -ARRAY_BEGIN { -ARRAY_END } - -BITWISE_AND & -BITWISE_OR | -BITWISE_XOR ^ -BITWISE_NOT ~ -BITWISE_LSHIFT << -BITWISE_RSHIFT >> -// BITWISE_RSHIFTL >+> -// BITWISE_RSHIFTA >-> -// BITWISE_LROTATE <|< -// BITWISE_RROTATE >|> - -LOGICAL_AND && -LOGICAL_OR || -LOGICAL_XOR ^^ -LOGICAL_NOT ! - -COMMENT_LINE // -COMMENT_BEGIN /* -COMMENT_END */ - -MATH_ADD + -MATH_SUB - -MATH_MULT * -MATH_DIV / -MATH_MOD % -MATH_EXP ** - -RELATIONAL_EQ == -RELATIONAL_GT > -RELATIONAL_GTE >= -RELATIONAL_LT < -RELATIONAL_LTE <= Deleted: trunk/interpreter/symbols_gen.cpp =================================================================== --- trunk/interpreter/symbols_gen.cpp 2006-10-08 03:50:28 UTC (rev 95) +++ trunk/interpreter/symbols_gen.cpp 2006-10-08 04:06:22 UTC (rev 96) @@ -1,188 +0,0 @@ -/* - * symbols_gen.cpp - * - * Copyright (c) 2006 Scott McMurray - * - * Licensed under the Open Software License version 3.0 - * ( See http://opensource.org/licenses/osl-3.0.php ) - * - */ - -#include <iostream> -#include <string> -#include <fstream> -#include <vector> -#include <cctype> -#include <limits> -#include <cerrno> -#include <cstdio> -#include <algorithm> - -#define AUTOGEN_INFO "autogen.txt" -#define LICENSE_HEADER "header.txt" - -#define SYMBOLS_FILE "symbols.txt" -#define SYMBOLS_HEADER "symbols.hpp" -#define SYMBOLS_HEADER_TEMP "symbols.hpp.tmp" -#define SYMBOLS_SOURCE "symbols.cpp" -#define SYMBOLS_SOURCE_TEMP "symbols.cpp.tmp" - -using namespace std; - -template <typename T, typename U> -bool sort_second( pair<T,U> const &lhs, pair<T,U> const &rhs ) { - return std::less<U>()(lhs.second, rhs.second); -} - -void escape(std::string &v) { - for ( unsigned i = 0; i < v.size(); ++i ) { - if ( v[i] == '\\' || v[i] == '"' ) { - v.insert( v.begin()+i, '\\' ); - ++i; - } - } -} -void escape_second( pair<string, string> &p ) { - escape( p.second ); -} - -int main() { - - vector< pair<string, string> > symbol_data; - - { - ifstream symbols_in( SYMBOLS_FILE ); - if ( !symbols_in.is_open() ) { - cerr << "Could Not Open " SYMBOLS_FILE "\n"; - return 1; - } - - - for ( string s, v; ; ) { - symbols_in >> s; - if ( !symbols_in ) break; - if ( s.empty() || !isalpha(s[0]) ) { - symbols_in.ignore( numeric_limits<streamsize>::max(), '\n' ); - continue; - } - symbols_in >> ws; - getline( symbols_in, v, '\n' ); - while ( !v.empty() && iscntrl( v[v.size()-1] ) ) { - v.erase( v.size()-1 ); - } - if ( !symbols_in ) break; - if ( s.empty() ) continue; -// symbols_in.ignore( numeric_limits<streamsize>::max(), '\n' ); - symbol_data.push_back( make_pair(s,v) ); - clog << "Added symbol \"" << s << "\" with value \"" << v << "\"\n"; - } - - sort( symbol_data.begin(), symbol_data.end(), - &sort_second<string,string> ); - for_each( symbol_data.begin(), symbol_data.end(), &escape_second ); - } - clog << "Read All Symbols\n"; - - { - ofstream symbols_out( SYMBOLS_HEADER_TEMP, ios::trunc ); - if ( !symbols_out.is_open() ) { - cerr << "Couldn't Open " SYMBOLS_HEADER_TEMP "\n"; - return 1; - } - symbols_out << "#ifndef SYMBOLS_HPP\n" - << "#define SYMBOLS_HPP\n"; - symbols_out << "\n"; - { - ifstream ag( AUTOGEN_INFO ); - symbols_out << ag.rdbuf(); - } - symbols_out << "\n"; - { - ifstream hp( LICENSE_HEADER ); - for ( string n; getline( hp, n ); ) { - if ( n.find("file/name.ext") != n.npos ) { - symbols_out << " * " SYMBOLS_HEADER "\n"; - } else { - symbols_out << n << "\n"; - } - } - } - symbols_out << "\n"; - symbols_out << "#include <string>\n"; - symbols_out << "#include <vector>\n"; - symbols_out << "\n"; - symbols_out << "typedef std::vector<std::string>::const_iterator symbol_cit;\n"; - symbols_out << "symbol_cit symbol_values_begin();\n"; - symbols_out << "symbol_cit symbol_values_end();\n"; - symbols_out << "\n"; - for ( unsigned i = 0; i < symbol_data.size(); ++i ) { - symbols_out << "#define " << symbol_data[i].first - << " (symbol_values_begin()[" << i << "])\n"; - } - symbols_out << "\n"; - symbols_out << "#endif\n"; - symbols_out << "\n"; - - } - clog << "Wrote Temp Header\n"; - - { - ofstream symbols_out( SYMBOLS_SOURCE_TEMP, ios::trunc ); - if ( !symbols_out.is_open() ) { - cerr << "Couldn't Open " SYMBOLS_SOURCE_TEMP "\n"; - return 1; - } - { - ifstream ag( AUTOGEN_INFO ); - symbols_out << ag.rdbuf(); - } - symbols_out << "\n"; - { - ifstream hp( LICENSE_HEADER ); - for ( string n; getline( hp, n ); ) { - if ( n.find("file/name.ext") != n.npos ) { - symbols_out << " * " SYMBOLS_SOURCE "\n"; - } else { - symbols_out << n << "\n"; - } - } - } - symbols_out << "\n"; - symbols_out << "#include \"symbols.hpp\"\n"; - symbols_out << "\n"; - symbols_out << "#ifndef UNNAMED\n"; - symbols_out << "#define UNNAMED\n"; - symbols_out << "#endif\n"; - symbols_out << "namespace UNNAMED {\n"; - symbols_out << "bool build_symbol_value_vector_(" - "std::vector<std::string> &v" - ") {\n"; - symbols_out << " if ( !v.empty() ) return false;\n"; - for ( unsigned i = 0; i < symbol_data.size(); ++i ) { - symbols_out << " v.push_back(\"" - << symbol_data[i].second - << "\"); // " << symbol_data[i].first << "\n"; - } - symbols_out << " return true;\n"; - symbols_out << "}\n"; - symbols_out << "\n"; - symbols_out << "std::vector<std::string> symbol_value_vector_;\n"; - symbols_out << "bool const symbol_value_vector_built = \n" - " build_symbol_value_vector_(symbol_value_vector_);\n"; - symbols_out << "} // namespace UNNAMED\n"; - symbols_out << "\n"; - symbols_out << "symbol_cit symbol_values_begin() {\n"; - symbols_out << " return symbol_value_vector_.begin();\n"; - symbols_out << "}\n"; - symbols_out << "symbol_cit symbol_values_end() {\n"; - symbols_out << " return symbol_value_vector_.end();\n"; - symbols_out << "}\n"; - symbols_out << "\n"; - - } - clog << "Wrote Temp Source\n"; - - rename( SYMBOLS_HEADER_TEMP, SYMBOLS_HEADER ); - rename( SYMBOLS_SOURCE_TEMP, SYMBOLS_SOURCE ); - clog << "Moved to Correct Locations\n"; -} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <me...@us...> - 2006-10-08 03:50:31
|
Revision: 95 http://svn.sourceforge.net/fuphyl/?rev=95&view=rev Author: me22 Date: 2006-10-07 20:50:28 -0700 (Sat, 07 Oct 2006) Log Message: ----------- wording improvement, thanks RHLinuxGUY Modified Paths: -------------- trunk/doc/fundamentals.txt Modified: trunk/doc/fundamentals.txt =================================================================== --- trunk/doc/fundamentals.txt 2006-10-05 21:04:08 UTC (rev 94) +++ trunk/doc/fundamentals.txt 2006-10-08 03:50:28 UTC (rev 95) @@ -50,7 +50,7 @@ vec = (1,2,3); len = length vec; -Using the 1-tuple rule above, this is equivalent to the following, more C-styled syntax: +Using the 1-tuple rule above, this could, equivalently, also be written using the following more C-styled syntax: vec = (1,2,3); len = length(vec); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <me...@us...> - 2006-10-05 21:04:22
|
Revision: 94 http://svn.sourceforge.net/fuphyl/?rev=94&view=rev Author: me22 Date: 2006-10-05 14:04:08 -0700 (Thu, 05 Oct 2006) Log Message: ----------- tagging the custom parser before switching to the flex/bison one Modified Paths: -------------- tags/custom_parser/interpreter/flowparser.cpp tags/custom_parser/interpreter/main.cpp tags/custom_parser/samples/demos.fph tags/custom_parser/samples/syntax_example.fph trunk/interpreter/flowparser.cpp trunk/interpreter/main.cpp trunk/samples/demos.fph trunk/samples/syntax_example.fph Added Paths: ----------- tags/custom_parser/ tags/custom_parser/interpreter/ tags/custom_parser/interpreter/node_display_visitor.cpp tags/custom_parser/interpreter/nodes.cpp Removed Paths: ------------- tags/custom_parser/interpreter/ tags/custom_parser/interpreter/node_display_visitor.cpp tags/custom_parser/interpreter/nodes.cpp Copied: tags/custom_parser (from rev 85, trunk) Copied: tags/custom_parser/interpreter (from rev 92, trunk/interpreter) Modified: tags/custom_parser/interpreter/flowparser.cpp =================================================================== --- trunk/interpreter/flowparser.cpp 2006-09-29 02:34:36 UTC (rev 92) +++ tags/custom_parser/interpreter/flowparser.cpp 2006-10-05 21:04:08 UTC (rev 94) @@ -14,7 +14,7 @@ #include "errors.hpp" #include <cassert> -#define DEBUGINFO +//#define DEBUGINFO #include <iostream> #include "callstack_display.hpp" @@ -44,7 +44,7 @@ *b = 0; ++b; } -LEAVING; +LEAVING; return root; } @@ -52,11 +52,11 @@ auto_node name) { assert( b != e ); ENTERING; - + std::auto_ptr<definition_node> root(new definition_node); root->start = (*b)->start; - root->name = name; - + root->name = name; + if ( is_token( **b, token::SYMBOL, OP_TYPE ) ) { /// set past OP_TYPE SHOW_NODE(**b); @@ -77,9 +77,9 @@ } if ( b == e || !is_token( **b, token::SYMBOL, OP_ASSIGN ) ) { push_error( error::ERROR, "Missing " + OP_ASSIGN + " after type", - (*b)->start.row, (*b)->start.col ); + (*b)->start.row, (*b)->start.col ); for ( ; b != e && !is_token( **b++, token::SYMBOL, OP_ASSIGN); ); - if ( b == e ) { LEAVING; return root.reset(), root; } + if ( b == e ) { LEAVING; return root.reset(), root; } } } @@ -96,21 +96,21 @@ if ( b == e ) { push_error( error::ERROR, "Unterminated Definition", root->start.row, root->start.col ); - LEAVING; return root.reset(), root; + LEAVING; return root.reset(), root; } if ( !is_token( **b, token::SYMBOL, OP_END ) ) { push_error( error::ERROR, "Missing " + OP_END + " after definition", (*b)->start.row, (*b)->start.col ); for ( ; b != e && !is_token( **b++, token::SYMBOL, OP_END); ); - if ( b == e ) { LEAVING; return root.reset(), root; } - } + if ( b == e ) { LEAVING; return root.reset(), root; } + } /// step past OP_END SHOW_NODE(**b); ++b; - -//std::cerr << "successful: "; SHOW_NODE(*root); std::cerr << "\n"; - + +//std::cerr << "successful: "; SHOW_NODE(*root); std::cerr << "\n"; + LEAVING; return root; } @@ -249,7 +249,7 @@ root->push_back( flowparse_definition(b,e,root->pop_back()) ); } else { SHOW_NODE(**b); - root->push_back( *b++ ); + root->push_back( *b++ ); } } LEAVING; return root; @@ -260,6 +260,6 @@ container_node &cn = pproot->as_a<container_node>(); cni b = cn.begin(); cni e = cn.end(); - std::auto_ptr<sequence_node> root = flowparse_sequence( b, e ); + std::auto_ptr<sequence_node> root = flowparse_sequence( b, e ); LEAVING; return auto_node( root.release() ); } Modified: tags/custom_parser/interpreter/main.cpp =================================================================== --- trunk/interpreter/main.cpp 2006-09-29 02:34:36 UTC (rev 92) +++ tags/custom_parser/interpreter/main.cpp 2006-10-05 21:04:08 UTC (rev 94) @@ -48,7 +48,7 @@ std::ifstream f( *tc ); if ( !f ) { std::cerr << "Could not open " << *tc << "!\n"; - continue; + continue; } tokenize( f, tokens ); std::cout << " "; @@ -63,22 +63,29 @@ std::cout << std::endl; if ( show_errors(*tc) ) continue; + std::cout << "\n\n"; auto_node pptree = preparse(tokens); token_sequence().swap(tokens); + { node_display_visitor ndv(std::cout); pptree->visit(ndv); + } std::cout << std::endl; if ( show_errors(*tc) ) continue; + std::cout << "\n\n"; -//break; - auto_node flowtree = flowparse(pptree); + { + node_display_visitor ndv(std::cout); flowtree->visit(ndv); + } std::cout << std::endl; if ( show_errors(*tc) ) continue; + std::cout << "\n\n"; + } } catch ( std::exception const &e ) { Deleted: tags/custom_parser/interpreter/node_display_visitor.cpp =================================================================== --- trunk/interpreter/node_display_visitor.cpp 2006-09-29 02:34:36 UTC (rev 92) +++ tags/custom_parser/interpreter/node_display_visitor.cpp 2006-10-05 21:04:08 UTC (rev 94) @@ -1,188 +0,0 @@ -/* - * node_display_visitor.hpp - * - * Copyright (c) 2006 Scott McMurray - * - * Licensed under the Open Software License version 3.0 - * ( See http://opensource.org/licenses/osl-3.0.php ) - * - */ - -#include "nodes.hpp" -#include "symbols.hpp" - -#include <iostream> -#include <string> -#include <cassert> - -//#define DEBUGINFO -#include "callstack_display.hpp" - -node_display_visitor::node_display_visitor(std::ostream &s) : sink(s) {} - -/* -std::string operator*(std::string const &s, unsigned n) { - if ( n == 0 ) return ""; - if ( n == 1 ) return s; - - if ( n & 1 ) { - return s*(n-1)+s; - } else { - return (s+s)*(n/2); - } -} -*/ -std::string indent_(unsigned n) { - std::string s; - while ( n-- ) s.push_back(' '); - return s; -// return std::string(" ")*n; -} - -#define INDENT indent_(indent) - -#if 0 -DEFINE_NODE_VISIT( node_display_visitor, node, /*n*/ ) { - sink << INDENT << "Visited Base Node -- This shouldn't ever happen\n"; -} -#endif - -DEFINE_NODE_VISIT( node_display_visitor, token_node, n ) { -//ENTERING; -// sink << n.data.type_text() << '{' << n.data.data << '}' << ' '; - sink << n.start.data; -//LEAVING; -} - -DEFINE_NODE_VISIT( node_display_visitor, conditional_node, n ) { -ENTERING; - sink << "if "; - n.cond->visit(*this); - indent += 4; - sink << ' ' << OP_COND << '\n' << INDENT; - n.true_branch->visit(*this); - sink << OP_END << '\n'; - indent -= 4; - sink << INDENT << "else "; - indent += 4; - if ( !n.false_branch->is_a<conditional_node>() ) { - sink << OP_COND << '\n' << INDENT; - } - n.false_branch->visit(*this); - indent -= 4; -LEAVING; -} - -DEFINE_NODE_VISIT( node_display_visitor, operator_node, /*n*/ ) { -ENTERING; - sink << "Not Yet Defined -- " << __FUNCTION__ << "\n"; -LEAVING; -} - -DEFINE_NODE_VISIT( node_display_visitor, definition_node, n ) { -ENTERING; -assert( n.name.get() ); - n.name->visit(*this); - indent += 4; - if ( n.type.get() ) { - sink << ' ' << OP_TYPE << ' '; - n.type->visit(*this); - } - sink << ' ' << OP_ASSIGN << '\n' << INDENT; -assert( n.body.get() ); - n.body->visit(*this); - sink << OP_END; - indent -= 4; -LEAVING; -} - -DEFINE_NODE_VISIT( node_display_visitor, function_node, /*n*/ ) { -ENTERING; - sink << "Not Yet Defined -- " << __FUNCTION__ << "\n"; -LEAVING; -} - -DEFINE_NODE_VISIT( node_display_visitor, container_node, n ) { -ENTERING; - container_node::const_iterator const begin = n.begin(); - container_node::const_iterator iter = begin; - container_node::const_iterator const end = n.end(); - - assert( begin != end || !n.is_a<container_node>() ); - - for ( ; iter != end; ++iter ) { -// assert( (*iter)->is_a<token_node>() ); - if ( begin != iter ) sink << ' '; - (*iter)->visit(*this); - } -LEAVING; -} - -DEFINE_NODE_VISIT( node_display_visitor, sequence_node, n ) { -ENTERING; - container_node::const_iterator const begin = n.begin(); - container_node::const_iterator iter = begin; - container_node::const_iterator const end = n.end(); - - assert( begin != end ); - - for ( ; iter != end; ++iter ) { -// assert( !(*iter)->is_a<token_node>() ); - if ( begin != iter ) { - if ( !(*(iter-1))->is_a<definition_node>() ) sink << OP_SEP; - sink << '\n' << INDENT; - } - assert( *iter ); - (*iter)->visit(*this); - } -LEAVING; -} - -DEFINE_NODE_VISIT( node_display_visitor, tuple_node, n ) { -ENTERING; - if ( n.empty() ) { - sink << TUPLE_BEGIN << LIST_END; - } else { - sink << TUPLE_BEGIN << ' '; - indent += 2; - container_node const &sn = n; - visit( sn ); -// sequence_node const &sn = n; -// visit( sn ); - indent -= 2; - sink << ' ' << TUPLE_END; - } -LEAVING; -} - -DEFINE_NODE_VISIT( node_display_visitor, list_node, n ) { -ENTERING; - if ( n.empty() ) { - sink << LIST_BEGIN << LIST_END; - } else { - sink << LIST_BEGIN << ' '; - indent += 2; - container_node const &sn = n; - visit( sn ); -// sequence_node const &sn = n; -// visit( sn ); - indent -= 2; - sink << ' ' << LIST_END; - } -LEAVING; -} - -DEFINE_NODE_VISIT( node_display_visitor, array_node, n ) { -ENTERING; - if ( n.empty() ) { - sink << ARRAY_BEGIN << ARRAY_END; - } else { - sink << ARRAY_BEGIN << ' '; - indent += 2; - // sequence_node const &sn = n; - visit( (sequence_node const &)n ); - indent -= 2; - sink << ' ' << ARRAY_END; - } -LEAVING; -} Copied: tags/custom_parser/interpreter/node_display_visitor.cpp (from rev 93, trunk/interpreter/node_display_visitor.cpp) =================================================================== --- tags/custom_parser/interpreter/node_display_visitor.cpp (rev 0) +++ tags/custom_parser/interpreter/node_display_visitor.cpp 2006-10-05 21:04:08 UTC (rev 94) @@ -0,0 +1,191 @@ +/* + * node_display_visitor.hpp + * + * Copyright (c) 2006 Scott McMurray + * + * Licensed under the Open Software License version 3.0 + * ( See http://opensource.org/licenses/osl-3.0.php ) + * + */ + +#include "nodes.hpp" +#include "symbols.hpp" + +#include <iostream> +#include <string> +#include <cassert> + +//#define DEBUGINFO +#include "callstack_display.hpp" + +node_display_visitor::node_display_visitor(std::ostream &s) + : indent(), sink(s) {} + +/* +std::string operator*(std::string const &s, unsigned n) { + if ( n == 0 ) return ""; + if ( n == 1 ) return s; + + if ( n & 1 ) { + return s*(n-1)+s; + } else { + return (s+s)*(n/2); + } +} +*/ +std::string indent_(unsigned n) { + std::string s; + while ( n-- ) s.push_back(' '); + return s; +// return std::string(" ")*n; +} + +//#define INDENT indent_(indent) +#include <iomanip> +#define INDENT std::setw(indent) << "" + +#if 0 +DEFINE_NODE_VISIT( node_display_visitor, node, /*n*/ ) { + sink << INDENT << "Visited Base Node -- This shouldn't ever happen\n"; +} +#endif + +DEFINE_NODE_VISIT( node_display_visitor, token_node, n ) { +//ENTERING; +// sink << n.data.type_text() << '{' << n.data.data << '}' << ' '; + sink << n.start.data; +//LEAVING; +} + +DEFINE_NODE_VISIT( node_display_visitor, conditional_node, n ) { +ENTERING; + sink << "if "; + n.cond->visit(*this); + indent += 4; + sink << ' ' << OP_COND << '\n' << INDENT; + n.true_branch->visit(*this); + sink << OP_END << '\n'; + indent -= 4; + sink << INDENT << "else "; + indent += 4; + if ( !n.false_branch->is_a<conditional_node>() ) { + sink << OP_COND << '\n' << INDENT; + } + n.false_branch->visit(*this); + indent -= 4; +LEAVING; +} + +DEFINE_NODE_VISIT( node_display_visitor, operator_node, /*n*/ ) { +ENTERING; + sink << "Not Yet Defined -- " << __FUNCTION__ << "\n"; +LEAVING; +} + +DEFINE_NODE_VISIT( node_display_visitor, definition_node, n ) { +ENTERING; +assert( n.name.get() ); + n.name->visit(*this); + indent += 4; + if ( n.type.get() ) { + sink << ' ' << OP_TYPE << ' '; + n.type->visit(*this); + } + sink << ' ' << OP_ASSIGN << '\n' << INDENT; +assert( n.body.get() ); + n.body->visit(*this); + sink << OP_END; + indent -= 4; +LEAVING; +} + +DEFINE_NODE_VISIT( node_display_visitor, function_node, /*n*/ ) { +ENTERING; + sink << "Not Yet Defined -- " << __FUNCTION__ << "\n"; +LEAVING; +} + +DEFINE_NODE_VISIT( node_display_visitor, container_node, n ) { +ENTERING; + container_node::const_iterator const begin = n.begin(); + container_node::const_iterator iter = begin; + container_node::const_iterator const end = n.end(); + + assert( begin != end || !n.is_a<container_node>() ); + + for ( ; iter != end; ++iter ) { +// assert( (*iter)->is_a<token_node>() ); + if ( begin != iter ) sink << ' '; + (*iter)->visit(*this); + } +LEAVING; +} + +DEFINE_NODE_VISIT( node_display_visitor, sequence_node, n ) { +ENTERING; + container_node::const_iterator const begin = n.begin(); + container_node::const_iterator iter = begin; + container_node::const_iterator const end = n.end(); + + assert( begin != end ); + + for ( ; iter != end; ++iter ) { +// assert( !(*iter)->is_a<token_node>() ); + if ( begin != iter ) { + if ( !(*(iter-1))->is_a<definition_node>() ) sink << OP_SEP; + sink << '\n' << INDENT; + } + assert( *iter ); + (*iter)->visit(*this); + } +LEAVING; +} + +DEFINE_NODE_VISIT( node_display_visitor, tuple_node, n ) { +ENTERING; + if ( n.empty() ) { + sink << TUPLE_BEGIN << LIST_END; + } else { + sink << TUPLE_BEGIN << ' '; + indent += 2; + container_node const &sn = n; + visit( sn ); +// sequence_node const &sn = n; +// visit( sn ); + indent -= 2; + sink << ' ' << TUPLE_END; + } +LEAVING; +} + +DEFINE_NODE_VISIT( node_display_visitor, list_node, n ) { +ENTERING; + if ( n.empty() ) { + sink << LIST_BEGIN << LIST_END; + } else { + sink << LIST_BEGIN << ' '; + indent += 2; + container_node const &sn = n; + visit( sn ); +// sequence_node const &sn = n; +// visit( sn ); + indent -= 2; + sink << ' ' << LIST_END; + } +LEAVING; +} + +DEFINE_NODE_VISIT( node_display_visitor, array_node, n ) { +ENTERING; + if ( n.empty() ) { + sink << ARRAY_BEGIN << ARRAY_END; + } else { + sink << ARRAY_BEGIN << ' '; + indent += 2; + // sequence_node const &sn = n; + visit( (sequence_node const &)n ); + indent -= 2; + sink << ' ' << ARRAY_END; + } +LEAVING; +} Deleted: tags/custom_parser/interpreter/nodes.cpp =================================================================== --- trunk/interpreter/nodes.cpp 2006-09-29 02:34:36 UTC (rev 92) +++ tags/custom_parser/interpreter/nodes.cpp 2006-10-05 21:04:08 UTC (rev 94) @@ -1,58 +0,0 @@ -/* - * nodes.cpp - * - * Copyright (c) 2006 Scott McMurray - * - * Licensed under the Open Software License version 3.0 - * ( See http://opensource.org/licenses/osl-3.0.php ) - * - */ - -#include "nodes.hpp" - -#include <cassert> - -#ifndef NDEBUG -#include <iostream> -#endif - -void node::visit(node_visitor &v) { -#if 0 - assert( std::cout << "Visiting a " << typeid(*this).name() - << " at " << &v - << " with a " << typeid(v).name() - << ".\n" ); - return visit_impl(v); -#elif 1 - assert( std::cout << '{' ); - visit_impl(v); - assert( std::cout << '}' ); -#elif 0 - bool skip = false - || is_a<token_node>() - || is_a<tuple_node>() - || is_a<list_node>() - || is_a<container_node>() - || is_a<conditional_node>() - || false; - if ( !skip ) - assert( std::cout << '{' << typeid(*this).name() << '}' ); - visit_impl(v); - if ( !skip ) - assert( std::cout << '{' << '/' << typeid(*this).name() << '}' ); -#else - return visit_impl(v); -#endif -} - -DEFINE_NODE_VISIT_IMPL( token_node ) -DEFINE_NODE_VISIT_IMPL( conditional_node ) -DEFINE_NODE_VISIT_IMPL( operator_node ) -DEFINE_NODE_VISIT_IMPL( definition_node ) -DEFINE_NODE_VISIT_IMPL( function_node ) -DEFINE_NODE_VISIT_IMPL( container_node ) -DEFINE_NODE_VISIT_IMPL( sequence_node ) -DEFINE_NODE_VISIT_IMPL( list_node ) -DEFINE_NODE_VISIT_IMPL( tuple_node ) -DEFINE_NODE_VISIT_IMPL( array_node ) -// DEFINE_NODE_VISIT_IMPL( ) Copied: tags/custom_parser/interpreter/nodes.cpp (from rev 93, trunk/interpreter/nodes.cpp) =================================================================== --- tags/custom_parser/interpreter/nodes.cpp (rev 0) +++ tags/custom_parser/interpreter/nodes.cpp 2006-10-05 21:04:08 UTC (rev 94) @@ -0,0 +1,58 @@ +/* + * nodes.cpp + * + * Copyright (c) 2006 Scott McMurray + * + * Licensed under the Open Software License version 3.0 + * ( See http://opensource.org/licenses/osl-3.0.php ) + * + */ + +#include "nodes.hpp" + +#include <cassert> + +#ifndef NDEBUG +#include <iostream> +#endif + +void node::visit(node_visitor &v) { +#if 0 + assert( std::cout << "Visiting a " << typeid(*this).name() + << " at " << &v + << " with a " << typeid(v).name() + << ".\n" ); + return visit_impl(v); +#elif 0 + assert( std::cout << '{' ); + visit_impl(v); + assert( std::cout << '}' ); +#elif 0 + bool skip = false + || is_a<token_node>() + || is_a<tuple_node>() + || is_a<list_node>() + || is_a<container_node>() + || is_a<conditional_node>() + || false; + if ( !skip ) + assert( std::cout << '{' << typeid(*this).name() << '}' ); + visit_impl(v); + if ( !skip ) + assert( std::cout << '{' << '/' << typeid(*this).name() << '}' ); +#else + return visit_impl(v); +#endif +} + +DEFINE_NODE_VISIT_IMPL( token_node ) +DEFINE_NODE_VISIT_IMPL( conditional_node ) +DEFINE_NODE_VISIT_IMPL( operator_node ) +DEFINE_NODE_VISIT_IMPL( definition_node ) +DEFINE_NODE_VISIT_IMPL( function_node ) +DEFINE_NODE_VISIT_IMPL( container_node ) +DEFINE_NODE_VISIT_IMPL( sequence_node ) +DEFINE_NODE_VISIT_IMPL( list_node ) +DEFINE_NODE_VISIT_IMPL( tuple_node ) +DEFINE_NODE_VISIT_IMPL( array_node ) +// DEFINE_NODE_VISIT_IMPL( ) Modified: tags/custom_parser/samples/demos.fph =================================================================== --- trunk/samples/demos.fph 2006-09-28 03:30:58 UTC (rev 85) +++ tags/custom_parser/samples/demos.fph 2006-10-05 21:04:08 UTC (rev 94) @@ -1,13 +1,17 @@ double n = 2*n; -factorial_slow n = +factorial_simple n = if n <= 1 ? 1; else ? - n * factorial_slow(n-1); - + n * factorial_simple(n-1); + +// /* + factorial (n,r=1) = if n <= 1 ? - r; + ((r)); else ? factorial( n-1, n*r ); + +// */ Modified: tags/custom_parser/samples/syntax_example.fph =================================================================== --- trunk/samples/syntax_example.fph 2006-09-28 03:30:58 UTC (rev 85) +++ tags/custom_parser/samples/syntax_example.fph 2006-10-05 21:04:08 UTC (rev 94) @@ -1,18 +1,18 @@ // WHAT YOU ACTUALLY TYPED -partition ( list : [T], +partition ( list : [,T,], cond : T->bool, matched : [T] = [], failed : [T] = []) = - , - , - , + , + , + , if list.empty ? ,,, ( ,, matched,, failed,, ) else if cond( list.head ) ? partition( list.tail, cond, matched.push( list.head ), failed ) - else ? + else , partition( list.tail, cond, matched, failed.push( list.head ) ); /////////////////////////////////////////////////////////////////////////////// Modified: trunk/interpreter/flowparser.cpp =================================================================== --- trunk/interpreter/flowparser.cpp 2006-09-29 03:20:58 UTC (rev 93) +++ trunk/interpreter/flowparser.cpp 2006-10-05 21:04:08 UTC (rev 94) @@ -14,7 +14,7 @@ #include "errors.hpp" #include <cassert> -#define DEBUGINFO +//#define DEBUGINFO #include <iostream> #include "callstack_display.hpp" @@ -44,7 +44,7 @@ *b = 0; ++b; } -LEAVING; +LEAVING; return root; } @@ -52,11 +52,11 @@ auto_node name) { assert( b != e ); ENTERING; - + std::auto_ptr<definition_node> root(new definition_node); root->start = (*b)->start; - root->name = name; - + root->name = name; + if ( is_token( **b, token::SYMBOL, OP_TYPE ) ) { /// set past OP_TYPE SHOW_NODE(**b); @@ -77,9 +77,9 @@ } if ( b == e || !is_token( **b, token::SYMBOL, OP_ASSIGN ) ) { push_error( error::ERROR, "Missing " + OP_ASSIGN + " after type", - (*b)->start.row, (*b)->start.col ); + (*b)->start.row, (*b)->start.col ); for ( ; b != e && !is_token( **b++, token::SYMBOL, OP_ASSIGN); ); - if ( b == e ) { LEAVING; return root.reset(), root; } + if ( b == e ) { LEAVING; return root.reset(), root; } } } @@ -96,21 +96,21 @@ if ( b == e ) { push_error( error::ERROR, "Unterminated Definition", root->start.row, root->start.col ); - LEAVING; return root.reset(), root; + LEAVING; return root.reset(), root; } if ( !is_token( **b, token::SYMBOL, OP_END ) ) { push_error( error::ERROR, "Missing " + OP_END + " after definition", (*b)->start.row, (*b)->start.col ); for ( ; b != e && !is_token( **b++, token::SYMBOL, OP_END); ); - if ( b == e ) { LEAVING; return root.reset(), root; } - } + if ( b == e ) { LEAVING; return root.reset(), root; } + } /// step past OP_END SHOW_NODE(**b); ++b; - -//std::cerr << "successful: "; SHOW_NODE(*root); std::cerr << "\n"; - + +//std::cerr << "successful: "; SHOW_NODE(*root); std::cerr << "\n"; + LEAVING; return root; } @@ -249,7 +249,7 @@ root->push_back( flowparse_definition(b,e,root->pop_back()) ); } else { SHOW_NODE(**b); - root->push_back( *b++ ); + root->push_back( *b++ ); } } LEAVING; return root; @@ -260,6 +260,6 @@ container_node &cn = pproot->as_a<container_node>(); cni b = cn.begin(); cni e = cn.end(); - std::auto_ptr<sequence_node> root = flowparse_sequence( b, e ); + std::auto_ptr<sequence_node> root = flowparse_sequence( b, e ); LEAVING; return auto_node( root.release() ); } Modified: trunk/interpreter/main.cpp =================================================================== --- trunk/interpreter/main.cpp 2006-09-29 03:20:58 UTC (rev 93) +++ trunk/interpreter/main.cpp 2006-10-05 21:04:08 UTC (rev 94) @@ -48,7 +48,7 @@ std::ifstream f( *tc ); if ( !f ) { std::cerr << "Could not open " << *tc << "!\n"; - continue; + continue; } tokenize( f, tokens ); std::cout << " "; @@ -63,22 +63,29 @@ std::cout << std::endl; if ( show_errors(*tc) ) continue; + std::cout << "\n\n"; auto_node pptree = preparse(tokens); token_sequence().swap(tokens); + { node_display_visitor ndv(std::cout); pptree->visit(ndv); + } std::cout << std::endl; if ( show_errors(*tc) ) continue; + std::cout << "\n\n"; -//break; - auto_node flowtree = flowparse(pptree); + { + node_display_visitor ndv(std::cout); flowtree->visit(ndv); + } std::cout << std::endl; if ( show_errors(*tc) ) continue; + std::cout << "\n\n"; + } } catch ( std::exception const &e ) { Modified: trunk/samples/demos.fph =================================================================== --- trunk/samples/demos.fph 2006-09-29 03:20:58 UTC (rev 93) +++ trunk/samples/demos.fph 2006-10-05 21:04:08 UTC (rev 94) @@ -1,13 +1,17 @@ double n = 2*n; -factorial_slow n = +factorial_simple n = if n <= 1 ? 1; else ? - n * factorial_slow(n-1); - + n * factorial_simple(n-1); + +// /* + factorial (n,r=1) = if n <= 1 ? - r; + ((r)); else ? factorial( n-1, n*r ); + +// */ Modified: trunk/samples/syntax_example.fph =================================================================== --- trunk/samples/syntax_example.fph 2006-09-29 03:20:58 UTC (rev 93) +++ trunk/samples/syntax_example.fph 2006-10-05 21:04:08 UTC (rev 94) @@ -1,18 +1,18 @@ // WHAT YOU ACTUALLY TYPED -partition ( list : [T], +partition ( list : [,T,], cond : T->bool, matched : [T] = [], failed : [T] = []) = - , - , - , + , + , + , if list.empty ? ,,, ( ,, matched,, failed,, ) else if cond( list.head ) ? partition( list.tail, cond, matched.push( list.head ), failed ) - else ? + else , partition( list.tail, cond, matched, failed.push( list.head ) ); /////////////////////////////////////////////////////////////////////////////// This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <me...@us...> - 2006-10-04 04:39:32
|
Revision: 83 http://svn.sourceforge.net/fuphyl/?rev=83&view=rev Author: me22 Date: 2006-09-27 11:35:59 -0700 (Wed, 27 Sep 2006) Log Message: ----------- More non-abstract return types for implementation detail functions. Modified Paths: -------------- trunk/interpreter/preparser.cpp Modified: trunk/interpreter/preparser.cpp =================================================================== --- trunk/interpreter/preparser.cpp 2006-09-27 04:52:47 UTC (rev 82) +++ trunk/interpreter/preparser.cpp 2006-09-27 18:35:59 UTC (rev 83) @@ -18,8 +18,8 @@ typedef token_sequence_cit tsi; -auto_node preparse_expression(tsi &iter, tsi const &end); -auto_node preparse_sequence(tsi &iter, tsi const &end); +std::auto_ptr<container_node> preparse_expression(tsi &iter, tsi const &end); +std::auto_ptr<container_node> preparse_sequence(tsi &iter, tsi const &end); auto_node preparse_token(tsi &iter, tsi const &end) { if ( iter == end ) return auto_node(); @@ -29,11 +29,10 @@ return auto_node( new token_node(*iter++) ); } -auto_node preparse_sequence(tsi &iter, tsi const &end) { - if ( iter == end ) return auto_node(); +std::auto_ptr<container_node> preparse_sequence(tsi &iter, tsi const &end) { + std::auto_ptr<container_node> root; + if ( iter == end ) return root; ENTERING; - std::auto_ptr<container_node> root; - if ( iter->is( token::SYMBOL, TUPLE_BEGIN ) ) { root.reset( new tuple_node ); SHOW_TOKEN(*iter); ++iter; @@ -91,13 +90,14 @@ } } LEAVING; - return auto_node( root.release() ); + return root; } -auto_node preparse_expression(tsi &iter, tsi const &end) { - if ( iter == end ) return auto_node(); +std::auto_ptr<container_node> preparse_expression(tsi &iter, tsi const &end) { + std::auto_ptr<container_node> root; + if ( iter == end ) return root; + root.reset( new container_node ); ENTERING; - std::auto_ptr<container_node> root( new container_node ); root->start = *iter; for ( ; iter != end; ) { if ( iter->is( token::SYMBOL, OP_TYPE ) @@ -122,14 +122,14 @@ } LEAVING; - return auto_node( root.release() ); + return root; } auto_node preparse(token_sequence tokens) { ENTERING; tsi b = tokens.begin(); tsi const e = tokens.end(); - auto_node root = preparse_sequence( b, e ); + std::auto_ptr<container_node> root = preparse_sequence( b, e ); LEAVING; - return root; + return auto_node( root.release() ); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <me...@us...> - 2006-10-04 04:39:32
|
Revision: 84 http://svn.sourceforge.net/fuphyl/?rev=84&view=rev Author: me22 Date: 2006-09-27 12:07:05 -0700 (Wed, 27 Sep 2006) Log Message: ----------- templated push_back from an auto_ptr Modified Paths: -------------- trunk/interpreter/nodes.hpp Modified: trunk/interpreter/nodes.hpp =================================================================== --- trunk/interpreter/nodes.hpp 2006-09-27 18:35:59 UTC (rev 83) +++ trunk/interpreter/nodes.hpp 2006-09-27 19:07:05 UTC (rev 84) @@ -95,7 +95,8 @@ bool empty() const { return nodes.empty(); } unsigned size() const { return nodes.size(); } - void push_back( auto_node n ) { + template <typename T> + void push_back( std::auto_ptr<T> n ) { nodes.push_back( n.get() ); n.release(); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <me...@us...> - 2006-10-04 04:39:31
|
Revision: 86 http://svn.sourceforge.net/fuphyl/?rev=86&view=rev Author: me22 Date: 2006-09-27 20:42:19 -0700 (Wed, 27 Sep 2006) Log Message: ----------- removing C-style cast Modified Paths: -------------- trunk/interpreter/node_display_visitor.cpp Modified: trunk/interpreter/node_display_visitor.cpp =================================================================== --- trunk/interpreter/node_display_visitor.cpp 2006-09-28 03:30:58 UTC (rev 85) +++ trunk/interpreter/node_display_visitor.cpp 2006-09-28 03:42:19 UTC (rev 86) @@ -118,8 +118,8 @@ } else { std::cout << TUPLE_BEGIN << ' '; indent += 2; - // sequence_node const &sn = n; - visit( (sequence_node const &)n ); + sequence_node const &sn = n; + visit( sn ); indent -= 2; std::cout << ' ' << TUPLE_END; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <me...@us...> - 2006-09-27 04:52:51
|
Revision: 82 http://svn.sourceforge.net/fuphyl/?rev=82&view=rev Author: me22 Date: 2006-09-26 21:52:47 -0700 (Tue, 26 Sep 2006) Log Message: ----------- Added definition body parsing Modified Paths: -------------- trunk/interpreter/flowparser.cpp Modified: trunk/interpreter/flowparser.cpp =================================================================== --- trunk/interpreter/flowparser.cpp 2006-09-27 04:44:50 UTC (rev 81) +++ trunk/interpreter/flowparser.cpp 2006-09-27 04:52:47 UTC (rev 82) @@ -18,7 +18,7 @@ std::auto_ptr<sequence_node> flowparse_sequence(cni &b, cni const &e); std::auto_ptr<sequence_node> flowparse_statement(cni &b, cni const &e); -std::auto_ptr<definition_node> flowparse_definition(cni &b, cni const &e); +//std::auto_ptr<definition_node> flowparse_definition(cni &b, cni const &e); std::auto_ptr<conditional_node> flowparse_conditional(cni &b, cni const &e); bool is_token(node const &n, token::token_type t, std::string const &d) { @@ -112,7 +112,35 @@ root->push_back( auto_node( flowparse_conditional(++b, e) ) ); root->back()->start = start; } else if ( is_token( **b, token::SYMBOL, OP_ASSIGN ) ) { -#error + // TODO: handle type specifiers + + if ( root->empty() ) { + push_error( error::ERROR, "Missing identifier before " + OP_ASSIGN, + (*b)->start.row, (*b)->start.col ); + for ( ; b != e && !is_token( **b++, token::SYMBOL, OP_END); ); + continue; + } + std::auto_ptr<definition_node> n(new definition_node); + n->start = (*b)->start; + n->name = root->pop_back(); + n->body = flowparse_statement(b,e); + + if ( b == e ) { + push_error( error::ERROR, "Unterminated Assignment", + n->start.row, n->start.col ); + break; + } + if ( !is_token( **b, token::SYMBOL, OP_END ) ) { + push_error( error::ERROR, "Missing " + OP_END + " after definition", + (*b)->start.row, (*b)->start.col ); + for ( ; b != e && !is_token( **b++, token::SYMBOL, OP_END); ); + if ( b == e ) continue; + } + + /// step past OP_END + ++b; + + root->push_back( auto_node(n.release()) ); } } return root; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <me...@us...> - 2006-09-27 04:44:53
|
Revision: 81 http://svn.sourceforge.net/fuphyl/?rev=81&view=rev Author: me22 Date: 2006-09-26 21:44:50 -0700 (Tue, 26 Sep 2006) Log Message: ----------- typeid() needs <typeid> Modified Paths: -------------- trunk/interpreter/nodes.hpp Modified: trunk/interpreter/nodes.hpp =================================================================== --- trunk/interpreter/nodes.hpp 2006-09-27 03:06:05 UTC (rev 80) +++ trunk/interpreter/nodes.hpp 2006-09-27 04:44:50 UTC (rev 81) @@ -12,6 +12,7 @@ */ #include <memory> +#include <typeinfo> #include "tokenizer.hpp" This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <me...@us...> - 2006-09-27 03:06:07
|
Revision: 80 http://svn.sourceforge.net/fuphyl/?rev=80&view=rev Author: me22 Date: 2006-09-26 20:06:05 -0700 (Tue, 26 Sep 2006) Log Message: ----------- Added check for file not found. Modified Paths: -------------- trunk/interpreter/main.cpp Modified: trunk/interpreter/main.cpp =================================================================== --- trunk/interpreter/main.cpp 2006-09-27 02:15:22 UTC (rev 79) +++ trunk/interpreter/main.cpp 2006-09-27 03:06:05 UTC (rev 80) @@ -46,9 +46,13 @@ std::cout << "Hello world! Welcome to FuPHyL." << std::endl; for ( char const **tc = testcases; **tc; ++tc ) { - std::cout << "File: " << *tc << "\n"; + std::cout << "File: " << *tc << std::endl; token_sequence tokens; std::ifstream f( *tc ); + if ( !f ) { + std::cerr << "Could not open " << *tc << "!\n"; + continue; + } tokenize( f, tokens ); std::cout << " "; //if(0) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <me...@us...> - 2006-09-27 02:15:25
|
Revision: 79 http://svn.sourceforge.net/fuphyl/?rev=79&view=rev Author: me22 Date: 2006-09-26 19:15:22 -0700 (Tue, 26 Sep 2006) Log Message: ----------- completed flowparse_conditional Modified Paths: -------------- trunk/interpreter/flowparser.cpp trunk/interpreter/nodes.hpp Modified: trunk/interpreter/flowparser.cpp =================================================================== --- trunk/interpreter/flowparser.cpp 2006-09-27 02:14:28 UTC (rev 78) +++ trunk/interpreter/flowparser.cpp 2006-09-27 02:15:22 UTC (rev 79) @@ -11,13 +11,116 @@ #include "parser.hpp" #include "callstack_display.hpp" +#include "symbols.hpp" +#include "errors.hpp" -auto_node flowparse_sequence(auto_node root); -auto_node flowparse_statement(auto_node root); -auto_node flowparse_definition(auto_node root); -auto_node flowparse_conditional(auto_node root); +typedef container_node::iterator cni; +std::auto_ptr<sequence_node> flowparse_sequence(cni &b, cni const &e); +std::auto_ptr<sequence_node> flowparse_statement(cni &b, cni const &e); +std::auto_ptr<definition_node> flowparse_definition(cni &b, cni const &e); +std::auto_ptr<conditional_node> flowparse_conditional(cni &b, cni const &e); +bool is_token(node const &n, token::token_type t, std::string const &d) { + return n.is_a<token_node>() + && n.as_a<token_node const>().start.is(t,d); +} + +std::auto_ptr<sequence_node> flowparse_statement(cni &b, cni const &e) { + std::auto_ptr<sequence_node> seq; + if ( b == e ) return seq; + seq = flowparse_sequence(b,e); + return seq; +} + +std::auto_ptr<conditional_node> flowparse_conditional(cni &b, cni const &e) { + std::auto_ptr<conditional_node> root; + if ( b == e ) return root; + root.reset(new conditional_node); + root->start = (*b)->start; + + root->cond = flowparse_sequence(b,e); + if ( b == e ) { + push_error( error::ERROR, "Unterminated Conditional", + root->start.row, root->start.col ); + return root.reset(), root; + } + if ( !root->cond.get() ) { + push_error( error::ERROR, "Empty condition in if", + root->start.row, root->start.col ); + } + + if ( !is_token( **b, token::SYMBOL, OP_COND ) ) { + push_error( error::ERROR, "Missing " + OP_COND + " after condition", + root->start.row, root->start.col ); + } + for ( ; b != e && !is_token( **b, token::SYMBOL, OP_COND ); ++b ); + if ( b == e ) { + return root.reset(), root; + } + ++b; + if ( b == e ) { + push_error( error::ERROR, "Missing statement after conditional", + root->start.row, root->start.col ); + } + + root->true_branch = flowparse_statement(b,e); + if ( b == e ) { + push_error( error::ERROR, "Missing else in conditional", + root->start.row, root->start.col ); + return root.reset(), root; + } + + if ( !is_token( **(b+1), token::TEXT, "else" ) ) { + push_error( error::ERROR, "Missing else in conditional", + root->start.row, root->start.col ); + } + for ( ; b != e && !is_token( **(b+1), token::TEXT, "else" ); ++b ); + if ( b == e ) { + return root.reset(), root; + } + ++b; + if ( b == e ) { + push_error( error::ERROR, "Missing statement after else", + root->start.row, root->start.col ); + return root.reset(), root; + } + + root->false_branch = flowparse_statement(b,e); + if ( !is_token( **(b+1), token::SYMBOL, OP_END ) ) { + push_error( error::ERROR, "Missing " + OP_END + " after else block", + root->start.row, root->start.col ); + } + for ( ; b != e && !is_token( **(b+1), token::SYMBOL, OP_END ); ++b ); + if ( b == e ) { + return root.reset(), root; + } + + /// OP_END left under b for definition to grab + + return root; +} + +std::auto_ptr<sequence_node> flowparse_sequence(cni &b, cni const &e) { + std::auto_ptr<sequence_node> root; + if ( b == e ) return root; + root.reset(new sequence_node); + root->start = (*b)->start; + for ( ; b != e; ) { + if ( is_token( **b, token::TEXT, "if" ) ) { + token start = (*b)->start; + root->push_back( auto_node( flowparse_conditional(++b, e) ) ); + root->back()->start = start; + } else if ( is_token( **b, token::SYMBOL, OP_ASSIGN ) ) { +#error + } + } + return root; +} + auto_node flowparse(auto_node root) { - return flowparse_sequence(root); + container_node const &cn = root->as_a<container_node const>(); + cni b = cn.begin(); + cni e = cn.end(); + return auto_node( flowparse_sequence( b, e ).release() ); } Modified: trunk/interpreter/nodes.hpp =================================================================== --- trunk/interpreter/nodes.hpp 2006-09-27 02:14:28 UTC (rev 78) +++ trunk/interpreter/nodes.hpp 2006-09-27 02:15:22 UTC (rev 79) @@ -37,7 +37,7 @@ template <typename T> bool is_a() const { return typeid(T) == typeid(*this); } template <typename T> - T &as_a() const { return dynamic_cast<T const &>(this); } + T &as_a() const { return dynamic_cast<T&>(*this); } void visit(node_visitor &v); @@ -98,6 +98,10 @@ nodes.push_back( n.get() ); n.release(); } + void push_back( node * &p ) { + nodes.push_back( p ); + p = 0; + } auto_node pop_back() { auto_node val( nodes.back() ); nodes.pop_back(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <me...@us...> - 2006-09-27 02:14:33
|
Revision: 78 http://svn.sourceforge.net/fuphyl/?rev=78&view=rev Author: me22 Date: 2006-09-26 19:14:28 -0700 (Tue, 26 Sep 2006) Log Message: ----------- renamed SHOW_NODE to the correct name SHOW_TOKEN Modified Paths: -------------- trunk/interpreter/callstack_display.hpp trunk/interpreter/preparser.cpp Modified: trunk/interpreter/callstack_display.hpp =================================================================== --- trunk/interpreter/callstack_display.hpp 2006-09-27 02:13:40 UTC (rev 77) +++ trunk/interpreter/callstack_display.hpp 2006-09-27 02:14:28 UTC (rev 78) @@ -31,11 +31,11 @@ for (unsigned i=0; i<level;++i) std::cerr << ' '; \ std::cerr << "} /""/ " << __FUNCTION__ << " L" << level << "\n"; \ } while (false) -#define SHOW_NODE(n) std::cerr << (n).type_text() << "{" << (n).data << "}" +#define SHOW_TOKEN(n) std::cerr << (n).type_text() << "{" << (n).data << "}" #else #define ENTERING #define LEAVING -#define SHOW_NODE(n) +#define SHOW_TOKEN(n) #endif #endif Modified: trunk/interpreter/preparser.cpp =================================================================== --- trunk/interpreter/preparser.cpp 2006-09-27 02:13:40 UTC (rev 77) +++ trunk/interpreter/preparser.cpp 2006-09-27 02:14:28 UTC (rev 78) @@ -24,7 +24,7 @@ auto_node preparse_token(tsi &iter, tsi const &end) { if ( iter == end ) return auto_node(); ENTERING; - SHOW_NODE(*iter); + SHOW_TOKEN(*iter); LEAVING; return auto_node( new token_node(*iter++) ); } @@ -36,13 +36,13 @@ if ( iter->is( token::SYMBOL, TUPLE_BEGIN ) ) { root.reset( new tuple_node ); - SHOW_NODE(*iter); ++iter; + SHOW_TOKEN(*iter); ++iter; } else if ( iter->is( token::SYMBOL, LIST_BEGIN ) ) { root.reset( new list_node ); - SHOW_NODE(*iter); ++iter; + SHOW_TOKEN(*iter); ++iter; } else if ( iter->is( token::SYMBOL, ARRAY_BEGIN ) ) { root.reset( new array_node ); - SHOW_NODE(*iter); ++iter; + SHOW_TOKEN(*iter); ++iter; } else { root.reset( new container_node ); } @@ -63,27 +63,27 @@ if ( !root->is_a<tuple_node>() ) { push_error( error::ERROR, "Unmatched " + TUPLE_END, iter->row, iter->col ); - SHOW_NODE(*iter); ++iter; + SHOW_TOKEN(*iter); ++iter; } else { - SHOW_NODE(*iter); ++iter; + SHOW_TOKEN(*iter); ++iter; break; } } else if ( iter->is( token::SYMBOL, LIST_END ) ) { if ( !root->is_a<list_node>() ) { push_error( error::ERROR, "Unmatched " + LIST_END, iter->row, iter->col ); - SHOW_NODE(*iter); ++iter; + SHOW_TOKEN(*iter); ++iter; } else { - SHOW_NODE(*iter); ++iter; + SHOW_TOKEN(*iter); ++iter; break; } } else if ( iter->is( token::SYMBOL, ARRAY_END ) ) { if ( !root->is_a<array_node>() ) { push_error( error::ERROR, "Unmatched " + ARRAY_END, iter->row, iter->col ); - SHOW_NODE(*iter); ++iter; + SHOW_TOKEN(*iter); ++iter; } else { - SHOW_NODE(*iter); ++iter; + SHOW_TOKEN(*iter); ++iter; break; } } else { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <me...@us...> - 2006-09-27 02:13:44
|
Revision: 77 http://svn.sourceforge.net/fuphyl/?rev=77&view=rev Author: me22 Date: 2006-09-26 19:13:40 -0700 (Tue, 26 Sep 2006) Log Message: ----------- Adding files used by symbols_gen to project Modified Paths: -------------- trunk/interpreter/interpreter.cbp Modified: trunk/interpreter/interpreter.cbp =================================================================== --- trunk/interpreter/interpreter.cbp 2006-09-25 02:41:02 UTC (rev 76) +++ trunk/interpreter/interpreter.cbp 2006-09-27 02:13:40 UTC (rev 77) @@ -37,6 +37,12 @@ <Compiler> <Add option="-DDEBUGINFO" /> </Compiler> + <Unit filename="autogen.txt"> + <Option compilerVar="CPP" /> + <Option compile="0" /> + <Option link="0" /> + <Option target="symbols_gen" /> + </Unit> <Unit filename="callstack_display.hpp"> <Option compilerVar="CPP" /> <Option compile="0" /> @@ -57,6 +63,12 @@ <Option compilerVar="CPP" /> <Option target="interpreter" /> </Unit> + <Unit filename="header.txt"> + <Option compilerVar="CPP" /> + <Option compile="0" /> + <Option link="0" /> + <Option target="symbols_gen" /> + </Unit> <Unit filename="main.cpp"> <Option compilerVar="CPP" /> <Option target="interpreter" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <me...@us...> - 2006-09-25 02:41:14
|
Revision: 76 http://svn.sourceforge.net/fuphyl/?rev=76&view=rev Author: me22 Date: 2006-09-24 19:41:02 -0700 (Sun, 24 Sep 2006) Log Message: ----------- rewritten preparser -- no longer handles control flow constructs, only builds container, list, tuple, and array nodes. Modified Paths: -------------- trunk/interpreter/errors.cpp trunk/interpreter/errors.hpp trunk/interpreter/interpreter.cbp trunk/interpreter/main.cpp trunk/interpreter/nodes.cpp trunk/interpreter/nodes.hpp trunk/interpreter/parser.hpp trunk/interpreter/preparser.cpp trunk/interpreter/symbols.txt trunk/interpreter/tokenizer.hpp Added Paths: ----------- trunk/interpreter/callstack_display.hpp trunk/interpreter/flowparser.cpp trunk/interpreter/node_display_visitor.cpp Added: trunk/interpreter/callstack_display.hpp =================================================================== --- trunk/interpreter/callstack_display.hpp (rev 0) +++ trunk/interpreter/callstack_display.hpp 2006-09-25 02:41:02 UTC (rev 76) @@ -0,0 +1,41 @@ +#ifndef CALLSTACK_DISPLAY_HPP +#define CALLSTACK_DISPLAY_HPP + +/* + * callstack_display.hpp + * + * Copyright (c) 2006 Scott McMurray + * + * Licensed under the Open Software License version 3.0 + * ( See http://opensource.org/licenses/osl-3.0.php ) + * + */ + +#ifdef DEBUGINFO +#include <iostream> +namespace { +unsigned level = 0; +} // namespace +#define ENTERING \ +do { \ + std::cerr << "\n"; \ + for (unsigned i=0; i<level;++i) std::cerr << ' '; \ + std::cerr << "Join " << __FUNCTION__ << " L" << level << " {\n"; \ + for (unsigned i=0; i<level+1;++i) std::cerr << ' '; \ + ++level; \ +} while (false) +#define LEAVING \ +do { \ + std::cerr << "\n"; \ + --level; \ + for (unsigned i=0; i<level;++i) std::cerr << ' '; \ + std::cerr << "} /""/ " << __FUNCTION__ << " L" << level << "\n"; \ +} while (false) +#define SHOW_NODE(n) std::cerr << (n).type_text() << "{" << (n).data << "}" +#else +#define ENTERING +#define LEAVING +#define SHOW_NODE(n) +#endif + +#endif Modified: trunk/interpreter/errors.cpp =================================================================== --- trunk/interpreter/errors.cpp 2006-09-23 04:26:20 UTC (rev 75) +++ trunk/interpreter/errors.cpp 2006-09-25 02:41:02 UTC (rev 76) @@ -1,49 +1,56 @@ -/* - * errors.cpp - * - * Copyright (c) 2006 Scott McMurray - * - * Licensed under the Open Software License version 3.0 - * ( See http://opensource.org/licenses/osl-3.0.php ) - * - */ - -#include "errors.hpp" - -#include <deque> - -char const *error::level_text() const { - static const char *_[] = { "Debug", "Log", "Warning", "Error", "Failure" }; - return _[level]; -} - - -#ifndef UNNAMED -#define UNNAMED -#endif -namespace UNNAMED { - std::deque<error> errors; -} - -bool is_error() { - return !errors.empty(); -} -error const &first_error() { - return errors.at(0); -} -void pop_error() { - errors.at(0); - errors.pop_front(); -} - -void push_error(unsigned short level, std::string const &message, - unsigned row /*= -1*/, unsigned short col /*= -1*/, - std::string const &data /*= ""*/) { - error e; - e.level = level; - e.message = message; - e.row = row; - e.col = col; - e.data = data; - errors.push_back(e); -} +/* + * errors.cpp + * + * Copyright (c) 2006 Scott McMurray + * + * Licensed under the Open Software License version 3.0 + * ( See http://opensource.org/licenses/osl-3.0.php ) + * + */ + +#include "errors.hpp" + +#include <deque> + +char const *error::level_text() const { + static const char *_[] = { "Debug", "Log", "Warning", "Error", "Failure" }; + return _[level]; +} + + +#ifndef UNNAMED +#define UNNAMED +#endif +namespace UNNAMED { + std::deque<error> errors; +} + +bool is_error() { + return !errors.empty(); +} +error const &first_error() { + return errors.at(0); +} +error const &last_error() { + return errors.at( errors.size()-1 ); +} +void pop_first_error() { + first_error(); + errors.pop_front(); +} +void pop_last_error() { + last_error(); + errors.pop_back(); +} + +void push_error(unsigned short level, std::string const &message, + unsigned row /*= -1*/, unsigned short col /*= -1*/, + std::string const &data /*= ""*/) { + error e; + e.level = level; + e.message = message; + e.row = row; + e.col = col; + e.data = data; + errors.push_back(e); +} Modified: trunk/interpreter/errors.hpp =================================================================== --- trunk/interpreter/errors.hpp 2006-09-23 04:26:20 UTC (rev 75) +++ trunk/interpreter/errors.hpp 2006-09-25 02:41:02 UTC (rev 76) @@ -1,39 +1,46 @@ -#ifndef ERRORS_HPP -#define ERRORS_HPP - -/* - * errors.hpp - * - * Copyright (c) 2006 Scott McMurray - * - * Licensed under the Open Software License version 3.0 - * ( See http://opensource.org/licenses/osl-3.0.php ) - * - */ - -#include <string> - -struct error { - enum { DEBUG, LOG, WARNING, ERROR, FAILURE }; - std::string message; - std::string data; - unsigned row; - unsigned short col; - unsigned short level; - char const *level_text() const; -}; - -bool is_error(); -error const &first_error(); -void pop_error(); -inline error const get_error() { - error e = first_error(); - pop_error(); - return e; -} - -void push_error(unsigned short level, std::string const &message, - unsigned row = -1, unsigned short col = -1, - std::string const &data = ""); - -#endif +#ifndef ERRORS_HPP +#define ERRORS_HPP + +/* + * errors.hpp + * + * Copyright (c) 2006 Scott McMurray + * + * Licensed under the Open Software License version 3.0 + * ( See http://opensource.org/licenses/osl-3.0.php ) + * + */ + +#include <string> + +struct error { + enum { DEBUG, LOG, WARNING, ERROR, FAILURE }; + std::string message; + std::string data; + unsigned row; + unsigned short col; + unsigned short level; + char const *level_text() const; +}; + +bool is_error(); +error const &first_error(); +error const &last_error(); +void pop_first_error(); +void pop_last_error(); +inline error const get_first_error() { + error e = first_error(); + pop_first_error(); + return e; +} +inline error const get_last_error() { + error e = last_error(); + pop_last_error(); + return e; +} + +void push_error(unsigned short level, std::string const &message, + unsigned row = -1, unsigned short col = -1, + std::string const &data = ""); + +#endif Added: trunk/interpreter/flowparser.cpp =================================================================== --- trunk/interpreter/flowparser.cpp (rev 0) +++ trunk/interpreter/flowparser.cpp 2006-09-25 02:41:02 UTC (rev 76) @@ -0,0 +1,23 @@ +/* + * flowparser.cpp + * + * Copyright (c) 2006 Scott McMurray + * + * Licensed under the Open Software License version 3.0 + * ( See http://opensource.org/licenses/osl-3.0.php ) + * + */ + +#include "parser.hpp" + +#include "callstack_display.hpp" + +auto_node flowparse_sequence(auto_node root); +auto_node flowparse_statement(auto_node root); +auto_node flowparse_definition(auto_node root); +auto_node flowparse_conditional(auto_node root); + + +auto_node flowparse(auto_node root) { + return flowparse_sequence(root); +} Modified: trunk/interpreter/interpreter.cbp =================================================================== --- trunk/interpreter/interpreter.cbp 2006-09-23 04:26:20 UTC (rev 75) +++ trunk/interpreter/interpreter.cbp 2006-09-25 02:41:02 UTC (rev 76) @@ -34,6 +34,15 @@ <VirtualTargets> <Add alias="All" targets="symbols_gen;interpreter;" /> </VirtualTargets> + <Compiler> + <Add option="-DDEBUGINFO" /> + </Compiler> + <Unit filename="callstack_display.hpp"> + <Option compilerVar="CPP" /> + <Option compile="0" /> + <Option link="0" /> + <Option target="interpreter" /> + </Unit> <Unit filename="errors.cpp"> <Option compilerVar="CPP" /> <Option target="interpreter" /> @@ -44,6 +53,10 @@ <Option link="0" /> <Option target="interpreter" /> </Unit> + <Unit filename="flowparser.cpp"> + <Option compilerVar="CPP" /> + <Option target="interpreter" /> + </Unit> <Unit filename="main.cpp"> <Option compilerVar="CPP" /> <Option target="interpreter" /> Modified: trunk/interpreter/main.cpp =================================================================== --- trunk/interpreter/main.cpp 2006-09-23 04:26:20 UTC (rev 75) +++ trunk/interpreter/main.cpp 2006-09-25 02:41:02 UTC (rev 76) @@ -19,15 +19,29 @@ //"../samples/quick_sort.fph", //"../samples/merge_sort.fph", //"../samples/insertion_sort.fph", - "../samples/lists.fph", + ///"../samples/lists.fph", //"../samples/set.fph", //"../samples/simple_sorts.fph", //"../samples/syntax_example.fph", - //"../samples/demos.fph", + "../samples/demos.fph", "" }; -int main(/*int argc, char *argv[]*/) { +bool show_errors(std::string const &file) { + if ( !is_error() ) return false; + for ( error e; is_error(); ) { + e = get_first_error(); + std::cerr << file << ": " << e.level_text() + << ": L" << e.row << "C" << e.col + << ": " << e.message; + if ( !e.data.empty() ) std::cerr << " ( " << e.data << " )"; + std::cerr << std::endl; + } + std::cerr << std::endl; + return true; +} +int main(/*int argc, char *argv[]*/) try { + system("g++ --version"); std::cout << "Hello world! Welcome to FuPHyL." << std::endl; @@ -46,25 +60,19 @@ << " "; } std::cout << std::endl; - bool hit_error = false; -//if(0) - for ( error e; is_error() && (e=get_error(),true); hit_error = true) { - std::cerr << *tc << ": " << e.level_text() - << ": L" << e.row << "C" << e.col - << ": " << e.message; - if ( !e.data.empty() ) std::cerr << " ( " << e.data << " )"; - std::cerr << std::endl; - } - std::cerr << std::endl; -// std::cerr << "Hit Error: " << std::boolalpha << hit_error << std::endl; - if ( hit_error ) continue; - hit_error = false; + if ( show_errors(*tc) ) continue; auto_node pptree = preparse(tokens); + token_sequence().swap(tokens); node_display_visitor ndv; pptree->visit(ndv); std::cout << '\n'; + + if ( show_errors(*tc) ) continue; } +} catch ( std::exception const &e ) { +} catch ( char const *s ) { + std::cerr << "Someone threw: " << s << "\n"; } Added: trunk/interpreter/node_display_visitor.cpp =================================================================== --- trunk/interpreter/node_display_visitor.cpp (rev 0) +++ trunk/interpreter/node_display_visitor.cpp 2006-09-25 02:41:02 UTC (rev 76) @@ -0,0 +1,152 @@ +/* + * node_display_visitor.hpp + * + * Copyright (c) 2006 Scott McMurray + * + * Licensed under the Open Software License version 3.0 + * ( See http://opensource.org/licenses/osl-3.0.php ) + * + */ + +#include "nodes.hpp" +#include "symbols.hpp" + +#include <iostream> +#include <string> +#include <cassert> + +/* +std::string operator*(std::string const &s, unsigned n) { + if ( n == 0 ) return ""; + if ( n == 1 ) return s; + + if ( n & 1 ) { + return s*(n-1)+s; + } else { + return (s+s)*(n/2); + } +} +*/ +std::string indent_(unsigned n) { + std::string s; + while ( n-- ) s.push_back(' '); + return s; +// return std::string(" ")*n; +} + +#define INDENT indent_(indent) + +#if 0 +DEFINE_NODE_VISIT( node_display_visitor, node, /*n*/ ) { + std::cout << INDENT << "Visited Base Node -- This shouldn't ever happen\n"; +} +#endif + +DEFINE_NODE_VISIT( node_display_visitor, token_node, n ) { +// std::cout << n.data.type_text() << '{' << n.data.data << '}' << ' '; + std::cout << n.start.data; +} + +DEFINE_NODE_VISIT( node_display_visitor, conditional_node, n ) { + std::cout << "if "; + n.cond->visit(*this); + indent += 4; + std::cout << ' ' << OP_COND << '\n' << INDENT; + n.true_branch->visit(*this); + std::cout << OP_END << '\n'; + indent -= 4; + std::cout << INDENT << "else "; + indent += 4; + if ( !n.false_branch->is_a<conditional_node>() ) { + std::cout << OP_COND << '\n' << INDENT; + } + n.false_branch->visit(*this); + indent -= 4; +} + +DEFINE_NODE_VISIT( node_display_visitor, operator_node, /*n*/ ) { + std::cout << "Not Yet Defined -- " << __FUNCTION__ << "\n"; +} + +DEFINE_NODE_VISIT( node_display_visitor, definition_node, n ) { + n.name->visit(*this); + indent += 4; + if ( n.type.get() ) { + std::cout << ' ' << OP_TYPE << ' '; + n.type->visit(*this); + } + std::cout << ' ' << OP_ASSIGN << '\n' << INDENT; + n.body->visit(*this); + std::cout << OP_END; + indent -= 4; +} + +DEFINE_NODE_VISIT( node_display_visitor, function_node, /*n*/ ) { + std::cout << "Not Yet Defined -- " << __FUNCTION__ << "\n"; +} + +DEFINE_NODE_VISIT( node_display_visitor, container_node, n ) { + container_node::iterator const begin = n.begin(); + container_node::iterator iter = begin; + container_node::iterator const end = n.end(); + + for ( ; iter != end; ++iter ) { +// assert( (*iter)->is_a<token_node>() ); + if ( begin != iter ) std::cout << ' '; + (*iter)->visit(*this); + } +} + +DEFINE_NODE_VISIT( node_display_visitor, sequence_node, n ) { + container_node::iterator const begin = n.begin(); + container_node::iterator iter = begin; + container_node::iterator const end = n.end(); + + for ( ; iter != end; ++iter ) { +// assert( !(*iter)->is_a<token_node>() ); + if ( begin != iter ) { + if ( !(*(iter-1))->is_a<definition_node>() ) std::cout << OP_SEP; + std::cout << '\n' << INDENT; + } + (*iter)->visit(*this); + } +} + +DEFINE_NODE_VISIT( node_display_visitor, tuple_node, n ) { + if ( n.empty() ) { + std::cout << TUPLE_BEGIN << LIST_END; + } else { + std::cout << TUPLE_BEGIN << ' '; + indent += 2; + // sequence_node const &sn = n; + visit( (sequence_node const &)n ); + indent -= 2; + std::cout << ' ' << TUPLE_END; + } +} + +DEFINE_NODE_VISIT( node_display_visitor, list_node, n ) { + if ( n.empty() ) { + std::cout << LIST_BEGIN << LIST_END; + } else { + std::cout << LIST_BEGIN << ' '; + indent += 2; + // sequence_node const &sn = n; + visit( (sequence_node const &)n ); + indent -= 2; + std::cout << ' ' << LIST_END; + } +} + +DEFINE_NODE_VISIT( node_display_visitor, array_node, n ) { + if ( n.empty() ) { + std::cout << ARRAY_BEGIN << ARRAY_END; + } else { + std::cout << ARRAY_BEGIN << ' '; + indent += 2; + // sequence_node const &sn = n; + visit( (sequence_node const &)n ); + indent -= 2; + std::cout << ' ' << ARRAY_END; + } +} Modified: trunk/interpreter/nodes.cpp =================================================================== --- trunk/interpreter/nodes.cpp 2006-09-23 04:26:20 UTC (rev 75) +++ trunk/interpreter/nodes.cpp 2006-09-25 02:41:02 UTC (rev 76) @@ -54,4 +54,5 @@ DEFINE_NODE_VISIT_IMPL( sequence_node ) DEFINE_NODE_VISIT_IMPL( list_node ) DEFINE_NODE_VISIT_IMPL( tuple_node ) +DEFINE_NODE_VISIT_IMPL( array_node ) // DEFINE_NODE_VISIT_IMPL( ) Modified: trunk/interpreter/nodes.hpp =================================================================== --- trunk/interpreter/nodes.hpp 2006-09-23 04:26:20 UTC (rev 75) +++ trunk/interpreter/nodes.hpp 2006-09-25 02:41:02 UTC (rev 76) @@ -30,24 +30,28 @@ LITERAL=1<<1, _ }; /*attribute_mask*/unsigned attr; + token start; template <typename T> - bool is_a() const { return dynamic_cast<T const *>(this); } + bool derived_from() const { return dynamic_cast<T const *>(this); } template <typename T> + bool is_a() const { return typeid(T) == typeid(*this); } + template <typename T> T &as_a() const { return dynamic_cast<T const &>(this); } void visit(node_visitor &v); + node() {} + node(token const &t) : start(t) {} + virtual ~node() {}; protected: DECLARE_NODE_VISIT_IMPL=0; }; struct token_node : node { - token data; - token_node() {} - token_node(token const &t) : data(t) {} + token_node(token const &t) : node(t) {} DECLARE_NODE_VISIT_IMPL; }; @@ -113,15 +117,17 @@ struct sequence_node : container_node { DECLARE_NODE_VISIT_IMPL; }; -struct list_node : container_node { +struct list_node : sequence_node { DECLARE_NODE_VISIT_IMPL; }; -struct tuple_node : container_node { +struct tuple_node : sequence_node { DECLARE_NODE_VISIT_IMPL; }; +struct array_node : sequence_node { + DECLARE_NODE_VISIT_IMPL; +}; struct node_visitor { - virtual void visit(node const &)=0; virtual void visit(token_node const &)=0; virtual void visit(conditional_node const &)=0; virtual void visit(operator_node const &)=0; @@ -131,13 +137,13 @@ virtual void visit(sequence_node const &)=0; virtual void visit(list_node const &)=0; virtual void visit(tuple_node const &)=0; + virtual void visit(array_node const &)=0; protected: virtual ~node_visitor() {}; }; #define DEFINE_STATEFUL_NODE_VISITOR(name,state) \ struct name : node_visitor { \ - virtual void visit(node const &); \ virtual void visit(token_node const &); \ virtual void visit(conditional_node const &); \ virtual void visit(operator_node const &); \ @@ -147,6 +153,7 @@ virtual void visit(sequence_node const &); \ virtual void visit(list_node const &); \ virtual void visit(tuple_node const &); \ + virtual void visit(array_node const &); \ state \ } Modified: trunk/interpreter/parser.hpp =================================================================== --- trunk/interpreter/parser.hpp 2006-09-23 04:26:20 UTC (rev 75) +++ trunk/interpreter/parser.hpp 2006-09-25 02:41:02 UTC (rev 76) @@ -17,5 +17,6 @@ #include "nodes.hpp" auto_node preparse(token_sequence tokens); +auto_node flowparse(auto_node root); #endif Modified: trunk/interpreter/preparser.cpp =================================================================== --- trunk/interpreter/preparser.cpp 2006-09-23 04:26:20 UTC (rev 75) +++ trunk/interpreter/preparser.cpp 2006-09-25 02:41:02 UTC (rev 76) @@ -11,103 +11,81 @@ #include "parser.hpp" #include "symbols.hpp" +#include "errors.hpp" #include <cassert> -#ifndef NDEBUG -#include <iostream> -namespace { -unsigned level = 0; -} // namespace -#define ENTERING \ -{ \ - std::cerr << "\n"; \ - for (unsigned i=0; i<level;++i) std::cerr << ' '; \ - std::cerr << "Join " << __FUNCTION__ << " L" << level << " {\n"; \ - ++level; \ -} -#define LEAVING \ -{ \ - std::cerr << "\n"; \ - --level; \ - for (unsigned i=0; i<level;++i) std::cerr << ' '; \ - std::cerr << "} // " << __FUNCTION__ << " L" << level << "\n"; \ -} -#define SHOW_ITER std::cerr << iter->type_text() << "{" << iter->data << "}" -#else -#define ENTERING -#define LEAVING -#define SHOW_ITER -#endif +#include "callstack_display.hpp" typedef token_sequence_cit tsi; -auto_node preparse_tuple(tsi &iter, tsi const &end); auto_node preparse_expression(tsi &iter, tsi const &end); -auto_node preparse_type(tsi &iter, tsi const &end); -auto_node preparse_list(tsi &iter, tsi const &end); -auto_node preparse_statement(tsi &iter, tsi const &end); auto_node preparse_sequence(tsi &iter, tsi const &end); -auto_node preparse_conditional(tsi &iter, tsi const &end); -//auto_node preparse_(tsi &iter, tsi const &end); -auto_node preparse_sequence(tsi &iter, tsi const &end) { +auto_node preparse_token(tsi &iter, tsi const &end) { + if ( iter == end ) return auto_node(); ENTERING; - std::auto_ptr<sequence_node> root( new sequence_node ); - while ( iter != end ) { - root->push_back( preparse_expression(iter,end) ); - } + SHOW_NODE(*iter); LEAVING; - return auto_node( root.release() ); + return auto_node( new token_node(*iter++) ); } -auto_node preparse_statement(tsi &iter, tsi const &end) { +auto_node preparse_sequence(tsi &iter, tsi const &end) { + if ( iter == end ) return auto_node(); ENTERING; - std::auto_ptr<sequence_node> root( new sequence_node ); - while ( iter != end ) { - token const &t = *iter; - if ( t.is( token::SYMBOL, OP_END ) ) { -SHOW_ITER; - ++iter; - break; - } else if ( t.is( token::SYMBOL, TUPLE_END ) - || t.is( token::SYMBOL, LIST_END ) ) { - break; - } else if ( t.is( token::TEXT, "else" ) ) { - break; - } else { - root->push_back( preparse_expression(iter,end) ); - } - } -LEAVING; - return auto_node( root.release() ); -} + std::auto_ptr<container_node> root; -auto_node preparse_list(tsi &iter, tsi const &end) { -ENTERING; - std::auto_ptr<list_node> root( new list_node ); - while ( iter != end ) { - token const &t = *iter; - if ( t.is( token::SYMBOL, LIST_END ) ) { -SHOW_ITER; - ++iter; - break; - } else { - root->push_back( preparse_expression(iter,end) ); - } + if ( iter->is( token::SYMBOL, TUPLE_BEGIN ) ) { + root.reset( new tuple_node ); + SHOW_NODE(*iter); ++iter; + } else if ( iter->is( token::SYMBOL, LIST_BEGIN ) ) { + root.reset( new list_node ); + SHOW_NODE(*iter); ++iter; + } else if ( iter->is( token::SYMBOL, ARRAY_BEGIN ) ) { + root.reset( new array_node ); + SHOW_NODE(*iter); ++iter; + } else { + root.reset( new container_node ); } -LEAVING; - return auto_node( root.release() ); -} -auto_node preparse_tuple(tsi &iter, tsi const &end) { -ENTERING; - std::auto_ptr<tuple_node> root( new tuple_node ); - while ( iter != end ) { - token const &t = *iter; - if ( t.is( token::SYMBOL, TUPLE_END ) ) { -SHOW_ITER; - ++iter; - break; + root->start = *iter; + + for ( ; iter != end; ) { + if ( iter->is( token::SYMBOL, OP_TYPE ) + || iter->is( token::SYMBOL, OP_COND ) + || iter->is( token::SYMBOL, OP_END ) + || iter->is( token::SYMBOL, OP_RESULT ) + || iter->is( token::SYMBOL, OP_ASSIGN ) + || iter->is( token::SYMBOL, OP_SEP ) + || iter->is( token::TEXT, "if" ) + || iter->is( token::TEXT, "else" ) ) { + root->push_back( preparse_token(iter,end) ); + } else if ( iter->is( token::SYMBOL, TUPLE_END ) ) { + if ( !root->is_a<tuple_node>() ) { + push_error( error::ERROR, "Unmatched " + TUPLE_END, + iter->row, iter->col ); + SHOW_NODE(*iter); ++iter; + } else { + SHOW_NODE(*iter); ++iter; + break; + } + } else if ( iter->is( token::SYMBOL, LIST_END ) ) { + if ( !root->is_a<list_node>() ) { + push_error( error::ERROR, "Unmatched " + LIST_END, + iter->row, iter->col ); + SHOW_NODE(*iter); ++iter; + } else { + SHOW_NODE(*iter); ++iter; + break; + } + } else if ( iter->is( token::SYMBOL, ARRAY_END ) ) { + if ( !root->is_a<array_node>() ) { + push_error( error::ERROR, "Unmatched " + ARRAY_END, + iter->row, iter->col ); + SHOW_NODE(*iter); ++iter; + } else { + SHOW_NODE(*iter); ++iter; + break; + } } else { root->push_back( preparse_expression(iter,end) ); } @@ -116,74 +94,37 @@ return auto_node( root.release() ); } -auto_node preparse_conditional(tsi &iter, tsi const &end) { -ENTERING; - std::auto_ptr<conditional_node> root( new conditional_node ); - root->cond = preparse_expression(iter,end); - assert( iter->is(token::SYMBOL, OP_COND) ); -SHOW_ITER; - ++iter; - root->true_branch = preparse_statement(iter,end); - assert( iter->is(token::TEXT, "else") ); -SHOW_ITER; - ++iter; - if ( iter->is(token::SYMBOL, OP_COND ) ) { SHOW_ITER; ++iter; } - root->false_branch = preparse_statement(iter,end); - - /// back up to let caller catch the OP_END too - --iter; -LEAVING; - return auto_node( root.release() ); -} - auto_node preparse_expression(tsi &iter, tsi const &end) { + if ( iter == end ) return auto_node(); ENTERING; std::auto_ptr<container_node> root( new container_node ); - while ( iter != end ) { - token const &t = *iter; SHOW_ITER; ++iter; - - if ( t.is( token::SYMBOL, OP_END ) - || t.is( token::SYMBOL, LIST_END ) - || t.is( token::SYMBOL, TUPLE_END ) - || t.is( token::SYMBOL, OP_COND ) ) { - --iter; + root->start = *iter; + for ( ; iter != end; ) { + if ( iter->is( token::SYMBOL, OP_TYPE ) + || iter->is( token::SYMBOL, OP_COND ) + || iter->is( token::SYMBOL, OP_END ) + || iter->is( token::SYMBOL, OP_RESULT ) + || iter->is( token::SYMBOL, OP_ASSIGN ) + || iter->is( token::SYMBOL, OP_SEP ) + || iter->is( token::TEXT, "if" ) + || iter->is( token::TEXT, "else" ) + || iter->is( token::SYMBOL, TUPLE_END ) + || iter->is( token::SYMBOL, LIST_END ) + || iter->is( token::SYMBOL, ARRAY_END ) ) { break; - } else if ( t.is( token::SYMBOL, OP_SEP ) ) { - break; - } else if ( t.is( token::TEXT, "else" ) ) { - --iter; - break; - } else if ( t.is( token::TEXT, "if" ) ) { - root->push_back( preparse_conditional(iter,end) ); - break; - } else if ( t.is( token::SYMBOL, OP_TYPE ) ) { - std::auto_ptr< definition_node > n( new definition_node ); - n->name = root; - n->attr |= node::TYPE; - n->body = preparse_expression(iter,end); - LEAVING; - return auto_node( n.release() ); - } else if ( t.is( token::SYMBOL, OP_ASSIGN ) ) { - std::auto_ptr< definition_node > n( new definition_node ); - n->name = root; - n->body = preparse_statement(iter,end); - LEAVING; - return auto_node( n.release() ); - } else if ( t.is( token::SYMBOL, TUPLE_BEGIN ) ) { - root->push_back( preparse_tuple(iter,end) ); - } else if ( t.is( token::SYMBOL, LIST_BEGIN ) ) { - root->push_back( preparse_list(iter,end) ); + } else if ( iter->is( token::SYMBOL, TUPLE_BEGIN ) + || iter->is( token::SYMBOL, LIST_BEGIN ) + || iter->is( token::SYMBOL, ARRAY_BEGIN ) ) { + root->push_back( preparse_sequence(iter,end) ); } else { - root->push_back( auto_node( new token_node(t) ) ); + root->push_back( preparse_token(iter,end) ); } } + LEAVING; return auto_node( root.release() ); } - -// throw __FUNCTION__; - auto_node preparse(token_sequence tokens) { ENTERING; tsi b = tokens.begin(); Modified: trunk/interpreter/symbols.txt =================================================================== --- trunk/interpreter/symbols.txt 2006-09-23 04:26:20 UTC (rev 75) +++ trunk/interpreter/symbols.txt 2006-09-25 02:41:02 UTC (rev 76) @@ -35,7 +35,6 @@ LIST_BEGIN [ LIST_END ] -//use OP_SEP instead// LIST_SEP , LIST_JOIN ++ //LIST_SPLIT * LIST_PREP >> @@ -43,11 +42,13 @@ TUPLE_BEGIN ( TUPLE_END ) -//use OP_SEP instead// TUPLE_SEP , TUPLE_REP $ TUPLE_GET @ TUPLE_SET @= +ARRAY_BEGIN { +ARRAY_END } + BITWISE_AND & BITWISE_OR | BITWISE_XOR ^ Modified: trunk/interpreter/tokenizer.hpp =================================================================== --- trunk/interpreter/tokenizer.hpp 2006-09-23 04:26:20 UTC (rev 75) +++ trunk/interpreter/tokenizer.hpp 2006-09-25 02:41:02 UTC (rev 76) @@ -30,6 +30,7 @@ bool is( token_type t, std::string const &d ) const { return is(t) && data == d; } + token() : data(), row(), col(), type(NOTHING) {} }; // explicit instantiation for likely separate compilation speedup This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <me...@us...> - 2006-09-23 04:26:25
|
Revision: 75 http://svn.sourceforge.net/fuphyl/?rev=75&view=rev Author: me22 Date: 2006-09-22 21:26:20 -0700 (Fri, 22 Sep 2006) Log Message: ----------- pretty printer working for simple cases. Modified Paths: -------------- trunk/interpreter/interpreter.cbp trunk/interpreter/main.cpp trunk/interpreter/nodes.cpp trunk/interpreter/nodes.hpp trunk/interpreter/preparser.cpp trunk/interpreter/tokenizer.cpp Modified: trunk/interpreter/interpreter.cbp =================================================================== --- trunk/interpreter/interpreter.cbp 2006-09-23 01:07:11 UTC (rev 74) +++ trunk/interpreter/interpreter.cbp 2006-09-23 04:26:20 UTC (rev 75) @@ -48,6 +48,10 @@ <Option compilerVar="CPP" /> <Option target="interpreter" /> </Unit> + <Unit filename="node_display_visitor.cpp"> + <Option compilerVar="CPP" /> + <Option target="interpreter" /> + </Unit> <Unit filename="nodes.cpp"> <Option compilerVar="CPP" /> <Option target="interpreter" /> @@ -68,6 +72,10 @@ <Option link="0" /> <Option target="interpreter" /> </Unit> + <Unit filename="preparser.cpp"> + <Option compilerVar="CPP" /> + <Option target="interpreter" /> + </Unit> <Unit filename="scope.cpp"> <Option compilerVar="CPP" /> <Option target="interpreter" /> Modified: trunk/interpreter/main.cpp =================================================================== --- trunk/interpreter/main.cpp 2006-09-23 01:07:11 UTC (rev 74) +++ trunk/interpreter/main.cpp 2006-09-23 04:26:20 UTC (rev 75) @@ -19,11 +19,11 @@ //"../samples/quick_sort.fph", //"../samples/merge_sort.fph", //"../samples/insertion_sort.fph", - //"../samples/lists.fph", + "../samples/lists.fph", //"../samples/set.fph", //"../samples/simple_sorts.fph", //"../samples/syntax_example.fph", - "../samples/demos.fph", + //"../samples/demos.fph", "" }; int main(/*int argc, char *argv[]*/) { @@ -46,8 +46,9 @@ << " "; } std::cout << std::endl; + bool hit_error = false; //if(0) - for ( error e; is_error() && (e=get_error(),true); ) { + for ( error e; is_error() && (e=get_error(),true); hit_error = true) { std::cerr << *tc << ": " << e.level_text() << ": L" << e.row << "C" << e.col << ": " << e.message; @@ -55,6 +56,15 @@ std::cerr << std::endl; } std::cerr << std::endl; + +// std::cerr << "Hit Error: " << std::boolalpha << hit_error << std::endl; + if ( hit_error ) continue; + hit_error = false; + + auto_node pptree = preparse(tokens); + node_display_visitor ndv; + pptree->visit(ndv); + std::cout << '\n'; } } Modified: trunk/interpreter/nodes.cpp =================================================================== --- trunk/interpreter/nodes.cpp 2006-09-23 01:07:11 UTC (rev 74) +++ trunk/interpreter/nodes.cpp 2006-09-23 04:26:20 UTC (rev 75) @@ -8,77 +8,50 @@ * */ +#include "nodes.hpp" + #include <cassert> #ifndef NDEBUG #include <iostream> #endif -#include "nodes.hpp" - void node::visit(node_visitor &v) { - assert( std::cout << "Visiting a " << typeid(v).name() +#if 0 + assert( std::cout << "Visiting a " << typeid(*this).name() << " at " << &v << " with a " << typeid(v).name() << ".\n" ); return visit_impl(v); +#elif 0 + assert( std::cout << '{' ); + visit_impl(v); + assert( std::cout << '}' ); +#elif 0 + bool skip = false + || is_a<token_node>() + || is_a<tuple_node>() + || is_a<list_node>() + || is_a<container_node>() + || is_a<conditional_node>() + || false; + if ( !skip ) + assert( std::cout << '{' << typeid(*this).name() << '}' ); + visit_impl(v); + if ( !skip ) + assert( std::cout << '{' << '/' << typeid(*this).name() << '}' ); +#else + return visit_impl(v); +#endif } -#define DEFINE_VISIT_IMPL(T) \ -void T::visit_impl(node_visitor &v) { v.visit(*this); } -DEFINE_VISIT_IMPL( token_node ) -DEFINE_VISIT_IMPL( conditional_node ) -DEFINE_VISIT_IMPL( operator_node ) -DEFINE_VISIT_IMPL( definition_node ) -DEFINE_VISIT_IMPL( function_node ) -DEFINE_VISIT_IMPL( sequence_node ) -DEFINE_VISIT_IMPL( list_node ) -DEFINE_VISIT_IMPL( tuple_node ) -// DEFINE_VISIT_IMPL( ) - -/* -std::auto_ptr<sequence_node> cons( std::auto_ptr<node> h ) { - std::auto_ptr<sequence_node> c( new sequence_node ); - c->head = h; - return c; -} -std::auto_ptr<sequence_node> cons( node *h ) { - return cons( std::auto_ptr<node>(h) ); -} - -std::auto_ptr<sequence_node> cons( std::auto_ptr<node> h, - std::auto_ptr<sequence_node> t ) { - if ( !t.get() ) { - return cons(h); - } else if ( !t->head.get() ) { - t->head = h; - return t; - } else { - std::auto_ptr<sequence_node> c( new sequence_node ); - c->head = h; - c->tail = t; - return c; - } -} -std::auto_ptr<sequence_node> cons( node* h, - std::auto_ptr<sequence_node> t ) { - return cons( std::auto_ptr<node>(h), t ); -} - -bool sequence_node::empty() const { - assert( head.get() || !tail.get() ); - return !head.get(); -} -unsigned sequence_node::size() const { - assert( head.get() || !tail.get() ); - if ( !tail.get() ) { - if ( head.get() ) { - return 1u; - } else { - return 0u; - } - } else { - return tail->size() + 1; - } -} -*/ +DEFINE_NODE_VISIT_IMPL( token_node ) +DEFINE_NODE_VISIT_IMPL( conditional_node ) +DEFINE_NODE_VISIT_IMPL( operator_node ) +DEFINE_NODE_VISIT_IMPL( definition_node ) +DEFINE_NODE_VISIT_IMPL( function_node ) +DEFINE_NODE_VISIT_IMPL( container_node ) +DEFINE_NODE_VISIT_IMPL( sequence_node ) +DEFINE_NODE_VISIT_IMPL( list_node ) +DEFINE_NODE_VISIT_IMPL( tuple_node ) +// DEFINE_NODE_VISIT_IMPL( ) Modified: trunk/interpreter/nodes.hpp =================================================================== --- trunk/interpreter/nodes.hpp 2006-09-23 01:07:11 UTC (rev 74) +++ trunk/interpreter/nodes.hpp 2006-09-23 04:26:20 UTC (rev 75) @@ -19,6 +19,9 @@ #define DECLARE_NODE_VISIT_IMPL \ virtual void visit_impl(node_visitor &) +#define DEFINE_NODE_VISIT_IMPL(T) \ +void T::visit_impl(node_visitor &v) { v.visit(*this); } + struct node; typedef std::auto_ptr<node> auto_node; @@ -26,20 +29,13 @@ enum attribute_mask { CONST=1<<0, LITERAL=1<<1, _ }; - attribute_mask attributes; + /*attribute_mask*/unsigned attr; template <typename T> - bool is_a() { return dynamic_cast<T*>(this); } + bool is_a() const { return dynamic_cast<T const *>(this); } template <typename T> - T &as_a() { return dynamic_cast<T&>(this); } + T &as_a() const { return dynamic_cast<T const &>(this); } -/* - template <typename T> - static auto_node create() { - return auto_node( new T ); - } -*/ - void visit(node_visitor &v); virtual ~node() {}; @@ -73,6 +69,7 @@ struct definition_node : node { auto_node name; + auto_node type; auto_node body; DECLARE_NODE_VISIT_IMPL; @@ -85,34 +82,10 @@ DECLARE_NODE_VISIT_IMPL; }; -/* -struct sequence_node; -typedef std::auto_ptr<sequence_node> auto_sequence_node; - - -struct sequence_node : node { - auto_node head; - auto_sequence_node tail; - - bool empty() const; - unsigned size() const; - - virtual void visit_impl(node_visitor &); -}; - -auto_sequence_node cons( auto_node h ); -auto_sequence_node cons( node *h ); - -auto_sequence_node cons( auto_node h, - auto_sequence_node t ); -auto_sequence_node cons( node *h, - auto_sequence_node t ); -*/ - struct container_node : node { typedef std::vector<node*>::const_iterator iterator; -// iterator begin() const { return nodes.begin() } -// iterator end() const { return nodes.end() } + iterator begin() const { return nodes.begin(); } + iterator end() const { return nodes.end(); } bool empty() const { return nodes.empty(); } unsigned size() const { return nodes.size(); } @@ -137,9 +110,9 @@ std::vector<node*> nodes; }; -//typedef sequence_node container_node; -typedef container_node sequence_node; - +struct sequence_node : container_node { + DECLARE_NODE_VISIT_IMPL; +}; struct list_node : container_node { DECLARE_NODE_VISIT_IMPL; }; @@ -154,6 +127,7 @@ virtual void visit(operator_node const &)=0; virtual void visit(definition_node const &)=0; virtual void visit(function_node const &)=0; + virtual void visit(container_node const &)=0; virtual void visit(sequence_node const &)=0; virtual void visit(list_node const &)=0; virtual void visit(tuple_node const &)=0; @@ -161,5 +135,31 @@ virtual ~node_visitor() {}; }; +#define DEFINE_STATEFUL_NODE_VISITOR(name,state) \ +struct name : node_visitor { \ + virtual void visit(node const &); \ + virtual void visit(token_node const &); \ + virtual void visit(conditional_node const &); \ + virtual void visit(operator_node const &); \ + virtual void visit(definition_node const &); \ + virtual void visit(function_node const &); \ + virtual void visit(container_node const &); \ + virtual void visit(sequence_node const &); \ + virtual void visit(list_node const &); \ + virtual void visit(tuple_node const &); \ + state \ +} + +#define DEFINE_NODE_VISITOR(name) \ +DEFINE_STATEFUL_NODE_VISITOR(name,) + +DEFINE_STATEFUL_NODE_VISITOR( node_display_visitor, + unsigned indent; + node_display_visitor() : indent() {} +); + +#define DEFINE_NODE_VISIT(visitor_type,node_type,n) \ +void visitor_type::visit(node_type const &n) + #endif Modified: trunk/interpreter/preparser.cpp =================================================================== --- trunk/interpreter/preparser.cpp 2006-09-23 01:07:11 UTC (rev 74) +++ trunk/interpreter/preparser.cpp 2006-09-23 04:26:20 UTC (rev 75) @@ -24,7 +24,6 @@ for (unsigned i=0; i<level;++i) std::cerr << ' '; \ std::cerr << "Join " << __FUNCTION__ << " L" << level << " {\n"; \ ++level; \ - if ( level > 20 ) throw __PRETTY_FUNCTION__; \ } #define LEAVING \ { \ @@ -44,6 +43,7 @@ auto_node preparse_tuple(tsi &iter, tsi const &end); auto_node preparse_expression(tsi &iter, tsi const &end); +auto_node preparse_type(tsi &iter, tsi const &end); auto_node preparse_list(tsi &iter, tsi const &end); auto_node preparse_statement(tsi &iter, tsi const &end); auto_node preparse_sequence(tsi &iter, tsi const &end); @@ -52,7 +52,7 @@ auto_node preparse_sequence(tsi &iter, tsi const &end) { ENTERING; - std::auto_ptr<container_node> root( new container_node ); + std::auto_ptr<sequence_node> root( new sequence_node ); while ( iter != end ) { root->push_back( preparse_expression(iter,end) ); } @@ -62,7 +62,7 @@ auto_node preparse_statement(tsi &iter, tsi const &end) { ENTERING; - std::auto_ptr<container_node> root( new container_node ); + std::auto_ptr<sequence_node> root( new sequence_node ); while ( iter != end ) { token const &t = *iter; if ( t.is( token::SYMBOL, OP_END ) ) { @@ -76,7 +76,6 @@ break; } else { root->push_back( preparse_expression(iter,end) ); - if ( root->back()->is_a<conditional_node>() ) break; } } LEAVING; @@ -121,12 +120,18 @@ ENTERING; std::auto_ptr<conditional_node> root( new conditional_node ); root->cond = preparse_expression(iter,end); + assert( iter->is(token::SYMBOL, OP_COND) ); +SHOW_ITER; + ++iter; root->true_branch = preparse_statement(iter,end); assert( iter->is(token::TEXT, "else") ); SHOW_ITER; ++iter; if ( iter->is(token::SYMBOL, OP_COND ) ) { SHOW_ITER; ++iter; } root->false_branch = preparse_statement(iter,end); + + /// back up to let caller catch the OP_END too + --iter; LEAVING; return auto_node( root.release() ); } @@ -139,11 +144,11 @@ if ( t.is( token::SYMBOL, OP_END ) || t.is( token::SYMBOL, LIST_END ) - || t.is( token::SYMBOL, TUPLE_END ) ) { + || t.is( token::SYMBOL, TUPLE_END ) + || t.is( token::SYMBOL, OP_COND ) ) { --iter; break; - } else if ( t.is( token::SYMBOL, OP_SEP ) - || t.is( token::SYMBOL, OP_COND ) ) { + } else if ( t.is( token::SYMBOL, OP_SEP ) ) { break; } else if ( t.is( token::TEXT, "else" ) ) { --iter; @@ -151,6 +156,13 @@ } else if ( t.is( token::TEXT, "if" ) ) { root->push_back( preparse_conditional(iter,end) ); break; + } else if ( t.is( token::SYMBOL, OP_TYPE ) ) { + std::auto_ptr< definition_node > n( new definition_node ); + n->name = root; + n->attr |= node::TYPE; + n->body = preparse_expression(iter,end); + LEAVING; + return auto_node( n.release() ); } else if ( t.is( token::SYMBOL, OP_ASSIGN ) ) { std::auto_ptr< definition_node > n( new definition_node ); n->name = root; Modified: trunk/interpreter/tokenizer.cpp =================================================================== --- trunk/interpreter/tokenizer.cpp 2006-09-23 01:07:11 UTC (rev 74) +++ trunk/interpreter/tokenizer.cpp 2006-09-23 04:26:20 UTC (rev 75) @@ -10,8 +10,8 @@ #include "tokenizer.hpp" -// pick one -#define ADD_IMPLICIT_SEP_BEFORE_END +// pick one (or neither) +//#define ADD_IMPLICIT_SEP_BEFORE_END //#define REMOVE_SEP_BEFORE_END #include <istream> @@ -246,6 +246,7 @@ //std::cerr << (skip?'n':'y'); if ( !skip) tokens.push_back( current ); +/* /// If this is an OP_END, add an implicit following OP_SEP if ( current.type == token::SYMBOL && current.data == OP_END ) { @@ -258,8 +259,8 @@ tokens.push_back( implicit_sep ); } +*/ - current.type = token::NOTHING; --col; source.unget(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <me...@us...> - 2006-09-23 01:07:16
|
Revision: 74 http://svn.sourceforge.net/fuphyl/?rev=74&view=rev Author: me22 Date: 2006-09-22 18:07:11 -0700 (Fri, 22 Sep 2006) Log Message: ----------- Finishing preparser split. Modified Paths: -------------- trunk/interpreter/preparser.cpp Added Paths: ----------- trunk/interpreter/parser.cpp Copied: trunk/interpreter/parser.cpp (from rev 73, trunk/interpreter/preparser.cpp) =================================================================== --- trunk/interpreter/parser.cpp (rev 0) +++ trunk/interpreter/parser.cpp 2006-09-23 01:07:11 UTC (rev 74) @@ -0,0 +1,11 @@ +/* + * parser.cpp + * + * Copyright (c) 2006 Scott McMurray + * + * Licensed under the Open Software License version 3.0 + * ( See http://opensource.org/licenses/osl-3.0.php ) + * + */ + +#include "parser.hpp" Modified: trunk/interpreter/preparser.cpp =================================================================== --- trunk/interpreter/preparser.cpp 2006-09-23 01:05:31 UTC (rev 73) +++ trunk/interpreter/preparser.cpp 2006-09-23 01:07:11 UTC (rev 74) @@ -1,5 +1,5 @@ /* - * parser.cpp + * preparser.cpp * * Copyright (c) 2006 Scott McMurray * This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <me...@us...> - 2006-09-23 01:05:36
|
Revision: 73 http://svn.sourceforge.net/fuphyl/?rev=73&view=rev Author: me22 Date: 2006-09-22 18:05:31 -0700 (Fri, 22 Sep 2006) Log Message: ----------- splitting parser and preparser impl Added Paths: ----------- trunk/interpreter/preparser.cpp Removed Paths: ------------- trunk/interpreter/parser.cpp Deleted: trunk/interpreter/parser.cpp =================================================================== --- trunk/interpreter/parser.cpp 2006-09-23 01:01:34 UTC (rev 72) +++ trunk/interpreter/parser.cpp 2006-09-23 01:05:31 UTC (rev 73) @@ -1,182 +0,0 @@ -/* - * parser.cpp - * - * Copyright (c) 2006 Scott McMurray - * - * Licensed under the Open Software License version 3.0 - * ( See http://opensource.org/licenses/osl-3.0.php ) - * - */ - -#include "parser.hpp" - -#include "symbols.hpp" - -#include <cassert> -#ifndef NDEBUG -#include <iostream> -namespace { -unsigned level = 0; -} // namespace -#define ENTERING \ -{ \ - std::cerr << "\n"; \ - for (unsigned i=0; i<level;++i) std::cerr << ' '; \ - std::cerr << "Join " << __FUNCTION__ << " L" << level << " {\n"; \ - ++level; \ - if ( level > 20 ) throw __PRETTY_FUNCTION__; \ -} -#define LEAVING \ -{ \ - std::cerr << "\n"; \ - --level; \ - for (unsigned i=0; i<level;++i) std::cerr << ' '; \ - std::cerr << "} // " << __FUNCTION__ << " L" << level << "\n"; \ -} -#define SHOW_ITER std::cerr << iter->type_text() << "{" << iter->data << "}" -#else -#define ENTERING -#define LEAVING -#define SHOW_ITER -#endif - -typedef token_sequence_cit tsi; - -auto_node preparse_tuple(tsi &iter, tsi const &end); -auto_node preparse_expression(tsi &iter, tsi const &end); -auto_node preparse_list(tsi &iter, tsi const &end); -auto_node preparse_statement(tsi &iter, tsi const &end); -auto_node preparse_sequence(tsi &iter, tsi const &end); -auto_node preparse_conditional(tsi &iter, tsi const &end); -//auto_node preparse_(tsi &iter, tsi const &end); - -auto_node preparse_sequence(tsi &iter, tsi const &end) { -ENTERING; - std::auto_ptr<container_node> root( new container_node ); - while ( iter != end ) { - root->push_back( preparse_expression(iter,end) ); - } -LEAVING; - return auto_node( root.release() ); -} - -auto_node preparse_statement(tsi &iter, tsi const &end) { -ENTERING; - std::auto_ptr<container_node> root( new container_node ); - while ( iter != end ) { - token const &t = *iter; - if ( t.is( token::SYMBOL, OP_END ) ) { -SHOW_ITER; - ++iter; - break; - } else if ( t.is( token::SYMBOL, TUPLE_END ) - || t.is( token::SYMBOL, LIST_END ) ) { - break; - } else if ( t.is( token::TEXT, "else" ) ) { - break; - } else { - root->push_back( preparse_expression(iter,end) ); - if ( root->back()->is_a<conditional_node>() ) break; - } - } -LEAVING; - return auto_node( root.release() ); -} - -auto_node preparse_list(tsi &iter, tsi const &end) { -ENTERING; - std::auto_ptr<list_node> root( new list_node ); - while ( iter != end ) { - token const &t = *iter; - if ( t.is( token::SYMBOL, LIST_END ) ) { -SHOW_ITER; - ++iter; - break; - } else { - root->push_back( preparse_expression(iter,end) ); - } - } -LEAVING; - return auto_node( root.release() ); -} - -auto_node preparse_tuple(tsi &iter, tsi const &end) { -ENTERING; - std::auto_ptr<tuple_node> root( new tuple_node ); - while ( iter != end ) { - token const &t = *iter; - if ( t.is( token::SYMBOL, TUPLE_END ) ) { -SHOW_ITER; - ++iter; - break; - } else { - root->push_back( preparse_expression(iter,end) ); - } - } -LEAVING; - return auto_node( root.release() ); -} - -auto_node preparse_conditional(tsi &iter, tsi const &end) { -ENTERING; - std::auto_ptr<conditional_node> root( new conditional_node ); - root->cond = preparse_expression(iter,end); - root->true_branch = preparse_statement(iter,end); - assert( iter->is(token::TEXT, "else") ); -SHOW_ITER; - ++iter; - if ( iter->is(token::SYMBOL, OP_COND ) ) { SHOW_ITER; ++iter; } - root->false_branch = preparse_statement(iter,end); -LEAVING; - return auto_node( root.release() ); -} - -auto_node preparse_expression(tsi &iter, tsi const &end) { -ENTERING; - std::auto_ptr<container_node> root( new container_node ); - while ( iter != end ) { - token const &t = *iter; SHOW_ITER; ++iter; - - if ( t.is( token::SYMBOL, OP_END ) - || t.is( token::SYMBOL, LIST_END ) - || t.is( token::SYMBOL, TUPLE_END ) ) { - --iter; - break; - } else if ( t.is( token::SYMBOL, OP_SEP ) - || t.is( token::SYMBOL, OP_COND ) ) { - break; - } else if ( t.is( token::TEXT, "else" ) ) { - --iter; - break; - } else if ( t.is( token::TEXT, "if" ) ) { - root->push_back( preparse_conditional(iter,end) ); - break; - } else if ( t.is( token::SYMBOL, OP_ASSIGN ) ) { - std::auto_ptr< definition_node > n( new definition_node ); - n->name = root; - n->body = preparse_statement(iter,end); - LEAVING; - return auto_node( n.release() ); - } else if ( t.is( token::SYMBOL, TUPLE_BEGIN ) ) { - root->push_back( preparse_tuple(iter,end) ); - } else if ( t.is( token::SYMBOL, LIST_BEGIN ) ) { - root->push_back( preparse_list(iter,end) ); - } else { - root->push_back( auto_node( new token_node(t) ) ); - } - } -LEAVING; - return auto_node( root.release() ); -} - - -// throw __FUNCTION__; - -auto_node preparse(token_sequence tokens) { -ENTERING; - tsi b = tokens.begin(); - tsi const e = tokens.end(); - auto_node root = preparse_sequence( b, e ); -LEAVING; - return root; -} Copied: trunk/interpreter/preparser.cpp (from rev 72, trunk/interpreter/parser.cpp) =================================================================== --- trunk/interpreter/preparser.cpp (rev 0) +++ trunk/interpreter/preparser.cpp 2006-09-23 01:05:31 UTC (rev 73) @@ -0,0 +1,182 @@ +/* + * parser.cpp + * + * Copyright (c) 2006 Scott McMurray + * + * Licensed under the Open Software License version 3.0 + * ( See http://opensource.org/licenses/osl-3.0.php ) + * + */ + +#include "parser.hpp" + +#include "symbols.hpp" + +#include <cassert> +#ifndef NDEBUG +#include <iostream> +namespace { +unsigned level = 0; +} // namespace +#define ENTERING \ +{ \ + std::cerr << "\n"; \ + for (unsigned i=0; i<level;++i) std::cerr << ' '; \ + std::cerr << "Join " << __FUNCTION__ << " L" << level << " {\n"; \ + ++level; \ + if ( level > 20 ) throw __PRETTY_FUNCTION__; \ +} +#define LEAVING \ +{ \ + std::cerr << "\n"; \ + --level; \ + for (unsigned i=0; i<level;++i) std::cerr << ' '; \ + std::cerr << "} // " << __FUNCTION__ << " L" << level << "\n"; \ +} +#define SHOW_ITER std::cerr << iter->type_text() << "{" << iter->data << "}" +#else +#define ENTERING +#define LEAVING +#define SHOW_ITER +#endif + +typedef token_sequence_cit tsi; + +auto_node preparse_tuple(tsi &iter, tsi const &end); +auto_node preparse_expression(tsi &iter, tsi const &end); +auto_node preparse_list(tsi &iter, tsi const &end); +auto_node preparse_statement(tsi &iter, tsi const &end); +auto_node preparse_sequence(tsi &iter, tsi const &end); +auto_node preparse_conditional(tsi &iter, tsi const &end); +//auto_node preparse_(tsi &iter, tsi const &end); + +auto_node preparse_sequence(tsi &iter, tsi const &end) { +ENTERING; + std::auto_ptr<container_node> root( new container_node ); + while ( iter != end ) { + root->push_back( preparse_expression(iter,end) ); + } +LEAVING; + return auto_node( root.release() ); +} + +auto_node preparse_statement(tsi &iter, tsi const &end) { +ENTERING; + std::auto_ptr<container_node> root( new container_node ); + while ( iter != end ) { + token const &t = *iter; + if ( t.is( token::SYMBOL, OP_END ) ) { +SHOW_ITER; + ++iter; + break; + } else if ( t.is( token::SYMBOL, TUPLE_END ) + || t.is( token::SYMBOL, LIST_END ) ) { + break; + } else if ( t.is( token::TEXT, "else" ) ) { + break; + } else { + root->push_back( preparse_expression(iter,end) ); + if ( root->back()->is_a<conditional_node>() ) break; + } + } +LEAVING; + return auto_node( root.release() ); +} + +auto_node preparse_list(tsi &iter, tsi const &end) { +ENTERING; + std::auto_ptr<list_node> root( new list_node ); + while ( iter != end ) { + token const &t = *iter; + if ( t.is( token::SYMBOL, LIST_END ) ) { +SHOW_ITER; + ++iter; + break; + } else { + root->push_back( preparse_expression(iter,end) ); + } + } +LEAVING; + return auto_node( root.release() ); +} + +auto_node preparse_tuple(tsi &iter, tsi const &end) { +ENTERING; + std::auto_ptr<tuple_node> root( new tuple_node ); + while ( iter != end ) { + token const &t = *iter; + if ( t.is( token::SYMBOL, TUPLE_END ) ) { +SHOW_ITER; + ++iter; + break; + } else { + root->push_back( preparse_expression(iter,end) ); + } + } +LEAVING; + return auto_node( root.release() ); +} + +auto_node preparse_conditional(tsi &iter, tsi const &end) { +ENTERING; + std::auto_ptr<conditional_node> root( new conditional_node ); + root->cond = preparse_expression(iter,end); + root->true_branch = preparse_statement(iter,end); + assert( iter->is(token::TEXT, "else") ); +SHOW_ITER; + ++iter; + if ( iter->is(token::SYMBOL, OP_COND ) ) { SHOW_ITER; ++iter; } + root->false_branch = preparse_statement(iter,end); +LEAVING; + return auto_node( root.release() ); +} + +auto_node preparse_expression(tsi &iter, tsi const &end) { +ENTERING; + std::auto_ptr<container_node> root( new container_node ); + while ( iter != end ) { + token const &t = *iter; SHOW_ITER; ++iter; + + if ( t.is( token::SYMBOL, OP_END ) + || t.is( token::SYMBOL, LIST_END ) + || t.is( token::SYMBOL, TUPLE_END ) ) { + --iter; + break; + } else if ( t.is( token::SYMBOL, OP_SEP ) + || t.is( token::SYMBOL, OP_COND ) ) { + break; + } else if ( t.is( token::TEXT, "else" ) ) { + --iter; + break; + } else if ( t.is( token::TEXT, "if" ) ) { + root->push_back( preparse_conditional(iter,end) ); + break; + } else if ( t.is( token::SYMBOL, OP_ASSIGN ) ) { + std::auto_ptr< definition_node > n( new definition_node ); + n->name = root; + n->body = preparse_statement(iter,end); + LEAVING; + return auto_node( n.release() ); + } else if ( t.is( token::SYMBOL, TUPLE_BEGIN ) ) { + root->push_back( preparse_tuple(iter,end) ); + } else if ( t.is( token::SYMBOL, LIST_BEGIN ) ) { + root->push_back( preparse_list(iter,end) ); + } else { + root->push_back( auto_node( new token_node(t) ) ); + } + } +LEAVING; + return auto_node( root.release() ); +} + + +// throw __FUNCTION__; + +auto_node preparse(token_sequence tokens) { +ENTERING; + tsi b = tokens.begin(); + tsi const e = tokens.end(); + auto_node root = preparse_sequence( b, e ); +LEAVING; + return root; +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <me...@us...> - 2006-09-23 01:01:37
|
Revision: 72 http://svn.sourceforge.net/fuphyl/?rev=72&view=rev Author: me22 Date: 2006-09-22 18:01:34 -0700 (Fri, 22 Sep 2006) Log Message: ----------- doh, TEXT != STRING Modified Paths: -------------- trunk/interpreter/parser.cpp Modified: trunk/interpreter/parser.cpp =================================================================== --- trunk/interpreter/parser.cpp 2006-09-22 22:52:12 UTC (rev 71) +++ trunk/interpreter/parser.cpp 2006-09-23 01:01:34 UTC (rev 72) @@ -20,19 +20,24 @@ } // namespace #define ENTERING \ { \ + std::cerr << "\n"; \ for (unsigned i=0; i<level;++i) std::cerr << ' '; \ std::cerr << "Join " << __FUNCTION__ << " L" << level << " {\n"; \ ++level; \ + if ( level > 20 ) throw __PRETTY_FUNCTION__; \ } #define LEAVING \ { \ + std::cerr << "\n"; \ --level; \ for (unsigned i=0; i<level;++i) std::cerr << ' '; \ std::cerr << "} // " << __FUNCTION__ << " L" << level << "\n"; \ } +#define SHOW_ITER std::cerr << iter->type_text() << "{" << iter->data << "}" #else #define ENTERING #define LEAVING +#define SHOW_ITER #endif typedef token_sequence_cit tsi; @@ -61,10 +66,14 @@ while ( iter != end ) { token const &t = *iter; if ( t.is( token::SYMBOL, OP_END ) ) { +SHOW_ITER; ++iter; break; - } else if ( t.is( token::STRING, "else" ) ) { + } else if ( t.is( token::SYMBOL, TUPLE_END ) + || t.is( token::SYMBOL, LIST_END ) ) { break; + } else if ( t.is( token::TEXT, "else" ) ) { + break; } else { root->push_back( preparse_expression(iter,end) ); if ( root->back()->is_a<conditional_node>() ) break; @@ -80,6 +89,7 @@ while ( iter != end ) { token const &t = *iter; if ( t.is( token::SYMBOL, LIST_END ) ) { +SHOW_ITER; ++iter; break; } else { @@ -96,6 +106,7 @@ while ( iter != end ) { token const &t = *iter; if ( t.is( token::SYMBOL, TUPLE_END ) ) { +SHOW_ITER; ++iter; break; } else { @@ -111,9 +122,10 @@ std::auto_ptr<conditional_node> root( new conditional_node ); root->cond = preparse_expression(iter,end); root->true_branch = preparse_statement(iter,end); - assert( iter->is(token::STRING, "else") ); + assert( iter->is(token::TEXT, "else") ); +SHOW_ITER; ++iter; - if ( iter->is(token::SYMBOL, OP_COND ) ) ++iter; + if ( iter->is(token::SYMBOL, OP_COND ) ) { SHOW_ITER; ++iter; } root->false_branch = preparse_statement(iter,end); LEAVING; return auto_node( root.release() ); @@ -123,7 +135,7 @@ ENTERING; std::auto_ptr<container_node> root( new container_node ); while ( iter != end ) { - token const &t = *iter; ++iter; + token const &t = *iter; SHOW_ITER; ++iter; if ( t.is( token::SYMBOL, OP_END ) || t.is( token::SYMBOL, LIST_END ) @@ -133,10 +145,10 @@ } else if ( t.is( token::SYMBOL, OP_SEP ) || t.is( token::SYMBOL, OP_COND ) ) { break; - } else if ( t.is( token::STRING, "else" ) ) { + } else if ( t.is( token::TEXT, "else" ) ) { --iter; break; - } else if ( t.is( token::STRING, "if" ) ) { + } else if ( t.is( token::TEXT, "if" ) ) { root->push_back( preparse_conditional(iter,end) ); break; } else if ( t.is( token::SYMBOL, OP_ASSIGN ) ) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <me...@us...> - 2006-09-22 22:52:15
|
Revision: 71 http://svn.sourceforge.net/fuphyl/?rev=71&view=rev Author: me22 Date: 2006-09-22 15:52:12 -0700 (Fri, 22 Sep 2006) Log Message: ----------- debugging callstack output Modified Paths: -------------- trunk/interpreter/parser.cpp Modified: trunk/interpreter/parser.cpp =================================================================== --- trunk/interpreter/parser.cpp 2006-09-22 18:48:46 UTC (rev 70) +++ trunk/interpreter/parser.cpp 2006-09-22 22:52:12 UTC (rev 71) @@ -13,6 +13,27 @@ #include "symbols.hpp" #include <cassert> +#ifndef NDEBUG +#include <iostream> +namespace { +unsigned level = 0; +} // namespace +#define ENTERING \ +{ \ + for (unsigned i=0; i<level;++i) std::cerr << ' '; \ + std::cerr << "Join " << __FUNCTION__ << " L" << level << " {\n"; \ + ++level; \ +} +#define LEAVING \ +{ \ + --level; \ + for (unsigned i=0; i<level;++i) std::cerr << ' '; \ + std::cerr << "} // " << __FUNCTION__ << " L" << level << "\n"; \ +} +#else +#define ENTERING +#define LEAVING +#endif typedef token_sequence_cit tsi; @@ -25,15 +46,18 @@ //auto_node preparse_(tsi &iter, tsi const &end); auto_node preparse_sequence(tsi &iter, tsi const &end) { +ENTERING; std::auto_ptr<container_node> root( new container_node ); while ( iter != end ) { root->push_back( preparse_expression(iter,end) ); } +LEAVING; return auto_node( root.release() ); } auto_node preparse_statement(tsi &iter, tsi const &end) { - std::auto_ptr<container_node> root( new list_node ); +ENTERING; + std::auto_ptr<container_node> root( new container_node ); while ( iter != end ) { token const &t = *iter; if ( t.is( token::SYMBOL, OP_END ) ) { @@ -46,10 +70,12 @@ if ( root->back()->is_a<conditional_node>() ) break; } } +LEAVING; return auto_node( root.release() ); } auto_node preparse_list(tsi &iter, tsi const &end) { +ENTERING; std::auto_ptr<list_node> root( new list_node ); while ( iter != end ) { token const &t = *iter; @@ -60,10 +86,12 @@ root->push_back( preparse_expression(iter,end) ); } } +LEAVING; return auto_node( root.release() ); } auto_node preparse_tuple(tsi &iter, tsi const &end) { +ENTERING; std::auto_ptr<tuple_node> root( new tuple_node ); while ( iter != end ) { token const &t = *iter; @@ -74,32 +102,36 @@ root->push_back( preparse_expression(iter,end) ); } } +LEAVING; return auto_node( root.release() ); } auto_node preparse_conditional(tsi &iter, tsi const &end) { +ENTERING; std::auto_ptr<conditional_node> root( new conditional_node ); root->cond = preparse_expression(iter,end); root->true_branch = preparse_statement(iter,end); assert( iter->is(token::STRING, "else") ); ++iter; + if ( iter->is(token::SYMBOL, OP_COND ) ) ++iter; root->false_branch = preparse_statement(iter,end); +LEAVING; return auto_node( root.release() ); } auto_node preparse_expression(tsi &iter, tsi const &end) { +ENTERING; std::auto_ptr<container_node> root( new container_node ); while ( iter != end ) { token const &t = *iter; ++iter; - // tokeniser should prevent these from firing - assert( !t.is( token::SYMBOL, OP_END ) ); - assert( !t.is( token::SYMBOL, LIST_END ) ); - assert( !t.is( token::SYMBOL, TUPLE_END ) ); - - if ( t.is( token::SYMBOL, OP_SEP ) ) { + if ( t.is( token::SYMBOL, OP_END ) + || t.is( token::SYMBOL, LIST_END ) + || t.is( token::SYMBOL, TUPLE_END ) ) { + --iter; break; - } else if ( t.is( token::SYMBOL, OP_COND ) ) { + } else if ( t.is( token::SYMBOL, OP_SEP ) + || t.is( token::SYMBOL, OP_COND ) ) { break; } else if ( t.is( token::STRING, "else" ) ) { --iter; @@ -111,6 +143,7 @@ std::auto_ptr< definition_node > n( new definition_node ); n->name = root; n->body = preparse_statement(iter,end); + LEAVING; return auto_node( n.release() ); } else if ( t.is( token::SYMBOL, TUPLE_BEGIN ) ) { root->push_back( preparse_tuple(iter,end) ); @@ -120,14 +153,18 @@ root->push_back( auto_node( new token_node(t) ) ); } } +LEAVING; return auto_node( root.release() ); } -// throw __PRETTY_FUNCTION__; +// throw __FUNCTION__; auto_node preparse(token_sequence tokens) { +ENTERING; tsi b = tokens.begin(); tsi const e = tokens.end(); - return preparse_sequence( b, e ); + auto_node root = preparse_sequence( b, e ); +LEAVING; + return root; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <me...@us...> - 2006-09-22 18:48:51
|
Revision: 70 http://svn.sourceforge.net/fuphyl/?rev=70&view=rev Author: me22 Date: 2006-09-22 11:48:46 -0700 (Fri, 22 Sep 2006) Log Message: ----------- post-working at school commit (minor changes) Modified Paths: -------------- trunk/interpreter/symbols.txt trunk/interpreter/tokenizer.cpp Modified: trunk/interpreter/symbols.txt =================================================================== --- trunk/interpreter/symbols.txt 2006-09-22 18:47:04 UTC (rev 69) +++ trunk/interpreter/symbols.txt 2006-09-22 18:48:46 UTC (rev 70) @@ -38,6 +38,8 @@ //use OP_SEP instead// LIST_SEP , LIST_JOIN ++ //LIST_SPLIT * +LIST_PREP >> +LIST_AP << TUPLE_BEGIN ( TUPLE_END ) Modified: trunk/interpreter/tokenizer.cpp =================================================================== --- trunk/interpreter/tokenizer.cpp 2006-09-22 18:47:04 UTC (rev 69) +++ trunk/interpreter/tokenizer.cpp 2006-09-22 18:48:46 UTC (rev 70) @@ -82,10 +82,13 @@ skip |= current.data.empty(); } -if ( !skip ) { -std::cerr << current.type_text(); -std::cerr << '{' << current.data << '}'; -} +#if 0 + if ( !skip ) { + std::cerr << current.type_text(); + std::cerr << '{' << current.data << '}'; + } +#endif + /// Use maximum munch to split runtogethered symbols if ( !skip && current.is( token::SYMBOL ) && !valid_symbol(current.data) ) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <me...@us...> - 2006-09-22 18:47:08
|
Revision: 69 http://svn.sourceforge.net/fuphyl/?rev=69&view=rev Author: me22 Date: 2006-09-22 11:47:04 -0700 (Fri, 22 Sep 2006) Log Message: ----------- fixed a disasterous typo that broke number parsing Modified Paths: -------------- trunk/interpreter/tokenizer.cpp Modified: trunk/interpreter/tokenizer.cpp =================================================================== --- trunk/interpreter/tokenizer.cpp 2006-09-22 18:34:54 UTC (rev 68) +++ trunk/interpreter/tokenizer.cpp 2006-09-22 18:47:04 UTC (rev 69) @@ -63,7 +63,6 @@ void add_token(token_sequence &tokens, token ¤t, std::istream &source, unsigned &col) { -//std::cerr << '{' << current.data << '}'; bool skip = false; switch ( current.type ) { case token::NOTHING: @@ -83,6 +82,10 @@ skip |= current.data.empty(); } +if ( !skip ) { +std::cerr << current.type_text(); +std::cerr << '{' << current.data << '}'; +} /// Use maximum munch to split runtogethered symbols if ( !skip && current.is( token::SYMBOL ) && !valid_symbol(current.data) ) { @@ -431,7 +434,7 @@ case token::NUMBER: { if ( std::isalnum(c) ) { current.data.push_back(c); - } else if ( c != '_' ) { + } else if ( c == '_' ) { // silently drop underscores, perl-style // allows literals like 1_234_567 or 1.234_567e6 } else { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <me...@us...> - 2006-09-22 18:34:57
|
Revision: 68 http://svn.sourceforge.net/fuphyl/?rev=68&view=rev Author: me22 Date: 2006-09-22 11:34:54 -0700 (Fri, 22 Sep 2006) Log Message: ----------- Include Comment with symbol name in generated source Modified Paths: -------------- trunk/interpreter/symbols_gen.cpp Modified: trunk/interpreter/symbols_gen.cpp =================================================================== --- trunk/interpreter/symbols_gen.cpp 2006-09-22 18:25:30 UTC (rev 67) +++ trunk/interpreter/symbols_gen.cpp 2006-09-22 18:34:54 UTC (rev 68) @@ -161,7 +161,7 @@ for ( unsigned i = 0; i < symbol_data.size(); ++i ) { symbols_out << " v.push_back(\"" << symbol_data[i].second - << "\");\n"; + << "\"); // " << symbol_data[i].first << "\n"; } symbols_out << " return true;\n"; symbols_out << "}\n"; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <me...@us...> - 2006-09-22 18:25:34
|
Revision: 67 http://svn.sourceforge.net/fuphyl/?rev=67&view=rev Author: me22 Date: 2006-09-22 11:25:30 -0700 (Fri, 22 Sep 2006) Log Message: ----------- AST nodes Added Paths: ----------- trunk/interpreter/nodes.cpp trunk/interpreter/nodes.hpp Added: trunk/interpreter/nodes.cpp =================================================================== --- trunk/interpreter/nodes.cpp (rev 0) +++ trunk/interpreter/nodes.cpp 2006-09-22 18:25:30 UTC (rev 67) @@ -0,0 +1,84 @@ +/* + * nodes.cpp + * + * Copyright (c) 2006 Scott McMurray + * + * Licensed under the Open Software License version 3.0 + * ( See http://opensource.org/licenses/osl-3.0.php ) + * + */ + +#include <cassert> + +#ifndef NDEBUG +#include <iostream> +#endif + +#include "nodes.hpp" + +void node::visit(node_visitor &v) { + assert( std::cout << "Visiting a " << typeid(v).name() + << " at " << &v + << " with a " << typeid(v).name() + << ".\n" ); + return visit_impl(v); +} + +#define DEFINE_VISIT_IMPL(T) \ +void T::visit_impl(node_visitor &v) { v.visit(*this); } +DEFINE_VISIT_IMPL( token_node ) +DEFINE_VISIT_IMPL( conditional_node ) +DEFINE_VISIT_IMPL( operator_node ) +DEFINE_VISIT_IMPL( definition_node ) +DEFINE_VISIT_IMPL( function_node ) +DEFINE_VISIT_IMPL( sequence_node ) +DEFINE_VISIT_IMPL( list_node ) +DEFINE_VISIT_IMPL( tuple_node ) +// DEFINE_VISIT_IMPL( ) + +/* +std::auto_ptr<sequence_node> cons( std::auto_ptr<node> h ) { + std::auto_ptr<sequence_node> c( new sequence_node ); + c->head = h; + return c; +} +std::auto_ptr<sequence_node> cons( node *h ) { + return cons( std::auto_ptr<node>(h) ); +} + +std::auto_ptr<sequence_node> cons( std::auto_ptr<node> h, + std::auto_ptr<sequence_node> t ) { + if ( !t.get() ) { + return cons(h); + } else if ( !t->head.get() ) { + t->head = h; + return t; + } else { + std::auto_ptr<sequence_node> c( new sequence_node ); + c->head = h; + c->tail = t; + return c; + } +} +std::auto_ptr<sequence_node> cons( node* h, + std::auto_ptr<sequence_node> t ) { + return cons( std::auto_ptr<node>(h), t ); +} + +bool sequence_node::empty() const { + assert( head.get() || !tail.get() ); + return !head.get(); +} +unsigned sequence_node::size() const { + assert( head.get() || !tail.get() ); + if ( !tail.get() ) { + if ( head.get() ) { + return 1u; + } else { + return 0u; + } + } else { + return tail->size() + 1; + } +} +*/ Added: trunk/interpreter/nodes.hpp =================================================================== --- trunk/interpreter/nodes.hpp (rev 0) +++ trunk/interpreter/nodes.hpp 2006-09-22 18:25:30 UTC (rev 67) @@ -0,0 +1,165 @@ +#ifndef NODES_HPP +#define NODES_HPP + +/* + * nodes.hpp + * + * Copyright (c) 2006 Scott McMurray + * + * Licensed under the Open Software License version 3.0 + * ( See http://opensource.org/licenses/osl-3.0.php ) + * + */ + +#include <memory> + +#include "tokenizer.hpp" + +struct node_visitor; +#define DECLARE_NODE_VISIT_IMPL \ + virtual void visit_impl(node_visitor &) + +struct node; +typedef std::auto_ptr<node> auto_node; + +struct node { + enum attribute_mask { CONST=1<<0, + LITERAL=1<<1, + _ }; + attribute_mask attributes; + + template <typename T> + bool is_a() { return dynamic_cast<T*>(this); } + template <typename T> + T &as_a() { return dynamic_cast<T&>(this); } + +/* + template <typename T> + static auto_node create() { + return auto_node( new T ); + } +*/ + + void visit(node_visitor &v); + + virtual ~node() {}; + protected: + DECLARE_NODE_VISIT_IMPL=0; +}; + +struct token_node : node { + token data; + + token_node() {} + token_node(token const &t) : data(t) {} + + DECLARE_NODE_VISIT_IMPL; +}; + +struct conditional_node : node { + auto_node cond; + auto_node true_branch; + auto_node false_branch; + + DECLARE_NODE_VISIT_IMPL; +}; + +struct operator_node : node { + token oper; + auto_node arg; + + DECLARE_NODE_VISIT_IMPL; +}; + +struct definition_node : node { + auto_node name; + auto_node body; + + DECLARE_NODE_VISIT_IMPL; +}; + +struct function_node : node { + std::auto_ptr<node> fn; + std::auto_ptr<node> arg; + + DECLARE_NODE_VISIT_IMPL; +}; + +/* +struct sequence_node; +typedef std::auto_ptr<sequence_node> auto_sequence_node; + + +struct sequence_node : node { + auto_node head; + auto_sequence_node tail; + + bool empty() const; + unsigned size() const; + + virtual void visit_impl(node_visitor &); +}; + +auto_sequence_node cons( auto_node h ); +auto_sequence_node cons( node *h ); + +auto_sequence_node cons( auto_node h, + auto_sequence_node t ); +auto_sequence_node cons( node *h, + auto_sequence_node t ); +*/ + +struct container_node : node { + typedef std::vector<node*>::const_iterator iterator; +// iterator begin() const { return nodes.begin() } +// iterator end() const { return nodes.end() } + + bool empty() const { return nodes.empty(); } + unsigned size() const { return nodes.size(); } + + void push_back( auto_node n ) { + nodes.push_back( n.get() ); + n.release(); + } + auto_node pop_back() { + auto_node val( nodes.back() ); + nodes.pop_back(); + return val; + } + node *back() { return nodes.back(); } + + DECLARE_NODE_VISIT_IMPL; + + ~container_node() { + while ( !empty() ) pop_back(); + } + protected: + std::vector<node*> nodes; +}; + +//typedef sequence_node container_node; +typedef container_node sequence_node; + +struct list_node : container_node { + DECLARE_NODE_VISIT_IMPL; +}; +struct tuple_node : container_node { + DECLARE_NODE_VISIT_IMPL; +}; + +struct node_visitor { + virtual void visit(node const &)=0; + virtual void visit(token_node const &)=0; + virtual void visit(conditional_node const &)=0; + virtual void visit(operator_node const &)=0; + virtual void visit(definition_node const &)=0; + virtual void visit(function_node const &)=0; + virtual void visit(sequence_node const &)=0; + virtual void visit(list_node const &)=0; + virtual void visit(tuple_node const &)=0; + protected: + virtual ~node_visitor() {}; +}; + +#endif + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <me...@us...> - 2006-09-22 18:22:08
|
Revision: 66 http://svn.sourceforge.net/fuphyl/?rev=66&view=rev Author: me22 Date: 2006-09-22 11:22:04 -0700 (Fri, 22 Sep 2006) Log Message: ----------- recursive-decent pre-parser Modified Paths: -------------- trunk/interpreter/interpreter.cbp trunk/interpreter/parser.cpp trunk/interpreter/parser.hpp trunk/interpreter/tokenizer.cpp Modified: trunk/interpreter/interpreter.cbp =================================================================== --- trunk/interpreter/interpreter.cbp 2006-09-20 22:05:28 UTC (rev 65) +++ trunk/interpreter/interpreter.cbp 2006-09-22 18:22:04 UTC (rev 66) @@ -48,6 +48,16 @@ <Option compilerVar="CPP" /> <Option target="interpreter" /> </Unit> + <Unit filename="nodes.cpp"> + <Option compilerVar="CPP" /> + <Option target="interpreter" /> + </Unit> + <Unit filename="nodes.hpp"> + <Option compilerVar="CPP" /> + <Option compile="0" /> + <Option link="0" /> + <Option target="interpreter" /> + </Unit> <Unit filename="parser.cpp"> <Option compilerVar="CPP" /> <Option target="interpreter" /> Modified: trunk/interpreter/parser.cpp =================================================================== --- trunk/interpreter/parser.cpp 2006-09-20 22:05:28 UTC (rev 65) +++ trunk/interpreter/parser.cpp 2006-09-22 18:22:04 UTC (rev 66) @@ -9,3 +9,125 @@ */ #include "parser.hpp" + +#include "symbols.hpp" + +#include <cassert> + +typedef token_sequence_cit tsi; + +auto_node preparse_tuple(tsi &iter, tsi const &end); +auto_node preparse_expression(tsi &iter, tsi const &end); +auto_node preparse_list(tsi &iter, tsi const &end); +auto_node preparse_statement(tsi &iter, tsi const &end); +auto_node preparse_sequence(tsi &iter, tsi const &end); +auto_node preparse_conditional(tsi &iter, tsi const &end); +//auto_node preparse_(tsi &iter, tsi const &end); + +auto_node preparse_sequence(tsi &iter, tsi const &end) { + std::auto_ptr<container_node> root( new container_node ); + while ( iter != end ) { + root->push_back( preparse_expression(iter,end) ); + } + return auto_node( root.release() ); +} + +auto_node preparse_statement(tsi &iter, tsi const &end) { + std::auto_ptr<container_node> root( new list_node ); + while ( iter != end ) { + token const &t = *iter; + if ( t.is( token::SYMBOL, OP_END ) ) { + ++iter; + break; + } else if ( t.is( token::STRING, "else" ) ) { + break; + } else { + root->push_back( preparse_expression(iter,end) ); + if ( root->back()->is_a<conditional_node>() ) break; + } + } + return auto_node( root.release() ); +} + +auto_node preparse_list(tsi &iter, tsi const &end) { + std::auto_ptr<list_node> root( new list_node ); + while ( iter != end ) { + token const &t = *iter; + if ( t.is( token::SYMBOL, LIST_END ) ) { + ++iter; + break; + } else { + root->push_back( preparse_expression(iter,end) ); + } + } + return auto_node( root.release() ); +} + +auto_node preparse_tuple(tsi &iter, tsi const &end) { + std::auto_ptr<tuple_node> root( new tuple_node ); + while ( iter != end ) { + token const &t = *iter; + if ( t.is( token::SYMBOL, TUPLE_END ) ) { + ++iter; + break; + } else { + root->push_back( preparse_expression(iter,end) ); + } + } + return auto_node( root.release() ); +} + +auto_node preparse_conditional(tsi &iter, tsi const &end) { + std::auto_ptr<conditional_node> root( new conditional_node ); + root->cond = preparse_expression(iter,end); + root->true_branch = preparse_statement(iter,end); + assert( iter->is(token::STRING, "else") ); + ++iter; + root->false_branch = preparse_statement(iter,end); + return auto_node( root.release() ); +} + +auto_node preparse_expression(tsi &iter, tsi const &end) { + std::auto_ptr<container_node> root( new container_node ); + while ( iter != end ) { + token const &t = *iter; ++iter; + + // tokeniser should prevent these from firing + assert( !t.is( token::SYMBOL, OP_END ) ); + assert( !t.is( token::SYMBOL, LIST_END ) ); + assert( !t.is( token::SYMBOL, TUPLE_END ) ); + + if ( t.is( token::SYMBOL, OP_SEP ) ) { + break; + } else if ( t.is( token::SYMBOL, OP_COND ) ) { + break; + } else if ( t.is( token::STRING, "else" ) ) { + --iter; + break; + } else if ( t.is( token::STRING, "if" ) ) { + root->push_back( preparse_conditional(iter,end) ); + break; + } else if ( t.is( token::SYMBOL, OP_ASSIGN ) ) { + std::auto_ptr< definition_node > n( new definition_node ); + n->name = root; + n->body = preparse_statement(iter,end); + return auto_node( n.release() ); + } else if ( t.is( token::SYMBOL, TUPLE_BEGIN ) ) { + root->push_back( preparse_tuple(iter,end) ); + } else if ( t.is( token::SYMBOL, LIST_BEGIN ) ) { + root->push_back( preparse_list(iter,end) ); + } else { + root->push_back( auto_node( new token_node(t) ) ); + } + } + return auto_node( root.release() ); +} + + +// throw __PRETTY_FUNCTION__; + +auto_node preparse(token_sequence tokens) { + tsi b = tokens.begin(); + tsi const e = tokens.end(); + return preparse_sequence( b, e ); +} Modified: trunk/interpreter/parser.hpp =================================================================== --- trunk/interpreter/parser.hpp 2006-09-20 22:05:28 UTC (rev 65) +++ trunk/interpreter/parser.hpp 2006-09-22 18:22:04 UTC (rev 66) @@ -14,57 +14,8 @@ #include <utility> #include "tokenizer.hpp" +#include "nodes.hpp" -namespace syntax { +auto_node preparse(token_sequence tokens); -struct node_visitor; - -struct node { - enum attribute_mask { CONST=1<<0, - LITERAL=1<<1, - _ }; - attribute_mask attributes; - - void visit(node_visitor &v); - protected: - virtual void visit_impl(node_visitor &v) {} - virtual ~node() {}; -}; - -struct token_node : node { - token data; - - virtual void visit_impl(node_visitor &v) {} -}; - -struct operator_node : node { - token oper; - auto_ptr<node> arg; - - virtual void visit_impl(node_visitor &v) {} -}; - -struct function_node : node { - auto_ptr<node> fn; - auto_ptr<node> arg; - - virtual void visit_impl(node_visitor &v) {} -}; - -struct sequence_node : node { - std::auto_ptr<node> head; - std::auto_ptr<node> tail; - - virtual void visit_impl(node_visitor &v) {} -}; - -struct list_node : sequence_node { - virtual void visit_impl(node_visitor &v) {} -}; -struct tuple_node : sequence_node { - virtual void visit_impl(node_visitor &v) {} -}; - -} // namespace syntax - #endif Modified: trunk/interpreter/tokenizer.cpp =================================================================== --- trunk/interpreter/tokenizer.cpp 2006-09-20 22:05:28 UTC (rev 65) +++ trunk/interpreter/tokenizer.cpp 2006-09-22 18:22:04 UTC (rev 66) @@ -11,8 +11,8 @@ #include "tokenizer.hpp" // pick one -//#define ADD_IMPLICIT_SEP_BEFORE_END -#define REMOVE_SEP_BEFORE_END +#define ADD_IMPLICIT_SEP_BEFORE_END +//#define REMOVE_SEP_BEFORE_END #include <istream> #include <cctype> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <me...@us...> - 2006-09-20 22:05:31
|
Revision: 65 http://svn.sourceforge.net/fuphyl/?rev=65&view=rev Author: me22 Date: 2006-09-20 15:05:28 -0700 (Wed, 20 Sep 2006) Log Message: ----------- Improve number literal handling Modified Paths: -------------- trunk/interpreter/tokenizer.cpp Modified: trunk/interpreter/tokenizer.cpp =================================================================== --- trunk/interpreter/tokenizer.cpp 2006-09-20 18:41:07 UTC (rev 64) +++ trunk/interpreter/tokenizer.cpp 2006-09-20 22:05:28 UTC (rev 65) @@ -94,7 +94,10 @@ token temp = current; current.data.erase( u ); add_token( tokens, current, source, col ); + + // undo add_token stream adjustment ++col; source.get(); + current = temp; current.col += u; current.data.erase( 0, u ); @@ -119,6 +122,15 @@ skip |= true; } + /// Merge sequential numeric literals + if ( !skip + && current.is( token::NUMBER ) + && previous.is( token::NUMBER ) ) { + // safe because for previous to be a number, it can't be token() + tokens.back().data += current.data; + skip |= true; + } + /// Remove spare separators after OP_SEP, *_BEGIN, /// OP_ASSIGN, or OP_COND if ( !skip ) { @@ -419,6 +431,9 @@ case token::NUMBER: { if ( std::isalnum(c) ) { current.data.push_back(c); + } else if ( c != '_' ) { + // silently drop underscores, perl-style + // allows literals like 1_234_567 or 1.234_567e6 } else { add_token( tokens, current, source, col ); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <me...@us...> - 2006-09-20 18:41:13
|
Revision: 64 http://svn.sourceforge.net/fuphyl/?rev=64&view=rev Author: me22 Date: 2006-09-20 11:41:07 -0700 (Wed, 20 Sep 2006) Log Message: ----------- added take_while and drop_while Modified Paths: -------------- trunk/samples/lists.fph Modified: trunk/samples/lists.fph =================================================================== --- trunk/samples/lists.fph 2006-09-19 21:04:12 UTC (rev 63) +++ trunk/samples/lists.fph 2006-09-20 18:41:07 UTC (rev 64) @@ -107,6 +107,26 @@ // take, drop, split_at, takewhile, dropwhile +take_while (list,f) = + if list.empty ? + []; + else ? + (h,t) = list.pop; + if f h ? + h.cons t.take_while \f; + else ? + []; + +drop_while (list,f) = + if list.empty ? + []; + else ? + (h,t) = list.pop; + if f h ? + list.drop_while \f; + else ? + list; + /////////////////////////////////////////////////////////////////////////////// // zip, unzip This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |