Browse Source

Improved implementation of global expressions.

Stan Hebben 6 years ago
parent
commit
86b521640c

+ 15
- 0
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/ExpressionFormatter.java View File

45
 import org.openzen.zenscript.codemodel.expression.GetLocalVariableExpression;
45
 import org.openzen.zenscript.codemodel.expression.GetLocalVariableExpression;
46
 import org.openzen.zenscript.codemodel.expression.GetStaticFieldExpression;
46
 import org.openzen.zenscript.codemodel.expression.GetStaticFieldExpression;
47
 import org.openzen.zenscript.codemodel.expression.GetterExpression;
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
 import org.openzen.zenscript.codemodel.expression.InterfaceCastExpression;
50
 import org.openzen.zenscript.codemodel.expression.InterfaceCastExpression;
49
 import org.openzen.zenscript.codemodel.expression.IsExpression;
51
 import org.openzen.zenscript.codemodel.expression.IsExpression;
50
 import org.openzen.zenscript.codemodel.expression.MakeConstExpression;
52
 import org.openzen.zenscript.codemodel.expression.MakeConstExpression;
471
 		result.append(expression.getter.name);
473
 		result.append(expression.getter.name);
472
 		return new ExpressionString(result.toString(), OperatorPriority.MEMBER);
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
 	@Override
490
 	@Override
476
 	public ExpressionString visitInterfaceCast(InterfaceCastExpression expression) {
491
 	public ExpressionString visitInterfaceCast(InterfaceCastExpression expression) {

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

84
 	
84
 	
85
 	public T visitGetter(GetterExpression expression);
85
 	public T visitGetter(GetterExpression expression);
86
 	
86
 	
87
+	public T visitGlobal(GlobalExpression expression);
88
+	
89
+	public T visitGlobalCall(GlobalCallExpression expression);
90
+	
87
 	public T visitInterfaceCast(InterfaceCastExpression expression);
91
 	public T visitInterfaceCast(InterfaceCastExpression expression);
88
 	
92
 	
89
 	public T visitIs(IsExpression expression);
93
 	public T visitIs(IsExpression expression);

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

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 View File

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 View File

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 View File

262
     public Void visitGetter(GetterExpression expression) {
262
     public Void visitGetter(GetterExpression expression) {
263
         return null;
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
     @Override
276
     @Override
267
     public Void visitInterfaceCast(InterfaceCastExpression expression) {
277
     public Void visitInterfaceCast(InterfaceCastExpression expression) {

+ 5
- 2
Linker/src/main/java/org/openzen/zenscript/linker/FileScope.java View File

17
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
17
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
18
 import org.openzen.zenscript.codemodel.expression.Expression;
18
 import org.openzen.zenscript.codemodel.expression.Expression;
19
 import org.openzen.zenscript.codemodel.partial.IPartialExpression;
19
 import org.openzen.zenscript.codemodel.partial.IPartialExpression;
20
+import org.openzen.zenscript.codemodel.partial.PartialGlobalExpression;
20
 import org.openzen.zenscript.codemodel.partial.PartialTypeExpression;
21
 import org.openzen.zenscript.codemodel.partial.PartialTypeExpression;
21
 import org.openzen.zenscript.codemodel.statement.LoopStatement;
22
 import org.openzen.zenscript.codemodel.statement.LoopStatement;
22
 import org.openzen.zenscript.codemodel.type.DefinitionTypeID;
23
 import org.openzen.zenscript.codemodel.type.DefinitionTypeID;
74
 		if (localDefinition != null)
75
 		if (localDefinition != null)
75
 			return new PartialTypeExpression(position, globalRegistry.getForDefinition(localDefinition, name.arguments));
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
 		return rootPackage.getMember(position, globalRegistry, name);
83
 		return rootPackage.getMember(position, globalRegistry, name);
81
 	}
84
 	}

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

47
 import org.openzen.zenscript.codemodel.expression.GetLocalVariableExpression;
47
 import org.openzen.zenscript.codemodel.expression.GetLocalVariableExpression;
48
 import org.openzen.zenscript.codemodel.expression.GetStaticFieldExpression;
48
 import org.openzen.zenscript.codemodel.expression.GetStaticFieldExpression;
49
 import org.openzen.zenscript.codemodel.expression.GetterExpression;
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
 import org.openzen.zenscript.codemodel.expression.InterfaceCastExpression;
52
 import org.openzen.zenscript.codemodel.expression.InterfaceCastExpression;
51
 import org.openzen.zenscript.codemodel.expression.IsExpression;
53
 import org.openzen.zenscript.codemodel.expression.IsExpression;
52
 import org.openzen.zenscript.codemodel.expression.MakeConstExpression;
54
 import org.openzen.zenscript.codemodel.expression.MakeConstExpression;
382
 	public Boolean visitGetter(GetterExpression expression) {
384
 	public Boolean visitGetter(GetterExpression expression) {
383
 		return expression.target.accept(this);
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
 	@Override
398
 	@Override
387
 	public Boolean visitInterfaceCast(InterfaceCastExpression expression) {
399
 	public Boolean visitInterfaceCast(InterfaceCastExpression expression) {

Loading…
Cancel
Save