From ae0b7c0063db6236be2d7cf01ddbf2159f77c98c Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Tue, 11 Dec 2012 03:51:03 +0000 Subject: Port kernel to C++11 and refactor some of the core modules. Convert functors to lambdas, and add C++ const, noexcept and inline annotations to get more efficient generated code. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1419985 13f79535-47bb-0310-9956-ffa450edef68 --- sca-cpp/trunk/modules/scheme/Makefile.am | 14 +-- sca-cpp/trunk/modules/scheme/driver.hpp | 2 +- sca-cpp/trunk/modules/scheme/element-value.cpp | 6 +- sca-cpp/trunk/modules/scheme/element-xml.cpp | 51 +++++++++ sca-cpp/trunk/modules/scheme/environment.hpp | 16 +-- sca-cpp/trunk/modules/scheme/eval.hpp | 10 +- sca-cpp/trunk/modules/scheme/io.hpp | 150 +++++++++++++------------ sca-cpp/trunk/modules/scheme/primitive.hpp | 86 +++++++------- sca-cpp/trunk/modules/scheme/scheme-shell.cpp | 2 +- sca-cpp/trunk/modules/scheme/scheme-test.cpp | 126 +++++++++++++++------ sca-cpp/trunk/modules/scheme/scheme-test.hpp | 34 ++++++ sca-cpp/trunk/modules/scheme/test.scm | 44 ++++++++ sca-cpp/trunk/modules/scheme/value-element.cpp | 6 +- sca-cpp/trunk/modules/scheme/value-xml.cpp | 51 --------- sca-cpp/trunk/modules/scheme/xml-element.cpp | 47 ++++++++ sca-cpp/trunk/modules/scheme/xml-value.cpp | 47 -------- 16 files changed, 416 insertions(+), 276 deletions(-) create mode 100644 sca-cpp/trunk/modules/scheme/element-xml.cpp create mode 100644 sca-cpp/trunk/modules/scheme/scheme-test.hpp create mode 100644 sca-cpp/trunk/modules/scheme/test.scm delete mode 100644 sca-cpp/trunk/modules/scheme/value-xml.cpp create mode 100644 sca-cpp/trunk/modules/scheme/xml-element.cpp delete mode 100644 sca-cpp/trunk/modules/scheme/xml-value.cpp (limited to 'sca-cpp/trunk/modules/scheme') diff --git a/sca-cpp/trunk/modules/scheme/Makefile.am b/sca-cpp/trunk/modules/scheme/Makefile.am index 130fe14303..460dbb3f82 100644 --- a/sca-cpp/trunk/modules/scheme/Makefile.am +++ b/sca-cpp/trunk/modules/scheme/Makefile.am @@ -30,18 +30,18 @@ value_element_LDFLAGS = element_value_SOURCES = element-value.cpp element_value_LDFLAGS = -xml_value_SOURCES = xml-value.cpp -xml_value_LDFLAGS = -lxml2 +xml_element_SOURCES = xml-element.cpp +xml_element_LDFLAGS = -lxml2 -value_xml_SOURCES = value-xml.cpp -value_xml_LDFLAGS = -lxml2 +element_xml_SOURCES = element-xml.cpp +element_xml_LDFLAGS = -lxml2 json_value_SOURCES = json-value.cpp -json_value_LDFLAGS = -lmozjs +json_value_LDFLAGS = -ljansson value_json_SOURCES = value-json.cpp -value_json_LDFLAGS = -lmozjs +value_json_LDFLAGS = -ljansson noinst_PROGRAMS = scheme-test -mod_PROGRAMS = scheme-shell element-value value-element xml-value value-xml json-value value-json +mod_PROGRAMS = scheme-shell element-value value-element xml-element element-xml json-value value-json TESTS = scheme-test diff --git a/sca-cpp/trunk/modules/scheme/driver.hpp b/sca-cpp/trunk/modules/scheme/driver.hpp index 112c226ed1..c94af7bbf7 100644 --- a/sca-cpp/trunk/modules/scheme/driver.hpp +++ b/sca-cpp/trunk/modules/scheme/driver.hpp @@ -55,7 +55,7 @@ const bool userPrint(const value val, ostream& out) { const value evalDriverLoop(istream& in, ostream& out, Env& env) { promptForInput(evalInputPrompt, out); - value input = readValue(in); + const value input = content(readValue(in)); if (isNil(input)) return input; const value output = evalExpr(input, env); diff --git a/sca-cpp/trunk/modules/scheme/element-value.cpp b/sca-cpp/trunk/modules/scheme/element-value.cpp index 8a443dbdb2..bce6042615 100644 --- a/sca-cpp/trunk/modules/scheme/element-value.cpp +++ b/sca-cpp/trunk/modules/scheme/element-value.cpp @@ -31,9 +31,9 @@ namespace tuscany { namespace scheme { -int elementValue() { - const value v = elementsToValues(readValue(cin)); - cout << writeValue(v); +const int elementValue() { + const value v = elementsToValues(content(readValue(cin))); + write(content(writeValue(v)), cout); return 0; } diff --git a/sca-cpp/trunk/modules/scheme/element-xml.cpp b/sca-cpp/trunk/modules/scheme/element-xml.cpp new file mode 100644 index 0000000000..c7bc7661ab --- /dev/null +++ b/sca-cpp/trunk/modules/scheme/element-xml.cpp @@ -0,0 +1,51 @@ +/* + * 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$ */ + +/** + * Convert a scheme value to an XML document. + */ + +#include "fstream.hpp" +#include "string.hpp" +#include "element.hpp" +#include "../xml/xml.hpp" +#include "eval.hpp" + +namespace tuscany { +namespace scheme { + +const int valueXML() { + const failable > s = xml::writeElements(content(readValue(cin))); + if (!hasContent(s)) { + cerr << reason(s) << " : " << rcode(s); + return 1; + } + write(content(s), cout); + return 0; +} + +} +} + +int main() { + return tuscany::scheme::valueXML(); +} + diff --git a/sca-cpp/trunk/modules/scheme/environment.hpp b/sca-cpp/trunk/modules/scheme/environment.hpp index 303a37cb3c..1a295c74f4 100644 --- a/sca-cpp/trunk/modules/scheme/environment.hpp +++ b/sca-cpp/trunk/modules/scheme/environment.hpp @@ -45,7 +45,7 @@ const value setSymbol("set!"); const value dotSymbol("."); const Env theEmptyEnvironment() { - return list(); + return nilListValue; } const bool isDefinition(const value& exp) { @@ -68,11 +68,11 @@ const gc_ptr firstFrame(const Env& env) { return car(env); } -list frameVariables(const Frame& frame) { +const list frameVariables(const Frame& frame) { return car((list )frame); } -list frameValues(const Frame& frame) { +const list frameValues(const Frame& frame) { return cdr((list )frame); } @@ -103,8 +103,7 @@ const Frame makeBinding(const Frame& frameSoFar, const list& variables, c } const gc_ptr makeFrame(const list& variables, const list values) { - gc_ptr frame = new (gc_new()) Frame(); - *frame = value(makeBinding(cons(value(list()), list()), variables, values)); + const gc_ptr frame = new (gc_new()) Frame(makeBinding(cons(value(nilListValue), nilListValue), variables, values)); return frame; } @@ -120,7 +119,7 @@ const value definitionValue(const value& exp) { const list exps(exp); if(isSymbol(car(cdr(exps)))) { if (isNil(cdr(cdr(exps)))) - return value(); + return nilValue; return car(cdr(cdr(exps))); } const list lexps(car(cdr(exps))); @@ -140,7 +139,8 @@ const Frame addBindingToFrame(const value& var, const value& val, const Frame& f } const bool defineVariable(const value& var, const value& val, Env& env) { - *firstFrame(env) = addBindingToFrame(var, val, *firstFrame(env)); + const Frame newFrame = addBindingToFrame(var, val, *firstFrame(env)); + setvalue(*firstFrame(env), addBindingToFrame(var, val, *firstFrame(env))); return true; } @@ -168,7 +168,7 @@ const value lookupEnvScan(const value& var, const list& vars, const list< const value lookupEnvLoop(const value& var, const Env& env) { if(env == theEmptyEnvironment()) { logStream() << "Unbound variable " << var << endl; - return value(); + return nilValue; } return lookupEnvScan(var, frameVariables(*firstFrame(env)), frameValues(*firstFrame(env)), env); } diff --git a/sca-cpp/trunk/modules/scheme/eval.hpp b/sca-cpp/trunk/modules/scheme/eval.hpp index 34d1a7bc17..5074471931 100644 --- a/sca-cpp/trunk/modules/scheme/eval.hpp +++ b/sca-cpp/trunk/modules/scheme/eval.hpp @@ -147,7 +147,7 @@ const value applyProcedure(const value& procedure, list& arguments) { return evalSequence(procedureBody(procedure), env); } logStream() << "Unknown procedure type " << procedure << endl; - return value(); + return nilValue; } const value sequenceToExp(const list exps) { @@ -209,16 +209,16 @@ const value expandClauses(const list& clauses) { if(isNil(rest)) return sequenceToExp(condActions(first)); logStream() << "else clause isn't last " << clauses << endl; - return value(); + return nilValue; } return makeIf(condPredicate(first), sequenceToExp(condActions(first)), expandClauses(rest)); } -value condToIf(const value& exp) { +const value condToIf(const value& exp) { return expandClauses(condClauses(exp)); } -value evalIf(const value& exp, Env& env) { +const value evalIf(const value& exp, Env& env) { if(isTrue(evalExpr(ifPredicate(exp), env))) return evalExpr(ifConsequent(exp), env); return evalExpr(ifAlternative(exp), env); @@ -255,7 +255,7 @@ const value evalExpr(const value& exp, Env& env) { return applyProcedure(evalExpr(operat(exp), env), operandValues); } logStream() << "Unknown expression type " << exp << endl; - return value(); + return nilValue; } const list quotedParameters(const list& p) { diff --git a/sca-cpp/trunk/modules/scheme/io.hpp b/sca-cpp/trunk/modules/scheme/io.hpp index 8f9d70e7fe..02f6923c86 100644 --- a/sca-cpp/trunk/modules/scheme/io.hpp +++ b/sca-cpp/trunk/modules/scheme/io.hpp @@ -37,39 +37,38 @@ namespace tuscany { namespace scheme { -const value rightParenthesis(mklist(")")); -const value leftParenthesis(mklist("(")); -const value comment(mklist(";")); +const value rightParenthesis(")"); +const value leftParenthesis("("); -const double stringToNumber(const string& str) { +inline const double stringToNumber(const string& str) { return atof(c_str(str)); } -const bool isWhitespace(const char ch) { +inline const bool isWhitespace(const char ch) { return ch != -1 && isspace(ch); } -const bool isIdentifierStart(const char ch) { +inline const bool isIdentifierStart(const char ch) { return ch != -1 && !isspace(ch) && !isdigit(ch); } -const bool isIdentifierPart(const char ch) { +inline const bool isIdentifierPart(const char ch) { return ch != -1 && !isspace(ch) && ch != '(' && ch != ')'; } -const bool isDigit(const char ch) { +inline const bool isDigit(const char ch) { return isdigit(ch) || ch == '.'; } -const bool isLeftParenthesis(const value& token) { +inline const bool isLeftParenthesis(const value& token) { return leftParenthesis == token; } -const bool isRightParenthesis(const value& token) { +inline const bool isRightParenthesis(const value& token) { return rightParenthesis == token; } -const char readChar(istream& in) { +inline const char readChar(istream& in) { if(in.eof()) { return -1; } @@ -77,14 +76,14 @@ const char readChar(istream& in) { return c; } -const char peekChar(istream& in) { +inline const char peekChar(istream& in) { if(eof(in)) return -1; char c = (char)peek(in); return c; } -const bool isQuote(const value& token) { +inline const bool isQuote(const value& token) { return token == quoteSymbol; } @@ -93,9 +92,9 @@ const value readQuoted(istream& in); const value readIdentifier(const char chr, istream& in); const value readString(istream& in); const value readNumber(const char chr, istream& in); -const value readValue(istream& in); +const failable readValue(istream& in); -const failable readToken(istream& in) { +inline const failable readToken(istream& in) { const char firstChar = readChar(in); if(isWhitespace(firstChar)) return readToken(in); @@ -119,18 +118,19 @@ const failable readToken(istream& in) { return readToken(in); } -const failable skipComment(istream& in) { - const char nextChar = readChar(in); - if (nextChar == '\n') - return readToken(in); - return skipComment(in); +inline const failable skipComment(istream& in) { + while(true) { + const char nextChar = readChar(in); + if (nextChar == '\n') + return readToken(in); + } } -const value readQuoted(istream& in) { - return mklist(quoteSymbol, readValue(in)); +inline const value readQuoted(istream& in) { + return mklist(quoteSymbol, content(readValue(in))); } -const list readList(const list& listSoFar, istream& in) { +inline const list readList(const list& listSoFar, istream& in) { const failable ftoken = readToken(in); if (!hasContent(ftoken)) return reverse(listSoFar); @@ -142,95 +142,97 @@ const list readList(const list& listSoFar, istream& in) { return readList(cons(token, listSoFar), in); } -const string listToString(const list& l) { - if(isNil(l)) - return ""; - const char buf[1] = { car(l) }; - return string(buf, 1) + listToString(cdr(l)); -} - -const list readIdentifierHelper(const list& listSoFar, istream& in) { - const char nextChar = peekChar(in); - if(isIdentifierPart(nextChar)) - return readIdentifierHelper(cons(readChar(in), listSoFar), in); - return reverse(listSoFar); +inline const string readIdentifierHelper(const char chr, istream& in) { + ostringstream buf; + buf << chr; + while(true) { + const char nextChar = peekChar(in); + if(!isIdentifierPart(nextChar)) + return str(buf); + buf << readChar(in); + } } -const value readIdentifier(const char chr, istream& in) { - const value val = c_str(listToString(readIdentifierHelper(mklist(chr), in))); +inline const value readIdentifier(const char chr, istream& in) { + const value val = c_str(readIdentifierHelper(chr, in)); if (val == "false") return value((bool)false); if (val == "true") return value((bool)true); if (val == "nil") - return value(); + return nilValue; return val; } -const list readStringHelper(const list& listSoFar, istream& in) { - const char nextChar = readChar(in); - if(nextChar == -1 || nextChar == '"') - return reverse(listSoFar); - if (nextChar == '\\') { - const char escapedChar = readChar(in); - if (escapedChar == -1) - return reverse(listSoFar); - return readStringHelper(cons(escapedChar, listSoFar), in); +inline const value readString(istream& in) { + ostringstream buf; + while(true) { + const char nextChar = readChar(in); + if(nextChar == -1 || nextChar == '"') + return (value)str(buf); + if (nextChar == '\\') { + const char escapedChar = readChar(in); + if (escapedChar == -1) + return (value)str(buf); + buf << escapedChar; + } + buf << nextChar; } - return readStringHelper(cons(nextChar, listSoFar), in); } -const value readString(istream& in) { - return listToString(readStringHelper(list(), in)); -} - -const list readNumberHelper(const list& listSoFar, 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, istream& in) { - return stringToNumber(listToString(readNumberHelper(mklist(chr), in))); +inline const value readNumber(const char chr, istream& in) { + ostringstream buf; + buf << chr; + while(true) { + const char nextChar = peekChar(in); + if(!isDigit(nextChar)) + return stringToNumber(str(buf)); + buf << readChar(in); + } } -const value readValue(istream& in) { +inline const failable readValue(istream& in) { const failable fnextToken = readToken(in); if (!hasContent(fnextToken)) - return value(); + return nilValue; const value nextToken = content(fnextToken); if(isLeftParenthesis(nextToken)) - return readList(list(), in); + return (value)readList(nilListValue, in); return nextToken; } -const value readValue(const string s) { +inline const failable readValue(const string& s) { istringstream in(s); const failable fnextToken = readToken(in); if (!hasContent(fnextToken)) - return value(); + return nilValue; const value nextToken = content(fnextToken); if(isLeftParenthesis(nextToken)) - return readList(list(), in); + return (value)readList(nilListValue, in); return nextToken; } -const bool writeValue(const value& val, ostream& out) { +inline const failable readValue(const list& l) { + ostringstream os; + write(l, os); + return readValue(str(os)); +} + +inline const failable writeValue(const value& val, ostream& out) { out << val; - return true; + return out; } -const string writeValue(const value& val) { +inline const failable > writeValue(const value& val) { ostringstream out; out << val; - return str(out); + return mklist(str(out)); } -const value readScript(istream& in) { - const value val = readValue(in); +inline const value readScript(istream& in) { + const value val = content(readValue(in)); if (isNil(val)) - return list(); + return nilListValue; return cons(val, (list)readScript(in)); } diff --git a/sca-cpp/trunk/modules/scheme/primitive.hpp b/sca-cpp/trunk/modules/scheme/primitive.hpp index 59aee12073..2e0c4f62dd 100644 --- a/sca-cpp/trunk/modules/scheme/primitive.hpp +++ b/sca-cpp/trunk/modules/scheme/primitive.hpp @@ -40,7 +40,7 @@ const value quoteSymbol("'"); const value lambdaSymbol("lambda"); #ifdef WANT_THREADS -perthread_ptr displayOutStream; +const perthread_ptr displayOutStream; #else ostream* displayOutStream = NULL; #endif @@ -51,49 +51,49 @@ perthread_ptr logOutStream; ostream* logOutStream = NULL; #endif -const bool setupDisplay(ostream& out) { +inline const bool setupDisplay(ostream& out) { displayOutStream = &out; return true; } -ostream& displayStream() { +inline ostream& displayStream() { if (displayOutStream == NULL) return cout; return *displayOutStream; } -const bool setupLog(ostream& out) { +inline const bool setupLog(ostream& out) { logOutStream = &out; return true; } -ostream& logStream() { +inline ostream& logStream() { if (logOutStream == NULL) return cerr; return *logOutStream; } -const value carProc(const list& args) { +inline const value carProc(const list& args) { return car((list )car(args)); } -const value cdrProc(const list& args) { +inline const value cdrProc(const list& args) { return cdr((list )car(args)); } -const value consProc(const list& args) { +inline const value consProc(const list& args) { return cons(car(args), (list )cadr(args)); } -const value listProc(const list& args) { +inline const value listProc(const list& args) { return args; } -const value assocProc(const list& args) { +inline const value assocProc(const list& args) { return assoc(car(args), (list >)cadr(args)); } -const value nulProc(const list& args) { +inline const value nulProc(const list& args) { const value v(car(args)); if (isNil(v)) return true; @@ -102,31 +102,31 @@ const value nulProc(const list& args) { return false; } -const value equalProc(const list& args) { +inline const value equalProc(const list& args) { return (bool)(car(args) == cadr(args)); } -const value addProc(const list& args) { +inline const value addProc(const list& args) { if (isNil(cdr(args))) return (double)car(args); return (double)car(args) + (double)cadr(args); } -const value subProc(const list& args) { +inline const value subProc(const list& args) { if (isNil(cdr(args))) return (double)0 - (double)car(args); return (double)car(args) - (double)cadr(args); } -const value mulProc(const list& args) { +inline const value mulProc(const list& args) { return (double)car(args) * (double)cadr(args); } -const value divProc(const list& args) { +inline const value divProc(const list& args) { return (double)car(args) / (double)cadr(args); } -const value displayProc(const list& args) { +inline const value displayProc(const list& args) { if (isNil(args)) { displayStream() << endl; return true; @@ -135,7 +135,7 @@ const value displayProc(const list& args) { return displayProc(cdr(args)); } -const value logProc(const list& args) { +inline const value logProc(const list& args) { if (isNil(args)) { logStream() << endl; return true; @@ -144,52 +144,52 @@ const value logProc(const list& args) { return logProc(cdr(args)); } -const value uuidProc(unused const list& args) { +inline const value uuidProc(unused const list& args) { return mkuuid(); } -const value cadrProc(const list& args) { +inline const value cadrProc(const list& args) { return cadr((list )car(args)); } -const value caddrProc(const list& args) { +inline const value caddrProc(const list& args) { return caddr((list )car(args)); } -const value cadddrProc(const list& args) { +inline const value cadddrProc(const list& args) { return cadddr((list )car(args)); } -const value cddrProc(const list& args) { +inline const value cddrProc(const list& args) { return cddr((list )car(args)); } -const value cdddrProc(const list& args) { +inline const value cdddrProc(const list& args) { return cdddr((list )car(args)); } -const value appendProc(const list& args) { +inline const value appendProc(const list& args) { return append((list )car(args), (list)cadr(args)); } -const value startProc(unused const list& args) { - return lambda&)>(); +inline const value startProc(unused const list& args) { + return lvvlambda(); } -const value stopProc(unused const list& args) { - return lambda&)>(); +inline const value stopProc(unused const list& args) { + return lvvlambda(); } -const value applyPrimitiveProcedure(const value& proc, list& args) { - const lambda&)> func(cadr((list)proc)); +inline const value applyPrimitiveProcedure(const value& proc, list& args) { + const lvvlambda func(cadr((list)proc)); return func(args); } -const bool isPrimitiveProcedure(const value& proc) { +inline const bool isPrimitiveProcedure(const value& proc) { return isTaggedList(proc, primitiveSymbol); } -const bool isSelfEvaluating(const value& exp) { +inline const bool isSelfEvaluating(const value& exp) { if(isNil(exp)) return true; if(isNumber(exp)) @@ -203,15 +203,15 @@ const bool isSelfEvaluating(const value& exp) { return false; } -const value primitiveImplementation(const list& proc) { +inline const value primitiveImplementation(const list& proc) { return car(cdr(proc)); } -template const value primitiveProcedure(const F& f) { - return mklist(primitiveSymbol, (lambda&)>)f); +template inline const value primitiveProcedure(const F& f) { + return mklist(primitiveSymbol, (lvvlambda)f); } -const list primitiveProcedureNames() { +inline const list primitiveProcedureNames() { return mklist("car") + "cdr" + "cons" @@ -237,7 +237,7 @@ const list primitiveProcedureNames() { + "stop"; } -const list primitiveProcedureObjects() { +inline const list primitiveProcedureObjects() { return mklist(primitiveProcedure(carProc)) + primitiveProcedure(cdrProc) + primitiveProcedure(consProc) @@ -263,23 +263,23 @@ const list primitiveProcedureObjects() { + primitiveProcedure(stopProc); } -const bool isFalse(const value& exp) { +inline const bool isFalse(const value& exp) { return (bool)exp == false; } -const bool isTrue(const value& exp) { +inline const bool isTrue(const value& exp) { return (bool)exp == true; } -const bool isQuoted(const value& exp) { +inline const bool isQuoted(const value& exp) { return isTaggedList(exp, quoteSymbol); } -const value textOfQuotation(const value& exp) { +inline const value textOfQuotation(const value& exp) { return car(cdr((list )exp)); } -const value makeLambda(const list& parameters, const list& body) { +inline const value makeLambda(const list& parameters, const list& body) { return cons(lambdaSymbol, cons(parameters, body)); } diff --git a/sca-cpp/trunk/modules/scheme/scheme-shell.cpp b/sca-cpp/trunk/modules/scheme/scheme-shell.cpp index 4aa67c2375..96ea5e834e 100644 --- a/sca-cpp/trunk/modules/scheme/scheme-shell.cpp +++ b/sca-cpp/trunk/modules/scheme/scheme-shell.cpp @@ -30,7 +30,7 @@ #include "driver.hpp" int main() { - tuscany::gc_scoped_pool pool; + const tuscany::gc_scoped_pool pool; tuscany::scheme::evalDriverRun(tuscany::cin, tuscany::cout); return 0; } diff --git a/sca-cpp/trunk/modules/scheme/scheme-test.cpp b/sca-cpp/trunk/modules/scheme/scheme-test.cpp index 5b69b8e588..e83f99456f 100644 --- a/sca-cpp/trunk/modules/scheme/scheme-test.cpp +++ b/sca-cpp/trunk/modules/scheme/scheme-test.cpp @@ -26,14 +26,16 @@ #include #include "stream.hpp" #include "string.hpp" +#include "perf.hpp" #include "driver.hpp" +#include "scheme-test.hpp" namespace tuscany { namespace scheme { -bool testEnv() { - gc_scoped_pool pool; - Env globalEnv = list(); +const bool testEnv() { + const gc_scoped_pool pool; + Env globalEnv = nilListValue; Env env = extendEnvironment(mklist("a"), mklist(1), globalEnv); defineVariable("x", env, env); assert(lookupVariableValue(value("x"), env) == env); @@ -41,7 +43,7 @@ bool testEnv() { return true; } -bool testEnvGC() { +const bool testEnvGC() { resetLambdaCounters(); resetListCounters(); resetValueCounters(); @@ -52,49 +54,49 @@ bool testEnvGC() { return true; } -bool testRead() { +const bool testRead() { istringstream is("abcd"); - assert(readValue(is) == "abcd"); + assert(content(readValue(is)) == "abcd"); istringstream is2("123"); - assert(readValue(is2) == value(123)); + assert(content(readValue(is2)) == value(123)); istringstream is3("(abcd)"); - assert(readValue(is3) == mklist(value("abcd"))); + assert(content(readValue(is3)) == mklist(value("abcd"))); istringstream is4("(abcd xyz)"); - assert(readValue(is4) == mklist("abcd", "xyz")); + assert(content(readValue(is4)) == mklist("abcd", "xyz")); istringstream is5("(abcd (xyz tuv))"); - assert(readValue(is5) == mklist("abcd", mklist("xyz", "tuv"))); + assert(content(readValue(is5)) == mklist("abcd", mklist("xyz", "tuv"))); return true; } -bool testWrite() { +const bool testWrite() { { - const list i = list() - + (list() + "item" + "cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b" - + (list() + "item" - + (list() + "name" + "Apple") - + (list() + "price" + "$2.99"))) - + (list() + "item" + "cart-53d67a61-aa5e-4e5e-8401-39edeba8b83c" - + (list() + "item" - + (list() + "name" + "Orange") - + (list() + "price" + "$3.55"))); + const list i = nilListValue + + (nilListValue + "item" + "cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b" + + (nilListValue + "item" + + (nilListValue + "name" + "Apple") + + (nilListValue + "price" + "$2.99"))) + + (nilListValue + "item" + "cart-53d67a61-aa5e-4e5e-8401-39edeba8b83c" + + (nilListValue + "item" + + (nilListValue + "name" + "Orange") + + (nilListValue + "price" + "$3.55"))); const list a = cons("Feed", cons("feed-1234", i)); ostringstream os; writeValue(a, os); istringstream is(str(os)); - assert(readValue(is) == a); + assert(content(readValue(is)) == a); } { - const list i = mklist("x", value()); + const list i = mklist("x", nilValue); const list a = mklist(i); ostringstream os; writeValue(a, os); istringstream is(str(os)); - assert(readValue(is) == a); + assert(content(readValue(is)) == a); } return true; } @@ -147,8 +149,8 @@ const string evalOutput(const string& scm) { return str(os); } -bool testEval() { - gc_scoped_pool pool; +const bool testEval() { + const gc_scoped_pool pool; assert(contains(evalOutput(testSchemeNumber), "testNumber ok")); assert(contains(evalOutput(testSchemeString), "testString ok")); assert(contains(evalOutput(testSchemeDefinition), "testDefinition ok")); @@ -161,8 +163,8 @@ bool testEval() { return true; } -bool testEvalExpr() { - gc_scoped_pool pool; +const bool testEvalExpr() { + const gc_scoped_pool pool; const value exp = mklist("+", 2, 3); Env env = setupEnvironment(); const value r = evalExpr(exp, env); @@ -170,8 +172,8 @@ bool testEvalExpr() { return true; } -bool testEvalRun() { - gc_scoped_pool pool; +const bool testEvalRun() { + const gc_scoped_pool pool; evalDriverRun(cin, cout); return true; } @@ -188,8 +190,8 @@ const string testReturnLambda( const string testCallLambda( "(define (testCallLambda l x y) (l x y))"); -bool testEvalLambda() { - gc_scoped_pool pool; +const bool testEvalLambda() { + const gc_scoped_pool pool; Env env = setupEnvironment(); const value trl = mklist("testReturnLambda"); @@ -208,7 +210,18 @@ bool testEvalLambda() { return true; } -bool testEvalGC() { +const bool testEvalScript() { + const gc_scoped_pool pool; + Env env = setupEnvironment(); + ifstream is("test.scm"); + const value script = readScript(is); + const value expr = mklist("echo", string("x")); + const value res = evalScript(expr, script, env); + assert(res == string("x")); + return true; +} + +const bool testEvalGC() { resetLambdaCounters(); resetListCounters(); resetValueCounters(); @@ -221,11 +234,55 @@ bool testEvalGC() { return true; } +const string testCustomer = "((customer (@name \"jdoe\") (account (id \"1234\") (@balance 1000)) (address (@city \"san francisco\") (@state \"ca\"))))"; + +const bool testReadWrite() { + const gc_scoped_pool pool; + + istringstream is(testCustomer); + const value r = content(readValue(is)); + + ostringstream os; + writeValue(r, os); + //assert(str(os) == testCustomer); + return true; +} + +const bool testReadWritePerf() { + const gc_scoped_pool pool; + + const blambda rwl = blambda(testReadWrite); + cout << "Scheme read + write test " << time(rwl, 5, 200) << " ms" << endl; + + return true; +} + +const bool testReadWriteBigDoc() { + const gc_scoped_pool pool; + + istringstream is(testBigDoc); + const value r = content(readValue(is)); + + ostringstream os; + writeValue(r, os); + //assert(str(os) == testBigDoc); + return true; +} + +const bool testReadWriteBigDocPerf() { + const gc_scoped_pool pool; + + const blambda rwl = blambda(testReadWriteBigDoc); + cout << "Scheme big doc read + write test " << time(rwl, 5, 200) << " ms" << endl; + + return true; +} + } } int main() { - tuscany::gc_scoped_pool p; + const tuscany::gc_scoped_pool p; tuscany::cout << "Testing..." << tuscany::endl; tuscany::scheme::testEnv(); @@ -235,7 +292,10 @@ int main() { tuscany::scheme::testEval(); tuscany::scheme::testEvalExpr(); tuscany::scheme::testEvalLambda(); + tuscany::scheme::testEvalScript(); tuscany::scheme::testEvalGC(); + tuscany::scheme::testReadWritePerf(); + tuscany::scheme::testReadWriteBigDocPerf(); tuscany::cout << "OK" << tuscany::endl; return 0; diff --git a/sca-cpp/trunk/modules/scheme/scheme-test.hpp b/sca-cpp/trunk/modules/scheme/scheme-test.hpp new file mode 100644 index 0000000000..67f32344a8 --- /dev/null +++ b/sca-cpp/trunk/modules/scheme/scheme-test.hpp @@ -0,0 +1,34 @@ +/* + * 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$ */ + +/** + * Test Scheme doc. + */ + +namespace tuscany { +namespace scheme { + +const string testBigDoc = "((feed (title \"Search Results\") (id \"search\") (entry (title \"An empty app template\") (id \"new\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"An empty test app\") (id \"test\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Check my public social data\") (id \"me360\") (author \"admin@example.com\") (updated \"Apr 28, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"nearme\") (id \"nearme\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"nearme2\") (id \"nearme2\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Our photos of an event\") (id \"ourphotos\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"My online store\") (id \"shoppingcart\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Slice\") (id \"slice\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test animation components\") (id \"testanimation\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test database components\") (id \"testdb\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test event components\") (id \"testevents\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test HTTP components\") (id \"testhttp\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test logic components\") (id \"testlogic\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test search components\") (id \"testsearch\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test HTTP components\") (id \"testhttp\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test social components\") (id \"testsocial\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test text processing components\") (id \"testtext\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test URL components\") (id \"testurl\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test values and lists\") (id \"testvalues\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test widgets\") (id \"testwidgets\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test more widgets\") (id \"testwidgets2\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test HTML generator components\") (id \"testwidgets3\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"jsdtest\") (id \"jsdtest\") (author \"jsdelfino\") (updated \"Jul 27, 2012\") (content (stats (description \"Test app\")))) (entry (title \"SMS send service\") (id \"twsms\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"boao\") (id \"boao\") (author \"jsdelfino\") (updated \"2012-10-12T12:06:59+00:00\") (content (stats (description \"Sample app\")))) (entry (title \"An empty test app\") (id \"test\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"An empty app template\") (id \"new\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"nearme\") (id \"nearme\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"nearme2\") (id \"nearme2\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Our photos of an event\") (id \"ourphotos\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"My online store\") (id \"shoppingcart\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Slice\") (id \"slice\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test animation components\") (id \"testanimation\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test database components\") (id \"testdb\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test event components\") (id \"testevents\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test social components\") (id \"testsocial\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test HTTP components\") (id \"testhttp\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test logic components\") (id \"testlogic\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test search components\") (id \"testsearch\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test HTTP components\") (id \"testhttp\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test text processing components\") (id \"testtext\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test URL components\") (id \"testurl\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test values and lists\") (id \"testvalues\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test widgets\") (id \"testwidgets\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test more widgets\") (id \"testwidgets2\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Test HTML generator components\") (id \"testwidgets3\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"test9\") (id \"test9\") (author \"jsdelfino\") (updated \"2012-09-25T06:17:18+00:00\") (content (stats (description \"Sample app\")))) (entry (title \"SMS send service\") (id \"twsms\") (author \"admin@example.com\") (updated \"Jan 01, 2012\") (content (stats (description \"Sample app\")))) (entry (title \"Check my public social data\") (id \"me360\") (author \"admin@example.com\") (updated \"Apr 28, 2012\") (content (stats (description \"Sample app\"))))))"; + + +} +} + diff --git a/sca-cpp/trunk/modules/scheme/test.scm b/sca-cpp/trunk/modules/scheme/test.scm new file mode 100644 index 0000000000..4bbff6e5c2 --- /dev/null +++ b/sca-cpp/trunk/modules/scheme/test.scm @@ -0,0 +1,44 @@ +; 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. + +; JSON-RPC test case + +(define (echo x) x) + +; ATOMPub test case + +(define (get id) + (if (nul id) + '((feed (title "Sample Feed") (id "123456789") (entry + (((title "Item") (id "111") (content (item (name "Apple") (currencyCode "USD") (currencySymbol "$") (price 2.99)))) + ((title "Item") (id "222") (content (item (name "Orange") (currencyCode "USD") (currencySymbol "$") (price 3.55)))) + ((title "Item") (id "333") (content (item (name "Pear") (currencyCode "USD") (currencySymbol "$") (price 1.55)))))))) + + (list (list 'entry '(title "Item") (list 'id (car id)) '(content (item (name "Apple") (currencyCode "USD") (currencySymbol "$") (price 2.99)))))) +) + +(define (post collection item) + '("123456789") +) + +(define (put id item) + true +) + +(define (delete id) + true +) diff --git a/sca-cpp/trunk/modules/scheme/value-element.cpp b/sca-cpp/trunk/modules/scheme/value-element.cpp index a4acdaf2d7..af5eac9b57 100644 --- a/sca-cpp/trunk/modules/scheme/value-element.cpp +++ b/sca-cpp/trunk/modules/scheme/value-element.cpp @@ -31,9 +31,9 @@ namespace tuscany { namespace scheme { -int valueElement() { - const value v = valuesToElements(readValue(cin)); - cout << writeValue(v); +const int valueElement() { + const value v = valuesToElements(content(readValue(cin))); + write(content(writeValue(v)), cout); return 0; } diff --git a/sca-cpp/trunk/modules/scheme/value-xml.cpp b/sca-cpp/trunk/modules/scheme/value-xml.cpp deleted file mode 100644 index ff785899c6..0000000000 --- a/sca-cpp/trunk/modules/scheme/value-xml.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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$ */ - -/** - * Convert a scheme value to an XML document. - */ - -#include "fstream.hpp" -#include "string.hpp" -#include "xml.hpp" -#include "element.hpp" -#include "eval.hpp" - -namespace tuscany { -namespace scheme { - -int valueXML() { - failable > s = writeXML(readValue(cin)); - if (!hasContent(s)) { - cerr << reason(s) << " : " << rcode(s); - return 1; - } - write(content(s), cout); - return 0; -} - -} -} - -int main() { - return tuscany::scheme::valueXML(); -} - diff --git a/sca-cpp/trunk/modules/scheme/xml-element.cpp b/sca-cpp/trunk/modules/scheme/xml-element.cpp new file mode 100644 index 0000000000..2050004895 --- /dev/null +++ b/sca-cpp/trunk/modules/scheme/xml-element.cpp @@ -0,0 +1,47 @@ +/* + * 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$ */ + +/** + * Convert an XML document to a scheme value. + */ + +#include "fstream.hpp" +#include "string.hpp" +#include "element.hpp" +#include "../xml/xml.hpp" +#include "eval.hpp" + +namespace tuscany { +namespace scheme { + +const int xmlValue() { + const value v = content(xml::readElements(streamList(cin))); + write(content(writeValue(v)), cout); + return 0; +} + +} +} + +int main() { + return tuscany::scheme::xmlValue(); +} + diff --git a/sca-cpp/trunk/modules/scheme/xml-value.cpp b/sca-cpp/trunk/modules/scheme/xml-value.cpp deleted file mode 100644 index d88f754aa5..0000000000 --- a/sca-cpp/trunk/modules/scheme/xml-value.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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$ */ - -/** - * Convert an XML document to a scheme value. - */ - -#include "fstream.hpp" -#include "string.hpp" -#include "xml.hpp" -#include "element.hpp" -#include "eval.hpp" - -namespace tuscany { -namespace scheme { - -int xmlValue() { - const value v = readXML(streamList(cin)); - cout << writeValue(v); - return 0; -} - -} -} - -int main() { - return tuscany::scheme::xmlValue(); -} - -- cgit v1.2.3