Skip to content

Commit 3e013cb

Browse files
committed
- Fix missing shift operators in some integer types
- Fix some problems when comparing a long with an int or ulong with uint - Add signed-unsigned implicit array conversion (sbyte[] <-> byte[], short[] <-> ushort[], int[] <-> uint[], long[] <-> ulong[]) - Fix bug when copying multiple lines from the source editor - Added string <-> byte[] conversion operations - Fix unsigned arrays, use compact representations - Added suffixes & custom suffixes - Added 0x 0b 0o for hex, binary and octal notation
1 parent 6c57403 commit 3e013cb

17 files changed

Lines changed: 466 additions & 96 deletions

File tree

‎CodeModel/src/main/java/org/openzen/zenscript/codemodel/FunctionHeader.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,4 +462,8 @@ private static boolean hasUnknowns(FunctionParameter[] parameters, ITypeID retur
462462

463463
return false;
464464
}
465+
466+
public boolean accepts(int arguments) {
467+
return arguments >= this.minParameters && arguments <= this.maxParameters;
468+
}
465469
}

‎CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/BuiltinID.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public enum BuiltinID {
3030
BYTE_AND_BYTE,
3131
BYTE_OR_BYTE,
3232
BYTE_XOR_BYTE,
33+
BYTE_SHL,
34+
BYTE_SHR,
3335
BYTE_COMPARE,
3436
BYTE_TO_SBYTE,
3537
BYTE_TO_SHORT,
@@ -60,6 +62,9 @@ public enum BuiltinID {
6062
SBYTE_AND_SBYTE,
6163
SBYTE_OR_SBYTE,
6264
SBYTE_XOR_SBYTE,
65+
SBYTE_SHL,
66+
SBYTE_SHR,
67+
SBYTE_USHR,
6368
SBYTE_COMPARE,
6469
SBYTE_TO_BYTE,
6570
SBYTE_TO_SHORT,
@@ -90,6 +95,9 @@ public enum BuiltinID {
9095
SHORT_AND_SHORT,
9196
SHORT_OR_SHORT,
9297
SHORT_XOR_SHORT,
98+
SHORT_SHL,
99+
SHORT_SHR,
100+
SHORT_USHR,
93101
SHORT_COMPARE,
94102
SHORT_TO_BYTE,
95103
SHORT_TO_SBYTE,
@@ -119,6 +127,8 @@ public enum BuiltinID {
119127
USHORT_AND_USHORT,
120128
USHORT_OR_USHORT,
121129
USHORT_XOR_USHORT,
130+
USHORT_SHL,
131+
USHORT_SHR,
122132
USHORT_COMPARE,
123133
USHORT_TO_BYTE,
124134
USHORT_TO_SBYTE,
@@ -235,6 +245,7 @@ public enum BuiltinID {
235245
LONG_SHR,
236246
LONG_USHR,
237247
LONG_COMPARE,
248+
LONG_COMPARE_INT,
238249
LONG_TO_BYTE,
239250
LONG_TO_SBYTE,
240251
LONG_TO_SHORT,
@@ -275,6 +286,8 @@ public enum BuiltinID {
275286
ULONG_SHL,
276287
ULONG_SHR,
277288
ULONG_COMPARE,
289+
ULONG_COMPARE_UINT,
290+
ULONG_COMPARE_USIZE,
278291
ULONG_TO_BYTE,
279292
ULONG_TO_SBYTE,
280293
ULONG_TO_SHORT,
@@ -315,6 +328,7 @@ public enum BuiltinID {
315328
USIZE_SHL,
316329
USIZE_SHR,
317330
USIZE_COMPARE,
331+
USIZE_COMPARE_UINT,
318332
USIZE_TO_BYTE,
319333
USIZE_TO_SBYTE,
320334
USIZE_TO_SHORT,
@@ -474,6 +488,15 @@ public enum BuiltinID {
474488
ARRAY_SAME,
475489
ARRAY_NOTSAME,
476490

491+
SBYTE_ARRAY_AS_BYTE_ARRAY,
492+
BYTE_ARRAY_AS_SBYTE_ARRAY,
493+
SHORT_ARRAY_AS_USHORT_ARRAY,
494+
USHORT_ARRAY_AS_SHORT_ARRAY,
495+
INT_ARRAY_AS_UINT_ARRAY,
496+
UINT_ARRAY_AS_INT_ARRAY,
497+
LONG_ARRAY_AS_ULONG_ARRAY,
498+
ULONG_ARRAY_AS_LONG_ARRAY,
499+
477500
FUNCTION_CALL,
478501
FUNCTION_SAME,
479502
FUNCTION_NOTSAME,

‎CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/DefinitionMemberGroup.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,15 @@ public boolean hasMethod(FunctionHeader header) {
105105
return false;
106106
}
107107

108+
public FunctionalMemberRef getStaticMethod(int arguments, ITypeID returnType) {
109+
for (TypeMember<FunctionalMemberRef> method : methods) {
110+
if (method.member.isStatic() && method.member.getHeader().accepts(arguments) && method.member.getHeader().getReturnType() == returnType)
111+
return method.member;
112+
}
113+
114+
return null;
115+
}
116+
108117
public List<TypeMember<FunctionalMemberRef>> getMethodMembers() {
109118
return this.methods;
110119
}

‎CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMemberBuilder.java‎

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,23 @@ public Void visitArray(ArrayTypeID array) {
193193
OperatorType.INDEXGET,
194194
sliceHeader,
195195
ARRAY_INDEXGETRANGE);
196+
197+
if (baseType == BYTE)
198+
castImplicit(definition, BYTE_ARRAY_AS_SBYTE_ARRAY, registry.getArray(SBYTE, 1));
199+
if (baseType == SBYTE)
200+
castImplicit(definition, SBYTE_ARRAY_AS_BYTE_ARRAY, registry.getArray(BYTE, 1));
201+
if (baseType == SHORT)
202+
castImplicit(definition, SHORT_ARRAY_AS_USHORT_ARRAY, registry.getArray(USHORT, 1));
203+
if (baseType == USHORT)
204+
castImplicit(definition, USHORT_ARRAY_AS_SHORT_ARRAY, registry.getArray(SHORT, 1));
205+
if (baseType == INT)
206+
castImplicit(definition, INT_ARRAY_AS_UINT_ARRAY, registry.getArray(UINT, 1));
207+
if (baseType == UINT)
208+
castImplicit(definition, UINT_ARRAY_AS_INT_ARRAY, registry.getArray(INT, 1));
209+
if (baseType == LONG)
210+
castImplicit(definition, LONG_ARRAY_AS_ULONG_ARRAY, registry.getArray(ULONG, 1));
211+
if (baseType == ULONG)
212+
castImplicit(definition, ULONG_ARRAY_AS_LONG_ARRAY, registry.getArray(LONG, 1));
196213
}
197214

198215
FunctionHeader containsHeader = new FunctionHeader(BOOL, new FunctionParameter(baseType, "value"));
@@ -558,6 +575,8 @@ private void visitByte() {
558575
and(builtin, BYTE_AND_BYTE, BYTE, BYTE);
559576
or(builtin, BYTE_OR_BYTE, BYTE, BYTE);
560577
xor(builtin, BYTE_XOR_BYTE, BYTE, BYTE);
578+
shl(builtin, BYTE_SHL, USIZE, BYTE);
579+
shr(builtin, BYTE_SHR, USIZE, BYTE);
561580
compare(builtin, BYTE_COMPARE, BYTE);
562581

563582
castImplicit(builtin, BYTE_TO_SBYTE, SBYTE);
@@ -597,6 +616,9 @@ private void visitSByte() {
597616
and(builtin, SBYTE_AND_SBYTE, SBYTE, SBYTE);
598617
or(builtin, SBYTE_OR_SBYTE, SBYTE, SBYTE);
599618
xor(builtin, SBYTE_XOR_SBYTE, SBYTE, SBYTE);
619+
shl(builtin, SBYTE_SHL, USIZE, SBYTE);
620+
shr(builtin, SBYTE_SHR, USIZE, SBYTE);
621+
ushr(builtin, SBYTE_USHR, USIZE, SBYTE);
600622
compare(builtin, SBYTE_COMPARE, SBYTE);
601623

602624
castImplicit(builtin, SBYTE_TO_BYTE, BYTE);
@@ -636,6 +658,9 @@ private void visitShort() {
636658
and(builtin, SHORT_AND_SHORT, SHORT, SHORT);
637659
or(builtin, SHORT_OR_SHORT, SHORT, SHORT);
638660
xor(builtin, SHORT_XOR_SHORT, SHORT, SHORT);
661+
shl(builtin, SHORT_SHL, USIZE, SHORT);
662+
shr(builtin, SHORT_SHR, USIZE, SHORT);
663+
ushr(builtin, SHORT_USHR, USIZE, SHORT);
639664
compare(builtin, SHORT_COMPARE, SHORT);
640665

641666
castExplicit(builtin, SHORT_TO_BYTE, BYTE);
@@ -674,6 +699,8 @@ private void visitUShort() {
674699
and(builtin, USHORT_AND_USHORT, USHORT, USHORT);
675700
or(builtin, USHORT_OR_USHORT, USHORT, USHORT);
676701
xor(builtin, USHORT_XOR_USHORT, USHORT, USHORT);
702+
shl(builtin, USHORT_SHL, USIZE, USHORT);
703+
shr(builtin, USHORT_SHR, USIZE, USHORT);
677704
compare(builtin, USHORT_COMPARE, USHORT);
678705

679706
castExplicit(builtin, USHORT_TO_BYTE, BYTE);
@@ -736,9 +763,9 @@ private void visitInt() {
736763
xor(builtin, INT_XOR_INT, INT, INT);
737764
xor(builtin, LONG_XOR_LONG, LONG, LONG, INT_TO_LONG);
738765

739-
shl(builtin, INT_SHL, INT, INT);
740-
shr(builtin, INT_SHR, INT, INT);
741-
ushr(builtin, INT_USHR, INT, INT);
766+
shl(builtin, INT_SHL, USIZE, INT);
767+
shr(builtin, INT_SHR, USIZE, INT);
768+
ushr(builtin, INT_USHR, USIZE, INT);
742769

743770
compare(builtin, INT_COMPARE, INT);
744771
compare(builtin, LONG_COMPARE, LONG, INT_TO_LONG);
@@ -816,8 +843,8 @@ private void visitUInt() {
816843
xor(builtin, UINT_XOR_UINT, UINT, UINT);
817844
xor(builtin, ULONG_XOR_ULONG, ULONG, ULONG, UINT_TO_ULONG);
818845

819-
shl(builtin, UINT_SHL, UINT, UINT);
820-
shr(builtin, UINT_SHR, UINT, UINT);
846+
shl(builtin, UINT_SHL, USIZE, UINT);
847+
shr(builtin, UINT_SHR, USIZE, UINT);
821848

822849
compare(builtin, UINT_COMPARE, UINT);
823850
compare(builtin, ULONG_COMPARE, ULONG, UINT_TO_LONG);
@@ -888,10 +915,11 @@ private void visitLong() {
888915
and(builtin, LONG_AND_LONG, LONG, LONG);
889916
xor(builtin, LONG_XOR_LONG, LONG, LONG);
890917

891-
shl(builtin, LONG_SHL, INT, LONG);
892-
shr(builtin, LONG_SHR, INT, LONG);
893-
ushr(builtin, LONG_USHR, INT, LONG);
918+
shl(builtin, LONG_SHL, USIZE, LONG);
919+
shr(builtin, LONG_SHR, USIZE, LONG);
920+
ushr(builtin, LONG_USHR, USIZE, LONG);
894921

922+
compare(builtin, LONG_COMPARE_INT, INT);
895923
compare(builtin, LONG_COMPARE, LONG);
896924
compare(builtin, FLOAT_COMPARE, FLOAT, LONG_TO_FLOAT);
897925
compare(builtin, DOUBLE_COMPARE, DOUBLE, LONG_TO_DOUBLE);
@@ -959,9 +987,11 @@ private void visitULong() {
959987
and(builtin, ULONG_AND_ULONG, ULONG, ULONG);
960988
xor(builtin, ULONG_XOR_ULONG, ULONG, ULONG);
961989

962-
shl(builtin, ULONG_SHL, INT, ULONG);
963-
shr(builtin, ULONG_SHR, INT, ULONG);
990+
shl(builtin, ULONG_SHL, USIZE, ULONG);
991+
shr(builtin, ULONG_SHR, USIZE, ULONG);
964992

993+
compare(builtin, ULONG_COMPARE_UINT, UINT);
994+
compare(builtin, ULONG_COMPARE_USIZE, USIZE);
965995
compare(builtin, ULONG_COMPARE, ULONG);
966996
compare(builtin, FLOAT_COMPARE, FLOAT, ULONG_TO_FLOAT);
967997
compare(builtin, DOUBLE_COMPARE, DOUBLE, ULONG_TO_DOUBLE);
@@ -1033,9 +1063,10 @@ private void visitUSize() {
10331063
and(builtin, USIZE_AND_USIZE, USIZE, USIZE);
10341064
xor(builtin, USIZE_XOR_USIZE, USIZE, USIZE);
10351065

1036-
shl(builtin, USIZE_SHL, INT, USIZE);
1037-
shr(builtin, USIZE_SHR, INT, USIZE);
1066+
shl(builtin, USIZE_SHL, USIZE, USIZE);
1067+
shr(builtin, USIZE_SHR, USIZE, USIZE);
10381068

1069+
compare(builtin, USIZE_COMPARE_UINT, UINT);
10391070
compare(builtin, USIZE_COMPARE, USIZE);
10401071
compare(builtin, ULONG_COMPARE, ULONG, USIZE_TO_ULONG);
10411072
compare(builtin, FLOAT_COMPARE, FLOAT, USIZE_TO_FLOAT);
@@ -1143,7 +1174,7 @@ private void visitDouble() {
11431174
castExplicit(builtin, DOUBLE_TO_USHORT, USHORT);
11441175
castExplicit(builtin, DOUBLE_TO_INT, INT);
11451176
castExplicit(builtin, DOUBLE_TO_UINT, UINT);
1146-
castExplicit(builtin, DOUBLE_TO_ULONG, ULONG);
1177+
castExplicit(builtin, DOUBLE_TO_LONG, LONG);
11471178
castExplicit(builtin, DOUBLE_TO_ULONG, ULONG);
11481179
castExplicit(builtin, DOUBLE_TO_USIZE, USIZE);
11491180
castImplicit(builtin, DOUBLE_TO_FLOAT, FLOAT);
@@ -1210,8 +1241,8 @@ private void visitString() {
12101241
processType(builtin, STRING);
12111242
}
12121243

1213-
private void castedTargetCall(OperatorMember member, BuiltinID casterBuiltin) {
1214-
CasterMemberRef caster = castImplicitRef(member.definition, casterBuiltin, member.header.parameters[0].type);
1244+
private void castedTargetCall(OperatorMember member, ITypeID toType, BuiltinID casterBuiltin) {
1245+
CasterMemberRef caster = castImplicitRef(member.definition, casterBuiltin, toType);
12151246
TranslatedOperatorMemberRef method = new TranslatedOperatorMemberRef(member, GenericMapper.EMPTY, call -> member.ref(null).call(call.position, caster.cast(call.position, call.target, true), call.arguments, call.scope));
12161247
members.getOrCreateGroup(member.operator).addMethod(method, TypeMemberPriority.SPECIFIED);
12171248
}
@@ -1269,7 +1300,7 @@ private void add(HighLevelDefinition definition, BuiltinID id, ITypeID operand,
12691300
}
12701301

12711302
private void add(HighLevelDefinition definition, BuiltinID id, ITypeID operand, ITypeID result, BuiltinID caster) {
1272-
castedTargetCall(addOp(definition, id, operand, result), caster);
1303+
castedTargetCall(addOp(definition, id, operand, result), result, caster);
12731304
}
12741305

12751306
private OperatorMember subOp(HighLevelDefinition cls, BuiltinID id, ITypeID operand, ITypeID result) {
@@ -1287,7 +1318,7 @@ private void sub(HighLevelDefinition cls, BuiltinID id, ITypeID operand, ITypeID
12871318
}
12881319

12891320
private void sub(HighLevelDefinition definition, BuiltinID id, ITypeID operand, ITypeID result, BuiltinID caster) {
1290-
castedTargetCall(subOp(definition, id, operand, result), caster);
1321+
castedTargetCall(subOp(definition, id, operand, result), result, caster);
12911322
}
12921323

12931324
private OperatorMember mulOp(HighLevelDefinition cls, BuiltinID id, ITypeID operand, ITypeID result) {
@@ -1305,7 +1336,7 @@ private void mul(HighLevelDefinition cls, BuiltinID id, ITypeID operand, ITypeID
13051336
}
13061337

13071338
private void mul(HighLevelDefinition definition, BuiltinID id, ITypeID operand, ITypeID result, BuiltinID caster) {
1308-
castedTargetCall(mulOp(definition, id, operand, result), caster);
1339+
castedTargetCall(mulOp(definition, id, operand, result), result, caster);
13091340
}
13101341

13111342
private OperatorMember divOp(HighLevelDefinition cls, BuiltinID id, ITypeID operand, ITypeID result) {
@@ -1323,7 +1354,7 @@ private void div(HighLevelDefinition cls, BuiltinID id, ITypeID operand, ITypeID
13231354
}
13241355

13251356
private void div(HighLevelDefinition definition, BuiltinID id, ITypeID operand, ITypeID result, BuiltinID caster) {
1326-
castedTargetCall(divOp(definition, id, operand, result), caster);
1357+
castedTargetCall(divOp(definition, id, operand, result), result, caster);
13271358
}
13281359

13291360
private OperatorMember modOp(HighLevelDefinition cls, BuiltinID id, ITypeID operand, ITypeID result) {
@@ -1341,7 +1372,7 @@ private void mod(HighLevelDefinition cls, BuiltinID id, ITypeID operand, ITypeID
13411372
}
13421373

13431374
private void mod(HighLevelDefinition definition, BuiltinID id, ITypeID operand, ITypeID result, BuiltinID caster) {
1344-
castedTargetCall(modOp(definition, id, operand, result), caster);
1375+
castedTargetCall(modOp(definition, id, operand, result), result, caster);
13451376
}
13461377

13471378
private OperatorMember shlOp(HighLevelDefinition cls, BuiltinID id, ITypeID operand, ITypeID result) {
@@ -1359,7 +1390,7 @@ private void shl(HighLevelDefinition cls, BuiltinID id, ITypeID operand, ITypeID
13591390
}
13601391

13611392
private void shl(HighLevelDefinition definition, BuiltinID id, ITypeID operand, ITypeID result, BuiltinID caster) {
1362-
castedTargetCall(shlOp(definition, id, operand, result), caster);
1393+
castedTargetCall(shlOp(definition, id, operand, result), result, caster);
13631394
}
13641395

13651396
private OperatorMember shrOp(HighLevelDefinition cls, BuiltinID id, ITypeID operand, ITypeID result) {
@@ -1377,7 +1408,7 @@ private void shr(HighLevelDefinition cls, BuiltinID id, ITypeID operand, ITypeID
13771408
}
13781409

13791410
private void shr(HighLevelDefinition definition, BuiltinID id, ITypeID operand, ITypeID result, BuiltinID caster) {
1380-
castedTargetCall(shrOp(definition, id, operand, result), caster);
1411+
castedTargetCall(shrOp(definition, id, operand, result), result, caster);
13811412
}
13821413

13831414
private OperatorMember ushrOp(HighLevelDefinition cls, BuiltinID id, ITypeID operand, ITypeID result) {
@@ -1395,7 +1426,7 @@ private void ushr(HighLevelDefinition cls, BuiltinID id, ITypeID operand, ITypeI
13951426
}
13961427

13971428
private void ushr(HighLevelDefinition definition, BuiltinID id, ITypeID operand, ITypeID result, BuiltinID caster) {
1398-
castedTargetCall(ushrOp(definition, id, operand, result), caster);
1429+
castedTargetCall(ushrOp(definition, id, operand, result), result, caster);
13991430
}
14001431

14011432
private OperatorMember orOp(HighLevelDefinition cls, BuiltinID id, ITypeID operand, ITypeID result) {
@@ -1431,7 +1462,7 @@ private void and(HighLevelDefinition cls, BuiltinID id, ITypeID operand, ITypeID
14311462
}
14321463

14331464
private void and(HighLevelDefinition definition, BuiltinID id, ITypeID operand, ITypeID result, BuiltinID caster) {
1434-
castedTargetCall(andOp(definition, id, operand, result), caster);
1465+
castedTargetCall(andOp(definition, id, operand, result), result, caster);
14351466
}
14361467

14371468
private OperatorMember xorOp(HighLevelDefinition cls, BuiltinID id, ITypeID operand, ITypeID result) {
@@ -1449,7 +1480,7 @@ private void xor(HighLevelDefinition cls, BuiltinID id, ITypeID operand, ITypeID
14491480
}
14501481

14511482
private void xor(HighLevelDefinition definition, BuiltinID id, ITypeID operand, ITypeID result, BuiltinID caster) {
1452-
castedTargetCall(xorOp(definition, id, operand, result), caster);
1483+
castedTargetCall(xorOp(definition, id, operand, result), result, caster);
14531484
}
14541485

14551486
private void indexGet(HighLevelDefinition cls, BuiltinID id, ITypeID operand, ITypeID result) {
@@ -1487,7 +1518,7 @@ private void compare(HighLevelDefinition cls, BuiltinID id, ITypeID operand) {
14871518
}
14881519

14891520
private void compare(HighLevelDefinition definition, BuiltinID id, ITypeID operand, BuiltinID caster) {
1490-
castedTargetCall(compareOp(definition, id, operand), caster);
1521+
castedTargetCall(compareOp(definition, id, operand), operand, caster);
14911522
}
14921523

14931524
private void getter(HighLevelDefinition cls, BuiltinID id, String name, ITypeID type) {

‎IDE/src/main/java/org/openzen/zenscript/ide/ui/view/editor/SourceEditor.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,11 @@ private void copy() {
470470
String extract = tokens.extract(
471471
SourcePosition.min(cursorStart, cursorEnd),
472472
SourcePosition.max(cursorStart, cursorEnd));
473+
if (context.getUIContext() == null)
474+
throw new NullPointerException("No context!");
475+
if (context.getUIContext().getClipboard() == null)
476+
throw new NullPointerException("No clipboard!");
477+
473478
context.getUIContext().getClipboard().copyAsString(extract);
474479
}
475480

@@ -816,6 +821,7 @@ public static TokenClass get(ZSTokenType type) {
816821
return IDENTIFIER;
817822
case T_FLOAT:
818823
case T_INT:
824+
case T_PREFIXED_INT:
819825
return NUMBER;
820826
case T_STRING_SQ:
821827
case T_STRING_DQ:

‎IDE/src/main/java/org/openzen/zenscript/ide/ui/view/editor/TokenModel.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public String extract(SourcePosition from, SourcePosition to) {
120120

121121
for (int i = fromT.line + 1; i < toT.line; i++) {
122122
result.append("\n");
123-
for (ZSToken t : fromLine.getTokens()) {
123+
for (ZSToken t : getLine(i).getTokens()) {
124124
result.append(t.content);
125125
}
126126
}

0 commit comments

Comments
 (0)