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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
import std/[ strutils, sequtils, json, critbits, algorithm, streams, terminal, os ] import env, meta, interpreter, parser, stdlib, utils, value import minline var SIMPLEREPL* = false 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 p(s: string, color = fgWhite) = if SIMPLEREPL: stdout.write(s) else: stdout.styledWrite(color, s) proc pv(item: MinValue) = case item.kind of minNull, minBool: p($item, fgGreen) of minSymbol: p($item, fgCyan) of minString: p($item, fgYellow) of minFloat, minInt: p($item, fgMagenta) of minQuotation: p("( ", fgRed) for val in item.qVal: pv(val); stdout.write(" ") p(")", fgRed) of minCommand: p("[ ", fgRed) p(item.cmdVal, fgRed) p(" ]", fgRed) of minDictionary: p("{ ", fgRed) for val in item.dVal.pairs: var v: MinValue if val.val.kind == minProcOp: v = "<native>".newSym else: v = val.val.val pv(v); p(" :" & $val.key & " ", fgCyan) p("}", fgRed) 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: p(" (\n", fgRed) for item in res.qVal: p(" "); pv(item); stdout.write("\n") stdout.write(" ".repeat(n.len)); p(")\n", fgRed) elif res.isCommand: p(" [", fgRed); p(res.cmdVal, fgYellow); p("]\n") elif res.isDictionary and res.dVal.len > 1: p(" {\n", fgRed) for item in res.dVal.pairs: var v: MinValue if item.val.kind == minProcOp: v = "<native>".newSym else: v = item.val.val p(" "); pv(v); p(" :" & $item.key & "\n", fgCyan) if res.objType == "": stdout.write " ".repeat(n.len); p("}\n", fgRed) else: stdout.write " ".repeat(n.len); p(" ;" & res.objType & "\n", fgBlue) stdout.write " ".repeat(n.len); p("}\n", fgRed) else: stdout.write " "; pv(i.stack[i.stack.len - 1]); stdout.write("\n") 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() |