diff options
author | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2009-09-27 20:03:58 +0000 |
---|---|---|
committer | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2009-09-27 20:03:58 +0000 |
commit | 49ba52239afa0e6ab295add695a7443633748cfc (patch) | |
tree | 6e1d2244ae2ebea2bcec28f77e446e5b3ee9b0eb /cpp/sca/modules/eval/read.hpp | |
parent | 16e2ee1f1cacc9486f328ca7a3d7e2480b7e43db (diff) |
Strawman implementation of a mini script evaluation library, which can be used for simple test case components without requiring integration with bigger and more complex 3rd party script engines like Ruby or Python. That way the core test cases should work without requiring the more complex Python or Ruby extensions to work.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@819393 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
-rw-r--r-- | cpp/sca/modules/eval/read.hpp | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/cpp/sca/modules/eval/read.hpp b/cpp/sca/modules/eval/read.hpp new file mode 100644 index 0000000000..8ede2189a6 --- /dev/null +++ b/cpp/sca/modules/eval/read.hpp @@ -0,0 +1,182 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_eval_read_hpp +#define tuscany_eval_read_hpp + +/** + * Script evaluator read functions. + */ + +#include <iostream> +#include <string> +#include <sstream> +#include <ctype.h> + +#include "list.hpp" +#include "value.hpp" +#include "primitive.hpp" + +namespace tuscany +{ + +const value rightParenthesis(makeList(value(")"))); +const value leftParenthesis(makeList(value("("))); + +const double stringToNumber(const std::string& str) { + double d; + std::istringstream is(str); + is >> d; + return d; +} + +const bool isWhitespace(const char ch) { + return ch != -1 && isspace(ch); +} + +const bool isIdentifierStart(const char ch) { + return ch != -1 && !isspace(ch) && !isdigit(ch); +} + +const bool isIdentifierPart(const char ch) { + return ch != -1 && !isspace(ch) && ch != '(' && ch != ')'; +} + +const bool isDigit(const char ch) { + return isdigit(ch); +} + +const bool isLeftParenthesis(const value& token) { + return leftParenthesis == token; +} + +const bool isRightParenthesis(const value& token) { + return rightParenthesis == token; +} + +const char readChar(std::istream& in) { + if(in.eof()) { + return -1; + } + char c = in.get(); + return c; +} + +const char peekChar(std::istream& in) { + if(in.eof()) + return -1; + char c = in.peek(); + return c; +} + +const bool isQuote(const value& token) { + return token == quoteSymbol; +} + +const value readQuoted(std::istream& in); +const value readIdentifier(const char chr, std::istream& in); +const value readString(const char chr, std::istream& in); +const value readNumber(const char chr, std::istream& in); +const value read(std::istream& in); + +const value readToken(std::istream& in) { + const char firstChar = readChar(in); + if(isWhitespace(firstChar)) + return readToken(in); + if(firstChar == '\'') + return readQuoted(in); + if(firstChar == '(') + return leftParenthesis; + if(firstChar == ')') + return rightParenthesis; + if(firstChar == '"') + return readString(firstChar, in); + if(isIdentifierStart(firstChar)) + return readIdentifier(firstChar, in); + if(isDigit(firstChar)) + return readNumber(firstChar, in); + if(firstChar == -1) + return value(); + std::cout << "Illegal lexical syntax '" << firstChar << "'" << "\n"; + return readToken(in); +} + +const value readQuoted(std::istream& in) { + return value(makeList(quoteSymbol, read(in))); +} + +const list<value> readList(const list<value>& listSoFar, std::istream& in) { + const value token = readToken(in); + if(isNil(token) || isRightParenthesis(token)) + return reverse(listSoFar); + if(isLeftParenthesis(token)) + return readList(cons(value(readList(list<value> (), in)), listSoFar), in); + return readList(cons(token, listSoFar), in); +} + +const std::string listToString(const list<char>& l) { + if(l == list<char> ()) + return ""; + return car(l) + listToString(cdr(l)); +} + +const list<char> readIdentifierHelper(const list<char>& listSoFar, std::istream& in) { + const char nextChar = peekChar(in); + if(isIdentifierPart(nextChar)) + return readIdentifierHelper(cons(readChar(in), listSoFar), in); + return reverse(listSoFar); +} + +const value readIdentifier(const char chr, std::istream& in) { + return value(listToString(readIdentifierHelper(makeList(chr), in)).c_str()); +} + +const list<char> readStringHelper(const list<char>& listSoFar, std::istream& in) { + const char nextChar = readChar(in); + if(nextChar != -1 && nextChar != '"') + return readStringHelper(cons(nextChar, listSoFar), in); + return reverse(listSoFar); +} + +const value readString(const char chr, std::istream& in) { + return value(listToString(readStringHelper(list<char>(), in))); +} + +const list<char> readNumberHelper(const list<char>& listSoFar, std::istream& in) { + const char nextChar = peekChar(in); + if(isDigit(nextChar)) + return readNumberHelper(cons(readChar(in), listSoFar), in); + return reverse(listSoFar); +} + +const value readNumber(const char chr, std::istream& in) { + return value(stringToNumber(listToString(readNumberHelper(makeList(chr), in)))); +} + +const value read(std::istream& in) { + const value nextToken = readToken(in); + if(isLeftParenthesis(nextToken)) + return value(readList(list<value> (), in)); + return nextToken; +} + +} +#endif /* tuscany_eval_read_hpp */ |