Skip to content

Commit

Permalink
Fix getters not being found if not referenced with this.
Browse files Browse the repository at this point in the history
  • Loading branch information
stanhebben committed Apr 5, 2024
1 parent d76a28d commit ccb2897
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 3 deletions.
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.openzen.zenscript.codemodel.GenericName;
import org.openzen.zenscript.codemodel.compilation.expression.AbstractCompilingExpression;
import org.openzen.zenscript.codemodel.compilation.expression.CompilingThisExpression;
import org.openzen.zenscript.codemodel.compilation.expression.InstanceFieldCompilingExpression;
import org.openzen.zenscript.codemodel.compilation.expression.InstanceMemberCompilingExpression;
import org.openzen.zenscript.codemodel.expression.*;
import org.openzen.zenscript.codemodel.expression.switchvalue.ErrorSwitchValue;
Expand Down Expand Up @@ -37,14 +38,28 @@ public CompilingExpression compile(ExpressionCompiler compiler) {
return new CompilingResolved(compiler, position, name, resolved.get());
} else {
if (compiler.getThisType().isPresent()) {
Optional<ResolvedType.Field> field = compiler.resolve(compiler.getThisType().get()).findField(name);
if (field.isPresent()) {
ResolvedType resolvedThis = compiler.resolve(compiler.getThisType().get());

Optional<InstanceCallable> getter = resolvedThis.findGetter(name);
if (getter.isPresent()) {
return new InstanceMemberCompilingExpression(
compiler,
position,
new CompilingThisExpression(compiler, position, compiler.getThisType().get()),
new GenericName(name, typeArguments));
}

Optional<ResolvedType.Field> field = resolvedThis.findField(name);
if (field.isPresent()) {
if (typeArguments.length > 0) {
return compiler.invalid(position, CompileErrors.typeArgumentsNotAllowedHere());
}
return new InstanceFieldCompilingExpression(
compiler,
position,
new CompilingThisExpression(compiler, position, compiler.getThisType().get()),
field.get());
}
}
return new CompilingUnresolved(compiler, position, name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Foo {
this() {}

printMe(): void {
//println(this.test);
println(this.test);
}
}

Expand Down
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);
}
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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#error: 3:UNDEFINED_VARIABLE

println(nothing);
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);
}

0 comments on commit ccb2897

Please sign in to comment.