minim.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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
import streams, critbits, parseopt2, strutils, os, json
import
core/types,
core/parser,
core/interpreter,
core/utils
import
lib/min_lang,
lib/min_stack,
lib/min_num,
lib/min_str,
lib/min_logic,
lib/min_time,
lib/min_io,
lib/min_sys,
lib/min_crypto,
lib/min_fs
const USE_LINENOISE* = true
when USE_LINENOISE:
import
vendor/linenoise
var REPL = false
var DEBUGGING = false
const PRELUDE* = "prelude.min".slurp.strip
let usage* = " MiNiM v" & version & " - a tiny concatenative programming language" & """
(c) 2014-2016 Fabio Cevasco
Usage:
minim [options] [filename]
Arguments:
filename A minim file to interpret (default: STDIN).
Options:
-e, --evaluate Evaluate a minim program inline
-h, --help Print this help
-v, --version Print the program version
-i, --interactive Start MiNiM Shell"""
var CURRSCOPE*: ref MinScope
proc getExecs(): seq[string] =
var res = newSeq[string](0)
let getFiles = proc(dir: string) =
for c, s in walkDir(dir, true):
if (c == pcFile or c == pcLinkToFile) and not res.contains(s):
res.add s
getFiles(getCurrentDir())
for dir in "PATH".getEnv.split(PathSep):
getFiles(dir)
return res
when USE_LINENOISE:
proc completionCallback*(str: cstring, completions: ptr linenoiseCompletions) {.cdecl.}=
var words = ($str).split(" ")
var w = if words.len > 0: words.pop else: ""
var sep = ""
if words.len > 0:
sep = " "
if w.startsWith("'"):
for s in CURRSCOPE.symbols.keys:
if startsWith("'$1"%s, w):
linenoiseAddCompletion completions, words.join(" ") & sep & "'" & s
return
if w.startsWith("~"):
for s in CURRSCOPE.symbols.keys:
if startsWith("~$1"%s, w):
linenoiseAddCompletion completions, words.join(" ") & sep & "~" & s
return
if w.startsWith("@"):
for s in CURRSCOPE.symbols.keys:
if startsWith("@$1"%s, w):
linenoiseAddCompletion completions, words.join(" ") & sep & "@" & s
return
if w.startsWith(">"):
for s in CURRSCOPE.symbols.keys:
if startsWith(">$1"%s, w):
linenoiseAddCompletion completions, words.join(" ") & sep & ">" & s
return
if w.startsWith("<"):
for s, v in MINIMSYMBOLS.readFile.parseJson.pairs:
if startsWith("<$1"%s, w):
linenoiseAddCompletion completions, words.join(" ") & sep & "<" & s
return
if w.startsWith("*"):
for s in CURRSCOPE.symbols.keys:
if startsWith("*$1"%s, w):
linenoiseAddCompletion completions, words.join(" ") & sep & "*" & s
return
if w.startsWith("$"):
for s,v in envPairs():
if startsWith("$$1"%s, w):
linenoiseAddCompletion completions, words.join(" ") & sep & "$" & s
return
if w.startsWith("\""):
for c,s in walkDir(getCurrentDir(), true):
if startsWith("\"$1"%s, w):
linenoiseAddCompletion completions, words.join(" ") & sep & "\"" & s & "\""
return
let execs = getExecs()
if w.startsWith("!"):
for s in execs:
if startsWith("!$1"%s, w):
linenoiseAddCompletion completions, words.join(" ") & sep & "!" & s
if w.startsWith("&"):
for s in execs:
if startsWith("&$1"%s, w):
linenoiseAddCompletion completions, words.join(" ") & sep & "&" & s
for s in CURRSCOPE.symbols.keys:
if s.startsWith(w):
linenoiseAddCompletion completions, words.join(" ") & sep & s
proc prompt(s: string): string =
when USE_LINENOISE:
var res = linenoise(s)
if not res.isNil:
discard $linenoiseHistoryAdd(res)
return $res
when not(USE_LINENOISE):
stdout.write(s)
return stdin.readLine
proc stdLib(i: In) =
i.lang_module
i.io_module
i.logic_module
i.num_module
i.stack_module
i.str_module
i.sys_module
i.time_module
i.fs_module
i.crypto_module
i.eval PRELUDE
if not MINIMSYMBOLS.fileExists:
MINIMSYMBOLS.writeFile("{}")
if not MINIMHISTORY.fileExists:
MINIMHISTORY.writeFile("")
if not MINIMRC.fileExists:
MINIMRC.writeFile("")
i.eval MINIMRC.readFile()
proc minimStream(s: Stream, filename: string, debugging = false) =
var i = newMinInterpreter(debugging)
i.pwd = filename.parentDir
i.stdLib()
i.open(s, filename)
discard i.parser.getToken()
i.interpret()
i.close()
proc minimString*(buffer: string, debugging = false) =
minimStream(newStringStream(buffer), "input", debugging)
proc minimFile*(filename: string, debugging = false) =
var stream = newFileStream(filename, fmRead)
if stream == nil:
stderr.writeLine("Error - Cannot read from file: "& filename)
stderr.flushFile()
minimStream(stream, filename, debugging)
proc minimFile*(file: File, filename="stdin", debugging = false) =
var stream = newFileStream(stdin)
if stream == nil:
stderr.writeLine("Error - Cannot read from "& filename)
stderr.flushFile()
minimStream(stream, filename, debugging)
proc minimRepl*(i: var MinInterpreter) =
i.stdLib()
var s = newStringStream("")
i.open(s, "")
var line: string
echo "MiNiM Shell v$1" % version
echo "-> Type 'exit' or 'quit' to exit."
while true:
when USE_LINENOISE:
CURRSCOPE = i.scope
linenoiseSetCompletionCallback completionCallback
discard linenoiseHistorySetMaxLen(1000)
discard linenoiseHistoryLoad(MINIMHISTORY)
line = prompt(": ")
if line.isNil:
echo "-> Exiting..."
quit(0)
if $line != "(null)":
discard linenoiseHistorySave(MINIMHISTORY)
i.parser.buf = $i.parser.buf & $line
i.parser.bufLen = i.parser.buf.len
discard i.parser.getToken()
try:
i.interpret()
except:
warn getCurrentExceptionMsg()
finally:
if i.stack.len > 0:
let last = i.stack[i.stack.len - 1]
let n = $i.stack.len
if last.isQuotation and last.qVal.len > 1:
echo "{$1} -> (" % n
for item in last.qVal:
echo " " & $item
echo " ".repeat(n.len) & " )"
else:
echo "{$1} -> $2" % [$i.stack.len, $i.stack[i.stack.len - 1]]
else:
echo "{0} --"
proc minimRepl*(debugging = false) =
var i = newMinInterpreter(debugging)
i.minimRepl
###
var file, s: string = ""
for kind, key, val in getopt():
case kind:
of cmdArgument:
file = key
of cmdLongOption, cmdShortOption:
case key:
of "debug", "d":
DEBUGGING = true
of "evaluate", "e":
s = val
of "help", "h":
echo usage
quit(0)
of "version", "v":
echo version
quit(0)
of "interactive", "i":
REPL = true
else:
discard
else:
discard
if s != "":
minimString(s, DEBUGGING)
elif file != "":
minimFile file, DEBUGGING
elif REPL:
minimRepl DEBUGGING
quit(0)
else:
minimFile stdin, "stdin", DEBUGGING
|