From cebe5c23a6714d842f76eb2ff43ff0cfe3400d56 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Sat, 5 Dec 2020 18:15:59 -0700 Subject: [PATCH] anonymous objects with (object...) --- src/kiss/SpecialForms.hx | 20 +++++++++++++++++++- src/test/cases/BasicTestCase.hx | 4 ++++ src/test/cases/BasicTestCase.kiss | 10 +++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/kiss/SpecialForms.hx b/src/kiss/SpecialForms.hx index ef8409d2..ce878cd8 100644 --- a/src/kiss/SpecialForms.hx +++ b/src/kiss/SpecialForms.hx @@ -57,7 +57,25 @@ class SpecialForms { // TODO rest - // TODO special form for object declaration + // Declare anonymous objects + map["object"] = (wholeExp:ReaderExp, args:Array, k:KissState) -> { + if (args.length % 2 != 0) { + throw CompileError.fromExp(wholeExp, "(object [field bindings]...) must have an even number of arguments"); + } + EObjectDecl([ + for (pair in args.groups(2)) + { + quotes: Unquoted, + field: switch (pair[0].def) { + case Symbol(name): name; + case TypedExp(_, + {pos: _, def: Symbol(_)}): throw CompileError.fromExp(pair[0], "type specification on anonymous objects will be ignored"); + default: throw CompileError.fromExp(pair[0], "first expression in anonymous object field binding should be a plain symbol"); + }, + expr: k.convert(pair[1]) + } + ]).withMacroPosOf(wholeExp); + }; map["new"] = (wholeExp:ReaderExp, args:Array, k:KissState) -> { wholeExp.checkNumArgs(1, null, '(new [type] [constructorArgs...])'); diff --git a/src/test/cases/BasicTestCase.hx b/src/test/cases/BasicTestCase.hx index 293e0ada..2880f823 100644 --- a/src/test/cases/BasicTestCase.hx +++ b/src/test/cases/BasicTestCase.hx @@ -230,6 +230,10 @@ class BasicTestCase extends Test { Assert.equals(30, applyWithMethod(new BasicObject(5))); Assert.equals(18, applyWithMethod(new BasicObject(3))); } + + function testAnonymousObject() { + _testAnonymousObject(); + } } class BasicObject { diff --git a/src/test/cases/BasicTestCase.kiss b/src/test/cases/BasicTestCase.kiss index dcac8eb6..3aca019f 100644 --- a/src/test/cases/BasicTestCase.kiss +++ b/src/test/cases/BasicTestCase.kiss @@ -308,4 +308,12 @@ (Assert.equals 6 (apply + [1 2 3]))) (defun applyWithMethod [obj] - (apply .multiply obj [6])) \ No newline at end of file + (apply .multiply obj [6])) + +(defun _testAnonymousObject [] + (let [obj + (object + a "string A" + b 5)] + (Assert.equals "string A" obj.a) + (Assert.equals 5 obj.b))) \ No newline at end of file