Browse Source

- Fix arithmetic rules for floats

- Fix float suffixes on integers
Stan Hebben 5 years ago
parent
commit
c19e68376b

+ 60
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMemberBuilder.java View File

@@ -65,6 +65,7 @@ import static org.openzen.zenscript.codemodel.type.member.BuiltinID.*;
65 65
 import static org.openzen.zencode.shared.CodePosition.BUILTIN;
66 66
 import org.openzen.zenscript.codemodel.Module;
67 67
 import org.openzen.zenscript.codemodel.definition.InterfaceDefinition;
68
+import org.openzen.zenscript.codemodel.expression.CallArguments;
68 69
 import org.openzen.zenscript.codemodel.expression.ConstantUSizeExpression;
69 70
 import org.openzen.zenscript.codemodel.member.IteratorMember;
70 71
 import org.openzen.zenscript.codemodel.type.InvalidTypeID;
@@ -1158,15 +1159,51 @@ public class TypeMemberBuilder implements TypeVisitorWithContext<Void, Void, Run
1158 1159
 		inc(builtin, FLOAT_INC, FLOAT);
1159 1160
 		dec(builtin, FLOAT_DEC, FLOAT);
1160 1161
 
1162
+		addWithCastedOperand(builtin, FLOAT_ADD_FLOAT, BYTE, FLOAT, BYTE_TO_FLOAT);
1163
+		addWithCastedOperand(builtin, FLOAT_ADD_FLOAT, SBYTE, FLOAT, SBYTE_TO_FLOAT);
1164
+		addWithCastedOperand(builtin, FLOAT_ADD_FLOAT, SHORT, FLOAT, SHORT_TO_FLOAT);
1165
+		addWithCastedOperand(builtin, FLOAT_ADD_FLOAT, USHORT, FLOAT, USHORT_TO_FLOAT);
1166
+		addWithCastedOperand(builtin, FLOAT_ADD_FLOAT, INT, FLOAT, INT_TO_FLOAT);
1167
+		addWithCastedOperand(builtin, FLOAT_ADD_FLOAT, UINT, FLOAT, UINT_TO_FLOAT);
1168
+		addWithCastedOperand(builtin, FLOAT_ADD_FLOAT, LONG, FLOAT, LONG_TO_FLOAT);
1169
+		addWithCastedOperand(builtin, FLOAT_ADD_FLOAT, ULONG, FLOAT, ULONG_TO_FLOAT);
1170
+		addWithCastedOperand(builtin, FLOAT_ADD_FLOAT, USIZE, FLOAT, USIZE_TO_FLOAT);
1161 1171
 		add(builtin, FLOAT_ADD_FLOAT, FLOAT, FLOAT);
1162 1172
 		add(builtin, DOUBLE_ADD_DOUBLE, DOUBLE, DOUBLE, FLOAT_TO_DOUBLE);
1163 1173
 		
1174
+		subWithCastedOperand(builtin, FLOAT_SUB_FLOAT, BYTE, FLOAT, BYTE_TO_FLOAT);
1175
+		subWithCastedOperand(builtin, FLOAT_SUB_FLOAT, SBYTE, FLOAT, SBYTE_TO_FLOAT);
1176
+		subWithCastedOperand(builtin, FLOAT_SUB_FLOAT, SHORT, FLOAT, SHORT_TO_FLOAT);
1177
+		subWithCastedOperand(builtin, FLOAT_SUB_FLOAT, USHORT, FLOAT, USHORT_TO_FLOAT);
1178
+		subWithCastedOperand(builtin, FLOAT_SUB_FLOAT, INT, FLOAT, INT_TO_FLOAT);
1179
+		subWithCastedOperand(builtin, FLOAT_SUB_FLOAT, UINT, FLOAT, UINT_TO_FLOAT);
1180
+		subWithCastedOperand(builtin, FLOAT_SUB_FLOAT, LONG, FLOAT, LONG_TO_FLOAT);
1181
+		subWithCastedOperand(builtin, FLOAT_SUB_FLOAT, ULONG, FLOAT, ULONG_TO_FLOAT);
1182
+		subWithCastedOperand(builtin, FLOAT_SUB_FLOAT, USIZE, FLOAT, USIZE_TO_FLOAT);
1164 1183
 		sub(builtin, FLOAT_SUB_FLOAT, FLOAT, FLOAT);
1165 1184
 		sub(builtin, DOUBLE_SUB_DOUBLE, DOUBLE, DOUBLE, LONG_TO_DOUBLE);
1166 1185
 		
1186
+		mulWithCastedOperand(builtin, FLOAT_MUL_FLOAT, BYTE, FLOAT, BYTE_TO_FLOAT);
1187
+		mulWithCastedOperand(builtin, FLOAT_MUL_FLOAT, SBYTE, FLOAT, SBYTE_TO_FLOAT);
1188
+		mulWithCastedOperand(builtin, FLOAT_MUL_FLOAT, SHORT, FLOAT, SHORT_TO_FLOAT);
1189
+		mulWithCastedOperand(builtin, FLOAT_MUL_FLOAT, USHORT, FLOAT, USHORT_TO_FLOAT);
1190
+		mulWithCastedOperand(builtin, FLOAT_MUL_FLOAT, INT, FLOAT, INT_TO_FLOAT);
1191
+		mulWithCastedOperand(builtin, FLOAT_MUL_FLOAT, UINT, FLOAT, UINT_TO_FLOAT);
1192
+		mulWithCastedOperand(builtin, FLOAT_MUL_FLOAT, LONG, FLOAT, LONG_TO_FLOAT);
1193
+		mulWithCastedOperand(builtin, FLOAT_MUL_FLOAT, ULONG, FLOAT, ULONG_TO_FLOAT);
1194
+		mulWithCastedOperand(builtin, FLOAT_MUL_FLOAT, USIZE, FLOAT, USIZE_TO_FLOAT);
1167 1195
 		mul(builtin, FLOAT_MUL_FLOAT, FLOAT, FLOAT);
1168 1196
 		mul(builtin, DOUBLE_MUL_DOUBLE, DOUBLE, DOUBLE, LONG_TO_DOUBLE);
1169 1197
 		
1198
+		divWithCastedOperand(builtin, FLOAT_DIV_FLOAT, BYTE, FLOAT, BYTE_TO_FLOAT);
1199
+		divWithCastedOperand(builtin, FLOAT_DIV_FLOAT, SBYTE, FLOAT, SBYTE_TO_FLOAT);
1200
+		divWithCastedOperand(builtin, FLOAT_DIV_FLOAT, SHORT, FLOAT, SHORT_TO_FLOAT);
1201
+		divWithCastedOperand(builtin, FLOAT_DIV_FLOAT, USHORT, FLOAT, USHORT_TO_FLOAT);
1202
+		divWithCastedOperand(builtin, FLOAT_DIV_FLOAT, INT, FLOAT, INT_TO_FLOAT);
1203
+		divWithCastedOperand(builtin, FLOAT_DIV_FLOAT, UINT, FLOAT, UINT_TO_FLOAT);
1204
+		divWithCastedOperand(builtin, FLOAT_DIV_FLOAT, LONG, FLOAT, LONG_TO_FLOAT);
1205
+		divWithCastedOperand(builtin, FLOAT_DIV_FLOAT, ULONG, FLOAT, ULONG_TO_FLOAT);
1206
+		divWithCastedOperand(builtin, FLOAT_DIV_FLOAT, USIZE, FLOAT, USIZE_TO_FLOAT);
1170 1207
 		div(builtin, FLOAT_DIV_FLOAT, FLOAT, FLOAT);
1171 1208
 		div(builtin, DOUBLE_DIV_DOUBLE, DOUBLE, DOUBLE, LONG_TO_DOUBLE);
1172 1209
 		
@@ -1267,6 +1304,13 @@ public class TypeMemberBuilder implements TypeVisitorWithContext<Void, Void, Run
1267 1304
 		members.getOrCreateGroup(member.operator).addMethod(method, TypeMemberPriority.SPECIFIED);
1268 1305
 	}
1269 1306
 	
1307
+	private void castedOperandCall(OperatorMember member, StoredType toType, BuiltinID casterBuiltin) {
1308
+		CasterMemberRef caster = castImplicitRef(member.definition, casterBuiltin, toType);
1309
+		TranslatedOperatorMemberRef method = new TranslatedOperatorMemberRef(member, members.type, GenericMapper.EMPTY,
1310
+				call -> member.ref(members.type, null).call(call.position, call.target, new CallArguments(caster.cast(call.position, call.arguments.arguments[0], true)), call.scope));
1311
+		members.getOrCreateGroup(member.operator).addMethod(method, TypeMemberPriority.SPECIFIED);
1312
+	}
1313
+	
1270 1314
 	private void register(IDefinitionMember member) {
1271 1315
 		member.registerTo(members, TypeMemberPriority.SPECIFIED, null);
1272 1316
 	}
@@ -1347,6 +1391,10 @@ public class TypeMemberBuilder implements TypeVisitorWithContext<Void, Void, Run
1347 1391
 		castedTargetCall(addOp(definition, id, operand.stored, result.stored), result.stored, caster);
1348 1392
 	}
1349 1393
 	
1394
+	private void addWithCastedOperand(HighLevelDefinition definition, BuiltinID id, BasicTypeID operand, BasicTypeID result, BuiltinID caster) {
1395
+		castedOperandCall(addOp(definition, id, operand.stored, result.stored), operand.stored, caster);
1396
+	}
1397
+	
1350 1398
 	private OperatorMember subOp(HighLevelDefinition cls, BuiltinID id, StoredType operand, StoredType result) {
1351 1399
 		return new OperatorMember(
1352 1400
 				BUILTIN,
@@ -1365,6 +1413,10 @@ public class TypeMemberBuilder implements TypeVisitorWithContext<Void, Void, Run
1365 1413
 		castedTargetCall(subOp(definition, id, operand.stored, result.stored), result.stored, caster);
1366 1414
 	}
1367 1415
 	
1416
+	private void subWithCastedOperand(HighLevelDefinition definition, BuiltinID id, BasicTypeID operand, BasicTypeID result, BuiltinID caster) {
1417
+		castedOperandCall(subOp(definition, id, operand.stored, result.stored), operand.stored, caster);
1418
+	}
1419
+	
1368 1420
 	private OperatorMember mulOp(HighLevelDefinition cls, BuiltinID id, StoredType operand, StoredType result) {
1369 1421
 		return new OperatorMember(
1370 1422
 				BUILTIN,
@@ -1383,6 +1435,10 @@ public class TypeMemberBuilder implements TypeVisitorWithContext<Void, Void, Run
1383 1435
 		castedTargetCall(mulOp(definition, id, operand.stored, result.stored), result.stored, caster);
1384 1436
 	}
1385 1437
 	
1438
+	private void mulWithCastedOperand(HighLevelDefinition definition, BuiltinID id, BasicTypeID operand, BasicTypeID result, BuiltinID caster) {
1439
+		castedOperandCall(mulOp(definition, id, operand.stored, result.stored), operand.stored, caster);
1440
+	}
1441
+	
1386 1442
 	private OperatorMember divOp(HighLevelDefinition cls, BuiltinID id, StoredType operand, StoredType result) {
1387 1443
 		return new OperatorMember(
1388 1444
 				BUILTIN,
@@ -1401,6 +1457,10 @@ public class TypeMemberBuilder implements TypeVisitorWithContext<Void, Void, Run
1401 1457
 		castedTargetCall(divOp(definition, id, operand.stored, result.stored), result.stored, caster);
1402 1458
 	}
1403 1459
 	
1460
+	private void divWithCastedOperand(HighLevelDefinition definition, BuiltinID id, BasicTypeID operand, BasicTypeID result, BuiltinID caster) {
1461
+		castedOperandCall(divOp(definition, id, operand.stored, result.stored), operand.stored, caster);
1462
+	}
1463
+	
1404 1464
 	private OperatorMember modOp(HighLevelDefinition cls, BuiltinID id, StoredType operand, StoredType result) {
1405 1465
 		return new OperatorMember(
1406 1466
 				BUILTIN,

+ 6
- 0
Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedExpressionInt.java View File

@@ -12,6 +12,8 @@ import org.openzen.zencode.shared.CompileExceptionCode;
12 12
 import org.openzen.zenscript.codemodel.expression.CallArguments;
13 13
 import org.openzen.zenscript.codemodel.expression.ConstantByteExpression;
14 14
 import org.openzen.zenscript.codemodel.expression.ConstantCharExpression;
15
+import org.openzen.zenscript.codemodel.expression.ConstantDoubleExpression;
16
+import org.openzen.zenscript.codemodel.expression.ConstantFloatExpression;
15 17
 import org.openzen.zenscript.codemodel.expression.ConstantIntExpression;
16 18
 import org.openzen.zenscript.codemodel.expression.ConstantLongExpression;
17 19
 import org.openzen.zenscript.codemodel.expression.ConstantSByteExpression;
@@ -116,6 +118,10 @@ public class ParsedExpressionInt extends ParsedExpression {
116 118
 			return new ConstantULongExpression(position, value);
117 119
 		if (suffix.equals("U") || suffix.equals("u"))
118 120
 			return new ConstantUIntExpression(position, (int)value);
121
+		if (suffix.equals("D") || suffix.equals("d"))
122
+			return new ConstantDoubleExpression(position, value);
123
+		if (suffix.equals("F") || suffix.equals("f"))
124
+			return new ConstantFloatExpression(position, value);
119 125
 		
120 126
 		for (StoredType hint : scope.hints) {
121 127
 			if (suffix.isEmpty() && (hint.type instanceof BasicTypeID)) {

+ 8
- 1
ScriptingExample/scripts/integration.zs View File

@@ -23,4 +23,11 @@ printMany(objects);
23 23
 
24 24
 
25 25
 println(<test string>);
26
-println(<test string>.length);
26
+println(<test string>.name);
27
+
28
+var diamond = <item:minecraft:diamond>;
29
+var dirt = <item:minecraft:dirt>;
30
+addShapedRecipe("TestRecipe", diamond, [[dirt, dirt, dirt],[dirt, dirt, dirt],[dirt, dirt, dirt]]);
31
+
32
+var count = 10;
33
+floatMethod(5f * count);

+ 12
- 2
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Globals.java View File

@@ -37,7 +37,17 @@ public class Globals implements ZenCodeGlobals {
37 37
 			System.out.println("  - " + object.getName());
38 38
 	}
39 39
 	
40
-	public static String bracket(String value) {
41
-		return "BRACKET:" + value;
40
+	@Global
41
+    public static void addShapedRecipe(String name, TestClass output, TestClass[][] inputs) {
42
+		System.out.println("Added " + name);
43
+	}
44
+	
45
+	@Global
46
+	public static void floatMethod(float argument) {
47
+		System.out.println(argument);
48
+	}
49
+	
50
+	public static TestClass bracket(String value) {
51
+		return new TestClass(value);
42 52
 	}
43 53
 }

+ 6
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/TestClass.java View File

@@ -33,4 +33,10 @@ public class TestClass implements ZenCodeType {
33 33
 	public TestClass wrap(String quotes) {
34 34
 		return new TestClass(quotes + name + quotes);
35 35
 	}
36
+	
37
+	@Caster(implicit = true)
38
+	@Override
39
+	public String toString() {
40
+		return name;
41
+	}
36 42
 }

+ 1
- 1
common.gradle View File

@@ -6,7 +6,7 @@ apply plugin: 'java'
6 6
 apply plugin: 'maven'
7 7
 
8 8
 String mavenGroupId = 'org.openzen.zencode'
9
-String mavenVersion = '0.3.5'
9
+String mavenVersion = '0.3.6'
10 10
 
11 11
 sourceCompatibility = '1.8'
12 12
 [compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

Loading…
Cancel
Save