Implemented object-related operators.
h3rald h3rald@h3rald.com
Sun, 26 Jun 2016 19:11:06 +0200
5 files changed,
50 insertions(+),
3 deletions(-)
M
core/utils.nim
→
core/utils.nim
@@ -28,6 +28,9 @@
proc isObject*(a: MinValue, t: string): bool = return a.isQuotation and not a.objType.isNil and a.objType == t +proc isObject*(a: MinValue): bool = + return a.isQuotation and not a.objType.isNil + proc newVal*(s: string): MinValue = return MinValue(kind: minString, strVal: s)@@ -229,4 +232,9 @@
proc reqObject*(i: var MinInterpreter, t: string, a: var MinValue) = a = i.pop if not a.isObject(t): - raiseInvalid("A $1 object is required" % [t]) + raiseInvalid("An object of type $1 is required on the stack" % [t]) + +proc reqObject*(i: var MinInterpreter, a: var MinValue) = + a = i.pop + if not a.isObject: + raiseInvalid("An object is required on the stack")
M
lib/min_lang.nim
→
lib/min_lang.nim
@@ -100,6 +100,20 @@ i.unquote("<module>", code)
i.scope.symbols[name.getString] = proc(i: In) = i.push code + .symbol("object") do (i: In): + var code, t: MinValue + i.reqStringLike t + i.reqQuotation code + code.filename = i.filename + code.objType = t.getString + i.unquote("<object>", code) + i.push code + + .symbol("type") do (i: In): + var obj: MinValue + i.reqObject obj + i.push obj.objType.newVal + .symbol("import") do (i: In): var mdl, rawName: MinValue var name: string
M
lib/min_logic.nim
→
lib/min_logic.nim
@@ -137,4 +137,22 @@ i.push true.newVal
else: i.push false.newVal + .symbol("object?") do (i: In): + if i.peek.isObject: + i.push true.newVal + else: + i.push false.newVal + + .symbol("module?") do (i: In): + if i.peek.isObject and i.peek.objType == "module": + i.push true.newVal + else: + i.push false.newVal + + .symbol("scope?") do (i: In): + if i.peek.isObject and i.peek.objType == "scope": + i.push true.newVal + else: + i.push false.newVal + .finalize()
M
lib/prelude.min
→
lib/prelude.min
@@ -20,6 +20,7 @@ (run) (&) sigil
(load) (@) sigil (call) (%) sigil (module) (=) sigil +(object) (^) sigil ; Aliases 'define ::
M
tests/lang.min
→
tests/lang.min
@@ -7,9 +7,9 @@
"Total Symbols: " print! symbols size put! " Total Sigils: " print! sigils size put! - (symbols size 176 ==) assert + (symbols size 181 ==) assert - (sigils size 11 ==) assert + (sigils size 12 ==) assert (debug? false ==) assert@@ -122,6 +122,12 @@ (9 factorial)
) ->> (40320 479001600 362880) ==) assert ((a b +) (4 :a 5 :b) with 9 ==) assert + + ((4 :four 5 :five) ^myobject type "myobject" ==) assert + + (("test" :test) =test1 test1 object?) assert + + (("test" :test) =test2 test2 module?) assert report ; Tidy up