Added sealed symbols; Added . and .. symbols.
h3rald h3rald@h3rald.com
Sun, 04 Sep 2016 13:28:00 +0200
7 files changed,
30 insertions(+),
11 deletions(-)
M
core/interpreter.nim
→
core/interpreter.nim
@@ -9,6 +9,9 @@
proc raiseEmptyStack() = raise MinEmptyStackError(msg:"Insufficient items on the stack") +proc raiseInvalid(msg: string) = + raise MinInvalidError(msg: msg) + proc fullname*(scope: ref MinScope): string = result = scope.name if scope.parent.isNotNil:@@ -40,6 +43,8 @@ proc setSymbol*(scope: ref MinScope, key: string, value: MinOperator): bool {.discardable.}=
result = false # check if a symbol already exists in current scope if scope.isNotNil and scope.symbols.hasKey(key): + if scope.symbols[key].sealed: + raiseInvalid("Symbol '$1' is sealed." % key) scope.symbols[key] = value result = true else:
M
core/types.nim
→
core/types.nim
@@ -85,6 +85,7 @@ MinOperatorKind* = enum
minProcOp minValOp MinOperator* = object + sealed*: bool case kind*: MinOperatorKind of minProcOp: prc*: MinOperatorProc
M
core/utils.nim
→
core/utils.nim
@@ -219,19 +219,19 @@ scope.parent = i.scope
return scope proc symbol*(scope: ref MinScope, sym: string, p: MinOperatorProc): ref MinScope = - scope.symbols[sym] = MinOperator(prc: p, kind: minProcOp) + scope.symbols[sym] = MinOperator(prc: p, kind: minProcOp, sealed: true) return scope proc symbol*(scope: ref MinScope, sym: string, v: MinValue): ref MinScope = - scope.symbols[sym] = MinOperator(val: v, kind: minValOp) + scope.symbols[sym] = MinOperator(val: v, kind: minValOp, sealed: true) return scope proc sigil*(scope: ref MinScope, sym: string, p: MinOperatorProc): ref MinScope = - scope.previous.sigils[sym] = MinOperator(prc: p, kind: minProcOp) + scope.previous.sigils[sym] = MinOperator(prc: p, kind: minProcOp, sealed: true) return scope proc sigil*(scope: ref MinScope, sym: string, v: MinValue): ref MinScope = - scope.previous.sigils[sym] = MinOperator(val: v, kind: minValOp) + scope.previous.sigils[sym] = MinOperator(val: v, kind: minValOp, sealed: true) return scope proc finalize*(scope: ref MinScope) =
M
lib/min_lang.nim
→
lib/min_lang.nim
@@ -65,7 +65,9 @@ i.debug "[define] " & symbol & " = " & $q1
#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) + if i.scope.symbols.hasKey(symbol) and i.scope.symbols[symbol].sealed: + raiseUndefined("Attempting to redefined sealed symbol '$1'" % symbol) + i.scope.symbols[symbol] = MinOperator(kind: minValOp, val: q1, sealed: false) .symbol("bind") do (i: In): var sym, val: MinValue
M
lib/min_sys.nim
→
lib/min_sys.nim
@@ -11,8 +11,11 @@
proc sys_module*(i: In)= i.define("sys") - .symbol("pwd") do (i: In): + .symbol(".") do (i: In): i.push newVal(getCurrentDir()) + + .symbol("..") do (i: In): + i.push newVal(getCurrentDir().parentDir) .symbol("cd") do (i: In): var f: MinValue
M
tests/lang.min
→
tests/lang.min
@@ -94,6 +94,8 @@ (first)
("Caught a " swap concat) ) try "Caught a MinEmptyStackError" ==) assert + ("aaaa" :. . "aaaa" ==) assert ;It is possible to shadow sealed symbols in child scopes + ( ( (("TestError" "Test Message") raise)@@ -130,6 +132,12 @@
(((a 1)(b 2)(c 3)) values (1 2 3) ==) assert ("$1 - $2 - $3" (1 true "test") interpolate "1 - true - test" ==) assert + + (((1 2 3)) :sym1 >sym1 stored-symbols "sym1" contains) assert + + (<sym1 symbols "sym1" contains) assert + + ('sym1 remove-symbol stored-symbols "sym1" contains false ==) assert report ; Tidy up
M
tests/sys.min
→
tests/sys.min
@@ -3,10 +3,10 @@ 'test import
"sys" describe - ("systest" mkdir "." ls "./systest" contains) assert + ("systest" mkdir . ls . "/systest" concat contains) assert - ("systest" cd pwd "systest" match) assert - ".." cd + ("systest" cd . "systest" match) assert + .. cd (&ls "\n" split "systest" contains) assert@@ -28,9 +28,9 @@ ("test.txt" "test2.txt" cp "test2.txt" file?) assert
("test.txt" "test1.txt" mv "test1.txt" file?) assert - ("test2.txt" rm "test1.txt" rm pwd ls "test1.txt" contains :t1 pwd ls "test2" contains t1 and false ==) assert + ("test2.txt" rm "test1.txt" rm . ls "test1.txt" contains :t1 . ls "test2" contains t1 and false ==) assert - ("systest" rmdir pwd ls "systest" contains false ==) assert + ("systest" rmdir . ls "systest" contains false ==) assert report clear-stack