1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
3 package edu.berkeley.sbp.misc;
5 import edu.berkeley.sbp.*;
6 import edu.berkeley.sbp.chr.*;
7 import edu.berkeley.sbp.misc.*;
8 import edu.berkeley.sbp.meta.*;
9 import edu.berkeley.sbp.util.*;
16 public static class Math {
18 public static @bind.as("(") Expr parenthesis(Expr e) { return e; }
19 public static class Expr implements Reflection.Show { }
21 public static @bind.as("Expr") class Numeric extends Expr {
22 public @bind.arg String numeric;
25 public static class BinOp extends Expr {
26 public @bind.arg Expr left;
27 public @bind.arg Expr right;
30 public static @bind.as("*") class Multiply extends BinOp { }
31 public static @bind.as("/") class Divide extends BinOp { }
32 public static @bind.as("+") class Add extends BinOp { }
33 public static @bind.as("-") class Subtract extends BinOp { }
37 // invoke "java -jar edu.berkeley.sbp.jar edu.berkeley.sbp.misc.Demo tests/demo.g <expr>"
38 public static void main(String[] s) throws Exception {
40 Parser metaGrammarParser = new CharParser(MetaGrammar.newInstance());
41 Tree<String> parsedGrammar = metaGrammarParser.parse(new CharInput(new FileInputStream(s[0]))).expand1();
42 Grammar.Bindings gbr = new AnnotationGrammarBindings(Math.class);
43 Union mathGrammar = Grammar.create(parsedGrammar, "Expr", gbr);
44 Parser mathParser = new CharParser(mathGrammar);
46 System.out.println("about to parse: \""+s[1]+"\"");
47 Tree tree = mathParser.parse(new CharInput(new StringReader(s[1]))).expand1();
49 // below is ugly voodoo which will go away very soon. ignore it.
50 TreeFunctor tf = (TreeFunctor)tree.head();
51 Math.Expr e = (Math.Expr)tf.invoke(tree);
52 // above is ugly voodoo which will go away very soon. ignore it.
54 System.out.println("done!");
55 System.out.println(Reflection.show(e));