Skip to content

Commit 35e4f85

Browse files
committed
Merge remote-tracking branch 'kindlich/development' into development
2 parents 4d53b6c + f463992 commit 35e4f85

File tree

173 files changed

+6048
-3270
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

173 files changed

+6048
-3270
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
.nb-gradle
33
build
44
*.class
5+
6+
out/

CodeFormatter/src/main/java/org/openzen/zenscript/formatter/ExpressionFormatter.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,16 @@ public ExpressionString visitCall(CallExpression expression) {
178178
}
179179
case INDEXGET: {
180180
StringBuilder result = new StringBuilder();
181-
result.append(expression.target);
181+
if(expression.target instanceof GetLocalVariableExpression) {
182+
result.append(((GetLocalVariableExpression) expression.target).variable.name);
183+
} else if(expression.target instanceof GetFunctionParameterExpression) {
184+
result.append(((GetFunctionParameterExpression) expression.target).parameter.name);
185+
} else {
186+
result.append(expression.target);
187+
}
182188
result.append("[");
183-
for (int i = 0; i < expression.arguments.arguments.length - 1; i++) {
189+
//why -1?
190+
for (int i = 0; i < expression.arguments.arguments.length; i++) {
184191
if (i > 0)
185192
result.append(", ");
186193

@@ -366,12 +373,12 @@ public ExpressionString visitConstantChar(ConstantCharExpression expression) {
366373

367374
@Override
368375
public ExpressionString visitConstantDouble(ConstantDoubleExpression expression) {
369-
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
376+
return new ExpressionString(Double.toString(expression.value), ZenScriptOperator.PRIMARY);
370377
}
371378

372379
@Override
373380
public ExpressionString visitConstantFloat(ConstantFloatExpression expression) {
374-
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
381+
return new ExpressionString(Float.toString(expression.value), ZenScriptOperator.PRIMARY);
375382
}
376383

377384
@Override

CodeFormatter/src/main/java/org/openzen/zenscript/formatter/StatementFormatter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ public Void visitEmpty(EmptyStatement statement) {
132132
public Void visitExpression(ExpressionStatement statement) {
133133
WhitespaceInfo whitespace = statement.getTag(WhitespaceInfo.class);
134134
beginSingleLine(whitespace);
135-
output.append(statement.expression.accept(expressionFormatter).value)
136-
.append(";");
135+
String value = statement.expression.accept(expressionFormatter).value;
136+
output.append(value).append(";");
137137
endSingleLine(whitespace);
138138
return null;
139139
}
@@ -278,7 +278,8 @@ public Void visitVar(VarStatement statement) {
278278
}
279279
if (statement.initializer != null) {
280280
output.append(" = ");
281-
output.append(statement.initializer.accept(expressionFormatter).value);
281+
String value = statement.initializer.accept(expressionFormatter).value;
282+
output.append(value);
282283
}
283284
output.append(";");
284285
endSingleLine(whitespace);

CodeFormatter/src/main/java/org/openzen/zenscript/formatter/TypeFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public String visitFunction(FunctionTypeID function) {
9090
@Override
9191
public String visitDefinition(DefinitionTypeID definition) {
9292
String importedName = importer.importDefinition(definition.definition);
93-
if (definition.typeArguments == null)
93+
if (definition.typeArguments == null || definition.typeArguments.length == 0)
9494
return importedName;
9595

9696
StringBuilder result = new StringBuilder();

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

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
*/
66
package org.openzen.zenscript.codemodel;
77

8-
import java.util.Arrays;
9-
import java.util.Map;
8+
import java.util.*;
9+
1010
import org.openzen.zencode.shared.CodePosition;
1111
import org.openzen.zenscript.codemodel.expression.CallArguments;
1212
import org.openzen.zenscript.codemodel.expression.Expression;
1313
import org.openzen.zenscript.codemodel.generic.TypeParameter;
14+
import org.openzen.zenscript.codemodel.type.ArrayTypeID;
1415
import org.openzen.zenscript.codemodel.type.BasicTypeID;
1516
import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
1617
import org.openzen.zenscript.codemodel.scope.TypeScope;
@@ -165,7 +166,11 @@ public StoredType getParameterType(boolean isVariadic, int index) {
165166

166167
public FunctionParameter getParameter(boolean isVariadic, int index) {
167168
if (isVariadic && index >= parameters.length - 1) {
168-
return parameters[parameters.length - 1];
169+
final FunctionParameter parameter = parameters[parameters.length - 1];
170+
if(parameter.type.type instanceof ArrayTypeID) {
171+
return new FunctionParameter(((ArrayTypeID) parameter.type.type).elementType, parameter.name);
172+
}
173+
return parameter;
169174
} else {
170175
return parameters[index];
171176
}
@@ -234,19 +239,33 @@ public boolean matchesExactly(CodePosition position, CallArguments arguments, Ty
234239
return false;
235240

236241
FunctionHeader header = fillGenericArguments(position, scope, arguments.typeArguments);
237-
for (int i = 0; i < header.parameters.length; i++) {
238-
if (!arguments.arguments[i].type.equals(header.parameters[i].type))
239-
return false;
240-
}
241-
242-
return true;
242+
final boolean variadicCall = header.isVariadicCall(arguments, scope);
243+
for(int i = 0; i < arguments.arguments.length; i++) {
244+
if(!arguments.arguments[i].type.equals(header.getParameterType(variadicCall, i)))
245+
return false;
246+
}
247+
248+
return true;
243249
}
244250

245251
public boolean matchesImplicitly(CodePosition position, CallArguments arguments, TypeScope scope) {
246252
if (!accepts(arguments.arguments.length))
247253
return false;
248254

249255
FunctionHeader header = fillGenericArguments(position, scope, arguments.typeArguments);
256+
if(isVariadic()) {
257+
boolean matches = true;
258+
for (int i = 0; i < arguments.arguments.length; i++) {
259+
if (!scope.getTypeMembers(arguments.arguments[i].type).canCastImplicit(header.getParameterType(true, i))) {
260+
matches = false;
261+
break;
262+
}
263+
}
264+
if(matches) {
265+
return true;
266+
}
267+
}
268+
250269
for (int i = 0; i < arguments.arguments.length; i++) {
251270
if (!scope.getTypeMembers(arguments.arguments[i].type).canCastImplicit(header.parameters[i].type))
252271
return false;
@@ -514,4 +533,43 @@ private static boolean hasUnknowns(FunctionParameter[] parameters, StoredType re
514533
public boolean accepts(int arguments) {
515534
return arguments >= this.minParameters && arguments <= this.maxParameters;
516535
}
536+
537+
@Override
538+
public boolean equals(Object o) {
539+
if(this == o)
540+
return true;
541+
if(o == null || getClass() != o.getClass())
542+
return false;
543+
544+
FunctionHeader that = (FunctionHeader) o;
545+
546+
if(minParameters != that.minParameters)
547+
return false;
548+
if(maxParameters != that.maxParameters)
549+
return false;
550+
if(hasUnknowns != that.hasUnknowns)
551+
return false;
552+
if(!Arrays.equals(typeParameters, that.typeParameters))
553+
return false;
554+
if(!returnType.equals(that.returnType))
555+
return false;
556+
if(!Arrays.equals(parameters, that.parameters))
557+
return false;
558+
if(!Objects.equals(thrownType, that.thrownType))
559+
return false;
560+
return Objects.equals(storage, that.storage);
561+
}
562+
563+
@Override
564+
public int hashCode() {
565+
int result = Arrays.hashCode(typeParameters);
566+
result = 31 * result + returnType.hashCode();
567+
result = 31 * result + Arrays.hashCode(parameters);
568+
result = 31 * result + (thrownType != null ? thrownType.hashCode() : 0);
569+
result = 31 * result + (storage != null ? storage.hashCode() : 0);
570+
result = 31 * result + minParameters;
571+
result = 31 * result + maxParameters;
572+
result = 31 * result + (hasUnknowns ? 1 : 0);
573+
return result;
574+
}
517575
}

CodeModel/src/main/java/org/openzen/zenscript/codemodel/GenericMapper.java

Lines changed: 91 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -5,94 +5,100 @@
55
*/
66
package org.openzen.zenscript.codemodel;
77

8-
import java.util.Collections;
9-
import java.util.HashMap;
10-
import java.util.Map;
11-
import org.openzen.zencode.shared.CodePosition;
12-
import org.openzen.zenscript.codemodel.generic.TypeParameter;
13-
import org.openzen.zenscript.codemodel.type.GenericTypeID;
14-
import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
15-
import org.openzen.zenscript.codemodel.type.StoredType;
8+
import org.openzen.zencode.shared.*;
9+
import org.openzen.zenscript.codemodel.generic.*;
10+
import org.openzen.zenscript.codemodel.type.*;
11+
12+
import java.util.*;
1613

1714
/**
18-
*
1915
* @author Hoofdgebruiker
2016
*/
2117
public class GenericMapper {
22-
public static final GenericMapper EMPTY = new GenericMapper(CodePosition.BUILTIN, null, Collections.emptyMap());
23-
24-
public final CodePosition position;
25-
public final GlobalTypeRegistry registry;
26-
private final Map<TypeParameter, StoredType> mapping;
27-
28-
public GenericMapper(CodePosition position, GlobalTypeRegistry registry, Map<TypeParameter, StoredType> mapping) {
29-
if (mapping == null)
30-
throw new IllegalArgumentException();
31-
32-
this.position = position;
33-
this.registry = registry;
34-
this.mapping = mapping;
35-
}
36-
37-
public Map<TypeParameter, StoredType> getMapping() {
38-
return mapping;
39-
}
40-
41-
public StoredType map(StoredType original) {
42-
return mapping.isEmpty() ? original : original.instance(this);
43-
}
44-
45-
public StoredType[] map(StoredType[] original) {
46-
if (mapping.isEmpty() || original.length == 0)
47-
return original;
48-
49-
StoredType[] mapped = new StoredType[original.length];
50-
for (int i = 0; i < original.length; i++)
51-
mapped[i] = original[i].instance(this);
52-
return mapped;
53-
}
54-
55-
public StoredType map(GenericTypeID type) {
56-
//if (!mapping.containsKey(type.parameter))
57-
// throw new IllegalStateException("No mapping found for type " + type);
58-
59-
return mapping.containsKey(type.parameter) ? mapping.get(type.parameter) : type.stored();
60-
}
61-
62-
public FunctionHeader map(FunctionHeader original) {
63-
return mapping.isEmpty() ? original : original.withGenericArguments(this);
64-
}
65-
66-
public GenericMapper getInner(CodePosition position, GlobalTypeRegistry registry, Map<TypeParameter, StoredType> mapping) {
67-
Map<TypeParameter, StoredType> resultMap = new HashMap<>(this.mapping);
68-
resultMap.putAll(mapping);
69-
return new GenericMapper(position, registry, resultMap);
70-
}
71-
72-
public GenericMapper getInner(CodePosition position, GlobalTypeRegistry registry, TypeParameter[] parameters) {
73-
Map<TypeParameter, StoredType> resultMap = new HashMap<>(this.mapping);
74-
for (TypeParameter parameter : parameters)
75-
resultMap.put(parameter, new StoredType(registry.getGeneric(parameter), null));
76-
return new GenericMapper(position, registry, resultMap);
77-
}
78-
79-
@Override
80-
public String toString() {
81-
if (mapping.isEmpty())
82-
return "{}";
83-
84-
StringBuilder result = new StringBuilder();
85-
result.append('{');
86-
boolean first = true;
87-
for (Map.Entry<TypeParameter, StoredType> entry : mapping.entrySet()) {
88-
if (first) {
89-
first = false;
90-
} else {
91-
result.append(", ");
92-
}
93-
result.append(entry.getKey().toString()).append(": ").append(entry.getValue());
94-
}
95-
result.append('}');
96-
return result.toString();
97-
}
18+
19+
public static final GenericMapper EMPTY = new GenericMapper(CodePosition.BUILTIN, null, Collections
20+
.emptyMap());
21+
22+
public final CodePosition position;
23+
public final GlobalTypeRegistry registry;
24+
private final Map<TypeParameter, StoredType> mapping;
25+
26+
public GenericMapper(CodePosition position, GlobalTypeRegistry registry, Map<TypeParameter, StoredType> mapping) {
27+
if(mapping == null)
28+
throw new IllegalArgumentException();
29+
30+
this.position = position;
31+
this.registry = registry;
32+
this.mapping = mapping;
33+
}
34+
35+
public Map<TypeParameter, StoredType> getMapping() {
36+
return mapping;
37+
}
38+
39+
public StoredType map(StoredType original) {
40+
return mapping.isEmpty() ? original : original.instance(this);
41+
}
42+
43+
public StoredType[] map(StoredType[] original) {
44+
if(mapping.isEmpty() || original.length == 0)
45+
return original;
46+
47+
StoredType[] mapped = new StoredType[original.length];
48+
for(int i = 0; i < original.length; i++)
49+
mapped[i] = original[i].instance(this);
50+
return mapped;
51+
}
52+
53+
public StoredType map(GenericTypeID type) {
54+
//if (!mapping.containsKey(type.parameter))
55+
// throw new IllegalStateException("No mapping found for type " + type);
56+
57+
return mapping.containsKey(type.parameter) ? mapping.get(type.parameter) : type.stored();
58+
}
59+
60+
public FunctionHeader map(FunctionHeader original) {
61+
return mapping.isEmpty() ? original : original.withGenericArguments(this);
62+
}
63+
64+
public GenericMapper getInner(CodePosition position, GlobalTypeRegistry registry, Map<TypeParameter, StoredType> mapping) {
65+
Map<TypeParameter, StoredType> resultMap = new HashMap<>(this.mapping);
66+
mapping.forEach((typeParameter, storedType) -> {
67+
if(resultMap.containsKey(typeParameter)) {
68+
if(storedType.type instanceof GenericTypeID && ((GenericTypeID) storedType.type).parameter == typeParameter) {
69+
return;
70+
}
71+
}
72+
resultMap.put(typeParameter, storedType);
73+
});
74+
75+
return new GenericMapper(position, registry, resultMap);
76+
}
77+
78+
public GenericMapper getInner(CodePosition position, GlobalTypeRegistry registry, TypeParameter[] parameters) {
79+
Map<TypeParameter, StoredType> resultMap = new HashMap<>(this.mapping);
80+
for(TypeParameter parameter : parameters)
81+
resultMap.put(parameter, new StoredType(registry.getGeneric(parameter), null));
82+
return new GenericMapper(position, registry, resultMap);
83+
}
84+
85+
@Override
86+
public String toString() {
87+
if(mapping.isEmpty())
88+
return "{}";
89+
90+
StringBuilder result = new StringBuilder();
91+
result.append('{');
92+
boolean first = true;
93+
for(Map.Entry<TypeParameter, StoredType> entry : mapping.entrySet()) {
94+
if(first) {
95+
first = false;
96+
} else {
97+
result.append(", ");
98+
}
99+
result.append(entry.getKey().toString()).append(": ").append(entry.getValue());
100+
}
101+
result.append('}');
102+
return result.toString();
103+
}
98104
}

CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public void normalize(TypeScope scope) {
196196
}
197197

198198
if (isDestructible() && destructor == null && !(this instanceof ExpansionDefinition)) {
199-
System.out.println("Added destructor to " + position);
199+
//System.out.println("Added destructor to " + position);
200200
destructor = new DestructorMember(position, this, Modifiers.PUBLIC);
201201
members.add(destructor);
202202
}

0 commit comments

Comments
 (0)