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 * (adapted from Console.java) 041 * @author João Carreira 042 * @see csheets.core.formula.compiler.Console 043 */ 044public class NumberSignConsole 045{ 046 047 /** 048 * Creates a new console for the given input stream. 049 * @param in the input stream from which formulas are read 050 * @param out the output stream to which messages are written 051 */ 052 public NumberSignConsole(InputStream in, OutputStream out) 053 { 054 // Wraps the output stream 055 PrintStream printer; 056 if (out instanceof PrintStream) 057 { 058 printer = (PrintStream)out; 059 } 060 else 061 { 062 printer = new PrintStream(out); 063 } 064 // Fetches a cell 065 Workbook workbook = new Workbook(1); 066 Spreadsheet sheet = workbook.getSpreadsheet(0); 067 Cell cell = sheet.getCell(new Address(0,0)); 068 069 // Reads and compiles input 070 NumberSignExpressionCompiler compiler = new NumberSignExpressionCompiler(); 071 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 072 String line; 073 try 074 { 075 while ((line = reader.readLine()) != null) 076 { 077 NumberSignFormulaParser parser = new NumberSignFormulaParser( 078 new NumberSignFormulaLexer(new StringReader(line))); 079 try 080 { 081 parser.expression(); 082 AST ast = parser.getAST(); 083 if (ast != null) 084 { 085 printer.println("AST: " + ast.toStringTree()); 086 // new antlr.debug.misc.ASTFrame("Formula Viewer", ast).setVisible(true); 087 Expression expression = compiler.convert(cell, ast); 088 printer.println("Formula: " + expression + " = " + expression.evaluate()); 089 } 090 } 091 catch (Exception e) 092 { 093 // System.err.println(e); 094 e.printStackTrace(); 095 } 096 } 097 } 098 catch (IOException e) 099 { 100 System.err.println(e); 101 } 102 } 103 104 /** 105 * Creates a new console for the command-line. 106 * @param args the command-line arguments (ignored) 107 */ 108 public static void main(String[] args) 109 { 110 new NumberSignConsole(System.in, System.out); 111 } 112}