Explorar el Código

Improved implementation of global expressions.

Stan Hebben hace 6 años
padre
commit
86b521640c

+ 15
- 0
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/ExpressionFormatter.java Ver fichero

@@ -45,6 +45,8 @@ import org.openzen.zenscript.codemodel.expression.GetFunctionParameterExpression
45 45
 import org.openzen.zenscript.codemodel.expression.GetLocalVariableExpression;
46 46
 import org.openzen.zenscript.codemodel.expression.GetStaticFieldExpression;
47 47
 import org.openzen.zenscript.codemodel.expression.GetterExpression;
48
+import org.openzen.zenscript.codemodel.expression.GlobalCallExpression;
49
+import org.openzen.zenscript.codemodel.expression.GlobalExpression;
48 50
 import org.openzen.zenscript.codemodel.expression.InterfaceCastExpression;
49 51
 import org.openzen.zenscript.codemodel.expression.IsExpression;
50 52
 import org.openzen.zenscript.codemodel.expression.MakeConstExpression;
@@ -471,6 +473,19 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
471 473
 		result.append(expression.getter.name);
472 474
 		return new ExpressionString(result.toString(), OperatorPriority.MEMBER);
473 475
 	}
476
+	
477
+	@Override
478
+	public ExpressionString visitGlobal(GlobalExpression expression) {
479
+		return new ExpressionString(expression.name, OperatorPriority.PRIMARY);
480
+	}
481
+	
482
+	@Override
483
+	public ExpressionString visitGlobalCall(GlobalCallExpression expression) {
484
+		StringBuilder result = new StringBuilder();
485
+		result.append(expression.name);
486
+		format(result, expression.arguments);
487
+		return new ExpressionString(result.toString(), OperatorPriority.PRIMARY);
488
+	}
474 489
 
475 490
 	@Override
476 491
 	public ExpressionString visitInterfaceCast(InterfaceCastExpression expression) {

+ 4
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ExpressionVisitor.java Ver fichero

@@ -84,6 +84,10 @@ public interface ExpressionVisitor<T> {
84 84
 	
85 85
 	public T visitGetter(GetterExpression expression);
86 86
 	
87
+	public T visitGlobal(GlobalExpression expression);
88
+	
89
+	public T visitGlobalCall(GlobalCallExpression expression);
90
+	
87 91
 	public T visitInterfaceCast(InterfaceCastExpression expression);
88 92
 	
89 93
 	public T visitIs(IsExpression expression);

+ 31
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/GlobalCallExpression.java Ver fichero

@@ -0,0 +1,31 @@
1
+/*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+package org.openzen.zenscript.codemodel.expression;
7
+
8
+import org.openzen.zenscript.shared.CodePosition;
9
+
10
+/**
11
+ *
12
+ * @author Hoofdgebruiker
13
+ */
14
+public class GlobalCallExpression extends Expression {
15
+	public final String name;
16
+	public final CallArguments arguments;
17
+	public final Expression resolution;
18
+	
19
+	public GlobalCallExpression(CodePosition position, String name, CallArguments arguments, Expression resolution) {
20
+		super(position, resolution.type);
21
+		
22
+		this.name = name;
23
+		this.arguments = arguments;
24
+		this.resolution = resolution;
25
+	}
26
+
27
+	@Override
28
+	public <T> T accept(ExpressionVisitor<T> visitor) {
29
+		return visitor.visitGlobalCall(this);
30
+	}
31
+}

+ 29
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/GlobalExpression.java Ver fichero

@@ -0,0 +1,29 @@
1
+/*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+package org.openzen.zenscript.codemodel.expression;
7
+
8
+import org.openzen.zenscript.shared.CodePosition;
9
+
10
+/**
11
+ *
12
+ * @author Hoofdgebruiker
13
+ */
14
+public class GlobalExpression extends Expression {
15
+	public final String name;
16
+	public final Expression resolution;
17
+	
18
+	public GlobalExpression(CodePosition position, String name, Expression resolution) {
19
+		super(position, resolution.type);
20
+		
21
+		this.name = name;
22
+		this.resolution = resolution;
23
+	}
24
+
25
+	@Override
26
+	public <T> T accept(ExpressionVisitor<T> visitor) {
27
+		return visitor.visitGlobal(this);
28
+	}
29
+}

+ 58
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/partial/PartialGlobalExpression.java Ver fichero

@@ -0,0 +1,58 @@
1
+/*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+package org.openzen.zenscript.codemodel.partial;
7
+
8
+import java.util.List;
9
+import org.openzen.zenscript.codemodel.FunctionHeader;
10
+import org.openzen.zenscript.codemodel.expression.CallArguments;
11
+import org.openzen.zenscript.codemodel.expression.Expression;
12
+import org.openzen.zenscript.codemodel.expression.GlobalCallExpression;
13
+import org.openzen.zenscript.codemodel.expression.GlobalExpression;
14
+import org.openzen.zenscript.codemodel.scope.TypeScope;
15
+import org.openzen.zenscript.codemodel.type.GenericName;
16
+import org.openzen.zenscript.codemodel.type.ITypeID;
17
+import org.openzen.zenscript.shared.CodePosition;
18
+
19
+/**
20
+ *
21
+ * @author Hoofdgebruiker
22
+ */
23
+public class PartialGlobalExpression implements IPartialExpression {
24
+	private final CodePosition position;
25
+	private final String name;
26
+	private final IPartialExpression resolution;
27
+	
28
+	public PartialGlobalExpression(CodePosition position, String name, IPartialExpression resolution) {
29
+		this.position = position;
30
+		this.name = name;
31
+		this.resolution = resolution;
32
+	}
33
+	
34
+	@Override
35
+	public Expression eval() {
36
+		return new GlobalExpression(position, name, resolution.eval());
37
+	}
38
+
39
+	@Override
40
+	public List<ITypeID>[] predictCallTypes(TypeScope scope, List<ITypeID> hints, int arguments) {
41
+		return resolution.predictCallTypes(scope, hints, arguments);
42
+	}
43
+
44
+	@Override
45
+	public List<FunctionHeader> getPossibleFunctionHeaders(TypeScope scope, List<ITypeID> hints, int arguments) {
46
+		return resolution.getPossibleFunctionHeaders(scope, hints, arguments);
47
+	}
48
+
49
+	@Override
50
+	public IPartialExpression getMember(CodePosition position, TypeScope scope, List<ITypeID> hints, GenericName name) {
51
+		return eval().getMember(position, scope, hints, name);
52
+	}
53
+
54
+	@Override
55
+	public Expression call(CodePosition position, TypeScope scope, List<ITypeID> hints, CallArguments arguments) {
56
+		return new GlobalCallExpression(position, name, arguments, resolution.call(position, scope, hints, arguments));
57
+	}
58
+}

+ 10
- 0
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaExpressionVisitor.java Ver fichero

@@ -262,6 +262,16 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
262 262
     public Void visitGetter(GetterExpression expression) {
263 263
         return null;
264 264
     }
265
+	
266
+	@Override
267
+	public Void visitGlobal(GlobalExpression expression) {
268
+		return expression.resolution.accept(this);
269
+	}
270
+	
271
+	@Override
272
+	public Void visitGlobalCall(GlobalCallExpression expression) {
273
+		return expression.resolution.accept(this);
274
+	}
265 275
 
266 276
     @Override
267 277
     public Void visitInterfaceCast(InterfaceCastExpression expression) {

+ 5
- 2
Linker/src/main/java/org/openzen/zenscript/linker/FileScope.java Ver fichero

@@ -17,6 +17,7 @@ import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
17 17
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
18 18
 import org.openzen.zenscript.codemodel.expression.Expression;
19 19
 import org.openzen.zenscript.codemodel.partial.IPartialExpression;
20
+import org.openzen.zenscript.codemodel.partial.PartialGlobalExpression;
20 21
 import org.openzen.zenscript.codemodel.partial.PartialTypeExpression;
21 22
 import org.openzen.zenscript.codemodel.statement.LoopStatement;
22 23
 import org.openzen.zenscript.codemodel.type.DefinitionTypeID;
@@ -74,8 +75,10 @@ public class FileScope extends BaseScope {
74 75
 		if (localDefinition != null)
75 76
 			return new PartialTypeExpression(position, globalRegistry.getForDefinition(localDefinition, name.arguments));
76 77
 		
77
-		if (globalSymbols.containsKey(name.name))
78
-			return globalSymbols.get(name.name).getExpression(position, globalRegistry, name.arguments);
78
+		if (globalSymbols.containsKey(name.name)) {
79
+			IPartialExpression resolution = globalSymbols.get(name.name).getExpression(position, globalRegistry, name.arguments);
80
+			return new PartialGlobalExpression(position, name.name, resolution);
81
+		}
79 82
 		
80 83
 		return rootPackage.getMember(position, globalRegistry, name);
81 84
 	}

+ 12
- 0
Validator/src/main/java/org/openzen/zenscript/validator/visitors/ExpressionValidator.java Ver fichero

@@ -47,6 +47,8 @@ import org.openzen.zenscript.codemodel.expression.GetFunctionParameterExpression
47 47
 import org.openzen.zenscript.codemodel.expression.GetLocalVariableExpression;
48 48
 import org.openzen.zenscript.codemodel.expression.GetStaticFieldExpression;
49 49
 import org.openzen.zenscript.codemodel.expression.GetterExpression;
50
+import org.openzen.zenscript.codemodel.expression.GlobalCallExpression;
51
+import org.openzen.zenscript.codemodel.expression.GlobalExpression;
50 52
 import org.openzen.zenscript.codemodel.expression.InterfaceCastExpression;
51 53
 import org.openzen.zenscript.codemodel.expression.IsExpression;
52 54
 import org.openzen.zenscript.codemodel.expression.MakeConstExpression;
@@ -382,6 +384,16 @@ public class ExpressionValidator implements ExpressionVisitor<Boolean> {
382 384
 	public Boolean visitGetter(GetterExpression expression) {
383 385
 		return expression.target.accept(this);
384 386
 	}
387
+	
388
+	@Override
389
+	public Boolean visitGlobal(GlobalExpression expression) {
390
+		return expression.resolution.accept(this);
391
+	}
392
+	
393
+	@Override
394
+	public Boolean visitGlobalCall(GlobalCallExpression expression) {
395
+		return expression.resolution.accept(this);
396
+	}
385 397
 
386 398
 	@Override
387 399
 	public Boolean visitInterfaceCast(InterfaceCastExpression expression) {

Loading…
Cancelar
Guardar