@@ -6,8 +6,8 @@ import java.util.Properties
66import ammonite .interp .{Parsers , Preprocessor }
77import ammonite .repl .Repl
88import ammonite .runtime .{History , Storage }
9- import ammonite .util .{ Name , Res }
10- import ammonite .util .Util .CodeSource
9+ import ammonite .util ._
10+ import ammonite .util .Util .{ CodeSource , VersionedWrapperId }
1111import fastparse .core .Parsed
1212import io .github .mandar2812 .dynaml .DynaZeppelin
1313import io .github .mandar2812 .dynaml .repl .{Defaults , DynaMLInterpreter }
@@ -22,7 +22,9 @@ class DynaMLZeppelinInterpreter(properties: Properties) extends Interpreter(prop
2222
2323 protected val outputBuffer = new ByteArrayOutputStream ()
2424
25- protected val dynaml_instance = new DynaZeppelin (outputStream = outputBuffer)
25+ protected val errorBuffer = new ByteArrayOutputStream ()
26+
27+ protected val dynaml_instance = new DynaZeppelin (outputStream = outputBuffer, errorStream = errorBuffer)
2628
2729 protected var CURRENT_LINE : Int = 0
2830
@@ -66,7 +68,7 @@ class DynaMLZeppelinInterpreter(properties: Properties) extends Interpreter(prop
6668 override def interpret (s : String , interpreterContext : InterpreterContext ) = {
6769 addHistory(s)
6870
69- val wrapperName = Name (" cmd" + CURRENT_LINE )
71+ /* val wrapperName = Name("cmd" + CURRENT_LINE)
7072 val fileName = wrapperName.encoded + ".sc"
7173 val result = for {
7274 blocks <- Preprocessor.splitScript(ammonite.interp.Interpreter.skipSheBangLine(s), fileName)
@@ -106,14 +108,66 @@ class DynaMLZeppelinInterpreter(properties: Properties) extends Interpreter(prop
106108 new InterpreterResult(InterpreterResult.Code.SUCCESS, output)
107109 } else {
108110 new InterpreterResult(InterpreterResult.Code.ERROR, result.toString)
111+ }*/
112+
113+ Parsers .Splitter .parse(s) match {
114+ case Parsed .Success (value, idx) =>
115+ val computation_output = dynaml_interp.processLine(s, value, CURRENT_LINE , false , () => CURRENT_LINE += 1 )
116+ val output = outputBuffer.toString(Charset .defaultCharset())
117+ val error = errorBuffer.toString(Charset .defaultCharset())
118+
119+ if (computation_output.isSuccess) {
120+ outputBuffer.reset()
121+ new InterpreterResult (InterpreterResult .Code .SUCCESS , output)
122+ } else {
123+ errorBuffer.reset()
124+ new InterpreterResult (InterpreterResult .Code .ERROR , " Syntax Error Mofo!" )
125+ }
126+
127+ case Parsed .Failure (_, index, extra) =>
128+ new InterpreterResult (InterpreterResult .Code .ERROR , fastparse.core.ParseError .msg(extra.input, extra.traced.expected, index))
109129 }
130+
131+
110132 }
111133
112134 def evaluate (s : String ) = {
113135 addHistory(s)
114136
115137 val wrapperName = Name (" cmd" + CURRENT_LINE )
116138 val fileName = wrapperName.encoded + " .sc"
139+
140+ def compileRunBlock (
141+ leadingSpaces : String , hookInfo : ImportHookInfo ,
142+ codeSource : CodeSource ,
143+ eval : (Preprocessor .Output , Name ) => Res [(Evaluated , Tag )],
144+ indexedWrapperName : Name ,
145+ wrapperIndex : Int = 1 ) = {
146+
147+ val printSuffix = if (wrapperIndex == 1 ) " " else " #" + wrapperIndex
148+ dynaml_interp.printer.info(" Compiling " + codeSource.printablePath + printSuffix)
149+ for {
150+ processed <- dynaml_interp.compilerManager.preprocess(codeSource.fileName).transform(
151+ hookInfo.stmts,
152+ " " ,
153+ leadingSpaces,
154+ codeSource.pkgName,
155+ indexedWrapperName,
156+ dynaml_interp.predefImports ++ dynaml_interp.frameImports ++ hookInfo.imports,
157+ _ => " scala.Iterator[String]()" ,
158+ extraCode = " " ,
159+ skipEmpty = false
160+ )
161+
162+ (ev, tag) <- eval(processed, indexedWrapperName)
163+ } yield ScriptOutput .BlockMetadata (
164+ VersionedWrapperId (ev.wrapper.map(_.encoded).mkString(" ." ), tag),
165+ leadingSpaces,
166+ hookInfo,
167+ ev.imports
168+ )
169+ }
170+
117171 val result = for {
118172 blocks <- Preprocessor .splitScript(ammonite.interp.Interpreter .skipSheBangLine(s), fileName)
119173
@@ -124,7 +178,26 @@ class DynaMLZeppelinInterpreter(properties: Properties) extends Interpreter(prop
124178 Some (dynaml_interp.wd/ " (console)" )
125179 )
126180
127- metadata <- dynaml_interp.processAllScriptBlocks(
181+ indexedWrapperName = ammonite.interp.Interpreter .indexWrapperName(codeSource.wrapperName, 1 )
182+
183+
184+ allSplittedChunks <- Res .Success (blocks)
185+ (leadingSpaces, stmts) = allSplittedChunks(1 - 1 )
186+ (hookStmts, importTrees) = dynaml_interp.parseImportHooks(codeSource, stmts)
187+ hookInfo <- dynaml_interp.resolveImportHooks(importTrees, hookStmts, codeSource)
188+
189+ res <- compileRunBlock(leadingSpaces, hookInfo, codeSource,
190+ (processed, indexedWrapperName) =>
191+ dynaml_interp.evaluateLine(
192+ processed, dynaml_interp.printer, fileName,
193+ indexedWrapperName, silent = false , () => CURRENT_LINE += 1 ),
194+ indexedWrapperName
195+ )
196+
197+
198+
199+
200+ /* metadata <- dynaml_interp.processAllScriptBlocks(
128201 blocks.map(_ => None),
129202 Res.Success(blocks),
130203 dynaml_interp.predefImports ++ dynaml_interp.frameImports,
@@ -135,19 +208,19 @@ class DynaMLZeppelinInterpreter(properties: Properties) extends Interpreter(prop
135208 indexedWrapperName, silent = false, () => CURRENT_LINE += 1),
136209 autoImport = true,
137210 ""
138- )
211+ )*/
139212 } yield {
140- metadata
213+ res
141214 }
142215
143216 if (result.isSuccess) {
144217
145218 val output = outputBuffer.toString(Charset .defaultCharset())
146219
147220 outputBuffer.reset()
148- val resStr = result.flatMap(d => {
221+ /* val resStr = result.flatMap(d => {
149222 Res(Some(d.blockInfo.map(blockm => blockm.finalImports.value.map(d => d.fromName.raw).mkString("\n")).mkString("\n")), "")
150- })
223+ })*/
151224
152225 output
153226 } else {
0 commit comments