| 1 | public class GnappoProduct { |
| 2 | |
| 3 | private static class SchemeExpressionPower implements SchemeExpression { |
| 4 | private SchemeExpression exp1; |
| 5 | private SchemeExpression exp2; |
| 6 | public SchemeExpressionPower(SchemeExpression expr1, SchemeExpression expr2) { |
| 7 | exp1=expr1; |
| 8 | exp2=expr2; |
| 9 | } |
| 10 | public SchemeValue evaluate(SchemeEnvironment env) throws SchemeException { |
| 11 | return SchemeFactoryManager.instance().create("SchemeExpressionInt", |
| 12 | ((int)(Math.pow(exp1.evaluate(env).asInt(),exp2.evaluate(env).asInt())))).evaluate(env); |
| 13 | } |
| 14 | public void prettyPrint(PrintStream out) { |
| 15 | out.print("(pow "); |
| 16 | exp1.prettyPrint(out); |
| 17 | out.print(" "); |
| 18 | exp2.prettyPrint(out); |
| 19 | out.print(")"); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * @param args |
| 25 | * @throws SchemeException |
| 26 | */ |
| 27 | public static void main(String[] args) throws SchemeException { |
| 28 | // TODO Auto-generated method stub |
| 29 | SchemeFactoryManager fm = SchemeFactoryManager.instance(); |
| 30 | fm.put("pow", new SchemeFactory() { |
| 31 | public SchemeExpression create(Object... args) { |
| 32 | return new SchemeExpressionPower((SchemeExpression)args[0],(SchemeExpression)args[1]); |
| 33 | } |
| 34 | }); |
| 35 | SchemeExpression power = fm.create("pow",fm.create("SchemeExpressionInt",2),fm.create("SchemeExpressionInt",4)); |
| 36 | power.prettyPrint(System.out); |
| 37 | System.out.println(); |
| 38 | System.out.print(power.evaluate(null)); |
| 39 | |
| 40 | } |
| 41 | |
| 42 | |
| 43 | |
| 44 | } |