all repos — min @ 2b31b6e07be6fe8fd0e7240d5b494b3f810561eb

A small but practical concatenative programming language.

Removed conditional builds
h3rald h3rald@h3rald.com
Sun, 28 Feb 2021 14:13:53 +0000
commit

2b31b6e07be6fe8fd0e7240d5b494b3f810561eb

parent

3e7c9799c0bb19c70e0f196febebe102a1041254

M min.nimmin.nim

@@ -1,19 +1,12 @@

import streams, strutils, - sequtils - -when defined(mini): - import - minpkg/core/minilogger -else: - import - json, - os, - algorithm, - logging, - minpkg/packages/niftylogger -import + sequtils, + json, + os, + algorithm, + logging, + minpkg/packages/niftylogger, minpkg/core/baseutils, minpkg/core/env, minpkg/core/parser,

@@ -29,22 +22,16 @@ minpkg/lib/min_dict,

minpkg/lib/min_num, minpkg/lib/min_str, minpkg/lib/min_logic, - minpkg/lib/min_time - -when not defined(mini): - import - minpkg/packages/nimline/nimline, - minpkg/lib/min_sys, - minpkg/lib/min_io, - minpkg/lib/min_dstore, - minpkg/lib/min_fs - -when not defined(lite) and not defined(mini): - import - minpkg/lib/min_http, - minpkg/lib/min_net, - minpkg/lib/min_crypto, - minpkg/lib/min_math + minpkg/lib/min_time, + minpkg/packages/nimline/nimline, + minpkg/lib/min_sys, + minpkg/lib/min_io, + minpkg/lib/min_dstore, + minpkg/lib/min_fs, + minpkg/lib/min_http, + minpkg/lib/min_net, + minpkg/lib/min_crypto, + minpkg/lib/min_math export env,

@@ -53,12 +40,8 @@ interpreter,

utils, value, scope, - min_lang - -when defined(mini): - export minilogger -else: - export niftylogger + min_lang, + niftylogger const PRELUDE* = "prelude.min".slurp.strip var NIMOPTIONS* = ""

@@ -160,31 +143,25 @@ i.logic_module

i.num_module i.str_module i.time_module - when not defined(mini): - i.sys_module - i.fs_module - i.dstore_module - i.io_module - when not defined(lite) and not defined(mini): - i.crypto_module - i.net_module - i.math_module - i.http_module + i.sys_module + i.fs_module + i.dstore_module + i.io_module + i.crypto_module + i.net_module + i.math_module + i.http_module if customPrelude == "": i.eval PRELUDE, "<prelude>" else: try: i.eval customPrelude.readFile, customPrelude except: - when defined(mini): - minilogger.warn("Unable to process custom prelude code in $1" % customPrelude) - else: - logging.warn("Unable to process custom prelude code in $1" % customPrelude) - when not defined(mini): - try: - i.eval MINRC.readFile() - except: - error "An error occurred evaluating the .minrc file." + logging.warn("Unable to process custom prelude code in $1" % customPrelude) + try: + i.eval MINRC.readFile() + except: + error "An error occurred evaluating the .minrc file." proc interpret*(i: In, s: Stream) = i.stdLib()

@@ -208,10 +185,9 @@

proc minFile*(filename: string, op = "interpret", main = true): seq[string] {.discardable.} proc compile*(i: In, s: Stream, main = true): seq[string] = - when not defined(mini): - if "nim".findExe == "": - logging.error "Nim compiler not found, unable to compile." - quit(7) + if "nim".findExe == "": + logging.error "Nim compiler not found, unable to compile." + quit(7) result = newSeq[string](0) i.open(s, i.filename) discard i.parser.getToken()

@@ -295,11 +271,6 @@ var COMPILE = false

var MODULEPATH = "" var exeName = "min" var iOpt = "\n -i, --interactive Start $1 shell (with advanced prompt, default if no file specidied)\n" - when defined(lite): - exeName = "litemin" - when defined(mini): - iOpt = "" - exeName = "minimin" proc printResult(i: In, res: MinValue) = if res.isNil:

@@ -345,31 +316,29 @@ let r = i.interpret($line)

if $line != "": i.printResult(r) - when not defined(mini): + proc minRepl*(i: var MinInterpreter) = + i.stdLib() + var s = newStringStream("") + i.open(s, "<repl>") + var line: string + echo "$# shell v$#" % [exeName, pkgVersion] + while true: + let symbols = toSeq(i.scope.symbols.keys) + EDITOR.completionCallback = proc(ed: LineEditor): seq[string] = + return ed.getCompletions(symbols) + # 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*(i: var MinInterpreter) = - i.stdLib() - var s = newStringStream("") - i.open(s, "<repl>") - var line: string - echo "$# shell v$#" % [exeName, pkgVersion] - while true: - let symbols = toSeq(i.scope.symbols.keys) - EDITOR.completionCallback = proc(ed: LineEditor): seq[string] = - return ed.getCompletions(symbols) - # 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 minRepl*() = + var i = newMinInterpreter(filename = "<repl>") + i.minRepl() proc minSimpleRepl*() = var i = newMinInterpreter(filename = "<repl>")

@@ -460,15 +429,13 @@ discard

var op = "interpret" if COMPILE: op = "compile" - - when not defined(mini): - if MODULEPATH.len > 0: - for f in walkDirRec(MODULEPATH): - if f.endsWith(".min"): - MINMODULES.add f - elif REPL: - minRepl() - quit(0) + if MODULEPATH.len > 0: + for f in walkDirRec(MODULEPATH): + if f.endsWith(".min"): + MINMODULES.add f + elif REPL: + minRepl() + quit(0) if s != "": minStr(s) elif file != "":

@@ -477,11 +444,8 @@ elif SIMPLEREPL:

minSimpleRepl() quit(0) else: - when defined(mini): + if isatty(stdin): + minRepl() + quit(0) + else: minStream newFileStream(stdin), "stdin", op - else: - if isatty(stdin): - minRepl() - quit(0) - else: - minStream newFileStream(stdin), "stdin", op
M min.nimsmin.nims

@@ -17,7 +17,7 @@ if findExe("musl-gcc") != "":

switch("gcc.exe", "musl-gcc") switch("gcc.linkerexe", "musl-gcc") -when defined(ssl) and not defined(mini): +when defined(ssl): switch("threads", "on") when defined(windows): # TODO", change once issue nim#15220 is resolved
M minpkg/core/baseutils.nimminpkg/core/baseutils.nim

@@ -1,4 +1,7 @@

-import strutils +import + strutils, + os, + json proc reverse*[T](xs: openarray[T]): seq[T] = result = newSeq[T](xs.len)

@@ -15,48 +18,11 @@ if pwd == "":

result = file else: result = pwd&"/"&file - -when defined(mini): - import - strutils - proc parentDirEx*(s: string): string = - let fslash = s.rfind("/") - let bslash = s.rfind("\\") - var dirEnd = fslash-1 - if dirEnd < 0: - dirEnd = bslash-1 - if dirEnd < 0: - dirEnd = s.len-1 - if dirEnd < 0: - return s - return s[0..dirEnd] - - proc escapeEx*(s: string, unquoted = false): string = - for c in s: - case c - of '\L': result.add("\\n") - of '\b': result.add("\\b") - of '\f': result.add("\\f") - of '\t': result.add("\\t") - of '\v': result.add("\\u000b") - of '\r': result.add("\\r") - of '"': result.add("\\\"") - of '\0'..'\7': result.add("\\u000" & $ord(c)) - of '\14'..'\31': result.add("\\u00" & toHex(ord(c), 2)) - of '\\': result.add("\\\\") - else: result.add(c) - if unquoted: - return result - return "\"" & result & "\"" +proc parentDirEx*(s: string): string = + return s.parentDir -else: - import os, json - - proc parentDirEx*(s: string): string = - return s.parentDir - - proc escapeEx*(s: string, unquoted = false): string = - if unquoted: - return s.escapeJsonUnquoted - return s.escapeJson +proc escapeEx*(s: string, unquoted = false): string = + if unquoted: + return s.escapeJsonUnquoted + return s.escapeJson
M minpkg/core/env.nimminpkg/core/env.nim

@@ -1,25 +1,20 @@

-when not defined(mini): - import - os, - ../packages/nimline/nimline +import + os, + ../packages/nimline/nimline - var HOME*: string - if defined(windows): - HOME = getenv("USERPROFILE") - if not defined(windows): - HOME = getenv("HOME") +var HOME*: string +if defined(windows): + HOME = getenv("USERPROFILE") +if not defined(windows): + HOME = getenv("HOME") - var MINRC* {.threadvar.}: string - MINRC = HOME / ".minrc" - var MINSYMBOLS* {.threadvar.}: string - MINSYMBOLS = HOME / ".min_symbols" - var MINHISTORY* {.threadvar.}: string - MINHISTORY = HOME / ".min_history" - var MINLIBS* {.threadvar.} : string - MINLIBS = HOME / ".minlibs" - - var EDITOR* {.threadvar.}: LineEditor - EDITOR = initEditor(historyFile = MINHISTORY) - +var MINRC* {.threadvar.}: string +MINRC = HOME / ".minrc" +var MINSYMBOLS* {.threadvar.}: string +MINSYMBOLS = HOME / ".min_symbols" +var MINHISTORY* {.threadvar.}: string +MINHISTORY = HOME / ".min_history" +var EDITOR* {.threadvar.}: LineEditor +EDITOR = initEditor(historyFile = MINHISTORY) var MINCOMPILED* {.threadvar.}: bool MINCOMPILED = false
M minpkg/core/interpreter.nimminpkg/core/interpreter.nim

@@ -5,14 +5,9 @@ sequtils,

os, critbits, json, - algorithm -when defined(mini): - import - minilogger -else: - import - base64, - logging + algorithm, + base64, + logging import baseutils, value,

@@ -153,9 +148,9 @@

proc close*(i: In) = i.parser.close(); -proc push*(i: In, val: MinValue) {.gcsafe, extern:"min_exported_symbol_$1".} +proc push*(i: In, val: MinValue) {.gcsafe.} -proc call*(i: In, q: var MinValue): MinValue {.gcsafe, extern:"min_exported_symbol_$1".}= +proc call*(i: In, q: var MinValue): MinValue {.gcsafe.}= var i2 = newMinInterpreter("<call>") i2.trace = i.trace i2.scope = i.scope

@@ -169,7 +164,7 @@ i.trace = i2.trace

raise return i2.stack.newVal -proc callValue*(i: In, v: var MinValue): MinValue {.gcsafe, extern:"min_exported_symbol_$1".}= +proc callValue*(i: In, v: var MinValue): MinValue {.gcsafe.}= var i2 = newMinInterpreter("<call-value>") i2.trace = i.trace i2.scope = i.scope

@@ -182,7 +177,7 @@ i.trace = i2.trace

raise return i2.stack[0] -proc copyDict*(i: In, val: MinValue): MinValue {.gcsafe, extern:"min_exported_symbol_$1".}= +proc copyDict*(i: In, val: MinValue): MinValue {.gcsafe.}= # Assuming val is a dictionary var v = newDict(i.scope) for item in val.scope.symbols.pairs:

@@ -195,7 +190,7 @@ if not val.obj.isNil:

v.obj = val.obj return v -proc apply*(i: In, op: MinOperator, sym = "") {.gcsafe, extern:"min_exported_symbol_$1".}= +proc apply*(i: In, op: MinOperator, sym = "") {.gcsafe.}= if op.kind == minProcOp: op.prc(i) else:

@@ -219,7 +214,7 @@ q.qVal = qqval

else: i.push(q) -proc apply*(i: In, q: var MinValue) {.gcsafe, extern:"min_exported_symbol_$1_2".}= +proc apply*(i: In, q: var MinValue) {.gcsafe.}= var i2 = newMinInterpreter("<apply>") i2.trace = i.trace i2.scope = i.scope

@@ -254,7 +249,7 @@ column: i.currSym.column,

outerSym: i.currSym.symVal, docComment: i.currSym.docComment) -proc push*(i: In, val: MinValue) {.gcsafe, extern:"min_exported_symbol_$1".}= +proc push*(i: In, val: MinValue) {.gcsafe.}= if val.kind == minSymbol: i.debug(val) if not i.evaluating:

@@ -324,8 +319,7 @@ i.stackTrace()

i.trace = @[] raise MinTrappedException(msg: msg) - -proc interpret*(i: In, parseOnly=false): MinValue {.discardable, extern:"min_exported_symbol_$1".} = +proc interpret*(i: In, parseOnly=false): MinValue {.discardable.} = var val: MinValue var q: MinValue if parseOnly:

@@ -344,14 +338,14 @@ return q

if i.stack.len > 0: return i.stack[i.stack.len - 1] -proc rawCompile*(i: In, indent = ""): seq[string] {.discardable, extern:"min_exported_symbol_$1".} = +proc rawCompile*(i: In, indent = ""): seq[string] {.discardable.} = while i.parser.token != tkEof: if i.trace.len == 0: i.stackcopy = i.stack handleErrors(i) do: result.add i.parser.compileMinValue(i, push = true, indent) -proc compileFile*(i: In, main: bool): seq[string] {.discardable, extern:"min_exported_symbol_$1".} = +proc compileFile*(i: In, main: bool): seq[string] {.discardable.} = result = newSeq[string](0) if not main: result.add "COMPILEDMINFILES[\"$#\"] = proc(i: In) {.gcsafe.}=" % i.filename

@@ -359,27 +353,25 @@ result = result.concat(i.rawCompile(" "))

else: result = i.rawCompile("") -proc initCompiledFile*(i: In, files: seq[string]): seq[string] {.discardable, extern:"min_exported_symbol_$1".} = +proc initCompiledFile*(i: In, files: seq[string]): seq[string] {.discardable.} = result = newSeq[string](0) result.add "import min" if files.len > 0 or (ASSETPATH != "" and not defined(mini)): result.add "import critbits" - when not defined(mini): - if ASSETPATH != "": - result.add "import base64" + if ASSETPATH != "": + result.add "import base64" result.add "MINCOMPILED = true" result.add "var i = newMinInterpreter(\"$#\")" % i.filename result.add "i.stdLib()" - when not defined(mini): - if ASSETPATH != "": - for f in walkDirRec(ASSETPATH): - let file = simplifyPath(i.filename, f) - logging.notice("- Including: $#" % file) - let ef = file.readFile.encode - let asset = "COMPILEDASSETS[\"$#\"] = \"$#\".decode" % [file, ef] - result.add asset + if ASSETPATH != "": + for f in walkDirRec(ASSETPATH): + let file = simplifyPath(i.filename, f) + logging.notice("- Including: $#" % file) + let ef = file.readFile.encode + let asset = "COMPILEDASSETS[\"$#\"] = \"$#\".decode" % [file, ef] + result.add asset -proc eval*(i: In, s: string, name="<eval>", parseOnly=false): MinValue {.discardable, extern:"min_exported_symbol_$1".}= +proc eval*(i: In, s: string, name="<eval>", parseOnly=false): MinValue {.discardable.}= var i2 = i.copy(name) i2.open(newStringStream(s), name) discard i2.parser.getToken()

@@ -389,7 +381,7 @@ i.stackcopy = i2.stackcopy

i.stack = i2.stack i.scope = i2.scope -proc load*(i: In, s: string, parseOnly=false): MinValue {.discardable, extern:"min_exported_symbol_$1".}= +proc load*(i: In, s: string, parseOnly=false): MinValue {.discardable.}= var fileLines = newSeq[string](0) var contents = "" try:
D minpkg/core/minilogger.nim

@@ -1,86 +0,0 @@

-import - strutils - -type - Level* = enum lvlAll, lvlDebug, lvlInfo, lvlNotice, lvlWarn, lvlError, lvlFatal, lvlNone - -const - LevelNames: array[Level, string] = [ "DEBUG", "DEBUG", "INFO", "NOTICE", "WARN", "ERROR", "FATAL", "NONE"] - -var LOGLEVEL* {.threadvar.}: Level -LOGLEVEL = lvlNotice - -proc logPrefix(level: Level): string = - case level: - of lvlDebug: - return ("---") - of lvlInfo: - return ("(i)") - of lvlNotice: - return (" ") - of lvlWarn: - return ("(!)") - of lvlError: - return ("(!)") - of lvlFatal: - return ("(x)") - else: - return (" ") - -proc log*(level: Level; args: varargs[string, `$`]) = - var f = stdout - if level >= LOGLEVEL: - if level >= lvlWarn: - f = stderr - let prefix = level.logPrefix() - f.write(prefix&" ") - f.write(args.join(" ")) - f.write("\n") - if level in {lvlError, lvlFatal}: flushFile(f) - -proc fatal*(args: varargs[string, `$`]) = - log(lvlFatal, args) - -proc error*(args: varargs[string, `$`]) = - log(lvlError, args) - -proc warn*(args: varargs[string, `$`]) = - log(lvlWarn, args) - -proc notice*(args: varargs[string, `$`]) = - log(lvlNotice, args) - -proc info*(args: varargs[string, `$`]) = - log(lvlInfo, args) - -proc debug*(args: varargs[string, `$`]) = - log(lvlDebug, args) - -proc getLogLevel*(): string = - return LevelNames[LOGLEVEL].toLowerAscii - -proc setLogFilter*(lvl: Level) = - LOGLEVEL = lvl - -proc setLogLevel*(val: var string): string {.discardable.} = - var lvl: Level - case val: - of "debug": - lvl = lvlDebug - of "info": - lvl = lvlInfo - of "notice": - lvl = lvlNotice - of "warn": - lvl = lvlWarn - of "error": - lvl = lvlError - of "fatal": - lvl = lvlFatal - of "none": - lvl = lvlNone - else: - val = "warn" - lvl = lvlWarn - LOGLEVEL = lvl - return val
M minpkg/core/scope.nimminpkg/core/scope.nim

@@ -29,7 +29,7 @@ return scope.parent.hasSymbol(key)

else: return false -proc delSymbol*(scope: ref MinScope, key: string): bool {.discardable, extern:"min_exported_symbol_$1".}= +proc delSymbol*(scope: ref MinScope, key: string): bool {.discardable.}= if scope.symbols.hasKey(key): if scope.symbols[key].sealed: raiseInvalid("Symbol '$1' is sealed." % key)

@@ -37,7 +37,7 @@ scope.symbols.excl(key)

return true return false -proc setSymbol*(scope: ref MinScope, key: string, value: MinOperator, override = false): bool {.discardable, extern:"min_exported_symbol_$1".}= +proc setSymbol*(scope: ref MinScope, key: string, value: MinOperator, override = false): bool {.discardable.}= result = false # check if a symbol already exists in current scope if not scope.isNil and scope.symbols.hasKey(key):

@@ -68,7 +68,7 @@ return scope.parent.hasSigil(key)

else: return false -proc delSigil*(scope: ref MinScope, key: string): bool {.discardable, extern:"min_exported_symbol_$1".}= +proc delSigil*(scope: ref MinScope, key: string): bool {.discardable.}= if scope.sigils.hasKey(key): if scope.sigils[key].sealed: raiseInvalid("Sigil '$1' is sealed." % key)

@@ -76,7 +76,7 @@ scope.sigils.excl(key)

return true return false -proc setSigil*(scope: ref MinScope, key: string, value: MinOperator, override = false): bool {.discardable, extern:"min_exported_symbol_$1".}= +proc setSigil*(scope: ref MinScope, key: string, value: MinOperator, override = false): bool {.discardable.}= result = false # check if a sigil already exists in current scope if not scope.isNil and scope.sigils.hasKey(key):