all repos — min @ 4f157a8e003dd068d99a9cf1c95ccea268a2496d

A small but practical concatenative programming language.

Completed MinOperator refactoring.
h3rald h3rald@h3rald.com
Sat, 03 Sep 2016 22:55:21 +0200
commit

4f157a8e003dd068d99a9cf1c95ccea268a2496d

parent

1e8587c3ee815bc80b2eadd97273fe998df9f36c

3 files changed, 45 insertions(+), 34 deletions(-)

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

@@ -157,7 +157,20 @@

proc close*(i: In) = i.parser.close(); -proc push*(i: In, val: MinValue) {.gcsafe.}= +proc push*(i: In, val: MinValue) {.gcsafe.} + +proc execOp*(i: In, op: MinOperator) = + case op.kind + of minProcOp: + op.prc(i) + of minValOp: + if op.val.kind == minQuotation: + for e in op.val.qVal: + i.push e + else: + i.push(op.val) + +proc push*(i: In, val: MinValue) = i.debug val if val.kind == minSymbol: if not i.evaluating:

@@ -166,33 +179,33 @@ let symbol = val.symVal

let sigil = "" & symbol[0] let found = i.scope.hasSymbol(symbol) if found: - let symbolProc = i.scope.getSymbol(symbol).prc # TODO review + let sym = i.scope.getSymbol(symbol) if i.unsafe: let stack = i.copystack try: - symbolProc(i) + i.execOp(sym) except: i.stack = stack raise else: i.execute: - i.symbolProc + i.execOp(sym) else: let found = i.scope.hasSigil(sigil) if symbol.len > 1 and found: - let sigilProc = i.scope.getSigil(sigil).prc # TODO review + let sig = i.scope.getSigil(sigil) let sym = symbol[1..symbol.len-1] i.stack.add(MinValue(kind: minString, strVal: sym)) if i.unsafe: let stack = i.copystack try: - i.sigilProc + i.execOp(sig) except: i.stack = stack raise else: i.execute: - i.sigilProc + i.execOp(sig) else: raiseUndefined("Undefined symbol: '"&val.symVal&"'") else:
M lib/min_lang.nimlib/min_lang.nim

@@ -62,10 +62,10 @@ if not q1.isQuotation:

q1 = @[q1].newVal symbol = sym.getString i.debug "[define] " & symbol & " = " & $q1 - # TODO review - let p = proc(i: In) = - i.push q1.qVal - i.scope.symbols[symbol] = MinOperator(kind: minProcOp, prc: p) + #let p = proc(i: In) = + # i.push q1.qVal + #i.scope.symbols[symbol] = MinOperator(kind: minProcOp, prc: p) + i.scope.symbols[symbol] = MinOperator(kind: minValOp, val: q1) .symbol("bind") do (i: In): var sym, val: MinValue

@@ -76,10 +76,10 @@ if not q1.isQuotation:

q1 = @[q1].newVal symbol = sym.getString i.debug "[bind] " & symbol & " = " & $q1 - # TODO review - let p = proc (i: In) = - i.push q1.qVal - let res = i.scope.setSymbol(symbol, MinOperator(kind: minProcOp, prc: p)) + #let p = proc (i: In) = + # i.push q1.qVal + #let res = i.scope.setSymbol(symbol, MinOperator(kind: minProcOp, prc: p)) + let res = i.scope.setSymbol(symbol, MinOperator(kind: minValOp, val: q1)) if not res: raiseUndefined("Attempting to bind undefined symbol: " & symbol)

@@ -103,17 +103,18 @@ i.reqStringLike name

i.reqQuotation code code.filename = i.filename i.unquote("<module>", code) - # TODO review - let p = proc(i: In) = - i.push code - i.scope.symbols[name.getString] = MinOperator(kind: minProcOp, prc: p) + #let p = proc(i: In) = + # i.push code + #i.scope.symbols[name.getString] = MinOperator(kind: minProcOp, prc: p) + i.scope.symbols[name.getString] = MinOperator(kind: minValOp, val: @[code].newVal) .symbol("import") do (i: In): var mdl, rawName: MinValue var name: string i.reqStringLike rawName name = rawName.getString - i.scope.getSymbol(name).prc(i) # TODO review + #i.scope.getSymbol(name).prc(i) + i.execOp(i.scope.getSymbol(name)) i.reqQuotation mdl if mdl.scope.isNotNil: for sym, val in mdl.scope.symbols.pairs:

@@ -128,12 +129,11 @@ var symbol = q1.qVal[0].symVal

if symbol.len == 1: if i.scope.hasSigil(symbol): raiseInvalid("Sigil '$1' already exists" % [symbol]) - # TODO review - let p = proc(i: In) = - i.evaluating = true - i.push q2.qVal - i.evaluating = false - i.scope.sigils[symbol] = MinOperator(kind: minProcOp, prc: p) + #let p = proc(i: In) = + # i.evaluating = true + # i.push q2.qVal + # i.evaluating = false + i.scope.sigils[symbol] = MinOperator(kind: minValOp, val: q2) else: raiseInvalid("A sigil can only have one character") else:

@@ -166,10 +166,11 @@ i.reqQuotation q

let s = symbol.getString let origScope = i.scope i.scope = q.scope - # TODO review - let sProc = i.scope.getSymbol(s).prc + #let sProc = i.scope.getSymbol(s).prc # Restore original quotation - sProc(i) + #sProc(i) + let sym = i.scope.getSymbol(s) + i.execOp(sym) i.scope = origScope .symbol("inspect") do (i: In):

@@ -485,8 +486,5 @@ i.push("call".newSym)

.sigil("/") do (i: In): i.push("dget".newSym) - - .sigil("-") do (i: In): - i.push("ddel".newSym) .finalize()
M minim.nimminim.nim

@@ -124,14 +124,14 @@ 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) - echo "MiNiM Shell v$1" % version - echo "-> Type 'exit' or 'quit' to exit." line = prompt(": ") if line.isNil: echo "-> Exiting..."