minpkg/core/shell.nim
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
import std/[ strutils, sequtils, json, critbits, algorithm, streams, os ] import env, meta, interpreter, parser, stdlib, utils, value import minline proc interpret*(i: In, s: string): MinValue = i.open(newStringStream(s), i.filename) discard i.parser.getToken() try: result = i.interpret() except CatchableError: discard i.close() proc getCompletions*(ed: LineEditor, symbols: seq[string]): seq[string] = var words = ed.lineText.split(" ") var word: string if words.len == 0: word = ed.lineText else: word = words[words.len-1] if word.startsWith("'"): return symbols.mapIt("'" & $it) if word.startsWith("~"): return symbols.mapIt("~" & $it) if word.startsWith("?"): return symbols.mapIt("?" & $it) if word.startsWith("@"): return symbols.mapIt("@" & $it) if word.startsWith("#"): return symbols.mapIt("#" & $it) if word.startsWith(">"): return symbols.mapIt(">" & $it) if word.startsWith("*"): return symbols.mapIt("*" & $it) if word.startsWith("("): return symbols.mapIt("(" & $it) if word.startsWith("<"): return toSeq(MINSYMBOLS.readFile.parseJson.pairs).mapIt("<" & $it[0]) if word.startsWith("$"): return toSeq(envPairs()).mapIt("$" & $it[0]) if word.startsWith("\""): var f = word[1..^1] if f == "": f = getCurrentDir().replace("\\", "/") return toSeq(walkDir(f, true)).mapIt("\"$1" % it.path.replace("\\", "/")) elif f.dirExists: f = f.replace("\\", "/") if f[f.len-1] != '/': f = f & "/" return toSeq(walkDir(f, true)).mapIt("\"$1$2" % [f, it.path.replace("\\", "/")]) else: var dir: string if f.contains("/") or dir.contains("\\"): dir = f.parentDir let file = f.extractFileName return toSeq(walkDir(dir, true)).filterIt( it.path.toLowerAscii.startsWith(file.toLowerAscii)).mapIt( "\"$1/$2" % [dir, it.path.replace("\\", "/")]) else: dir = getCurrentDir() return toSeq(walkDir(dir, true)).filterIt( it.path.toLowerAscii.startsWith(f.toLowerAscii)).mapIt("\"$1" % [ it.path.replace("\\", "/")]) return symbols proc printResult(i: In, res: MinValue) = if res.isNil: return if i.stack.len > 0: let n = $i.stack.len if res.isQuotation and res.qVal.len > 1: echo " (" for item in res.qVal: echo " " & $item echo " ".repeat(n.len) & ")" elif res.isCommand: echo " [" & res.cmdVal & "]" elif res.isDictionary and res.dVal.len > 1: echo " {" for item in res.dVal.pairs: var v = "" if item.val.kind == minProcOp: v = "<native>" else: v = $item.val.val echo " " & v & " :" & $item.key if res.objType == "": echo " ".repeat(n.len) & "}" else: echo " ".repeat(n.len) & " ;" & res.objType echo " ".repeat(n.len) & "}" else: echo " $1" % [$i.stack[i.stack.len - 1]] proc minSimpleRepl*(i: var MinInterpreter) = i.stdLib() var s = newStringStream("") i.open(s, "<repl>") var line: string while true: i.push(i.newSym("prompt")) let vals = i.expect("str") let v = vals[0] let prompt = v.getString() stdout.write(prompt) stdout.flushFile() line = stdin.readLine() let r = i.interpret($line) if $line != "": i.printResult(r) proc minRepl*(i: var MinInterpreter) = DEV = true i.stdLib() var s = newStringStream("") i.open(s, "<repl>") var line: string echo "$# shell v$#" % [pkgName, pkgVersion] while true: let symbols = toSeq(i.scope.symbols.keys) EDITOR.completionCallback = proc(ed: LineEditor): seq[string] = var completions = ed.getCompletions(symbols) completions.sort() return completions # evaluate prompt i.push(i.newSym("prompt")) let vals = i.expect("str") let v = vals[0] let prompt = v.getString() line = EDITOR.readLine(prompt) let r = i.interpret($line) if $line != "": i.printResult(r) proc minRepl*() = var i = newMinInterpreter(filename = "<repl>") i.minRepl() proc minSimpleRepl*() = var i = newMinInterpreter(filename = "<repl>") i.minSimpleRepl() |