-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix getters not being found if not referenced with this.
- Loading branch information
1 parent
d76a28d
commit ccb2897
Showing
8 changed files
with
183 additions
and
3 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
.../openzen/zenscript/codemodel/compilation/expression/InstanceFieldCompilingExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package org.openzen.zenscript.codemodel.compilation.expression; | ||
|
||
import org.openzen.zencode.shared.CodePosition; | ||
import org.openzen.zenscript.codemodel.GenericName; | ||
import org.openzen.zenscript.codemodel.OperatorType; | ||
import org.openzen.zenscript.codemodel.compilation.*; | ||
import org.openzen.zenscript.codemodel.expression.Expression; | ||
import org.openzen.zenscript.codemodel.ssa.CodeBlockStatement; | ||
import org.openzen.zenscript.codemodel.ssa.SSAVariableCollector; | ||
import org.openzen.zenscript.codemodel.type.TypeID; | ||
|
||
import java.util.Optional; | ||
|
||
public class InstanceFieldCompilingExpression extends AbstractCompilingExpression { | ||
private final CompilingExpression instance; | ||
private final ResolvedType.Field field; | ||
|
||
public InstanceFieldCompilingExpression(ExpressionCompiler compiler, CodePosition position, CompilingExpression instance, ResolvedType.Field field) { | ||
super(compiler, position); | ||
|
||
this.instance = instance; | ||
this.field = field; | ||
} | ||
|
||
@Override | ||
public Expression eval() { | ||
Expression instance = this.instance.eval(); | ||
return field.get(compiler.at(position), instance); | ||
} | ||
|
||
@Override | ||
public CastedExpression cast(CastedEval cast) { | ||
return cast.of(eval()); | ||
} | ||
|
||
@Override | ||
public Optional<CompilingCallable> call() { | ||
Expression instance = this.instance.eval(); | ||
ResolvedType fieldType = compiler.resolve(field.getType()); | ||
Optional<CompilingCallable> result = fieldType | ||
.findOperator(OperatorType.CALL) | ||
.map(method -> method.bind(compiler, field.get(compiler.at(position), instance), TypeID.NONE)); | ||
|
||
return Optional.of(result.orElseGet(() -> new InvalidCompilingExpression(compiler, position, CompileErrors.cannotCall()))); | ||
} | ||
|
||
@Override | ||
public CompilingExpression assign(CompilingExpression value) { | ||
Expression instance = this.instance.eval(); | ||
return new FieldSetter(compiler, position, instance, field, value); | ||
} | ||
|
||
@Override | ||
public void collect(SSAVariableCollector collector) { | ||
// TODO | ||
} | ||
|
||
@Override | ||
public void linkVariables(CodeBlockStatement.VariableLinker linker) { | ||
// TODO | ||
} | ||
|
||
private static class FieldSetter extends AbstractCompilingExpression { | ||
private final Expression instance; | ||
private final ResolvedType.Field field; | ||
private final CompilingExpression value; | ||
|
||
public FieldSetter( | ||
ExpressionCompiler compiler, | ||
CodePosition position, | ||
Expression instance, | ||
ResolvedType.Field field, | ||
CompilingExpression value | ||
) { | ||
super(compiler, position); | ||
|
||
this.instance = instance; | ||
this.field = field; | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public Expression eval() { | ||
return field.set(compiler.at(position), instance, value.eval()); | ||
} | ||
|
||
@Override | ||
public CastedExpression cast(CastedEval cast) { | ||
return cast.of(eval()); | ||
} | ||
|
||
@Override | ||
public void collect(SSAVariableCollector collector) { | ||
// TODO | ||
//instance.collect(collector); | ||
value.collect(collector); | ||
} | ||
|
||
@Override | ||
public void linkVariables(CodeBlockStatement.VariableLinker linker) { | ||
value.linkVariables(linker); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ class Foo { | |
this() {} | ||
|
||
printMe(): void { | ||
//println(this.test); | ||
println(this.test); | ||
} | ||
} | ||
|
||
|
19 changes: 19 additions & 0 deletions
19
ScriptingEngineTester/src/main/resources/zencode-tests/arrays/arrays-map-1.zc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#output: 2 | ||
#output: 4 | ||
#output: 6 | ||
|
||
public expand int[] { | ||
public map(projection as function(value as int) as int) as int[] { | ||
var len = length; | ||
var values = new int[](len); | ||
for i, value in this | ||
values[i] = projection(value); | ||
return values; | ||
} | ||
} | ||
|
||
var a = [1, 2, 3]; | ||
var b = a.map(x => x * 2); | ||
for item in b { | ||
println(item); | ||
} |
26 changes: 26 additions & 0 deletions
26
ScriptingEngineTester/src/main/resources/zencode-tests/arrays/arrays-map-2.zc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#output: 2 | ||
#output: 4 | ||
#output: 6 | ||
|
||
public expand <T> T[] { | ||
public map<U>(projection as function(value as T) as U) as U[] { | ||
var values = new U[](length); | ||
for i, value in this | ||
values[i] = projection(value); | ||
return values; | ||
} | ||
|
||
[Native("mapKeyValues")] | ||
public map<U>(projection as function(index as usize, value as T) as U) as U[] { | ||
var values = new U[](length); | ||
for i, value in this | ||
values[i] = projection(i, value); | ||
return values; | ||
} | ||
} | ||
|
||
var a = [1, 2, 3]; | ||
var b = a.map<int>(x => x * 2); | ||
for item in b { | ||
println(item); | ||
} |
3 changes: 3 additions & 0 deletions
3
ScriptingEngineTester/src/main/resources/zencode-tests/statements/no-such-variable.zc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#error: 3:UNDEFINED_VARIABLE | ||
|
||
println(nothing); |
File renamed without changes.
13 changes: 13 additions & 0 deletions
13
ScriptingEngineTester/src/main/resources/zencode-tests/statements/try-catch-2.zc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#output: TestException | ||
#output: test | ||
|
||
function break() as void throws TestException { | ||
throw new TestException("test"); | ||
} | ||
|
||
try { | ||
break(); | ||
} catch ex { | ||
println(typeof(ex)); | ||
println(ex.message); | ||
} |