anonymous objects with (object...)

This commit is contained in:
2020-12-05 18:15:59 -07:00
parent 15f281d0ee
commit cebe5c23a6
3 changed files with 32 additions and 2 deletions

View File

@@ -57,7 +57,25 @@ class SpecialForms {
// TODO rest
// TODO special form for object declaration
// Declare anonymous objects
map["object"] = (wholeExp:ReaderExp, args:Array<ReaderExp>, 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<ReaderExp>, k:KissState) -> {
wholeExp.checkNumArgs(1, null, '(new [type] [constructorArgs...])');

View File

@@ -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 {

View File

@@ -308,4 +308,12 @@
(Assert.equals 6 (apply + [1 2 3])))
(defun applyWithMethod [obj]
(apply .multiply obj [6]))
(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)))