all repos — min @ 6d9717124714f473da9c798a10a17cf3cea4fc27

A small but practical concatenative programming language.

Fixed compilation errors and warnings.
h3rald h3rald@h3rald.com
Sun, 08 May 2016 16:21:20 +0200
commit

6d9717124714f473da9c798a10a17cf3cea4fc27

parent

323618c811ea88c8d4456cabbce6bfd8c9d728c6

M core/interpreter.nimcore/interpreter.nim

@@ -47,9 +47,9 @@

proc error*(i: MinInterpreter, status: MinError, message = "") = var msg = if message == "": ERRORS[status] else: message if i.filename == "": - stderr.writeln("`$1`: Error - $2" %[i.currSym.symVal, msg]) + stderr.writeLine("`$1`: Error - $2" % [i.currSym.symVal, msg]) else: - stderr.writeln("$1 [$2,$3] `$4`: Error - $5" %[i.filename, $i.currSym.line, $i.currSym.column, i.currSym.symVal, msg]) + stderr.writeLine("$1 [$2,$3] `$4`: Error - $5" % [i.filename, $i.currSym.line, $i.currSym.column, i.currSym.symVal, msg]) quit(int(status)) proc open*(i: var MinInterpreter, stream:Stream, filename: string) =

@@ -67,7 +67,7 @@ return s

proc debug(i: var MinInterpreter, value: MinValue) = if i.debugging: - stderr.writeln("-- " &i.dump & $value) + stderr.writeLine("-- " & i.dump & $value) proc push*(i: var MinInterpreter, val: MinValue) = i.debug val

@@ -126,7 +126,7 @@ i.open(newStringStream(s), "eval")

discard i.parser.getToken() i.interpret() except: - stderr.writeln getCurrentExceptionMsg() + stderr.writeLine getCurrentExceptionMsg() finally: i.filename = fn

@@ -137,7 +137,7 @@ i.open(newStringStream(s.readFile), s)

discard i.parser.getToken() i.interpret() except: - stderr.writeln getCurrentExceptionMsg() + stderr.writeLine getCurrentExceptionMsg() finally: i.filename = fn
M core/utils.nimcore/utils.nim

@@ -1,11 +1,13 @@

import tables, strutils import parser, interpreter -template minsym*(name: string, body: stmt): stmt {.immediate.} = - SYMBOLS[name] = proc (i: var MinInterpreter) = + +template minsym*(name: string, i: expr, body: stmt): stmt {.immediate.} = + bind SYMBOLS + SYMBOLS[name] = proc (i: var MinInterpreter) {.closure.} = body -template minsigil*(name: char, body: stmt): stmt {.immediate.} = +template minsigil*(name: char, i: expr, body: stmt): stmt {.immediate.} = SIGILS[name] = proc (i: var MinInterpreter) = body

@@ -46,7 +48,7 @@ proc newVal*(s: bool): MinValue =

return MinValue(kind: minBool, boolVal: s) proc warn*(s: string) = - stderr.writeln s + stderr.writeLine s proc linrec*(i: var MinInterpreter, p, t, r1, r2: MinValue) = i.push p.qVal
M lib/io.nimlib/io.nim

@@ -3,18 +3,18 @@ import ../core/parser, ../core/interpreter, ../core/utils

# I/O -minsym "puts": +minsym "puts", i: let a = i.peek echo a -minsym "gets": +minsym "gets", i: i.push newVal(stdin.readLine()) -minsym "print": +minsym "print", i: let a = i.peek a.print -minsym "read": +minsym "read", i: let a = i.pop if a.isString: if a.strVal.fileExists:

@@ -27,7 +27,7 @@ warn "File '$1' not found" % [a.strVal]

else: i.error(errIncorrect, "A string is required on the stack") -minsym "write": +minsym "write", i: let a = i.pop let b = i.pop if a.isString and b.isString:
M lib/lang.nimlib/lang.nim

@@ -1,31 +1,31 @@

import tables, strutils import ../core/parser, ../core/interpreter, ../core/utils -minsym "exit": +minsym "exit", i: quit(0) -minsym "symbols": +minsym "symbols", i: var q = newSeq[MinValue](0) for s in SYMBOLS.keys: q.add s.newVal i.push q.newVal -minsym "sigils": +minsym "sigils", i: var q = newSeq[MinValue](0) for s in SIGILS.keys: q.add s.newVal i.push q.newVal -minsym "debug?": +minsym "debug?", i: i.push i.debugging.newVal -minsym "debug": +minsym "debug", i: i.debugging = not i.debugging echo "Debugging: $1" % [$i.debugging] # Language constructs -minsym "bind": +minsym "bind", i: var q2 = i.pop # new (can be a quoted symbol or a string) var q1 = i.pop # existing (auto-quoted) var symbol: string

@@ -39,12 +39,12 @@ else:

i.error errIncorrect, "The top quotation must contain only one symbol value" if SYMBOLS.hasKey(symbol): i.error errSystem, "Symbol '$1' already exists" % [symbol] - minsym symbol: + minsym symbol, i: i.evaluating = true i.push q1.qVal i.evaluating = false -minsym "unbind": +minsym "unbind", i: var q1 = i.pop if q1.qVal.len == 1 and q1.qVal[0].kind == minSymbol: var symbol = q1.qVal[0].symVal

@@ -52,10 +52,10 @@ SYMBOLS.del symbol

else: i.error errIncorrect, "The top quotation must contain only one symbol value" -minsigil "'": +minsigil "'", i: i.push(@[MinValue(kind: minSymbol, symVal: i.pop.strVal)].newVal) -minsym "sigil": +minsym "sigil", i: var q1 = i.pop let q2 = i.pop if q1.isString:

@@ -66,7 +66,7 @@ var symbol = q1.qVal[0].symVal

if symbol.len == 1: if SIGILS.hasKey(symbol): i.error errSystem, "Sigil '$1' already exists" % [symbol] - minsigil symbol: + minsigil symbol, i: i.evaluating = true i.push q2.qVal i.evaluating = false

@@ -77,14 +77,14 @@ i.error errIncorrect, "The top quotation must contain only one symbol value"

else: i.error errIncorrect, "Two quotations are required on the stack" -minsym "eval": +minsym "eval", i: let s = i.pop if s.isString: i.eval s.strVal else: i.error(errIncorrect, "A string is required on the stack") -minsym "load": +minsym "load", i: let s = i.pop if s.isString: i.load s.strVal

@@ -94,20 +94,20 @@

# Operations on the whole stack -minsym "clear": +minsym "clear", i: while i.stack.len > 0: discard i.pop -minsym "dump": +minsym "dump", i: echo i.dump -minsym "stack": +minsym "stack", i: var s = i.stack i.push s # Operations on quotations or strings -minsym "concat": +minsym "concat", i: var q1 = i.pop var q2 = i.pop if q1.isString and q2.isString:

@@ -119,7 +119,7 @@ i.push newVal(q)

else: i.error(errIncorrect, "Two quotations or two strings are required on the stack") -minsym "first": +minsym "first", i: var q = i.pop if q.isQuotation: i.push q.qVal[0]

@@ -128,7 +128,7 @@ i.push newVal($q.strVal[0])

else: i.error(errIncorrect, "A quotation or a string is required on the stack") -minsym "rest": +minsym "rest", i: var q = i.pop if q.isQuotation: i.push newVal(q.qVal[1..q.qVal.len-1])
M lib/logic.nimlib/logic.nim

@@ -3,7 +3,7 @@ import ../core/parser, ../core/interpreter, ../core/utils

# Comparison operators -minsym ">": +minsym ">", i: let n2 = i.pop let n1 = i.pop if n1.isNumber and n2.isNumber:

@@ -20,7 +20,7 @@ i.push newVal(n1.strVal > n2.strVal)

else: i.error(errIncorrect, "Two numbers or two strings are required on the stack") -minsym ">=": +minsym ">=", i: let n2 = i.pop let n1 = i.pop if n1.isNumber and n2.isNumber:

@@ -37,7 +37,7 @@ i.push newVal(n1.strVal >= n2.strVal)

else: i.error(errIncorrect, "Two numbers or two strings are required on the stack") -minsym "<": +minsym "<", i: let n1 = i.pop let n2 = i.pop if n1.isNumber and n2.isNumber:

@@ -54,7 +54,7 @@ i.push newVal(n1.strVal > n2.strVal)

else: i.error(errIncorrect, "Two numbers or two strings are required on the stack") -minsym "<=": +minsym "<=", i: let n1 = i.pop let n2 = i.pop if n1.isNumber and n2.isNumber:

@@ -71,7 +71,7 @@ i.push newVal(n1.strVal >= n2.strVal)

else: i.error(errIncorrect, "Two numbers or two strings are required on the stack") -minsym "==": +minsym "==", i: let n2 = i.pop let n1 = i.pop if (n1.kind == n2.kind or (n1.isNumber and n2.isNumber)) and not n1.isSymbol:

@@ -79,7 +79,7 @@ i.push newVal(n1 == n2)

else: i.error(errIncorrect, "Two non-symbol values of similar type are required") -minsym "!=": +minsym "!=", i: let n2 = i.pop let n1 = i.pop if (n1.kind == n2.kind or (n1.isNumber and n2.isNumber)) and not n1.isSymbol:

@@ -89,14 +89,14 @@ i.error(errIncorrect, "Two non-symbol values of similar type are required")

# Boolean Logic -minsym "not": +minsym "not", i: let b = i.pop if b.isBool: i.push newVal(not b.boolVal) else: i.error(errIncorrect, "A bool value is required on the stack") -minsym "and": +minsym "and", i: let a = i.pop let b = i.pop if a.isBool and b.isBool:

@@ -104,7 +104,7 @@ i.push newVal(a.boolVal and b.boolVal)

else: i.error(errIncorrect, "Two bool values are required on the stack") -minsym "or": +minsym "or", i: let a = i.pop let b = i.pop if a.isBool and b.isBool:

@@ -112,7 +112,7 @@ i.push newVal(a.boolVal or b.boolVal)

else: i.error(errIncorrect, "Two bool values are required on the stack") -minsym "xor": +minsym "xor", i: let a = i.pop let b = i.pop if a.isBool and b.isBool:

@@ -120,37 +120,37 @@ i.push newVal(a.boolVal xor b.boolVal)

else: i.error(errIncorrect, "Two bool values are required on the stack") -minsym "string?": +minsym "string?", i: if i.peek.kind == minString: i.push true.newVal else: i.push false.newVal -minsym "int?": +minsym "int?", i: if i.peek.kind == minInt: i.push true.newVal else: i.push false.newVal -minsym "float?": +minsym "float?", i: if i.peek.kind == minFloat: i.push true.newVal else: i.push false.newVal -minsym "number?": +minsym "number?", i: if i.peek.kind == minFloat or i.peek.kind == minInt: i.push true.newVal else: i.push false.newVal -minsym "bool?": +minsym "bool?", i: if i.peek.kind == minBool: i.push true.newVal else: i.push false.newVal -minsym "quotation?": +minsym "quotation?", i: if i.peek.kind == minQuotation: i.push true.newVal else:
M lib/numbers.nimlib/numbers.nim

@@ -3,7 +3,7 @@ import ../core/interpreter, ../core/utils

# Arithmetic -minsym "+": +minsym "+", i: let a = i.pop let b = i.pop if a.isInt:

@@ -21,7 +21,7 @@ i.push newVal(a.floatVal + b.intVal.float)

else: i.error(errTwoNumbersRequired) -minsym "-": +minsym "-", i: let a = i.pop let b = i.pop if a.isInt:

@@ -39,7 +39,7 @@ i.push newVal(b.intVal.float - a.floatVal)

else: i.error(errTwoNumbersRequired) -minsym "*": +minsym "*", i: let a = i.pop let b = i.pop if a.isInt:

@@ -57,7 +57,7 @@ i.push newVal(a.floatVal * b.intVal.float)

else: i.error(errTwoNumbersRequired) -minsym "/": +minsym "/", i: let a = i.pop let b = i.pop if b.isInt and b.intVal == 0:

@@ -77,7 +77,7 @@ i.push newVal(b.intVal.float / a.floatVal)

else: i.error(errTwoNumbersRequired) -minsym "div": +minsym "div", i: let b = i.pop let a = i.pop if a.isInt and b.isInt:

@@ -85,7 +85,7 @@ i.push(newVal(a.intVal div b.intVal))

else: i.error errIncorrect, "Two integers are required on the stack" -minsym "mod": +minsym "mod", i: let b = i.pop let a = i.pop if a.isInt and b.isInt:
M lib/quotations.nimlib/quotations.nim

@@ -3,18 +3,18 @@ import ../core/parser, ../core/interpreter, ../core/utils

# Operations on quotations -minsym "quote": +minsym "quote", i: let a = i.pop i.push MinValue(kind: minQuotation, qVal: @[a]) -minsym "unquote": +minsym "unquote", i: let q = i.pop if not q.isQuotation: i.error errNoQuotation for item in q.qVal: i.push item -minsym "cons": +minsym "cons", i: var q = i.pop let v = i.pop if not q.isQuotation:

@@ -22,7 +22,7 @@ i.error errNoQuotation

q.qVal.add v i.push q -minsym "at": +minsym "at", i: var index = i.pop var q = i.pop if index.isInt and q.isQuotation:

@@ -30,7 +30,7 @@ i.push q.qVal[index.intVal]

else: i.error errIncorrect, "An integer and a quotation are required on the stack" -minsym "map": +minsym "map", i: let prog = i.pop let list = i.pop if prog.isQuotation and list.isQuotation:

@@ -44,7 +44,7 @@ i.apply("cons")

else: i.error(errIncorrect, "Two quotations are required on the stack") -minsym "times": +minsym "times", i: let t = i.pop let prog = i.pop if t.isInt and prog.isQuotation:

@@ -54,7 +54,7 @@ i.push pitem

else: i.error errIncorrect, "An integer and a quotation are required on the stack" -minsym "ifte": +minsym "ifte", i: let fpath = i.pop let tpath = i.pop let check = i.pop

@@ -70,7 +70,7 @@ i.push fpath.qVal

else: i.error(errIncorrect, "Three quotations are required on the stack") -minsym "while": +minsym "while", i: let d = i.pop let b = i.pop if b.isQuotation and d.isQuotation:

@@ -83,7 +83,7 @@ check = i.pop

else: i.error(errIncorrect, "Two quotations are required on the stack") -minsym "filter": +minsym "filter", i: let filter = i.pop let list = i.pop var res = newSeq[MinValue](0)

@@ -98,7 +98,7 @@ i.push res.newVal

else: i.error(errIncorrect, "Two quotations are required on the stack") -minsym "linrec": +minsym "linrec", i: var r2 = i.pop var r1 = i.pop var t = i.pop
M lib/stack.nimlib/stack.nim

@@ -3,16 +3,16 @@ import ../core/interpreter, ../core/utils

# Common stack operations -minsym "id": +minsym "id", i: discard -minsym "pop": +minsym "pop", i: discard i.pop -minsym "dup": +minsym "dup", i: i.push i.peek -minsym "dip": +minsym "dip", i: let q = i.pop if not q.isQuotation: i.error errNoQuotation

@@ -21,13 +21,13 @@ for item in q.qVal:

i.push item i.push v -minsym "swap": +minsym "swap", i: let a = i.pop let b = i.pop i.push a i.push b -minsym "sip": +minsym "sip", i: let a = i.pop let b = i.pop if a.isQuotation and b.isQuotation:
M lib/strings.nimlib/strings.nim

@@ -2,7 +2,7 @@ import tables, strutils

import ../core/parser, ../core/interpreter, ../core/utils import ../vendor/slre -minsym "split": +minsym "split", i: let sep = i.pop let s = i.pop if s.isString and sep.isString:

@@ -11,7 +11,7 @@ i.push e.newVal

else: i.error errIncorrect, "Two strings are required on the stack" -minsym "match": +minsym "match", i: let reg = i.pop let str = i.pop if str.isString and reg.isString:

@@ -23,7 +23,7 @@ i.push res.newVal

else: i.error(errIncorrect, "Two strings are required on the stack") -minsym "match?": +minsym "match?", i: let reg = i.pop let str = i.pop if str.isString and reg.isString:

@@ -35,7 +35,7 @@ i.push false.newVal

else: i.error(errIncorrect, "Two strings are required on the stack") -minsym "replace": +minsym "replace", i: let s_replace = i.pop let reg = i.pop let s_find = i.pop
M lib/sys.nimlib/sys.nim

@@ -3,10 +3,10 @@ import ../core/parser, ../core/interpreter, ../core/utils

# OS -minsym "pwd": +minsym "pwd", i: i.push newVal(getCurrentDir()) -minsym "cd": +minsym "cd", i: let f = i.pop if f.isString: try:

@@ -16,7 +16,7 @@ warn getCurrentExceptionMsg()

else: i.error errIncorrect, "A string is required on the stack" -minsym "ls": +minsym "ls", i: let a = i.pop var list = newSeq[MinValue](0) if a.isString:

@@ -29,14 +29,14 @@ warn "Directory '$1' not found" % [a.strVal]

else: i.error(errIncorrect, "A string is required on the stack") -minsym "system": +minsym "system", i: let a = i.pop if a.isString: i.push execShellCmd(a.strVal).newVal else: i.error(errIncorrect, "A string is required on the stack") -minsym "run": +minsym "run", i: let a = i.pop if a.isString: let words = a.strVal.split(" ")

@@ -48,14 +48,14 @@ i.push execProcess(cmd, args, nil, {poUsePath}).newVal

else: i.error(errIncorrect, "A string is required on the stack") -minsym "getenv": +minsym "getenv", i: let a = i.pop if a.isString: i.push a.strVal.getEnv.newVal else: i.error(errIncorrect, "A string is required on the stack") -minsym "putenv": +minsym "putenv", i: let value = i.pop let key = i.pop if value.isString and key.isString:

@@ -63,27 +63,27 @@ key.strVal.putEnv value.strVal

else: i.error(errIncorrect, "Two strings are required on the stack") -minsym "os": +minsym "os", i: i.push hostOS.newVal -minsym "cpu": +minsym "cpu", i: i.push hostCPU.newVal -minsym "file?": +minsym "file?", i: let f = i.pop if f.isString: i.push f.strVal.fileExists.newVal else: i.error errIncorrect, "A string is required on the stack" -minsym "dir?": +minsym "dir?", i: let f = i.pop if f.isString: i.push f.strVal.dirExists.newVal else: i.error errIncorrect, "A string is required on the stack" -minsym "rm": +minsym "rm", i: let f = i.pop if f.isString: try:

@@ -93,7 +93,7 @@ warn getCurrentExceptionMsg()

else: i.error errIncorrect, "A string is required on the stack" -minsym "cp": +minsym "cp", i: let b = i.pop let a = i.pop if a.isString and b.isString:

@@ -104,7 +104,7 @@ warn getCurrentExceptionMsg()

else: i.error errIncorrect, "Two strings are required on the stack" -minsym "mv": +minsym "mv", i: let b = i.pop let a = i.pop if a.isString and b.isString:

@@ -115,7 +115,7 @@ warn getCurrentExceptionMsg()

else: i.error errIncorrect, "Two strings are required on the stack" -minsym "rmdir": +minsym "rmdir", i: let f = i.pop if f.isString: try:

@@ -125,7 +125,7 @@ warn getCurrentExceptionMsg()

else: i.error errIncorrect, "A string is required on the stack" -minsym "mkdir": +minsym "mkdir", i: let f = i.pop if f.isString: try:
M lib/time.nimlib/time.nim

@@ -3,9 +3,9 @@ import ../core/interpreter, ../core/utils

# Time -minsym "timestamp": +minsym "timestamp", i: i.push getTime().int.newVal -minsym "now": +minsym "now", i: i.push epochTime().newVal
M minim.nimminim.nim

@@ -20,7 +20,7 @@ var repl = false

const prelude = "lib/prelude.min".slurp.strip const - USE_LINENOISE = (defined(i386) or defined(amd64)) and not defined(windows) + USE_LINENOISE = false #(defined(i386) or defined(amd64))# and not defined(windows) let usage* = " MiNiM v" & version & " - a tiny concatenative system programming language" & """

@@ -36,7 +36,7 @@ Options:

-e, --evaluate Evaluate a minim program inline -h, --help Print this help -v, --version Print the program version - -i, --interactive Starts MiNiM's Read Evel Print Loop""" + -i, --interactive Starts MiNiM's Read Eval Print Loop""" when USE_LINENOISE: import vendor/linenoise

@@ -55,7 +55,7 @@ discard $linenoiseHistoryAdd(res)

return $res else: proc prompt(s: string): string = - stdout.print(s) + stdout.write(s) return stdin.readLine proc minimStream(s: Stream, filename: string) =

@@ -72,14 +72,14 @@

proc minimFile*(filename: string) = var stream = newFileStream(filename, fmRead) if stream == nil: - stderr.writeln("Error - Cannot read from file: "& filename) + stderr.writeLine("Error - Cannot read from file: "& filename) stderr.flushFile() minimStream(stream, filename) proc minimFile*(file: File, filename="stdin") = var stream = newFileStream(stdin) if stream == nil: - stderr.writeln("Error - Cannot read from "& filename) + stderr.writeLine("Error - Cannot read from "& filename) stderr.flushFile() minimStream(stream, filename)

@@ -91,8 +91,8 @@ echo "MiNiM v"&version&" - REPL initialized."

i.eval prelude echo "Prelude loaded." echo "-> Type 'exit' or 'quit' to exit." - if USE_LINENOISE: - discard linenoiseSetCompletionCallback completionCallback + #if USE_LINENOISE: + # discard linenoiseSetCompletionCallback completionCallback var line: string while true: line = prompt(": ")
M vendor/slre.nimvendor/slre.nim

@@ -105,7 +105,7 @@ return newSeq[string](0)

else: raise newException(ValueError, $(rawre.err_str)) -proc gsub*(s_find: string, re: string, s_replace): string = +proc gsub*(s_find: string, re: string, s_replace: string): string = var matches = s_find.match(re) if matches.len > 0: var res = s_find.replace(matches[0], s_replace)