all repos — min @ 00aa7873aeedd7b96937f5fa563d7d1bd92368a3

A small but practical concatenative programming language.

Implemented json serialization support.
h3rald h3rald@h3rald.com
Sat, 16 Jul 2016 13:36:05 +0200
commit

00aa7873aeedd7b96937f5fa563d7d1bd92368a3

parent

7242a0ed9cce30ab96160fed1f7e34d5b49e5b3d

4 files changed, 74 insertions(+), 3 deletions(-)

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

@@ -1,4 +1,4 @@

-import tables, strutils, macros, critbits, httpclient, json, os +import tables, strutils, macros, critbits, httpclient, json, os, regex import types, parser, interpreter proc cfgfile*(): string =

@@ -291,3 +291,48 @@

proc critbit*(o: JsonNode): CritBitTree[string] = for key, value in o.pairs: result[key] = value.getStr + +proc `%`*(a: MinValue): JsonNode = + case a.kind: + of minBool: + return %a.boolVal + of minSymbol: + return %(";sym:$1" % [a.symVal]) + of minString: + return %a.strVal + of minInt: + return %a.intVal + of minFloat: + return %a.floatVal + of minQuotation: + result = newJArray() + for i in a.qVal: + result.add %i + +proc fromJson*(json: JsonNode): MinValue = + case json.kind: + of JNull: + result = newSeq[MinValue](0).newVal + of JBool: + result = json.getBVal.newVal + of JInt: + result = json.getNum.newVal + of JFloat: + result = json.getFNum.newVal + of JString: + let s = json.getStr + if s.match("^;sym:"): + result = regex.replace(s, "^;sym:", "").newSym + else: + result = json.getStr.newVal + of JObject: + var res = newSeq[MinValue](0) + for key, value in json.pairs: + res.add @[key.newSym, value.fromJson].newVal + return res.newVal + of JArray: + var res = newSeq[MinValue](0) + for value in json.items: + res.add value.fromJson + return res.newVal +
M lib/min_lang.nimlib/min_lang.nim

@@ -1,4 +1,4 @@

-import critbits, strutils, os +import critbits, strutils, os, json import ../core/types, ../core/parser,

@@ -34,6 +34,19 @@ q.add s.newVal

scope = scope.parent i.push q.newVal + .symbol("config") do (i: In): + echo cfgfile().readFile + + .symbol("from-json") do (i: In): + var s: MinValue + i.reqString s + i.push s.getString.parseJson.fromJson + + .symbol("to-json") do (i: In): + var q: MinValue + i.reqQuotation q + i.push(($(%q)).newVal) + .symbol("debug?") do (i: In): i.push i.debugging.newVal

@@ -107,6 +120,12 @@ .symbol("type") do (i: In):

var obj: MinValue i.reqObject obj i.push obj.objType.newVal + + .symbol("defines?") do (i: In): + var obj, s: MinValue + i.reqStringLike s + i.reqObject obj + i.push obj.scope.symbols.hasKey(s.getString).newVal .symbol("import") do (i: In): var mdl, rawName: MinValue
M tests/lang.mintests/lang.min

@@ -124,9 +124,17 @@ ((a b +) (4 :a 5 :b) with 9 ==) assert

((4 :four 5 :five) ^myobject type "myobject" ==) assert + ((4 :four 7 :seven) ^myobject2 'seven defines?) assert + (("test" :test) =test1 test1 object?) assert (("test" :test) =test2 test2 module?) assert + + ("{\"a\": 1, \"b\": 2.3}" from-json ((a 1) (b 2.3)) ==) assert + + ((1 2 3 "aaa" 'q q true) to-json "[1,2,3,\"aaa\",\";sym:'q\",\";sym:q\",true]" ==) assert + + ((1 2 3 "aaa" 'q q true) to-json from-json (1 2 3 "aaa" 'q q true) ==) assert report ; Tidy up
M tests/logic.mintests/logic.min

@@ -48,7 +48,6 @@ ("test1" "test2" >= false ==) assert

("test3" "test2" >=) assert ("test2" "test2" >=) assert - (true true ==) assert (false true == false ==) assert (true false == false ==) assert