package org.simantics.scl.compiler.environment;
-import gnu.trove.procedure.TObjectProcedure;
-
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import org.simantics.scl.compiler.common.names.Name;
+import org.simantics.scl.compiler.compilation.CompilationContext;
import org.simantics.scl.compiler.elaboration.contexts.TypeTranslationContext;
import org.simantics.scl.compiler.elaboration.modules.SCLValue;
-import org.simantics.scl.compiler.elaboration.modules.TypeAlias;
import org.simantics.scl.compiler.elaboration.modules.TypeClass;
-import org.simantics.scl.compiler.elaboration.modules.TypeConstructor;
+import org.simantics.scl.compiler.elaboration.modules.TypeDescriptor;
import org.simantics.scl.compiler.elaboration.relations.SCLEntityType;
import org.simantics.scl.compiler.elaboration.relations.SCLRelation;
import org.simantics.scl.compiler.elaboration.rules.MappingRelation;
import org.simantics.scl.compiler.elaboration.rules.TransformationRule;
import org.simantics.scl.compiler.environment.filter.AcceptAllNamespaceFilter;
-import org.simantics.scl.compiler.errors.ErrorLog;
import org.simantics.scl.compiler.internal.codegen.effects.EffectConstructor;
import org.simantics.scl.compiler.internal.parsing.exceptions.SCLSyntaxErrorException;
import org.simantics.scl.compiler.internal.parsing.parser.SCLParserImpl;
import org.simantics.scl.compiler.types.TCon;
import org.simantics.scl.compiler.types.Type;
+import gnu.trove.procedure.TObjectProcedure;
+
public class Environments {
/**
* Get the SCLValue object representing an SCL value defined in a given environment.
* @return A TypeConstructor instance, or null if not found.
* @throws AmbiguousNameException if the same name is found in multiple imported modules.
*/
- public static TypeConstructor getTypeConstructor(Environment environment, String localName) throws AmbiguousNameException {
- return getEnvironmentEntry(environment, localName, getTypeConstructor);
+ public static TypeDescriptor getTypeDescriptor(Environment environment, String localName) throws AmbiguousNameException {
+ return getEnvironmentEntry(environment, localName, getTypeDescriptor);
}
/**
public static TypeClass getTypeClass(Environment environment, String localName) throws AmbiguousNameException {
return getEnvironmentEntry(environment, localName, getTypeClass);
}
-
- /**
- * Get the TypeAlias object representing a type alias defined in a given environment.
- * The name can be a local name or a fully scoped name with modules separated by periods.
- * @param environment the environment
- * @param localName the name to be searched for
- * @return A TypeAlias instance, or null if not found.
- * @throws AmbiguousNameException if the same name is found in multiple imported modules.
- */
- public static TypeAlias getTypeAlias(Environment environment, String localName) throws AmbiguousNameException {
- return getEnvironmentEntry(environment, localName, getTypeAlias);
- }
/**
* Get the Name object representing an SCL value defined in a given environment.
* @return A TCon instance, or null if not found.
* @throws AmbiguousNameException if the same name is used in multiple imported modules.
*/
- public static TCon getTypeConstructorName(Environment environment, String localName) throws AmbiguousNameException {
- TypeConstructor typeConstructor = getTypeConstructor(environment, localName);
- if(typeConstructor == null)
+ public static TCon getTypeDescriptorName(Environment environment, String localName) throws AmbiguousNameException {
+ TypeDescriptor typeDescriptor = getTypeDescriptor(environment, localName);
+ if(typeDescriptor == null)
return null;
else
- return typeConstructor.name;
+ return typeDescriptor.name;
}
/**
*/
public static Type getType(Environment environment, String typeText) throws SCLExpressionCompilationException {
SCLParserImpl parser = new SCLParserImpl(new StringReader(typeText));
- ErrorLog errorLog = new ErrorLog();
+ CompilationContext compilationContext = new CompilationContext();
+ compilationContext.environment = environment;
try {
TypeAst typeAst = (TypeAst)parser.parseType();
- TypeTranslationContext context = new TypeTranslationContext(errorLog, environment);
+ TypeTranslationContext context = new TypeTranslationContext(compilationContext);
Type type = context.toType(typeAst);
- if(errorLog.isEmpty())
+ if(compilationContext.errorLog.isEmpty())
return type;
} catch(SCLSyntaxErrorException e) {
- errorLog.log(e.location, e.getMessage());
+ compilationContext.errorLog.log(e.location, e.getMessage());
} catch(Exception e) {
- errorLog.log(e);
+ compilationContext.errorLog.log(e);
}
- throw new SCLExpressionCompilationException(errorLog.getErrors());
+ throw new SCLExpressionCompilationException(compilationContext.errorLog.getErrors());
}
/**
}
};
- private static final NamespaceValueAccessor<TypeConstructor> getTypeConstructor = new NamespaceValueAccessor<TypeConstructor>() {
+ private static final NamespaceValueAccessor<TypeDescriptor> getTypeDescriptor = new NamespaceValueAccessor<TypeDescriptor>() {
@Override
- public TypeConstructor get(Namespace ns, String name) throws AmbiguousNameException {
- return ns.getTypeConstructor(name);
+ public TypeDescriptor get(Namespace ns, String name) throws AmbiguousNameException {
+ return ns.getTypeDescriptor(name);
}
};
}
};
- private static final NamespaceValueAccessor<TypeAlias> getTypeAlias = new NamespaceValueAccessor<TypeAlias>() {
- @Override
- public TypeAlias get(Namespace ns, String name) throws AmbiguousNameException {
- return ns.getTypeAlias(name);
- }
- };
-
private static <T> T getEnvironmentEntry(Environment environment, String localName, NamespaceValueAccessor<T> accessor) throws AmbiguousNameException {
Namespace namespace = environment.getLocalNamespace();
int curPos = 0;
int pos = localName.indexOf('.', curPos);
if(pos < 0)
return accessor.get(namespace, localName.substring(curPos));
- namespace = namespace.getNamespace(localName.substring(curPos, pos));
- if(namespace == null)
- return null;
+ Namespace newNamespace = namespace.getNamespace(localName.substring(curPos, pos));
+ if(newNamespace == null)
+ return accessor.get(namespace, localName.substring(curPos));
+ namespace = newNamespace;
curPos = pos + 1;
}
}