all repos — min @ 348fc9a09c3c5d63115719799a132645dd2e953d

A small but practical concatenative programming language.

refactor(isNotNil) Added isNotNil proc, refactoring.
h3rald h3rald@h3rald.com
Sat, 04 Jun 2016 16:30:10 +0200
commit

348fc9a09c3c5d63115719799a132645dd2e953d

parent

22a43dc378092f7d1e3c9597afa9b11738867062

4 files changed, 30 insertions(+), 15 deletions(-)

jump to
M core/interpreter.nimcore/interpreter.nim

@@ -23,13 +23,13 @@ ROOT.name = "ROOT"

proc fullname*(scope: ref MinScope): string = result = scope.name - if not scope.parent.isNil: + if scope.parent.isNotNil: result = scope.parent.fullname & ":" & result proc getSymbol*(scope: ref MinScope, key: string): MinOperator = if scope.symbols.hasKey(key): return scope.symbols[key] - elif not scope.parent.isNil: + elif scope.parent.isNotNil: return scope.parent.getSymbol(key) proc delSymbol*(scope: ref MinScope, key: string): bool {.discardable.}=

@@ -41,18 +41,18 @@

proc setSymbol*(scope: ref MinScope, key: string, value: MinOperator): bool {.discardable.}= result = false # check if a symbol already exists in current scope - if not scope.isNil and scope.symbols.hasKey(key): + if scope.isNotNil and scope.symbols.hasKey(key): scope.symbols[key] = value result = true else: # Go up the scope chain and attempt to find the symbol - if not scope.parent.isNil: + if scope.parent.isNotNil: result = scope.parent.setSymbol(key, value) proc getSigil*(scope: ref MinScope, key: string): MinOperator = if scope.sigils.hasKey(key): return scope.sigils[key] - elif not scope.parent.isNil: + elif scope.parent.isNotNil: return scope.parent.getSigil(key) proc dump*(i: MinInterpreter): string =

@@ -126,7 +126,7 @@ i.currSym = val

let symbol = val.symVal let sigil = "" & symbol[0] let symbolProc = i.scope.getSymbol(symbol) - if not symbolProc.isNil: + if symbolProc.isNotNil: if i.unsafe: symbolProc(i) else:

@@ -138,7 +138,7 @@ except:

i.error(errSystem, getCurrentExceptionMsg()) else: let sigilProc = i.scope.getSigil(sigil) - if symbol.len > 1 and not sigilProc.isNil: + if symbol.len > 1 and sigilProc.isNotNil: let sym = symbol[1..symbol.len-1] i.stack.add(MinValue(kind: minString, strVal: sym)) if i.unsafe:

@@ -185,7 +185,6 @@ proc unquote*(i: In, name: string, q: var MinValue) =

i.newScope(name, q): for v in q.qVal: i.push v - proc eval*(i: var MinInterpreter, s: string) = let fn = i.filename
M core/parser.nimcore/parser.nim

@@ -454,6 +454,3 @@ else:

return false else: return false - -proc isNil*(a: MinValue): bool = - return a.kind != minQuotation and a.kind != minString and a.kind != minFloat and a.kind != minInt and a.kind != minBool and a.kind != minSymbol
M core/types.nimcore/types.nim

@@ -96,3 +96,7 @@ errIncorrect,

errRuntime, errTwoNumbersRequired, errDivisionByZero + +proc isNotNil*[T](obj: T): bool = + return not obj.isNil +
M lib/lang.nimlib/lang.nim

@@ -6,6 +6,8 @@ ../core/interpreter,

../core/utils, ../core/regex +var TIN {.threadvar.}: MinInterpreter + ROOT .symbol("exit") do (i: In):

@@ -14,7 +16,7 @@

.symbol("symbols") do (i: In): var q = newSeq[MinValue](0) var scope = i.scope - while not scope.isNil: + while scope.isNotNil: for s in scope.symbols.keys: q.add s.newVal scope = scope.parent

@@ -23,7 +25,7 @@

.symbol("sigils") do (i: In): var q = newSeq[MinValue](0) var scope = i.scope - while not scope.isNil: + while scope.isNotNil: for s in scope.sigils.keys: q.add s.newVal scope = scope.parent

@@ -102,7 +104,7 @@ except:

echo getCurrentExceptionMsg() if not mdl.isQuotation: i.error errNoQuotation - if not mdl.scope.isNil: + if mdl.scope.isNotNil: #echo "MODULE SCOPE PARENT: ", mdl.scope.name for sym, val in mdl.scope.symbols.pairs: i.debug "[import] $1:$2" % [i.scope.name, sym]

@@ -120,7 +122,7 @@ if q1.isQuotation and q2.isQuotation:

if q1.qVal.len == 1 and q1.qVal[0].kind == minSymbol: var symbol = q1.qVal[0].symVal if symbol.len == 1: - if not i.scope.getSigil(symbol).isNil: + if i.scope.getSigil(symbol).isNotNil: i.error errSystem, "Sigil '$1' already exists" % [symbol] return i.scope.sigils[symbol] = proc(i: var MinInterpreter) =

@@ -200,6 +202,7 @@ .symbol("try") do (i: In):

var prog = i.pop if not prog.isQuotation: i.error errNoQuotation + return if prog.qVal.len < 2: i.error errIncorrect, "Quotation must contain at least two elements" return

@@ -246,6 +249,7 @@ .symbol("setstack") do (i: In):

let q = i.pop if not q.isQuotation: i.error errNoQuotation + return i.stack = q.qVal # Operations on quotations or strings

@@ -261,6 +265,7 @@ let q = q2.qVal & q1.qVal

i.push newVal(q) else: i.error(errIncorrect, "Two quotations or two strings are required on the stack") + return .symbol("first") do (i: In): var q = i.pop

@@ -270,6 +275,7 @@ elif q.isString:

i.push newVal($q.strVal[0]) else: i.error(errIncorrect, "A quotation or a string is required on the stack") + return .symbol("rest") do (i: In): var q = i.pop

@@ -279,6 +285,7 @@ elif q.isString:

i.push newVal(q.strVal[1..q.strVal.len-1]) else: i.error(errIncorrect, "A quotation or a string is required on the stack") + return .symbol("quote") do (i: In): let a = i.pop

@@ -316,6 +323,7 @@ if index.isInt and q.isQuotation:

i.push q.qVal[index.intVal] else: i.error errIncorrect, "An integer and a quotation are required on the stack" + return .symbol("size") do (i: In): let q = i.pop

@@ -325,6 +333,7 @@ elif q.isString:

i.push q.strVal.len.newVal else: i.error(errIncorrect, "A quotation or a string is required on the stack") + return .symbol("contains") do (i: In): let v = i.pop

@@ -346,6 +355,7 @@ i.apply("swap")

i.apply("append") else: i.error(errIncorrect, "Two quotations are required on the stack") + return .symbol("times") do (i: In): let t = i.pop

@@ -355,6 +365,7 @@ for c in 1..t.intVal:

i.unquote("<times-quotation>", prog) else: i.error errIncorrect, "An integer and a quotation are required on the stack" + return .symbol("ifte") do (i: In): var fpath = i.pop

@@ -371,6 +382,7 @@ else:

i.unquote("<ifte-false>", fpath) else: i.error(errIncorrect, "Three quotations are required on the stack") + return .symbol("while") do (i: In): var d = i.pop

@@ -385,6 +397,7 @@ i.unquote("<while-check>", b)

check = i.pop else: i.error(errIncorrect, "Two quotations are required on the stack") + return .symbol("filter") do (i: In): var filter = i.pop

@@ -400,6 +413,7 @@ res.add e

i.push res.newVal else: i.error(errIncorrect, "Two quotations are required on the stack") + return .symbol("linrec") do (i: In): var r2 = i.pop

@@ -419,5 +433,6 @@ i.unquote("<linrec-r2>", r2)

i.linrec(p, t, r1, r2) else: i.error(errIncorrect, "Four quotations are required on the stack") + return .finalize()