From e86a8f2db10c1523a41020b0b1b1a0435c4d4329 Mon Sep 17 00:00:00 2001 From: eutropius225 <11538216+eutropius225@users.noreply.github.com> Date: Fri, 23 Oct 2020 12:52:30 +0100 Subject: [PATCH 1/2] Allow bracketed expressions in ZenType declarations. This can reduce ambiguity and possibly allow for function arrays, or associatives with function keys, if `ZenTypeFunctionCallable#toJavaClass` ever gets implemented. --- src/main/java/stanhebben/zenscript/type/ZenType.java | 4 ++++ src/main/java/stanhebben/zenscript/type/ZenTypeArray.java | 2 +- .../java/stanhebben/zenscript/type/ZenTypeAssociative.java | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/stanhebben/zenscript/type/ZenType.java b/src/main/java/stanhebben/zenscript/type/ZenType.java index 8b1ae3a..722a27d 100644 --- a/src/main/java/stanhebben/zenscript/type/ZenType.java +++ b/src/main/java/stanhebben/zenscript/type/ZenType.java @@ -194,6 +194,10 @@ public static ZenType read(ZenTokener parser, IEnvironmentGlobal environment) { base = new ZenTypeArrayList(read(parser, environment)); parser.required(ZenTokener.T_SQBRCLOSE, "] expected"); break; + case ZenTokener.T_BROPEN: + base = read(parser, environment); + parser.required(ZenTokener.T_BRCLOSE, ") expected"); + break; default: throw new ParseException(next, "Unknown type: " + next.getValue()); } diff --git a/src/main/java/stanhebben/zenscript/type/ZenTypeArray.java b/src/main/java/stanhebben/zenscript/type/ZenTypeArray.java index 5fbad57..25219fa 100644 --- a/src/main/java/stanhebben/zenscript/type/ZenTypeArray.java +++ b/src/main/java/stanhebben/zenscript/type/ZenTypeArray.java @@ -16,7 +16,7 @@ public abstract class ZenTypeArray extends ZenType { public ZenTypeArray(ZenType base) { this.base = base; - name = base + "[]"; + name = "(" + base + ")[]"; } public ZenTypeArray(ZenType base, String name) { diff --git a/src/main/java/stanhebben/zenscript/type/ZenTypeAssociative.java b/src/main/java/stanhebben/zenscript/type/ZenTypeAssociative.java index 32642b5..f81c1a6 100644 --- a/src/main/java/stanhebben/zenscript/type/ZenTypeAssociative.java +++ b/src/main/java/stanhebben/zenscript/type/ZenTypeAssociative.java @@ -28,7 +28,7 @@ public ZenTypeAssociative(ZenType valueType, ZenType keyType) { this.valueType = valueType; this.keyType = keyType; - name = valueType.getName() + "[" + keyType.getName() + "]"; + name = "(" + valueType.getName() + ")[" + keyType.getName() + "]"; } public ZenType getValueType() { From 2ba5e7be944b07fa730dccc203fe1e509866cebc Mon Sep 17 00:00:00 2001 From: eutropius225 <11538216+eutropius225@users.noreply.github.com> Date: Wed, 11 Nov 2020 21:25:57 +0000 Subject: [PATCH 2/2] Move bracketing in toStrings to ZenTypeFunction --- .../zenscript/type/ZenTypeArray.java | 2 +- .../zenscript/type/ZenTypeAssociative.java | 2 +- .../zenscript/type/ZenTypeFunction.java | 33 +++++++------------ 3 files changed, 14 insertions(+), 23 deletions(-) diff --git a/src/main/java/stanhebben/zenscript/type/ZenTypeArray.java b/src/main/java/stanhebben/zenscript/type/ZenTypeArray.java index 25219fa..5fbad57 100644 --- a/src/main/java/stanhebben/zenscript/type/ZenTypeArray.java +++ b/src/main/java/stanhebben/zenscript/type/ZenTypeArray.java @@ -16,7 +16,7 @@ public abstract class ZenTypeArray extends ZenType { public ZenTypeArray(ZenType base) { this.base = base; - name = "(" + base + ")[]"; + name = base + "[]"; } public ZenTypeArray(ZenType base, String name) { diff --git a/src/main/java/stanhebben/zenscript/type/ZenTypeAssociative.java b/src/main/java/stanhebben/zenscript/type/ZenTypeAssociative.java index f81c1a6..32642b5 100644 --- a/src/main/java/stanhebben/zenscript/type/ZenTypeAssociative.java +++ b/src/main/java/stanhebben/zenscript/type/ZenTypeAssociative.java @@ -28,7 +28,7 @@ public ZenTypeAssociative(ZenType valueType, ZenType keyType) { this.valueType = valueType; this.keyType = keyType; - name = "(" + valueType.getName() + ")[" + keyType.getName() + "]"; + name = valueType.getName() + "[" + keyType.getName() + "]"; } public ZenType getValueType() { diff --git a/src/main/java/stanhebben/zenscript/type/ZenTypeFunction.java b/src/main/java/stanhebben/zenscript/type/ZenTypeFunction.java index b2c68df..94ec44c 100644 --- a/src/main/java/stanhebben/zenscript/type/ZenTypeFunction.java +++ b/src/main/java/stanhebben/zenscript/type/ZenTypeFunction.java @@ -28,28 +28,18 @@ public ZenTypeFunction(ZenType returnType, List argument for(int i = 0; i < argumentTypes.length; i++) { argumentTypes[i] = arguments.get(i).getType(); } - - StringBuilder nameBuilder = new StringBuilder(); - nameBuilder.append("function("); - boolean first = true; - for(ZenType type : argumentTypes) { - if(first) { - first = false; - } else { - nameBuilder.append(','); - } - nameBuilder.append(type.getName()); - } - nameBuilder.append(returnType.getName()); - name = nameBuilder.toString(); + name = makeName(); } public ZenTypeFunction(ZenType returnType, ZenType[] argumentTypes) { this.returnType = returnType; this.argumentTypes = argumentTypes; - - StringBuilder nameBuilder = new StringBuilder(); - nameBuilder.append("function("); + this.name = makeName(); + } + + private String makeName() { + StringBuilder nameBuilder = new StringBuilder() + .append("(function("); boolean first = true; for(ZenType type : argumentTypes) { if(first) { @@ -59,9 +49,10 @@ public ZenTypeFunction(ZenType returnType, ZenType[] argumentTypes) { } nameBuilder.append(type.getName()); } - nameBuilder.append(')'); - nameBuilder.append(returnType.getName()); - name = nameBuilder.toString(); + nameBuilder.append(')') + .append(returnType.getName()) + .append(')'); + return nameBuilder.toString(); } @Override @@ -227,4 +218,4 @@ public ZenType[] getArgumentTypes() { public String getNameForInterfaceSignature() { return ZenTypeFunctionCallable.makeInterfaceName(returnType, argumentTypes); } -} \ No newline at end of file +}