all repos — min @ 1e8587c3ee815bc80b2eadd97273fe998df9f36c

A small but practical concatenative programming language.

Started refactoring using new MinOperator type.
h3rald h3rald@h3rald.com
Sat, 03 Sep 2016 22:13:13 +0200
commit

1e8587c3ee815bc80b2eadd97273fe998df9f36c

parent

6ddcd55cb718debbbb262b862847609e97eabbe8

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

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

@@ -166,7 +166,7 @@ let symbol = val.symVal

let sigil = "" & symbol[0] let found = i.scope.hasSymbol(symbol) if found: - let symbolProc = i.scope.getSymbol(symbol) + let symbolProc = i.scope.getSymbol(symbol).prc # TODO review if i.unsafe: let stack = i.copystack try:

@@ -180,7 +180,7 @@ i.symbolProc

else: let found = i.scope.hasSigil(sigil) if symbol.len > 1 and found: - let sigilProc = i.scope.getSigil(sigil) + let sigilProc = i.scope.getSigil(sigil).prc # TODO review let sym = symbol[1..symbol.len-1] i.stack.add(MinValue(kind: minString, strVal: sym)) if i.unsafe:

@@ -255,4 +255,5 @@ finally:

i.filename = fn proc apply*(i: In, symbol: string) = - i.scope.getSymbol(symbol)(i) + # TODO review + i.scope.getSymbol(symbol).prc(i)
M core/types.nimcore/types.nim

@@ -80,7 +80,16 @@ evaluating*: bool

unsafe*: bool In* = var MinInterpreter Val* = var MinValue - MinOperator* = proc (i: In) {.closure.} + MinOperatorProc* = proc (i: In) {.closure.} + MinOperatorKind* = enum + minProcOp + minValOp + MinOperator* = object + case kind*: MinOperatorKind + of minProcOp: + prc*: MinOperatorProc + of minValOp: + val*: MinValue MinSigil* = proc (i: In, sym: string) MinParsingError* = ref object of ValueError MinUndefinedError* = ref object of ValueError
M core/utils.nimcore/utils.nim

@@ -218,21 +218,30 @@ scope.name = name

scope.parent = i.scope return scope -proc symbol*(scope: ref MinScope, sym: string, p: MinOperator): ref MinScope = - scope.symbols[sym] = p +proc symbol*(scope: ref MinScope, sym: string, p: MinOperatorProc): ref MinScope = + scope.symbols[sym] = MinOperator(prc: p, kind: minProcOp) + return scope + +proc symbol*(scope: ref MinScope, sym: string, v: MinValue): ref MinScope = + scope.symbols[sym] = MinOperator(val: v, kind: minValOp) + return scope + +proc sigil*(scope: ref MinScope, sym: string, p: MinOperatorProc): ref MinScope = + scope.previous.sigils[sym] = MinOperator(prc: p, kind: minProcOp) return scope -proc sigil*(scope: ref MinScope, sym: string, p: MinOperator): ref MinScope = - scope.previous.sigils[sym] = p +proc sigil*(scope: ref MinScope, sym: string, v: MinValue): ref MinScope = + scope.previous.sigils[sym] = MinOperator(val: v, kind: minValOp) return scope proc finalize*(scope: ref MinScope) = var mdl = newSeq[MinValue](0).newVal mdl.scope = scope - mdl.scope.previous.symbols[scope.name] = proc(i: In) {.gcsafe, closure.} = + let op = proc(i: In) {.gcsafe, closure.} = i.evaluating = true i.push mdl i.evaluating = false + mdl.scope.previous.symbols[scope.name] = MinOperator(kind: minProcOp, prc: op) template alias*[T](varname: untyped, value: var T) = var varname {.inject.}: type(value)
M lib/min_lang.nimlib/min_lang.nim

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

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

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

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

@@ -99,15 +103,17 @@ i.reqStringLike name

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

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

if symbol.len == 1: if i.scope.hasSigil(symbol): raiseInvalid("Sigil '$1' already exists" % [symbol]) - i.scope.sigils[symbol] = proc(i: In) = + # 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) else: raiseInvalid("A sigil can only have one character") else:

@@ -158,7 +166,8 @@ i.reqQuotation q

let s = symbol.getString let origScope = i.scope i.scope = q.scope - let sProc = i.scope.getSymbol(s) + # TODO review + let sProc = i.scope.getSymbol(s).prc # Restore original quotation sProc(i) i.scope = origScope