diff --git a/build.gradle b/build.gradle index 2f7819c..725f3da 100644 --- a/build.gradle +++ b/build.gradle @@ -12,55 +12,49 @@ allprojects { } } - repositories { - mavenCentral() - } + repositories { mavenCentral() } dependencies { compile 'org.slf4j:slf4j-simple:1.7.5' testCompile 'junit:junit:4.11' } - test { - systemProperties 'org.slf4j.simpleLogger.defaultLogLevel': 'debug' - } + test { systemProperties 'org.slf4j.simpleLogger.defaultLogLevel': 'debug' } } project(':common'){ sourceSets { main { - java { - srcDir 'interfaces/src' - } + java { srcDir 'interfaces/src' } } } } project(':javabite-compiler') { + apply plugin:'application' + + jar { + manifest { + attributes 'Main-Class': 'swp_compiler_ss13.javabite.compiler.JavabiteCompiler' + } + } + + mainClassName = 'swp_compiler_ss13.javabite.compiler.JavabiteCompiler' + dependencies { compile project(':javabite-common'), project(':javabite-lexer'), project(':javabite-parser'), project(':javabite-code-gen'), project(':javabite-backend') } } project(':javabite-lexer') { - dependencies { - compile project(':javabite-common') - } + dependencies { compile project(':javabite-common') } } project(':javabite-parser') { - dependencies { - compile project(':javabite-common') - } + dependencies { compile project(':javabite-common') } } project(':javabite-code-gen') { - dependencies { - compile project(':javabite-common') - } + dependencies { compile project(':javabite-common') } } project(':javabite-backend') { - dependencies { - compile project(':javabite-common') - } + dependencies { compile project(':javabite-common') } } project(':javabite-common') { - dependencies { - compile project(':common') - } + dependencies { compile project(':common') } } \ No newline at end of file diff --git a/javabite-compiler/src/main/java/swp_compiler_ss13/javabite/compiler/JavabiteCompiler.java b/javabite-compiler/src/main/java/swp_compiler_ss13/javabite/compiler/JavabiteCompiler.java new file mode 100644 index 0000000..30856e5 --- /dev/null +++ b/javabite-compiler/src/main/java/swp_compiler_ss13/javabite/compiler/JavabiteCompiler.java @@ -0,0 +1,70 @@ +package swp_compiler_ss13.javabite.compiler; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import swp_compiler_ss13.common.backend.Backend; +import swp_compiler_ss13.common.ir.IntermediateCodeGenerator; +import swp_compiler_ss13.common.lexer.Lexer; +import swp_compiler_ss13.common.parser.Parser; +import swp_compiler_ss13.javabite.compiler.util.ModuleProvider; + +/** + * main class for the JavaBite-compiler + * + * @author flofreud + * + */ +public class JavabiteCompiler { + final static Logger log = LoggerFactory.getLogger(JavabiteCompiler.class); + + Lexer lexer = null; + Parser parser = null; + IntermediateCodeGenerator codegen = null; + Backend backend = null; + + public JavabiteCompiler() { + lexer = ModuleProvider.getLexerInstance(); + parser = ModuleProvider.getParserInstance(); + codegen = ModuleProvider.getCodeGeneratorInstance(); + backend = ModuleProvider.getBackendInstance(); + } + + /** + * @return check if all modules could be loaded + */ + public boolean checkSetup() { + boolean setupOk = true; + + if (lexer == null) { + setupOk = false; + log.error("No lexer module loaded..."); + } + if (parser == null) { + setupOk = false; + log.error("No parser module loaded..."); + } + if (codegen == null) { + setupOk = false; + log.error("No code generation module loaded..."); + } + if (backend == null) { + setupOk = false; + log.error("No backend module loaded..."); + } + + return setupOk; + } + + + public static void main(String[] args) { + System.out.println("Javabite-Compiler Basic Console"); + JavabiteCompiler compiler = new JavabiteCompiler(); + if (compiler.checkSetup()) { + System.out.println("Compiler is ready to start"); + } else { + System.out.println("Compiler could not load all need modules"); + } + + } +} diff --git a/javabite-compiler/src/main/java/swp_compiler_ss13/javabite/compiler/util/ModuleProvider.java b/javabite-compiler/src/main/java/swp_compiler_ss13/javabite/compiler/util/ModuleProvider.java new file mode 100644 index 0000000..fd71215 --- /dev/null +++ b/javabite-compiler/src/main/java/swp_compiler_ss13/javabite/compiler/util/ModuleProvider.java @@ -0,0 +1,102 @@ +package swp_compiler_ss13.javabite.compiler.util; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.ServiceConfigurationError; +import java.util.ServiceLoader; + +import swp_compiler_ss13.common.backend.Backend; +import swp_compiler_ss13.common.ir.IntermediateCodeGenerator; +import swp_compiler_ss13.common.lexer.Lexer; +import swp_compiler_ss13.common.parser.Parser; + +/** + * The module provider gives access to the module implementation which is + * configured by the ServiceLoader-API. + * + * If the configuration of the ServiceLoader-API is not correct, a + * {@link ServiceConfigurationError} may be thrown. + * + * @author flofreud + * + */ +public class ModuleProvider { + + /** + * @return a available Lexer implementation instance + * @throws ServiceConfigurationError + */ + public static Lexer getLexerInstance() { + return getModuleInstance(Lexer.class); + } + + /** + * @return a collection of all available Lexer implementation instance + * @throws ServiceConfigurationError + */ + public static Collection getAllLexerInstance() { + return getAllModuleInstances(Lexer.class); + } + + /** + * @return a available Parser implementation instance + * @throws ServiceConfigurationError + */ + public static Parser getParserInstance() { + return getModuleInstance(Parser.class); + } + + /** + * @return a collection of all available Parser implementation instance + * @throws ServiceConfigurationError + */ + public static Collection getAllParserInstance() { + return getAllModuleInstances(Parser.class); + } + + /** + * @return a available IntermediateCodeGenerator implementation instance + * @throws ServiceConfigurationError + */ + public static IntermediateCodeGenerator getCodeGeneratorInstance() { + return getModuleInstance(IntermediateCodeGenerator.class); + } + + /** + * @return a collection of all available IntermediateCodeGenerator implementation instance + * @throws ServiceConfigurationError + */ + public static Collection getCodeAllGeneratorInstance() { + return getAllModuleInstances(IntermediateCodeGenerator.class); + } + + /** + * @return a available Backend implementation instance + * @throws ServiceConfigurationError + */ + public static Backend getBackendInstance() { + return getModuleInstance(Backend.class); + } + + /** + * @return a collection of all available Backend implementation instance + * @throws ServiceConfigurationError + */ + public static Collection getAllBackendInstance() { + return getAllModuleInstances(Backend.class); + } + + private static T getModuleInstance(Class clazz) { + Iterator iterator = ServiceLoader.load(clazz).iterator(); + return iterator.hasNext() ? iterator.next() : null; + } + + private static Collection getAllModuleInstances( + Class clazz) { + Collection result = new ArrayList(); + for (T e : ServiceLoader.load(clazz)) + result.add(e); + return result; + } +} diff --git a/javabite-compiler/src/main/resources/META-INF/services/swp_compiler_ss13.common.backend.Backend b/javabite-compiler/src/main/resources/META-INF/services/swp_compiler_ss13.common.backend.Backend new file mode 100644 index 0000000..e69de29 diff --git a/javabite-compiler/src/main/resources/META-INF/services/swp_compiler_ss13.common.ir.IntermediateCodeGenerator b/javabite-compiler/src/main/resources/META-INF/services/swp_compiler_ss13.common.ir.IntermediateCodeGenerator new file mode 100644 index 0000000..2013945 --- /dev/null +++ b/javabite-compiler/src/main/resources/META-INF/services/swp_compiler_ss13.common.ir.IntermediateCodeGenerator @@ -0,0 +1 @@ +swp_compiler_ss13.javabite.codegen.JavaBiteCodeGenerator \ No newline at end of file diff --git a/javabite-compiler/src/main/resources/META-INF/services/swp_compiler_ss13.common.lexer.Lexer b/javabite-compiler/src/main/resources/META-INF/services/swp_compiler_ss13.common.lexer.Lexer new file mode 100644 index 0000000..e69de29 diff --git a/javabite-compiler/src/main/resources/META-INF/services/swp_compiler_ss13.common.parser.Parser b/javabite-compiler/src/main/resources/META-INF/services/swp_compiler_ss13.common.parser.Parser new file mode 100644 index 0000000..6012eee --- /dev/null +++ b/javabite-compiler/src/main/resources/META-INF/services/swp_compiler_ss13.common.parser.Parser @@ -0,0 +1 @@ +swp_compiler_ss13.javabite.parser.JavabiteParser \ No newline at end of file