From b38ff5a30986514a26bd800d113b40d6fcfef0db Mon Sep 17 00:00:00 2001 From: David Robillard Date: Tue, 7 Dec 2010 23:26:35 +0000 Subject: Rename 'lex' to the now more appropriate 'parse'. git-svn-id: http://svn.drobilla.net/resp/resp@308 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- Makefile | 4 +- src/lex.cpp | 207 ---------------------------------------------------------- src/parse.cpp | 207 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 209 insertions(+), 209 deletions(-) delete mode 100644 src/lex.cpp create mode 100644 src/parse.cpp diff --git a/Makefile b/Makefile index b33dd9a..c5af049 100644 --- a/Makefile +++ b/Makefile @@ -25,10 +25,10 @@ OBJECTS = \ build/c.o \ build/compile.o \ build/constrain.o \ + build/expand.o \ build/gc.o \ - build/lex.o \ build/lift.o \ - build/expand.o \ + build/parse.o \ build/pprint.o \ build/repl.o \ build/resp.o \ diff --git a/src/lex.cpp b/src/lex.cpp deleted file mode 100644 index 2ac838f..0000000 --- a/src/lex.cpp +++ /dev/null @@ -1,207 +0,0 @@ -/* Resp: A programming language - * Copyright (C) 2008-2009 David Robillard - * - * Resp is free software: you can redistribute it and/or modify it under - * the terms of the GNU Affero General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * Resp is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General - * Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Resp. If not, see . - */ - -/** @file - * @brief Parsing (build an AST from text) - */ - -#include -#include -#include "resp.hpp" - -using namespace std; - -static inline int -read_char(Cursor& cur, istream& in) -{ - int ch = in.get(); - switch (ch) { - case '\n': ++cur.line; cur.col = 0; break; - default: ++cur.col; - } - return ch; -} - -static inline void -skip_space(Cursor& cur, istream& in) -{ - while (isspace(in.peek())) - read_char(cur, in); -} - -static inline void -eat_char(Cursor& cur, istream& in, const char character) -{ - const char c = read_char(cur, in); - assert(c == character); - return; -} - -static AST* -read_string(Cursor& cur, istream& in) -{ - string str; - char c; - Cursor loc = cur; - eat_char(cur, in, '"'); - while ((c = read_char(cur, in)) != '"') { - if (c == '\\') { // string escape - switch (c = read_char(cur, in)) { - case '"': - str.push_back('"'); - break; - case '\\': - str.push_back('\\'); - break; - default: - cin.putback(c); - throw Error(cur, string("unknown string escape `\\") + (char)c + "'"); - } - } else { // any other character - str.push_back(c); - } - } - return new AString(loc, str); -} - -static AST* -read_line_comment(Cursor& cur, istream& in) -{ - char c; - while ((c = read_char(cur, in)) != '\n') {} - return NULL; -} - -static AST* -read_list(PEnv& penv, Cursor& cur, istream& in) -{ - List list; - - eat_char(cur, in, '('); - while (true) { - skip_space(cur, in); - if (in.peek() == ')') { - eat_char(cur, in, ')'); - return list.head; - } - - list.push_back(read_expression(penv, cur, in)); - } - assert(false); -} - -static AST* -read_special(Cursor& cur, istream& in) -{ - eat_char(cur, in, '#'); - switch (in.peek()) { - case '|': - while (!(read_char(cur, in) == '|' && read_char(cur, in) == '#')) {} - return NULL; - case 't': - eat_char(cur, in, 't'); - return new ALiteral(T_BOOL, true, cur); - case 'f': - return new ALiteral(T_BOOL, false, cur); - default: - throw Error(cur, (format("unknown special lexeme `%1%'") % in.peek()).str()); - } - assert(false); - return NULL; -} - -static AST* -read_number(Cursor& cur, istream& in) -{ - string str; - char c; - Cursor loc = cur; - while ((c = in.peek()) != EOF) { - if (isdigit(c) || c == '.') - str += read_char(cur, in); - else - break; - } - - if (str.find('.') == string::npos) - return new ALiteral(T_INT32, strtol(str.c_str(), NULL, 10), loc); - else - return new ALiteral(T_FLOAT, strtod(str.c_str(), NULL), loc); -} - -static AST* -read_symbol(PEnv& penv, Cursor& cur, istream& in) -{ - string str; - char c; - Cursor loc = cur; - while ((c = in.peek()) != EOF) { - if (!isspace(c) && c != ')' && c != '(' && c != EOF && c != -1) { - str += read_char(cur, in); - } else { - break; - } - } - - return penv.sym(str); -} - -/// Read an expression from @a in -AST* -read_expression(PEnv& penv, Cursor& cur, istream& in) -{ - while (!cin.eof()) { - skip_space(cur, in); - const char c = in.peek(); - switch (c) { - case EOF: - return NULL; - case ';': - read_line_comment(cur, in); - break; - case '"': - return read_string(cur, in); - case '(': - return read_list(penv, cur, in); - case ')': - throw Error(cur, "unexpected `)'"); - case '#': - { - AST* ret = read_special(cur, in); - if (ret) - return ret; - break; - } - case '-': - case '+': - read_char(cur, in); - if (isdigit(in.peek())) { - in.putback(c); - return read_number(cur, in); - } else { - in.putback(c); - return read_symbol(penv, cur, in); - } - default: - if (isdigit(c)) - return read_number(cur, in); - else - return read_symbol(penv, cur, in); - } - } - return NULL; -} diff --git a/src/parse.cpp b/src/parse.cpp new file mode 100644 index 0000000..2ac838f --- /dev/null +++ b/src/parse.cpp @@ -0,0 +1,207 @@ +/* Resp: A programming language + * Copyright (C) 2008-2009 David Robillard + * + * Resp is free software: you can redistribute it and/or modify it under + * the terms of the GNU Affero General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Resp is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General + * Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Resp. If not, see . + */ + +/** @file + * @brief Parsing (build an AST from text) + */ + +#include +#include +#include "resp.hpp" + +using namespace std; + +static inline int +read_char(Cursor& cur, istream& in) +{ + int ch = in.get(); + switch (ch) { + case '\n': ++cur.line; cur.col = 0; break; + default: ++cur.col; + } + return ch; +} + +static inline void +skip_space(Cursor& cur, istream& in) +{ + while (isspace(in.peek())) + read_char(cur, in); +} + +static inline void +eat_char(Cursor& cur, istream& in, const char character) +{ + const char c = read_char(cur, in); + assert(c == character); + return; +} + +static AST* +read_string(Cursor& cur, istream& in) +{ + string str; + char c; + Cursor loc = cur; + eat_char(cur, in, '"'); + while ((c = read_char(cur, in)) != '"') { + if (c == '\\') { // string escape + switch (c = read_char(cur, in)) { + case '"': + str.push_back('"'); + break; + case '\\': + str.push_back('\\'); + break; + default: + cin.putback(c); + throw Error(cur, string("unknown string escape `\\") + (char)c + "'"); + } + } else { // any other character + str.push_back(c); + } + } + return new AString(loc, str); +} + +static AST* +read_line_comment(Cursor& cur, istream& in) +{ + char c; + while ((c = read_char(cur, in)) != '\n') {} + return NULL; +} + +static AST* +read_list(PEnv& penv, Cursor& cur, istream& in) +{ + List list; + + eat_char(cur, in, '('); + while (true) { + skip_space(cur, in); + if (in.peek() == ')') { + eat_char(cur, in, ')'); + return list.head; + } + + list.push_back(read_expression(penv, cur, in)); + } + assert(false); +} + +static AST* +read_special(Cursor& cur, istream& in) +{ + eat_char(cur, in, '#'); + switch (in.peek()) { + case '|': + while (!(read_char(cur, in) == '|' && read_char(cur, in) == '#')) {} + return NULL; + case 't': + eat_char(cur, in, 't'); + return new ALiteral(T_BOOL, true, cur); + case 'f': + return new ALiteral(T_BOOL, false, cur); + default: + throw Error(cur, (format("unknown special lexeme `%1%'") % in.peek()).str()); + } + assert(false); + return NULL; +} + +static AST* +read_number(Cursor& cur, istream& in) +{ + string str; + char c; + Cursor loc = cur; + while ((c = in.peek()) != EOF) { + if (isdigit(c) || c == '.') + str += read_char(cur, in); + else + break; + } + + if (str.find('.') == string::npos) + return new ALiteral(T_INT32, strtol(str.c_str(), NULL, 10), loc); + else + return new ALiteral(T_FLOAT, strtod(str.c_str(), NULL), loc); +} + +static AST* +read_symbol(PEnv& penv, Cursor& cur, istream& in) +{ + string str; + char c; + Cursor loc = cur; + while ((c = in.peek()) != EOF) { + if (!isspace(c) && c != ')' && c != '(' && c != EOF && c != -1) { + str += read_char(cur, in); + } else { + break; + } + } + + return penv.sym(str); +} + +/// Read an expression from @a in +AST* +read_expression(PEnv& penv, Cursor& cur, istream& in) +{ + while (!cin.eof()) { + skip_space(cur, in); + const char c = in.peek(); + switch (c) { + case EOF: + return NULL; + case ';': + read_line_comment(cur, in); + break; + case '"': + return read_string(cur, in); + case '(': + return read_list(penv, cur, in); + case ')': + throw Error(cur, "unexpected `)'"); + case '#': + { + AST* ret = read_special(cur, in); + if (ret) + return ret; + break; + } + case '-': + case '+': + read_char(cur, in); + if (isdigit(in.peek())) { + in.putback(c); + return read_number(cur, in); + } else { + in.putback(c); + return read_symbol(penv, cur, in); + } + default: + if (isdigit(c)) + return read_number(cur, in); + else + return read_symbol(penv, cur, in); + } + } + return NULL; +} -- cgit v1.2.1