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