From abbcb0d0b733a2955cd46c4abc5e01fb5df10430 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Mon, 31 Oct 2022 21:41:07 +0000 Subject: [PATCH] haxe property support. close #87 --- src/kiss/FieldForms.hx | 29 ++++++++++++++++++++++++++++- src/test/cases/BasicTestCase.hx | 4 ++++ src/test/cases/BasicTestCase.kiss | 13 ++++++++++++- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/kiss/FieldForms.hx b/src/kiss/FieldForms.hx index aeb2847..8e71a17 100644 --- a/src/kiss/FieldForms.hx +++ b/src/kiss/FieldForms.hx @@ -3,6 +3,7 @@ package kiss; import haxe.macro.Expr; import haxe.macro.Context; import kiss.Reader; +import kiss.ReaderExp; import kiss.Helpers; import kiss.Stream; import kiss.KissError; @@ -105,10 +106,36 @@ class FieldForms { if (type != null) k.addVarInScope({name: name, type: type}, false); + + function varOrPropKind(args:Array) { + return if (args.length > 1) { + switch (args[1].def) { + case CallExp({pos:_, def:Symbol("property")}, innerArgs): + args[1].checkNumArgs(2, 3, "(property )"); + function accessType(read, arg) { + var acceptable = ["default", "null", if (read) "get" else "set", "dynamic", "never"]; + return switch (arg.def) { + case Symbol(access) if (acceptable.contains(access)): + access; + default: throw KissError.fromExp(arg, 'Expected a haxe property access keyword: one of [${acceptable.join(", ")}]'); + }; + } + var readAccess = accessType(true, innerArgs[0]); + var writeAccess = accessType(false, innerArgs[1]); + var value = if (innerArgs.length > 2) k.convert(innerArgs[2]) else null; + FProp(readAccess, writeAccess, type, value); + default: + FVar(type, k.convert(args[1])); + }; + } else { + FVar(type, null); + }; + } + ({ name: name, access: access, - kind: FVar(type, if (args.length > 1) k.convert(args[1]) else null), + kind: varOrPropKind(args), pos: wholeExp.macroPos() } : Field); } diff --git a/src/test/cases/BasicTestCase.hx b/src/test/cases/BasicTestCase.hx index 927ad0f..86fa387 100644 --- a/src/test/cases/BasicTestCase.hx +++ b/src/test/cases/BasicTestCase.hx @@ -387,6 +387,10 @@ class BasicTestCase extends Test { Assert.isFalse(_hasThreadSupport()); #end } + + function testHaxeProperties() { + _testHaxeProperties(); + } } class BasicObject { diff --git a/src/test/cases/BasicTestCase.kiss b/src/test/cases/BasicTestCase.kiss index 20d839e..557bee3 100644 --- a/src/test/cases/BasicTestCase.kiss +++ b/src/test/cases/BasicTestCase.kiss @@ -726,4 +726,15 @@ From:[(assert false (+ \"false \" \"should \" \"have \" \"been \" \"true\"))]" m (function _hasThreadSupport [] (#if target.threaded true - false)) \ No newline at end of file + false)) + +(var &mut _staticProp 5) +(var :Int staticProp (property get set)) +(function set_staticProp [v] (set _staticProp v)) +(function get_staticProp [] _staticProp) + +(function _testHaxeProperties [] + (Assert.equals 5 staticProp) + (Assert.equals 9 (set staticProp 9)) + (Assert.equals 9 staticProp) + (Assert.equals 9 _staticProp)) \ No newline at end of file