feat(modules) Implemented module definition and import.
h3rald h3rald@h3rald.com
Sat, 21 May 2016 21:02:27 +0200
4 files changed,
47 insertions(+),
1 deletions(-)
M
core/interpreter.nim
→
core/interpreter.nim
@@ -32,11 +32,14 @@ return scope.parent.getSigil(key)
proc newMinInterpreter*(debugging = false): MinInterpreter = var st:MinStack = newSeq[MinValue](0) + #var scope: ref MinScope = new MinScope + #scope.parent = ROOT var pr:MinParser var i:MinInterpreter = MinInterpreter( filename: "input", parser: pr, stack: st, + #scope: scope, scope: ROOT, debugging: debugging, currSym: MinValue(column: 1, line: 1, kind: minSymbol, symVal: "")
M
core/types.nim
→
core/types.nim
@@ -32,7 +32,7 @@ of minInt: intVal*: int
of minFloat: floatVal*: float of minQuotation: qVal*: seq[MinValue] - scope: MinScope + scope*: ref MinScope of minString: strVal*: string of minSymbol: symVal*: string of minBool: boolVal*: bool
M
lib/lang.nim
→
lib/lang.nim
@@ -56,6 +56,44 @@ i.scope.symbols.excl symbol
else: i.error errIncorrect, "The top quotation must contain only one symbol value" +minsym "define", i: + let name = i.pop + var code = i.pop + if not name.isString or not code.isQuotation: + i.error(errIncorrect, "A string and a quotation are require on the stack") + let id = name.strVal + let scope = i.scope + let stack = i.copystack + i.scope = new MinScope + code.scope = i.scope + i.scope.parent = scope + for item in code.qVal: + i.push item + let p = proc(i: var MinInterpreter) = + i.evaluating = true + i.push code + i.evaluating = false + let symbols = i.scope.symbols + i.scope = scope + i.scope.symbols[id] = p + # Define symbols in parent scope as well + for sym, val in symbols.pairs: + i.scope.symbols[id & ":" & sym] = val + i.stack = stack + +minsym "import", i: + var mdl: MinValue + try: + i.scope.getSymbol(i.pop.strVal)(i) + mdl = i.pop + except: + discard + if not mdl.isQuotation: + i.error errNoQuotation + if not mdl.scope.isNil: + for sym, val in mdl.scope.symbols.pairs: + i.scope.symbols[sym] = val + minsigil "'", i: i.push(@[MinValue(kind: minSymbol, symVal: i.pop.strVal)].newVal)
M
lib/prelude.min
→
lib/prelude.min
@@ -3,9 +3,14 @@ (bind) (:) sigil
(getenv) ($) sigil (system) (!) sigil (run) (&) sigil +(import) (#) sigil +(define) (=) sigil +(load) (@) sigil // Aliases 'bind :: +'define := +'import :# 'exit :quit 'concat :, 'print :%