/* // $Id: //guest/julian_hyde/saffron/src/main/saffron/walden/Main.java#1 $ // Saffron preprocessor and data engine // Copyright (C) 2002 Julian Hyde <julian.hyde@mail.com> // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Library General Public // License as published by the Free Software Foundation; either // version 2 of the License, or (at your option) any later version. // // This library 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 // Library General Public License for more details. // // You should have received a copy of the GNU Library General Public // License along with this library; if not, write to the // Free Software Foundation, Inc., 59 Temple Place - Suite 330, // Boston, MA 02111-1307, USA. // // See the COPYING file located in the top-level-directory of // the archive of this library for complete text of license. // // jhyde, May 10, 2002 */ package saffron.walden; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import openjava.mop.Environment; import openjava.mop.OJClass; import openjava.ptree.ClassDeclaration; import openjava.ptree.Expression; import openjava.ptree.ParseTree; import openjava.tools.parser.ParseException; import openjava.tools.parser.Parser; import saffron.Statement; import saffron.runtime.VarDecl; import saffron.util.Util; import java.io.*; import java.util.ArrayList; /** * <code>Walden</code> is a command-line interpreter for Saffron, and dynamic * Java statements in general; <code>Main</code> is its main entry point. * * @author jhyde * @since May 10, 2002 * @version $Id: //guest/julian_hyde/saffron/src/main/saffron/walden/Main.java#1 $ **/ public class Main { public static void main(String[] args) { new Interpreter( new InputStreamReader(System.in), new OutputStreamWriter(System.out)).run(); } /** * Returns a JUnit test to test the Schedule class. This method is * automatically recognized by JUnit test harnesses. */ public static Test suite() { TestSuite suite = new TestSuite(); suite.addTestSuite(WaldonTestCase.class); return suite; } /** * JUnit test suite for {@link Main}. */ public static class WaldonTestCase extends TestCase { public WaldonTestCase(String name) { super(name); } private void assertInterpret(String input, String output) { StringWriter sw = new StringWriter(); Interpreter interpreter = new Interpreter( new StringReader(input), sw); interpreter.interactive = false; interpreter.run(); assertEquals(output, sw.toString()); } private String linesToString(String[] lines, String sep) { StringWriter sw = new StringWriter(); for (int i = 0; i < lines.length; i++) { sw.write(lines[i]); sw.write(sep); } return sw.toString(); } private void assertInterpret( String[] inputLines, String[] outputLines) { String input = linesToString(inputLines, ";" + Util.lineSeparator); String output = linesToString(outputLines, Util.lineSeparator); assertInterpret(input, output); } public void _testOne() { assertInterpret(new String[] {"1"}, new String[] {"1"}); } public void _testTwo() { assertInterpret( new String[] { "\"x\"", "null"}, new String[] { "x", "null"}); } public void _testDeclaration() { assertInterpret( new String[] { "int i = 1", "i"}, new String[] { "i: int: 1", "int: 1"}); } public void testVoid() { assertInterpret( new String[] { "System.out.println(\"foo\")"}, new String[] { "void"}); } public void testSelect() { assertInterpret( new String[] { "select from new int[] {1,2,3}"}, new String[] { "int[]: {1, 2, 3}"}); } } } class Interpreter { Reader in; Writer out; PrintWriter pw; boolean interactive = true; String prompt = "> "; Interpreter(Reader in, Writer out) { this.in = in; this.out = out; boolean flush = true; this.pw = new PrintWriter(out, flush); } void run() { try { run0(); } finally { pw.flush(); } } private void run0() { Parser parser = new Parser(in); Statement statement = new Statement(); ArrayList argumentList = new ArrayList(); while (true) { ParseTree parseTree = null; if (interactive) { pw.print(prompt); pw.flush(); } Statement.Argument[] arguments = (Statement.Argument[]) argumentList.toArray(new Statement.Argument[0]); ClassDeclaration classDecl = statement.init(arguments); Environment env = statement.getEnvironment(); try { parseTree = parser.InteractiveStatement(env); if (parseTree == null) { break; } } catch (ParseException e) { pw.println("Error: " + e); continue; } if (interactive) { pw.print("Evaluate: "); pw.flush(); pw.println(parseTree); } parseTree = statement.validate(parseTree); OJClass clazz = null; if (parseTree instanceof Expression) { Expression expression = (Expression) parseTree; clazz = Util.getType(env, expression); } Object o = statement.evaluate(classDecl,parseTree,arguments); if (interactive) { pw.print("Result [" + clazz + "]: "); pw.flush(); } if (o instanceof VarDecl[]) { VarDecl[] decls = (VarDecl[]) o; for (int i = 0; i < decls.length; i++) { VarDecl decl = decls[i]; if (i > 0) { pw.print(", "); } pw.print(decl.name); pw.print(": "); pw.print(decl.clazz.getName()); pw.print(": "); Util.println(pw, decl.value); } } else if (clazz == Util.clazzVoid) { pw.println("void"); } else { pw.print(clazz); pw.print(": "); Util.println(pw, o); } if (o instanceof VarDecl[]) { VarDecl[] decls = (VarDecl[]) o; for (int i = 0; i < decls.length; i++) { VarDecl decl = decls[i]; argumentList.add( new Statement.Argument( decl.name, decl.clazz, decl.value)); } } } } } // End Main.java
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 1801 | Julian Hyde |
saffron: add ObjectSchema; rules can now be matched more than once; started to implement correlations in queries in from list. |