Fixed deprecation warnings.
@@ -2,16 +2,16 @@ import streams, strutils, tables
import parser, ../vendor/linenoise type - TMinInterpreter* = object - stack*: TMinStack - parser*: TMinParser - currSym: TMinValue + MinInterpreter* = object + stack*: MinStack + parser*: MinParser + currSym: MinValue filename*: string debugging*: bool evaluating*: bool - TMinOperator* = proc (i: var TMinInterpreter) - TMinSigil* = proc (i: var TMinInterpreter, sym: string) - TMinError* = enum + MinOperator* = proc (i: var MinInterpreter) + MinSigil* = proc (i: var MinInterpreter, sym: string) + MinError* = enum errSystem, errParser, errGeneric,@@ -23,7 +23,7 @@ errTwoNumbersRequired,
errDivisionByZero -const ERRORS: array [TMinError, string] = [ +const ERRORS: array [MinError, string] = [ "A system error occurred", "A parsing error occurred", "A generic error occurred",@@ -35,16 +35,16 @@ "Two numbers are required on the stack",
"Division by zero" ] -var SYMBOLS* = initTable[string, TMinOperator]() -var SIGILS* = initTable[string, TMinOperator]() +var SYMBOLS* = initTable[string, MinOperator]() +var SIGILS* = initTable[string, MinOperator]() -proc newMinInterpreter*(debugging = false): TMinInterpreter = - var s:TMinStack = newSeq[TMinValue](0) - var p:TMinParser - var i:TMinInterpreter = TMinInterpreter(filename: "input", parser: p, stack: s, debugging: debugging, currSym: TMinValue(column: 1, line: 1, kind: minSymbol, symVal: "")) +proc newMinInterpreter*(debugging = false): MinInterpreter = + var s:MinStack = newSeq[MinValue](0) + var p:MinParser + var i:MinInterpreter = MinInterpreter(filename: "input", parser: p, stack: s, debugging: debugging, currSym: MinValue(column: 1, line: 1, kind: minSymbol, symVal: "")) return i -proc error*(i: TMinInterpreter, status: TMinError, message = "") = +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])@@ -52,24 +52,24 @@ else:
stderr.writeln("$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 TMinInterpreter, stream:PStream, filename: string) = +proc open*(i: var MinInterpreter, stream:Stream, filename: string) = i.filename = filename i.parser.open(stream, filename) -proc close*(i: var TMinInterpreter) = +proc close*(i: var MinInterpreter) = i.parser.close(); -proc dump*(i: TMinInterpreter): string = +proc dump*(i: MinInterpreter): string = var s = "" for item in i.stack: s = s & $item & " " return s -proc debug(i: var TMinInterpreter, value: TMinValue) = +proc debug(i: var MinInterpreter, value: MinValue) = if i.debugging: stderr.writeln("-- " &i.dump & $value) -proc push*(i: var TMinInterpreter, val: TMinValue) = +proc push*(i: var MinInterpreter, val: MinValue) = i.debug val if val.kind == minSymbol: if not i.evaluating:@@ -85,7 +85,7 @@ else:
if SIGILS.hasKey(sigil) and symbol.len > 1: let sym = symbol[1..symbol.len-1] try: - i.stack.add(TMinValue(kind: minString, strVal: sym)) + i.stack.add(MinValue(kind: minString, strVal: sym)) SIGILS[sigil](i) except: i.error(errSystem, getCurrentExceptionMsg())@@ -94,24 +94,24 @@ i.error(errUndefined, "Undefined symbol: '"&val.symVal&"'")
else: i.stack.add(val) -proc push*(i: var TMinInterpreter, q: seq[TMinValue]) = +proc push*(i: var MinInterpreter, q: seq[MinValue]) = for e in q: i.push e -proc pop*(i: var TMinInterpreter): TMinValue = +proc pop*(i: var MinInterpreter): MinValue = if i.stack.len > 0: return i.stack.pop else: i.error(errEmptyStack) -proc peek*(i: TMinInterpreter): TMinValue = +proc peek*(i: MinInterpreter): MinValue = if i.stack.len > 0: return i.stack[i.stack.len-1] else: i.error(errEmptyStack) -proc interpret*(i: var TMinInterpreter) = - var val: TMinValue +proc interpret*(i: var MinInterpreter) = + var val: MinValue while i.parser.token != tkEof: try: val = i.parser.parseMinValue@@ -119,7 +119,7 @@ except:
i.error errParser, getCurrentExceptionMsg() i.push val -proc eval*(i: var TMinInterpreter, s: string) = +proc eval*(i: var MinInterpreter, s: string) = let fn = i.filename try: i.open(newStringStream(s), "eval")@@ -130,7 +130,7 @@ stderr.writeln getCurrentExceptionMsg()
finally: i.filename = fn -proc load*(i: var TMinInterpreter, s: string) = +proc load*(i: var MinInterpreter, s: string) = let fn = i.filename try: i.open(newStringStream(s.readFile), s)@@ -141,11 +141,11 @@ stderr.writeln getCurrentExceptionMsg()
finally: i.filename = fn -proc apply*(i: var TMinInterpreter, symbol: string) = +proc apply*(i: var MinInterpreter, symbol: string) = SYMBOLS[symbol](i) -proc copystack*(i: var TMinInterpreter): TMinStack = - var s = newSeq[TMinValue](0) +proc copystack*(i: var MinInterpreter): MinStack = + var s = newSeq[MinValue](0) for i in i.stack: s.add i return s
@@ -2,7 +2,7 @@ # Adapted from: https://github.com/Araq/Nimrod/blob/v0.9.6/lib/pure/min.nim
import lexbase, strutils, streams, unicode, tables type - TMinTokenKind* = enum + MinTokenKind* = enum tkError, tkEof, tkString,@@ -13,24 +13,24 @@ tkBracketRi,
tkSymbol, tkTrue, tkFalse - TMinKind* = enum + MinKind* = enum minInt, minFloat, minQuotation, minString, minSymbol, minBool - TMinValue* = object + MinValue* = object line*: int column*: int - case kind*: TMinKind + case kind*: MinKind of minInt: intVal*: int of minFloat: floatVal*: float - of minQuotation: qVal*: seq[TMinValue] + of minQuotation: qVal*: seq[MinValue] of minString: strVal*: string of minSymbol: symVal*: string of minBool: boolVal*: bool - TMinEventKind* = enum ## enumeration of all events that may occur when parsing + MinEventKind* = enum ## enumeration of all events that may occur when parsing eMinError, ## an error ocurred during parsing eMinEof, ## end of file reached eMinString, ## a string literal@@ -38,7 +38,7 @@ eMinInt, ## an integer literal
eMinFloat, ## a float literal eMinQuotationStart, ## start of an array: the ``(`` token eMinQuotationEnd ## start of an array: the ``)`` token - TMinParserError* = enum ## enumeration that lists all errors that can occur + MinParserError* = enum ## enumeration that lists all errors that can occur errNone, ## no error errInvalidToken, ## invalid token errStringExpected, ## string expected@@ -47,24 +47,24 @@ errQuoteExpected, ## ``"`` or ``'`` expected
errEOC_Expected, ## ``*/`` expected errEofExpected, ## EOF expected errExprExpected - TMinParserState = enum + MinParserState = enum stateEof, stateStart, stateQuotation, stateExpectValue - TMinParser* = object of TBaseLexer + MinParser* = object of BaseLexer a*: string - token*: TMinTokenKind - state*: seq[TMinParserState] - kind*: TMinEventKind - err*: TMinParserError + token*: MinTokenKind + state*: seq[MinParserState] + kind*: MinEventKind + err*: MinParserError filename*: string - TMinStack* = seq[TMinValue] - EMinParsingError* = ref object of EInvalidValue - EMinUndefinedError* = ref object of EInvalidValue + MinStack* = seq[MinValue] + EMinParsingError* = ref object of ValueError + EMinUndefinedError* = ref object of ValueError const - errorMessages: array [TMinParserError, string] = [ + errorMessages: array [MinParserError, string] = [ "no error", "invalid token", "string expected",@@ -74,7 +74,7 @@ "'*/' expected",
"EOF expected", "expression expected" ] - tokToStr: array [TMinTokenKind, string] = [ + tokToStr: array [MinTokenKind, string] = [ "invalid token", "EOF", "string literal",@@ -87,59 +87,59 @@ "true",
"false" ] -proc open*(my: var TMinParser, input: PStream, filename: string) = +proc open*(my: var MinParser, input: Stream, filename: string) = lexbase.open(my, input) my.filename = filename my.state = @[stateStart] my.kind = eMinError my.a = "" -proc close*(my: var TMinParser) {.inline.} = +proc close*(my: var MinParser) {.inline.} = lexbase.close(my) -proc getInt*(my: TMinParser): int {.inline.} = +proc getInt*(my: MinParser): int {.inline.} = assert(my.kind == eMinInt) return parseint(my.a) -proc getFloat*(my: TMinParser): float {.inline.} = +proc getFloat*(my: MinParser): float {.inline.} = assert(my.kind == eMinFloat) return parseFloat(my.a) -proc kind*(my: TMinParser): TMinEventKind {.inline.} = +proc kind*(my: MinParser): MinEventKind {.inline.} = return my.kind -proc getColumn*(my: TMinParser): int {.inline.} = +proc getColumn*(my: MinParser): int {.inline.} = result = getColNumber(my, my.bufpos) -proc getLine*(my: TMinParser): int {.inline.} = +proc getLine*(my: MinParser): int {.inline.} = result = my.lineNumber -proc getFilename*(my: TMinParser): string {.inline.} = +proc getFilename*(my: MinParser): string {.inline.} = result = my.filename -proc errorMsg*(my: TMinParser, msg: string): string = +proc errorMsg*(my: MinParser, msg: string): string = assert(my.kind == eMinError) result = "$1 [l:$2, c:$3] ERROR - $4" % [ my.filename, $getLine(my), $getColumn(my), msg] -proc errorMsg*(my: TMinParser): string = +proc errorMsg*(my: MinParser): string = assert(my.kind == eMinError) result = errorMsg(my, errorMessages[my.err]) -proc errorMsgExpected*(my: TMinParser, e: string): string = +proc errorMsgExpected*(my: MinParser, e: string): string = result = errorMsg(my, e & " expected") -proc raiseParseError*(p: TMinParser, msg: string) {.noinline, noreturn.} = +proc raiseParseError*(p: MinParser, msg: string) {.noinline, noreturn.} = raise EMinParsingError(msg: errorMsgExpected(p, msg)) -proc raiseUndefinedError*(p:TMinParser, msg: string) {.noinline, noreturn.} = +proc raiseUndefinedError*(p:MinParser, msg: string) {.noinline, noreturn.} = raise EMinUndefinedError(msg: errorMsg(p, msg)) -#proc error(p: TMinParser, msg: string) = +#proc error(p: MinParser, msg: string) = # writeln(stderr, p.errorMsg(msg)) # flushFile(stderr) -proc parseNumber(my: var TMinParser) = +proc parseNumber(my: var MinParser) = var pos = my.bufpos var buf = my.buf if buf[pos] == '-':@@ -178,7 +178,7 @@ of 'a'..'f': x = (x shl 4) or (ord(c) - ord('a') + 10)
of 'A'..'F': x = (x shl 4) or (ord(c) - ord('A') + 10) else: result = false # error -proc parseString(my: var TMinParser): TMinTokenKind = +proc parseString(my: var MinParser): MinTokenKind = result = tkString var pos = my.bufpos + 1 var buf = my.buf@@ -218,7 +218,7 @@ if handleHexChar(buf[pos], r): inc(pos)
if handleHexChar(buf[pos], r): inc(pos) if handleHexChar(buf[pos], r): inc(pos) if handleHexChar(buf[pos], r): inc(pos) - add(my.a, toUTF8(TRune(r))) + add(my.a, toUTF8(Rune(r))) else: # don't bother with the error add(my.a, buf[pos])@@ -236,7 +236,7 @@ add(my.a, buf[pos])
inc(pos) my.bufpos = pos # store back -proc parseSymbol(my: var TMinParser): TMinTokenKind = +proc parseSymbol(my: var MinParser): MinTokenKind = result = tkSymbol var pos = my.bufpos var buf = my.buf@@ -246,7 +246,7 @@ add(my.a, buf[pos])
inc(pos) my.bufpos = pos -proc skip(my: var TMinParser) = +proc skip(my: var MinParser) = var pos = my.bufpos var buf = my.buf while true:@@ -304,7 +304,7 @@ else:
break my.bufpos = pos -proc getToken*(my: var TMinParser): TMinTokenKind = +proc getToken*(my: var MinParser): MinTokenKind = setLen(my.a, 0) skip(my) case my.buf[my.bufpos]@@ -343,7 +343,7 @@ discard
my.token = result -proc next*(my: var TMinParser) = +proc next*(my: var MinParser) = var tk = getToken(my) var i = my.state.len-1 case my.state[i]@@ -357,7 +357,7 @@ of stateStart:
case tk of tkString, tkInt, tkFloat, tkTrue, tkFalse: my.state[i] = stateEof # expect EOF next! - my.kind = TMinEventKind(ord(tk)) + my.kind = MinEventKind(ord(tk)) of tkBracketLe: my.state.add(stateQuotation) # we expect any my.kind = eMinQuotationStart@@ -369,7 +369,7 @@ my.err = errEofExpected
of stateQuotation: case tk of tkString, tkInt, tkFloat, tkTrue, tkFalse: - my.kind = TMinEventKind(ord(tk)) + my.kind = MinEventKind(ord(tk)) of tkBracketLe: my.state.add(stateQuotation) my.kind = eMinQuotationStart@@ -382,7 +382,7 @@ my.err = errBracketRiExpected
of stateExpectValue: case tk of tkString, tkInt, tkFloat, tkTrue, tkFalse: - my.kind = TMinEventKind(ord(tk)) + my.kind = MinEventKind(ord(tk)) of tkBracketLe: my.state.add(stateQuotation) my.kind = eMinQuotationStart@@ -390,44 +390,44 @@ else:
my.kind = eMinError my.err = errExprExpected -proc eat(p: var TMinParser, token: TMinTokenKind) = +proc eat(p: var MinParser, token: MinTokenKind) = if p.token == token: discard getToken(p) else: raiseParseError(p, tokToStr[token]) -proc parseMinValue*(p: var TMinParser): TMinValue = +proc parseMinValue*(p: var MinParser): MinValue = #echo p.a, " (", p.token, ")" case p.token of tkTrue: - result = TMinValue(kind: minBool, boolVal: true, column: p.getColumn, line: p.lineNumber) + result = MinValue(kind: minBool, boolVal: true, column: p.getColumn, line: p.lineNumber) discard getToken(p) of tkFalse: - result = TMinValue(kind: minBool, boolVal: false, column: p.getColumn, line: p.lineNumber) + result = MinValue(kind: minBool, boolVal: false, column: p.getColumn, line: p.lineNumber) discard getToken(p) of tkString: - result = TMinValue(kind: minString, strVal: p.a, column: p.getColumn, line: p.lineNumber) + result = MinValue(kind: minString, strVal: p.a, column: p.getColumn, line: p.lineNumber) p.a = "" discard getToken(p) of tkInt: - result = TMinValue(kind: minInt, intVal: parseint(p.a), column: p.getColumn, line: p.lineNumber) + result = MinValue(kind: minInt, intVal: parseint(p.a), column: p.getColumn, line: p.lineNumber) discard getToken(p) of tkFloat: - result = TMinValue(kind: minFloat, floatVal: parseFloat(p.a), column: p.getColumn, line: p.lineNumber) + result = MinValue(kind: minFloat, floatVal: parseFloat(p.a), column: p.getColumn, line: p.lineNumber) discard getToken(p) of tkBracketLe: - var q = newSeq[TMinValue](0) + var q = newSeq[MinValue](0) discard getToken(p) while p.token != tkBracketRi: q.add parseMinValue(p) eat(p, tkBracketRi) - result = TMinValue(kind: minQuotation, qVal: q, column: p.getColumn, line: p.lineNumber) + result = MinValue(kind: minQuotation, qVal: q, column: p.getColumn, line: p.lineNumber) of tkSymbol: - result = TMinValue(kind: minSymbol, symVal: p.a, column: p.getColumn, line: p.lineNumber) + result = MinValue(kind: minSymbol, symVal: p.a, column: p.getColumn, line: p.lineNumber) p.a = "" discard getToken(p) else: raiseUndefinedError(p, "Undefined value: '"&p.a&"'") -proc `$`*(a: TMinValue): string = +proc `$`*(a: MinValue): string = case a.kind: of minBool: return $a.boolVal@@ -446,10 +446,10 @@ q = q & $i & " "
q = q & ")" return q -proc print*(a: TMinValue) = +proc print*(a: MinValue) = stdout.write($a) -proc `==`*(a: TMinValue, b: TMinValue): bool = +proc `==`*(a: MinValue, b: MinValue): bool = if a.kind == minInt and b.kind == minInt: return a.intVal == b.intVal elif a.kind == minInt and b.kind == minFloat:
@@ -2,53 +2,53 @@ import tables, strutils
import parser, interpreter template minsym*(name: string, body: stmt): stmt {.immediate.} = - SYMBOLS[name] = proc (i: var TMinInterpreter) = + SYMBOLS[name] = proc (i: var MinInterpreter) = body template minsigil*(name: char, body: stmt): stmt {.immediate.} = - SIGILS[name] = proc (i: var TMinInterpreter) = + SIGILS[name] = proc (i: var MinInterpreter) = body -proc isSymbol*(s: TMinValue): bool = +proc isSymbol*(s: MinValue): bool = return s.kind == minSymbol -proc isQuotation*(s: TMinValue): bool = +proc isQuotation*(s: MinValue): bool = return s.kind == minQuotation -proc isString*(s: TMinValue): bool = +proc isString*(s: MinValue): bool = return s.kind == minString -proc isFloat*(s: TMinValue): bool = +proc isFloat*(s: MinValue): bool = return s.kind == minFloat -proc isInt*(s: TMinValue): bool = +proc isInt*(s: MinValue): bool = return s.kind == minInt -proc isNumber*(s: TMinValue): bool = +proc isNumber*(s: MinValue): bool = return s.kind == minInt or s.kind == minFloat -proc isBool*(s: TMinValue): bool = +proc isBool*(s: MinValue): bool = return s.kind == minBool -proc newVal*(s: string): TMinValue = - return TMinValue(kind: minString, strVal: s) +proc newVal*(s: string): MinValue = + return MinValue(kind: minString, strVal: s) -proc newVal*(q: seq[TMinValue]): TMinValue = - return TMinValue(kind: minQuotation, qVal: q) +proc newVal*(q: seq[MinValue]): MinValue = + return MinValue(kind: minQuotation, qVal: q) -proc newVal*(s: int): TMinValue = - return TMinValue(kind: minInt, intVal: s) +proc newVal*(s: int): MinValue = + return MinValue(kind: minInt, intVal: s) -proc newVal*(s: float): TMinValue = - return TMinValue(kind: minFloat, floatVal: s) +proc newVal*(s: float): MinValue = + return MinValue(kind: minFloat, floatVal: s) -proc newVal*(s: bool): TMinValue = - return TMinValue(kind: minBool, boolVal: s) +proc newVal*(s: bool): MinValue = + return MinValue(kind: minBool, boolVal: s) proc warn*(s: string) = stderr.writeln s -proc linrec*(i: var TMinInterpreter, p, t, r1, r2: TMinValue) = +proc linrec*(i: var MinInterpreter, p, t, r1, r2: MinValue) = i.push p.qVal var check = i.pop if check.isBool and check.boolVal == true:
@@ -5,13 +5,13 @@ minsym "exit":
quit(0) minsym "symbols": - var q = newSeq[TMinValue](0) + var q = newSeq[MinValue](0) for s in SYMBOLS.keys: q.add s.newVal i.push q.newVal minsym "sigils": - var q = newSeq[TMinValue](0) + var q = newSeq[MinValue](0) for s in SIGILS.keys: q.add s.newVal i.push q.newVal@@ -53,7 +53,7 @@ else:
i.error errIncorrect, "The top quotation must contain only one symbol value" minsigil "'": - i.push(@[TMinValue(kind: minSymbol, symVal: i.pop.strVal)].newVal) + i.push(@[MinValue(kind: minSymbol, symVal: i.pop.strVal)].newVal) minsym "sigil": var q1 = i.pop
@@ -5,7 +5,7 @@ # Operations on quotations
minsym "quote": let a = i.pop - i.push TMinValue(kind: minQuotation, qVal: @[a]) + i.push MinValue(kind: minQuotation, qVal: @[a]) minsym "unquote": let q = i.pop@@ -34,7 +34,7 @@ minsym "map":
let prog = i.pop let list = i.pop if prog.isQuotation and list.isQuotation: - i.push newVal(newSeq[TMinValue](0)) + i.push newVal(newSeq[MinValue](0)) for litem in list.qVal: i.push litem for pitem in prog.qVal:@@ -86,7 +86,7 @@
minsym "filter": let filter = i.pop let list = i.pop - var res = newSeq[TMinValue](0) + var res = newSeq[MinValue](0) if filter.isQuotation and list.isQuotation: for e in list.qVal: i.push e
@@ -16,7 +16,7 @@ let reg = i.pop
let str = i.pop if str.isString and reg.isString: var matches = str.strVal.match(reg.strVal) - var res = newSeq[TMinValue](0) + var res = newSeq[MinValue](0) for s in matches: res.add s.newVal i.push res.newVal
@@ -18,7 +18,7 @@ i.error errIncorrect, "A string is required on the stack"
minsym "ls": let a = i.pop - var list = newSeq[TMinValue](0) + var list = newSeq[MinValue](0) if a.isString: if a.strVal.existsDir: for i in walkdir(a.strVal):
@@ -58,7 +58,7 @@ proc prompt(s: string): string =
stdout.print(s) return stdin.readLine -proc minimStream(s: PStream, filename: string) = +proc minimStream(s: Stream, filename: string) = var i = newMinInterpreter(debugging) i.eval prelude i.open(s, filename)@@ -76,7 +76,7 @@ stderr.writeln("Error - Cannot read from file: "& filename)
stderr.flushFile() minimStream(stream, filename) -proc minimFile*(file: TFile, filename="stdin") = +proc minimFile*(file: File, filename="stdin") = var stream = newFileStream(stdin) if stream == nil: stderr.writeln("Error - Cannot read from "& filename)@@ -128,6 +128,8 @@ of "version", "v":
echo version of "interactive", "i": repl = true + else: + discard else: discard
@@ -103,7 +103,7 @@ return res
else: return newSeq[string](0) else: - raise newException(EInvalidValue, $(rawre.err_str)) + raise newException(ValueError, $(rawre.err_str)) proc gsub*(s_find: string, re: string, s_replace): string = var matches = s_find.match(re)