1 package org.simantics.scl.compiler.tests;
3 import java.util.Arrays;
5 import org.junit.Before;
7 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
8 import org.simantics.scl.compiler.elaboration.expressions.Expression;
9 import org.simantics.scl.compiler.elaboration.expressions.Variable;
10 import org.simantics.scl.compiler.environment.AbstractLocalEnvironment;
11 import org.simantics.scl.compiler.environment.Environment;
12 import org.simantics.scl.compiler.environment.LocalEnvironment;
13 import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
14 import org.simantics.scl.compiler.errors.CompilationErrorFormatter;
15 import org.simantics.scl.compiler.module.repository.ImportFailure;
16 import org.simantics.scl.compiler.module.repository.ImportFailureException;
17 import org.simantics.scl.compiler.module.repository.ModuleRepository;
18 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
19 import org.simantics.scl.compiler.source.repository.CompositeModuleSourceRepository;
20 import org.simantics.scl.compiler.source.repository.SourceRepositories;
21 import org.simantics.scl.compiler.top.ExpressionEvaluator;
22 import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
23 import org.simantics.scl.compiler.types.Type;
24 import org.simantics.scl.compiler.types.Types;
25 import org.simantics.scl.runtime.function.Function;
26 import org.simantics.scl.runtime.tuple.Tuple0;
28 import junit.framework.Assert;
30 public class TestExpressionEvaluator {
32 public static final boolean TIMING = false;
33 public static final int COUNT = 10000;
35 ModuleRepository moduleRepository;
37 RuntimeEnvironment runtimeEnvironment;
40 public void initialize() throws Exception {
41 moduleRepository = InitialRepository.getInitialRepository();
43 // Environment for compiling expressions
44 EnvironmentSpecification environmentSpecification = new EnvironmentSpecification();
45 environmentSpecification.importModule("Builtin", "");
46 environmentSpecification.importModule("Prelude", "");
49 runtimeEnvironment = moduleRepository.createRuntimeEnvironment(environmentSpecification,
50 getClass().getClassLoader());
51 } catch(ImportFailureException e) {
52 for(ImportFailure failure : e.failures)
53 System.err.println("Failed to import " + failure.moduleName);
58 private void testExpression0(String expressionText,
60 Type expectedType) throws Exception {
61 // Compiling and running expression
63 Object result = new ExpressionEvaluator(runtimeEnvironment, expressionText)
64 .expectedType(expectedType)
66 if(expectedValue != null)
67 Assert.assertEquals(expectedValue, result);
68 } catch(SCLExpressionCompilationException e) {
69 System.out.println(CompilationErrorFormatter.toString(expressionText, e.getErrors()));
74 private void testExpression(String expressionText,
76 Type expectedType) throws Exception {
78 System.out.println(expressionText);
79 long beginTime = System.nanoTime();
80 for(int i=0;i<COUNT;++i)
81 testExpression0(expressionText, expectedValue, expectedType);
82 long endTime = System.nanoTime();
83 System.out.println( " " + (endTime-beginTime)*1e-6/COUNT + " ms");
86 testExpression0(expressionText, expectedValue, expectedType);
90 public void testExpressionCompiler() throws Exception {
97 testExpression("map (\\(_,x) -> x) [(1,2),(2,3)]",
98 Arrays.asList(2.0, 3.0),
99 Types.list(Types.DOUBLE));
100 testExpression("map (\\x -> snd x) [(1,2),(2,3)]",
101 Arrays.asList(2.0, 3.0),
102 Types.list(Types.DOUBLE));
103 testExpression("let f x = x+1 in (f . f . f) 3",
107 testExpression("print \"Hello world!\"",
110 testExpression("[1,2+3,4+5]",
111 Arrays.asList(1,5,9),
112 Types.list(Types.INTEGER));
113 testExpression("let a = 5.3 in let f x = x+a in f 3",
116 testExpression("let mm x y = if x < y then x else y in mm 2 (mm 1 3)",
122 public void testLocalEnvironment() throws Exception {
123 String expressionText = "a + b";
124 LocalEnvironment localEnvironment = new AbstractLocalEnvironment() {
125 Variable[] localParameters = new Variable[] {
126 new Variable("a", Types.DOUBLE),
127 new Variable("b", Types.DOUBLE),
131 public Expression resolve(Environment environment, String localName) {
132 if(localName.equals("a"))
133 return new EVariable(localParameters[0]);
134 else if(localName.equals("b"))
135 return new EVariable(localParameters[1]);
141 protected Variable[] getContextVariables() {
142 return localParameters;
146 Object result = new ExpressionEvaluator(runtimeEnvironment, expressionText)
147 .localEnvironment(localEnvironment)
148 .expectedType(Types.DOUBLE)
151 Double.valueOf(15.0),
152 ((Function)result).apply(7.0, 8.0));
153 } catch(SCLExpressionCompilationException e) {
154 System.out.println(CompilationErrorFormatter.toString(expressionText, e.getErrors()));
160 public void testArities() throws Exception {
161 for(int arity=1;arity<50;++arity) {
163 StringBuilder b = new StringBuilder();
165 for(int i=0;i<arity;++i)
166 b.append("v" + i + " ");
168 for(int i=0;i<arity;++i) {
173 //System.out.println(b.toString());
176 Type expectedType = Types.INTEGER;
177 for(int i=0;i<arity;++i)
178 expectedType = Types.function(Types.INTEGER, expectedType);
180 Function function = (Function)new ExpressionEvaluator(runtimeEnvironment, b.toString())
181 .expectedType(expectedType)
182 .interpretIfPossible(false)
186 Object[] parameters = new Object[arity];
188 for(int i=0;i<arity;++i) {
190 parameters[i] = value;
193 Object result = function.applyArray(parameters);
194 Assert.assertEquals(sum, result);