mirror of
https://github.com/GTNewHorizons/CraftTweaker.git
synced 2026-11-19 11:49:05 +00:00
format all the things!
This commit is contained in:
1 parent
54ec75fd3d
commit
b36c6dade6
377 files changed
+6833
-6772
No files matched your search
@@ -18,14 +18,14 @@ import stanhebben.zenscript.symbols.IZenSymbol;
|
||||
*/
|
||||
public interface IZenCompileEnvironment {
|
||||
public IZenErrorLogger getErrorLogger();
|
||||
|
||||
|
||||
public IZenSymbol getGlobal(String name);
|
||||
|
||||
|
||||
public IZenSymbol getDollar(String name);
|
||||
|
||||
|
||||
public IZenSymbol getBracketed(IEnvironmentGlobal environment, List<Token> tokens);
|
||||
|
||||
|
||||
public TypeRegistry getTypeRegistry();
|
||||
|
||||
|
||||
public TypeExpansion getExpansion(String type);
|
||||
}
|
||||
@@ -16,7 +16,7 @@ public interface IZenErrorLogger {
|
||||
* @param message error message
|
||||
*/
|
||||
public void error(ZenPosition position, String message);
|
||||
|
||||
|
||||
/**
|
||||
* Called when a warning is generated during compilation.
|
||||
*
|
||||
|
||||
@@ -34,8 +34,8 @@ import static stanhebben.zenscript.util.ZenTypeUtil.signature;
|
||||
import stanhebben.zenscript.value.IAny;
|
||||
|
||||
/**
|
||||
* Type expansions provide additional members for existing types. They can
|
||||
* add members, casters or operators and these will be available to all compiling
|
||||
* Type expansions provide additional members for existing types. They can add
|
||||
* members, casters or operators and these will be available to all compiling
|
||||
* scripts.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
@@ -48,7 +48,7 @@ public class TypeExpansion {
|
||||
private final List<ZenNativeOperator> trinaryOperators;
|
||||
private final List<ZenNativeOperator> binaryOperators;
|
||||
private final List<ZenNativeOperator> unaryOperators;
|
||||
|
||||
|
||||
/**
|
||||
* Creates an empty type expansion. Use the expand method to register the
|
||||
* actual expansions.
|
||||
@@ -57,7 +57,7 @@ public class TypeExpansion {
|
||||
*/
|
||||
public TypeExpansion(String type) {
|
||||
this.type = type;
|
||||
|
||||
|
||||
members = new HashMap<String, ZenExpandMember>();
|
||||
staticMembers = new HashMap<String, ZenExpandMember>();
|
||||
casters = new ArrayList<ZenExpandCaster>();
|
||||
@@ -65,7 +65,7 @@ public class TypeExpansion {
|
||||
binaryOperators = new ArrayList<ZenNativeOperator>();
|
||||
unaryOperators = new ArrayList<ZenNativeOperator>();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Added a native type expansion. The provided class must contain the proper
|
||||
* annotations for the required expansions, else nothing will happen. Each
|
||||
@@ -78,7 +78,7 @@ public class TypeExpansion {
|
||||
public void expand(Class cls, ITypeRegistry types) {
|
||||
for (Method method : cls.getMethods()) {
|
||||
String methodName = method.getName();
|
||||
|
||||
|
||||
for (Annotation annotation : method.getAnnotations()) {
|
||||
if (annotation instanceof ZenCaster) {
|
||||
checkStatic(method);
|
||||
@@ -87,7 +87,7 @@ public class TypeExpansion {
|
||||
checkStatic(method);
|
||||
ZenGetter getterAnnotation = (ZenGetter) annotation;
|
||||
String name = getterAnnotation.value().length() == 0 ? method.getName() : getterAnnotation.value();
|
||||
|
||||
|
||||
if (!members.containsKey(name)) {
|
||||
members.put(name, new ZenExpandMember(type, name));
|
||||
}
|
||||
@@ -96,7 +96,7 @@ public class TypeExpansion {
|
||||
checkStatic(method);
|
||||
ZenSetter setterAnnotation = (ZenSetter) annotation;
|
||||
String name = setterAnnotation.value().length() == 0 ? method.getName() : setterAnnotation.value();
|
||||
|
||||
|
||||
if (!members.containsKey(name)) {
|
||||
members.put(name, new ZenExpandMember(type, name));
|
||||
}
|
||||
@@ -174,7 +174,7 @@ public class TypeExpansion {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Registers all casting rules to the given delegate.
|
||||
*
|
||||
@@ -186,7 +186,7 @@ public class TypeExpansion {
|
||||
caster.constructCastingRules(environment, rules);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves a caster from this expansion. May return null if no suitable
|
||||
* caster was available.
|
||||
@@ -203,18 +203,18 @@ public class TypeExpansion {
|
||||
return caster;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (ZenExpandCaster caster : casters) {
|
||||
if (caster.getTarget().canCastImplicit(type, environment)) {
|
||||
return caster;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves a unary operator from this expansion. May return null if no
|
||||
* Retrieves a unary operator from this expansion. May return null if no
|
||||
* suitable operator was available.
|
||||
*
|
||||
* @param position calling position
|
||||
@@ -233,10 +233,10 @@ public class TypeExpansion {
|
||||
return new ExpressionCallStatic(position, environment, op.getMethod(), value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves a binary operator from this expansion. May return null if no
|
||||
* suitable operator was available.
|
||||
@@ -259,10 +259,10 @@ public class TypeExpansion {
|
||||
return new ExpressionCallStatic(position, environment, op.getMethod(), left, right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves a ternary operator from this expansion. May return null if no
|
||||
* suitable operator was available.
|
||||
@@ -287,10 +287,10 @@ public class TypeExpansion {
|
||||
return new ExpressionCallStatic(position, environment, op.getMethod(), first, second, third);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves an instance member from this expansion. May return null if no
|
||||
* suitable member was available.
|
||||
@@ -299,7 +299,8 @@ public class TypeExpansion {
|
||||
* @param environment compile environment
|
||||
* @param value instance value
|
||||
* @param member member name
|
||||
* @return resulting member expression, or null if no such member was available
|
||||
* @return resulting member expression, or null if no such member was
|
||||
* available
|
||||
*/
|
||||
public IPartialExpression instanceMember(
|
||||
ZenPosition position,
|
||||
@@ -309,10 +310,10 @@ public class TypeExpansion {
|
||||
if (members.containsKey(member)) {
|
||||
return members.get(member).instance(position, environment, value);
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves a static member from this expansion. May return null if no
|
||||
* suitable member was available.
|
||||
@@ -320,25 +321,26 @@ public class TypeExpansion {
|
||||
* @param position calling position
|
||||
* @param environment compile environment
|
||||
* @param member member name
|
||||
* @return resulting static member expression, or null if no such member was available
|
||||
* @return resulting static member expression, or null if no such member was
|
||||
* available
|
||||
*/
|
||||
public IPartialExpression staticMember(ZenPosition position, IEnvironmentGlobal environment, String member) {
|
||||
if (staticMembers.containsKey(member)) {
|
||||
return staticMembers.get(member).instance(position, environment);
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public void compileAnyCast(ZenType type, MethodOutput output, IEnvironmentGlobal environment, int localValue, int localClass) {
|
||||
if (type == null)
|
||||
throw new IllegalArgumentException("type cannot be null");
|
||||
|
||||
|
||||
Type asmType = type.toASMType();
|
||||
if (asmType == null) {
|
||||
throw new RuntimeException("type has no asm type");
|
||||
}
|
||||
|
||||
|
||||
for (ZenExpandCaster caster : casters) {
|
||||
Label skip = new Label();
|
||||
output.loadObject(localClass);
|
||||
@@ -349,13 +351,13 @@ public class TypeExpansion {
|
||||
output.returnType(caster.getTarget().toASMType());
|
||||
output.label(skip);
|
||||
}
|
||||
|
||||
|
||||
for (ZenExpandCaster caster : casters) {
|
||||
String casterAny = caster.getTarget().getAnyClassName(environment);
|
||||
if (casterAny == null)
|
||||
// TODO: make sure this isn't necessary
|
||||
continue;
|
||||
|
||||
|
||||
Label skip = new Label();
|
||||
output.loadObject(localClass);
|
||||
output.invokeStatic(casterAny, "rtCanCastImplicit", "(Ljava/lang/Class;)Z");
|
||||
@@ -368,7 +370,7 @@ public class TypeExpansion {
|
||||
output.label(skip);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void compileAnyCanCastImplicit(ZenType type, MethodOutput output, IEnvironmentGlobal environment, int localClass) {
|
||||
for (ZenExpandCaster caster : casters) {
|
||||
Label skip = new Label();
|
||||
@@ -379,14 +381,14 @@ public class TypeExpansion {
|
||||
output.returnInt();
|
||||
output.label(skip);
|
||||
}
|
||||
|
||||
|
||||
for (ZenExpandCaster caster : casters) {
|
||||
String casterAny = caster.getTarget().getAnyClassName(environment);
|
||||
if (casterAny == null) {
|
||||
// TODO: make sure no type ever does this
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
Label skip = new Label();
|
||||
output.loadObject(localClass);
|
||||
output.invokeStatic(casterAny, "rtCanCastImplicit", "(Ljava/lang/Class;)Z");
|
||||
@@ -396,12 +398,12 @@ public class TypeExpansion {
|
||||
output.label(skip);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean compileAnyUnary(MethodOutput output, OperatorType type, IEnvironmentMethod environment) {
|
||||
for (ZenNativeOperator operator : unaryOperators) {
|
||||
if (operator.getOperator() == type) {
|
||||
ZenType returnType = operator.getMethod().getReturnType();
|
||||
|
||||
|
||||
output.loadObject(0);
|
||||
operator.getMethod().invokeStatic(output);
|
||||
output.invokeStatic(
|
||||
@@ -412,14 +414,14 @@ public class TypeExpansion {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// #######################
|
||||
// ### Private methods ###
|
||||
// #######################
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the given method is static. Throws an exception if not.
|
||||
*
|
||||
|
||||
@@ -26,8 +26,8 @@ import stanhebben.zenscript.type.ZenType;
|
||||
* <li>A set of statuments</li>
|
||||
* </ul>
|
||||
*
|
||||
* This parsed file cannot be executed by itself, but it can be compiled into
|
||||
* a module, possibly together with other files.
|
||||
* This parsed file cannot be executed by itself, but it can be compiled into a
|
||||
* module, possibly together with other files.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
@@ -38,7 +38,7 @@ public class ZenParsedFile {
|
||||
private final Map<String, ParsedFunction> functions;
|
||||
private final List<Statement> statements;
|
||||
private final IEnvironmentGlobal environmentScript;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs and parses a given file.
|
||||
*
|
||||
@@ -50,43 +50,43 @@ public class ZenParsedFile {
|
||||
public ZenParsedFile(String filename, String classname, ZenTokener tokener, IEnvironmentGlobal environment) {
|
||||
this.filename = filename;
|
||||
this.classname = classname;
|
||||
|
||||
|
||||
imports = new ArrayList<Import>();
|
||||
functions = new HashMap<String, ParsedFunction>();
|
||||
statements = new ArrayList<Statement>();
|
||||
environmentScript = new EnvironmentScript(environment);
|
||||
|
||||
|
||||
tokener.setFile(this);
|
||||
|
||||
|
||||
while (tokener.peek() != null && tokener.peek().getType() == T_IMPORT) {
|
||||
Token start = tokener.next();
|
||||
|
||||
|
||||
List<String> importName = new ArrayList<String>();
|
||||
Token tName = tokener.required(T_ID, "identifier expected");
|
||||
importName.add(tName.getValue());
|
||||
|
||||
|
||||
while (tokener.optional(T_DOT) != null) {
|
||||
Token tNamePart = tokener.required(T_ID, "identifier expected");
|
||||
importName.add(tNamePart.getValue());
|
||||
}
|
||||
|
||||
|
||||
String rename = null;
|
||||
if (tokener.optional(T_AS) != null) {
|
||||
Token tRename = tokener.required(T_ID, "identifier expected");
|
||||
rename = tRename.getValue();
|
||||
}
|
||||
|
||||
|
||||
tokener.required(T_SEMICOLON, "; expected");
|
||||
|
||||
|
||||
imports.add(new Import(start.getPosition(), importName, rename));
|
||||
}
|
||||
|
||||
|
||||
for (Import imprt : imports) {
|
||||
List<String> name = imprt.getName();
|
||||
IPartialExpression type = null;
|
||||
|
||||
|
||||
StringBuilder nameSoFar = new StringBuilder();
|
||||
|
||||
|
||||
for (String part : name) {
|
||||
if (type == null) {
|
||||
nameSoFar.append(part);
|
||||
@@ -104,7 +104,7 @@ public class ZenParsedFile {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (type != null) {
|
||||
IZenSymbol symbol = type.toSymbol();
|
||||
if (symbol == null) {
|
||||
@@ -116,7 +116,7 @@ public class ZenParsedFile {
|
||||
environmentScript.putValue(imprt.getRename(), new SymbolType(ZenType.ANY), imprt.getPosition());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
while (tokener.hasNext()) {
|
||||
Token next = tokener.peek();
|
||||
if (next.getType() == T_FUNCTION) {
|
||||
@@ -130,11 +130,11 @@ public class ZenParsedFile {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public IEnvironmentGlobal getEnvironment() {
|
||||
return environmentScript;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the output classname for this file.
|
||||
*
|
||||
@@ -143,7 +143,7 @@ public class ZenParsedFile {
|
||||
public String getClassName() {
|
||||
return classname;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the input filename for this file.
|
||||
*
|
||||
@@ -152,7 +152,7 @@ public class ZenParsedFile {
|
||||
public String getFileName() {
|
||||
return filename;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the imports list.
|
||||
*
|
||||
@@ -161,7 +161,7 @@ public class ZenParsedFile {
|
||||
public List<Import> getImports() {
|
||||
return imports;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets this file's script statements.
|
||||
*
|
||||
@@ -170,7 +170,7 @@ public class ZenParsedFile {
|
||||
public List<Statement> getStatements() {
|
||||
return statements;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the functions defined inside this file.
|
||||
*
|
||||
@@ -179,7 +179,7 @@ public class ZenParsedFile {
|
||||
public Map<String, ParsedFunction> getFunctions() {
|
||||
return functions;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return filename;
|
||||
|
||||
@@ -2,8 +2,8 @@ package stanhebben.zenscript;
|
||||
|
||||
/**
|
||||
* ZenRuntimeExceptions are thrown when an internal error occurs during
|
||||
* execution of scripts. Since ZenScript is only partially typesafe, errors
|
||||
* may occur at runtime due to impossible operations being executed.
|
||||
* execution of scripts. Since ZenScript is only partially typesafe, errors may
|
||||
* occur at runtime due to impossible operations being executed.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
|
||||
@@ -17,7 +17,7 @@ import stanhebben.zenscript.parser.TokenStream;
|
||||
*/
|
||||
public class ZenTokener extends TokenStream {
|
||||
private static final HashMap<String, Integer> KEYWORDS;
|
||||
|
||||
|
||||
public static final int T_ID = 1;
|
||||
public static final int T_INTVALUE = 2;
|
||||
public static final int T_FLOATVALUE = 3;
|
||||
@@ -63,7 +63,7 @@ public class ZenTokener extends TokenStream {
|
||||
public static final int T_NOTEQ = 42;
|
||||
public static final int T_NOT = 43;
|
||||
public static final int T_DOLLAR = 44;
|
||||
|
||||
|
||||
public static final int T_ANY = 99;
|
||||
public static final int T_BOOL = 100;
|
||||
public static final int T_BYTE = 101;
|
||||
@@ -76,7 +76,7 @@ public class ZenTokener extends TokenStream {
|
||||
public static final int T_FUNCTION = 108;
|
||||
public static final int T_IN = 109;
|
||||
public static final int T_VOID = 110;
|
||||
|
||||
|
||||
public static final int T_AS = 120;
|
||||
public static final int T_VERSION = 121;
|
||||
public static final int T_IF = 122;
|
||||
@@ -85,119 +85,119 @@ public class ZenTokener extends TokenStream {
|
||||
public static final int T_RETURN = 125;
|
||||
public static final int T_VAR = 126;
|
||||
public static final int T_VAL = 127;
|
||||
|
||||
|
||||
public static final int T_NULL = 140;
|
||||
public static final int T_TRUE = 141;
|
||||
public static final int T_FALSE = 142;
|
||||
|
||||
|
||||
public static final int T_IMPORT = 160;
|
||||
|
||||
|
||||
private static final String[] REGEXPS = {
|
||||
"#[^\n]*[\n\\e]",
|
||||
"//[^\n]*[\n\\e]",
|
||||
"/\\*[^\\*]*(\\*[^/]*)*\\*/",
|
||||
"[ \t\r\n]*",
|
||||
"[a-zA-Z_][a-zA-Z_0-9]*",
|
||||
"\\-?(0|[1-9][0-9]*)\\.[0-9]+([eE][\\+\\-]?[0-9]+)?",
|
||||
"\\-?(0|[1-9][0-9]*)",
|
||||
"\"([^\"\\\\]|\\\\([\'\"\\\\/bfnrt]|u[0-9a-fA-F]{4}))*\"",
|
||||
"\'([^\'\\\\]|\\\\([\'\"\\\\/bfnrt]|u[0-9a-fA-F]{4}))*\'",
|
||||
"\\{",
|
||||
"\\}",
|
||||
"\\[",
|
||||
"\\]",
|
||||
"\\.\\.",
|
||||
"\\.",
|
||||
",",
|
||||
"\\+=",
|
||||
"\\+",
|
||||
"\\-=",
|
||||
"\\-",
|
||||
"\\*=",
|
||||
"\\*",
|
||||
"/=",
|
||||
"/",
|
||||
"%=",
|
||||
"%",
|
||||
"\\|=",
|
||||
"\\|\\|",
|
||||
"\\|",
|
||||
"&=",
|
||||
"&&",
|
||||
"&",
|
||||
"\\^=",
|
||||
"\\^",
|
||||
"\\?",
|
||||
":",
|
||||
"\\(",
|
||||
"\\)",
|
||||
"~=",
|
||||
"~",
|
||||
";",
|
||||
"<=",
|
||||
"<",
|
||||
">=",
|
||||
">",
|
||||
"==",
|
||||
"=",
|
||||
"!=",
|
||||
"!",
|
||||
"$"
|
||||
"#[^\n]*[\n\\e]",
|
||||
"//[^\n]*[\n\\e]",
|
||||
"/\\*[^\\*]*(\\*[^/]*)*\\*/",
|
||||
"[ \t\r\n]*",
|
||||
"[a-zA-Z_][a-zA-Z_0-9]*",
|
||||
"\\-?(0|[1-9][0-9]*)\\.[0-9]+([eE][\\+\\-]?[0-9]+)?",
|
||||
"\\-?(0|[1-9][0-9]*)",
|
||||
"\"([^\"\\\\]|\\\\([\'\"\\\\/bfnrt]|u[0-9a-fA-F]{4}))*\"",
|
||||
"\'([^\'\\\\]|\\\\([\'\"\\\\/bfnrt]|u[0-9a-fA-F]{4}))*\'",
|
||||
"\\{",
|
||||
"\\}",
|
||||
"\\[",
|
||||
"\\]",
|
||||
"\\.\\.",
|
||||
"\\.",
|
||||
",",
|
||||
"\\+=",
|
||||
"\\+",
|
||||
"\\-=",
|
||||
"\\-",
|
||||
"\\*=",
|
||||
"\\*",
|
||||
"/=",
|
||||
"/",
|
||||
"%=",
|
||||
"%",
|
||||
"\\|=",
|
||||
"\\|\\|",
|
||||
"\\|",
|
||||
"&=",
|
||||
"&&",
|
||||
"&",
|
||||
"\\^=",
|
||||
"\\^",
|
||||
"\\?",
|
||||
":",
|
||||
"\\(",
|
||||
"\\)",
|
||||
"~=",
|
||||
"~",
|
||||
";",
|
||||
"<=",
|
||||
"<",
|
||||
">=",
|
||||
">",
|
||||
"==",
|
||||
"=",
|
||||
"!=",
|
||||
"!",
|
||||
"$"
|
||||
};
|
||||
private static final int[] FINALS = {
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
T_ID,
|
||||
T_FLOATVALUE,
|
||||
T_INTVALUE,
|
||||
T_STRINGVALUE,
|
||||
T_STRINGVALUE,
|
||||
T_AOPEN,
|
||||
T_ACLOSE,
|
||||
T_SQBROPEN,
|
||||
T_SQBRCLOSE,
|
||||
T_DOT2,
|
||||
T_DOT,
|
||||
T_COMMA,
|
||||
T_PLUSASSIGN,
|
||||
T_PLUS,
|
||||
T_MINUSASSIGN,
|
||||
T_MINUS,
|
||||
T_MULASSIGN,
|
||||
T_MUL,
|
||||
T_DIVASSIGN,
|
||||
T_DIV,
|
||||
T_MODASSIGN,
|
||||
T_MOD,
|
||||
T_ORASSIGN,
|
||||
T_OR2,
|
||||
T_OR,
|
||||
T_ANDASSIGN,
|
||||
T_AND2,
|
||||
T_AND,
|
||||
T_XORASSIGN,
|
||||
T_XOR,
|
||||
T_QUEST,
|
||||
T_COLON,
|
||||
T_BROPEN,
|
||||
T_BRCLOSE,
|
||||
T_TILDEASSIGN,
|
||||
T_TILDE,
|
||||
T_SEMICOLON,
|
||||
T_LTEQ,
|
||||
T_LT,
|
||||
T_GTEQ,
|
||||
T_GT,
|
||||
T_EQ,
|
||||
T_ASSIGN,
|
||||
T_NOTEQ,
|
||||
T_NOT,
|
||||
T_DOLLAR
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
T_ID,
|
||||
T_FLOATVALUE,
|
||||
T_INTVALUE,
|
||||
T_STRINGVALUE,
|
||||
T_STRINGVALUE,
|
||||
T_AOPEN,
|
||||
T_ACLOSE,
|
||||
T_SQBROPEN,
|
||||
T_SQBRCLOSE,
|
||||
T_DOT2,
|
||||
T_DOT,
|
||||
T_COMMA,
|
||||
T_PLUSASSIGN,
|
||||
T_PLUS,
|
||||
T_MINUSASSIGN,
|
||||
T_MINUS,
|
||||
T_MULASSIGN,
|
||||
T_MUL,
|
||||
T_DIVASSIGN,
|
||||
T_DIV,
|
||||
T_MODASSIGN,
|
||||
T_MOD,
|
||||
T_ORASSIGN,
|
||||
T_OR2,
|
||||
T_OR,
|
||||
T_ANDASSIGN,
|
||||
T_AND2,
|
||||
T_AND,
|
||||
T_XORASSIGN,
|
||||
T_XOR,
|
||||
T_QUEST,
|
||||
T_COLON,
|
||||
T_BROPEN,
|
||||
T_BRCLOSE,
|
||||
T_TILDEASSIGN,
|
||||
T_TILDE,
|
||||
T_SEMICOLON,
|
||||
T_LTEQ,
|
||||
T_LT,
|
||||
T_GTEQ,
|
||||
T_GT,
|
||||
T_EQ,
|
||||
T_ASSIGN,
|
||||
T_NOTEQ,
|
||||
T_NOT,
|
||||
T_DOLLAR
|
||||
};
|
||||
private static final CompiledDFA DFA = new NFA(REGEXPS, FINALS).toDFA().optimize().compile();
|
||||
|
||||
|
||||
static {
|
||||
KEYWORDS = new HashMap<String, Integer>();
|
||||
KEYWORDS.put("any", T_ANY);
|
||||
@@ -212,7 +212,7 @@ public class ZenTokener extends TokenStream {
|
||||
KEYWORDS.put("function", T_FUNCTION);
|
||||
KEYWORDS.put("in", T_IN);
|
||||
KEYWORDS.put("void", T_VOID);
|
||||
|
||||
|
||||
KEYWORDS.put("as", T_AS);
|
||||
KEYWORDS.put("version", T_VERSION);
|
||||
KEYWORDS.put("if", T_IF);
|
||||
@@ -221,16 +221,16 @@ public class ZenTokener extends TokenStream {
|
||||
KEYWORDS.put("return", T_RETURN);
|
||||
KEYWORDS.put("var", T_VAR);
|
||||
KEYWORDS.put("val", T_VAL);
|
||||
|
||||
|
||||
KEYWORDS.put("null", T_NULL);
|
||||
KEYWORDS.put("true", T_TRUE);
|
||||
KEYWORDS.put("false", T_FALSE);
|
||||
|
||||
|
||||
KEYWORDS.put("import", T_IMPORT);
|
||||
}
|
||||
|
||||
|
||||
private final IZenCompileEnvironment environment;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a tokener from the given reader.
|
||||
*
|
||||
@@ -240,10 +240,10 @@ public class ZenTokener extends TokenStream {
|
||||
*/
|
||||
public ZenTokener(Reader contents, IZenCompileEnvironment environment) throws IOException {
|
||||
super(contents, DFA);
|
||||
|
||||
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a tokener from the given string.
|
||||
*
|
||||
@@ -253,10 +253,10 @@ public class ZenTokener extends TokenStream {
|
||||
*/
|
||||
public ZenTokener(String contents, IZenCompileEnvironment environment) throws IOException {
|
||||
super(new StringReader(contents), DFA);
|
||||
|
||||
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves the compile environment of this tokener.
|
||||
*
|
||||
@@ -265,11 +265,11 @@ public class ZenTokener extends TokenStream {
|
||||
public IZenCompileEnvironment getEnvironment() {
|
||||
return environment;
|
||||
}
|
||||
|
||||
|
||||
// ##################################
|
||||
// ### TokenStream implementation ###
|
||||
// ##################################
|
||||
|
||||
|
||||
@Override
|
||||
public Token process(Token token) {
|
||||
if (token.getType() == T_ID) {
|
||||
|
||||
@@ -6,7 +6,8 @@ import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Makes a class implementing List expose itself as iterable over keys and values.
|
||||
* Makes a class implementing List expose itself as iterable over keys and
|
||||
* values.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
|
||||
@@ -6,7 +6,8 @@ import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Makes a class implementing Map expose itself as iterable over keys and values.
|
||||
* Makes a class implementing Map expose itself as iterable over keys and
|
||||
* values.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
@@ -14,5 +15,6 @@ import java.lang.annotation.Target;
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface IterableMap {
|
||||
String key();
|
||||
|
||||
String value();
|
||||
}
|
||||
@@ -13,5 +13,5 @@ import java.lang.annotation.Target;
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.PARAMETER)
|
||||
public @interface NotNull {
|
||||
|
||||
|
||||
}
|
||||
@@ -15,5 +15,5 @@ import java.lang.annotation.Target;
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.PARAMETER)
|
||||
public @interface Optional {
|
||||
|
||||
|
||||
}
|
||||
@@ -17,5 +17,5 @@ import java.lang.annotation.Target;
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface ZenCaster {
|
||||
|
||||
|
||||
}
|
||||
@@ -12,13 +12,13 @@ import java.lang.annotation.Target;
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||
public @interface ZenClass {
|
||||
/**
|
||||
* Contains the class' package and name. If omitted, the java package and
|
||||
* name will be used.
|
||||
*
|
||||
* @return
|
||||
* @return
|
||||
*/
|
||||
String value() default "";
|
||||
}
|
||||
@@ -6,16 +6,17 @@ import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Indicates that the given class contains an expansion for a certain type.
|
||||
* The expansions themselves are given through annotated static methods.
|
||||
* Indicates that the given class contains an expansion for a certain type. The
|
||||
* expansions themselves are given through annotated static methods.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||
public @interface ZenExpansion {
|
||||
/**
|
||||
* Contains the type of the annotated class. May be any kind of ZenScript type.
|
||||
* Contains the type of the annotated class. May be any kind of ZenScript
|
||||
* type.
|
||||
*
|
||||
* Examples:
|
||||
* <ul>
|
||||
|
||||
@@ -6,9 +6,9 @@ import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Indicates a member getter. A member getter is a special kind of operator which
|
||||
* is a fallback if a member could not be resolved. Can be used to make contents
|
||||
* available as if they were members.
|
||||
* Indicates a member getter. A member getter is a special kind of operator
|
||||
* which is a fallback if a member could not be resolved. Can be used to make
|
||||
* contents available as if they were members.
|
||||
*
|
||||
* If this method is a native class, it accepts a single string parameter with
|
||||
* the member name. If this method is an expansion method, it accepts the source
|
||||
@@ -19,5 +19,5 @@ import java.lang.annotation.Target;
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface ZenMemberGetter {
|
||||
|
||||
|
||||
}
|
||||
@@ -9,8 +9,8 @@ import java.lang.annotation.Target;
|
||||
* Used to indicate a member setter. Companion to member getters, but allow
|
||||
* setting contents of this object as if they were members.
|
||||
*
|
||||
* If this method is a native class, it accepts a string parameter with
|
||||
* the member name and a second parameter with the value. If this method is an
|
||||
* If this method is a native class, it accepts a string parameter with the
|
||||
* member name and a second parameter with the value. If this method is an
|
||||
* expansion method, it accepts the source value, the member name and the value
|
||||
* to be assigned.
|
||||
*
|
||||
@@ -19,5 +19,5 @@ import java.lang.annotation.Target;
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface ZenMemberSetter {
|
||||
|
||||
|
||||
}
|
||||
@@ -8,9 +8,9 @@ import java.lang.annotation.Target;
|
||||
/**
|
||||
* Used to indicate an exposed method.
|
||||
*
|
||||
* If this method is part of a native class, it will simply accept the parameters
|
||||
* as provided. If it is part of an expansion method, it will also accept the
|
||||
* instance value as first parameter.
|
||||
* If this method is part of a native class, it will simply accept the
|
||||
* parameters as provided. If it is part of an expansion method, it will also
|
||||
* accept the instance value as first parameter.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
|
||||
@@ -12,8 +12,8 @@ import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Used to indicate static methods for expansions. For native classes, use ZenMethod
|
||||
* instead on a static method.
|
||||
* Used to indicate static methods for expansions. For native classes, use
|
||||
* ZenMethod instead on a static method.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
|
||||
@@ -6,8 +6,8 @@ import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Used to annotate setters. Will make the given method accessible as setter, that is,
|
||||
* it's called when value.name is being assinged a value.
|
||||
* Used to annotate setters. Will make the given method accessible as setter,
|
||||
* that is, it's called when value.name is being assinged a value.
|
||||
*
|
||||
* For a native class, a single argument with the assinged value is provided.
|
||||
* For an expansion, the target object and assigned value are provided.
|
||||
|
||||
@@ -12,15 +12,15 @@ package stanhebben.zenscript.compiler;
|
||||
*/
|
||||
public class ClassNameGenerator {
|
||||
private int counter = 0;
|
||||
|
||||
|
||||
public ClassNameGenerator() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public String generate() {
|
||||
return "ZenClass" + counter++;
|
||||
}
|
||||
|
||||
|
||||
public String generate(String prefix) {
|
||||
return prefix + (counter++);
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ public class EnvironmentClass implements IEnvironmentClass {
|
||||
private final ClassVisitor output;
|
||||
private final IEnvironmentGlobal global;
|
||||
private final Map<String, IZenSymbol> local;
|
||||
|
||||
|
||||
public EnvironmentClass(ClassVisitor output, IEnvironmentGlobal global) {
|
||||
this.output = output;
|
||||
this.global = global;
|
||||
|
||||
@@ -29,7 +29,7 @@ public class EnvironmentGlobal implements IEnvironmentGlobal {
|
||||
private final Map<String, IZenSymbol> local;
|
||||
private final ClassNameGenerator nameGen;
|
||||
private final TypeRegistry types;
|
||||
|
||||
|
||||
public EnvironmentGlobal(
|
||||
IZenCompileEnvironment environment,
|
||||
Map<String, byte[]> classes,
|
||||
@@ -41,31 +41,31 @@ public class EnvironmentGlobal implements IEnvironmentGlobal {
|
||||
this.types = environment.getTypeRegistry();
|
||||
this.local = new HashMap<String, IZenSymbol>();
|
||||
}
|
||||
|
||||
|
||||
public IZenCompileEnvironment getCompileEnvironment() {
|
||||
return environment;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ZenType getType(Type type) {
|
||||
return types.getType(type);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean containsClass(String name) {
|
||||
return classes.containsKey(name);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void putClass(String name, byte[] data) {
|
||||
classes.put(name, data);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String makeClassName() {
|
||||
return nameGen.generate();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public TypeExpansion getExpansion(String type) {
|
||||
return environment.getExpansion(type);
|
||||
|
||||
@@ -29,24 +29,24 @@ public class EnvironmentMethod implements IEnvironmentMethod {
|
||||
private final HashMap<SymbolLocal, Integer> locals;
|
||||
private final IEnvironmentClass environment;
|
||||
private final Map<String, IZenSymbol> local;
|
||||
|
||||
|
||||
public EnvironmentMethod(MethodOutput output, IEnvironmentClass environment) {
|
||||
this.output = output;
|
||||
this.locals = new HashMap<SymbolLocal, Integer>();
|
||||
this.environment = environment;
|
||||
this.local = new HashMap<String, IZenSymbol>();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public MethodOutput getOutput() {
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ClassVisitor getClassOutput() {
|
||||
return environment.getClassOutput();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int getLocal(SymbolLocal variable) {
|
||||
if (!locals.containsKey(variable)) {
|
||||
@@ -54,7 +54,7 @@ public class EnvironmentMethod implements IEnvironmentMethod {
|
||||
}
|
||||
return locals.get(variable);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ZenType getType(Type type) {
|
||||
return environment.getType(type);
|
||||
@@ -79,7 +79,7 @@ public class EnvironmentMethod implements IEnvironmentMethod {
|
||||
public TypeExpansion getExpansion(String name) {
|
||||
return environment.getExpansion(name);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String makeClassName() {
|
||||
return environment.makeClassName();
|
||||
|
||||
@@ -28,7 +28,7 @@ public class EnvironmentScope implements IEnvironmentMethod {
|
||||
private final IEnvironmentMethod outer;
|
||||
private final Map<String, IZenSymbol> local;
|
||||
private final Map<SymbolLocal, Integer> locals;
|
||||
|
||||
|
||||
public EnvironmentScope(IEnvironmentMethod outer) {
|
||||
this.outer = outer;
|
||||
this.local = new HashMap<String, IZenSymbol>();
|
||||
|
||||
@@ -24,7 +24,7 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class EnvironmentScript implements IEnvironmentGlobal {
|
||||
private final IEnvironmentGlobal parent;
|
||||
private final Map<String, IZenSymbol> imports;
|
||||
|
||||
|
||||
public EnvironmentScript(IEnvironmentGlobal parent) {
|
||||
this.parent = parent;
|
||||
imports = new HashMap<String, IZenSymbol>();
|
||||
@@ -59,7 +59,8 @@ public class EnvironmentScript implements IEnvironmentGlobal {
|
||||
public IPartialExpression getValue(String name, ZenPosition position) {
|
||||
if (imports.containsKey(name)) {
|
||||
IZenSymbol imprt = imports.get(name);
|
||||
if (imprt == null) throw new RuntimeException("How could this happen?");
|
||||
if (imprt == null)
|
||||
throw new RuntimeException("How could this happen?");
|
||||
return imprt.instance(position);
|
||||
} else {
|
||||
return parent.getValue(name, position);
|
||||
@@ -68,8 +69,9 @@ public class EnvironmentScript implements IEnvironmentGlobal {
|
||||
|
||||
@Override
|
||||
public void putValue(String name, IZenSymbol value, ZenPosition position) {
|
||||
if (value == null) throw new IllegalArgumentException("value cannot be null");
|
||||
|
||||
if (value == null)
|
||||
throw new IllegalArgumentException("value cannot be null");
|
||||
|
||||
if (imports.containsKey(name)) {
|
||||
error(position, "Value already defined in this scope: " + name);
|
||||
} else {
|
||||
|
||||
@@ -20,20 +20,20 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
*/
|
||||
public interface IEnvironmentGlobal extends ITypeRegistry, IZenErrorLogger {
|
||||
public IZenCompileEnvironment getEnvironment();
|
||||
|
||||
|
||||
public TypeExpansion getExpansion(String name);
|
||||
|
||||
|
||||
public String makeClassName();
|
||||
|
||||
|
||||
public boolean containsClass(String name);
|
||||
|
||||
|
||||
public Set<String> getClassNames();
|
||||
|
||||
|
||||
public byte[] getClass(String name);
|
||||
|
||||
|
||||
public void putClass(String name, byte[] data);
|
||||
|
||||
|
||||
public IPartialExpression getValue(String name, ZenPosition position);
|
||||
|
||||
|
||||
public void putValue(String name, IZenSymbol value, ZenPosition position);
|
||||
}
|
||||
@@ -15,6 +15,6 @@ import stanhebben.zenscript.util.MethodOutput;
|
||||
*/
|
||||
public interface IEnvironmentMethod extends IEnvironmentClass {
|
||||
public MethodOutput getOutput();
|
||||
|
||||
|
||||
public int getLocal(SymbolLocal variable);
|
||||
}
|
||||
@@ -17,21 +17,21 @@ public class Import {
|
||||
private final ZenPosition position;
|
||||
private final List<String> name;
|
||||
private final String rename;
|
||||
|
||||
|
||||
public Import(ZenPosition position, List<String> name, String rename) {
|
||||
this.position = position;
|
||||
this.name = name;
|
||||
this.rename = rename;
|
||||
}
|
||||
|
||||
|
||||
public ZenPosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
|
||||
public List<String> getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
public String getRename() {
|
||||
return rename == null ? name.get(name.size() - 1) : rename;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,8 @@ public class ParsedFunction {
|
||||
parser.next();
|
||||
Token tName = parser.required(ZenTokener.T_ID, "identifier expected");
|
||||
|
||||
// function (argname [as type], argname [as type], ...) [as type] { ...contents... }
|
||||
// function (argname [as type], argname [as type], ...) [as type] {
|
||||
// ...contents... }
|
||||
parser.required(T_BROPEN, "( expected");
|
||||
|
||||
List<ParsedFunctionArgument> arguments = new ArrayList<ParsedFunctionArgument>();
|
||||
@@ -37,7 +38,7 @@ public class ParsedFunction {
|
||||
if (parser.optional(T_AS) != null) {
|
||||
type = ZenType.read(parser, environment);
|
||||
}
|
||||
|
||||
|
||||
arguments.add(new ParsedFunctionArgument(argName.getValue(), type));
|
||||
|
||||
while (parser.optional(T_COMMA) != null) {
|
||||
@@ -46,20 +47,20 @@ public class ParsedFunction {
|
||||
if (parser.optional(T_AS) != null) {
|
||||
type2 = ZenType.read(parser, environment);
|
||||
}
|
||||
|
||||
|
||||
arguments.add(new ParsedFunctionArgument(argName2.getValue(), type2));
|
||||
}
|
||||
|
||||
|
||||
parser.required(T_BRCLOSE, ") expected");
|
||||
}
|
||||
|
||||
|
||||
ZenType type = ZenTypeAny.INSTANCE;
|
||||
if (parser.optional(T_AS) != null) {
|
||||
type = ZenType.read(parser, environment);
|
||||
}
|
||||
|
||||
|
||||
parser.required(T_AOPEN, "{ expected");
|
||||
|
||||
|
||||
Statement[] statements;
|
||||
if (parser.optional(T_ACLOSE) != null) {
|
||||
statements = new Statement[0];
|
||||
@@ -71,25 +72,25 @@ public class ParsedFunction {
|
||||
}
|
||||
statements = statementsAL.toArray(new Statement[statementsAL.size()]);
|
||||
}
|
||||
|
||||
|
||||
return new ParsedFunction(tName.getPosition(), tName.getValue(), arguments, type, statements);
|
||||
}
|
||||
|
||||
|
||||
private final ZenPosition position;
|
||||
private final String name;
|
||||
private final List<ParsedFunctionArgument> arguments;
|
||||
private final ZenType returnType;
|
||||
private final Statement[] statements;
|
||||
|
||||
|
||||
private final String signature;
|
||||
|
||||
|
||||
private ParsedFunction(ZenPosition position, String name, List<ParsedFunctionArgument> arguments, ZenType returnType, Statement[] statements) {
|
||||
this.position = position;
|
||||
this.name = name;
|
||||
this.arguments = arguments;
|
||||
this.returnType = returnType;
|
||||
this.statements = statements;
|
||||
|
||||
|
||||
StringBuilder sig = new StringBuilder();
|
||||
sig.append("(");
|
||||
for (ParsedFunctionArgument argument : arguments) {
|
||||
@@ -99,27 +100,27 @@ public class ParsedFunction {
|
||||
sig.append(returnType.getSignature());
|
||||
signature = sig.toString();
|
||||
}
|
||||
|
||||
|
||||
public ZenPosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
public String getSignature() {
|
||||
return signature;
|
||||
}
|
||||
|
||||
|
||||
public ZenType getReturnType() {
|
||||
return returnType;
|
||||
}
|
||||
|
||||
|
||||
public List<ParsedFunctionArgument> getArguments() {
|
||||
return arguments;
|
||||
}
|
||||
|
||||
|
||||
public ZenType[] getArgumentTypes() {
|
||||
ZenType[] result = new ZenType[arguments.size()];
|
||||
for (int i = 0; i < arguments.size(); i++) {
|
||||
@@ -127,7 +128,7 @@ public class ParsedFunction {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public Statement[] getStatements() {
|
||||
return statements;
|
||||
}
|
||||
|
||||
+3
-3
@@ -9,16 +9,16 @@ import stanhebben.zenscript.type.ZenType;
|
||||
public class ParsedFunctionArgument {
|
||||
private final String name;
|
||||
private final ZenType type;
|
||||
|
||||
|
||||
public ParsedFunctionArgument(String name, ZenType type) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
public ZenType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@@ -11,23 +11,23 @@ import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.casting.ICastingRule;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
public abstract class Expression implements IPartialExpression {
|
||||
public abstract class Expression implements IPartialExpression {
|
||||
public static final Expression parse(ZenTokener parser, IEnvironmentMethod environment, ZenType predictedType) {
|
||||
return ParsedExpression.read(parser, environment)
|
||||
.compile(environment, predictedType)
|
||||
.eval(environment);
|
||||
.compile(environment, predictedType)
|
||||
.eval(environment);
|
||||
}
|
||||
|
||||
|
||||
private final ZenPosition position;
|
||||
|
||||
|
||||
public Expression(ZenPosition position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
|
||||
public ZenPosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
|
||||
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
|
||||
if (getType().equals(type)) {
|
||||
return this;
|
||||
@@ -41,9 +41,9 @@ public abstract class Expression implements IPartialExpression {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public abstract void compile(boolean result, IEnvironmentMethod environment);
|
||||
|
||||
|
||||
public void compileIf(Label onElse, IEnvironmentMethod environment) {
|
||||
if (getType() == ZenType.BOOL) {
|
||||
compile(true, environment);
|
||||
@@ -55,42 +55,42 @@ public abstract class Expression implements IPartialExpression {
|
||||
throw new RuntimeException("cannot compile non-pointer non-boolean value to if condition");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// #########################################
|
||||
// ### IPartialExpression implementation ###
|
||||
// #########################################
|
||||
|
||||
|
||||
@Override
|
||||
public Expression eval(IEnvironmentGlobal environment) {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Expression assign(ZenPosition position, IEnvironmentGlobal environment, Expression other) {
|
||||
environment.error(position, "not a valid lvalue");
|
||||
return new ExpressionInvalid(position, getType());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public IPartialExpression getMember(ZenPosition position, IEnvironmentGlobal environment, String name) {
|
||||
return getType().getMember(position, environment, this, name);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Expression call(ZenPosition position, IEnvironmentMethod environment, Expression... values) {
|
||||
return getType().call(position, environment, this, values);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ZenType[] predictCallTypes(int numArguments) {
|
||||
return getType().predictCallTypes(numArguments);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public IZenSymbol toSymbol() {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ZenType toType(IEnvironmentGlobal environment) {
|
||||
environment.error(position, "not a valid type");
|
||||
|
||||
@@ -9,10 +9,10 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ExpressionAndAnd extends Expression {
|
||||
private final Expression a;
|
||||
private final Expression b;
|
||||
|
||||
|
||||
public ExpressionAndAnd(ZenPosition position, Expression a, Expression b) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
}
|
||||
@@ -27,9 +27,9 @@ public class ExpressionAndAnd extends Expression {
|
||||
// if not a: return false
|
||||
// if not b: return false
|
||||
// return true
|
||||
|
||||
|
||||
MethodOutput output = environment.getOutput();
|
||||
|
||||
|
||||
Label skip = new Label();
|
||||
a.compile(true, environment);
|
||||
output.ifEQ(skip);
|
||||
@@ -37,7 +37,7 @@ public class ExpressionAndAnd extends Expression {
|
||||
output.ifEQ(skip);
|
||||
output.iConst1();
|
||||
output.label(skip);
|
||||
|
||||
|
||||
if (!result) {
|
||||
output.pop();
|
||||
}
|
||||
|
||||
@@ -17,10 +17,10 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ExpressionArgument extends Expression {
|
||||
private final int id;
|
||||
private final ZenType type;
|
||||
|
||||
|
||||
public ExpressionArgument(ZenPosition position, int id, ZenType type) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.id = id;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
+4
-4
@@ -27,10 +27,10 @@ public class ExpressionArithmeticBinary extends Expression {
|
||||
private final OperatorType operator;
|
||||
private final Expression a;
|
||||
private final Expression b;
|
||||
|
||||
|
||||
public ExpressionArithmeticBinary(ZenPosition position, OperatorType operator, Expression a, Expression b) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.operator = operator;
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
@@ -46,10 +46,10 @@ public class ExpressionArithmeticBinary extends Expression {
|
||||
if (result) {
|
||||
a.compile(result, environment);
|
||||
b.compile(result, environment);
|
||||
|
||||
|
||||
ZenType type = a.getType();
|
||||
MethodOutput output = environment.getOutput();
|
||||
|
||||
|
||||
if (type == ZenType.BOOL) {
|
||||
switch (operator) {
|
||||
case AND:
|
||||
|
||||
+26
-14
@@ -18,10 +18,10 @@ public class ExpressionArithmeticCompare extends Expression {
|
||||
private final Expression a;
|
||||
private final Expression b;
|
||||
private final CompareType type;
|
||||
|
||||
|
||||
public ExpressionArithmeticCompare(ZenPosition position, CompareType type, Expression a, Expression b) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
this.type = type;
|
||||
@@ -36,14 +36,14 @@ public class ExpressionArithmeticCompare extends Expression {
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
a.compile(result, environment);
|
||||
b.compile(result, environment);
|
||||
|
||||
|
||||
if (result) {
|
||||
MethodOutput output = environment.getOutput();
|
||||
if (a.getType() == ZenType.BOOL) {
|
||||
if (type == CompareType.EQ) {
|
||||
Label onThen = new Label();
|
||||
Label onEnd = new Label();
|
||||
|
||||
|
||||
output.ifICmpEQ(onThen);
|
||||
output.iConst0();
|
||||
output.goTo(onEnd);
|
||||
@@ -53,7 +53,7 @@ public class ExpressionArithmeticCompare extends Expression {
|
||||
} else if (type == CompareType.NE) {
|
||||
Label onThen = new Label();
|
||||
Label onEnd = new Label();
|
||||
|
||||
|
||||
output.ifICmpNE(onThen);
|
||||
output.iConst0();
|
||||
output.goTo(onEnd);
|
||||
@@ -66,7 +66,7 @@ public class ExpressionArithmeticCompare extends Expression {
|
||||
} else {
|
||||
Label onThen = new Label();
|
||||
Label onEnd = new Label();
|
||||
|
||||
|
||||
if (a.getType() == ZenTypeLong.INSTANCE) {
|
||||
output.lCmp();
|
||||
} else if (a.getType() == ZenTypeFloat.INSTANCE) {
|
||||
@@ -80,19 +80,31 @@ public class ExpressionArithmeticCompare extends Expression {
|
||||
} else {
|
||||
throw new RuntimeException("Unsupported type for arithmetic compare");
|
||||
}
|
||||
|
||||
|
||||
switch (type) {
|
||||
case EQ: output.ifICmpEQ(onThen); break;
|
||||
case NE: output.ifICmpNE(onThen); break;
|
||||
case LE: output.ifICmpLE(onThen); break;
|
||||
case GE: output.ifICmpGE(onThen); break;
|
||||
case LT: output.ifICmpLT(onThen); break;
|
||||
case GT: output.ifICmpGT(onThen); break;
|
||||
case EQ:
|
||||
output.ifICmpEQ(onThen);
|
||||
break;
|
||||
case NE:
|
||||
output.ifICmpNE(onThen);
|
||||
break;
|
||||
case LE:
|
||||
output.ifICmpLE(onThen);
|
||||
break;
|
||||
case GE:
|
||||
output.ifICmpGE(onThen);
|
||||
break;
|
||||
case LT:
|
||||
output.ifICmpLT(onThen);
|
||||
break;
|
||||
case GT:
|
||||
output.ifICmpGT(onThen);
|
||||
break;
|
||||
default:
|
||||
environment.error(getPosition(), "this kind of comparison is not supported on int values");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
output.iConst0();
|
||||
output.goTo(onEnd);
|
||||
output.label(onThen);
|
||||
|
||||
+4
-4
@@ -16,10 +16,10 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ExpressionArithmeticUnary extends Expression {
|
||||
private final Expression base;
|
||||
private final OperatorType operator;
|
||||
|
||||
|
||||
public ExpressionArithmeticUnary(ZenPosition position, OperatorType operator, Expression base) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.base = base;
|
||||
this.operator = operator;
|
||||
}
|
||||
@@ -32,7 +32,7 @@ public class ExpressionArithmeticUnary extends Expression {
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
base.compile(result, environment);
|
||||
|
||||
|
||||
MethodOutput output = environment.getOutput();
|
||||
if (result) {
|
||||
ZenType type = base.getType();
|
||||
@@ -69,7 +69,7 @@ public class ExpressionArithmeticUnary extends Expression {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
environment.error(getPosition(), "Invalid operation");
|
||||
}
|
||||
}
|
||||
@@ -13,10 +13,10 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ExpressionArray extends Expression {
|
||||
private final Expression[] contents;
|
||||
private final ZenTypeArrayBasic type;
|
||||
|
||||
|
||||
public ExpressionArray(ZenPosition position, ZenTypeArrayBasic type, Expression... contents) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.contents = contents;
|
||||
this.type = type;
|
||||
}
|
||||
@@ -26,14 +26,14 @@ public class ExpressionArray extends Expression {
|
||||
if (this.type.equals(type)) {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
if (type instanceof ZenTypeArrayBasic) {
|
||||
ZenTypeArrayBasic arrayType = (ZenTypeArrayBasic) type;
|
||||
Expression[] newContents = new Expression[contents.length];
|
||||
for (int i = 0; i < contents.length; i++) {
|
||||
newContents[i] = contents[i].cast(position, environment, arrayType.getBaseType());
|
||||
}
|
||||
|
||||
|
||||
return new ExpressionArray(getPosition(), arrayType, newContents);
|
||||
} else {
|
||||
ICastingRule castingRule = this.type.getCastingRule(type, environment);
|
||||
@@ -55,11 +55,11 @@ public class ExpressionArray extends Expression {
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
ZenType baseType = type.getBaseType();
|
||||
Type asmBaseType = type.getBaseType().toASMType();
|
||||
|
||||
|
||||
MethodOutput output = environment.getOutput();
|
||||
output.constant(contents.length);
|
||||
output.newArray(asmBaseType);
|
||||
|
||||
|
||||
for (int i = 0; i < contents.length; i++) {
|
||||
output.dup();
|
||||
output.constant(i);
|
||||
|
||||
@@ -19,10 +19,10 @@ public class ExpressionArrayGet extends Expression {
|
||||
private final Expression array;
|
||||
private final Expression index;
|
||||
private final ZenType baseType;
|
||||
|
||||
|
||||
public ExpressionArrayGet(ZenPosition position, Expression array, Expression index) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.array = array;
|
||||
this.index = index;
|
||||
this.baseType = ((ZenTypeArray) array.getType()).getBaseType();
|
||||
@@ -37,7 +37,7 @@ public class ExpressionArrayGet extends Expression {
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
array.compile(result, environment);
|
||||
index.compile(result, environment);
|
||||
|
||||
|
||||
if (result) {
|
||||
environment.getOutput().arrayLoad(baseType.toASMType());
|
||||
}
|
||||
|
||||
@@ -17,10 +17,10 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
*/
|
||||
public class ExpressionArrayLength extends Expression {
|
||||
private final Expression value;
|
||||
|
||||
|
||||
public ExpressionArrayLength(ZenPosition position, Expression value) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,15 +18,15 @@ public class ExpressionArraySet extends Expression {
|
||||
private final Expression array;
|
||||
private final Expression index;
|
||||
private final Expression value;
|
||||
|
||||
|
||||
public ExpressionArraySet(ZenPosition position, Expression array, Expression index, Expression value) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.array = array;
|
||||
this.index = index;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return ZenType.VOID;
|
||||
@@ -37,7 +37,7 @@ public class ExpressionArraySet extends Expression {
|
||||
array.compile(result, environment);
|
||||
index.compile(result, environment);
|
||||
value.compile(result, environment);
|
||||
|
||||
|
||||
if (result) {
|
||||
environment.getOutput().arrayStore(value.getType().toASMType());
|
||||
}
|
||||
|
||||
@@ -9,28 +9,24 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ExpressionAs extends Expression {
|
||||
private final Expression value;
|
||||
private final ICastingRule castingRule;
|
||||
|
||||
|
||||
public ExpressionAs(ZenPosition position, Expression value, ICastingRule castingRule) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.value = value;
|
||||
this.castingRule = castingRule;
|
||||
}
|
||||
|
||||
/*@Override
|
||||
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
|
||||
if (castingRule.getResultingType().equals(type)) {
|
||||
return this;
|
||||
}
|
||||
|
||||
ICastingRule newCastingRule = type.getCastingRule(type, environment);
|
||||
if (newCastingRule == null) {
|
||||
environment.error(position, "Cannot cast " + getType() + " to " + type);
|
||||
return new ExpressionInvalid(position, type);
|
||||
} else {
|
||||
return new ExpressionAs(position, this, newCastingRule);
|
||||
}
|
||||
}*/
|
||||
/*
|
||||
* @Override public Expression cast(ZenPosition position, IEnvironmentGlobal
|
||||
* environment, ZenType type) { if
|
||||
* (castingRule.getResultingType().equals(type)) { return this; }
|
||||
*
|
||||
* ICastingRule newCastingRule = type.getCastingRule(type, environment); if
|
||||
* (newCastingRule == null) { environment.error(position, "Cannot cast " +
|
||||
* getType() + " to " + type); return new ExpressionInvalid(position, type);
|
||||
* } else { return new ExpressionAs(position, this, newCastingRule); } }
|
||||
*/
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
@@ -40,7 +36,7 @@ public class ExpressionAs extends Expression {
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
value.compile(result, environment);
|
||||
|
||||
|
||||
if (result) {
|
||||
castingRule.compile(environment);
|
||||
}
|
||||
|
||||
@@ -18,13 +18,13 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
*/
|
||||
public class ExpressionBool extends Expression {
|
||||
private final boolean value;
|
||||
|
||||
|
||||
public ExpressionBool(ZenPosition position, boolean value) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
|
||||
if (type == ZenType.BOOL) {
|
||||
|
||||
@@ -21,14 +21,14 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ExpressionCallStatic extends Expression {
|
||||
private final IJavaMethod method;
|
||||
private final Expression[] arguments;
|
||||
|
||||
|
||||
public ExpressionCallStatic(
|
||||
ZenPosition position,
|
||||
IEnvironmentGlobal environment,
|
||||
IJavaMethod method,
|
||||
Expression... arguments) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.method = method;
|
||||
this.arguments = JavaMethod.rematch(position, method, environment, arguments);
|
||||
}
|
||||
@@ -40,14 +40,14 @@ public class ExpressionCallStatic extends Expression {
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
MethodOutput output = environment.getOutput();
|
||||
|
||||
MethodOutput output = environment.getOutput();
|
||||
|
||||
for (Expression argument : arguments) {
|
||||
argument.compile(true, environment);
|
||||
}
|
||||
|
||||
|
||||
method.invokeStatic(output);
|
||||
|
||||
|
||||
if (method.getReturnType() != ZenType.VOID && !result) {
|
||||
output.pop(method.getReturnType().isLarge());
|
||||
}
|
||||
|
||||
@@ -19,10 +19,10 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
*/
|
||||
public class ExpressionCallVirtual extends Expression {
|
||||
private final IJavaMethod method;
|
||||
|
||||
|
||||
private final Expression receiver;
|
||||
private final Expression[] arguments;
|
||||
|
||||
|
||||
public ExpressionCallVirtual(
|
||||
ZenPosition position,
|
||||
IEnvironmentGlobal environment,
|
||||
@@ -30,9 +30,9 @@ public class ExpressionCallVirtual extends Expression {
|
||||
Expression receiver,
|
||||
Expression... arguments) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.method = method;
|
||||
|
||||
|
||||
this.receiver = receiver;
|
||||
this.arguments = JavaMethod.rematch(position, method, environment, arguments);
|
||||
}
|
||||
@@ -45,11 +45,11 @@ public class ExpressionCallVirtual extends Expression {
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
receiver.compile(true, environment);
|
||||
|
||||
|
||||
for (Expression argument : arguments) {
|
||||
argument.compile(true, environment);
|
||||
}
|
||||
|
||||
|
||||
method.invokeVirtual(environment.getOutput());
|
||||
if (method.getReturnType() != ZenType.VOID && !result) {
|
||||
environment.getOutput().pop(method.getReturnType().isLarge());
|
||||
|
||||
+24
-11
@@ -21,10 +21,10 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ExpressionCompareGeneric extends Expression {
|
||||
private final Expression value; // should return a compareTo value (<=>0)
|
||||
private final CompareType type;
|
||||
|
||||
|
||||
public ExpressionCompareGeneric(ZenPosition position, Expression value, CompareType type) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.value = value;
|
||||
this.type = type;
|
||||
}
|
||||
@@ -35,24 +35,37 @@ public class ExpressionCompareGeneric extends Expression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {;
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
;
|
||||
value.compile(result, environment);
|
||||
if (result) {
|
||||
MethodOutput output = environment.getOutput();
|
||||
Label lblThen = new Label();
|
||||
Label lblEnd = new Label();
|
||||
|
||||
|
||||
switch (type) {
|
||||
case LT: output.ifLT(lblThen); break;
|
||||
case GT: output.ifGT(lblThen); break;
|
||||
case LE: output.ifLE(lblThen); break;
|
||||
case GE: output.ifGE(lblThen); break;
|
||||
case NE: output.ifNE(lblThen); break;
|
||||
case EQ: output.ifEQ(lblThen); break;
|
||||
case LT:
|
||||
output.ifLT(lblThen);
|
||||
break;
|
||||
case GT:
|
||||
output.ifGT(lblThen);
|
||||
break;
|
||||
case LE:
|
||||
output.ifLE(lblThen);
|
||||
break;
|
||||
case GE:
|
||||
output.ifGE(lblThen);
|
||||
break;
|
||||
case NE:
|
||||
output.ifNE(lblThen);
|
||||
break;
|
||||
case EQ:
|
||||
output.ifEQ(lblThen);
|
||||
break;
|
||||
default:
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
|
||||
output.iConst0();
|
||||
output.goTo(lblEnd);
|
||||
output.label(lblThen);
|
||||
|
||||
@@ -9,10 +9,10 @@ public class ExpressionConditional extends Expression {
|
||||
private final Expression condition;
|
||||
private final Expression onIf;
|
||||
private final Expression onElse;
|
||||
|
||||
|
||||
public ExpressionConditional(ZenPosition position, Expression condition, Expression onIf, Expression onElse) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.condition = condition;
|
||||
this.onIf = onIf;
|
||||
this.onElse = onElse;
|
||||
@@ -27,7 +27,7 @@ public class ExpressionConditional extends Expression {
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
Label lblElse = new Label();
|
||||
Label lblExit = new Label();
|
||||
|
||||
|
||||
condition.compileIf(lblElse, environment);
|
||||
onIf.compile(result, environment);
|
||||
environment.getOutput().goTo(lblExit);
|
||||
|
||||
@@ -19,10 +19,10 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ExpressionFloat extends Expression {
|
||||
private final double value;
|
||||
private final ZenType type;
|
||||
|
||||
|
||||
public ExpressionFloat(ZenPosition position, double value, ZenType type) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.value = value;
|
||||
this.type = type;
|
||||
}
|
||||
@@ -34,8 +34,9 @@ public class ExpressionFloat extends Expression {
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
if (!result) return;
|
||||
|
||||
if (!result)
|
||||
return;
|
||||
|
||||
if (type == ZenTypeFloat.INSTANCE) {
|
||||
environment.getOutput().constant((float) value);
|
||||
} else if (type == ZenTypeDouble.INSTANCE) {
|
||||
|
||||
@@ -26,18 +26,18 @@ public class ExpressionFunction extends Expression {
|
||||
private final List<ParsedFunctionArgument> arguments;
|
||||
private final ZenType returnType;
|
||||
private final List<Statement> statements;
|
||||
|
||||
|
||||
private final ZenTypeFunction functionType;
|
||||
|
||||
|
||||
public ExpressionFunction(ZenPosition position, List<ParsedFunctionArgument> arguments, ZenType returnType, List<Statement> statements) {
|
||||
super(position);
|
||||
|
||||
|
||||
System.out.println("Function expression: " + arguments.size() + " arguments");
|
||||
|
||||
|
||||
this.arguments = arguments;
|
||||
this.returnType = returnType;
|
||||
this.statements = statements;
|
||||
|
||||
|
||||
ZenType[] argumentTypes = new ZenType[arguments.size()];
|
||||
for (int i = 0; i < arguments.size(); i++) {
|
||||
argumentTypes[i] = arguments.get(i).getType();
|
||||
@@ -70,7 +70,7 @@ public class ExpressionFunction extends Expression {
|
||||
return new ExpressionInvalid(position);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return new ExpressionJavaLambda(
|
||||
position,
|
||||
nativeClass,
|
||||
|
||||
@@ -23,18 +23,19 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ExpressionInt extends Expression {
|
||||
private final long value;
|
||||
private final ZenType type;
|
||||
|
||||
|
||||
public ExpressionInt(ZenPosition position, long value, ZenType type) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.value = value;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
|
||||
if (type == this.type) return this;
|
||||
|
||||
if (type == this.type)
|
||||
return this;
|
||||
|
||||
switch (type.getNumberType()) {
|
||||
case ZenType.NUM_BYTE:
|
||||
case ZenType.NUM_SHORT:
|
||||
@@ -45,7 +46,7 @@ public class ExpressionInt extends Expression {
|
||||
case ZenType.NUM_DOUBLE:
|
||||
return new ExpressionFloat(getPosition(), value, type);
|
||||
}
|
||||
|
||||
|
||||
return super.cast(position, environment, type);
|
||||
}
|
||||
|
||||
@@ -56,8 +57,9 @@ public class ExpressionInt extends Expression {
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
if (!result) return;
|
||||
|
||||
if (!result)
|
||||
return;
|
||||
|
||||
MethodOutput output = environment.getOutput();
|
||||
if (type == ZenTypeByte.INSTANCE) {
|
||||
output.biPush((byte) value);
|
||||
|
||||
@@ -18,7 +18,7 @@ public class ExpressionIntegerRange extends Expression {
|
||||
|
||||
public ExpressionIntegerRange(ZenPosition position, Expression from, Expression to) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.from = from;
|
||||
this.to = to;
|
||||
}
|
||||
|
||||
@@ -19,22 +19,22 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
*/
|
||||
public class ExpressionInvalid extends Expression {
|
||||
private final ZenType type;
|
||||
|
||||
|
||||
public ExpressionInvalid(ZenPosition position) {
|
||||
super(position);
|
||||
|
||||
|
||||
type = ZenTypeAny.INSTANCE;
|
||||
}
|
||||
|
||||
|
||||
public ExpressionInvalid(ZenPosition position, ZenType type) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.type = type;
|
||||
|
||||
|
||||
// XXX: remove before release
|
||||
//throw new RuntimeException("Constructing invalid expression");
|
||||
// throw new RuntimeException("Constructing invalid expression");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Expression getMember(ZenPosition position, IEnvironmentGlobal errors, String name) {
|
||||
return new ExpressionInvalid(position);
|
||||
|
||||
@@ -31,9 +31,9 @@ public class ExpressionJavaLambda extends Expression {
|
||||
private final Class interfaceClass;
|
||||
private final List<ParsedFunctionArgument> arguments;
|
||||
private final List<Statement> statements;
|
||||
|
||||
|
||||
private final ZenType type;
|
||||
|
||||
|
||||
public ExpressionJavaLambda(
|
||||
ZenPosition position,
|
||||
Class interfaceClass,
|
||||
@@ -41,11 +41,11 @@ public class ExpressionJavaLambda extends Expression {
|
||||
List<Statement> statements,
|
||||
ZenType type) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.interfaceClass = interfaceClass;
|
||||
this.arguments = arguments;
|
||||
this.statements = statements;
|
||||
|
||||
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@@ -56,43 +56,44 @@ public class ExpressionJavaLambda extends Expression {
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
if (!result) return;
|
||||
|
||||
if (!result)
|
||||
return;
|
||||
|
||||
Method method = interfaceClass.getMethods()[0];
|
||||
|
||||
|
||||
// generate class
|
||||
String clsName = environment.makeClassName();
|
||||
|
||||
|
||||
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
|
||||
cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, clsName, null, "java/lang/Object", new String[] { internal(interfaceClass) });
|
||||
|
||||
|
||||
MethodOutput constructor = new MethodOutput(cw, Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
|
||||
constructor.start();
|
||||
constructor.loadObject(0);
|
||||
constructor.invokeSpecial("java/lang/Object", "<init>", "()V");
|
||||
constructor.ret();
|
||||
constructor.end();
|
||||
|
||||
|
||||
MethodOutput output = new MethodOutput(cw, Opcodes.ACC_PUBLIC, method.getName(), descriptor(method), null, null);
|
||||
|
||||
|
||||
IEnvironmentClass environmentClass = new EnvironmentClass(cw, environment);
|
||||
IEnvironmentMethod environmentMethod = new EnvironmentMethod(output, environmentClass);
|
||||
|
||||
|
||||
for (int i = 0; i < arguments.size(); i++) {
|
||||
environmentMethod.putValue(
|
||||
arguments.get(i).getName(),
|
||||
new SymbolArgument(i + 1, environment.getType(method.getGenericParameterTypes()[i])),
|
||||
getPosition());
|
||||
}
|
||||
|
||||
|
||||
output.start();
|
||||
for (Statement statement : statements) {
|
||||
statement.compile(environmentMethod);
|
||||
}
|
||||
output.end();
|
||||
|
||||
|
||||
environment.putClass(clsName, cw.toByteArray());
|
||||
|
||||
|
||||
// make class instance
|
||||
environment.getOutput().newObject(clsName);
|
||||
environment.getOutput().dup();
|
||||
|
||||
+4
-4
@@ -24,22 +24,22 @@ public class ExpressionJavaMethodCurled extends Expression {
|
||||
private final JavaMethod method;
|
||||
private final ZenType type;
|
||||
private final Expression receiver;
|
||||
|
||||
|
||||
public ExpressionJavaMethodCurled(
|
||||
ZenPosition position,
|
||||
JavaMethod method,
|
||||
IEnvironmentGlobal environment,
|
||||
Expression receiver) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.method = method;
|
||||
this.receiver = receiver;
|
||||
|
||||
|
||||
List<ParsedFunctionArgument> arguments = new ArrayList<ParsedFunctionArgument>();
|
||||
for (int i = 0; i < method.getMethod().getParameterTypes().length; i++) {
|
||||
arguments.add(new ParsedFunctionArgument("p" + i, environment.getType(method.getMethod().getGenericParameterTypes()[i])));
|
||||
}
|
||||
|
||||
|
||||
this.type = new ZenTypeFunction(environment.getType(method.getMethod().getGenericReturnType()), arguments);
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -23,17 +23,17 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ExpressionJavaMethodStatic extends Expression {
|
||||
private final JavaMethod method;
|
||||
private final ZenType type;
|
||||
|
||||
|
||||
public ExpressionJavaMethodStatic(ZenPosition position, JavaMethod method, IEnvironmentGlobal environment) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.method = method;
|
||||
|
||||
|
||||
List<ParsedFunctionArgument> arguments = new ArrayList<ParsedFunctionArgument>();
|
||||
for (int i = 0; i < method.getParameterTypes().length; i++) {
|
||||
arguments.add(new ParsedFunctionArgument("p" + i, environment.getType(method.getMethod().getGenericParameterTypes()[i])));
|
||||
}
|
||||
|
||||
|
||||
this.type = new ZenTypeFunction(environment.getType(method.getMethod().getGenericReturnType()), arguments);
|
||||
}
|
||||
|
||||
|
||||
@@ -17,13 +17,13 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
*/
|
||||
public class ExpressionLocalGet extends Expression {
|
||||
private final SymbolLocal variable;
|
||||
|
||||
|
||||
public ExpressionLocalGet(ZenPosition position, SymbolLocal variable) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.variable = variable;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return variable.getType();
|
||||
|
||||
@@ -18,14 +18,14 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ExpressionLocalSet extends Expression {
|
||||
private final SymbolLocal variable;
|
||||
private final Expression value;
|
||||
|
||||
|
||||
public ExpressionLocalSet(ZenPosition position, SymbolLocal variable, Expression value) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.variable = variable;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return variable.getType();
|
||||
@@ -34,7 +34,7 @@ public class ExpressionLocalSet extends Expression {
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
int local = environment.getLocal(variable);
|
||||
|
||||
|
||||
value.compile(true, environment);
|
||||
if (result) {
|
||||
environment.getOutput().dup();
|
||||
|
||||
@@ -16,13 +16,13 @@ public class ExpressionMap extends Expression {
|
||||
private final Expression[] keys;
|
||||
private final Expression[] values;
|
||||
private final ZenTypeAssociative type;
|
||||
|
||||
|
||||
public ExpressionMap(ZenPosition position, Expression[] keys, Expression[] values, ZenTypeAssociative type) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.keys = keys;
|
||||
this.values = values;
|
||||
|
||||
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@@ -36,12 +36,12 @@ public class ExpressionMap extends Expression {
|
||||
if (result) {
|
||||
ZenType keyType = type.getKeyType();
|
||||
ZenType valueType = type.getValueType();
|
||||
|
||||
|
||||
MethodOutput output = environment.getOutput();
|
||||
output.newObject(HashMap.class);
|
||||
output.dup();
|
||||
output.invokeSpecial(internal(HashMap.class), "<init>", "()V");
|
||||
|
||||
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
output.dup();
|
||||
keys[i].cast(getPosition(), environment, keyType).compile(true, environment);
|
||||
@@ -58,23 +58,23 @@ public class ExpressionMap extends Expression {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
|
||||
if (this.type.equals(type)) {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
if (type instanceof ZenTypeAssociative) {
|
||||
ZenTypeAssociative associativeType = (ZenTypeAssociative) type;
|
||||
Expression[] newKeys = new Expression[keys.length];
|
||||
Expression[] newValues = new Expression[values.length];
|
||||
|
||||
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
newKeys[i] = keys[i].cast(position, environment, associativeType.getKeyType());
|
||||
newValues[i] = values[i].cast(position, environment, associativeType.getValueType());
|
||||
}
|
||||
|
||||
|
||||
return new ExpressionMap(getPosition(), newKeys, newValues, associativeType);
|
||||
} else {
|
||||
ICastingRule castingRule = this.type.getCastingRule(type, environment);
|
||||
|
||||
@@ -20,10 +20,10 @@ import static stanhebben.zenscript.util.ZenTypeUtil.internal;
|
||||
public class ExpressionMapContains extends Expression {
|
||||
private final Expression map;
|
||||
private final Expression key;
|
||||
|
||||
|
||||
public ExpressionMapContains(ZenPosition position, Expression map, Expression key) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.map = map;
|
||||
this.key = key;
|
||||
}
|
||||
@@ -38,7 +38,7 @@ public class ExpressionMapContains extends Expression {
|
||||
if (result) {
|
||||
map.compile(result, environment);
|
||||
key.compile(result, environment);
|
||||
|
||||
|
||||
environment.getOutput().invokeInterface(internal(Map.class), "containsKey", "()Ljava/lang/Object;");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,15 +20,15 @@ import static stanhebben.zenscript.util.ZenTypeUtil.internal;
|
||||
public class ExpressionMapIndexGet extends Expression {
|
||||
private final Expression map;
|
||||
private final Expression index;
|
||||
|
||||
|
||||
private final ZenType type;
|
||||
|
||||
|
||||
public ExpressionMapIndexGet(ZenPosition position, Expression map, Expression index) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.map = map;
|
||||
this.index = index;
|
||||
|
||||
|
||||
type = ((ZenTypeAssociative) map.getType()).getValueType();
|
||||
}
|
||||
|
||||
|
||||
@@ -21,16 +21,16 @@ public class ExpressionMapIndexSet extends Expression {
|
||||
private final Expression map;
|
||||
private final Expression index;
|
||||
private final Expression value;
|
||||
|
||||
|
||||
private final ZenType type;
|
||||
|
||||
|
||||
public ExpressionMapIndexSet(ZenPosition position, Expression map, Expression index, Expression value) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.map = map;
|
||||
this.index = index;
|
||||
this.value = value;
|
||||
|
||||
|
||||
type = ZenTypeVoid.INSTANCE;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,10 +19,10 @@ import static stanhebben.zenscript.util.ZenTypeUtil.internal;
|
||||
*/
|
||||
public class ExpressionMapSize extends Expression {
|
||||
private final Expression map;
|
||||
|
||||
|
||||
public ExpressionMapSize(ZenPosition position, Expression map) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ public class ExpressionNull extends Expression {
|
||||
public ExpressionNull(ZenPosition position) {
|
||||
super(position);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
|
||||
if (type.isPointer()) {
|
||||
|
||||
@@ -7,10 +7,10 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ExpressionOrOr extends Expression {
|
||||
private final Expression a;
|
||||
private final Expression b;
|
||||
|
||||
|
||||
public ExpressionOrOr(ZenPosition position, Expression a, Expression b) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
}
|
||||
|
||||
@@ -17,10 +17,10 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
*/
|
||||
public class ExpressionString extends Expression {
|
||||
private final String value;
|
||||
|
||||
|
||||
public ExpressionString(ZenPosition position, String value) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,13 +19,13 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
*/
|
||||
public class ExpressionStringConcat extends Expression {
|
||||
private final List<Expression> values;
|
||||
|
||||
|
||||
public ExpressionStringConcat(ZenPosition position, List<Expression> values) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.values = values;
|
||||
}
|
||||
|
||||
|
||||
public void add(Expression value) {
|
||||
values.add(value);
|
||||
}
|
||||
@@ -39,18 +39,18 @@ public class ExpressionStringConcat extends Expression {
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
if (result) {
|
||||
MethodOutput output = environment.getOutput();
|
||||
|
||||
|
||||
// Step 1: construct StringBuilder
|
||||
output.newObject(StringBuilder.class);
|
||||
output.dup();
|
||||
output.construct(StringBuilder.class);
|
||||
|
||||
|
||||
// Step 2: concatenate Strings
|
||||
for (Expression value : values) {
|
||||
value.compile(true, environment);
|
||||
output.invoke(StringBuilder.class, "append", StringBuilder.class, String.class);
|
||||
}
|
||||
|
||||
|
||||
// Step 3: return String
|
||||
output.invoke(StringBuilder.class, "toString", String.class);
|
||||
} else {
|
||||
|
||||
+4
-4
@@ -17,14 +17,14 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ExpressionStringContains extends Expression {
|
||||
private final Expression haystack;
|
||||
private final Expression needle;
|
||||
|
||||
|
||||
public ExpressionStringContains(ZenPosition position, Expression haystack, Expression needle) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.haystack = haystack;
|
||||
this.needle = needle;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return ZenType.STRING;
|
||||
@@ -34,7 +34,7 @@ public class ExpressionStringContains extends Expression {
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
haystack.compile(result, environment);
|
||||
needle.compile(result, environment);
|
||||
|
||||
|
||||
if (result) {
|
||||
environment.getOutput().invokeVirtual(String.class, "contains", CharSequence.class);
|
||||
}
|
||||
|
||||
@@ -17,14 +17,14 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ExpressionStringIndex extends Expression {
|
||||
private final Expression source;
|
||||
private final Expression index;
|
||||
|
||||
|
||||
public ExpressionStringIndex(ZenPosition position, Expression source, Expression index) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.source = source;
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return ZenType.STRING;
|
||||
@@ -34,7 +34,7 @@ public class ExpressionStringIndex extends Expression {
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
source.compile(result, environment);
|
||||
index.compile(result, environment);
|
||||
|
||||
|
||||
if (result) {
|
||||
environment.getOutput().dup();
|
||||
environment.getOutput().iConst1();
|
||||
|
||||
+2
-2
@@ -21,10 +21,10 @@ public class ExpressionJavaStaticField extends Expression {
|
||||
private final Class cls;
|
||||
private final Field field;
|
||||
private final ITypeRegistry types;
|
||||
|
||||
|
||||
public ExpressionJavaStaticField(ZenPosition position, Class cls, Field field, ITypeRegistry types) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.cls = cls;
|
||||
this.field = field;
|
||||
this.types = types;
|
||||
|
||||
+7
-7
@@ -19,18 +19,18 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
*/
|
||||
public interface IPartialExpression {
|
||||
public Expression eval(IEnvironmentGlobal environment);
|
||||
|
||||
|
||||
public Expression assign(ZenPosition position, IEnvironmentGlobal environment, Expression other);
|
||||
|
||||
|
||||
public IPartialExpression getMember(ZenPosition position, IEnvironmentGlobal environment, String name);
|
||||
|
||||
|
||||
public Expression call(ZenPosition position, IEnvironmentMethod environment, Expression... values);
|
||||
|
||||
|
||||
public ZenType[] predictCallTypes(int numArguments);
|
||||
|
||||
|
||||
public IZenSymbol toSymbol();
|
||||
|
||||
|
||||
public ZenType getType();
|
||||
|
||||
|
||||
public ZenType toType(IEnvironmentGlobal environment);
|
||||
}
|
||||
+1
-1
@@ -23,7 +23,7 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class PartialJavaClass implements IPartialExpression {
|
||||
private final ZenPosition position;
|
||||
private final Type cls;
|
||||
|
||||
|
||||
public PartialJavaClass(ZenPosition position, Type cls) {
|
||||
this.position = position;
|
||||
this.cls = cls;
|
||||
|
||||
@@ -24,7 +24,7 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class PartialLocal implements IPartialExpression {
|
||||
private final ZenPosition position;
|
||||
private final SymbolLocal variable;
|
||||
|
||||
|
||||
public PartialLocal(ZenPosition position, SymbolLocal variable) {
|
||||
this.position = position;
|
||||
this.variable = variable;
|
||||
|
||||
@@ -22,7 +22,7 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class PartialPackage implements IPartialExpression {
|
||||
private final ZenPosition position;
|
||||
private final SymbolPackage contents;
|
||||
|
||||
|
||||
public PartialPackage(ZenPosition position, SymbolPackage contents) {
|
||||
this.position = position;
|
||||
this.contents = contents;
|
||||
|
||||
+4
-4
@@ -29,7 +29,7 @@ public class PartialStaticGenerated implements IPartialExpression {
|
||||
private final String signature;
|
||||
private final ZenType[] argumentTypes;
|
||||
private final ZenType returnType;
|
||||
|
||||
|
||||
public PartialStaticGenerated(ZenPosition position, String owner, String method, String signature, ZenType[] argumentTypes, ZenType returnType) {
|
||||
this.position = position;
|
||||
this.owner = owner;
|
||||
@@ -38,7 +38,7 @@ public class PartialStaticGenerated implements IPartialExpression {
|
||||
this.argumentTypes = argumentTypes;
|
||||
this.returnType = returnType;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Expression eval(IEnvironmentGlobal environment) {
|
||||
environment.error(position, "cannot use a function as value");
|
||||
@@ -63,12 +63,12 @@ public class PartialStaticGenerated implements IPartialExpression {
|
||||
environment.error(position, "invalid number of arguments");
|
||||
return new ExpressionInvalid(position);
|
||||
}
|
||||
|
||||
|
||||
Expression[] arguments = new Expression[argumentTypes.length];
|
||||
for (int i = 0; i < arguments.length; i++) {
|
||||
arguments[i] = values[i].cast(position, environment, argumentTypes[i]);
|
||||
}
|
||||
|
||||
|
||||
return new ExpressionCallStatic(position, environment, JavaMethod.getStatic(owner, method, returnType, argumentTypes), arguments);
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -24,7 +24,7 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class PartialStaticGetter implements IPartialExpression {
|
||||
private final ZenPosition position;
|
||||
private final IJavaMethod method;
|
||||
|
||||
|
||||
public PartialStaticGetter(ZenPosition position, IJavaMethod method) {
|
||||
this.position = position;
|
||||
this.method = method;
|
||||
|
||||
+1
-1
@@ -25,7 +25,7 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class PartialStaticMethod implements IPartialExpression {
|
||||
private final ZenPosition position;
|
||||
private final IJavaMethod method;
|
||||
|
||||
|
||||
public PartialStaticMethod(ZenPosition position, IJavaMethod method) {
|
||||
this.position = position;
|
||||
this.method = method;
|
||||
|
||||
@@ -22,7 +22,7 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class PartialType implements IPartialExpression {
|
||||
private final ZenPosition position;
|
||||
private final ZenType type;
|
||||
|
||||
|
||||
public PartialType(ZenPosition position, ZenType type) {
|
||||
this.position = position;
|
||||
this.type = type;
|
||||
@@ -55,7 +55,7 @@ public class PartialType implements IPartialExpression {
|
||||
public IZenSymbol toSymbol() {
|
||||
return new SymbolType(type);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return null; // not an expression
|
||||
|
||||
@@ -38,3 +38,4 @@
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript;
|
||||
|
||||
@@ -11,62 +11,64 @@ package stanhebben.zenscript.parser;
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class ArrayListI {
|
||||
private int[] data;
|
||||
private int size;
|
||||
private int[] data;
|
||||
private int size;
|
||||
|
||||
/**
|
||||
* Creates a new, empty ArrayList of integers.
|
||||
*/
|
||||
public ArrayListI() {
|
||||
data = new int[8];
|
||||
}
|
||||
/**
|
||||
* Creates a new, empty ArrayList of integers.
|
||||
*/
|
||||
public ArrayListI() {
|
||||
data = new int[8];
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a value.
|
||||
*
|
||||
* @param value integer value
|
||||
*/
|
||||
public void add(int value) {
|
||||
if (size == data.length) expand();
|
||||
data[size++] = value;
|
||||
}
|
||||
/**
|
||||
* Adds a value.
|
||||
*
|
||||
* @param value integer value
|
||||
*/
|
||||
public void add(int value) {
|
||||
if (size == data.length)
|
||||
expand();
|
||||
data[size++] = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value at the specified position.
|
||||
*
|
||||
* @param index position
|
||||
* @return value at this position
|
||||
*/
|
||||
public int get(int index) {
|
||||
if (index >= size) throw new ArrayIndexOutOfBoundsException();
|
||||
return data[index];
|
||||
}
|
||||
/**
|
||||
* Gets the value at the specified position.
|
||||
*
|
||||
* @param index position
|
||||
* @return value at this position
|
||||
*/
|
||||
public int get(int index) {
|
||||
if (index >= size)
|
||||
throw new ArrayIndexOutOfBoundsException();
|
||||
return data[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the size of this ArrayList.
|
||||
*
|
||||
* @return size
|
||||
*/
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
/**
|
||||
* Gets the size of this ArrayList.
|
||||
*
|
||||
* @return size
|
||||
*/
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
////////////////////
|
||||
// Private methods
|
||||
////////////////////
|
||||
// //////////////////
|
||||
// Private methods
|
||||
// //////////////////
|
||||
|
||||
/**
|
||||
* Doubles the size of this array.
|
||||
*/
|
||||
private void expand() {
|
||||
int[] newdata = new int[data.length * 2];
|
||||
System.arraycopy(data, 0, newdata, 0, size);
|
||||
data = newdata;
|
||||
}
|
||||
/**
|
||||
* Doubles the size of this array.
|
||||
*/
|
||||
private void expand() {
|
||||
int[] newdata = new int[data.length * 2];
|
||||
System.arraycopy(data, 0, newdata, 0, size);
|
||||
data = newdata;
|
||||
}
|
||||
|
||||
public int[] toArray() {
|
||||
int[] result = new int[size];
|
||||
System.arraycopy(data, 0, result, 0, size);
|
||||
return result;
|
||||
}
|
||||
public int[] toArray() {
|
||||
int[] result = new int[size];
|
||||
System.arraycopy(data, 0, result, 0, size);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -13,107 +13,111 @@ package stanhebben.zenscript.parser;
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class CharStream {
|
||||
private char[] data;
|
||||
private int index;
|
||||
private char[] data;
|
||||
private int index;
|
||||
|
||||
/**
|
||||
* Constructs a character stream from a data string.
|
||||
*
|
||||
* @param data data string
|
||||
*/
|
||||
public CharStream(String data) {
|
||||
this.data = data.toCharArray();
|
||||
index = 0;
|
||||
}
|
||||
/**
|
||||
* Constructs a character stream from a data string.
|
||||
*
|
||||
* @param data data string
|
||||
*/
|
||||
public CharStream(String data) {
|
||||
this.data = data.toCharArray();
|
||||
index = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the next character in the stream equals the specified character.
|
||||
*
|
||||
* @param ch checked character
|
||||
* @return true if the next character equals the specified character
|
||||
*/
|
||||
public boolean peek(char ch) {
|
||||
if (index == data.length) return false;
|
||||
return data[index] == ch;
|
||||
}
|
||||
/**
|
||||
* Checks if the next character in the stream equals the specified
|
||||
* character.
|
||||
*
|
||||
* @param ch checked character
|
||||
* @return true if the next character equals the specified character
|
||||
*/
|
||||
public boolean peek(char ch) {
|
||||
if (index == data.length)
|
||||
return false;
|
||||
return data[index] == ch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes an optional character. If the next character does not equal
|
||||
* the specified character, nothing will happen.
|
||||
*
|
||||
* @param ch checked character
|
||||
* @return true if the next character equals the specified character
|
||||
*/
|
||||
public boolean optional(char ch) {
|
||||
if (index == data.length) return false;
|
||||
if (data[index] == ch) {
|
||||
index++;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Processes an optional character. If the next character does not equal the
|
||||
* specified character, nothing will happen.
|
||||
*
|
||||
* @param ch checked character
|
||||
* @return true if the next character equals the specified character
|
||||
*/
|
||||
public boolean optional(char ch) {
|
||||
if (index == data.length)
|
||||
return false;
|
||||
if (data[index] == ch) {
|
||||
index++;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes an optional range of characters. If the next character is not
|
||||
* within the specified bounds, nothing will happen.
|
||||
*
|
||||
* @param from lower character value bound, inclusive
|
||||
* @param to upper character value bound, inclusive
|
||||
* @return the processed character
|
||||
*/
|
||||
public char optional(char from, char to) {
|
||||
if (index == data.length) return 0;
|
||||
if (data[index] >= from && data[index] <= to) {
|
||||
return data[index++];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Processes an optional range of characters. If the next character is not
|
||||
* within the specified bounds, nothing will happen.
|
||||
*
|
||||
* @param from lower character value bound, inclusive
|
||||
* @param to upper character value bound, inclusive
|
||||
* @return the processed character
|
||||
*/
|
||||
public char optional(char from, char to) {
|
||||
if (index == data.length)
|
||||
return 0;
|
||||
if (data[index] >= from && data[index] <= to) {
|
||||
return data[index++];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes a required character. If the next character does not equal the
|
||||
* specified character, an IllegalArgumentException is thrown.
|
||||
*
|
||||
* @param ch required character
|
||||
*/
|
||||
public void required(char ch) {
|
||||
if (data[index] != ch) {
|
||||
throw new IllegalArgumentException("Unexpected character: " + data[index]);
|
||||
}
|
||||
index++;
|
||||
}
|
||||
/**
|
||||
* Processes a required character. If the next character does not equal the
|
||||
* specified character, an IllegalArgumentException is thrown.
|
||||
*
|
||||
* @param ch required character
|
||||
*/
|
||||
public void required(char ch) {
|
||||
if (data[index] != ch) {
|
||||
throw new IllegalArgumentException("Unexpected character: " + data[index]);
|
||||
}
|
||||
index++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes a required character range. If the next character is not within
|
||||
* the specified bounds, an IllegalArgumentException is thrown.
|
||||
*
|
||||
* @param from lower character value bound, inclusive
|
||||
* @param to upper character value bound, inclusive
|
||||
* @return the processed character
|
||||
*/
|
||||
public char required(char from, char to) {
|
||||
if (data[index] < from || data[index] > to) {
|
||||
throw new IllegalArgumentException("Unexpected character: " + data[index]);
|
||||
}
|
||||
return data[index++];
|
||||
}
|
||||
/**
|
||||
* Processes a required character range. If the next character is not within
|
||||
* the specified bounds, an IllegalArgumentException is thrown.
|
||||
*
|
||||
* @param from lower character value bound, inclusive
|
||||
* @param to upper character value bound, inclusive
|
||||
* @return the processed character
|
||||
*/
|
||||
public char required(char from, char to) {
|
||||
if (data[index] < from || data[index] > to) {
|
||||
throw new IllegalArgumentException("Unexpected character: " + data[index]);
|
||||
}
|
||||
return data[index++];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next character in the stream.
|
||||
*
|
||||
* @return the next character
|
||||
*/
|
||||
public char next() {
|
||||
return data[index++];
|
||||
}
|
||||
/**
|
||||
* Returns the next character in the stream.
|
||||
*
|
||||
* @return the next character
|
||||
*/
|
||||
public char next() {
|
||||
return data[index++];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the end of the stream has been reached.
|
||||
*
|
||||
* @return true if there are more characters in the stream
|
||||
*/
|
||||
public boolean hasMore() {
|
||||
return index < data.length;
|
||||
}
|
||||
/**
|
||||
* Checks if the end of the stream has been reached.
|
||||
*
|
||||
* @return true if there are more characters in the stream
|
||||
*/
|
||||
public boolean hasMore() {
|
||||
return index < data.length;
|
||||
}
|
||||
}
|
||||
@@ -8,8 +8,8 @@ package stanhebben.zenscript.parser;
|
||||
import static java.util.Arrays.sort;
|
||||
|
||||
/**
|
||||
* Represents a compiled DFA. A compiled DFA has a compact representation and
|
||||
* is immediately usable for efficient processing.
|
||||
* Represents a compiled DFA. A compiled DFA has a compact representation and is
|
||||
* immediately usable for efficient processing.
|
||||
*
|
||||
* A compiled DFA can be converted to a compact integer array. This array can
|
||||
* then be hardcoded in an application.
|
||||
@@ -17,136 +17,137 @@ import static java.util.Arrays.sort;
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class CompiledDFA {
|
||||
public static final int NOFINAL = Integer.MIN_VALUE;
|
||||
public static final int NOFINAL = Integer.MIN_VALUE;
|
||||
|
||||
public HashMapII[] transitions;
|
||||
public int[] finals;
|
||||
public HashMapII[] transitions;
|
||||
public int[] finals;
|
||||
|
||||
/**
|
||||
* Constructs a compiled DFA from the specified transition graph and finals
|
||||
* arrays.
|
||||
*
|
||||
* The transition array specifies all transitions for each state. The finals
|
||||
* array specifies the final class index of each state, or NOFINAL if the state
|
||||
* is not a final. There can multiple final classes, which can, for example,
|
||||
* be used to distinguish token types.
|
||||
*
|
||||
* @param transitions transitions graph
|
||||
* @param finals finals
|
||||
*/
|
||||
public CompiledDFA(HashMapII[] transitions, int[] finals) {
|
||||
this.transitions = transitions;
|
||||
this.finals = finals;
|
||||
}
|
||||
/**
|
||||
* Constructs a compiled DFA from the specified transition graph and finals
|
||||
* arrays.
|
||||
*
|
||||
* The transition array specifies all transitions for each state. The finals
|
||||
* array specifies the final class index of each state, or NOFINAL if the
|
||||
* state is not a final. There can multiple final classes, which can, for
|
||||
* example, be used to distinguish token types.
|
||||
*
|
||||
* @param transitions transitions graph
|
||||
* @param finals finals
|
||||
*/
|
||||
public CompiledDFA(HashMapII[] transitions, int[] finals) {
|
||||
this.transitions = transitions;
|
||||
this.finals = finals;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an integer array with a DFA definition (generated by toArray())
|
||||
* to a CompiledDFA.
|
||||
*
|
||||
* @param definition definition array
|
||||
*/
|
||||
public CompiledDFA(int[] definition) {
|
||||
int ix = 0;
|
||||
int numStates = definition[ix++];
|
||||
transitions = new HashMapII[numStates];
|
||||
finals = new int[numStates];
|
||||
/**
|
||||
* Converts an integer array with a DFA definition (generated by toArray())
|
||||
* to a CompiledDFA.
|
||||
*
|
||||
* @param definition definition array
|
||||
*/
|
||||
public CompiledDFA(int[] definition) {
|
||||
int ix = 0;
|
||||
int numStates = definition[ix++];
|
||||
transitions = new HashMapII[numStates];
|
||||
finals = new int[numStates];
|
||||
|
||||
for (int i = 0; i < numStates; i++) {
|
||||
transitions[i] = new HashMapII();
|
||||
finals[i] = definition[ix++];
|
||||
for (int i = 0; i < numStates; i++) {
|
||||
transitions[i] = new HashMapII();
|
||||
finals[i] = definition[ix++];
|
||||
|
||||
int numRanges = definition[ix++];
|
||||
for (int j = 0; j < numRanges; j++) {
|
||||
int from = definition[ix++];
|
||||
int to = definition[ix++];
|
||||
int state = definition[ix++];
|
||||
for (int k = from; k <= to; k++) {
|
||||
transitions[i].put(k, state);
|
||||
}
|
||||
}
|
||||
int numRanges = definition[ix++];
|
||||
for (int j = 0; j < numRanges; j++) {
|
||||
int from = definition[ix++];
|
||||
int to = definition[ix++];
|
||||
int state = definition[ix++];
|
||||
for (int k = from; k <= to; k++) {
|
||||
transitions[i].put(k, state);
|
||||
}
|
||||
}
|
||||
|
||||
int numSingles = definition[ix++];
|
||||
for (int j = 0; j < numSingles; j++) {
|
||||
int label = definition[ix++];
|
||||
int state = definition[ix++];
|
||||
transitions[i].put(label, state);
|
||||
}
|
||||
}
|
||||
}
|
||||
int numSingles = definition[ix++];
|
||||
for (int j = 0; j < numSingles; j++) {
|
||||
int label = definition[ix++];
|
||||
int state = definition[ix++];
|
||||
transitions[i].put(label, state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the DFA to an integer array.
|
||||
*
|
||||
* @return an integer array representation of the DFA
|
||||
*/
|
||||
public int[] toArray() {
|
||||
ArrayListI result = new ArrayListI();
|
||||
result.add(finals.length);
|
||||
/**
|
||||
* Converts the DFA to an integer array.
|
||||
*
|
||||
* @return an integer array representation of the DFA
|
||||
*/
|
||||
public int[] toArray() {
|
||||
ArrayListI result = new ArrayListI();
|
||||
result.add(finals.length);
|
||||
|
||||
for (int i = 0; i < finals.length; i++) {
|
||||
result.add(finals[i]);
|
||||
for (int i = 0; i < finals.length; i++) {
|
||||
result.add(finals[i]);
|
||||
|
||||
ArrayListI singles = new ArrayListI();
|
||||
ArrayListI rangeFrom = new ArrayListI();
|
||||
ArrayListI rangeTo = new ArrayListI();
|
||||
ArrayListI singles = new ArrayListI();
|
||||
ArrayListI rangeFrom = new ArrayListI();
|
||||
ArrayListI rangeTo = new ArrayListI();
|
||||
|
||||
int[] keys = transitions[i].keysArray();
|
||||
sort(keys);
|
||||
for (int j = 0; j < keys.length; j++) {
|
||||
int from = j;
|
||||
int state = transitions[i].get(j);
|
||||
while (j + 1 < keys.length && keys[j + 1] == keys[j] + 1 && transitions[i].get(j + 1) == state) j++;
|
||||
if (j == from) {
|
||||
singles.add(keys[j]);
|
||||
} else {
|
||||
rangeFrom.add(keys[from]);
|
||||
rangeTo.add(keys[j]);
|
||||
}
|
||||
}
|
||||
int[] keys = transitions[i].keysArray();
|
||||
sort(keys);
|
||||
for (int j = 0; j < keys.length; j++) {
|
||||
int from = j;
|
||||
int state = transitions[i].get(j);
|
||||
while (j + 1 < keys.length && keys[j + 1] == keys[j] + 1 && transitions[i].get(j + 1) == state)
|
||||
j++;
|
||||
if (j == from) {
|
||||
singles.add(keys[j]);
|
||||
} else {
|
||||
rangeFrom.add(keys[from]);
|
||||
rangeTo.add(keys[j]);
|
||||
}
|
||||
}
|
||||
|
||||
result.add(rangeFrom.size());
|
||||
for (int j = 0; j < rangeFrom.size(); j++) {
|
||||
result.add(rangeFrom.get(j));
|
||||
result.add(rangeTo.get(j));
|
||||
result.add(transitions[i].get(rangeFrom.get(j)));
|
||||
}
|
||||
result.add(singles.size());
|
||||
for (int j = 0; j < singles.size(); j++) {
|
||||
result.add(singles.get(j));
|
||||
result.add(transitions[i].get(singles.get(j)));
|
||||
}
|
||||
}
|
||||
return result.toArray();
|
||||
}
|
||||
result.add(rangeFrom.size());
|
||||
for (int j = 0; j < rangeFrom.size(); j++) {
|
||||
result.add(rangeFrom.get(j));
|
||||
result.add(rangeTo.get(j));
|
||||
result.add(transitions[i].get(rangeFrom.get(j)));
|
||||
}
|
||||
result.add(singles.size());
|
||||
for (int j = 0; j < singles.size(); j++) {
|
||||
result.add(singles.get(j));
|
||||
result.add(transitions[i].get(singles.get(j)));
|
||||
}
|
||||
}
|
||||
return result.toArray();
|
||||
}
|
||||
|
||||
/* Used for debugging */
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (int i = 0; i < transitions.length; i++) {
|
||||
HashMapII map = transitions[i];
|
||||
/* Used for debugging */
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (int i = 0; i < transitions.length; i++) {
|
||||
HashMapII map = transitions[i];
|
||||
|
||||
IteratorI it = map.keys();
|
||||
while (it.hasNext()) {
|
||||
int v = it.next();
|
||||
result.append("edge(");
|
||||
result.append(i);
|
||||
result.append(", ");
|
||||
result.append(v);
|
||||
result.append("): ");
|
||||
result.append(map.get(v));
|
||||
result.append("\r\n");
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < finals.length; i++) {
|
||||
if (finals[i] != DFA.NOFINAL) {
|
||||
result.append("final(");
|
||||
result.append(i);
|
||||
result.append("): ");
|
||||
result.append(finals[i]);
|
||||
result.append("\r\n");
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
IteratorI it = map.keys();
|
||||
while (it.hasNext()) {
|
||||
int v = it.next();
|
||||
result.append("edge(");
|
||||
result.append(i);
|
||||
result.append(", ");
|
||||
result.append(v);
|
||||
result.append("): ");
|
||||
result.append(map.get(v));
|
||||
result.append("\r\n");
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < finals.length; i++) {
|
||||
if (finals[i] != DFA.NOFINAL) {
|
||||
result.append("final(");
|
||||
result.append(i);
|
||||
result.append("): ");
|
||||
result.append(finals[i]);
|
||||
result.append("\r\n");
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
}
|
||||
@@ -16,234 +16,235 @@ import java.util.Queue;
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class DFA {
|
||||
public static final int NOFINAL = Integer.MIN_VALUE;
|
||||
public static final int NOFINAL = Integer.MIN_VALUE;
|
||||
|
||||
private DFAState initial;
|
||||
private DFAState initial;
|
||||
|
||||
/**
|
||||
* Constructs a new DFA with the specified initial state.
|
||||
*
|
||||
* @param initial
|
||||
*/
|
||||
public DFA(DFAState initial) {
|
||||
this.initial = initial;
|
||||
}
|
||||
/**
|
||||
* Constructs a new DFA with the specified initial state.
|
||||
*
|
||||
* @param initial
|
||||
*/
|
||||
public DFA(DFAState initial) {
|
||||
this.initial = initial;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compiles this DFA into a more efficient structure.
|
||||
*
|
||||
* @return the compiled DFA
|
||||
*/
|
||||
public CompiledDFA compile() {
|
||||
ArrayList<DFAState> nodeList = new ArrayList<DFAState>();
|
||||
HashMap<DFAState, Integer> nodes = new HashMap<DFAState, Integer>();
|
||||
nodes.put(initial, 0);
|
||||
nodeList.add(initial);
|
||||
/**
|
||||
* Compiles this DFA into a more efficient structure.
|
||||
*
|
||||
* @return the compiled DFA
|
||||
*/
|
||||
public CompiledDFA compile() {
|
||||
ArrayList<DFAState> nodeList = new ArrayList<DFAState>();
|
||||
HashMap<DFAState, Integer> nodes = new HashMap<DFAState, Integer>();
|
||||
nodes.put(initial, 0);
|
||||
nodeList.add(initial);
|
||||
|
||||
/* Find all reachable nodes in the dfs */
|
||||
int counter = 1;
|
||||
Queue<DFAState> todo = new LinkedList<DFAState>();
|
||||
todo.add(initial);
|
||||
/* Find all reachable nodes in the dfs */
|
||||
int counter = 1;
|
||||
Queue<DFAState> todo = new LinkedList<DFAState>();
|
||||
todo.add(initial);
|
||||
|
||||
while (!todo.isEmpty()) {
|
||||
DFAState current = todo.poll();
|
||||
while (!todo.isEmpty()) {
|
||||
DFAState current = todo.poll();
|
||||
|
||||
IteratorI it = current.transitions.keys();
|
||||
while (it.hasNext()) {
|
||||
int k = it.next();
|
||||
DFAState next = current.transitions.get(k);
|
||||
if (!nodes.containsKey(next)) {
|
||||
todo.add(next);
|
||||
nodes.put(next, counter++);
|
||||
nodeList.add(next);
|
||||
}
|
||||
}
|
||||
}
|
||||
IteratorI it = current.transitions.keys();
|
||||
while (it.hasNext()) {
|
||||
int k = it.next();
|
||||
DFAState next = current.transitions.get(k);
|
||||
if (!nodes.containsKey(next)) {
|
||||
todo.add(next);
|
||||
nodes.put(next, counter++);
|
||||
nodeList.add(next);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Compile */
|
||||
HashMapII[] transitions = new HashMapII[counter];
|
||||
int[] finals2 = new int[counter];
|
||||
/* Compile */
|
||||
HashMapII[] transitions = new HashMapII[counter];
|
||||
int[] finals2 = new int[counter];
|
||||
|
||||
for (DFAState node : nodeList) {
|
||||
int index = nodes.get(node);
|
||||
finals2[index] = node.finalCode;
|
||||
for (DFAState node : nodeList) {
|
||||
int index = nodes.get(node);
|
||||
finals2[index] = node.finalCode;
|
||||
|
||||
transitions[index] = new HashMapII();
|
||||
IteratorI it = node.transitions.keys();
|
||||
while (it.hasNext()) {
|
||||
int k = it.next();
|
||||
DFAState next = node.transitions.get(k);
|
||||
transitions[index].put(k, nodes.get(next));
|
||||
}
|
||||
}
|
||||
transitions[index] = new HashMapII();
|
||||
IteratorI it = node.transitions.keys();
|
||||
while (it.hasNext()) {
|
||||
int k = it.next();
|
||||
DFAState next = node.transitions.get(k);
|
||||
transitions[index].put(k, nodes.get(next));
|
||||
}
|
||||
}
|
||||
|
||||
return new CompiledDFA(transitions, finals2);
|
||||
}
|
||||
return new CompiledDFA(transitions, finals2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the minimal version of this DFA.
|
||||
*
|
||||
* @return the minimal DFA
|
||||
*/
|
||||
public DFA optimize() {
|
||||
CompiledDFA compiled = compile();
|
||||
HashMapII[] transitions = compiled.transitions;
|
||||
int size = transitions.length;
|
||||
/**
|
||||
* Generates the minimal version of this DFA.
|
||||
*
|
||||
* @return the minimal DFA
|
||||
*/
|
||||
public DFA optimize() {
|
||||
CompiledDFA compiled = compile();
|
||||
HashMapII[] transitions = compiled.transitions;
|
||||
int size = transitions.length;
|
||||
|
||||
/* Collect all edges and determine alphabet */
|
||||
HashSetI alphabet = new HashSetI();
|
||||
for (int i = 0; i < size; i++) {
|
||||
IteratorI it = transitions[i].keys();
|
||||
while (it.hasNext()) {
|
||||
int k = it.next();
|
||||
alphabet.add(k);
|
||||
}
|
||||
}
|
||||
/* Collect all edges and determine alphabet */
|
||||
HashSetI alphabet = new HashSetI();
|
||||
for (int i = 0; i < size; i++) {
|
||||
IteratorI it = transitions[i].keys();
|
||||
while (it.hasNext()) {
|
||||
int k = it.next();
|
||||
alphabet.add(k);
|
||||
}
|
||||
}
|
||||
|
||||
/* Initialize distinguishing array */
|
||||
boolean[][] distinguishable = new boolean[size + 1][size + 1];
|
||||
for (int i = 0; i < size; i++) {
|
||||
for (int j = 0; j < size; j++) {
|
||||
distinguishable[i][j] = compiled.finals[i] != compiled.finals[j];
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < size; i++) {
|
||||
distinguishable[i][size] = true;
|
||||
distinguishable[size][i] = true;
|
||||
}
|
||||
/* Initialize distinguishing array */
|
||||
boolean[][] distinguishable = new boolean[size + 1][size + 1];
|
||||
for (int i = 0; i < size; i++) {
|
||||
for (int j = 0; j < size; j++) {
|
||||
distinguishable[i][j] = compiled.finals[i] != compiled.finals[j];
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < size; i++) {
|
||||
distinguishable[i][size] = true;
|
||||
distinguishable[size][i] = true;
|
||||
}
|
||||
|
||||
/* Minimization algorithm implementation */
|
||||
boolean changed;
|
||||
do {
|
||||
changed = false;
|
||||
IteratorI ita = alphabet.iterator();
|
||||
while (ita.hasNext()) {
|
||||
int x = ita.next();
|
||||
for (int i = 0; i < size; i++) {
|
||||
int ti = transitions[i].get(x, size);
|
||||
for (int j = 0; j < size; j++) {
|
||||
if (distinguishable[i][j]) continue;
|
||||
/* Minimization algorithm implementation */
|
||||
boolean changed;
|
||||
do {
|
||||
changed = false;
|
||||
IteratorI ita = alphabet.iterator();
|
||||
while (ita.hasNext()) {
|
||||
int x = ita.next();
|
||||
for (int i = 0; i < size; i++) {
|
||||
int ti = transitions[i].get(x, size);
|
||||
for (int j = 0; j < size; j++) {
|
||||
if (distinguishable[i][j])
|
||||
continue;
|
||||
|
||||
int tj = transitions[j].get(x, size);
|
||||
if (distinguishable[ti][tj]) {
|
||||
distinguishable[i][j] = true;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (changed);
|
||||
int tj = transitions[j].get(x, size);
|
||||
if (distinguishable[ti][tj]) {
|
||||
distinguishable[i][j] = true;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (changed);
|
||||
|
||||
/* Group nodes */
|
||||
HashMapI<DFAState> nodeMap = new HashMapI<DFAState>();
|
||||
outer: for (int i = 0; i < size; i++) {
|
||||
for (int j = 0; j < size; j++) {
|
||||
if (!distinguishable[i][j] && nodeMap.containsKey(j)) {
|
||||
nodeMap.put(i, nodeMap.get(j));
|
||||
if (compiled.finals[i] != NOFINAL) {
|
||||
if (nodeMap.get(j).getFinal() != NOFINAL && nodeMap.get(j).getFinal() != compiled.finals[i]) {
|
||||
throw new RuntimeException("Eh?");
|
||||
}
|
||||
}
|
||||
continue outer;
|
||||
}
|
||||
}
|
||||
DFAState node = new DFAState();
|
||||
node.setFinal(compiled.finals[i]);
|
||||
nodeMap.put(i, node);
|
||||
}
|
||||
/* Group nodes */
|
||||
HashMapI<DFAState> nodeMap = new HashMapI<DFAState>();
|
||||
outer: for (int i = 0; i < size; i++) {
|
||||
for (int j = 0; j < size; j++) {
|
||||
if (!distinguishable[i][j] && nodeMap.containsKey(j)) {
|
||||
nodeMap.put(i, nodeMap.get(j));
|
||||
if (compiled.finals[i] != NOFINAL) {
|
||||
if (nodeMap.get(j).getFinal() != NOFINAL && nodeMap.get(j).getFinal() != compiled.finals[i]) {
|
||||
throw new RuntimeException("Eh?");
|
||||
}
|
||||
}
|
||||
continue outer;
|
||||
}
|
||||
}
|
||||
DFAState node = new DFAState();
|
||||
node.setFinal(compiled.finals[i]);
|
||||
nodeMap.put(i, node);
|
||||
}
|
||||
|
||||
for (int i = 0; i < compiled.transitions.length; i++) {
|
||||
IteratorI iter = transitions[i].keys();
|
||||
while (iter.hasNext()) {
|
||||
int k = iter.next();
|
||||
for (int i = 0; i < compiled.transitions.length; i++) {
|
||||
IteratorI iter = transitions[i].keys();
|
||||
while (iter.hasNext()) {
|
||||
int k = iter.next();
|
||||
|
||||
nodeMap.get(i).addTransition(k, nodeMap.get(transitions[i].get(k)));
|
||||
}
|
||||
}
|
||||
|
||||
DFA result = new DFA(nodeMap.get(0));
|
||||
return result;
|
||||
}
|
||||
nodeMap.get(i).addTransition(k, nodeMap.get(transitions[i].get(k)));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder result = new StringBuilder();
|
||||
CompiledDFA dfs = compile();
|
||||
for (int i = 0; i < dfs.transitions.length; i++) {
|
||||
HashMapII map = dfs.transitions[i];
|
||||
|
||||
IteratorI it = map.keys();
|
||||
while (it.hasNext()) {
|
||||
int v = it.next();
|
||||
result.append("edge(");
|
||||
result.append(i);
|
||||
result.append(", ");
|
||||
result.append(v);
|
||||
result.append("): ");
|
||||
result.append(map.get(v));
|
||||
result.append("\r\n");
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < dfs.finals.length; i++) {
|
||||
if (dfs.finals[i] != NOFINAL) {
|
||||
result.append("final(");
|
||||
result.append(i);
|
||||
result.append("): ");
|
||||
result.append(dfs.finals[i]);
|
||||
result.append("\r\n");
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
DFA result = new DFA(nodeMap.get(0));
|
||||
return result;
|
||||
}
|
||||
|
||||
/////////////////////////
|
||||
// Public inner classes
|
||||
/////////////////////////
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder result = new StringBuilder();
|
||||
CompiledDFA dfs = compile();
|
||||
for (int i = 0; i < dfs.transitions.length; i++) {
|
||||
HashMapII map = dfs.transitions[i];
|
||||
|
||||
/**
|
||||
* Represents a state in a DFA.
|
||||
*/
|
||||
public static class DFAState {
|
||||
private HashMapI<DFAState> transitions;
|
||||
private int finalCode = NOFINAL;
|
||||
IteratorI it = map.keys();
|
||||
while (it.hasNext()) {
|
||||
int v = it.next();
|
||||
result.append("edge(");
|
||||
result.append(i);
|
||||
result.append(", ");
|
||||
result.append(v);
|
||||
result.append("): ");
|
||||
result.append(map.get(v));
|
||||
result.append("\r\n");
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < dfs.finals.length; i++) {
|
||||
if (dfs.finals[i] != NOFINAL) {
|
||||
result.append("final(");
|
||||
result.append(i);
|
||||
result.append("): ");
|
||||
result.append(dfs.finals[i]);
|
||||
result.append("\r\n");
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new DFA state.
|
||||
*/
|
||||
public DFAState() {
|
||||
transitions = new HashMapI<DFAState>();
|
||||
}
|
||||
// ///////////////////////
|
||||
// Public inner classes
|
||||
// ///////////////////////
|
||||
|
||||
/**
|
||||
* Adds a transition.
|
||||
*
|
||||
* @param label transition edge label
|
||||
* @param next next state
|
||||
*/
|
||||
public void addTransition(int label, DFAState next) {
|
||||
transitions.put(label, next);
|
||||
}
|
||||
/**
|
||||
* Represents a state in a DFA.
|
||||
*/
|
||||
public static class DFAState {
|
||||
private HashMapI<DFAState> transitions;
|
||||
private int finalCode = NOFINAL;
|
||||
|
||||
/**
|
||||
* Sets the final class of this state. Finals can be divided in multiple
|
||||
* class, in which case each class gets its own index. Class NOFINAL is
|
||||
* used to indicate nonfinals.
|
||||
*
|
||||
* @param finalCode final index
|
||||
*/
|
||||
public void setFinal(int finalCode) {
|
||||
this.finalCode = finalCode;
|
||||
}
|
||||
/**
|
||||
* Creates a new DFA state.
|
||||
*/
|
||||
public DFAState() {
|
||||
transitions = new HashMapI<DFAState>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the final class of this state. Equals NOFINAL if this state is
|
||||
* not a final.
|
||||
*
|
||||
* @return final index
|
||||
*/
|
||||
public int getFinal() {
|
||||
return finalCode;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Adds a transition.
|
||||
*
|
||||
* @param label transition edge label
|
||||
* @param next next state
|
||||
*/
|
||||
public void addTransition(int label, DFAState next) {
|
||||
transitions.put(label, next);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the final class of this state. Finals can be divided in multiple
|
||||
* class, in which case each class gets its own index. Class NOFINAL is
|
||||
* used to indicate nonfinals.
|
||||
*
|
||||
* @param finalCode final index
|
||||
*/
|
||||
public void setFinal(int finalCode) {
|
||||
this.finalCode = finalCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the final class of this state. Equals NOFINAL if this state is
|
||||
* not a final.
|
||||
*
|
||||
* @return final index
|
||||
*/
|
||||
public int getFinal() {
|
||||
return finalCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,206 +11,211 @@ package stanhebben.zenscript.parser;
|
||||
* @param <T>
|
||||
*/
|
||||
public class HashMapI<T> {
|
||||
private int[] keys;
|
||||
private T[] values;
|
||||
private int[] next;
|
||||
private int mask;
|
||||
private int size;
|
||||
private int[] keys;
|
||||
private T[] values;
|
||||
private int[] next;
|
||||
private int mask;
|
||||
private int size;
|
||||
|
||||
/**
|
||||
* Creates a new HashMap for integer keys.
|
||||
*/
|
||||
public HashMapI() {
|
||||
keys = new int[16];
|
||||
values = (T[])new Object[16];
|
||||
next = new int[16];
|
||||
mask = 15;
|
||||
size = 0;
|
||||
/**
|
||||
* Creates a new HashMap for integer keys.
|
||||
*/
|
||||
public HashMapI() {
|
||||
keys = new int[16];
|
||||
values = (T[]) new Object[16];
|
||||
next = new int[16];
|
||||
mask = 15;
|
||||
size = 0;
|
||||
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
keys[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
keys[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Associates the specified value with the specified key in this map. If the
|
||||
* map previously contained a mapping for the key, the old value is replaced.
|
||||
*
|
||||
* @param key key with which the specified value is to be associated
|
||||
* @param value value to be associated with the specified key
|
||||
*/
|
||||
public void put(int key, T value) {
|
||||
if (size > (keys.length * 3) >> 2) {
|
||||
expand();
|
||||
}
|
||||
/**
|
||||
* Associates the specified value with the specified key in this map. If the
|
||||
* map previously contained a mapping for the key, the old value is
|
||||
* replaced.
|
||||
*
|
||||
* @param key key with which the specified value is to be associated
|
||||
* @param value value to be associated with the specified key
|
||||
*/
|
||||
public void put(int key, T value) {
|
||||
if (size > (keys.length * 3) >> 2) {
|
||||
expand();
|
||||
}
|
||||
|
||||
int index = key & mask;
|
||||
int index = key & mask;
|
||||
|
||||
if (keys[index] == Integer.MIN_VALUE) {
|
||||
keys[index] = key;
|
||||
values[index] = value;
|
||||
} else {
|
||||
if (keys[index] == key) {
|
||||
values[index] = value;
|
||||
return;
|
||||
}
|
||||
while (next[index] != 0) {
|
||||
index = next[index] - 1;
|
||||
if (keys[index] == key) {
|
||||
values[index] = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
int ref = index;
|
||||
while (keys[index] != Integer.MIN_VALUE) {
|
||||
index++;
|
||||
if (index == keys.length) index = 0;
|
||||
}
|
||||
next[ref] = index + 1;
|
||||
if (keys[index] == Integer.MIN_VALUE) {
|
||||
keys[index] = key;
|
||||
values[index] = value;
|
||||
} else {
|
||||
if (keys[index] == key) {
|
||||
values[index] = value;
|
||||
return;
|
||||
}
|
||||
while (next[index] != 0) {
|
||||
index = next[index] - 1;
|
||||
if (keys[index] == key) {
|
||||
values[index] = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
int ref = index;
|
||||
while (keys[index] != Integer.MIN_VALUE) {
|
||||
index++;
|
||||
if (index == keys.length)
|
||||
index = 0;
|
||||
}
|
||||
next[ref] = index + 1;
|
||||
|
||||
keys[index] = key;
|
||||
values[index] = value;
|
||||
}
|
||||
keys[index] = key;
|
||||
values[index] = value;
|
||||
}
|
||||
|
||||
size++;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value to which the specified key is mapped, or null if this
|
||||
* map contains no mapping for the key.
|
||||
*
|
||||
* @param key the key whose associated value is to be returned
|
||||
* @return the value to which the specified key is mapped, or null if this map contains no mapping for the key
|
||||
*/
|
||||
public T get(int key) {
|
||||
int index = key & mask;
|
||||
while (keys[index] != key) {
|
||||
if (next[index] == 0) {
|
||||
return null;
|
||||
}
|
||||
index = next[index] - 1;
|
||||
}
|
||||
return values[index];
|
||||
}
|
||||
/**
|
||||
* Returns the value to which the specified key is mapped, or null if this
|
||||
* map contains no mapping for the key.
|
||||
*
|
||||
* @param key the key whose associated value is to be returned
|
||||
* @return the value to which the specified key is mapped, or null if this
|
||||
* map contains no mapping for the key
|
||||
*/
|
||||
public T get(int key) {
|
||||
int index = key & mask;
|
||||
while (keys[index] != key) {
|
||||
if (next[index] == 0) {
|
||||
return null;
|
||||
}
|
||||
index = next[index] - 1;
|
||||
}
|
||||
return values[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this map contains a mapping for the specified key.
|
||||
*
|
||||
* @param key The key whose presence in this map is to be tested
|
||||
* @return true if this map contains a mapping for the specified key.
|
||||
*/
|
||||
public boolean containsKey(int key) {
|
||||
int index = key & mask;
|
||||
while (keys[index] != key) {
|
||||
if (next[index] == 0) {
|
||||
return false;
|
||||
}
|
||||
index = next[index] - 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Returns true if this map contains a mapping for the specified key.
|
||||
*
|
||||
* @param key The key whose presence in this map is to be tested
|
||||
* @return true if this map contains a mapping for the specified key.
|
||||
*/
|
||||
public boolean containsKey(int key) {
|
||||
int index = key & mask;
|
||||
while (keys[index] != key) {
|
||||
if (next[index] == 0) {
|
||||
return false;
|
||||
}
|
||||
index = next[index] - 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an iterator over the keys in this HashMap. The elements are
|
||||
* returned in no particular order.
|
||||
*
|
||||
* @return an iterator over the keys in this HashMap
|
||||
*/
|
||||
public IteratorI keys() {
|
||||
return new KeyIterator();
|
||||
}
|
||||
/**
|
||||
* Returns an iterator over the keys in this HashMap. The elements are
|
||||
* returned in no particular order.
|
||||
*
|
||||
* @return an iterator over the keys in this HashMap
|
||||
*/
|
||||
public IteratorI keys() {
|
||||
return new KeyIterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array with all keys in this HashMap. The elements are returned
|
||||
* in no particular order.
|
||||
*
|
||||
* @return an array with all keys in this HashMap
|
||||
*/
|
||||
public int[] keysArray() {
|
||||
int[] result = new int[size];
|
||||
int ix = 0;
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
if (keys[i] != Integer.MIN_VALUE) result[ix++] = keys[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* Returns an array with all keys in this HashMap. The elements are returned
|
||||
* in no particular order.
|
||||
*
|
||||
* @return an array with all keys in this HashMap
|
||||
*/
|
||||
public int[] keysArray() {
|
||||
int[] result = new int[size];
|
||||
int ix = 0;
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
if (keys[i] != Integer.MIN_VALUE)
|
||||
result[ix++] = keys[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
////////////////////
|
||||
// Private methods
|
||||
////////////////////
|
||||
// //////////////////
|
||||
// Private methods
|
||||
// //////////////////
|
||||
|
||||
/**
|
||||
* Expands the size of this array. (it doubles the size)
|
||||
*/
|
||||
private void expand() {
|
||||
int[] newKeys = new int[keys.length * 2];
|
||||
T[] newValues = (T[])new Object[values.length * 2];
|
||||
int[] newNext = new int[next.length * 2];
|
||||
int newMask = newKeys.length - 1;
|
||||
/**
|
||||
* Expands the size of this array. (it doubles the size)
|
||||
*/
|
||||
private void expand() {
|
||||
int[] newKeys = new int[keys.length * 2];
|
||||
T[] newValues = (T[]) new Object[values.length * 2];
|
||||
int[] newNext = new int[next.length * 2];
|
||||
int newMask = newKeys.length - 1;
|
||||
|
||||
for (int i = 0; i < newKeys.length; i++) {
|
||||
newKeys[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
for (int i = 0; i < newKeys.length; i++) {
|
||||
newKeys[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
if (keys[i] == Integer.MIN_VALUE) {
|
||||
continue;
|
||||
}
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
if (keys[i] == Integer.MIN_VALUE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int key = keys[i];
|
||||
T value = values[i];
|
||||
int index = key & newMask;
|
||||
int key = keys[i];
|
||||
T value = values[i];
|
||||
int index = key & newMask;
|
||||
|
||||
if (newKeys[index] == Integer.MIN_VALUE) {
|
||||
newKeys[index] = key;
|
||||
newValues[index] = value;
|
||||
} else {
|
||||
while (newNext[index] != 0) {
|
||||
index = newNext[index] - 1;
|
||||
}
|
||||
int ref = index;
|
||||
while (newKeys[index] != Integer.MIN_VALUE) {
|
||||
index = (index + 1) & newMask;
|
||||
}
|
||||
newNext[ref] = index + 1;
|
||||
if (newKeys[index] == Integer.MIN_VALUE) {
|
||||
newKeys[index] = key;
|
||||
newValues[index] = value;
|
||||
} else {
|
||||
while (newNext[index] != 0) {
|
||||
index = newNext[index] - 1;
|
||||
}
|
||||
int ref = index;
|
||||
while (newKeys[index] != Integer.MIN_VALUE) {
|
||||
index = (index + 1) & newMask;
|
||||
}
|
||||
newNext[ref] = index + 1;
|
||||
|
||||
newKeys[index] = key;
|
||||
newValues[index] = value;
|
||||
}
|
||||
}
|
||||
newKeys[index] = key;
|
||||
newValues[index] = value;
|
||||
}
|
||||
}
|
||||
|
||||
keys = newKeys;
|
||||
values = newValues;
|
||||
next = newNext;
|
||||
mask = newMask;
|
||||
}
|
||||
keys = newKeys;
|
||||
values = newValues;
|
||||
next = newNext;
|
||||
mask = newMask;
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
// Private inner classes
|
||||
//////////////////////////
|
||||
// ////////////////////////
|
||||
// Private inner classes
|
||||
// ////////////////////////
|
||||
|
||||
private class KeyIterator implements IteratorI {
|
||||
private int i;
|
||||
private class KeyIterator implements IteratorI {
|
||||
private int i;
|
||||
|
||||
public KeyIterator() {
|
||||
i = 0;
|
||||
skip();
|
||||
}
|
||||
public KeyIterator() {
|
||||
i = 0;
|
||||
skip();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return i < keys.length;
|
||||
}
|
||||
public boolean hasNext() {
|
||||
return i < keys.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int next() {
|
||||
int result = keys[i++];
|
||||
skip();
|
||||
return result;
|
||||
}
|
||||
public int next() {
|
||||
int result = keys[i++];
|
||||
skip();
|
||||
return result;
|
||||
}
|
||||
|
||||
private void skip() {
|
||||
while (i < keys.length && keys[i] == Integer.MIN_VALUE) i++;
|
||||
}
|
||||
}
|
||||
private void skip() {
|
||||
while (i < keys.length && keys[i] == Integer.MIN_VALUE)
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,233 +5,240 @@
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
/**
|
||||
* A HashMap implementation which is optimized to associate integers with integers.
|
||||
* A HashMap implementation which is optimized to associate integers with
|
||||
* integers.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class HashMapII {
|
||||
private int[] keys;
|
||||
private int[] values;
|
||||
private int[] next;
|
||||
private int mask;
|
||||
private int size;
|
||||
private int[] keys;
|
||||
private int[] values;
|
||||
private int[] next;
|
||||
private int mask;
|
||||
private int size;
|
||||
|
||||
/**
|
||||
* Creates a new HashMap for integer keys and values.
|
||||
*/
|
||||
public HashMapII() {
|
||||
keys = new int[16];
|
||||
values = new int[16];
|
||||
next = new int[16];
|
||||
mask = 15;
|
||||
size = 0;
|
||||
/**
|
||||
* Creates a new HashMap for integer keys and values.
|
||||
*/
|
||||
public HashMapII() {
|
||||
keys = new int[16];
|
||||
values = new int[16];
|
||||
next = new int[16];
|
||||
mask = 15;
|
||||
size = 0;
|
||||
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
keys[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of key/value pairs in the hashmap.
|
||||
*
|
||||
* @return hashmap size
|
||||
*/
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
keys[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Associates the specified value with the specified key in this map. If the
|
||||
* map previously contained a mapping for the key, the old value is replaced.
|
||||
*
|
||||
* @param key key with which the specified value is to be associated
|
||||
* @param value value to be associated with the specified key
|
||||
*/
|
||||
public void put(int key, int value) {
|
||||
if (size > (keys.length * 3) >> 2) {
|
||||
expand();
|
||||
}
|
||||
/**
|
||||
* Returns the number of key/value pairs in the hashmap.
|
||||
*
|
||||
* @return hashmap size
|
||||
*/
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
int index = key & mask;
|
||||
/**
|
||||
* Associates the specified value with the specified key in this map. If the
|
||||
* map previously contained a mapping for the key, the old value is
|
||||
* replaced.
|
||||
*
|
||||
* @param key key with which the specified value is to be associated
|
||||
* @param value value to be associated with the specified key
|
||||
*/
|
||||
public void put(int key, int value) {
|
||||
if (size > (keys.length * 3) >> 2) {
|
||||
expand();
|
||||
}
|
||||
|
||||
if (keys[index] == Integer.MIN_VALUE) {
|
||||
keys[index] = key;
|
||||
values[index] = value;
|
||||
} else {
|
||||
while (next[index] != 0) {
|
||||
index = next[index] - 1;
|
||||
if (keys[index] == key) {
|
||||
values[index] = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
int ref = index;
|
||||
while (keys[index] != Integer.MIN_VALUE) {
|
||||
index++;
|
||||
if (index == keys.length) index = 0;
|
||||
}
|
||||
next[ref] = index + 1;
|
||||
int index = key & mask;
|
||||
|
||||
keys[index] = key;
|
||||
values[index] = value;
|
||||
}
|
||||
if (keys[index] == Integer.MIN_VALUE) {
|
||||
keys[index] = key;
|
||||
values[index] = value;
|
||||
} else {
|
||||
while (next[index] != 0) {
|
||||
index = next[index] - 1;
|
||||
if (keys[index] == key) {
|
||||
values[index] = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
int ref = index;
|
||||
while (keys[index] != Integer.MIN_VALUE) {
|
||||
index++;
|
||||
if (index == keys.length)
|
||||
index = 0;
|
||||
}
|
||||
next[ref] = index + 1;
|
||||
|
||||
size++;
|
||||
}
|
||||
keys[index] = key;
|
||||
values[index] = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value to which the specified key is mapped, or null if this
|
||||
* map contains no mapping for the key.
|
||||
*
|
||||
* @param key the key whose associated value is to be returned
|
||||
* @return the value to which the specified key is mapped, or Integer.MIN_VALUE if this map contains no mapping for the key
|
||||
*/
|
||||
public int get(int key) {
|
||||
int index = key & mask;
|
||||
while (keys[index] != key) {
|
||||
if (next[index] == 0) {
|
||||
return Integer.MIN_VALUE;
|
||||
}
|
||||
index = next[index] - 1;
|
||||
}
|
||||
return values[index];
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value to which the specified key is mapped, or null if this
|
||||
* map contains no mapping for the key.
|
||||
*
|
||||
* @param key the key whose associated value is to be returned
|
||||
* @param def the default key value
|
||||
* @return the value to which the specified key is mapped, or the default value if this map contains no mapping for the key
|
||||
*/
|
||||
public int get(int key, int def) {
|
||||
int index = key & mask;
|
||||
while (keys[index] != key) {
|
||||
if (next[index] == 0) {
|
||||
return def;
|
||||
}
|
||||
index = next[index] - 1;
|
||||
}
|
||||
return values[index];
|
||||
}
|
||||
/**
|
||||
* Returns the value to which the specified key is mapped, or null if this
|
||||
* map contains no mapping for the key.
|
||||
*
|
||||
* @param key the key whose associated value is to be returned
|
||||
* @return the value to which the specified key is mapped, or
|
||||
* Integer.MIN_VALUE if this map contains no mapping for the key
|
||||
*/
|
||||
public int get(int key) {
|
||||
int index = key & mask;
|
||||
while (keys[index] != key) {
|
||||
if (next[index] == 0) {
|
||||
return Integer.MIN_VALUE;
|
||||
}
|
||||
index = next[index] - 1;
|
||||
}
|
||||
return values[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this map contains a mapping for the specified key.
|
||||
*
|
||||
* @param key The key whose presence in this map is to be tested
|
||||
* @return true if this map contains a mapping for the specified key.
|
||||
*/
|
||||
public boolean containsKey(int key) {
|
||||
int index = key & mask;
|
||||
while (keys[index] != key) {
|
||||
if (next[index] == 0) {
|
||||
return false;
|
||||
}
|
||||
index = next[index] - 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Returns the value to which the specified key is mapped, or null if this
|
||||
* map contains no mapping for the key.
|
||||
*
|
||||
* @param key the key whose associated value is to be returned
|
||||
* @param def the default key value
|
||||
* @return the value to which the specified key is mapped, or the default
|
||||
* value if this map contains no mapping for the key
|
||||
*/
|
||||
public int get(int key, int def) {
|
||||
int index = key & mask;
|
||||
while (keys[index] != key) {
|
||||
if (next[index] == 0) {
|
||||
return def;
|
||||
}
|
||||
index = next[index] - 1;
|
||||
}
|
||||
return values[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an iterator over the keys in this HashMap. The elements are
|
||||
* returned in no particular order.
|
||||
*
|
||||
* @return an iterator over the keys in this HashMap
|
||||
*/
|
||||
public IteratorI keys() {
|
||||
return new KeyIterator();
|
||||
}
|
||||
/**
|
||||
* Returns true if this map contains a mapping for the specified key.
|
||||
*
|
||||
* @param key The key whose presence in this map is to be tested
|
||||
* @return true if this map contains a mapping for the specified key.
|
||||
*/
|
||||
public boolean containsKey(int key) {
|
||||
int index = key & mask;
|
||||
while (keys[index] != key) {
|
||||
if (next[index] == 0) {
|
||||
return false;
|
||||
}
|
||||
index = next[index] - 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array with all keys in this HashMap. The elements are returned
|
||||
* in no particular order.
|
||||
*
|
||||
* @return an array with all keys in this HashMap
|
||||
*/
|
||||
public int[] keysArray() {
|
||||
int[] result = new int[size];
|
||||
int ix = 0;
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
if (keys[i] != Integer.MIN_VALUE) result[ix++] = keys[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* Returns an iterator over the keys in this HashMap. The elements are
|
||||
* returned in no particular order.
|
||||
*
|
||||
* @return an iterator over the keys in this HashMap
|
||||
*/
|
||||
public IteratorI keys() {
|
||||
return new KeyIterator();
|
||||
}
|
||||
|
||||
////////////////////
|
||||
// Private methods
|
||||
////////////////////
|
||||
/**
|
||||
* Returns an array with all keys in this HashMap. The elements are returned
|
||||
* in no particular order.
|
||||
*
|
||||
* @return an array with all keys in this HashMap
|
||||
*/
|
||||
public int[] keysArray() {
|
||||
int[] result = new int[size];
|
||||
int ix = 0;
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
if (keys[i] != Integer.MIN_VALUE)
|
||||
result[ix++] = keys[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expands the size of this array. (it doubles the size)
|
||||
*/
|
||||
private void expand() {
|
||||
int[] newKeys = new int[keys.length * 2];
|
||||
int[] newValues = new int[values.length * 2];
|
||||
int[] newNext = new int[next.length * 2];
|
||||
int newMask = newKeys.length - 1;
|
||||
// //////////////////
|
||||
// Private methods
|
||||
// //////////////////
|
||||
|
||||
for (int i = 0; i < newKeys.length; i++) {
|
||||
newKeys[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
/**
|
||||
* Expands the size of this array. (it doubles the size)
|
||||
*/
|
||||
private void expand() {
|
||||
int[] newKeys = new int[keys.length * 2];
|
||||
int[] newValues = new int[values.length * 2];
|
||||
int[] newNext = new int[next.length * 2];
|
||||
int newMask = newKeys.length - 1;
|
||||
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
if (keys[i] == Integer.MIN_VALUE) {
|
||||
continue;
|
||||
}
|
||||
for (int i = 0; i < newKeys.length; i++) {
|
||||
newKeys[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
|
||||
int key = keys[i];
|
||||
int value = values[i];
|
||||
int index = key & newMask;
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
if (keys[i] == Integer.MIN_VALUE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (newKeys[index] == Integer.MIN_VALUE) {
|
||||
newKeys[index] = key;
|
||||
newValues[index] = value;
|
||||
} else {
|
||||
while (newNext[index] != 0) {
|
||||
index = newNext[index] - 1;
|
||||
}
|
||||
int ref = index;
|
||||
while (newKeys[index] != Integer.MIN_VALUE) {
|
||||
index = (index + 1) & newMask;
|
||||
}
|
||||
newNext[ref] = index + 1;
|
||||
int key = keys[i];
|
||||
int value = values[i];
|
||||
int index = key & newMask;
|
||||
|
||||
newKeys[index] = key;
|
||||
newValues[index] = value;
|
||||
}
|
||||
}
|
||||
if (newKeys[index] == Integer.MIN_VALUE) {
|
||||
newKeys[index] = key;
|
||||
newValues[index] = value;
|
||||
} else {
|
||||
while (newNext[index] != 0) {
|
||||
index = newNext[index] - 1;
|
||||
}
|
||||
int ref = index;
|
||||
while (newKeys[index] != Integer.MIN_VALUE) {
|
||||
index = (index + 1) & newMask;
|
||||
}
|
||||
newNext[ref] = index + 1;
|
||||
|
||||
keys = newKeys;
|
||||
values = newValues;
|
||||
next = newNext;
|
||||
mask = newMask;
|
||||
}
|
||||
newKeys[index] = key;
|
||||
newValues[index] = value;
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
// Private inner classes
|
||||
//////////////////////////
|
||||
keys = newKeys;
|
||||
values = newValues;
|
||||
next = newNext;
|
||||
mask = newMask;
|
||||
}
|
||||
|
||||
private class KeyIterator implements IteratorI {
|
||||
private int i;
|
||||
// ////////////////////////
|
||||
// Private inner classes
|
||||
// ////////////////////////
|
||||
|
||||
public KeyIterator() {
|
||||
i = 0;
|
||||
skip();
|
||||
}
|
||||
private class KeyIterator implements IteratorI {
|
||||
private int i;
|
||||
|
||||
public boolean hasNext() {
|
||||
return i < keys.length;
|
||||
}
|
||||
public KeyIterator() {
|
||||
i = 0;
|
||||
skip();
|
||||
}
|
||||
|
||||
public int next() {
|
||||
int result = keys[i++];
|
||||
skip();
|
||||
return result;
|
||||
}
|
||||
public boolean hasNext() {
|
||||
return i < keys.length;
|
||||
}
|
||||
|
||||
private void skip() {
|
||||
while (i < keys.length && keys[i] == Integer.MIN_VALUE) i++;
|
||||
}
|
||||
}
|
||||
public int next() {
|
||||
int result = keys[i++];
|
||||
skip();
|
||||
return result;
|
||||
}
|
||||
|
||||
private void skip() {
|
||||
while (i < keys.length && keys[i] == Integer.MIN_VALUE)
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,185 +12,187 @@ import java.util.Arrays;
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class HashSetI {
|
||||
private int[] values;
|
||||
private int[] next;
|
||||
private int mask;
|
||||
private int size;
|
||||
private int[] values;
|
||||
private int[] next;
|
||||
private int mask;
|
||||
private int size;
|
||||
|
||||
/**
|
||||
* Creates a new HashSet for integer values.
|
||||
*/
|
||||
public HashSetI() {
|
||||
values = new int[16];
|
||||
next = new int[16];
|
||||
mask = 15;
|
||||
size = 0;
|
||||
/**
|
||||
* Creates a new HashSet for integer values.
|
||||
*/
|
||||
public HashSetI() {
|
||||
values = new int[16];
|
||||
next = new int[16];
|
||||
mask = 15;
|
||||
size = 0;
|
||||
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
values[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
values[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
public HashSetI(HashSetI original) {
|
||||
values = Arrays.copyOf(original.values, original.values.length);
|
||||
next = Arrays.copyOf(original.next, original.next.length);
|
||||
mask = original.mask;
|
||||
size = original.size;
|
||||
}
|
||||
public HashSetI(HashSetI original) {
|
||||
values = Arrays.copyOf(original.values, original.values.length);
|
||||
next = Arrays.copyOf(original.next, original.next.length);
|
||||
mask = original.mask;
|
||||
size = original.size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the specified value to this HashSet. If this value is already in this
|
||||
* HashSet, nothing happens.
|
||||
*
|
||||
* @param value value to be added to the HashSet
|
||||
*/
|
||||
public void add(int value) {
|
||||
if (size > (values.length * 3) >> 2) {
|
||||
expand();
|
||||
}
|
||||
/**
|
||||
* Adds the specified value to this HashSet. If this value is already in
|
||||
* this HashSet, nothing happens.
|
||||
*
|
||||
* @param value value to be added to the HashSet
|
||||
*/
|
||||
public void add(int value) {
|
||||
if (size > (values.length * 3) >> 2) {
|
||||
expand();
|
||||
}
|
||||
|
||||
int index = value & mask;
|
||||
int index = value & mask;
|
||||
|
||||
if (values[index] == Integer.MIN_VALUE) {
|
||||
values[index] = value;
|
||||
} else {
|
||||
if (values[index] == value) {
|
||||
return;
|
||||
}
|
||||
while (next[index] != 0) {
|
||||
index = next[index] - 1;
|
||||
if (values[index] == value) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
int ref = index;
|
||||
while (values[index] != Integer.MIN_VALUE) {
|
||||
index++;
|
||||
if (index == values.length) index = 0;
|
||||
}
|
||||
next[ref] = index + 1;
|
||||
if (values[index] == Integer.MIN_VALUE) {
|
||||
values[index] = value;
|
||||
} else {
|
||||
if (values[index] == value) {
|
||||
return;
|
||||
}
|
||||
while (next[index] != 0) {
|
||||
index = next[index] - 1;
|
||||
if (values[index] == value) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
int ref = index;
|
||||
while (values[index] != Integer.MIN_VALUE) {
|
||||
index++;
|
||||
if (index == values.length)
|
||||
index = 0;
|
||||
}
|
||||
next[ref] = index + 1;
|
||||
|
||||
values[index] = value;
|
||||
}
|
||||
values[index] = value;
|
||||
}
|
||||
|
||||
size++;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this HashSet contains the specified value.
|
||||
*
|
||||
* @param value integer value to add
|
||||
* @return true if this HashSet contains the specified value
|
||||
*/
|
||||
public boolean contains(int value) {
|
||||
int index = value & mask;
|
||||
while (values[index] != value) {
|
||||
if (next[index] == 0) {
|
||||
return false;
|
||||
}
|
||||
index = next[index] - 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Checks if this HashSet contains the specified value.
|
||||
*
|
||||
* @param value integer value to add
|
||||
* @return true if this HashSet contains the specified value
|
||||
*/
|
||||
public boolean contains(int value) {
|
||||
int index = value & mask;
|
||||
while (values[index] != value) {
|
||||
if (next[index] == 0) {
|
||||
return false;
|
||||
}
|
||||
index = next[index] - 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an iterator over this HashSet. The elements are returned in no
|
||||
* particular order.
|
||||
*
|
||||
* @return an iterator over this HashSet
|
||||
*/
|
||||
public IteratorI iterator() {
|
||||
return new KeyIterator();
|
||||
}
|
||||
/**
|
||||
* Returns an iterator over this HashSet. The elements are returned in no
|
||||
* particular order.
|
||||
*
|
||||
* @return an iterator over this HashSet
|
||||
*/
|
||||
public IteratorI iterator() {
|
||||
return new KeyIterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the contents of this HashSet to an integer array. The order of
|
||||
* the elements is unspecified.
|
||||
*
|
||||
* @return this HashSet as integer array
|
||||
*/
|
||||
public int[] toArray() {
|
||||
int[] result = new int[size];
|
||||
int ix = 0;
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
if (values[i] != Integer.MIN_VALUE) {
|
||||
result[ix++] = values[i];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* Converts the contents of this HashSet to an integer array. The order of
|
||||
* the elements is unspecified.
|
||||
*
|
||||
* @return this HashSet as integer array
|
||||
*/
|
||||
public int[] toArray() {
|
||||
int[] result = new int[size];
|
||||
int ix = 0;
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
if (values[i] != Integer.MIN_VALUE) {
|
||||
result[ix++] = values[i];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
// Private inner methods
|
||||
//////////////////////////
|
||||
// ////////////////////////
|
||||
// Private inner methods
|
||||
// ////////////////////////
|
||||
|
||||
/**
|
||||
* Expands the capacity of this HashSet. (double it)
|
||||
*/
|
||||
private void expand() {
|
||||
int[] newKeys = new int[values.length * 2];
|
||||
int[] newNext = new int[next.length * 2];
|
||||
int newMask = newKeys.length - 1;
|
||||
/**
|
||||
* Expands the capacity of this HashSet. (double it)
|
||||
*/
|
||||
private void expand() {
|
||||
int[] newKeys = new int[values.length * 2];
|
||||
int[] newNext = new int[next.length * 2];
|
||||
int newMask = newKeys.length - 1;
|
||||
|
||||
for (int i = 0; i < newKeys.length; i++) {
|
||||
newKeys[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
for (int i = 0; i < newKeys.length; i++) {
|
||||
newKeys[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
if (values[i] == Integer.MIN_VALUE) {
|
||||
continue;
|
||||
}
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
if (values[i] == Integer.MIN_VALUE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int key = values[i];
|
||||
int index = key & newMask;
|
||||
int key = values[i];
|
||||
int index = key & newMask;
|
||||
|
||||
if (newKeys[index] == Integer.MIN_VALUE) {
|
||||
newKeys[index] = key;
|
||||
} else {
|
||||
while (newNext[index] != 0) {
|
||||
index = newNext[index] - 1;
|
||||
}
|
||||
int ref = index;
|
||||
while (newKeys[index] != Integer.MIN_VALUE) {
|
||||
index = (index + 1) & newMask;
|
||||
}
|
||||
newNext[ref] = index + 1;
|
||||
if (newKeys[index] == Integer.MIN_VALUE) {
|
||||
newKeys[index] = key;
|
||||
} else {
|
||||
while (newNext[index] != 0) {
|
||||
index = newNext[index] - 1;
|
||||
}
|
||||
int ref = index;
|
||||
while (newKeys[index] != Integer.MIN_VALUE) {
|
||||
index = (index + 1) & newMask;
|
||||
}
|
||||
newNext[ref] = index + 1;
|
||||
|
||||
newKeys[index] = key;
|
||||
}
|
||||
}
|
||||
newKeys[index] = key;
|
||||
}
|
||||
}
|
||||
|
||||
values = newKeys;
|
||||
next = newNext;
|
||||
mask = newMask;
|
||||
}
|
||||
values = newKeys;
|
||||
next = newNext;
|
||||
mask = newMask;
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
// Private inner classes
|
||||
//////////////////////////
|
||||
// ////////////////////////
|
||||
// Private inner classes
|
||||
// ////////////////////////
|
||||
|
||||
private class KeyIterator implements IteratorI {
|
||||
private int i;
|
||||
private class KeyIterator implements IteratorI {
|
||||
private int i;
|
||||
|
||||
public KeyIterator() {
|
||||
i = 0;
|
||||
skip();
|
||||
}
|
||||
public KeyIterator() {
|
||||
i = 0;
|
||||
skip();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return i < values.length;
|
||||
}
|
||||
public boolean hasNext() {
|
||||
return i < values.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int next() {
|
||||
int result = values[i++];
|
||||
skip();
|
||||
return result;
|
||||
}
|
||||
public int next() {
|
||||
int result = values[i++];
|
||||
skip();
|
||||
return result;
|
||||
}
|
||||
|
||||
private void skip() {
|
||||
while (i < values.length && values[i] == Integer.MIN_VALUE) i++;
|
||||
}
|
||||
}
|
||||
private void skip() {
|
||||
while (i < values.length && values[i] == Integer.MIN_VALUE)
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
@@ -11,17 +11,17 @@ package stanhebben.zenscript.parser;
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public interface IteratorI {
|
||||
/**
|
||||
* Returns true if the iteration has more elements.
|
||||
*
|
||||
* @return true if the iteration has more elements
|
||||
*/
|
||||
public boolean hasNext();
|
||||
/**
|
||||
* Returns true if the iteration has more elements.
|
||||
*
|
||||
* @return true if the iteration has more elements
|
||||
*/
|
||||
public boolean hasNext();
|
||||
|
||||
/**
|
||||
* Returns the next element in this iteration.
|
||||
*
|
||||
* @return the next element in this iteration
|
||||
*/
|
||||
public int next();
|
||||
/**
|
||||
* Returns the next element in this iteration.
|
||||
*
|
||||
* @return the next element in this iteration
|
||||
*/
|
||||
public int next();
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -15,43 +15,43 @@ public class ParseException extends RuntimeException {
|
||||
private final ZenParsedFile file;
|
||||
private final int line;
|
||||
private final int lineOffset;
|
||||
|
||||
|
||||
private final Token token;
|
||||
private final String message;
|
||||
|
||||
public ParseException(Token token, String error) {
|
||||
super("Error parsing line " + token.getPosition().getLine() + ":" + token.getPosition().getLineOffset() + " - " + error + " (last token: " + token.getValue() + ")");
|
||||
|
||||
|
||||
public ParseException(Token token, String error) {
|
||||
super("Error parsing line " + token.getPosition().getLine() + ":" + token.getPosition().getLineOffset() + " - " + error + " (last token: " + token.getValue() + ")");
|
||||
|
||||
this.file = token.getPosition().getFile();
|
||||
this.line = token.getPosition().getLine();
|
||||
this.lineOffset = token.getPosition().getLineOffset();
|
||||
|
||||
this.token = token;
|
||||
this.message = error;
|
||||
}
|
||||
|
||||
|
||||
this.token = token;
|
||||
this.message = error;
|
||||
}
|
||||
|
||||
public ParseException(ZenParsedFile file, int line, int lineOffset, String error) {
|
||||
this.file = file;
|
||||
this.line = line;
|
||||
this.lineOffset = lineOffset;
|
||||
|
||||
|
||||
token = null;
|
||||
message = error;
|
||||
}
|
||||
|
||||
public ZenParsedFile getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public int getLine() {
|
||||
return line;
|
||||
}
|
||||
|
||||
public int getLineOffset() {
|
||||
return lineOffset;
|
||||
}
|
||||
|
||||
public String getExplanation() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public ZenParsedFile getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public int getLine() {
|
||||
return line;
|
||||
}
|
||||
|
||||
public int getLineOffset() {
|
||||
return lineOffset;
|
||||
}
|
||||
|
||||
public String getExplanation() {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
@@ -14,46 +14,46 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
*/
|
||||
public class Token {
|
||||
private final ZenPosition position;
|
||||
private final String value;
|
||||
private final int type;
|
||||
private final String value;
|
||||
private final int type;
|
||||
|
||||
/**
|
||||
* Constructs a new token.
|
||||
*
|
||||
* @param value token string value
|
||||
* @param type token type
|
||||
/**
|
||||
* Constructs a new token.
|
||||
*
|
||||
* @param value token string value
|
||||
* @param type token type
|
||||
* @param position token position
|
||||
*/
|
||||
public Token(String value, int type, ZenPosition position) {
|
||||
this.value = value;
|
||||
this.type = type;
|
||||
*/
|
||||
public Token(String value, int type, ZenPosition position) {
|
||||
this.value = value;
|
||||
this.type = type;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public ZenPosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the string value of this token.
|
||||
*
|
||||
* @return token value
|
||||
*/
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
public ZenPosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the token type of this token.
|
||||
*
|
||||
* @return token type
|
||||
*/
|
||||
public int getType() {
|
||||
return type;
|
||||
}
|
||||
/**
|
||||
* Returns the string value of this token.
|
||||
*
|
||||
* @return token value
|
||||
*/
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return position.getLine() + ":" + position.getLineOffset() + " (" + type + ") " + value;
|
||||
}
|
||||
/**
|
||||
* Returns the token type of this token.
|
||||
*
|
||||
* @return token type
|
||||
*/
|
||||
public int getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return position.getLine() + ":" + position.getLineOffset() + " (" + type + ") " + value;
|
||||
}
|
||||
}
|
||||
@@ -13,9 +13,10 @@ import stanhebben.zenscript.ZenParsedFile;
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class TokenException extends RuntimeException {
|
||||
public TokenException(ZenParsedFile file, int line, int lineOffset, char value) {
|
||||
super("Invalid character at " + file + ":" + line + " - " + value);
|
||||
|
||||
if (line < 0) throw new IllegalArgumentException("Line cannot be negative");
|
||||
}
|
||||
public TokenException(ZenParsedFile file, int line, int lineOffset, char value) {
|
||||
super("Invalid character at " + file + ":" + line + " - " + value);
|
||||
|
||||
if (line < 0)
|
||||
throw new IllegalArgumentException("Line cannot be negative");
|
||||
}
|
||||
}
|
||||
@@ -24,251 +24,252 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
*/
|
||||
public class TokenStream implements Iterator<Token> {
|
||||
private ZenParsedFile file;
|
||||
|
||||
private CountingReader reader;
|
||||
private CompiledDFA dfa;
|
||||
private Token next;
|
||||
private int nextChar;
|
||||
private int line;
|
||||
private int lineOffset;
|
||||
|
||||
private int tokenMemoryOffset;
|
||||
private LinkedList<Token> tokenMemory;
|
||||
private Stack<Integer> marks;
|
||||
private int tokenMemoryCurrent;
|
||||
|
||||
/**
|
||||
* Creates a token stream using the specified reader and DFA.
|
||||
*
|
||||
* @param reader reader to read characters from
|
||||
* @param dfa DFA to tokenize the stream
|
||||
* @throws IOException
|
||||
*/
|
||||
public TokenStream(Reader reader, CompiledDFA dfa) throws IOException {
|
||||
tokenMemoryOffset = 0;
|
||||
tokenMemoryCurrent = 0;
|
||||
tokenMemory = new LinkedList<Token>();
|
||||
marks = new Stack<Integer>();
|
||||
|
||||
this.reader = new CountingReader(reader);
|
||||
this.dfa = dfa;
|
||||
nextChar = this.reader.read();
|
||||
line = 1;
|
||||
lineOffset = 1;
|
||||
advance();
|
||||
}
|
||||
private CountingReader reader;
|
||||
private CompiledDFA dfa;
|
||||
private Token next;
|
||||
private int nextChar;
|
||||
private int line;
|
||||
private int lineOffset;
|
||||
|
||||
private int tokenMemoryOffset;
|
||||
private LinkedList<Token> tokenMemory;
|
||||
private Stack<Integer> marks;
|
||||
private int tokenMemoryCurrent;
|
||||
|
||||
/**
|
||||
* Creates a token stream using the specified reader and DFA.
|
||||
*
|
||||
* @param reader reader to read characters from
|
||||
* @param dfa DFA to tokenize the stream
|
||||
* @throws IOException
|
||||
*/
|
||||
public TokenStream(Reader reader, CompiledDFA dfa) throws IOException {
|
||||
tokenMemoryOffset = 0;
|
||||
tokenMemoryCurrent = 0;
|
||||
tokenMemory = new LinkedList<Token>();
|
||||
marks = new Stack<Integer>();
|
||||
|
||||
this.reader = new CountingReader(reader);
|
||||
this.dfa = dfa;
|
||||
nextChar = this.reader.read();
|
||||
line = 1;
|
||||
lineOffset = 1;
|
||||
advance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a token stream which reads data from the specified string.
|
||||
*
|
||||
* @param data data to read
|
||||
* @param dfa DFA to tokenize the stream
|
||||
* @throws IOException
|
||||
*/
|
||||
public TokenStream(String data, CompiledDFA dfa) throws IOException {
|
||||
this(new StringReader(data), dfa);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a token stream which reads data from the specified string.
|
||||
*
|
||||
* @param data data to read
|
||||
* @param dfa DFA to tokenize the stream
|
||||
* @throws IOException
|
||||
*/
|
||||
public TokenStream(String data, CompiledDFA dfa) throws IOException {
|
||||
this(new StringReader(data), dfa);
|
||||
}
|
||||
|
||||
public void setFile(ZenParsedFile file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
|
||||
public ZenParsedFile getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
|
||||
public int getLine() {
|
||||
return line;
|
||||
}
|
||||
|
||||
|
||||
public int getLineOffset() {
|
||||
return lineOffset;
|
||||
}
|
||||
|
||||
public Token peek() {
|
||||
if (tokenMemoryCurrent < tokenMemoryOffset + tokenMemory.size()) {
|
||||
return tokenMemory.get((tokenMemoryCurrent) - tokenMemoryOffset);
|
||||
} else {
|
||||
return next;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isNext(int type) {
|
||||
return peek().getType() == type;
|
||||
}
|
||||
public Token peek() {
|
||||
if (tokenMemoryCurrent < tokenMemoryOffset + tokenMemory.size()) {
|
||||
return tokenMemory.get((tokenMemoryCurrent) - tokenMemoryOffset);
|
||||
} else {
|
||||
return next;
|
||||
}
|
||||
}
|
||||
|
||||
public Token optional(int type) {
|
||||
if (peek() != null && peek().getType() == type) {
|
||||
return next();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public boolean isNext(int type) {
|
||||
return peek().getType() == type;
|
||||
}
|
||||
|
||||
public Token required(int type, String error) {
|
||||
public Token optional(int type) {
|
||||
if (peek() != null && peek().getType() == type) {
|
||||
return next();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Token required(int type, String error) {
|
||||
Token t = peek();
|
||||
if (t != null && t.getType() == type) {
|
||||
return next();
|
||||
} else {
|
||||
throw new ParseException(file, line, lineOffset, error);
|
||||
}
|
||||
}
|
||||
if (t != null && t.getType() == type) {
|
||||
return next();
|
||||
} else {
|
||||
throw new ParseException(file, line, lineOffset, error);
|
||||
}
|
||||
}
|
||||
|
||||
// LL(*) ability
|
||||
// LL(*) ability
|
||||
|
||||
/**
|
||||
* Pushes a mark on the mark stack.
|
||||
*/
|
||||
public void pushMark() {
|
||||
marks.push(tokenMemoryCurrent);
|
||||
}
|
||||
/**
|
||||
* Pushes a mark on the mark stack.
|
||||
*/
|
||||
public void pushMark() {
|
||||
marks.push(tokenMemoryCurrent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pops a mark from the mark stack without reset.
|
||||
*/
|
||||
public void popMark() {
|
||||
marks.pop();
|
||||
/**
|
||||
* Pops a mark from the mark stack without reset.
|
||||
*/
|
||||
public void popMark() {
|
||||
marks.pop();
|
||||
|
||||
if (marks.isEmpty()) {
|
||||
tokenMemoryOffset = tokenMemoryCurrent;
|
||||
tokenMemory.clear();
|
||||
}
|
||||
}
|
||||
if (marks.isEmpty()) {
|
||||
tokenMemoryOffset = tokenMemoryCurrent;
|
||||
tokenMemory.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pops a mark from the mark stack and resets the stream's position to it
|
||||
*/
|
||||
public void reset() {
|
||||
tokenMemoryCurrent = marks.pop();
|
||||
}
|
||||
/**
|
||||
* Pops a mark from the mark stack and resets the stream's position to it
|
||||
*/
|
||||
public void reset() {
|
||||
tokenMemoryCurrent = marks.pop();
|
||||
}
|
||||
|
||||
// //////////////////////////
|
||||
// Iterator implementation
|
||||
// //////////////////////////
|
||||
|
||||
////////////////////////////
|
||||
// Iterator implementation
|
||||
////////////////////////////
|
||||
public boolean hasNext() {
|
||||
return next != null;
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return next != null;
|
||||
}
|
||||
public Token next() {
|
||||
if (tokenMemoryCurrent < tokenMemoryOffset + tokenMemory.size()) {
|
||||
return tokenMemory.get((tokenMemoryCurrent++) - tokenMemoryOffset);
|
||||
} else {
|
||||
Token result = next;
|
||||
|
||||
public Token next() {
|
||||
if (tokenMemoryCurrent < tokenMemoryOffset + tokenMemory.size()) {
|
||||
return tokenMemory.get((tokenMemoryCurrent++) - tokenMemoryOffset);
|
||||
} else {
|
||||
Token result = next;
|
||||
if (marks.isEmpty()) {
|
||||
tokenMemoryOffset++;
|
||||
} else {
|
||||
tokenMemory.add(result);
|
||||
}
|
||||
tokenMemoryCurrent++;
|
||||
|
||||
if (marks.isEmpty()) {
|
||||
tokenMemoryOffset++;
|
||||
} else {
|
||||
tokenMemory.add(result);
|
||||
}
|
||||
tokenMemoryCurrent++;
|
||||
advance();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
advance();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException("Not supported.");
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException("Not supported.");
|
||||
}
|
||||
// ////////////////////
|
||||
// Protected methods
|
||||
// ////////////////////
|
||||
|
||||
//////////////////////
|
||||
// Protected methods
|
||||
//////////////////////
|
||||
/**
|
||||
* Can be implemented by subclasses to post-process tokens.
|
||||
*
|
||||
* @param token
|
||||
* @return processed token
|
||||
*/
|
||||
protected Token process(Token token) {
|
||||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Can be implemented by subclasses to post-process tokens.
|
||||
*
|
||||
* @param token
|
||||
* @return processed token
|
||||
*/
|
||||
protected Token process(Token token) {
|
||||
return token;
|
||||
}
|
||||
// //////////////////
|
||||
// Private methods
|
||||
// //////////////////
|
||||
|
||||
////////////////////
|
||||
// Private methods
|
||||
////////////////////
|
||||
/**
|
||||
* Advances to the next non - whitespace token.
|
||||
*/
|
||||
private void advance() {
|
||||
do {
|
||||
advanceToken();
|
||||
} while (next != null && next.getType() < 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Advances to the next non - whitespace token.
|
||||
*/
|
||||
private void advance() {
|
||||
do {
|
||||
advanceToken();
|
||||
} while (next != null && next.getType() < 0);
|
||||
}
|
||||
/**
|
||||
* Advances to the next token.
|
||||
*/
|
||||
private void advanceToken() {
|
||||
if (nextChar < 0) {
|
||||
next = null;
|
||||
return;
|
||||
}
|
||||
try {
|
||||
int state = 0;
|
||||
StringBuilder value = new StringBuilder();
|
||||
int tLine = line;
|
||||
int tLineOffset = lineOffset;
|
||||
while (dfa.transitions[state].containsKey(nextChar)) {
|
||||
value.append((char) nextChar);
|
||||
state = dfa.transitions[state].get(nextChar);
|
||||
line = reader.line;
|
||||
lineOffset = reader.lineOffset;
|
||||
nextChar = reader.read();
|
||||
}
|
||||
|
||||
/**
|
||||
* Advances to the next token.
|
||||
*/
|
||||
private void advanceToken() {
|
||||
if (nextChar < 0) {
|
||||
next = null;
|
||||
return;
|
||||
}
|
||||
try {
|
||||
int state = 0;
|
||||
StringBuilder value = new StringBuilder();
|
||||
int tLine = line;
|
||||
int tLineOffset = lineOffset;
|
||||
while (dfa.transitions[state].containsKey(nextChar)) {
|
||||
value.append((char)nextChar);
|
||||
state = dfa.transitions[state].get(nextChar);
|
||||
line = reader.line;
|
||||
lineOffset = reader.lineOffset;
|
||||
nextChar = reader.read();
|
||||
}
|
||||
|
||||
if (line < 0) throw new IllegalStateException("Line cannot be negative");
|
||||
|
||||
if (dfa.finals[state] != CompiledDFA.NOFINAL) {
|
||||
if (state == 0) throw new TokenException(file, line, lineOffset, (char)nextChar);
|
||||
next = process(new Token(value.toString(), dfa.finals[state], new ZenPosition(file, tLine, tLineOffset)));
|
||||
} else {
|
||||
if (line < 0)
|
||||
throw new IllegalStateException("Line cannot be negative");
|
||||
|
||||
if (dfa.finals[state] != CompiledDFA.NOFINAL) {
|
||||
if (state == 0)
|
||||
throw new TokenException(file, line, lineOffset, (char) nextChar);
|
||||
next = process(new Token(value.toString(), dfa.finals[state], new ZenPosition(file, tLine, tLineOffset)));
|
||||
} else {
|
||||
if (nextChar < 0 && value.length() == 0) {
|
||||
return; // happens on comments at the end of files
|
||||
}
|
||||
throw new TokenException(file, line, lineOffset, (char)nextChar);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
throw new TokenException(file, line, lineOffset, (char) nextChar);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
// Private inner classes
|
||||
//////////////////////////
|
||||
// ////////////////////////
|
||||
// Private inner classes
|
||||
// ////////////////////////
|
||||
|
||||
/**
|
||||
* Keeps a line and line offset count.
|
||||
*/
|
||||
private class CountingReader {
|
||||
private int line;
|
||||
private int lineOffset;
|
||||
private Reader reader;
|
||||
private boolean eof;
|
||||
/**
|
||||
* Keeps a line and line offset count.
|
||||
*/
|
||||
private class CountingReader {
|
||||
private int line;
|
||||
private int lineOffset;
|
||||
private Reader reader;
|
||||
private boolean eof;
|
||||
|
||||
public CountingReader(Reader reader) {
|
||||
this.reader = reader;
|
||||
line = 1;
|
||||
lineOffset = 1;
|
||||
}
|
||||
public CountingReader(Reader reader) {
|
||||
this.reader = reader;
|
||||
line = 1;
|
||||
lineOffset = 1;
|
||||
}
|
||||
|
||||
public int read() throws IOException {
|
||||
int ch = reader.read();
|
||||
if (ch == -1) {
|
||||
eof = true;
|
||||
return ch;
|
||||
}
|
||||
if (ch == '\n') {
|
||||
line++;
|
||||
lineOffset = 1;
|
||||
} else {
|
||||
lineOffset++;
|
||||
}
|
||||
return ch;
|
||||
}
|
||||
}
|
||||
public int read() throws IOException {
|
||||
int ch = reader.read();
|
||||
if (ch == -1) {
|
||||
eof = true;
|
||||
return ch;
|
||||
}
|
||||
if (ch == '\n') {
|
||||
line++;
|
||||
lineOffset = 1;
|
||||
} else {
|
||||
lineOffset++;
|
||||
}
|
||||
return ch;
|
||||
}
|
||||
}
|
||||
}
|
||||
+58
-57
@@ -39,7 +39,7 @@ public abstract class ParsedExpression {
|
||||
public static ParsedExpression read(ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
return readAssignExpression(parser, environment);
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readAssignExpression(ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
Token token = parser.peek();
|
||||
if (token == null) {
|
||||
@@ -47,17 +47,17 @@ public abstract class ParsedExpression {
|
||||
environment.error(position, "unexpected end of file; expression expected");
|
||||
return new ParsedExpressionInvalid(position);
|
||||
}
|
||||
|
||||
|
||||
ZenPosition position = token.getPosition();
|
||||
|
||||
|
||||
ParsedExpression left = readConditionalExpression(position, parser, environment);
|
||||
|
||||
|
||||
if (parser.peek() == null) {
|
||||
ZenPosition position2 = new ZenPosition(parser.getFile(), parser.getLine(), parser.getLineOffset());
|
||||
environment.error(position2, "unexpected end of file - ; expected");
|
||||
return new ParsedExpressionInvalid(position2);
|
||||
}
|
||||
|
||||
|
||||
switch (parser.peek().getType()) {
|
||||
case T_ASSIGN:
|
||||
parser.next();
|
||||
@@ -90,76 +90,76 @@ public abstract class ParsedExpression {
|
||||
parser.next();
|
||||
return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, environment), OperatorType.XOR);
|
||||
}
|
||||
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readConditionalExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
ParsedExpression left = readOrOrExpression(position, parser, environment);
|
||||
|
||||
|
||||
if (parser.optional(T_QUEST) != null) {
|
||||
ParsedExpression onIf = readOrOrExpression(parser.peek().getPosition(), parser, environment);
|
||||
parser.required(T_COLON, ": expected");
|
||||
ParsedExpression onElse = readConditionalExpression(parser.peek().getPosition(), parser, environment);
|
||||
return new ParsedExpressionConditional(position, left, onIf, onElse);
|
||||
}
|
||||
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readOrOrExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
ParsedExpression left = readAndAndExpression(position, parser, environment);
|
||||
|
||||
|
||||
while (parser.optional(T_OR2) != null) {
|
||||
ParsedExpression right = readAndAndExpression(parser.peek().getPosition(), parser, environment);
|
||||
left = new ParsedExpressionOrOr(position, left, right);
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readAndAndExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
ParsedExpression left = readOrExpression(position, parser, environment);
|
||||
|
||||
|
||||
while (parser.optional(T_AND2) != null) {
|
||||
ParsedExpression right = readOrExpression(parser.peek().getPosition(), parser, environment);
|
||||
left = new ParsedExpressionAndAnd(position, left, right);
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readOrExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
ParsedExpression left = readXorExpression(position, parser, environment);
|
||||
|
||||
|
||||
while (parser.optional(T_OR) != null) {
|
||||
ParsedExpression right = readXorExpression(parser.peek().getPosition(), parser, environment);
|
||||
left = new ParsedExpressionBinary(position, left, right, OperatorType.OR);
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readXorExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
ParsedExpression left = readAndExpression(position, parser, environment);
|
||||
|
||||
|
||||
while (parser.optional(T_XOR) != null) {
|
||||
ParsedExpression right = readAndExpression(parser.peek().getPosition(), parser, environment);
|
||||
left = new ParsedExpressionBinary(position, left, right, OperatorType.XOR);
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readAndExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
ParsedExpression left = readCompareExpression(position, parser, environment);
|
||||
|
||||
|
||||
while (parser.optional(T_AND) != null) {
|
||||
ParsedExpression right = readCompareExpression(parser.peek().getPosition(), parser, environment);
|
||||
left = new ParsedExpressionBinary(position, left, right, OperatorType.AND);
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readCompareExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
ParsedExpression left = readAddExpression(position, parser, environment);
|
||||
|
||||
|
||||
switch (parser.peek() == null ? -1 : parser.peek().getType()) {
|
||||
case T_EQ: {
|
||||
parser.next();
|
||||
@@ -197,13 +197,13 @@ public abstract class ParsedExpression {
|
||||
return new ParsedExpressionBinary(position, left, right, OperatorType.CONTAINS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readAddExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
ParsedExpression left = readMulExpression(position, parser, environment);
|
||||
|
||||
|
||||
while (true) {
|
||||
if (parser.optional(T_PLUS) != null) {
|
||||
ParsedExpression right = readMulExpression(parser.peek().getPosition(), parser, environment);
|
||||
@@ -220,10 +220,10 @@ public abstract class ParsedExpression {
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readMulExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
ParsedExpression left = readUnaryExpression(position, parser, environment);
|
||||
|
||||
|
||||
while (true) {
|
||||
if (parser.optional(T_MUL) != null) {
|
||||
ParsedExpression right = readUnaryExpression(parser.peek().getPosition(), parser, environment);
|
||||
@@ -238,10 +238,10 @@ public abstract class ParsedExpression {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readUnaryExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
switch (parser.peek().getType()) {
|
||||
case T_NOT:
|
||||
@@ -260,10 +260,10 @@ public abstract class ParsedExpression {
|
||||
return readPostfixExpression(position, parser, environment);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readPostfixExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
ParsedExpression base = readPrimaryExpression(position, parser, environment);
|
||||
|
||||
|
||||
while (true) {
|
||||
if (parser.optional(T_DOT) != null) {
|
||||
Token indexString = parser.optional(T_ID);
|
||||
@@ -279,8 +279,8 @@ public abstract class ParsedExpression {
|
||||
}
|
||||
}
|
||||
} else if (parser.optional(T_DOT2) != null) {
|
||||
//ParsedExpression to = readAssignExpression(parser, errors);
|
||||
//base = base.range(position, errors, to);
|
||||
// ParsedExpression to = readAssignExpression(parser, errors);
|
||||
// base = base.range(position, errors, to);
|
||||
throw new RuntimeException("not yet supported");
|
||||
} else if (parser.optional(T_SQBROPEN) != null) {
|
||||
ParsedExpression index = readAssignExpression(parser, environment);
|
||||
@@ -303,10 +303,10 @@ public abstract class ParsedExpression {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readPrimaryExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
switch (parser.peek().getType()) {
|
||||
case T_INTVALUE:
|
||||
@@ -321,20 +321,19 @@ public abstract class ParsedExpression {
|
||||
return new ParsedExpressionValue(
|
||||
position,
|
||||
new ExpressionString(position, unescapeString(parser.next().getValue())));
|
||||
/*case T_DOLLAR: {
|
||||
Expression result = new ExpressionDollar(position);
|
||||
if (parser.isNext(T_STRINGVALUE)) {
|
||||
return new ExpressionIndexString(position, result, unescapeString(parser.next().getValue()));
|
||||
}
|
||||
return result;
|
||||
}*/
|
||||
/*
|
||||
* case T_DOLLAR: { Expression result = new
|
||||
* ExpressionDollar(position); if (parser.isNext(T_STRINGVALUE))
|
||||
* { return new ExpressionIndexString(position, result,
|
||||
* unescapeString(parser.next().getValue())); } return result; }
|
||||
*/
|
||||
case T_ID:
|
||||
return new ParsedExpressionVariable(position, parser.next().getValue());
|
||||
case T_FUNCTION:
|
||||
// function (argname, argname, ...) { ...contents... }
|
||||
parser.next();
|
||||
parser.required(T_BROPEN, "( expected");
|
||||
|
||||
|
||||
List<ParsedFunctionArgument> arguments = new ArrayList<ParsedFunctionArgument>();
|
||||
if (parser.optional(T_BRCLOSE) == null) {
|
||||
do {
|
||||
@@ -344,27 +343,27 @@ public abstract class ParsedExpression {
|
||||
if (parser.optional(T_AS) != null) {
|
||||
type = ZenType.read(parser, environment);
|
||||
}
|
||||
|
||||
|
||||
arguments.add(new ParsedFunctionArgument(name, type));
|
||||
} while (parser.optional(T_COMMA) != null);
|
||||
|
||||
|
||||
parser.required(T_BRCLOSE, ") expected");
|
||||
}
|
||||
|
||||
|
||||
ZenType returnType = ZenTypeAny.INSTANCE;
|
||||
if (parser.optional(T_AS) != null) {
|
||||
returnType = ZenType.read(parser, environment);
|
||||
}
|
||||
|
||||
|
||||
parser.required(T_AOPEN, "{ expected");
|
||||
|
||||
|
||||
List<Statement> statements = new ArrayList<Statement>();
|
||||
if (parser.optional(T_ACLOSE) == null) {
|
||||
while (parser.optional(T_ACLOSE) == null) {
|
||||
statements.add(Statement.read(parser, environment, returnType));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return new ParsedExpressionFunction(position, returnType, arguments, statements);
|
||||
case T_LT: {
|
||||
Token start = parser.next();
|
||||
@@ -380,12 +379,13 @@ public abstract class ParsedExpression {
|
||||
builder.append('<');
|
||||
Token last = null;
|
||||
for (Token token : tokens) {
|
||||
if (last != null) builder.append(' ');
|
||||
if (last != null)
|
||||
builder.append(' ');
|
||||
builder.append(token.getValue());
|
||||
last = token;
|
||||
}
|
||||
builder.append('>');
|
||||
|
||||
|
||||
parser.getEnvironment().getErrorLogger().error(start.getPosition(), "Could not resolve " + builder.toString());
|
||||
return new ParsedExpressionInvalid(start.getPosition());
|
||||
} else {
|
||||
@@ -410,7 +410,7 @@ public abstract class ParsedExpression {
|
||||
}
|
||||
case T_AOPEN: {
|
||||
parser.next();
|
||||
|
||||
|
||||
List<ParsedExpression> keys = new ArrayList<ParsedExpression>();
|
||||
List<ParsedExpression> values = new ArrayList<ParsedExpression>();
|
||||
if (parser.optional(T_ACLOSE) == null) {
|
||||
@@ -418,13 +418,13 @@ public abstract class ParsedExpression {
|
||||
keys.add(readAssignExpression(parser, environment));
|
||||
parser.required(T_COLON, ": expected");
|
||||
values.add(readAssignExpression(parser, environment));
|
||||
|
||||
|
||||
if (parser.optional(T_COMMA) == null) {
|
||||
parser.required(T_ACLOSE, "} or , expected");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return new ParsedExpressionMap(position, keys, values);
|
||||
}
|
||||
}
|
||||
@@ -447,18 +447,19 @@ public abstract class ParsedExpression {
|
||||
throw new ParseException(last, "Invalid expression, last token: " + last.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
private final ZenPosition position;
|
||||
|
||||
|
||||
public ParsedExpression(ZenPosition position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
|
||||
public ZenPosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
|
||||
public abstract IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType);
|
||||
|
||||
|
||||
public Expression compileKey(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
return compile(environment, predictedType).eval(environment);
|
||||
}
|
||||
|
||||
+2
-2
@@ -19,10 +19,10 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ParsedExpressionAndAnd extends ParsedExpression {
|
||||
private final ParsedExpression left;
|
||||
private final ParsedExpression right;
|
||||
|
||||
|
||||
public ParsedExpressionAndAnd(ZenPosition position, ParsedExpression left, ParsedExpression right) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
+5
-5
@@ -25,10 +25,10 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
*/
|
||||
public class ParsedExpressionArray extends ParsedExpression {
|
||||
private final List<ParsedExpression> contents;
|
||||
|
||||
|
||||
public ParsedExpressionArray(ZenPosition position, List<ParsedExpression> contents) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.contents = contents;
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ public class ParsedExpressionArray extends ParsedExpression {
|
||||
ZenType predictedBaseType = null;
|
||||
ZenTypeArrayBasic arrayType = ZenType.ANYARRAY;
|
||||
ICastingRule castingRule = null;
|
||||
|
||||
|
||||
if (predictedType instanceof ZenTypeArray) {
|
||||
predictedBaseType = ((ZenTypeArray) predictedType).getBaseType();
|
||||
if (predictedType instanceof ZenTypeArrayBasic) {
|
||||
@@ -60,7 +60,7 @@ public class ParsedExpressionArray extends ParsedExpression {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Expression[] cContents = new Expression[contents.size()];
|
||||
for (int i = 0; i < contents.size(); i++) {
|
||||
cContents[i] = contents.get(i).compile(environment, predictedBaseType).eval(environment);
|
||||
@@ -72,7 +72,7 @@ public class ParsedExpressionArray extends ParsedExpression {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Expression compileKey(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
if (contents.size() == 1 && contents.get(0) instanceof ParsedExpressionVariable) {
|
||||
|
||||
+5
-5
@@ -18,19 +18,19 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ParsedExpressionAssign extends ParsedExpression {
|
||||
private final ParsedExpression left;
|
||||
private final ParsedExpression right;
|
||||
|
||||
|
||||
public ParsedExpressionAssign(ZenPosition position, ParsedExpression left, ParsedExpression right) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
IPartialExpression cLeft = left.compile(environment, predictedType);
|
||||
return cLeft
|
||||
.assign(getPosition(), environment,
|
||||
right.compile(environment, cLeft.getType()).eval(environment));
|
||||
.assign(getPosition(), environment,
|
||||
right.compile(environment, cLeft.getType()).eval(environment));
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -21,10 +21,10 @@ public class ParsedExpressionBinary extends ParsedExpression {
|
||||
private final ParsedExpression left;
|
||||
private final ParsedExpression right;
|
||||
private final OperatorType operator;
|
||||
|
||||
|
||||
public ParsedExpressionBinary(ZenPosition position, ParsedExpression left, ParsedExpression right, OperatorType operator) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
this.operator = operator;
|
||||
|
||||
+2
-2
@@ -18,10 +18,10 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
*/
|
||||
public class ParsedExpressionBool extends ParsedExpression {
|
||||
private final boolean value;
|
||||
|
||||
|
||||
public ParsedExpressionBool(ZenPosition position, boolean value) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
||||
+5
-5
@@ -20,25 +20,25 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ParsedExpressionCall extends ParsedExpression {
|
||||
private final ParsedExpression receiver;
|
||||
private final List<ParsedExpression> arguments;
|
||||
|
||||
|
||||
public ParsedExpressionCall(ZenPosition position, ParsedExpression receiver, List<ParsedExpression> arguments) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.receiver = receiver;
|
||||
this.arguments = arguments;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
IPartialExpression cReceiver = receiver.compile(environment, null);
|
||||
ZenType[] predictedTypes = cReceiver.predictCallTypes(arguments.size());
|
||||
|
||||
|
||||
Expression[] cArguments = new Expression[arguments.size()];
|
||||
for (int i = 0; i < cArguments.length; i++) {
|
||||
IPartialExpression cArgument = arguments.get(i).compile(environment, predictedTypes[i]);
|
||||
cArguments[i] = cArgument.eval(environment);
|
||||
}
|
||||
|
||||
|
||||
return cReceiver.call(getPosition(), environment, cArguments);
|
||||
}
|
||||
}
|
||||
+5
-5
@@ -18,10 +18,10 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ParsedExpressionCast extends ParsedExpression {
|
||||
private final ParsedExpression value;
|
||||
private final ZenType type;
|
||||
|
||||
|
||||
public ParsedExpressionCast(ZenPosition position, ParsedExpression value, ZenType type) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.value = value;
|
||||
this.type = type;
|
||||
}
|
||||
@@ -29,8 +29,8 @@ public class ParsedExpressionCast extends ParsedExpression {
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
return value
|
||||
.compile(environment, type)
|
||||
.eval(environment)
|
||||
.cast(getPosition(), environment, type);
|
||||
.compile(environment, type)
|
||||
.eval(environment)
|
||||
.cast(getPosition(), environment, type);
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -21,14 +21,14 @@ public class ParsedExpressionCompare extends ParsedExpression {
|
||||
private final ParsedExpression left;
|
||||
private final ParsedExpression right;
|
||||
private final CompareType type;
|
||||
|
||||
|
||||
public ParsedExpressionCompare(
|
||||
ZenPosition position,
|
||||
ParsedExpression left,
|
||||
ParsedExpression right,
|
||||
CompareType type) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
this.type = type;
|
||||
|
||||
+2
-2
@@ -20,10 +20,10 @@ public class ParsedExpressionConditional extends ParsedExpression {
|
||||
private final ParsedExpression condition;
|
||||
private final ParsedExpression ifThen;
|
||||
private final ParsedExpression ifElse;
|
||||
|
||||
|
||||
public ParsedExpressionConditional(ZenPosition position, ParsedExpression condition, ParsedExpression ifThen, ParsedExpression ifElse) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.condition = condition;
|
||||
this.ifThen = ifThen;
|
||||
this.ifElse = ifElse;
|
||||
|
||||
+4
-4
@@ -28,10 +28,10 @@ public class ParsedExpressionFunction extends ParsedExpression {
|
||||
private final ZenType returnType;
|
||||
private final List<ParsedFunctionArgument> arguments;
|
||||
private final List<Statement> statements;
|
||||
|
||||
|
||||
public ParsedExpressionFunction(ZenPosition position, ZenType returnType, List<ParsedFunctionArgument> arguments, List<Statement> statements) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.returnType = returnType;
|
||||
this.arguments = arguments;
|
||||
this.statements = statements;
|
||||
@@ -41,7 +41,7 @@ public class ParsedExpressionFunction extends ParsedExpression {
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
if (predictedType != null && predictedType instanceof ZenTypeNative) {
|
||||
System.out.println("Known predicted function type: " + predictedType);
|
||||
|
||||
|
||||
ZenTypeNative nativeType = (ZenTypeNative) predictedType;
|
||||
Class nativeClass = nativeType.getNativeClass();
|
||||
if (nativeClass.isInterface() && nativeClass.getMethods().length == 1) {
|
||||
@@ -64,7 +64,7 @@ public class ParsedExpressionFunction extends ParsedExpression {
|
||||
return new ExpressionInvalid(getPosition());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return new ExpressionJavaLambda(
|
||||
getPosition(),
|
||||
nativeClass,
|
||||
|
||||
Loaded 100 of 377 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user