Search in sources :

Example 1 with IEnvironmentGlobal

use of stanhebben.zenscript.compiler.IEnvironmentGlobal in project ZenScript by CraftTweaker.

the class MainTest method main.

public static void main(String[] args) throws IOException {
    // Create a compile environment needed for the registry
    GenericCompileEnvironment compileEnvironment = new GenericCompileEnvironment();
    // Creates a logger needed for the registry
    GenericErrorLogger logger = new GenericErrorLogger(System.out);
    // Creates the IZenRegistry, needed to store all the ZenClass' and Symbols
    GenericRegistry registry = new GenericRegistry(compileEnvironment, logger);
    // Registers A print function as a global method
    registry.registerGlobal("print", registry.getStaticFunction(GenericFunctions.class, "print", String.class));
    // Creates a IEnvironmentGlobal needed to compile the scripts
    Map<String, byte[]> classes = new HashMap<>();
    IEnvironmentGlobal environmentGlobal = registry.makeGlobalEnvironment(classes);
    // Loads the script file
    File file = new File("script.zs");
    String fileName = file.getName();
    FileReader reader = new FileReader(file);
    // Creates a ZenTokener and ZenParsedFile for the file
    ZenTokener parser = new ZenTokener(reader, registry.getCompileEnvironment(), fileName, false);
    ZenParsedFile zenParsedFile = new ZenParsedFile(fileName, fileName, parser, environmentGlobal);
    try {
        // Compiles and runs the script
        ZenModule.compileScripts(fileName, Collections.singletonList(zenParsedFile), environmentGlobal, false);
        ZenModule module = new ZenModule(classes, MainTest.class.getClassLoader());
        Runnable runnable = module.getMain();
        if (runnable != null)
            runnable.run();
    } catch (Throwable ex) {
        registry.getErrorLogger().error("Error executing: " + fileName + ": " + ex.getMessage(), ex);
    }
}
Also used : IEnvironmentGlobal(stanhebben.zenscript.compiler.IEnvironmentGlobal)

Example 2 with IEnvironmentGlobal

use of stanhebben.zenscript.compiler.IEnvironmentGlobal in project CraftTweaker by CraftTweaker.

the class BracketHandlerEntity method find.

private IZenSymbol find(IEnvironmentGlobal environment, List<Token> tokens, int startIndex, int endIndex) {
    StringBuilder valueBuilder = new StringBuilder();
    for (int i = startIndex; i < endIndex; i++) {
        Token token = tokens.get(i);
        valueBuilder.append(token.getValue());
    }
    return position -> new ExpressionCallStatic(position, environment, method, new ExpressionString(position, valueBuilder.toString()));
}
Also used : IJavaMethod(stanhebben.zenscript.type.natives.IJavaMethod) CraftTweakerAPI(crafttweaker.CraftTweakerAPI) java.util(java.util) IBracketHandler(crafttweaker.zenscript.IBracketHandler) IEnvironmentGlobal(stanhebben.zenscript.compiler.IEnvironmentGlobal) crafttweaker.annotations(crafttweaker.annotations) Token(stanhebben.zenscript.parser.Token) stanhebben.zenscript.expression(stanhebben.zenscript.expression) IZenSymbol(stanhebben.zenscript.symbols.IZenSymbol) IEntityDefinition(crafttweaker.api.entity.IEntityDefinition) Token(stanhebben.zenscript.parser.Token)

Example 3 with IEnvironmentGlobal

use of stanhebben.zenscript.compiler.IEnvironmentGlobal in project CraftTweaker by CraftTweaker.

the class BracketHandlerLiquid method find.

private IZenSymbol find(IEnvironmentGlobal environment, List<Token> tokens, int startIndex, int endIndex) {
    StringBuilder valueBuilder = new StringBuilder();
    for (int i = startIndex; i < endIndex; i++) {
        Token token = tokens.get(i);
        valueBuilder.append(token.getValue());
    }
    Fluid fluid = fluidNames.get(valueBuilder.toString());
    if (fluid != null) {
        return position -> new ExpressionCallStatic(position, environment, method, new ExpressionString(position, valueBuilder.toString()));
    }
    return null;
}
Also used : stanhebben.zenscript.type.natives(stanhebben.zenscript.type.natives) java.util(java.util) IBracketHandler(crafttweaker.zenscript.IBracketHandler) GlobalRegistry(crafttweaker.zenscript.GlobalRegistry) IEnvironmentGlobal(stanhebben.zenscript.compiler.IEnvironmentGlobal) crafttweaker.annotations(crafttweaker.annotations) Token(stanhebben.zenscript.parser.Token) net.minecraftforge.fluids(net.minecraftforge.fluids) stanhebben.zenscript.expression(stanhebben.zenscript.expression) IZenSymbol(stanhebben.zenscript.symbols.IZenSymbol) ILiquidStack(crafttweaker.api.liquid.ILiquidStack) MCLiquidStack(crafttweaker.mc1120.liquid.MCLiquidStack) Token(stanhebben.zenscript.parser.Token)

Example 4 with IEnvironmentGlobal

use of stanhebben.zenscript.compiler.IEnvironmentGlobal in project CraftTweaker by CraftTweaker.

the class BracketHandlerOre method find.

private IZenSymbol find(IEnvironmentGlobal environment, List<Token> tokens, int startIndex, int endIndex) {
    StringBuilder valueBuilder = new StringBuilder();
    for (int i = startIndex; i < endIndex; i++) {
        Token token = tokens.get(i);
        valueBuilder.append(token.getValue());
    }
    IJavaMethod method = JavaMethod.get(GlobalRegistry.getTypes(), BracketHandlerOre.class, valueBuilder.toString().contains("*") ? "getOreList" : "getOre", String.class);
    return position -> new ExpressionCallStatic(position, environment, method, new ExpressionString(position, valueBuilder.toString()));
}
Also used : stanhebben.zenscript.type.natives(stanhebben.zenscript.type.natives) java.util(java.util) IBracketHandler(crafttweaker.zenscript.IBracketHandler) Token(stanhebben.zenscript.parser.Token) IOreDictEntry(crafttweaker.api.oredict.IOreDictEntry) stanhebben.zenscript.expression(stanhebben.zenscript.expression) crafttweaker(crafttweaker) GlobalRegistry(crafttweaker.zenscript.GlobalRegistry) IEnvironmentGlobal(stanhebben.zenscript.compiler.IEnvironmentGlobal) crafttweaker.annotations(crafttweaker.annotations) MCOreDictEntry(crafttweaker.mc1120.oredict.MCOreDictEntry) Pattern(java.util.regex.Pattern) IZenSymbol(stanhebben.zenscript.symbols.IZenSymbol) Token(stanhebben.zenscript.parser.Token)

Example 5 with IEnvironmentGlobal

use of stanhebben.zenscript.compiler.IEnvironmentGlobal in project CraftTweaker by CraftTweaker.

the class CrTTweaker method loadScript.

@Override
public boolean loadScript(boolean isSyntaxCommand, String loaderName) {
    CraftTweakerAPI.logInfo("Loading scripts");
    CRT_LOADING_STARTED_EVENT_EVENT_LIST.publish(new CrTLoadingStartedEvent(loaderName, isSyntaxCommand, networkSide));
    preprocessorManager.clean();
    Set<String> executed = new HashSet<>();
    boolean loadSuccessful = true;
    List<ScriptFile> scriptFiles = collectScriptFiles(isSyntaxCommand);
    // preprocessor magic
    for (ScriptFile scriptFile : scriptFiles) {
        scriptFile.addAll(preprocessorManager.checkFileForPreprocessors(scriptFile));
    }
    scriptFiles.sort(PreprocessorManager.SCRIPT_FILE_COMPARATOR);
    Map<String, byte[]> classes = new HashMap<>();
    IEnvironmentGlobal environmentGlobal = GlobalRegistry.makeGlobalEnvironment(classes);
    // ZS magic
    long totalTime = System.currentTimeMillis();
    for (ScriptFile scriptFile : scriptFiles) {
        if (!scriptFile.getLoaderName().equals(loaderName) && !isSyntaxCommand) {
            CraftTweakerAPI.logInfo(getTweakerDescriptor(loaderName) + ": Skipping file " + scriptFile + " as we are currently loading with a different loader");
            continue;
        }
        if (!scriptFile.shouldBeLoadedOn(networkSide)) {
            CraftTweakerAPI.logInfo(getTweakerDescriptor(loaderName) + ": Skipping file " + scriptFile + " as we are on the wrong side of the Network");
            continue;
        }
        if (!executed.contains(scriptFile.getEffectiveName())) {
            // long time = System.currentTimeMillis();
            executed.add(scriptFile.getEffectiveName());
            CraftTweakerAPI.logInfo(getTweakerDescriptor(loaderName) + ": Loading Script: " + scriptFile);
            ZenParsedFile zenParsedFile = null;
            String filename = scriptFile.getEffectiveName();
            String className = extractClassName(filename);
            CRT_LOADING_SCRIPT_PRE_EVENT_LIST.publish(new CrTLoadingScriptEventPre(filename));
            Reader reader = null;
            try {
                reader = new InputStreamReader(new BufferedInputStream(scriptFile.open()), "UTF-8");
                CrTScriptLoadEvent loadEvent = new CrTScriptLoadEvent(scriptFile);
                preprocessorManager.postLoadEvent(loadEvent);
                // blocks the parsing of the script
                if (scriptFile.isParsingBlocked())
                    continue;
                ZenTokener parser = new ZenTokener(reader, environmentGlobal.getEnvironment(), filename, scriptFile.areBracketErrorsIgnored());
                zenParsedFile = new ZenParsedFile(filename, className, parser, environmentGlobal);
            } catch (IOException ex) {
                CraftTweakerAPI.logError(getTweakerDescriptor(loaderName) + ": Could not load script " + scriptFile + ": " + ex.getMessage());
                loadSuccessful = false;
            } catch (ParseException ex) {
                CraftTweakerAPI.logError(getTweakerDescriptor(loaderName) + ": Error parsing " + ex.getFile().getFileName() + ":" + ex.getLine() + " -- " + ex.getExplanation());
                loadSuccessful = false;
            } catch (Exception ex) {
                CraftTweakerAPI.logError(getTweakerDescriptor(loaderName) + ": Error loading " + scriptFile + ": " + ex.toString(), ex);
                loadSuccessful = false;
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException ignored) {
                    }
                }
            }
            try {
                // Stops if the compile is disabled
                if (zenParsedFile == null || scriptFile.isCompileBlocked())
                    continue;
                compileScripts(className, Collections.singletonList(zenParsedFile), environmentGlobal, scriptFile.isDebugEnabled() || DEBUG);
                // stops if the execution is disabled
                if (scriptFile.isExecutionBlocked() || isSyntaxCommand)
                    continue;
                ZenModule module = new ZenModule(classes, CraftTweakerAPI.class.getClassLoader());
                Runnable runnable = module.getMain();
                if (runnable != null)
                    runnable.run();
            } catch (Throwable ex) {
                CraftTweakerAPI.logError("[" + loaderName + "]: Error executing " + scriptFile + ": " + ex.getMessage(), ex);
            }
            CRT_LOADING_SCRIPT_POST_EVENT_LIST.publish(new CrTLoadingScriptEventPost(filename));
        // CraftTweakerAPI.logInfo("Completed file: " + filename +" in: " + (System.currentTimeMillis() - time) + "ms");
        }
    }
    CraftTweakerAPI.logInfo("Completed script loading in: " + (System.currentTimeMillis() - totalTime) + "ms");
    return loadSuccessful;
}
Also used : ZenModule(stanhebben.zenscript.ZenModule) ParseException(stanhebben.zenscript.parser.ParseException) IEnvironmentGlobal(stanhebben.zenscript.compiler.IEnvironmentGlobal) ParseException(stanhebben.zenscript.parser.ParseException)

Aggregations

IEnvironmentGlobal (stanhebben.zenscript.compiler.IEnvironmentGlobal)6 crafttweaker.annotations (crafttweaker.annotations)4 IBracketHandler (crafttweaker.zenscript.IBracketHandler)4 java.util (java.util)4 stanhebben.zenscript.expression (stanhebben.zenscript.expression)4 Token (stanhebben.zenscript.parser.Token)4 IZenSymbol (stanhebben.zenscript.symbols.IZenSymbol)4 crafttweaker (crafttweaker)2 GlobalRegistry (crafttweaker.zenscript.GlobalRegistry)2 stanhebben.zenscript.type.natives (stanhebben.zenscript.type.natives)2 IJavaMethod (stanhebben.zenscript.type.natives.IJavaMethod)2 CraftTweakerAPI (crafttweaker.CraftTweakerAPI)1 IEntityDefinition (crafttweaker.api.entity.IEntityDefinition)1 crafttweaker.api.item (crafttweaker.api.item)1 ILiquidStack (crafttweaker.api.liquid.ILiquidStack)1 CraftTweakerMC.getIItemStackWildcardSize (crafttweaker.api.minecraft.CraftTweakerMC.getIItemStackWildcardSize)1 IOreDictEntry (crafttweaker.api.oredict.IOreDictEntry)1 MCItemUtils (crafttweaker.mc1120.item.MCItemUtils)1 MCLiquidStack (crafttweaker.mc1120.liquid.MCLiquidStack)1 MCOreDictEntry (crafttweaker.mc1120.oredict.MCOreDictEntry)1