1 package org.simantics.scl.compiler.elaboration.expressions;
3 import java.util.ArrayList;
6 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
7 import org.simantics.scl.compiler.elaboration.errors.NotPatternException;
8 import org.simantics.scl.compiler.elaboration.expressions.block.LetStatement;
9 import org.simantics.scl.compiler.elaboration.expressions.lhstype.FunctionDefinitionLhs;
10 import org.simantics.scl.compiler.elaboration.expressions.lhstype.LhsType;
11 import org.simantics.scl.compiler.errors.Locations;
13 import gnu.trove.map.hash.THashMap;
14 import gnu.trove.procedure.TObjectObjectProcedure;
17 * Generated mainly from EBlock
19 public class EPreLet extends ASTExpression {
21 List<LetStatement> assignments;
24 public EPreLet(List<LetStatement> assignments, Expression in) {
25 this.assignments = assignments;
30 public Expression resolve(final TranslationContext context) {
32 THashMap<String, ArrayList<LetStatement>> functionDefinitions =
33 new THashMap<String, ArrayList<LetStatement>>();
34 ArrayList<LetStatement> otherDefinitions = new ArrayList<LetStatement>();
35 final THashMap<String, Variable> localVars = new THashMap<String, Variable>();
37 for(LetStatement assign : assignments) {
38 LhsType lhsType = assign.pattern.getLhsType();
39 if(!(assign.pattern instanceof EVar) && lhsType instanceof FunctionDefinitionLhs) {
40 String name = ((FunctionDefinitionLhs)lhsType).functionName;
41 ArrayList<LetStatement> group = functionDefinitions.get(name);
43 group = new ArrayList<LetStatement>(2);
44 functionDefinitions.put(name, group);
47 localVars.put(name, context.newVariable(name));
50 otherDefinitions.add(assign);
51 assign.pattern = assign.pattern.resolveAsPattern(context);
54 } catch (NotPatternException e) {
55 context.getErrorLog().log(e.getExpression().location, "Not a pattern.");
59 final ArrayList<Assignment> as = new ArrayList<Assignment>(functionDefinitions.size() + otherDefinitions.size());
60 functionDefinitions.forEachEntry(new TObjectObjectProcedure<String, ArrayList<LetStatement>>() {
62 public boolean execute(String name, ArrayList<LetStatement> cases) {
63 as.add(new Assignment(
64 new EVariable(cases.size()==1 ? cases.get(0).pattern.location : location, localVars.get(name)),
65 context.translateCases(cases)));
69 for(LetStatement stat : otherDefinitions)
70 as.add(new Assignment(
71 stat.pattern /* already resolved above */,
72 stat.value.resolve(context)));
73 Expression inExpr = in.resolve(context);
76 ELet result = new ELet(location, as.toArray(new Assignment[as.size()]), inExpr);
77 /*System.out.println("-----------------------------------------");
78 System.out.println(this);
79 System.out.println("-----------------------------------------");
80 System.out.println(result);
81 System.out.println("-----------------------------------------");*/
86 public void setLocationDeep(long loc) {
87 if(location == Locations.NO_LOCATION) {
89 for(LetStatement assignment : assignments)
90 assignment.setLocationDeep(loc);
91 in.setLocationDeep(loc);
96 public Expression accept(ExpressionTransformer transformer) {
97 return transformer.transform(this);
101 public int getSyntacticFunctionArity() {
102 return in.getSyntacticFunctionArity();
106 public void accept(ExpressionVisitor visitor) {