From 7444d4b3a2f3c25fac462d8a168898656dafd52e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Hannu=20Niemist=C3=B6?= Date: Wed, 27 Dec 2017 14:12:55 +0200 Subject: [PATCH] Compilation of SCL expressions from SCL refs #7695 Change-Id: I053be9e6d4197ad563a31f09e21c6bea89c3e5ed --- .../scl/SCL/CallHierarchy.scl | 2 +- .../scl/SCL/Common.scl | 27 ++++++++ .../scl/SCL/Environment.scl | 19 ++++++ .../scl/SCL/Expressions.scl | 31 +++++++++ .../scl/SCL/Module.scl | 9 +++ .../scl/SCL/ModuleRepository.scl | 45 ++++++++++++ .../scl/SCL/Reflection.scl | 68 +------------------ ...ava => CurrentModuleRepositoryModule.java} | 8 +-- .../EnvironmentSpecification.java | 9 +++ .../module/repository/ModuleRepository.java | 4 ++ .../BuiltinModuleSourceRepository.java | 4 +- 11 files changed, 153 insertions(+), 73 deletions(-) create mode 100644 bundles/org.simantics.scl.compiler/scl/SCL/Common.scl create mode 100644 bundles/org.simantics.scl.compiler/scl/SCL/Environment.scl create mode 100644 bundles/org.simantics.scl.compiler/scl/SCL/Expressions.scl create mode 100644 bundles/org.simantics.scl.compiler/scl/SCL/Module.scl create mode 100644 bundles/org.simantics.scl.compiler/scl/SCL/ModuleRepository.scl rename bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/{ReflectionJavaModule.java => CurrentModuleRepositoryModule.java} (89%) diff --git a/bundles/org.simantics.scl.compiler/scl/SCL/CallHierarchy.scl b/bundles/org.simantics.scl.compiler/scl/SCL/CallHierarchy.scl index 4e823970e..99ff5d18d 100644 --- a/bundles/org.simantics.scl.compiler/scl/SCL/CallHierarchy.scl +++ b/bundles/org.simantics.scl.compiler/scl/SCL/CallHierarchy.scl @@ -3,7 +3,7 @@ module { export = [whoCalls, unusedDefinitions] } -import "SCL/Reflection" +import "SCL/ModuleRepository" @JavaType "org.simantics.scl.compiler.module.debug.SymbolReference" data SymbolReference = diff --git a/bundles/org.simantics.scl.compiler/scl/SCL/Common.scl b/bundles/org.simantics.scl.compiler/scl/SCL/Common.scl new file mode 100644 index 000000000..9fe13d115 --- /dev/null +++ b/bundles/org.simantics.scl.compiler/scl/SCL/Common.scl @@ -0,0 +1,27 @@ +module { + export = [moduleOfName, nameOfName, createName] +} + +// Names + +importJava "org.simantics.scl.compiler.common.names.Name" where + data Name + + @JavaName module + moduleOfName_ :: Name -> String + @JavaName name + nameOfName_ :: Name -> String + + @JavaName create + createName_ :: String -> String -> Name + +instance Show Name where + sb <+ n = sb << moduleOfName n << "/" << nameOfName n + +moduleOfName = moduleOfName_ +nameOfName = nameOfName_ +createName = createName_ + +// Locations + +type Location = Long \ No newline at end of file diff --git a/bundles/org.simantics.scl.compiler/scl/SCL/Environment.scl b/bundles/org.simantics.scl.compiler/scl/SCL/Environment.scl new file mode 100644 index 000000000..b8306ad4e --- /dev/null +++ b/bundles/org.simantics.scl.compiler/scl/SCL/Environment.scl @@ -0,0 +1,19 @@ +include "./ModuleRepository" + +importJava "org.simantics.scl.compiler.environment.Environment" where + data Environment + +importJava "org.simantics.scl.compiler.runtime.RuntimeEnvironment" where + data RuntimeEnvironment + +importJava "org.simantics.scl.compiler.environment.specification.EnvironmentSpecification" where + data EnvironmentSpecification + + fromList :: [(String,String)] -> EnvironmentSpecification + +importJava "org.simantics.scl.compiler.module.repository.ModuleRepository" where + @JavaName createRuntimeEnvironment + createRuntimeEnvironment_ :: ModuleRepository -> EnvironmentSpecification -> RuntimeEnvironment + +createRuntimeEnvironment :: [(String,String)] -> RuntimeEnvironment +createRuntimeEnvironment = createRuntimeEnvironment_ MODULE_REPOSITORY . fromList \ No newline at end of file diff --git a/bundles/org.simantics.scl.compiler/scl/SCL/Expressions.scl b/bundles/org.simantics.scl.compiler/scl/SCL/Expressions.scl new file mode 100644 index 000000000..6bb857102 --- /dev/null +++ b/bundles/org.simantics.scl.compiler/scl/SCL/Expressions.scl @@ -0,0 +1,31 @@ +include "./Environment" + +importJava "org.simantics.scl.compiler.top.ExpressionEvaluator" where + data ExpressionEvaluator + + @JavaName "" + createExpressionEvaluator :: RuntimeEnvironment -> String -> ExpressionEvaluator + + eval :: ExpressionEvaluator -> a + + expectedType :: ExpressionEvaluator -> Type -> ExpressionEvaluator + +importJava "org.simantics.scl.compiler.top.SCLExpressionCompilationException" where + data SCLExpressionCompilationException + +data EvaluationModifier + +applyModifier evaluator _ = () + +evaluateExpression :: Typeable a => RuntimeEnvironment -> [EvaluationModifier] -> String -> a +evaluateExpression runtimeEnvironment modifiers expression = do + evaluator = createExpressionEvaluator runtimeEnvironment expression + expectedType evaluator (typeOfProxy (TypeProxy :: TypeProxy a)) + iter (applyModifier evaluator) modifiers + eval evaluator + +untypedEvaluateExpression :: RuntimeEnvironment -> [EvaluationModifier] -> String -> a +untypedEvaluateExpression runtimeEnvironment modifiers expression = do + evaluator = createExpressionEvaluator runtimeEnvironment expression + iter (applyModifier evaluator) modifiers + eval evaluator \ No newline at end of file diff --git a/bundles/org.simantics.scl.compiler/scl/SCL/Module.scl b/bundles/org.simantics.scl.compiler/scl/SCL/Module.scl new file mode 100644 index 000000000..22efc682b --- /dev/null +++ b/bundles/org.simantics.scl.compiler/scl/SCL/Module.scl @@ -0,0 +1,9 @@ +include "./Common" + +importJava "org.simantics.scl.compiler.module.Module" where + data Module + + @JavaName getValueNames + valueNamesOf_ :: Module -> [String] + +valueNamesOf = valueNamesOf_ \ No newline at end of file diff --git a/bundles/org.simantics.scl.compiler/scl/SCL/ModuleRepository.scl b/bundles/org.simantics.scl.compiler/scl/SCL/ModuleRepository.scl new file mode 100644 index 000000000..eceb418be --- /dev/null +++ b/bundles/org.simantics.scl.compiler/scl/SCL/ModuleRepository.scl @@ -0,0 +1,45 @@ +module { + export = [possibleUnsafeSclValueByName, unsafeSclValueByName, sclModuleNames, moduleByName] +} + +include "./CurrentModuleRepository" +include "./Module" + +importJava "org.simantics.scl.compiler.module.repository.ModuleRepository" where + // data ModuleRepository + // defined in CurrentModuleRepository + + @JavaName getValue + unsafeSclValueByName_ :: ModuleRepository -> String -> a + + @JavaName getSourceRepository + moduleSourceRepositoryOf :: ModuleRepository -> ModuleSourceRepository + + @JavaName getModule + moduleByName_ :: ModuleRepository -> String -> Failable Module + +importJava "org.simantics.scl.compiler.source.repository.ModuleSourceRepository" where + data ModuleSourceRepository + + @JavaName getModuleNames + sclModuleNames_ :: ModuleSourceRepository -> [String] + +importJava "org.simantics.scl.compiler.errors.Failable" where + data Failable a + + didSucceed :: Failable a -> Boolean + getResult :: Failable a -> Maybe a + +unsafeSclValueByName :: String -> a +unsafeSclValueByName = unsafeSclValueByName_ MODULE_REPOSITORY + +possibleUnsafeSclValueByName :: String -> Maybe a +possibleUnsafeSclValueByName name = Just (unsafeSclValueByName name) `catch` \(_ :: Exception) -> Nothing + +sclModuleNames :: [String] +sclModuleNames = sclModuleNames_ (moduleSourceRepositoryOf MODULE_REPOSITORY) + +moduleByName :: String -> Maybe Module +moduleByName name = getResult failable + where + failable = moduleByName_ MODULE_REPOSITORY name \ No newline at end of file diff --git a/bundles/org.simantics.scl.compiler/scl/SCL/Reflection.scl b/bundles/org.simantics.scl.compiler/scl/SCL/Reflection.scl index 6b2fd0bcd..3e8aeb97b 100644 --- a/bundles/org.simantics.scl.compiler/scl/SCL/Reflection.scl +++ b/bundles/org.simantics.scl.compiler/scl/SCL/Reflection.scl @@ -1,69 +1,5 @@ module { - export = [possibleUnsafeSclValueByName, unsafeSclValueByName, sclModuleNames, moduleByName, - moduleOfName, nameOfName, createName, valueNamesOf] + deprecated = "Use more specialized modules like ModuleRepository" } -include "SCL/ReflectionJava" - -importJava "org.simantics.scl.compiler.module.repository.ModuleRepository" where - @JavaName getValue - unsafeSclValueByName_ :: ModuleRepository -> String -> a - - @JavaName getSourceRepository - moduleSourceRepositoryOf :: ModuleRepository -> ModuleSourceRepository - - @JavaName getModule - moduleByName_ :: ModuleRepository -> String -> Failable Module - -importJava "org.simantics.scl.compiler.source.repository.ModuleSourceRepository" where - data ModuleSourceRepository - - @JavaName getModuleNames - sclModuleNames_ :: ModuleSourceRepository -> [String] - -importJava "org.simantics.scl.compiler.errors.Failable" where - data Failable a - - didSucceed :: Failable a -> Boolean - getResult :: Failable a -> Maybe a - -importJava "org.simantics.scl.compiler.module.Module" where - data Module - - @JavaName getValueNames - valueNamesOf_ :: Module -> [String] - -importJava "org.simantics.scl.compiler.common.names.Name" where - data Name - - @JavaName module - moduleOfName_ :: Name -> String - @JavaName name - nameOfName_ :: Name -> String - - @JavaName create - createName_ :: String -> String -> Name - -instance Show Name where - sb <+ n = sb << moduleOfName n << "/" << nameOfName n - -moduleOfName = moduleOfName_ -nameOfName = nameOfName_ -createName = createName_ -valueNamesOf = valueNamesOf_ - -type Location = Long - -unsafeSclValueByName :: String -> a -unsafeSclValueByName = unsafeSclValueByName_ MODULE_REPOSITORY - -possibleUnsafeSclValueByName :: String -> Maybe a -possibleUnsafeSclValueByName name = Just (unsafeSclValueByName name) `catch` \(_ :: Exception) -> Nothing - -sclModuleNames :: [String] -sclModuleNames = sclModuleNames_ (moduleSourceRepositoryOf MODULE_REPOSITORY) - -moduleByName :: String -> Maybe Module -moduleByName name = getResult failable - where - failable = moduleByName_ MODULE_REPOSITORY name \ No newline at end of file +include "./ModuleRepository" \ No newline at end of file diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/ReflectionJavaModule.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/CurrentModuleRepositoryModule.java similarity index 89% rename from bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/ReflectionJavaModule.java rename to bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/CurrentModuleRepositoryModule.java index 1ccc4af4d..c93e18a12 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/ReflectionJavaModule.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/CurrentModuleRepositoryModule.java @@ -17,12 +17,12 @@ import org.simantics.scl.compiler.types.Type; import org.simantics.scl.compiler.types.Types; import org.simantics.scl.compiler.types.kinds.Kinds; -public class ReflectionJavaModule extends ConcreteModule { +public class CurrentModuleRepositoryModule extends ConcreteModule { - public static ReflectionJavaModule INSTANCE = new ReflectionJavaModule(); + public static CurrentModuleRepositoryModule INSTANCE = new CurrentModuleRepositoryModule(); - public ReflectionJavaModule() { - super("SCL/ReflectionJava"); + public CurrentModuleRepositoryModule() { + super("SCL/CurrentModuleRepository"); // ModuleRepository type TCon ModuleRepository = Types.con(getName(), "ModuleRepository"); diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/environment/specification/EnvironmentSpecification.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/environment/specification/EnvironmentSpecification.java index c6dc8db4e..d247685f5 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/environment/specification/EnvironmentSpecification.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/environment/specification/EnvironmentSpecification.java @@ -1,8 +1,10 @@ package org.simantics.scl.compiler.environment.specification; import java.util.ArrayList; +import java.util.List; import org.simantics.scl.compiler.module.ImportDeclaration; +import org.simantics.scl.runtime.tuple.Tuple2; public class EnvironmentSpecification { public final ArrayList imports = new ArrayList(); @@ -19,6 +21,13 @@ public class EnvironmentSpecification { spec.importModule(strings[i], strings[i+1]); return spec; } + + public static EnvironmentSpecification fromList(List imports) { + EnvironmentSpecification spec = new EnvironmentSpecification(); + for(Tuple2 tuple : imports) + spec.importModule((String)tuple.c0, (String)tuple.c1); + return spec; + } @Override public int hashCode() { diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/repository/ModuleRepository.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/repository/ModuleRepository.java index f2e4e97ae..0f0b85420 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/repository/ModuleRepository.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/repository/ModuleRepository.java @@ -369,6 +369,10 @@ public class ModuleRepository { return createRuntimeEnvironment(environmentSpecification, parentClassLoader, null); } + public RuntimeEnvironment createRuntimeEnvironment(EnvironmentSpecification environmentSpecification) throws ImportFailureException { + return createRuntimeEnvironment(environmentSpecification, getClass().getClassLoader()); + } + public RuntimeEnvironment createRuntimeEnvironment( EnvironmentSpecification environmentSpecification, ClassLoader parentClassLoader, diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/repository/BuiltinModuleSourceRepository.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/repository/BuiltinModuleSourceRepository.java index f3fd69740..22c986b5c 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/repository/BuiltinModuleSourceRepository.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/repository/BuiltinModuleSourceRepository.java @@ -4,13 +4,13 @@ import org.simantics.scl.compiler.elaboration.java.Builtins; import org.simantics.scl.compiler.elaboration.java.JavaModule; import org.simantics.scl.compiler.elaboration.java.LoggingModule; import org.simantics.scl.compiler.elaboration.java.MinigraphModule; -import org.simantics.scl.compiler.elaboration.java.ReflectionJavaModule; +import org.simantics.scl.compiler.elaboration.java.CurrentModuleRepositoryModule; public class BuiltinModuleSourceRepository extends MapModuleSourceRepository implements ModuleSourceRepository { public BuiltinModuleSourceRepository() { super(Builtins.INSTANCE, JavaModule.INSTANCE, - ReflectionJavaModule.INSTANCE, + CurrentModuleRepositoryModule.INSTANCE, MinigraphModule.INSTANCE, new LoggingModule()); } -- 2.47.1