001/* 002 * Copyright (c) 2005 Einar Pehrson <einar@pehrson.nu>. 003 * 004 * This file is part of 005 * CleanSheets - a spreadsheet application for the Java platform. 006 * 007 * CleanSheets is free software; you can redistribute it and/or modify 008 * it under the terms of the GNU General Public License as published by 009 * the Free Software Foundation; either version 2 of the License, or 010 * (at your option) any later version. 011 * 012 * CleanSheets is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU General Public License 018 * along with CleanSheets; if not, write to the Free Software 019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 020 */ 021package csheets.core.formula.compiler; 022 023import java.io.BufferedReader; 024import java.io.IOException; 025import java.io.InputStream; 026import java.io.InputStreamReader; 027import java.io.OutputStream; 028import java.io.PrintStream; 029import java.io.StringReader; 030 031import antlr.collections.AST; 032import csheets.core.Address; 033import csheets.core.Cell; 034import csheets.core.Spreadsheet; 035import csheets.core.Workbook; 036import csheets.core.formula.Expression; 037 038/** 039 * A test-class for processing formulas from an input stream. 040 * @author Einar Pehrson 041 */ 042public class Console { 043 044 /** 045 * Creates a new console for the given input stream. 046 * @param in the input stream from which formulas are read 047 * @param out the output stream to which messages are written 048 */ 049 public Console(InputStream in, OutputStream out) { 050 // Wraps the output stream 051 PrintStream printer; 052 if (out instanceof PrintStream) 053 printer = (PrintStream)out; 054 else 055 printer = new PrintStream(out); 056 057 // Fetches a cell 058 Workbook workbook = new Workbook(1); 059 Spreadsheet sheet = workbook.getSpreadsheet(0); 060 Cell cell = sheet.getCell(new Address(0,0)); 061 062 // Reads and compiles input 063 ExcelExpressionCompiler compiler = new ExcelExpressionCompiler(); 064 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 065 String line; 066 try { 067 while ((line = reader.readLine()) != null) { 068 FormulaParser parser = new FormulaParser( 069 new FormulaLexer(new StringReader(line))); 070 try { 071 parser.expression(); 072 AST ast = parser.getAST(); 073 if (ast != null) { 074 printer.println("AST: " + ast.toStringTree()); 075 // new antlr.debug.misc.ASTFrame("Formula Viewer", ast).setVisible(true); 076 Expression expression = compiler.convert(cell, ast); 077 printer.println("Formula: " + expression + " = " + expression.evaluate()); 078 } 079 } catch (Exception e) { 080 // System.err.println(e); 081 e.printStackTrace(); 082 } 083 } 084 } catch (IOException e) { 085 System.err.println(e); 086 } 087 } 088 089 /** 090 * Creates a new console for the command-line. 091 * @param args the command-line arguments (ignored) 092 */ 093 public static void main(String[] args) { 094 new Console(System.in, System.out); 095 } 096}