From 6e680afc5c10fd473b48aba22a52440a89ddff04 Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Sat, 27 Apr 2019 17:40:24 +0200 Subject: [PATCH] Make Spry a compile-time option --- src/blobset.nim | 160 +++++++++++++++++++++++++----------------------- 1 file changed, 83 insertions(+), 77 deletions(-) diff --git a/src/blobset.nim b/src/blobset.nim index 6383b2c..8ad04ae 100644 --- a/src/blobset.nim +++ b/src/blobset.nim @@ -7,13 +7,14 @@ import ./blobsets, ./blobsets/filestores, ./blobsets/httpservers, ./blobsets/httpstores # Basic Spry -import spryvm/spryvm +when defined(spry): + import spryvm/spryvm -# Spry extra modules -import spryvm/sprycore, spryvm/spryextend, spryvm/sprymath, spryvm/spryos, spryvm/spryio, - spryvm/spryoo, spryvm/sprystring, spryvm/sprymodules, spryvm/spryreflect, spryvm/sprymemfile, - spryvm/spryblock, - ./blobsets/spryblobs + # Spry extra modules + import spryvm/sprycore, spryvm/spryextend, spryvm/sprymath, spryvm/spryos, spryvm/spryio, + spryvm/spryoo, spryvm/sprystring, spryvm/sprymodules, spryvm/spryreflect, spryvm /sprymemfile, + spryvm/spryblock, + ./blobsets/spryblobs import os, strutils when defined(readLine): @@ -832,77 +833,78 @@ proc getLine(prompt: string): string = stdout.write(prompt) result = stdin.readline() -proc spryMain() = - let spry = newInterpreter() - spry.addCore() - spry.addExtend() - spry.addMath() - spry.addOS() - spry.addIO() - spry.addOO() - spry.addString() - spry.addModules() - spry.addReflect() - spry.addMemfile() - spry.addBlock() - spry.addBlobSets() - var - lines, stashed, fileLines = newSeq[string]() - suspended: bool = true - - echo "Welcome to interactive Spry!" - echo "An empty line will evaluate previous lines, so hit enter twice." - # We collect lines until an empty line is entered, easy way to enter - # multiline code. - - while true: - var line: string - if suspended: - line = getLine(Prompt) - else: - if fileLines.len == 0: - quit 0 - # Read a line, eh, would be nice with removeFirst or popFirst... - line = fileLines[0] - fileLines.delete(0) - # Logic for pausing - if line.strip() == "# pause": - var enter = getLine(" ") - if enter.strip() == "s": - stdout.write(" \n") - stashed = lines - lines = newSeq[string]() - suspended = true - continue +when defined(spry): + proc spryMain() = + let spry = newInterpreter() + spry.addCore() + spry.addExtend() + spry.addMath() + spry.addOS() + spry.addIO() + spry.addOO() + spry.addString() + spry.addModules() + spry.addReflect() + spry.addMemfile() + spry.addBlock() + spry.addBlobSets() + var + lines, stashed, fileLines = newSeq[string]() + suspended: bool = true + + echo "Welcome to interactive Spry!" + echo "An empty line will evaluate previous lines, so hit enter twice." + # We collect lines until an empty line is entered, easy way to enter + # multiline code. + + while true: + var line: string + if suspended: + line = getLine(Prompt) else: - stdout.write(line & "\n") - - # Logic to start the script again - if suspended and line.strip() == "c": - lines = stashed - suspended = false - continue - - # Finally time to eval - if line.strip().len() == 0: - let code = lines.join("\n") - lines = newSeq[string]() - try: - # Let the interpreter eval the code. We need to eval whatever we - # get (ispry acting as a func). The surrounding block is just because we only - # want to pass one Node. - var result = spry.evalRoot("[" & code & "]") - #discard spry.setBinding(newEvalWord("@"), result) - var output = $result - # Print any result - if output.isNil: - output = if suspended: "nil" else: "" - stdout.write(output & "\n") - except: - echo "Oops, sorry about that: " & getCurrentExceptionMsg() & "\n" - echo getStackTrace() - else: - lines.add(line) + if fileLines.len == 0: + quit 0 + # Read a line, eh, would be nice with removeFirst or popFirst... + line = fileLines[0] + fileLines.delete(0) + # Logic for pausing + if line.strip() == "# pause": + var enter = getLine(" ") + if enter.strip() == "s": + stdout.write(" \n") + stashed = lines + lines = newSeq[string]() + suspended = true + continue + else: + stdout.write(line & "\n") + + # Logic to start the script again + if suspended and line.strip() == "c": + lines = stashed + suspended = false + continue + + # Finally time to eval + if line.strip().len() == 0: + let code = lines.join("\n") + lines = newSeq[string]() + try: + # Let the interpreter eval the code. We need to eval whatever we + # get (ispry acting as a func). The surrounding block is just because we only + # want to pass one Node. + var result = spry.evalRoot("[" & code & "]") + #discard spry.setBinding(newEvalWord("@"), result) + var output = $result + # Print any result + if output.isNil: + output = if suspended: "nil" else: "" + stdout.write(output & "\n") + except: + echo "Oops, sorry about that: " & getCurrentExceptionMsg() & "\n" + echo getStackTrace() + else: + lines.add(line) when isMainModule: var cmd = "" @@ -917,7 +919,11 @@ when isMainModule: of "dump": dumpMain() of "ingest": waitFor ingestMain() of "server": waitFor serverMain() - of "spry": spryMain() + of "spry": + when defined(spry): + spryMain() + else: + quit "not compiled with Spry interpreter" of "check": waitFor checkMain() of "replicate": waitFor replicateMain() else: quit("no such subcommand " & cmd)