-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
298 additions
and
9 deletions.
There are no files selected for viewing
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,53 @@ | ||
package leekscript.common; | ||
|
||
public class SetType extends Type { | ||
|
||
private Type type; | ||
|
||
public SetType(Type type) { | ||
super(type == Type.VOID ? "Set<empty>" : type == Type.ANY ? "Set" : "Set<" + type.toString() + ">", "h", "SetLeekValue", "SetLeekValue", "new SetLeekValue()"); | ||
this.type = type; | ||
} | ||
|
||
@Override | ||
public CastType accepts(Type type) { | ||
if (type instanceof SetType at) { | ||
if (at.type == Type.VOID) return CastType.UPCAST; | ||
if (this.type == Type.VOID) return CastType.UPCAST; | ||
var cast = this.type.accepts(at.type); | ||
if (cast.ordinal() >= CastType.SAFE_DOWNCAST.ordinal()) return CastType.UNSAFE_DOWNCAST; | ||
return cast; | ||
} | ||
return super.accepts(type); | ||
} | ||
|
||
@Override | ||
public Type element() { | ||
return type; | ||
} | ||
|
||
public boolean isSet() { | ||
return true; | ||
} | ||
|
||
public boolean canBeIterable() { | ||
return true; | ||
} | ||
|
||
public boolean isIterable() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return this.type.hashCode() * 31 + 1; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (object instanceof SetType at) { | ||
return this.type.equals(at.type); | ||
} | ||
return false; | ||
} | ||
} |
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
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
103 changes: 103 additions & 0 deletions
103
src/main/java/leekscript/compiler/expression/LeekSet.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,103 @@ | ||
package leekscript.compiler.expression; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.stream.Collectors; | ||
|
||
import leekscript.common.Type; | ||
import leekscript.compiler.Hover; | ||
import leekscript.compiler.Token; | ||
import leekscript.compiler.JavaWriter; | ||
import leekscript.compiler.Location; | ||
import leekscript.compiler.WordCompiler; | ||
import leekscript.compiler.bloc.MainLeekBlock; | ||
import leekscript.compiler.exceptions.LeekCompilerException; | ||
|
||
public class LeekSet extends Expression { | ||
|
||
private final ArrayList<Expression> mValues = new ArrayList<Expression>(); | ||
private Token openingToken; | ||
private Token closingToken; | ||
|
||
public Type type = Type.SET; | ||
|
||
public LeekSet(Token openingToken) { | ||
this.openingToken = openingToken; | ||
} | ||
|
||
public void addValue(Expression param) { | ||
mValues.add(param); | ||
} | ||
|
||
public void setClosingToken(Token closingToken) { | ||
this.closingToken = closingToken; | ||
closingToken.setExpression(this); | ||
openingToken.setExpression(this); | ||
} | ||
|
||
@Override | ||
public int getNature() { | ||
return ARRAY; | ||
} | ||
|
||
@Override | ||
public Type getType() { | ||
return type; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return mValues.stream().map(value -> value.toString()).collect(Collectors.joining(", ", "<", ">")); | ||
} | ||
|
||
@Override | ||
public boolean validExpression(WordCompiler compiler, MainLeekBlock mainblock) throws LeekExpressionException { | ||
for (Expression parameter : mValues) { | ||
parameter.validExpression(compiler, mainblock); | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public void preAnalyze(WordCompiler compiler) throws LeekCompilerException { | ||
for (var value : mValues) { | ||
value.preAnalyze(compiler); | ||
} | ||
} | ||
|
||
@Override | ||
public void analyze(WordCompiler compiler) throws LeekCompilerException { | ||
operations = 0; | ||
|
||
var types = new HashSet<Type>(); | ||
for (var value : mValues) { | ||
value.analyze(compiler); | ||
operations += 2 + value.getOperations(); | ||
types.add(value.getType()); | ||
} | ||
|
||
this.type = Type.set(types.size() == 0 ? Type.VOID : Type.compound(types)); | ||
} | ||
|
||
@Override | ||
public void writeJavaCode(MainLeekBlock mainblock, JavaWriter writer) { | ||
writer.addCode("new SetLeekValue(" + writer.getAIThis() + ", new Object[] { "); | ||
for (int i = 0; i < mValues.size(); i++) { | ||
if (i != 0) writer.addCode(", "); | ||
mValues.get(i).writeJavaCode(mainblock, writer); | ||
} | ||
writer.addCode(" })"); | ||
} | ||
|
||
@Override | ||
public Location getLocation() { | ||
return new Location(openingToken.getLocation(), closingToken.getLocation()); | ||
} | ||
|
||
@Override | ||
public Hover hover(Token token) { | ||
var hover = new Hover(getType(), getLocation(), toString()); | ||
hover.setSize(mValues.size()); | ||
return hover; | ||
} | ||
} |
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
Oops, something went wrong.