The Pizza Compiler
The Pizza language is an extension to Java with three new features: |
Pizza Example: Enumeration
|
|
/* File lineinput.java * Original file --- Nov./Dec. 96 --- Martin Odersky * Comments/markup --- Dec. 96 --- John Maraist */ package net.sf.pizzacompiler.examples; import java.util.*; import java.io.*; /** This class implements the Enumeration interface, and * provides a nice interface to stream-based file I/O. */ class Lines implements Enumeration { /* Local storage of the data source. */ DataInputStream in; /* Initializer for the class to a given data source. */ Lines(DataInputStream in) { this.in = in; } /* Trivial method to determine whether we should continue reading * from the file. */ public boolean hasMoreElements() { return true; } /* Read in a line of text, and catch any exceptions which arise * from reading through the end of the file. */ public Object nextElement() { try { return in.readLine(); } catch (IOException ex) { throw new NoSuchElementException(); } } } /** a program to convert input lines to upper case */ class Caps { static String toUpperCase(String s) { return s.toUpperCase(); } static boolean nonEmpty(String s) { return s.length() != 0; } static void println(String s) { System.out.println(s); } public static void main(String[] argv) { Enumeration lines = new Lines(new DataInputStream(System.in)); while (lines.hasMoreElements()) { String line = (String)lines.nextElement(); if (nonEmpty(line)) { System.out.println(line.toUpperCase()); } else { break; } } } } /** a program to sum up a list of integers */ class Adder { static boolean nonEmpty(String s) { return s.length() != 0; } public static void main(String[] argv) { Enumeration lines = new Lines(new DataInputStream(System.in)); int sum = 0; while (lines.hasMoreElements()) { String line = (String)lines.nextElement(); if (nonEmpty(line)) { sum = sum + Integer.parseInt(line); } else { break; } } System.out.println(sum); } }