From 0eed708ca683461b1c731f5f6a1539fb2ae38a91 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Thu, 6 Oct 2022 01:21:10 +0000 Subject: [PATCH] (extends) and (implements) for pure-kiss classes --- src/kiss/SpecialForms.hx | 27 +++++++++++++++++++++++++++ src/test/BasicInterface.hx | 5 +++++ src/test/PureKissClass.kiss | 12 ++++++++++-- 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 src/test/BasicInterface.hx diff --git a/src/kiss/SpecialForms.hx b/src/kiss/SpecialForms.hx index 16ea0eb..fc9bf87 100644 --- a/src/kiss/SpecialForms.hx +++ b/src/kiss/SpecialForms.hx @@ -580,6 +580,33 @@ class SpecialForms { return none(wholeExp); }; + k.doc("extends", 1, 1, "(extends )"); + map["extends"] = (wholeExp:ReaderExp, exps:Array, k:KissState) -> { + requireContext(wholeExp, "extends"); + var type = context.getType(); + type.kind = switch (type.kind) { + case TDClass(null, interfaces, false, false, false): + TDClass(Reader.toString(exps[0].def).asTypePath(), interfaces, false, false, false); + default: + throw KissError.fromExp(wholeExp, '${type.name} must be a class without a superclass'); + } + return none(wholeExp); + }; + + k.doc("implements", 1, null, "(implements )"); + map["implements"] = (wholeExp:ReaderExp, exps:Array, k:KissState) -> { + requireContext(wholeExp, "implements"); + var type = context.getType(); + var interfaces = [for (exp in exps) Reader.toString(exp.def).asTypePath()]; + type.kind = switch (type.kind) { + case TDClass(superClass, [], false, false, false): + TDClass(superClass, interfaces, false, false, false); + default: + throw KissError.fromExp(wholeExp, '${type.name} must be a class without any interfaces'); + } + return none(wholeExp); + }; + return map; } diff --git a/src/test/BasicInterface.hx b/src/test/BasicInterface.hx new file mode 100644 index 0000000..0684fdb --- /dev/null +++ b/src/test/BasicInterface.hx @@ -0,0 +1,5 @@ +package test; + +interface BasicInterface { + public var otherNum(default,null):Int; +} \ No newline at end of file diff --git a/src/test/PureKissClass.kiss b/src/test/PureKissClass.kiss index c4dc227..fa905de 100644 --- a/src/test/PureKissClass.kiss +++ b/src/test/PureKissClass.kiss @@ -2,8 +2,16 @@ (importAs test.OtherPureKissClass Alias) (using test.OtherPureKissClass) +(extends OtherPureKissClass) +(implements test.BasicInterface) + +(defNew [num &prop :Int otherNum] + (super num)) + (function test [] (assert (= (.extensionMethod "string") "EXTENDED")) (let [instance (new OtherPureKissClass 5) - other (new Alias 5)] - (assert (= instance.num other.num 5)))) \ No newline at end of file + other (new Alias 5) + third (new PureKissClass 5 6)] + (assert (= instance.num other.num third.num 5)) + (assert (= third.otherNum 6)))) \ No newline at end of file