|
| 1 | +package com.scriptbasic; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileNotFoundException; |
| 5 | +import java.io.FileReader; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.Reader; |
| 8 | +import java.io.StringReader; |
| 9 | +import java.io.Writer; |
| 10 | + |
| 11 | +import com.scriptbasic.factories.FactoryFactory; |
| 12 | +import com.scriptbasic.interfaces.AnalysisException; |
| 13 | +import com.scriptbasic.interfaces.EngineApi; |
| 14 | +import com.scriptbasic.interfaces.ExecutionException; |
| 15 | +import com.scriptbasic.interfaces.ExtendedInterpreter; |
| 16 | +import com.scriptbasic.interfaces.Factory; |
| 17 | +import com.scriptbasic.interfaces.HierarchicalReader; |
| 18 | +import com.scriptbasic.interfaces.LexicalAnalyzer; |
| 19 | +import com.scriptbasic.interfaces.ScriptBasicException; |
| 20 | +import com.scriptbasic.interfaces.SourcePath; |
| 21 | +import com.scriptbasic.interfaces.SourceProvider; |
| 22 | +import com.scriptbasic.readers.GenericHierarchicalReader; |
| 23 | +import com.scriptbasic.readers.GenericReader; |
| 24 | +import com.scriptbasic.sourceproviders.BasicSourcePath; |
| 25 | +import com.scriptbasic.sourceproviders.FileSourceProvider; |
| 26 | +import com.scriptbasic.utility.FactoryUtility; |
| 27 | +import com.scriptbasic.utility.RightValueUtility; |
| 28 | + |
| 29 | +public class Engine implements EngineApi { |
| 30 | + |
| 31 | + private final Factory factory; |
| 32 | + private final ExtendedInterpreter interpreter; |
| 33 | + |
| 34 | + @Override |
| 35 | + public Factory getBasicFactory() { |
| 36 | + return factory; |
| 37 | + } |
| 38 | + |
| 39 | + public Engine() { |
| 40 | + factory = FactoryFactory.getFactory(); |
| 41 | + interpreter = FactoryUtility.getExtendedInterpreter(factory); |
| 42 | + } |
| 43 | + |
| 44 | + private Reader input; |
| 45 | + private Writer output; |
| 46 | + private Writer error; |
| 47 | + |
| 48 | + @Override |
| 49 | + public Reader getInput() { |
| 50 | + return input; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void setInput(final Reader input) { |
| 55 | + this.input = input; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public Writer getOutput() { |
| 60 | + return output; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void setOutput(final Writer output) { |
| 65 | + this.output = output; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public Writer getError() { |
| 70 | + return error; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void setError(final Writer error) { |
| 75 | + this.error = error; |
| 76 | + } |
| 77 | + |
| 78 | + private void eval(final Reader reader, String fileName, |
| 79 | + SourceProvider sourceProvider) throws ScriptBasicException { |
| 80 | + try { |
| 81 | + final com.scriptbasic.interfaces.Reader sourceReader; |
| 82 | + if (reader == null && sourceProvider != null) { |
| 83 | + sourceReader = sourceProvider.get(fileName); |
| 84 | + } else { |
| 85 | + GenericReader genericReader = new GenericReader(); |
| 86 | + genericReader.set(reader); |
| 87 | + genericReader.setSourceProvider(sourceProvider); |
| 88 | + sourceReader = genericReader; |
| 89 | + } |
| 90 | + sourceReader.set((String) fileName); |
| 91 | + final LexicalAnalyzer lexicalAnalyzer = FactoryUtility |
| 92 | + .getLexicalAnalyzer(factory); |
| 93 | + HierarchicalReader hReader = new GenericHierarchicalReader(); |
| 94 | + hReader.include(sourceReader); |
| 95 | + lexicalAnalyzer.set(hReader); |
| 96 | + interpreter.setProgram(FactoryUtility.getSyntaxAnalyzer(factory) |
| 97 | + .analyze()); |
| 98 | + interpreter.setWriter(output); |
| 99 | + interpreter.setErrorWriter(error); |
| 100 | + interpreter.setReader(input); |
| 101 | + interpreter.execute(); |
| 102 | + } catch (AnalysisException e) { |
| 103 | + throw new ScriptBasicException(e); |
| 104 | + } catch (ExecutionException e) { |
| 105 | + throw new ScriptBasicException(e); |
| 106 | + } catch (IOException e) { |
| 107 | + throw new ScriptBasicException(e); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void eval(final String sourceCode) throws ScriptBasicException { |
| 113 | + eval(new StringReader(sourceCode), null, null); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void eval(final Reader reader) throws ScriptBasicException { |
| 118 | + eval(reader, null, null); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void eval(final File sourceFile) throws ScriptBasicException { |
| 123 | + try { |
| 124 | + eval(new FileReader(sourceFile), sourceFile.getAbsolutePath(), null); |
| 125 | + } catch (FileNotFoundException e) { |
| 126 | + throw new ScriptBasicException(e); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void eval(String sourceFileName, String... path) |
| 132 | + throws ScriptBasicException { |
| 133 | + FileSourceProvider sourceProvider = new FileSourceProvider(); |
| 134 | + BasicSourcePath sourcePath = new BasicSourcePath(); |
| 135 | + for (String p : path) { |
| 136 | + sourcePath.add(p); |
| 137 | + } |
| 138 | + sourceProvider.setSourcePath(sourcePath); |
| 139 | + eval(null, sourceFileName, sourceProvider); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void eval(final String sourceFileName, final SourcePath path) |
| 144 | + throws ScriptBasicException { |
| 145 | + FileSourceProvider sourceProvider = new FileSourceProvider(); |
| 146 | + sourceProvider.setSourcePath(path); |
| 147 | + eval(null, sourceFileName, sourceProvider); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public void eval(final String sourceName, final SourceProvider provider) |
| 152 | + throws ScriptBasicException { |
| 153 | + eval(null, sourceName, provider); |
| 154 | + } |
| 155 | + |
| 156 | + public void setVariable(final String name, final Object value) |
| 157 | + throws ScriptBasicException { |
| 158 | + try { |
| 159 | + interpreter.getVariables().setVariable(name, |
| 160 | + RightValueUtility.createRightValue(value)); |
| 161 | + } catch (ExecutionException e) { |
| 162 | + throw new ScriptBasicException(e); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public Object getVariable(final String name) throws ScriptBasicException { |
| 168 | + try { |
| 169 | + return interpreter.getVariable(name); |
| 170 | + } catch (ExecutionException e) { |
| 171 | + throw new ScriptBasicException(e); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public Iterable<String> getVariablesIterator() { |
| 177 | + return interpreter.getVariables().getGlobalMap().getVariableNameSet(); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public Object call(String subroutineName, Object... args) |
| 182 | + throws ScriptBasicException { |
| 183 | + try { |
| 184 | + return interpreter.call(subroutineName, args); |
| 185 | + } catch (ExecutionException e) { |
| 186 | + throw new ScriptBasicException(e); |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments