haxe property support. close #87

This commit is contained in:
2022-10-31 21:41:07 +00:00
parent 5edd1ac097
commit 3088be2815
3 changed files with 44 additions and 2 deletions

View File

@@ -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<ReaderExp>) {
return if (args.length > 1) {
switch (args[1].def) {
case CallExp({pos:_, def:Symbol("property")}, innerArgs):
args[1].checkNumArgs(2, 3, "(property <read access> <write access> <?value>)");
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);
}

View File

@@ -387,6 +387,10 @@ class BasicTestCase extends Test {
Assert.isFalse(_hasThreadSupport());
#end
}
function testHaxeProperties() {
_testHaxeProperties();
}
}
class BasicObject {

View File

@@ -726,4 +726,15 @@ From:[(assert false (+ \"false \" \"should \" \"have \" \"been \" \"true\"))]" m
(function _hasThreadSupport []
(#if target.threaded
true
false))
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))