mirror of
https://github.com/GTNewHorizons/CraftTweaker.git
synced 2026-11-17 10:55:04 +00:00
Added projects and basic build scripts. Project for scripting engine, API, Main 1.7 version and one for each mod supported in 1.7.
This commit is contained in:
1 parent
219106f392
commit
38b78ce305
281 files changed
+25090
-49
No files matched your search
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript;
|
||||
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public interface IZenErrorLogger {
|
||||
public void error(ZenPosition position, String message);
|
||||
|
||||
public void warning(ZenPosition position, String message);
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.annotations.ZenCaster;
|
||||
import stanhebben.zenscript.annotations.ZenGetter;
|
||||
import stanhebben.zenscript.annotations.ZenMethod;
|
||||
import stanhebben.zenscript.annotations.ZenMethodStatic;
|
||||
import stanhebben.zenscript.annotations.ZenOperator;
|
||||
import stanhebben.zenscript.annotations.ZenSetter;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.ITypeRegistry;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionString;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.expand.ZenExpandCaster;
|
||||
import stanhebben.zenscript.type.expand.ZenExpandMember;
|
||||
import stanhebben.zenscript.type.natives.JavaMethod;
|
||||
import stanhebben.zenscript.type.natives.ZenNativeOperator;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class TypeExpansion {
|
||||
private final Map<String, ZenExpandMember> members;
|
||||
private final Map<String, ZenExpandMember> staticMembers;
|
||||
private final List<ZenExpandCaster> casters;
|
||||
private final List<ZenNativeOperator> trinaryOperators;
|
||||
private final List<ZenNativeOperator> binaryOperators;
|
||||
private final List<ZenNativeOperator> unaryOperators;
|
||||
|
||||
public TypeExpansion() {
|
||||
members = new HashMap<String, ZenExpandMember>();
|
||||
staticMembers = new HashMap<String, ZenExpandMember>();
|
||||
casters = new ArrayList<ZenExpandCaster>();
|
||||
trinaryOperators = new ArrayList<ZenNativeOperator>();
|
||||
binaryOperators = new ArrayList<ZenNativeOperator>();
|
||||
unaryOperators = new ArrayList<ZenNativeOperator>();
|
||||
}
|
||||
|
||||
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);
|
||||
casters.add(new ZenExpandCaster(new JavaMethod(cls, method, types)));
|
||||
} else if (annotation instanceof ZenGetter) {
|
||||
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());
|
||||
}
|
||||
members.get(name).setGetter(new JavaMethod(cls, method, types));
|
||||
} else if (annotation instanceof ZenSetter) {
|
||||
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());
|
||||
}
|
||||
members.get(name).setSetter(new JavaMethod(cls, method, types));
|
||||
} else if (annotation instanceof ZenOperator) {
|
||||
checkStatic(method);
|
||||
ZenOperator operatorAnnotation = (ZenOperator) annotation;
|
||||
switch (operatorAnnotation.value()) {
|
||||
case NEG:
|
||||
case NOT:
|
||||
if (method.getParameterTypes().length != 0) {
|
||||
// TODO: error
|
||||
} else {
|
||||
unaryOperators.add(new ZenNativeOperator(
|
||||
operatorAnnotation.value(),
|
||||
new JavaMethod(cls, method, types)));
|
||||
}
|
||||
break;
|
||||
case ADD:
|
||||
case SUB:
|
||||
case CAT:
|
||||
case MUL:
|
||||
case DIV:
|
||||
case MOD:
|
||||
case AND:
|
||||
case OR:
|
||||
case XOR:
|
||||
case INDEXGET:
|
||||
case RANGE:
|
||||
case CONTAINS:
|
||||
case COMPARE:
|
||||
if (method.getParameterTypes().length != 1) {
|
||||
// TODO: error
|
||||
} else {
|
||||
binaryOperators.add(new ZenNativeOperator(
|
||||
operatorAnnotation.value(),
|
||||
new JavaMethod(cls, method, types)));
|
||||
}
|
||||
break;
|
||||
case INDEXSET:
|
||||
if (method.getParameterTypes().length != 2) {
|
||||
// TODO: error
|
||||
} else {
|
||||
trinaryOperators.add(new ZenNativeOperator(
|
||||
operatorAnnotation.value(),
|
||||
new JavaMethod(cls, method, types)));
|
||||
}
|
||||
break;
|
||||
}
|
||||
} else if (annotation instanceof ZenMethod) {
|
||||
checkStatic(method);
|
||||
ZenMethod methodAnnotation = (ZenMethod) annotation;
|
||||
if (methodAnnotation.value().length() > 0) {
|
||||
methodName = methodAnnotation.value();
|
||||
}
|
||||
if (!members.containsKey(methodName)) {
|
||||
members.put(methodName, new ZenExpandMember());
|
||||
}
|
||||
members.get(methodName).addMethod(new JavaMethod(cls, method, types));
|
||||
} else if (annotation instanceof ZenMethodStatic) {
|
||||
checkStatic(method);
|
||||
ZenMethodStatic methodAnnotation = (ZenMethodStatic) annotation;
|
||||
if (methodAnnotation.value().length() > 0) {
|
||||
methodName = methodAnnotation.value();
|
||||
}
|
||||
if (!staticMembers.containsKey(methodName)) {
|
||||
staticMembers.put(methodName, new ZenExpandMember());
|
||||
}
|
||||
staticMembers.get(methodName).addMethod(new JavaMethod(cls, method, types));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ZenExpandCaster getCaster(ZenType type, IEnvironmentGlobal environment) {
|
||||
for (ZenExpandCaster caster : casters) {
|
||||
if (caster.getTarget().equals(type)) {
|
||||
return caster;
|
||||
}
|
||||
}
|
||||
for (ZenExpandCaster caster : casters) {
|
||||
if (caster.getTarget().canCastImplicit(type, environment)) {
|
||||
return caster;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Expression unary(ZenPosition position, IEnvironmentGlobal environment, Expression value, OperatorType operator) {
|
||||
for (ZenNativeOperator op : unaryOperators) {
|
||||
if (op.getOperator() == operator) {
|
||||
return op.getMethod().callStatic(position, environment, value);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Expression binary(ZenPosition position, IEnvironmentGlobal environment, Expression left, Expression right, OperatorType operator) {
|
||||
for (ZenNativeOperator op : binaryOperators) {
|
||||
if (op.getOperator() == operator) {
|
||||
return op.getMethod().callStatic(position, environment, left, right);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Expression trinary(ZenPosition position, IEnvironmentGlobal environment, Expression first, Expression second, Expression third, OperatorType operator) {
|
||||
for (ZenNativeOperator op : trinaryOperators) {
|
||||
if (op.getOperator() == operator) {
|
||||
return op.getMethod().callStatic(position, environment, first, second, third);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public IPartialExpression member(ZenPosition position, IEnvironmentGlobal environment, Expression value, String member) {
|
||||
if (members.containsKey(member)) {
|
||||
return members.get(member).instance(position, environment, value);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public IPartialExpression staticMember(ZenPosition position, IEnvironmentGlobal environment, String member) {
|
||||
if (staticMembers.containsKey(member)) {
|
||||
return staticMembers.get(member).instance(position, environment);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void checkStatic(Method method) {
|
||||
if ((method.getModifiers() & Modifier.STATIC) == 0) {
|
||||
throw new RuntimeException("Expansion method " + method.getName() + " must be static");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,294 @@
|
||||
package stanhebben.zenscript;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import stanhebben.zenscript.compiler.ClassNameGenerator;
|
||||
import stanhebben.zenscript.compiler.EnvironmentClass;
|
||||
import stanhebben.zenscript.compiler.EnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.EnvironmentMethod;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.definitions.Import;
|
||||
import stanhebben.zenscript.definitions.ParsedFunction;
|
||||
import stanhebben.zenscript.definitions.ParsedFunctionArgument;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.statements.Statement;
|
||||
import stanhebben.zenscript.statements.StatementReturn;
|
||||
import stanhebben.zenscript.symbols.IZenCompileEnvironment;
|
||||
import stanhebben.zenscript.symbols.IZenSymbol;
|
||||
import stanhebben.zenscript.symbols.SymbolArgument;
|
||||
import stanhebben.zenscript.symbols.SymbolZenStaticMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.StringUtil;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ZenModule {
|
||||
public static void compileScripts(String mainclass, String mainFileName, List<ZenParsedFile> scripts, IEnvironmentGlobal environmentGlobal) {
|
||||
ClassWriter clsMain = new ClassWriter(ClassWriter.COMPUTE_MAXS);
|
||||
clsMain.visitSource(mainclass, null);
|
||||
|
||||
clsMain.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, "__ZenMain__", null, internal(Object.class), new String[] {internal(Runnable.class)});
|
||||
MethodOutput mainRun = new MethodOutput(clsMain, Opcodes.ACC_PUBLIC, "run", "()V", null, null);
|
||||
mainRun.start();
|
||||
|
||||
for (ZenParsedFile script : scripts) {
|
||||
for (Map.Entry<String, ParsedFunction> function : script.getFunctions().entrySet()) {
|
||||
ParsedFunction fn = function.getValue();
|
||||
environmentGlobal.putValue(function.getKey(), new SymbolZenStaticMethod(
|
||||
script.getClassName(),
|
||||
fn.getName(),
|
||||
fn.getSignature(),
|
||||
fn.getArgumentTypes(),
|
||||
fn.getReturnType()));
|
||||
}
|
||||
}
|
||||
|
||||
for (ZenParsedFile script : scripts) {
|
||||
ClassWriter clsScript = new ClassWriter(ClassWriter.COMPUTE_MAXS);
|
||||
clsScript.visitSource(script.getFileName(), null);
|
||||
EnvironmentClass environmentScript = new EnvironmentClass(clsScript, environmentGlobal);
|
||||
|
||||
clsScript.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, script.getClassName(), null, internal(Object.class), new String[] {internal(Runnable.class)});
|
||||
for (Import imprt : script.getImports()) {
|
||||
List<String> name = imprt.getName();
|
||||
IPartialExpression value = environmentGlobal.getValue(name.get(0), imprt.getPosition());
|
||||
for (int i = 1; i < name.size(); i++) {
|
||||
IPartialExpression member = value.getMember(imprt.getPosition(), environmentGlobal, name.get(i));
|
||||
if (member == null) {
|
||||
environmentGlobal.error(imprt.getPosition(), StringUtil.join(name, ".") + " not found");
|
||||
break;
|
||||
}
|
||||
value = member;
|
||||
}
|
||||
IZenSymbol symbol = value.toSymbol();
|
||||
if (symbol == null) {
|
||||
environmentGlobal.error(imprt.getPosition(), StringUtil.join(name, ".") + " is not a valid import");
|
||||
} else {
|
||||
environmentScript.putValue(
|
||||
imprt.getRename() == null
|
||||
? name.get(name.size() - 1)
|
||||
: imprt.getRename(), value.toSymbol());
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<String, ParsedFunction> function : script.getFunctions().entrySet()) {
|
||||
ParsedFunction fn = function.getValue();
|
||||
|
||||
String signature = fn.getSignature();
|
||||
MethodOutput methodOutput = new MethodOutput(clsScript, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, function.getKey(), signature, null, null);
|
||||
EnvironmentMethod methodEnvironment = new EnvironmentMethod(methodOutput, environmentScript);
|
||||
|
||||
List<ParsedFunctionArgument> arguments = function.getValue().getArguments();
|
||||
for (int i = 0; i < arguments.size(); i++) {
|
||||
ParsedFunctionArgument argument = arguments.get(i);
|
||||
methodEnvironment.putValue(argument.getName(), new SymbolArgument(i, argument.getType()));
|
||||
}
|
||||
|
||||
methodOutput.start();
|
||||
Statement[] statements = fn.getStatements();
|
||||
for (Statement statement : statements) {
|
||||
statement.compile(methodEnvironment);
|
||||
}
|
||||
if (function.getValue().getReturnType() != ZenType.VOID) {
|
||||
if (statements[statements.length - 1] instanceof StatementReturn) {
|
||||
if (((StatementReturn) statements[statements.length - 1]).getExpression() != null) {
|
||||
fn.getReturnType()
|
||||
.defaultValue(fn.getPosition())
|
||||
.compile(true, methodEnvironment);
|
||||
methodOutput.returnType(fn.getReturnType().toASMType());
|
||||
}
|
||||
} else {
|
||||
fn.getReturnType()
|
||||
.defaultValue(fn.getPosition())
|
||||
.compile(true, methodEnvironment);
|
||||
methodOutput.returnType(fn.getReturnType().toASMType());
|
||||
}
|
||||
}
|
||||
methodOutput.end();
|
||||
}
|
||||
|
||||
if (script.getStatements().size() > 0) {
|
||||
MethodOutput scriptOutput = new MethodOutput(clsScript, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "__script__", "()V", null, null);
|
||||
IEnvironmentMethod functionMethod = new EnvironmentMethod(scriptOutput, environmentScript);
|
||||
scriptOutput.start();
|
||||
for (Statement statement : script.getStatements()) {
|
||||
statement.compile(functionMethod);
|
||||
}
|
||||
scriptOutput.ret();
|
||||
scriptOutput.end();
|
||||
|
||||
mainRun.invokeStatic(script.getClassName(), "__script__", "()V");
|
||||
}
|
||||
|
||||
clsScript.visitEnd();
|
||||
environmentGlobal.putClass(script.getClassName(), clsScript.toByteArray());
|
||||
}
|
||||
|
||||
mainRun.ret();
|
||||
mainRun.end();
|
||||
clsMain.visitEnd();
|
||||
|
||||
MethodVisitor constructor = clsMain.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
|
||||
constructor.visitCode();
|
||||
constructor.visitVarInsn(Opcodes.ALOAD, 0);
|
||||
constructor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
|
||||
constructor.visitInsn(Opcodes.RETURN);
|
||||
constructor.visitMaxs(0, 0);
|
||||
constructor.visitEnd();
|
||||
|
||||
environmentGlobal.putClass("__ZenMain__", clsMain.toByteArray());
|
||||
}
|
||||
|
||||
public static ZenModule compileScriptFile(File single, IZenCompileEnvironment environment) throws IOException {
|
||||
Map<String, byte[]> classes = new HashMap<String, byte[]>();
|
||||
ClassNameGenerator nameGen = new ClassNameGenerator();
|
||||
EnvironmentGlobal environmentGlobal = new EnvironmentGlobal(
|
||||
environment,
|
||||
classes,
|
||||
nameGen
|
||||
);
|
||||
|
||||
String filename = single.getName();
|
||||
String className = extractClassName(filename);
|
||||
|
||||
FileInputStream input = new FileInputStream(single);
|
||||
Reader reader = new InputStreamReader(new BufferedInputStream(input));
|
||||
ZenParser parser = new ZenParser(reader, environment);
|
||||
ZenParsedFile file = new ZenParsedFile(filename, className, parser, environmentGlobal);
|
||||
reader.close();
|
||||
|
||||
List<ZenParsedFile> files = new ArrayList<ZenParsedFile>();
|
||||
files.add(file);
|
||||
|
||||
compileScripts(className, filename, files, environmentGlobal);
|
||||
|
||||
// debug: output classes
|
||||
File outputDir = new File("generated");
|
||||
outputDir.mkdir();
|
||||
|
||||
for (Map.Entry<String, byte[]> entry : classes.entrySet()) {
|
||||
File outputFile = new File(outputDir, entry.getKey() + ".class");
|
||||
FileOutputStream output = new FileOutputStream(outputFile);
|
||||
output.write(entry.getValue());
|
||||
output.close();
|
||||
}
|
||||
|
||||
return new ZenModule(classes);
|
||||
}
|
||||
|
||||
public static ZenModule compileZip(File file, String subdir, IZenCompileEnvironment environment) throws IOException {
|
||||
Map<String, byte[]> classes = new HashMap<String, byte[]>();
|
||||
ClassNameGenerator nameGen = new ClassNameGenerator();
|
||||
EnvironmentGlobal environmentGlobal = new EnvironmentGlobal(
|
||||
environment,
|
||||
classes,
|
||||
nameGen
|
||||
);
|
||||
|
||||
List<ZenParsedFile> files = new ArrayList<ZenParsedFile>();
|
||||
|
||||
ZipFile zipFile = new ZipFile(file);
|
||||
Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
||||
while (entries.hasMoreElements()) {
|
||||
ZipEntry entry = entries.nextElement();
|
||||
|
||||
if (entry.getName().startsWith(subdir) && !entry.getName().equals(subdir)) {
|
||||
String filename = entry.getName().substring(subdir.length());
|
||||
String className = extractClassName(filename);
|
||||
|
||||
Reader reader = new InputStreamReader(new BufferedInputStream(zipFile.getInputStream(entry)));
|
||||
ZenParser parser = new ZenParser(reader, environment);
|
||||
ZenParsedFile pfile = new ZenParsedFile(filename, className, parser, environmentGlobal);
|
||||
files.add(pfile);
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
|
||||
String filename = file.getName();
|
||||
String className = extractClassName(filename);
|
||||
compileScripts(className, filename, files, environmentGlobal);
|
||||
|
||||
// debug: output classes
|
||||
File outputDir = new File("generated");
|
||||
outputDir.mkdir();
|
||||
|
||||
for (Map.Entry<String, byte[]> entry : classes.entrySet()) {
|
||||
File outputFile = new File(outputDir, entry.getKey() + ".class");
|
||||
FileOutputStream output = new FileOutputStream(outputFile);
|
||||
output.write(entry.getValue());
|
||||
output.close();
|
||||
}
|
||||
|
||||
return new ZenModule(classes);
|
||||
}
|
||||
|
||||
private final Map<String, byte[]> classes;
|
||||
private final MyClassLoader classLoader;
|
||||
|
||||
private ZenModule(Map<String, byte[]> classes) {
|
||||
this.classes = classes;
|
||||
classLoader = new MyClassLoader();
|
||||
}
|
||||
|
||||
public Runnable getMain() {
|
||||
try {
|
||||
return (Runnable) classLoader.loadClass("__ZenMain__").newInstance();
|
||||
} catch (InstantiationException e) {
|
||||
return null;
|
||||
} catch (IllegalAccessException e) {
|
||||
return null;
|
||||
} catch (ClassNotFoundException ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private class MyClassLoader extends ClassLoader {
|
||||
@Override
|
||||
public Class<?> findClass(String name) throws ClassNotFoundException {
|
||||
if (classes.containsKey(name)) {
|
||||
return this.defineClass(name, classes.get(name), 0, classes.get(name).length);
|
||||
}
|
||||
|
||||
return super.findClass(name);
|
||||
}
|
||||
}
|
||||
|
||||
private static String extractClassName(String filename) {
|
||||
filename = filename.replace('\\', '/');
|
||||
if (filename.startsWith("/")) filename = filename.substring(1);
|
||||
|
||||
int lastSlash = filename.lastIndexOf('/');
|
||||
int lastDot = filename.lastIndexOf('.');
|
||||
|
||||
if (lastDot > lastSlash) {
|
||||
filename = filename.substring(0, lastDot);
|
||||
}
|
||||
if (lastSlash > 0) {
|
||||
String dir = filename.substring(0, lastSlash);
|
||||
String name = filename.substring(lastSlash + 1, lastSlash + 2).toUpperCase() + filename.substring(lastSlash + 2);
|
||||
return dir + '.' + name;
|
||||
} else {
|
||||
return filename.substring(0, 1).toUpperCase() + filename.substring(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import static stanhebben.zenscript.ZenParser.*;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.definitions.Import;
|
||||
import stanhebben.zenscript.definitions.ParsedFunction;
|
||||
import stanhebben.zenscript.parser.Token;
|
||||
import stanhebben.zenscript.statements.Statement;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ZenParsedFile {
|
||||
private final String filename;
|
||||
private final String classname;
|
||||
private final List<Import> imports;
|
||||
private final Map<String, ParsedFunction> functions;
|
||||
private final List<Statement> statements;
|
||||
|
||||
public ZenParsedFile(String filename, String classname, ZenParser parser, IEnvironmentGlobal environment) {
|
||||
this.filename = filename;
|
||||
this.classname = classname;
|
||||
|
||||
imports = new ArrayList<Import>();
|
||||
functions = new HashMap<String, ParsedFunction>();
|
||||
statements = new ArrayList<Statement>();
|
||||
|
||||
parser.setFile(this);
|
||||
|
||||
while (parser.peek() != null && parser.peek().getType() == T_IMPORT) {
|
||||
Token start = parser.next();
|
||||
|
||||
List<String> importName = new ArrayList<String>();
|
||||
Token tName = parser.required(T_ID, "identifier expected");
|
||||
importName.add(tName.getValue());
|
||||
|
||||
while (parser.optional(T_DOT) != null) {
|
||||
Token tNamePart = parser.required(T_ID, "identifier expected");
|
||||
importName.add(tNamePart.getValue());
|
||||
}
|
||||
|
||||
String rename = null;
|
||||
if (parser.optional(T_AS) != null) {
|
||||
Token tRename = parser.required(T_ID, "identifier expected");
|
||||
rename = tRename.getValue();
|
||||
}
|
||||
|
||||
parser.required(T_SEMICOLON, "; expected");
|
||||
|
||||
imports.add(new Import(start.getPosition(), importName, rename));
|
||||
}
|
||||
|
||||
while (parser.hasNext()) {
|
||||
Token next = parser.peek();
|
||||
if (next.getType() == T_FUNCTION) {
|
||||
ParsedFunction function = ParsedFunction.parse(parser, environment);
|
||||
if (functions.containsKey(function.getName())) {
|
||||
environment.error(function.getPosition(), "function " + function.getName() + " already exists");
|
||||
}
|
||||
functions.put(function.getName(), function);
|
||||
} else {
|
||||
statements.add(Statement.read(parser, environment));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String getClassName() {
|
||||
return classname;
|
||||
}
|
||||
|
||||
public String getFileName() {
|
||||
return filename;
|
||||
}
|
||||
|
||||
public List<Import> getImports() {
|
||||
return imports;
|
||||
}
|
||||
|
||||
public List<Statement> getStatements() {
|
||||
return statements;
|
||||
}
|
||||
|
||||
public Map<String, ParsedFunction> getFunctions() {
|
||||
return functions;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ZenParsedModule {
|
||||
private List<ZenParsedFile> files;
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
package stanhebben.zenscript;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.util.HashMap;
|
||||
import stanhebben.zenscript.parser.CompiledDFA;
|
||||
import stanhebben.zenscript.parser.NFA;
|
||||
import stanhebben.zenscript.parser.Token;
|
||||
import stanhebben.zenscript.parser.TokenStream;
|
||||
import stanhebben.zenscript.symbols.IZenCompileEnvironment;
|
||||
|
||||
public class ZenParser 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;
|
||||
public static final int T_STRINGVALUE = 4;
|
||||
public static final int T_AOPEN = 5;
|
||||
public static final int T_ACLOSE = 6;
|
||||
public static final int T_SQBROPEN = 7;
|
||||
public static final int T_SQBRCLOSE = 8;
|
||||
public static final int T_DOT2 = 9;
|
||||
public static final int T_DOT = 10;
|
||||
public static final int T_COMMA = 11;
|
||||
public static final int T_PLUSASSIGN = 12;
|
||||
public static final int T_PLUS = 13;
|
||||
public static final int T_MINUSASSIGN = 14;
|
||||
public static final int T_MINUS = 15;
|
||||
public static final int T_MULASSIGN = 16;
|
||||
public static final int T_MUL = 17;
|
||||
public static final int T_DIVASSIGN = 18;
|
||||
public static final int T_DIV = 19;
|
||||
public static final int T_MODASSIGN = 20;
|
||||
public static final int T_MOD = 21;
|
||||
public static final int T_ORASSIGN = 22;
|
||||
public static final int T_OR = 23;
|
||||
public static final int T_OR2 = 40;
|
||||
public static final int T_ANDASSIGN = 24;
|
||||
public static final int T_AND2 = 41;
|
||||
public static final int T_AND = 25;
|
||||
public static final int T_XORASSIGN = 26;
|
||||
public static final int T_XOR = 27;
|
||||
public static final int T_QUEST = 28;
|
||||
public static final int T_COLON = 29;
|
||||
public static final int T_BROPEN = 30;
|
||||
public static final int T_BRCLOSE = 31;
|
||||
public static final int T_TILDEASSIGN = 45;
|
||||
public static final int T_TILDE = 32;
|
||||
public static final int T_SEMICOLON = 33;
|
||||
public static final int T_LTEQ = 34;
|
||||
public static final int T_LT = 35;
|
||||
public static final int T_GTEQ = 36;
|
||||
public static final int T_GT = 37;
|
||||
public static final int T_EQ = 38;
|
||||
public static final int T_ASSIGN = 39;
|
||||
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;
|
||||
public static final int T_SHORT = 102;
|
||||
public static final int T_INT = 103;
|
||||
public static final int T_LONG = 104;
|
||||
public static final int T_FLOAT = 105;
|
||||
public static final int T_DOUBLE = 106;
|
||||
public static final int T_STRING = 107;
|
||||
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;
|
||||
public static final int T_ELSE = 123;
|
||||
public static final int T_FOR = 124;
|
||||
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",
|
||||
"//[^\n]*\n",
|
||||
"[ \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}))*\"",
|
||||
"\\{",
|
||||
"\\}",
|
||||
"\\[",
|
||||
"\\]",
|
||||
"\\.\\.",
|
||||
"\\.",
|
||||
",",
|
||||
"\\+=",
|
||||
"\\+",
|
||||
"\\-=",
|
||||
"\\-",
|
||||
"\\*=",
|
||||
"\\*",
|
||||
"/=",
|
||||
"/",
|
||||
"%=",
|
||||
"%",
|
||||
"\\|=",
|
||||
"\\|\\|",
|
||||
"\\|",
|
||||
"&=",
|
||||
"&&",
|
||||
"&",
|
||||
"\\^=",
|
||||
"\\^",
|
||||
"\\?",
|
||||
":",
|
||||
"\\(",
|
||||
"\\)",
|
||||
"~=",
|
||||
"~",
|
||||
";",
|
||||
"<=",
|
||||
"<",
|
||||
">=",
|
||||
">",
|
||||
"==",
|
||||
"=",
|
||||
"!=",
|
||||
"!",
|
||||
"$"
|
||||
};
|
||||
private static final int[] FINALS = {
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
T_ID,
|
||||
T_FLOATVALUE,
|
||||
T_INTVALUE,
|
||||
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);
|
||||
KEYWORDS.put("bool", T_BOOL);
|
||||
KEYWORDS.put("byte", T_BYTE);
|
||||
KEYWORDS.put("short", T_SHORT);
|
||||
KEYWORDS.put("int", T_INT);
|
||||
KEYWORDS.put("long", T_LONG);
|
||||
KEYWORDS.put("float", T_FLOAT);
|
||||
KEYWORDS.put("double", T_DOUBLE);
|
||||
KEYWORDS.put("string", T_STRING);
|
||||
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);
|
||||
KEYWORDS.put("else", T_ELSE);
|
||||
KEYWORDS.put("for", T_FOR);
|
||||
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;
|
||||
|
||||
public ZenParser(Reader contents, IZenCompileEnvironment environment) throws IOException {
|
||||
super(contents, DFA);
|
||||
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
public ZenParser(String contents, IZenCompileEnvironment environment) throws IOException {
|
||||
super(new StringReader(contents), DFA);
|
||||
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
public IZenCompileEnvironment getEnvironment() {
|
||||
return environment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token process(Token token) {
|
||||
if (token.getType() == T_ID) {
|
||||
if (KEYWORDS.containsKey(token.getValue())) {
|
||||
return new Token(token.getValue(), KEYWORDS.get(token.getValue()), token.getPosition());
|
||||
}
|
||||
}
|
||||
return token;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ZenRuntimeException extends RuntimeException {
|
||||
public ZenRuntimeException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.annotations;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public enum CompareType {
|
||||
FULL,
|
||||
LT,
|
||||
GT,
|
||||
EQ,
|
||||
NE,
|
||||
LE,
|
||||
GE
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Makes a class implementing List expose itself as iterable over keys and values.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface IterableList {
|
||||
String value();
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Makes a class implementing Map expose itself as iterable over keys and values.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface IterableMap {
|
||||
String key();
|
||||
String value();
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Indicates a simple iterable. Should implement the iterable interface.
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface IterableSimple {
|
||||
String value();
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.PARAMETER)
|
||||
public @interface NotNull {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.annotations;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public enum OperatorType {
|
||||
ADD,
|
||||
SUB,
|
||||
MUL,
|
||||
DIV,
|
||||
MOD,
|
||||
CAT,
|
||||
OR,
|
||||
AND,
|
||||
XOR,
|
||||
NEG,
|
||||
NOT,
|
||||
INDEXSET,
|
||||
INDEXGET,
|
||||
RANGE,
|
||||
CONTAINS,
|
||||
COMPARE,
|
||||
MEMBERGETTER,
|
||||
MEMBERSETTER,
|
||||
EQUALS
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Denotes that this parameter is optional. Inside the script, this parameter
|
||||
* may be omitted, in which case it is automatically filled with the default
|
||||
* value of that parameter type (false, null or 0). Invalid for @NonNull type.
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.PARAMETER)
|
||||
public @interface Optional {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Denotes a caster. Casters are capable of converting one type into another.
|
||||
* They implement the 'as' operator.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface ZenCaster {
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Exposes this class to ZenScript. The class can then be imported and accessed
|
||||
* into any script.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||
public @interface ZenClass {
|
||||
String value() default "";
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||
public @interface ZenExpansion {
|
||||
public String value();
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Indicates a getter. Will make the given method accessible as getter, that is,
|
||||
* it's called when value.name is being used.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface ZenGetter {
|
||||
String value() default "";
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stan
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface ZenMemberGetter {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stan
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface ZenMemberSetter {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface ZenMethod {
|
||||
public String value() default "";
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface ZenMethodStatic {
|
||||
public String value() default "";
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface ZenOperator {
|
||||
OperatorType value();
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface ZenSetter {
|
||||
String value() default "";
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.compiler;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ClassNameGenerator {
|
||||
private int counter = 0;
|
||||
|
||||
public ClassNameGenerator() {
|
||||
|
||||
}
|
||||
|
||||
public String generate() {
|
||||
return "ZenClass" + counter++;
|
||||
}
|
||||
|
||||
public String generate(String prefix) {
|
||||
return prefix + (counter++);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.compiler;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.objectweb.asm.ClassVisitor;
|
||||
import stanhebben.zenscript.TypeExpansion;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.symbols.IZenCompileEnvironment;
|
||||
import stanhebben.zenscript.symbols.IZenSymbol;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stan
|
||||
*/
|
||||
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;
|
||||
this.local = new HashMap<String, IZenSymbol>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassVisitor getClassOutput() {
|
||||
return output;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType(Type type) {
|
||||
return global.getType(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IZenCompileEnvironment getEnvironment() {
|
||||
return global.getEnvironment();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeExpansion getExpansion(String name) {
|
||||
return global.getExpansion(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String makeClassName() {
|
||||
return global.makeClassName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsClass(String name) {
|
||||
return global.containsClass(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putClass(String name, byte[] data) {
|
||||
global.putClass(name, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression getValue(String name, ZenPosition position) {
|
||||
if (local.containsKey(name)) {
|
||||
return local.get(name).instance(position);
|
||||
} else {
|
||||
return global.getValue(name, position);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putValue(String name, IZenSymbol value) {
|
||||
local.put(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(ZenPosition position, String message) {
|
||||
global.error(position, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warning(ZenPosition position, String message) {
|
||||
global.warning(position, message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.compiler;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import stanhebben.zenscript.IZenErrorLogger;
|
||||
import stanhebben.zenscript.TypeExpansion;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.symbols.IZenCompileEnvironment;
|
||||
import stanhebben.zenscript.symbols.IZenSymbol;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class EnvironmentGlobal implements IEnvironmentGlobal {
|
||||
private final IZenCompileEnvironment environment;
|
||||
private final IZenErrorLogger errors;
|
||||
private final Map<String, byte[]> classes;
|
||||
private final Map<String, IZenSymbol> local;
|
||||
private final ClassNameGenerator nameGen;
|
||||
private final TypeRegistry types;
|
||||
|
||||
public EnvironmentGlobal(
|
||||
IZenCompileEnvironment environment,
|
||||
Map<String, byte[]> classes,
|
||||
ClassNameGenerator nameGen) {
|
||||
this.environment = environment;
|
||||
this.errors = environment.getErrorLogger();
|
||||
this.classes = classes;
|
||||
this.nameGen = nameGen;
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(ZenPosition position, String message) {
|
||||
errors.error(position, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warning(ZenPosition position, String message) {
|
||||
errors.warning(position, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IZenCompileEnvironment getEnvironment() {
|
||||
return environment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression getValue(String name, ZenPosition position) {
|
||||
if (local.containsKey(name)) {
|
||||
return local.get(name).instance(position);
|
||||
} else {
|
||||
IZenSymbol symbol = environment.getGlobal(name);
|
||||
return symbol == null ? null : symbol.instance(position);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putValue(String name, IZenSymbol value) {
|
||||
if (local.containsKey(name)) {
|
||||
throw new RuntimeException("already exists");
|
||||
} else {
|
||||
local.put(name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.compiler;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.objectweb.asm.ClassVisitor;
|
||||
import stanhebben.zenscript.TypeExpansion;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.symbols.IZenCompileEnvironment;
|
||||
import stanhebben.zenscript.symbols.IZenSymbol;
|
||||
import stanhebben.zenscript.symbols.SymbolLocal;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class EnvironmentMethod implements IEnvironmentMethod {
|
||||
private final MethodOutput output;
|
||||
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)) {
|
||||
locals.put(variable, output.local(variable.getType().toASMType()));
|
||||
}
|
||||
return locals.get(variable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType(Type type) {
|
||||
return environment.getType(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(ZenPosition position, String message) {
|
||||
environment.error(position, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warning(ZenPosition position, String message) {
|
||||
environment.warning(position, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IZenCompileEnvironment getEnvironment() {
|
||||
return environment.getEnvironment();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeExpansion getExpansion(String name) {
|
||||
return environment.getExpansion(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String makeClassName() {
|
||||
return environment.makeClassName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putClass(String name, byte[] data) {
|
||||
environment.putClass(name, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsClass(String name) {
|
||||
return environment.containsClass(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression getValue(String name, ZenPosition position) {
|
||||
if (local.containsKey(name)) {
|
||||
return local.get(name).instance(position);
|
||||
} else {
|
||||
return environment.getValue(name, position);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putValue(String name, IZenSymbol value) {
|
||||
local.put(name, value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.compiler;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.objectweb.asm.ClassVisitor;
|
||||
import stanhebben.zenscript.TypeExpansion;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.symbols.IZenCompileEnvironment;
|
||||
import stanhebben.zenscript.symbols.IZenSymbol;
|
||||
import stanhebben.zenscript.symbols.SymbolLocal;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stan
|
||||
*/
|
||||
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>();
|
||||
this.locals = new HashMap<SymbolLocal, Integer>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MethodOutput getOutput() {
|
||||
return outer.getOutput();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLocal(SymbolLocal variable) {
|
||||
if (locals.containsKey(variable)) {
|
||||
return locals.get(variable);
|
||||
} else {
|
||||
return outer.getLocal(variable);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassVisitor getClassOutput() {
|
||||
return outer.getClassOutput();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType(Type type) {
|
||||
return outer.getType(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IZenCompileEnvironment getEnvironment() {
|
||||
return outer.getEnvironment();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeExpansion getExpansion(String name) {
|
||||
return outer.getExpansion(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String makeClassName() {
|
||||
return outer.makeClassName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsClass(String name) {
|
||||
return outer.containsClass(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putClass(String name, byte[] data) {
|
||||
outer.putClass(name, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression getValue(String name, ZenPosition position) {
|
||||
if (local.containsKey(name)) {
|
||||
return local.get(name).instance(position);
|
||||
} else {
|
||||
return outer.getValue(name, position);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putValue(String name, IZenSymbol value) {
|
||||
local.put(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(ZenPosition position, String message) {
|
||||
outer.error(position, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warning(ZenPosition position, String message) {
|
||||
outer.warning(position, message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.compiler;
|
||||
|
||||
import org.objectweb.asm.ClassVisitor;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stan
|
||||
*/
|
||||
public interface IEnvironmentClass extends IEnvironmentGlobal {
|
||||
public ClassVisitor getClassOutput();
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.compiler;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import stanhebben.zenscript.IZenErrorLogger;
|
||||
import stanhebben.zenscript.TypeExpansion;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.symbols.IZenCompileEnvironment;
|
||||
import stanhebben.zenscript.symbols.IZenSymbol;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stan
|
||||
*/
|
||||
public interface IEnvironmentGlobal extends ITypeRegistry, IZenErrorLogger {
|
||||
public IZenCompileEnvironment getEnvironment();
|
||||
|
||||
public TypeExpansion getExpansion(String name);
|
||||
|
||||
public String makeClassName();
|
||||
|
||||
public boolean containsClass(String name);
|
||||
|
||||
public void putClass(String name, byte[] data);
|
||||
|
||||
public IPartialExpression getValue(String name, ZenPosition position);
|
||||
|
||||
public void putValue(String name, IZenSymbol value);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.compiler;
|
||||
|
||||
import stanhebben.zenscript.symbols.SymbolLocal;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stan
|
||||
*/
|
||||
public interface IEnvironmentMethod extends IEnvironmentClass {
|
||||
public MethodOutput getOutput();
|
||||
|
||||
public int getLocal(SymbolLocal variable);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.compiler;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stan
|
||||
*/
|
||||
public interface ITypeRegistry {
|
||||
public ZenType getType(Type type);
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.compiler;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeAny;
|
||||
import stanhebben.zenscript.type.ZenTypeArrayBasic;
|
||||
import stanhebben.zenscript.type.ZenTypeArrayList;
|
||||
import stanhebben.zenscript.type.ZenTypeAssociative;
|
||||
import stanhebben.zenscript.type.ZenTypeBool;
|
||||
import stanhebben.zenscript.type.ZenTypeBoolObject;
|
||||
import stanhebben.zenscript.type.ZenTypeByte;
|
||||
import stanhebben.zenscript.type.ZenTypeByteObject;
|
||||
import stanhebben.zenscript.type.ZenTypeDouble;
|
||||
import stanhebben.zenscript.type.ZenTypeDoubleObject;
|
||||
import stanhebben.zenscript.type.ZenTypeFloat;
|
||||
import stanhebben.zenscript.type.ZenTypeFloatObject;
|
||||
import stanhebben.zenscript.type.ZenTypeInt;
|
||||
import stanhebben.zenscript.type.ZenTypeIntObject;
|
||||
import stanhebben.zenscript.type.ZenTypeLong;
|
||||
import stanhebben.zenscript.type.ZenTypeLongObject;
|
||||
import stanhebben.zenscript.type.ZenTypeNative;
|
||||
import stanhebben.zenscript.type.ZenTypeShort;
|
||||
import stanhebben.zenscript.type.ZenTypeShortObject;
|
||||
import stanhebben.zenscript.type.ZenTypeString;
|
||||
import stanhebben.zenscript.type.ZenTypeVoid;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class TypeRegistry implements ITypeRegistry {
|
||||
private final Map<Class, ZenType> types;
|
||||
|
||||
public TypeRegistry() {
|
||||
types = new HashMap<Class, ZenType>();
|
||||
|
||||
types.put(boolean.class, ZenTypeBool.INSTANCE);
|
||||
types.put(byte.class, ZenTypeByte.INSTANCE);
|
||||
types.put(short.class, ZenTypeShort.INSTANCE);
|
||||
types.put(int.class, ZenTypeInt.INSTANCE);
|
||||
types.put(long.class, ZenTypeLong.INSTANCE);
|
||||
types.put(float.class, ZenTypeFloat.INSTANCE);
|
||||
types.put(double.class, ZenTypeDouble.INSTANCE);
|
||||
types.put(void.class, ZenTypeVoid.INSTANCE);
|
||||
|
||||
types.put(Boolean.class, ZenTypeBoolObject.INSTANCE);
|
||||
types.put(Byte.class, ZenTypeByteObject.INSTANCE);
|
||||
types.put(Short.class, ZenTypeShortObject.INSTANCE);
|
||||
types.put(Integer.class, ZenTypeIntObject.INSTANCE);
|
||||
types.put(Long.class, ZenTypeLongObject.INSTANCE);
|
||||
types.put(Float.class, ZenTypeFloatObject.INSTANCE);
|
||||
types.put(Double.class, ZenTypeDoubleObject.INSTANCE);
|
||||
|
||||
types.put(String.class, ZenTypeString.INSTANCE);
|
||||
types.put(List.class, new ZenTypeArrayBasic(ZenTypeAny.INSTANCE));
|
||||
}
|
||||
|
||||
public ZenType getClassType(Class cls) {
|
||||
if (types.containsKey(cls)) {
|
||||
return types.get(cls);
|
||||
} else if (cls.isArray()) {
|
||||
ZenType result = new ZenTypeArrayBasic(getType(cls.getComponentType()));
|
||||
types.put(cls, result);
|
||||
return result;
|
||||
} else {
|
||||
ZenTypeNative result = new ZenTypeNative(cls);
|
||||
types.put(cls, result);
|
||||
result.complete(this);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType(Type type) {
|
||||
if (type instanceof ParameterizedType) {
|
||||
ParameterizedType pType = (ParameterizedType) type;
|
||||
Type raw = pType.getRawType();
|
||||
if (raw instanceof Class) {
|
||||
Class rawClass = (Class) raw;
|
||||
if (List.class.isAssignableFrom(rawClass)) {
|
||||
return getListType(pType);
|
||||
} else if (Map.class.isAssignableFrom(rawClass)) {
|
||||
return getMapType(pType);
|
||||
} else {
|
||||
return getClassType(rawClass);
|
||||
}
|
||||
} else {
|
||||
return getType(raw);
|
||||
}
|
||||
} else if (type instanceof Class) {
|
||||
return getClassType((Class) type);
|
||||
} else {
|
||||
// TODO: cannot retrieve type
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private ZenType getListType(ParameterizedType type) {
|
||||
if (type.getRawType() == List.class) {
|
||||
return new ZenTypeArrayList(getType(type.getActualTypeArguments()[0]));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private ZenType getMapType(ParameterizedType type) {
|
||||
if (type.getRawType() == Map.class) {
|
||||
return new ZenTypeAssociative(
|
||||
getType(type.getActualTypeArguments()[1]),
|
||||
getType(type.getActualTypeArguments()[0]));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.definitions;
|
||||
|
||||
import java.util.List;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.definitions;
|
||||
|
||||
import static stanhebben.zenscript.ZenParser.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import stanhebben.zenscript.ZenParser;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.parser.Token;
|
||||
import stanhebben.zenscript.statements.Statement;
|
||||
import stanhebben.zenscript.type.ZenTypeAny;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ParsedFunction {
|
||||
public static ParsedFunction parse(ZenParser parser, IEnvironmentGlobal environment) {
|
||||
parser.next();
|
||||
Token tName = parser.required(ZenParser.T_ID, "identifier expected");
|
||||
|
||||
// function (argname [as type], argname [as type], ...) [as type] { ...contents... }
|
||||
parser.required(T_BROPEN, "( expected");
|
||||
|
||||
List<ParsedFunctionArgument> arguments = new ArrayList<ParsedFunctionArgument>();
|
||||
if (parser.optional(T_BRCLOSE) == null) {
|
||||
Token argName = parser.required(T_ID, "identifier expected");
|
||||
ZenType type = ZenTypeAny.INSTANCE;
|
||||
if (parser.optional(T_AS) != null) {
|
||||
type = ZenType.read(parser, environment);
|
||||
}
|
||||
|
||||
arguments.add(new ParsedFunctionArgument(argName.getValue(), type));
|
||||
|
||||
while (parser.optional(T_COMMA) != null) {
|
||||
Token argName2 = parser.required(T_ID, "identifier expected");
|
||||
ZenType type2 = ZenTypeAny.INSTANCE;
|
||||
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];
|
||||
} else {
|
||||
ArrayList<Statement> statementsAL = new ArrayList<Statement>();
|
||||
|
||||
while (parser.optional(T_ACLOSE) == null) {
|
||||
statementsAL.add(Statement.read(parser, environment));
|
||||
}
|
||||
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) {
|
||||
sig.append(argument.getType().getSignature());
|
||||
}
|
||||
sig.append(")");
|
||||
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++) {
|
||||
result[i] = arguments.get(i).getType();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Statement[] getStatements() {
|
||||
return statements;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package stanhebben.zenscript.definitions;
|
||||
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import org.objectweb.asm.Label;
|
||||
import stanhebben.zenscript.ZenParser;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.parser.expression.ParsedExpression;
|
||||
import stanhebben.zenscript.symbols.IZenSymbol;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeBool;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
public abstract class Expression implements IPartialExpression {
|
||||
public static final Expression parse(ZenParser parser, IEnvironmentMethod environment) {
|
||||
return ParsedExpression.read(parser, environment)
|
||||
.compile(environment)
|
||||
.eval(environment);
|
||||
}
|
||||
|
||||
private final ZenPosition position;
|
||||
|
||||
public Expression(ZenPosition position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public ZenPosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
public abstract ZenType getType();
|
||||
|
||||
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
|
||||
if (getType() == type) {
|
||||
return this;
|
||||
} else if (getType().canCastImplicit(type, environment)) {
|
||||
return new ExpressionAs(position, this, type);
|
||||
} else {
|
||||
environment.error(position, "Cannot cast " + getType() + " to " + type);
|
||||
return new ExpressionInvalid(position, type);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void compile(boolean result, IEnvironmentMethod environment);
|
||||
|
||||
public void compileIf(Label onElse, IEnvironmentMethod environment) {
|
||||
if (getType() == ZenTypeBool.INSTANCE) {
|
||||
compile(true, environment);
|
||||
environment.getOutput().ifEQ(onElse);
|
||||
} else if (getType().isPointer()) {
|
||||
compile(true, environment);
|
||||
environment.getOutput().ifNull(onElse);
|
||||
} else {
|
||||
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 IZenSymbol toSymbol() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType toType(IEnvironmentGlobal environment) {
|
||||
environment.error(position, "not a valid type");
|
||||
return ZenType.ANY;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import org.objectweb.asm.Label;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return a.getType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
// 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);
|
||||
b.compile(true, environment);
|
||||
output.ifEQ(skip);
|
||||
output.iConst1();
|
||||
output.label(skip);
|
||||
|
||||
if (!result) {
|
||||
output.pop();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compileIf(Label onElse, IEnvironmentMethod environment) {
|
||||
a.compile(true, environment);
|
||||
environment.getOutput().ifEQ(onElse);
|
||||
b.compile(true, environment);
|
||||
environment.getOutput().ifEQ(onElse);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
environment.getOutput().load(type.toASMType(), id);
|
||||
}
|
||||
}
|
||||
+173
@@ -0,0 +1,173 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeBool;
|
||||
import stanhebben.zenscript.type.ZenTypeByte;
|
||||
import stanhebben.zenscript.type.ZenTypeDouble;
|
||||
import stanhebben.zenscript.type.ZenTypeFloat;
|
||||
import stanhebben.zenscript.type.ZenTypeInt;
|
||||
import stanhebben.zenscript.type.ZenTypeLong;
|
||||
import stanhebben.zenscript.type.ZenTypeShort;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return a.getType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
if (result) {
|
||||
a.compile(result, environment);
|
||||
b.compile(result, environment);
|
||||
|
||||
ZenType type = a.getType();
|
||||
MethodOutput output = environment.getOutput();
|
||||
|
||||
if (type == ZenTypeBool.INSTANCE) {
|
||||
switch (operator) {
|
||||
case AND:
|
||||
output.iAnd();
|
||||
break;
|
||||
case OR:
|
||||
output.iOr();
|
||||
break;
|
||||
case XOR:
|
||||
output.iXor();
|
||||
break;
|
||||
default:
|
||||
throw new RuntimeException("Unsupported operator on " + type + ": " + operator);
|
||||
}
|
||||
} else if (type == ZenTypeByte.INSTANCE || type == ZenTypeShort.INSTANCE || type == ZenTypeInt.INSTANCE) {
|
||||
switch (operator) {
|
||||
case ADD:
|
||||
output.iAdd();
|
||||
break;
|
||||
case SUB:
|
||||
output.iSub();
|
||||
break;
|
||||
case MUL:
|
||||
output.iMul();
|
||||
break;
|
||||
case DIV:
|
||||
output.iDiv();
|
||||
break;
|
||||
case MOD:
|
||||
output.iRem();
|
||||
break;
|
||||
case AND:
|
||||
output.iAnd();
|
||||
break;
|
||||
case OR:
|
||||
output.iOr();
|
||||
break;
|
||||
case XOR:
|
||||
output.iXor();
|
||||
break;
|
||||
default:
|
||||
throw new RuntimeException("Unsupported operator on " + type + ": " + operator);
|
||||
}
|
||||
} else if (type == ZenTypeLong.INSTANCE) {
|
||||
switch (operator) {
|
||||
case ADD:
|
||||
output.lAdd();
|
||||
break;
|
||||
case SUB:
|
||||
output.lSub();
|
||||
break;
|
||||
case MUL:
|
||||
output.lMul();
|
||||
break;
|
||||
case DIV:
|
||||
output.lDiv();
|
||||
break;
|
||||
case MOD:
|
||||
output.lRem();
|
||||
break;
|
||||
case AND:
|
||||
output.lAnd();
|
||||
break;
|
||||
case OR:
|
||||
output.lOr();
|
||||
break;
|
||||
case XOR:
|
||||
output.lXor();
|
||||
break;
|
||||
default:
|
||||
throw new RuntimeException("Unsupported operator on " + type + ": " + operator);
|
||||
}
|
||||
} else if (type == ZenTypeFloat.INSTANCE) {
|
||||
switch (operator) {
|
||||
case ADD:
|
||||
output.fAdd();
|
||||
break;
|
||||
case SUB:
|
||||
output.fSub();
|
||||
break;
|
||||
case MUL:
|
||||
output.fMul();
|
||||
break;
|
||||
case DIV:
|
||||
output.fDiv();
|
||||
break;
|
||||
case MOD:
|
||||
output.fRem();
|
||||
break;
|
||||
default:
|
||||
throw new RuntimeException("Unsupported operator on " + type + ": " + operator);
|
||||
}
|
||||
} else if (type == ZenTypeDouble.INSTANCE) {
|
||||
switch (operator) {
|
||||
case ADD:
|
||||
output.dAdd();
|
||||
break;
|
||||
case SUB:
|
||||
output.dSub();
|
||||
break;
|
||||
case MUL:
|
||||
output.dMul();
|
||||
break;
|
||||
case DIV:
|
||||
output.dDiv();
|
||||
break;
|
||||
case MOD:
|
||||
output.dRem();
|
||||
break;
|
||||
default:
|
||||
throw new RuntimeException("Unsupported operator on " + type + ": " + operator);
|
||||
}
|
||||
} else {
|
||||
throw new RuntimeException("Internal compilation error: " + type + " is not a supported arithmetic type");
|
||||
}
|
||||
} else {
|
||||
a.compile(result, environment);
|
||||
b.compile(result, environment);
|
||||
}
|
||||
}
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import org.objectweb.asm.Label;
|
||||
import stanhebben.zenscript.annotations.CompareType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeBool;
|
||||
import stanhebben.zenscript.type.ZenTypeByte;
|
||||
import stanhebben.zenscript.type.ZenTypeDouble;
|
||||
import stanhebben.zenscript.type.ZenTypeFloat;
|
||||
import stanhebben.zenscript.type.ZenTypeInt;
|
||||
import stanhebben.zenscript.type.ZenTypeLong;
|
||||
import stanhebben.zenscript.type.ZenTypeShort;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return ZenTypeBool.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
a.compile(result, environment);
|
||||
b.compile(result, environment);
|
||||
|
||||
if (result) {
|
||||
MethodOutput output = environment.getOutput();
|
||||
if (a.getType() == ZenTypeBool.INSTANCE) {
|
||||
if (type == CompareType.EQ) {
|
||||
Label onThen = new Label();
|
||||
Label onEnd = new Label();
|
||||
|
||||
output.ifICmpEQ(onThen);
|
||||
output.iConst0();
|
||||
output.goTo(onEnd);
|
||||
output.label(onThen);
|
||||
output.iConst1();
|
||||
output.label(onEnd);
|
||||
} else if (type == CompareType.NE) {
|
||||
Label onThen = new Label();
|
||||
Label onEnd = new Label();
|
||||
|
||||
output.ifICmpNE(onThen);
|
||||
output.iConst0();
|
||||
output.goTo(onEnd);
|
||||
output.label(onThen);
|
||||
output.iConst1();
|
||||
output.label(onEnd);
|
||||
} else {
|
||||
environment.error(getPosition(), "this kind of comparison is not supported on bool values");
|
||||
}
|
||||
} else {
|
||||
Label onThen = new Label();
|
||||
Label onEnd = new Label();
|
||||
|
||||
if (a.getType() == ZenTypeLong.INSTANCE) {
|
||||
output.lCmp();
|
||||
} else if (a.getType() == ZenTypeFloat.INSTANCE) {
|
||||
output.fCmp();
|
||||
} else if (a.getType() == ZenTypeDouble.INSTANCE) {
|
||||
output.dCmp();
|
||||
} else if (a.getType() == ZenTypeByte.INSTANCE
|
||||
|| a.getType() == ZenTypeShort.INSTANCE
|
||||
|| a.getType() == ZenTypeInt.INSTANCE) {
|
||||
// nothing to do
|
||||
} 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;
|
||||
default:
|
||||
environment.error(getPosition(), "this kind of comparison is not supported on int values");
|
||||
return;
|
||||
}
|
||||
|
||||
output.iConst0();
|
||||
output.goTo(onEnd);
|
||||
output.label(onThen);
|
||||
output.iConst1();
|
||||
output.label(onEnd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeBool;
|
||||
import stanhebben.zenscript.type.ZenTypeByte;
|
||||
import stanhebben.zenscript.type.ZenTypeDouble;
|
||||
import stanhebben.zenscript.type.ZenTypeFloat;
|
||||
import stanhebben.zenscript.type.ZenTypeInt;
|
||||
import stanhebben.zenscript.type.ZenTypeLong;
|
||||
import stanhebben.zenscript.type.ZenTypeShort;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return base.getType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
base.compile(result, environment);
|
||||
|
||||
MethodOutput output = environment.getOutput();
|
||||
if (result) {
|
||||
ZenType type = base.getType();
|
||||
if (type == ZenTypeBool.INSTANCE) {
|
||||
if (operator == OperatorType.NOT) {
|
||||
output.iNot();
|
||||
return;
|
||||
}
|
||||
} else if (type == ZenTypeByte.INSTANCE || type == ZenTypeShort.INSTANCE || type == ZenTypeInt.INSTANCE) {
|
||||
if (operator == OperatorType.NOT) {
|
||||
output.iNot();
|
||||
return;
|
||||
} else if (operator == OperatorType.NEG) {
|
||||
output.iNeg();
|
||||
return;
|
||||
}
|
||||
} else if (type == ZenTypeLong.INSTANCE) {
|
||||
if (operator == OperatorType.NOT) {
|
||||
output.lNot();
|
||||
return;
|
||||
} else if (operator == OperatorType.NEG) {
|
||||
output.lNeg();
|
||||
return;
|
||||
}
|
||||
} else if (type == ZenTypeFloat.INSTANCE) {
|
||||
if (operator == OperatorType.NEG) {
|
||||
output.fNeg();
|
||||
return;
|
||||
}
|
||||
} else if (type == ZenTypeDouble.INSTANCE) {
|
||||
if (operator == OperatorType.NEG) {
|
||||
output.fNeg();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
environment.error(getPosition(), "Invalid operation");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import org.objectweb.asm.Type;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeAny;
|
||||
import stanhebben.zenscript.type.ZenTypeArrayBasic;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
public class ExpressionArray extends Expression {
|
||||
private final Expression[] contents;
|
||||
private final ZenTypeArrayBasic type;
|
||||
|
||||
public ExpressionArray(ZenPosition position, Expression... contents) {
|
||||
super(position);
|
||||
|
||||
this.contents = contents;
|
||||
type = new ZenTypeArrayBasic(ZenTypeAny.INSTANCE);
|
||||
}
|
||||
|
||||
public ExpressionArray(ZenPosition position, Expression[] contents, ZenTypeArrayBasic type) {
|
||||
super(position);
|
||||
this.contents = contents;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
|
||||
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(), newContents, arrayType);
|
||||
} else if (this.type.canCastImplicit(type, environment)) {
|
||||
return this.type.cast(position, environment, this, type);
|
||||
} else {
|
||||
environment.error(position, "cannot cast " + this.type + " to " + type);
|
||||
return new ExpressionInvalid(position, type);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
Type baseType = type.getBaseType().toASMType();
|
||||
|
||||
MethodOutput output = environment.getOutput();
|
||||
output.constant(contents.length);
|
||||
output.newArray(baseType);
|
||||
|
||||
for (int i = 0; i < contents.length; i++) {
|
||||
output.dup();
|
||||
output.constant(i);
|
||||
contents[i].compile(result, environment);
|
||||
output.arrayStore(baseType);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeArray;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stan
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return baseType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
array.compile(result, environment);
|
||||
index.compile(result, environment);
|
||||
|
||||
if (result) {
|
||||
environment.getOutput().arrayLoad(baseType.toASMType());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeInt;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ExpressionArrayLength extends Expression {
|
||||
private final Expression value;
|
||||
|
||||
public ExpressionArrayLength(ZenPosition position, Expression value) {
|
||||
super(position);
|
||||
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return ZenTypeInt.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
environment.getOutput().arrayLength();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stan
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
array.compile(result, environment);
|
||||
index.compile(result, environment);
|
||||
value.compile(result, environment);
|
||||
|
||||
if (result) {
|
||||
environment.getOutput().arrayStore(value.getType().toASMType());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
public class ExpressionAs extends Expression {
|
||||
private final Expression value;
|
||||
private final ZenType type;
|
||||
|
||||
public ExpressionAs(ZenPosition position, Expression value, ZenType type) {
|
||||
super(position);
|
||||
|
||||
this.value = value;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
|
||||
if (!this.type.canCastExplicit(type, environment)) {
|
||||
environment.error(position, "cannot cast " + this.type + " to " + type);
|
||||
}
|
||||
return new ExpressionAs(getPosition(), this, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
value.compile(result, environment);
|
||||
if (result) {
|
||||
value.getType().compileCast(getPosition(), environment, type);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import org.objectweb.asm.Label;
|
||||
import stanhebben.zenscript.compiler.EnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.compiler.EnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeBool;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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 == ZenTypeBool.INSTANCE) {
|
||||
return this;
|
||||
} else if (type.canCastExplicit(type, environment)) {
|
||||
return new ExpressionAs(position, this, type);
|
||||
} else {
|
||||
environment.error(position, "Cannot cast a bool constant to " + type.toString());
|
||||
return new ExpressionInvalid(position, type);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression getMember(ZenPosition position, IEnvironmentGlobal environment, String name) {
|
||||
environment.error(position, "Bool constants do not have members");
|
||||
return new ExpressionInvalid(position, ZenTypeBool.INSTANCE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return ZenTypeBool.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
if (result) {
|
||||
if (value) {
|
||||
environment.getOutput().iConst1();
|
||||
} else {
|
||||
environment.getOutput().iConst0();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compileIf(Label onElse, IEnvironmentMethod environment) {
|
||||
if (!value) {
|
||||
environment.getOutput().goTo(onElse);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import org.objectweb.asm.Label;
|
||||
import stanhebben.zenscript.annotations.CompareType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeBool;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return ZenTypeBool.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
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;
|
||||
default:
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
output.iConst0();
|
||||
output.goTo(lblEnd);
|
||||
output.label(lblThen);
|
||||
output.iConst1();
|
||||
output.label(lblEnd);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import org.objectweb.asm.Label;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return onIf.getType(); // TODO: improve
|
||||
}
|
||||
|
||||
@Override
|
||||
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);
|
||||
environment.getOutput().label(lblElse);
|
||||
onElse.compile(result, environment);
|
||||
environment.getOutput().label(lblExit);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeDouble;
|
||||
import stanhebben.zenscript.type.ZenTypeFloat;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
if (!result) return;
|
||||
|
||||
if (type == ZenTypeFloat.INSTANCE) {
|
||||
environment.getOutput().constant((float) value);
|
||||
} else if (type == ZenTypeDouble.INSTANCE) {
|
||||
environment.getOutput().constant(value);
|
||||
} else {
|
||||
// TODO: internal error
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.definitions.ParsedFunctionArgument;
|
||||
import stanhebben.zenscript.statements.Statement;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeAny;
|
||||
import stanhebben.zenscript.type.ZenTypeFunction;
|
||||
import stanhebben.zenscript.type.ZenTypeNative;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ExpressionFunction extends Expression {
|
||||
private final List<ParsedFunctionArgument> arguments;
|
||||
private final ZenType returnType;
|
||||
private final Statement[] statements;
|
||||
|
||||
private final ZenTypeFunction functionType;
|
||||
|
||||
public ExpressionFunction(ZenPosition position, List<ParsedFunctionArgument> arguments, ZenType returnType, Statement[] statements) {
|
||||
super(position);
|
||||
|
||||
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();
|
||||
}
|
||||
functionType = new ZenTypeFunction(returnType, argumentTypes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
|
||||
if (type instanceof ZenTypeNative) {
|
||||
ZenTypeNative nativeType = (ZenTypeNative) type;
|
||||
Class nativeClass = nativeType.getNativeClass();
|
||||
if (nativeClass.isInterface() && nativeClass.getMethods().length == 1) {
|
||||
// functional interface
|
||||
Method method = nativeClass.getMethods()[0];
|
||||
if (returnType != ZenTypeAny.INSTANCE
|
||||
&& !returnType.canCastImplicit(environment.getType(method.getGenericReturnType()), environment)) {
|
||||
environment.error(position, "return type not compatible");
|
||||
return new ExpressionInvalid(position);
|
||||
}
|
||||
if (arguments.size() != method.getParameterTypes().length) {
|
||||
environment.error(position, "number of arguments incorrect");
|
||||
return new ExpressionInvalid(position);
|
||||
}
|
||||
for (int i = 0; i < arguments.size(); i++) {
|
||||
ZenType argumentType = environment.getType(method.getGenericParameterTypes()[i]);
|
||||
if (arguments.get(i).getType() != ZenTypeAny.INSTANCE
|
||||
&& !argumentType.canCastImplicit(arguments.get(i).getType(), environment)) {
|
||||
environment.error(position, "argument " + i + " doesn't match");
|
||||
return new ExpressionInvalid(position);
|
||||
}
|
||||
}
|
||||
|
||||
return new ExpressionJavaLambda(
|
||||
position,
|
||||
nativeClass,
|
||||
arguments,
|
||||
statements,
|
||||
environment.getType(nativeClass));
|
||||
} else {
|
||||
environment.error(position, type.toString() + " is not a functional interface");
|
||||
return new ExpressionInvalid(position);
|
||||
}
|
||||
} else {
|
||||
environment.error(position, "Cannot cast a function literal to " + type.toString());
|
||||
return new ExpressionInvalid(position);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return functionType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
// TODO: implement
|
||||
throw new UnsupportedOperationException("not yet implemented");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeByte;
|
||||
import stanhebben.zenscript.type.ZenTypeInt;
|
||||
import stanhebben.zenscript.type.ZenTypeLong;
|
||||
import stanhebben.zenscript.type.ZenTypeShort;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
|
||||
switch (type.getNumberType()) {
|
||||
case ZenType.NUM_BYTE:
|
||||
case ZenType.NUM_SHORT:
|
||||
case ZenType.NUM_INT:
|
||||
case ZenType.NUM_LONG:
|
||||
return new ExpressionInt(getPosition(), value, type);
|
||||
case ZenType.NUM_FLOAT:
|
||||
case ZenType.NUM_DOUBLE:
|
||||
return new ExpressionFloat(getPosition(), value, type);
|
||||
}
|
||||
|
||||
return super.cast(position, environment, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
if (!result) return;
|
||||
|
||||
MethodOutput output = environment.getOutput();
|
||||
if (type == ZenTypeByte.INSTANCE) {
|
||||
output.biPush((byte) value);
|
||||
} else if (type == ZenTypeShort.INSTANCE) {
|
||||
output.siPush((short) value);
|
||||
} else if (type == ZenTypeInt.INSTANCE) {
|
||||
output.constant((int) value);
|
||||
} else if (type == ZenTypeLong.INSTANCE) {
|
||||
output.constant(value);
|
||||
} else {
|
||||
// TODO: internal error
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ExpressionIntegerRange extends Expression {
|
||||
private final Expression from;
|
||||
private final Expression to;
|
||||
|
||||
public ExpressionIntegerRange(ZenPosition position, Expression from, Expression to) {
|
||||
super(position);
|
||||
|
||||
this.from = from;
|
||||
this.to = to;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return null; // TODO: implement
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
// TODO: implement
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import org.objectweb.asm.Label;
|
||||
import stanhebben.zenscript.compiler.EnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.compiler.EnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeAny;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression getMember(ZenPosition position, IEnvironmentGlobal errors, String name) {
|
||||
return new ExpressionInvalid(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression cast(ZenPosition position, IEnvironmentGlobal errors, ZenType type) {
|
||||
return new ExpressionInvalid(position, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
if (result) {
|
||||
environment.getOutput().aConstNull();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compileIf(Label onElse, IEnvironmentMethod environment) {
|
||||
environment.getOutput().goTo(onElse);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.natives.JavaMethod;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ExpressionJavaCallStatic extends Expression {
|
||||
private final JavaMethod method;
|
||||
private final Expression[] arguments;
|
||||
|
||||
public ExpressionJavaCallStatic(
|
||||
ZenPosition position,
|
||||
JavaMethod method,
|
||||
Expression... arguments) {
|
||||
super(position);
|
||||
|
||||
this.method = method;
|
||||
this.arguments = arguments;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return method.getReturnType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
MethodOutput output = environment.getOutput();
|
||||
|
||||
for (Expression argument : arguments) {
|
||||
argument.compile(true, environment);
|
||||
}
|
||||
|
||||
output.invokeStatic(method);
|
||||
|
||||
if (method.getReturnType() != ZenType.VOID && !result) {
|
||||
output.pop(method.getReturnType().isLarge());
|
||||
}
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ExpressionJavaCallStaticGenerated extends Expression {
|
||||
private final String target;
|
||||
private final String name;
|
||||
private final String descriptor;
|
||||
|
||||
private final ZenType returnZType;
|
||||
private final Expression[] arguments;
|
||||
|
||||
public ExpressionJavaCallStaticGenerated(
|
||||
ZenPosition position,
|
||||
String target, String name, String descriptor,
|
||||
ZenType returnZType,
|
||||
Expression... arguments) {
|
||||
super(position);
|
||||
|
||||
this.target = target;
|
||||
this.name = name;
|
||||
this.descriptor = descriptor;
|
||||
|
||||
this.returnZType = returnZType;
|
||||
this.arguments = arguments;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return returnZType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
MethodOutput output = environment.getOutput();
|
||||
|
||||
for (Expression argument : arguments) {
|
||||
argument.compile(true, environment);
|
||||
}
|
||||
|
||||
output.invokeStatic(target, name, descriptor);
|
||||
if (!descriptor.endsWith("V") && !result) {
|
||||
output.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.natives.JavaMethod;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ExpressionJavaCallVirtual extends Expression {
|
||||
private final JavaMethod method;
|
||||
|
||||
private final Expression receiver;
|
||||
private final Expression[] arguments;
|
||||
|
||||
public ExpressionJavaCallVirtual(
|
||||
ZenPosition position,
|
||||
JavaMethod method,
|
||||
Expression receiver,
|
||||
Expression... arguments) {
|
||||
super(position);
|
||||
|
||||
this.method = method;
|
||||
|
||||
this.receiver = receiver;
|
||||
this.arguments = arguments;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return method.getReturnType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
receiver.compile(true, environment);
|
||||
|
||||
for (Expression argument : arguments) {
|
||||
argument.compile(true, environment);
|
||||
}
|
||||
|
||||
environment.getOutput().invoke(method);
|
||||
if (method.getReturnType() != ZenType.VOID && !result) {
|
||||
environment.getOutput().pop(method.getReturnType().isLarge());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import stanhebben.zenscript.compiler.EnvironmentClass;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.compiler.EnvironmentMethod;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentClass;
|
||||
import stanhebben.zenscript.definitions.ParsedFunctionArgument;
|
||||
import stanhebben.zenscript.statements.Statement;
|
||||
import stanhebben.zenscript.symbols.SymbolArgument;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.descriptor;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ExpressionJavaLambda extends Expression {
|
||||
private final Class interfaceClass;
|
||||
private final List<ParsedFunctionArgument> arguments;
|
||||
private final Statement[] statements;
|
||||
|
||||
private final ZenType type;
|
||||
|
||||
public ExpressionJavaLambda(
|
||||
ZenPosition position,
|
||||
Class interfaceClass,
|
||||
List<ParsedFunctionArgument> arguments,
|
||||
Statement[] statements,
|
||||
ZenType type) {
|
||||
super(position);
|
||||
|
||||
this.interfaceClass = interfaceClass;
|
||||
this.arguments = arguments;
|
||||
this.statements = statements;
|
||||
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
if (!result) return;
|
||||
|
||||
Method method = interfaceClass.getMethods()[0];
|
||||
|
||||
// generate class
|
||||
String clsName = environment.makeClassName();
|
||||
|
||||
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
|
||||
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])));
|
||||
}
|
||||
|
||||
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();
|
||||
environment.getOutput().construct(clsName);
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.definitions.ParsedFunctionArgument;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeFunction;
|
||||
import stanhebben.zenscript.type.natives.JavaMethod;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
// TODO: compile
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.definitions.ParsedFunctionArgument;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeFunction;
|
||||
import stanhebben.zenscript.type.natives.JavaMethod;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
// TODO: compile
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.symbols.SymbolLocal;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
int local = environment.getLocal(variable);
|
||||
environment.getOutput().load(variable.getType().toASMType(), local);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.symbols.SymbolLocal;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
int local = environment.getLocal(variable);
|
||||
|
||||
value.compile(true, environment);
|
||||
if (result) {
|
||||
environment.getOutput().dup();
|
||||
}
|
||||
environment.getOutput().store(variable.getType().toASMType(), local);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import java.util.Map;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeBool;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return ZenTypeBool.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
if (result) {
|
||||
map.compile(result, environment);
|
||||
key.compile(result, environment);
|
||||
|
||||
environment.getOutput().invokeInterface(internal(Map.class), "containsKey", "()Ljava/lang/Object;");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import java.util.Map;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeAssociative;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
if (result) {
|
||||
map.compile(result, environment);
|
||||
index.compile(result, environment);
|
||||
environment.getOutput().invokeInterface(
|
||||
internal(Map.class),
|
||||
"get",
|
||||
"(Ljava/lang/Object;)Ljava/lang/Object;");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import java.util.Map;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeVoid;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
if (result) {
|
||||
map.compile(result, environment);
|
||||
index.compile(result, environment);
|
||||
environment.getOutput().invokeInterface(
|
||||
internal(Map.class),
|
||||
"put",
|
||||
"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import java.util.Map;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeInt;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ExpressionMapSize extends Expression {
|
||||
private final Expression map;
|
||||
|
||||
public ExpressionMapSize(ZenPosition position, Expression map) {
|
||||
super(position);
|
||||
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return ZenTypeInt.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
if (result) {
|
||||
map.compile(true, environment);
|
||||
environment.getOutput().invokeInterface(internal(Map.class), "size", "()I");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import org.objectweb.asm.Label;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeNull;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ExpressionNull extends Expression {
|
||||
public ExpressionNull(ZenPosition position) {
|
||||
super(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
|
||||
if (type.isPointer()) {
|
||||
return this;
|
||||
} else {
|
||||
environment.error(position, "Cannot convert null to " + type);
|
||||
return new ExpressionInvalid(position);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return ZenTypeNull.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
if (result) {
|
||||
environment.getOutput().aConstNull();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compileIf(Label onElse, IEnvironmentMethod environment) {
|
||||
environment.getOutput().goTo(onElse);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return a.getType(); // TODO: combine both types
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
// TODO: implement
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeString;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ExpressionString extends Expression {
|
||||
private final String value;
|
||||
|
||||
public ExpressionString(ZenPosition position, String value) {
|
||||
super(position);
|
||||
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return ZenTypeString.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
if (result) {
|
||||
environment.getOutput().constant(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import java.util.List;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeString;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return ZenTypeString.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
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 {
|
||||
for (Expression value : values) {
|
||||
value.compile(false, environment);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeAny;
|
||||
import stanhebben.zenscript.type.ZenTypeAssociative;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
|
||||
|
||||
public class ExpressionTable extends Expression {
|
||||
private final Expression[] keys;
|
||||
private final Expression[] values;
|
||||
private final ZenType type;
|
||||
|
||||
public ExpressionTable(ZenPosition position, Expression[] keys, Expression[] values) {
|
||||
super(position);
|
||||
|
||||
this.keys = keys;
|
||||
this.values = values;
|
||||
|
||||
type = new ZenTypeAssociative(ZenTypeAny.INSTANCE, ZenTypeAny.INSTANCE);
|
||||
}
|
||||
|
||||
public ExpressionTable(ZenPosition position, Expression[] keys, Expression[] values, ZenType type) {
|
||||
super(position);
|
||||
|
||||
this.keys = keys;
|
||||
this.values = values;
|
||||
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
if (result) {
|
||||
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].compile(true, environment);
|
||||
values[i].compile(true, environment);
|
||||
output.invokeInterface(internal(Map.class), "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
|
||||
output.pop();
|
||||
}
|
||||
} else {
|
||||
for (Expression key : keys) {
|
||||
key.compile(false, environment);
|
||||
}
|
||||
for (Expression value : values) {
|
||||
value.compile(false, environment);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
|
||||
if (type instanceof ZenTypeAssociative) {
|
||||
ZenTypeAssociative keyType = (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, keyType.getKeyType());
|
||||
newValues[i] = values[i].cast(position, environment, keyType.getValueType());
|
||||
}
|
||||
|
||||
return new ExpressionTable(getPosition(), newKeys, newValues, type);
|
||||
} else {
|
||||
return super.cast(position, environment, type);
|
||||
}
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression.partial;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.compiler.ITypeRegistry;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stan
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return types.getType(field.getGenericType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile(boolean result, IEnvironmentMethod environment) {
|
||||
if (result) {
|
||||
environment.getOutput().getStaticField(cls, field);
|
||||
}
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression.partial;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.symbols.IZenSymbol;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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 IZenSymbol toSymbol();
|
||||
|
||||
public ZenType toType(IEnvironmentGlobal environment);
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression.partial;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionInvalid;
|
||||
import stanhebben.zenscript.symbols.IZenSymbol;
|
||||
import stanhebben.zenscript.symbols.SymbolJavaClass;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression eval(IEnvironmentGlobal environment) {
|
||||
environment.error(position, "cannot use class name as expression");
|
||||
return new ExpressionInvalid(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression assign(ZenPosition position, IEnvironmentGlobal environment, Expression other) {
|
||||
environment.error(position, "cannot assign to a class name");
|
||||
return new ExpressionInvalid(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression getMember(ZenPosition position, IEnvironmentGlobal environment, String name) {
|
||||
return environment.getType(cls).getStaticMember(position, environment, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression call(ZenPosition position, IEnvironmentMethod environment, Expression... values) {
|
||||
environment.error(position, "cannot call a class name");
|
||||
return new ExpressionInvalid(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IZenSymbol toSymbol() {
|
||||
return new SymbolJavaClass(cls);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType toType(IEnvironmentGlobal environment) {
|
||||
return environment.getType(cls);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression.partial;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionInvalid;
|
||||
import stanhebben.zenscript.expression.ExpressionLocalGet;
|
||||
import stanhebben.zenscript.expression.ExpressionLocalSet;
|
||||
import stanhebben.zenscript.symbols.IZenSymbol;
|
||||
import stanhebben.zenscript.symbols.SymbolLocal;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression eval(IEnvironmentGlobal environment) {
|
||||
return new ExpressionLocalGet(position, variable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression getMember(ZenPosition position, IEnvironmentGlobal environment, String name) {
|
||||
return variable.getType().getMember(position, environment, this, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression assign(ZenPosition position, IEnvironmentGlobal environment, Expression other) {
|
||||
if (variable.isFinal()) {
|
||||
environment.error(position, "value cannot be changed");
|
||||
return new ExpressionInvalid(position);
|
||||
} else {
|
||||
return new ExpressionLocalSet(position, variable, other);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression call(ZenPosition position, IEnvironmentMethod environment, Expression... values) {
|
||||
return variable.getType().call(position, environment, eval(environment), values);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IZenSymbol toSymbol() {
|
||||
return variable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType toType(IEnvironmentGlobal environment) {
|
||||
environment.error(position, "not a valid type");
|
||||
return ZenType.ANY;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression.partial;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionInvalid;
|
||||
import stanhebben.zenscript.symbols.IZenSymbol;
|
||||
import stanhebben.zenscript.symbols.SymbolPackage;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression eval(IEnvironmentGlobal environment) {
|
||||
environment.error(position, "Cannot use package name as expression");
|
||||
return new ExpressionInvalid(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression assign(ZenPosition position, IEnvironmentGlobal environment, Expression other) {
|
||||
environment.error(position, "Cannot assign to a package");
|
||||
return new ExpressionInvalid(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression getMember(ZenPosition position, IEnvironmentGlobal environment, String name) {
|
||||
IZenSymbol member = contents.get(name);
|
||||
if (member == null) {
|
||||
environment.error(position, "No such member: " + name);
|
||||
return new ExpressionInvalid(position);
|
||||
} else {
|
||||
return member.instance(position);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression call(ZenPosition position, IEnvironmentMethod environment, Expression... values) {
|
||||
environment.error(position, "cannot call a package");
|
||||
return new ExpressionInvalid(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IZenSymbol toSymbol() {
|
||||
return null; // not supposed to be used as symbol
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType toType(IEnvironmentGlobal environment) {
|
||||
environment.error(position, "not a valid type");
|
||||
return ZenType.ANY;
|
||||
}
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression.partial;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionInvalid;
|
||||
import stanhebben.zenscript.expression.ExpressionJavaCallStaticGenerated;
|
||||
import stanhebben.zenscript.symbols.IZenSymbol;
|
||||
import stanhebben.zenscript.symbols.SymbolZenStaticMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stan
|
||||
*/
|
||||
public class PartialStaticGenerated implements IPartialExpression {
|
||||
private final ZenPosition position;
|
||||
private final String owner;
|
||||
private final String method;
|
||||
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;
|
||||
this.method = method;
|
||||
this.signature = signature;
|
||||
this.argumentTypes = argumentTypes;
|
||||
this.returnType = returnType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression eval(IEnvironmentGlobal environment) {
|
||||
environment.error(position, "cannot use a function as value");
|
||||
return new ExpressionInvalid(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression assign(ZenPosition position, IEnvironmentGlobal environment, Expression other) {
|
||||
environment.error(position, "cannot assign to a function");
|
||||
return new ExpressionInvalid(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression getMember(ZenPosition position, IEnvironmentGlobal environment, String name) {
|
||||
environment.error(position, "functions don't have members");
|
||||
return new ExpressionInvalid(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression call(ZenPosition position, IEnvironmentMethod environment, Expression... values) {
|
||||
if (values.length != argumentTypes.length) {
|
||||
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 ExpressionJavaCallStaticGenerated(position, owner, method, signature, returnType, arguments);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IZenSymbol toSymbol() {
|
||||
return new SymbolZenStaticMethod(owner, method, signature, argumentTypes, returnType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType toType(IEnvironmentGlobal environment) {
|
||||
environment.error(position, "not a valid type");
|
||||
return ZenType.ANY;
|
||||
}
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression.partial;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionInvalid;
|
||||
import stanhebben.zenscript.symbols.IZenSymbol;
|
||||
import stanhebben.zenscript.symbols.SymbolJavaStaticGetter;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.natives.JavaMethod;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class PartialStaticGetter implements IPartialExpression {
|
||||
private final ZenPosition position;
|
||||
private final JavaMethod method;
|
||||
|
||||
public PartialStaticGetter(ZenPosition position, JavaMethod method) {
|
||||
this.position = position;
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression eval(IEnvironmentGlobal environment) {
|
||||
return method.callStatic(position, environment);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression assign(ZenPosition position, IEnvironmentGlobal environment, Expression other) {
|
||||
environment.error(position, "cannot alter this final");
|
||||
return new ExpressionInvalid(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression getMember(ZenPosition position, IEnvironmentGlobal environment, String name) {
|
||||
return method.getReturnType().getMember(position, environment, this, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression call(ZenPosition position, IEnvironmentMethod environment, Expression... values) {
|
||||
environment.error(position, "value cannot be called");
|
||||
return new ExpressionInvalid(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IZenSymbol toSymbol() {
|
||||
return new SymbolJavaStaticGetter(method);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType toType(IEnvironmentGlobal environment) {
|
||||
environment.error(position, "not a valid type");
|
||||
return ZenType.ANY;
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.expression.partial;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionInvalid;
|
||||
import stanhebben.zenscript.symbols.IZenSymbol;
|
||||
import stanhebben.zenscript.symbols.SymbolJavaStaticMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.natives.JavaMethod;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class PartialStaticMethod implements IPartialExpression {
|
||||
private final ZenPosition position;
|
||||
private final JavaMethod method;
|
||||
|
||||
public PartialStaticMethod(ZenPosition position, JavaMethod method) {
|
||||
this.position = position;
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression eval(IEnvironmentGlobal environment) {
|
||||
environment.error(position, "not a valid expression");
|
||||
return new ExpressionInvalid(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression assign(ZenPosition position, IEnvironmentGlobal environment, Expression other) {
|
||||
environment.error(position, "cannot alter this final");
|
||||
return new ExpressionInvalid(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression getMember(ZenPosition position, IEnvironmentGlobal environment, String name) {
|
||||
environment.error(position, "methods have no members");
|
||||
return new ExpressionInvalid(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression call(ZenPosition position, IEnvironmentMethod environment, Expression... values) {
|
||||
if (method.accepts(environment, values)) {
|
||||
return method.callStatic(position, environment, values);
|
||||
} else {
|
||||
environment.error(position, "parameter count mismatch");
|
||||
return new ExpressionInvalid(position, method.getReturnType());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IZenSymbol toSymbol() {
|
||||
return new SymbolJavaStaticMethod(method);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType toType(IEnvironmentGlobal environment) {
|
||||
return ZenType.ANY;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
/**
|
||||
* This class is similar to an ArrayList, but is optimized for integers.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class ArrayListI {
|
||||
private int[] data;
|
||||
private int size;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
////////////////////
|
||||
// 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;
|
||||
}
|
||||
|
||||
public int[] toArray() {
|
||||
int[] result = new int[size];
|
||||
System.arraycopy(data, 0, result, 0, size);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
/**
|
||||
* Represents a character stream. Suitable for character by character parsing.
|
||||
*
|
||||
* Used in the regular expression parser.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class CharStream {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 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 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++];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
|
||||
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.
|
||||
*
|
||||
* A compiled DFA can be converted to a compact integer array. This array can
|
||||
* then be hardcoded in an application.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class CompiledDFA {
|
||||
public static final int NOFINAL = Integer.MIN_VALUE;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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++];
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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]);
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
||||
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];
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,249 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
/**
|
||||
* Implements a DFA.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class DFA {
|
||||
public static final int NOFINAL = Integer.MIN_VALUE;
|
||||
|
||||
private DFAState 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);
|
||||
|
||||
/* 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();
|
||||
|
||||
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];
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
/* 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);
|
||||
|
||||
/* 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();
|
||||
|
||||
nodeMap.get(i).addTransition(k, nodeMap.get(transitions[i].get(k)));
|
||||
}
|
||||
}
|
||||
|
||||
DFA result = new DFA(nodeMap.get(0));
|
||||
return result;
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
|
||||
/////////////////////////
|
||||
// Public inner classes
|
||||
/////////////////////////
|
||||
|
||||
/**
|
||||
* Represents a state in a DFA.
|
||||
*/
|
||||
public static class DFAState {
|
||||
private HashMapI<DFAState> transitions;
|
||||
private int finalCode = NOFINAL;
|
||||
|
||||
/**
|
||||
* Creates a new DFA state.
|
||||
*/
|
||||
public DFAState() {
|
||||
transitions = new HashMapI<DFAState>();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,216 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
/**
|
||||
* A HashMap implementation which is optimized for integer keys.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
* @param <T>
|
||||
*/
|
||||
public class HashMapI<T> {
|
||||
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;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
keys[index] = key;
|
||||
values[index] = value;
|
||||
}
|
||||
|
||||
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 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 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
|
||||
////////////////////
|
||||
|
||||
/**
|
||||
* 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 < keys.length; i++) {
|
||||
if (keys[i] == Integer.MIN_VALUE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
newKeys[index] = key;
|
||||
newValues[index] = value;
|
||||
}
|
||||
}
|
||||
|
||||
keys = newKeys;
|
||||
values = newValues;
|
||||
next = newNext;
|
||||
mask = newMask;
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
// Private inner classes
|
||||
//////////////////////////
|
||||
|
||||
private class KeyIterator implements IteratorI {
|
||||
private int i;
|
||||
|
||||
public KeyIterator() {
|
||||
i = 0;
|
||||
skip();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return i < keys.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int next() {
|
||||
int result = keys[i++];
|
||||
skip();
|
||||
return result;
|
||||
}
|
||||
|
||||
private void skip() {
|
||||
while (i < keys.length && keys[i] == Integer.MIN_VALUE) i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
|
||||
int index = key & mask;
|
||||
|
||||
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;
|
||||
|
||||
keys[index] = key;
|
||||
values[index] = value;
|
||||
}
|
||||
|
||||
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 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 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 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 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
|
||||
////////////////////
|
||||
|
||||
/**
|
||||
* 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 < newKeys.length; i++) {
|
||||
newKeys[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
if (keys[i] == Integer.MIN_VALUE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int key = keys[i];
|
||||
int 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;
|
||||
|
||||
newKeys[index] = key;
|
||||
newValues[index] = value;
|
||||
}
|
||||
}
|
||||
|
||||
keys = newKeys;
|
||||
values = newValues;
|
||||
next = newNext;
|
||||
mask = newMask;
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
// Private inner classes
|
||||
//////////////////////////
|
||||
|
||||
private class KeyIterator implements IteratorI {
|
||||
private int i;
|
||||
|
||||
public KeyIterator() {
|
||||
i = 0;
|
||||
skip();
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return i < keys.length;
|
||||
}
|
||||
|
||||
public int next() {
|
||||
int result = keys[i++];
|
||||
skip();
|
||||
return result;
|
||||
}
|
||||
|
||||
private void skip() {
|
||||
while (i < keys.length && keys[i] == Integer.MIN_VALUE) i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* HashSet implementation which is optimized for integer values.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class HashSetI {
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
// 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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
newKeys[index] = key;
|
||||
}
|
||||
}
|
||||
|
||||
values = newKeys;
|
||||
next = newNext;
|
||||
mask = newMask;
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
// Private inner classes
|
||||
//////////////////////////
|
||||
|
||||
private class KeyIterator implements IteratorI {
|
||||
private int i;
|
||||
|
||||
public KeyIterator() {
|
||||
i = 0;
|
||||
skip();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return i < values.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int next() {
|
||||
int result = values[i++];
|
||||
skip();
|
||||
return result;
|
||||
}
|
||||
|
||||
private void skip() {
|
||||
while (i < values.length && values[i] == Integer.MIN_VALUE) i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
/**
|
||||
* This interface represents an iterator over integer values.
|
||||
*
|
||||
* @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 the next element in this iteration.
|
||||
*
|
||||
* @return the next element in this iteration
|
||||
*/
|
||||
public int next();
|
||||
}
|
||||
@@ -0,0 +1,574 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class NFA {
|
||||
public static final int NOFINAL = Integer.MIN_VALUE;
|
||||
public static final int EPSILON = Integer.MIN_VALUE + 1;
|
||||
|
||||
private final NFAState initial;
|
||||
|
||||
private HashMap<NodeSet, DFA.DFAState> converted;
|
||||
|
||||
/**
|
||||
* Creates a new NFA with the specified initial state.
|
||||
*
|
||||
* @param initial initial state
|
||||
*/
|
||||
public NFA(NFAState initial) {
|
||||
this.initial = initial;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new NFA from the specified regular expression.
|
||||
*
|
||||
* Note: the regular expression implementation is not complete. Shorthand
|
||||
* character classes (\s, \w, \d), hexadecimal and unicode escape sequences and
|
||||
* unicode properties are not implemented.
|
||||
*
|
||||
* Anchors, lazy plus operators, lookbehind and lookforward are not implemented
|
||||
* since they cannot be implemented in an NFA.
|
||||
*
|
||||
* @param regexp regular expression
|
||||
*/
|
||||
public NFA(String regexp) {
|
||||
Partial main = processRegExp(new CharStream(regexp));
|
||||
initial = new NFAState();
|
||||
initial.addTransition(main.tailLabel, main.tail);
|
||||
main.head.setFinal(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an array of regular expressions to an NFA. Each regular expression
|
||||
* can have its own final class. The length of both arrays must match.
|
||||
*
|
||||
* @param regexp regular expression array
|
||||
* @param finals final classes
|
||||
*/
|
||||
public NFA(String[] regexp, int[] finals) {
|
||||
initial = new NFAState();
|
||||
|
||||
Partial[] partials = new Partial[regexp.length];
|
||||
for (int i = 0; i < regexp.length; i++) {
|
||||
//System.out.println("Processing regexp " + i + ": " + regexp[i]);
|
||||
partials[i] = processRegExp(new CharStream(regexp[i]));
|
||||
partials[i].head.setFinal(finals[i]);
|
||||
initial.addTransition(partials[i].tailLabel, partials[i].tail);
|
||||
}
|
||||
|
||||
//System.out.println("NFA generated");
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts this NFA to a DFA. The resulting DFA is not optimal. If the
|
||||
* NFA is ambiguous, the token with lowest value will receive preference.
|
||||
*
|
||||
* @return this NFA as DFA
|
||||
*/
|
||||
public DFA toDFA() {
|
||||
converted = new HashMap<NodeSet, DFA.DFAState>();
|
||||
HashSet<NFAState> closure = new HashSet<NFAState>();
|
||||
this.initial.closure(closure);
|
||||
DFA.DFAState init = convert(new NodeSet(closure));
|
||||
|
||||
DFA dfs = new DFA(init);
|
||||
return dfs;
|
||||
}
|
||||
|
||||
////////////////////
|
||||
// Private methods
|
||||
////////////////////
|
||||
|
||||
/* Converts a set of possible states to a DFA state. */
|
||||
private DFA.DFAState convert(NodeSet nodes) {
|
||||
if (!converted.containsKey(nodes)) {
|
||||
DFA.DFAState node = new DFA.DFAState();
|
||||
converted.put(nodes, node);
|
||||
|
||||
HashSet<Integer> edgeSet = new HashSet<Integer>();
|
||||
for (NFAState n : nodes.nodes) {
|
||||
n.alphabet(edgeSet);
|
||||
}
|
||||
for (int i : edgeSet) {
|
||||
HashSet<NFAState> edge = new HashSet<NFAState>();
|
||||
for (NFAState n : nodes.nodes) {
|
||||
n.edge(i, edge);
|
||||
}
|
||||
NodeSet set = new NodeSet(edge);
|
||||
node.addTransition(i, convert(set));
|
||||
}
|
||||
int finalCode = DFA.NOFINAL;
|
||||
for (NFAState n : nodes.nodes) {
|
||||
if (n.finalCode != NOFINAL) {
|
||||
if (finalCode != DFA.NOFINAL) {
|
||||
finalCode = Math.min(n.finalCode, finalCode);
|
||||
} else {
|
||||
finalCode = n.finalCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
node.setFinal(finalCode);
|
||||
|
||||
return node;
|
||||
}
|
||||
return converted.get(nodes);
|
||||
}
|
||||
|
||||
/* Processes a regular expression */
|
||||
private Partial processRegExp(CharStream stream) {
|
||||
Partial partial = processRegExp0(stream);
|
||||
if (stream.optional('|')) {
|
||||
ArrayList<Partial> partials = new ArrayList<Partial>();
|
||||
partials.add(partial);
|
||||
partials.add(processRegExp0(stream));
|
||||
while (stream.optional('|')) {
|
||||
partials.add(processRegExp0(stream));
|
||||
}
|
||||
NFAState head = new NFAState();
|
||||
NFAState tail = new NFAState();
|
||||
for (Partial p : partials) {
|
||||
tail.addTransition(p.tailLabel, p.tail);
|
||||
p.head.addTransition(EPSILON, head);
|
||||
}
|
||||
return new Partial(EPSILON, tail, head);
|
||||
} else {
|
||||
return partial;
|
||||
}
|
||||
}
|
||||
|
||||
/* Processes an element of an alternation clause */
|
||||
private Partial processRegExp0(CharStream stream) {
|
||||
Partial partial = processRegExp1(stream);
|
||||
while (!stream.peek(')') && !stream.peek('|') && stream.hasMore()) {
|
||||
Partial partial2 = processRegExp1(stream);
|
||||
partial.head.addTransition(partial2.tailLabel, partial2.tail);
|
||||
partial = new Partial(partial.tailLabel, partial.tail, partial2.head);
|
||||
}
|
||||
return partial;
|
||||
}
|
||||
|
||||
/* Processes a partial with optional repetition */
|
||||
private Partial processRegExp1(CharStream stream) {
|
||||
Partial partial = processPartial(stream);
|
||||
if (stream.optional('*')) {
|
||||
NFAState node = new NFAState();
|
||||
partial.head.addTransition(EPSILON, node);
|
||||
node.addTransition(partial.tailLabel, partial.tail);
|
||||
return new Partial(EPSILON, node, node);
|
||||
} else if (stream.optional('+')) {
|
||||
NFAState node = new NFAState();
|
||||
partial.head.addTransition(EPSILON, node);
|
||||
node.addTransition(partial.tailLabel, partial.tail);
|
||||
return new Partial(partial.tailLabel, partial.tail, node);
|
||||
} else if (stream.optional('?')) {
|
||||
NFAState node = new NFAState();
|
||||
node.addTransition(EPSILON, partial.head);
|
||||
node.addTransition(partial.tailLabel, partial.tail);
|
||||
return new Partial(EPSILON, node, partial.head);
|
||||
} else if (stream.optional('{')) {
|
||||
int amount = processInt(stream);
|
||||
if (amount < 0) throw new IllegalArgumentException("Repitition count expected");
|
||||
if (stream.optional(',')) {
|
||||
int amount2 = processInt(stream);
|
||||
stream.required('}');
|
||||
if (amount2 < 0) {
|
||||
// unbounded
|
||||
Partial[] duplicates = new Partial[amount];
|
||||
for (int i = 0; i < duplicates.length - 1; i++) {
|
||||
duplicates[i] = partial.duplicate();
|
||||
}
|
||||
duplicates[amount - 1] = partial;
|
||||
for (int i = 0; i < duplicates.length - 1; i++) {
|
||||
duplicates[i].head.addTransition(duplicates[i + 1].tailLabel, duplicates[i + 1].tail);
|
||||
}
|
||||
duplicates[amount - 1].head.addTransition(duplicates[amount - 1].tailLabel, duplicates[amount - 1].tail);
|
||||
return new Partial(duplicates[0].tailLabel, duplicates[0].tail, duplicates[amount - 1].head);
|
||||
} else {
|
||||
Partial[] duplicates = new Partial[amount2];
|
||||
for (int i = 0; i < duplicates.length - 1; i++) {
|
||||
duplicates[i] = partial.duplicate();
|
||||
}
|
||||
duplicates[amount2 - 1] = partial;
|
||||
for (int i = 0; i < duplicates.length - 1; i++) {
|
||||
duplicates[i].head.addTransition(duplicates[i + 1].tailLabel, duplicates[i + 1].tail);
|
||||
}
|
||||
for (int i = amount; i < amount2; i++) {
|
||||
if (i == 0) {
|
||||
/* insert additional node before the chain because minimal repeat is 0 */
|
||||
NFAState additional = new NFAState();
|
||||
additional.addTransition(duplicates[0].tailLabel, duplicates[0].tail);
|
||||
additional.addTransition(EPSILON, duplicates[amount2 - 1].head);
|
||||
duplicates[0].tailLabel = EPSILON;
|
||||
duplicates[0].tail = additional;
|
||||
} else {
|
||||
duplicates[i - 1].head.addTransition(EPSILON, duplicates[amount2 - 1].head);
|
||||
}
|
||||
}
|
||||
return new Partial(duplicates[0].tailLabel, duplicates[0].tail, duplicates[amount2 - 1].head);
|
||||
}
|
||||
} else {
|
||||
stream.required('}');
|
||||
|
||||
Partial[] duplicates = new Partial[amount];
|
||||
for (int i = 0; i < duplicates.length - 1; i++) {
|
||||
duplicates[i] = partial.duplicate();
|
||||
}
|
||||
duplicates[amount - 1] = partial;
|
||||
for (int i = 0; i < duplicates.length - 1; i++) {
|
||||
duplicates[i].head.addTransition(duplicates[i + 1].tailLabel, duplicates[i + 1].tail);
|
||||
}
|
||||
return new Partial(duplicates[0].tailLabel, duplicates[0].tail, duplicates[amount - 1].head);
|
||||
}
|
||||
} else {
|
||||
return partial;
|
||||
}
|
||||
}
|
||||
|
||||
/* Processes a part of a regular expression, which can be a character,
|
||||
* expression between brackets, a dot or a character list */
|
||||
private Partial processPartial(CharStream stream) {
|
||||
if (stream.optional('(')) {
|
||||
Partial result = processRegExp(stream);
|
||||
stream.required(')');
|
||||
return result;
|
||||
} else if (stream.optional('[')) {
|
||||
NFAState head = new NFAState();
|
||||
NFAState tail = new NFAState();
|
||||
|
||||
IteratorI iter = processCharList(stream).iterator();
|
||||
while (iter.hasNext()) {
|
||||
tail.addTransition(iter.next(), head);
|
||||
}
|
||||
stream.required(']');
|
||||
return new Partial(EPSILON, tail, head);
|
||||
} else if (stream.optional('.')) {
|
||||
NFAState head = new NFAState();
|
||||
NFAState tail = new NFAState();
|
||||
|
||||
for (int i = 0; i <= 256; i++) {
|
||||
tail.addTransition(i, head);
|
||||
}
|
||||
return new Partial(EPSILON, tail, head);
|
||||
} else {
|
||||
NFAState node = new NFAState();
|
||||
return new Partial(processChar(stream), node, node);
|
||||
}
|
||||
}
|
||||
|
||||
/* Processes a character list */
|
||||
private HashSetI processCharList(CharStream stream) {
|
||||
boolean invert = stream.optional('^');
|
||||
HashSetI base = new HashSetI();
|
||||
do {
|
||||
processCharPartial(base, stream);
|
||||
} while (!stream.peek(']'));
|
||||
if (invert) {
|
||||
HashSetI result = new HashSetI();
|
||||
for (int i = 0; i <= 256; i++) {
|
||||
if (!base.contains(i)) result.add(i);
|
||||
}
|
||||
return result;
|
||||
} else {
|
||||
return base;
|
||||
}
|
||||
}
|
||||
|
||||
/* Processes a character partial, which can be a single character or a range
|
||||
* of characters. */
|
||||
private void processCharPartial(HashSetI out, CharStream stream) {
|
||||
if (stream.optional('.')) {
|
||||
for (int i = 0; i <= 256; i++) {
|
||||
out.add(i);
|
||||
}
|
||||
} else {
|
||||
char from = processChar(stream);
|
||||
if (stream.optional('-')) {
|
||||
char to = processChar(stream);
|
||||
for (int i = from; i <= to; i++) {
|
||||
out.add(i);
|
||||
}
|
||||
} else {
|
||||
out.add(from);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Processes a single character */
|
||||
private char processChar(CharStream stream) {
|
||||
if (stream.optional('\\')) {
|
||||
if (stream.optional('r')) return '\r';
|
||||
if (stream.optional('n')) return '\n';
|
||||
if (stream.optional('t')) return '\t';
|
||||
if (stream.optional('[')) return '[';
|
||||
if (stream.optional(']')) return ']';
|
||||
if (stream.optional('(')) return '(';
|
||||
if (stream.optional(')')) return ')';
|
||||
if (stream.optional('.')) return '.';
|
||||
if (stream.optional('+')) return '+';
|
||||
if (stream.optional('-')) return '-';
|
||||
if (stream.optional('\\')) return '\\';
|
||||
if (stream.optional('{')) return '{';
|
||||
if (stream.optional('}')) return '}';
|
||||
if (stream.optional('?')) return '?';
|
||||
if (stream.optional('*')) return '*';
|
||||
if (stream.optional('~')) return '~';
|
||||
if (stream.optional('|')) return '|';
|
||||
if (stream.optional('^')) return '^';
|
||||
throw new IllegalArgumentException("Invalid character: " + stream.next());
|
||||
} else {
|
||||
if (stream.peek('[')
|
||||
|| stream.peek(']')
|
||||
|| stream.peek('(')
|
||||
|| stream.peek(')')
|
||||
|| stream.peek('{')
|
||||
|| stream.peek('}')
|
||||
|| stream.peek('.')
|
||||
|| stream.peek('-')
|
||||
|| stream.peek('+')
|
||||
|| stream.peek('?')
|
||||
|| stream.peek('*')) {
|
||||
throw new IllegalArgumentException("Invalid character: " + stream.next());
|
||||
} else {
|
||||
return stream.next();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Processes an optional integer, returns -1 if the next character do not
|
||||
* represent an integer */
|
||||
private int processInt(CharStream stream) {
|
||||
int data = stream.optional('0', '9') - '0';
|
||||
if (data < 0) return -1;
|
||||
char ch = stream.optional('0', '9');
|
||||
while (ch != 0) {
|
||||
data = data * 10 + (ch - '0');
|
||||
ch = stream.optional('0', '9');
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
/////////////////////////
|
||||
// Public inner classes
|
||||
/////////////////////////
|
||||
|
||||
/**
|
||||
* Represents an NFA state.
|
||||
*/
|
||||
public static class NFAState {
|
||||
private static int counter = 1;
|
||||
|
||||
private ArrayList<Transition> transitions;
|
||||
private ArrayList<NFAState> closure;
|
||||
private int index;
|
||||
private int finalCode = NOFINAL;
|
||||
|
||||
/**
|
||||
* Creates a new state.
|
||||
*/
|
||||
public NFAState() {
|
||||
transitions = new ArrayList<Transition>();
|
||||
index = counter++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a transition.
|
||||
*
|
||||
* @param label transition label
|
||||
* @param next next state
|
||||
*/
|
||||
public void addTransition(int label, NFAState next) {
|
||||
transitions.add(new Transition(label, next));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the final state of this state.
|
||||
*
|
||||
* @param finalCode
|
||||
*/
|
||||
public void setFinal(int finalCode) {
|
||||
if (this.finalCode == finalCode) return;
|
||||
this.finalCode = finalCode;
|
||||
|
||||
for (Transition t : transitions) {
|
||||
if (t.label == EPSILON) {
|
||||
t.next.setFinal(finalCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////
|
||||
// Private methods
|
||||
////////////////////
|
||||
|
||||
/**
|
||||
* Determines the (partial) closure of this state.
|
||||
*
|
||||
* @param output output to store the closure in
|
||||
*/
|
||||
private void closure(HashSet<NFAState> output) {
|
||||
for (NFAState node : closure()) output.add(node);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the (full) closure of this state.
|
||||
*
|
||||
* @return this state's closure
|
||||
*/
|
||||
private ArrayList<NFAState> closure() {
|
||||
if (closure == null) {
|
||||
HashSet<NFAState> tmp = new HashSet<NFAState>();
|
||||
tmp.add(this);
|
||||
for (Transition transition : transitions) {
|
||||
if (transition.label == EPSILON) {
|
||||
if (!tmp.contains(transition.next)) {
|
||||
tmp.add(transition.next);
|
||||
transition.next.closure(tmp);
|
||||
}
|
||||
}
|
||||
}
|
||||
closure = new ArrayList<NFAState>();
|
||||
for (NFAState node : tmp) closure.add(node);
|
||||
}
|
||||
return closure;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the possible set of states after a transition with the
|
||||
* specified value
|
||||
*
|
||||
* @param label transition label
|
||||
* @param output possible set of states (out)
|
||||
*/
|
||||
private void edge(int label, HashSet<NFAState> output) {
|
||||
for (Transition transition : transitions) {
|
||||
if (transition.label == label) {
|
||||
if (!output.contains(transition.next)) {
|
||||
output.add(transition.next);
|
||||
transition.next.closure(output);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the alphabet of this state.
|
||||
*
|
||||
* @param output alphabet (out)
|
||||
*/
|
||||
private void alphabet(HashSet<Integer> output) {
|
||||
for (NFAState node : closure()) {
|
||||
for (Transition t : node.transitions) {
|
||||
if (t.label != EPSILON) {
|
||||
output.add(t.label);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////
|
||||
// Inner class
|
||||
////////////////
|
||||
|
||||
/* Represents a transition */
|
||||
private class Transition {
|
||||
private int label;
|
||||
private NFAState next;
|
||||
|
||||
public Transition(int input, NFAState next) {
|
||||
this.label = input;
|
||||
this.next = next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Hashable set of nodes */
|
||||
private class NodeSet {
|
||||
private NFAState[] nodes;
|
||||
|
||||
public NodeSet(HashSet<NFAState> nodes) {
|
||||
this.nodes = new NFAState[nodes.size()];
|
||||
int i = 0;
|
||||
for (NFAState node : nodes) this.nodes[i++] = node;
|
||||
Arrays.sort(this.nodes, new Comparator<NFAState>() {
|
||||
public int compare(NFAState o1, NFAState o2) {
|
||||
return o1.index - o2.index;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(nodes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (other.getClass() != NodeSet.class) return false;
|
||||
return Arrays.equals(nodes, ((NodeSet)other).nodes);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains a partially parsed regular expression. Has a head and a tail.
|
||||
*
|
||||
* The tail is a transition and thus has a label and a next node. The head
|
||||
* is the 'final' state of the partial.
|
||||
*/
|
||||
private static class Partial {
|
||||
private int tailLabel;
|
||||
private NFAState tail;
|
||||
private NFAState head;
|
||||
|
||||
/**
|
||||
* Creates a new partial
|
||||
* @param tailLabel tail label
|
||||
* @param tail tail node
|
||||
* @param head head node
|
||||
*/
|
||||
public Partial(int tailLabel, NFAState tail, NFAState head) {
|
||||
this.tailLabel = tailLabel;
|
||||
this.tail = tail;
|
||||
this.head = head;
|
||||
}
|
||||
|
||||
/**
|
||||
* Duplicates the NFA of this partial.
|
||||
*
|
||||
* @return a duplicate of this partial
|
||||
*/
|
||||
public Partial duplicate() {
|
||||
HashMap<NFAState, NFAState> nodes = new HashMap<NFAState, NFAState>();
|
||||
|
||||
Queue<NFAState> todo = new LinkedList<NFAState>();
|
||||
todo.add(tail);
|
||||
nodes.put(tail, new NFAState());
|
||||
while (!todo.isEmpty()) {
|
||||
NFAState node = todo.poll();
|
||||
for (NFAState.Transition t : node.transitions) {
|
||||
if (!nodes.containsKey(t.next)) {
|
||||
nodes.put(t.next, new NFAState());
|
||||
todo.add(t.next);
|
||||
}
|
||||
nodes.get(node).addTransition(t.label, nodes.get(t.next));
|
||||
}
|
||||
}
|
||||
|
||||
return new Partial(tailLabel, nodes.get(tail), nodes.get(head));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
import stanhebben.zenscript.ZenParsedFile;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stan
|
||||
*/
|
||||
public class ParseException extends RuntimeException {
|
||||
private final Token token;
|
||||
private final String message;
|
||||
|
||||
public ParseException(Token token, String error) {
|
||||
super(token == null ? "Error at end of file - " + error : "Error parsing line " + token.getPosition().getLine() + ":" + token.getPosition().getLineOffset() + " - " + error + " (last token: " + token.getValue() + ")");
|
||||
|
||||
this.token = token;
|
||||
this.message = error;
|
||||
}
|
||||
|
||||
public ZenParsedFile getFile() {
|
||||
return token == null ? null : token.getPosition().getFile();
|
||||
}
|
||||
|
||||
public int getLine() {
|
||||
return token == null ? -1 : token.getPosition().getLine();
|
||||
}
|
||||
|
||||
public int getLineOffset() {
|
||||
return token == null ? -1 : token.getPosition().getLineOffset();
|
||||
}
|
||||
|
||||
public String getExplanation() {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
* Represents a token in a token stream.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class Token {
|
||||
private final ZenPosition position;
|
||||
private final String value;
|
||||
private final int 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;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public ZenPosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the string value of this token.
|
||||
*
|
||||
* @return token value
|
||||
*/
|
||||
public String getValue() {
|
||||
return 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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
/**
|
||||
* Thrown when an exception occurs while tokening a character stream.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class TokenException extends RuntimeException {
|
||||
public TokenException(int line, int lineOffset, char value) {
|
||||
super("Invalid character at " + line + ":" + lineOffset + " - " + value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,260 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Stack;
|
||||
import stanhebben.zenscript.ZenParsedFile;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
* Represents a token stream. A token stream reads characters from a reader and
|
||||
* presents it as a series of tokens.
|
||||
*
|
||||
* Token classes with a negative class are considered to be whitespace.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 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 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(t, error);
|
||||
}
|
||||
}
|
||||
|
||||
// LL(*) ability
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////
|
||||
// Iterator implementation
|
||||
////////////////////////////
|
||||
|
||||
public boolean hasNext() {
|
||||
return next != null;
|
||||
}
|
||||
|
||||
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++;
|
||||
|
||||
advance();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException("Not supported.");
|
||||
}
|
||||
|
||||
//////////////////////
|
||||
// Protected methods
|
||||
//////////////////////
|
||||
|
||||
/**
|
||||
* Can be implemented by subclasses to post-process tokens.
|
||||
*
|
||||
* @param token
|
||||
* @return processed token
|
||||
*/
|
||||
protected Token process(Token token) {
|
||||
return token;
|
||||
}
|
||||
|
||||
////////////////////
|
||||
// Private methods
|
||||
////////////////////
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
if (dfa.finals[state] != CompiledDFA.NOFINAL) {
|
||||
if (state == 0) throw new TokenException(line, lineOffset, (char)nextChar);
|
||||
next = process(new Token(value.toString(), dfa.finals[state], new ZenPosition(file, tLine, tLineOffset)));
|
||||
} else {
|
||||
throw new TokenException(line, lineOffset, (char)nextChar);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
// 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;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,440 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import stanhebben.zenscript.ZenParser;
|
||||
import static stanhebben.zenscript.ZenParser.*;
|
||||
import stanhebben.zenscript.annotations.CompareType;
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.definitions.ParsedFunctionArgument;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionFloat;
|
||||
import stanhebben.zenscript.expression.ExpressionFunction;
|
||||
import stanhebben.zenscript.expression.ExpressionInt;
|
||||
import stanhebben.zenscript.expression.ExpressionString;
|
||||
import stanhebben.zenscript.parser.ParseException;
|
||||
import stanhebben.zenscript.parser.Token;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.statements.Statement;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeAny;
|
||||
import stanhebben.zenscript.type.ZenTypeDouble;
|
||||
import stanhebben.zenscript.type.ZenTypeInt;
|
||||
import static stanhebben.zenscript.util.StringUtil.unescapeString;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public abstract class ParsedExpression {
|
||||
public static ParsedExpression read(ZenParser parser, IEnvironmentGlobal environment) {
|
||||
return readAssignExpression(parser, environment);
|
||||
}
|
||||
|
||||
private static ParsedExpression readAssignExpression(ZenParser parser, IEnvironmentGlobal environment) {
|
||||
ZenPosition position = parser.peek().getPosition();
|
||||
|
||||
ParsedExpression left = readConditionalExpression(position, parser, environment);
|
||||
switch (parser.peek() == null ? -1 : parser.peek().getType()) {
|
||||
case T_ASSIGN:
|
||||
parser.next();
|
||||
return new ParsedExpressionAssign(position, left, readAssignExpression(parser, environment));
|
||||
case T_PLUSASSIGN:
|
||||
parser.next();
|
||||
return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, environment), OperatorType.ADD);
|
||||
case T_MINUSASSIGN:
|
||||
parser.next();
|
||||
return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, environment), OperatorType.SUB);
|
||||
case T_TILDEASSIGN:
|
||||
parser.next();
|
||||
return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, environment), OperatorType.CAT);
|
||||
case T_MULASSIGN:
|
||||
parser.next();
|
||||
return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, environment), OperatorType.MUL);
|
||||
case T_DIVASSIGN:
|
||||
parser.next();
|
||||
return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, environment), OperatorType.DIV);
|
||||
case T_MODASSIGN:
|
||||
parser.next();
|
||||
return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, environment), OperatorType.MOD);
|
||||
case T_ORASSIGN:
|
||||
parser.next();
|
||||
return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, environment), OperatorType.OR);
|
||||
case T_ANDASSIGN:
|
||||
parser.next();
|
||||
return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, environment), OperatorType.AND);
|
||||
case T_XORASSIGN:
|
||||
parser.next();
|
||||
return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, environment), OperatorType.XOR);
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
private static ParsedExpression readConditionalExpression(ZenPosition position, ZenParser 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, ZenParser 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, ZenParser 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, ZenParser 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, ZenParser 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, ZenParser 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, ZenParser parser, IEnvironmentGlobal environment) {
|
||||
ParsedExpression left = readAddExpression(position, parser, environment);
|
||||
|
||||
switch (parser.peek() == null ? -1 : parser.peek().getType()) {
|
||||
case T_EQ: {
|
||||
parser.next();
|
||||
ParsedExpression right = readAddExpression(parser.peek().getPosition(), parser, environment);
|
||||
return new ParsedExpressionCompare(position, left, right, CompareType.EQ);
|
||||
}
|
||||
case T_NOTEQ: {
|
||||
parser.next();
|
||||
ParsedExpression right = readAddExpression(parser.peek().getPosition(), parser, environment);
|
||||
return new ParsedExpressionCompare(position, left, right, CompareType.NE);
|
||||
}
|
||||
case T_LT: {
|
||||
parser.next();
|
||||
ParsedExpression right = readAddExpression(parser.peek().getPosition(), parser, environment);
|
||||
return new ParsedExpressionCompare(position, left, right, CompareType.LT);
|
||||
}
|
||||
case T_LTEQ: {
|
||||
parser.next();
|
||||
ParsedExpression right = readAddExpression(parser.peek().getPosition(), parser, environment);
|
||||
return new ParsedExpressionCompare(position, left, right, CompareType.LE);
|
||||
}
|
||||
case T_GT: {
|
||||
parser.next();
|
||||
ParsedExpression right = readAddExpression(parser.peek().getPosition(), parser, environment);
|
||||
return new ParsedExpressionCompare(position, left, right, CompareType.GT);
|
||||
}
|
||||
case T_GTEQ: {
|
||||
parser.next();
|
||||
ParsedExpression right = readAddExpression(parser.peek().getPosition(), parser, environment);
|
||||
return new ParsedExpressionCompare(position, left, right, CompareType.GE);
|
||||
}
|
||||
case T_IN: {
|
||||
parser.next();
|
||||
ParsedExpression right = readAddExpression(parser.peek().getPosition(), parser, environment);
|
||||
return new ParsedExpressionBinary(position, left, right, OperatorType.CONTAINS);
|
||||
}
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
private static ParsedExpression readAddExpression(ZenPosition position, ZenParser 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);
|
||||
left = new ParsedExpressionBinary(position, left, right, OperatorType.ADD);
|
||||
} else if (parser.optional(T_MINUS) != null) {
|
||||
ParsedExpression right = readMulExpression(parser.peek().getPosition(), parser, environment);
|
||||
left = new ParsedExpressionBinary(position, left, right, OperatorType.SUB);
|
||||
} else if (parser.optional(T_TILDE) != null) {
|
||||
ParsedExpression right = readMulExpression(parser.peek().getPosition(), parser, environment);
|
||||
left = new ParsedExpressionBinary(position, left, right, OperatorType.CAT);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
private static ParsedExpression readMulExpression(ZenPosition position, ZenParser 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);
|
||||
left = new ParsedExpressionBinary(position, left, right, OperatorType.MUL);
|
||||
} else if (parser.optional(T_DIV) != null) {
|
||||
ParsedExpression right = readUnaryExpression(parser.peek().getPosition(), parser, environment);
|
||||
left = new ParsedExpressionBinary(position, left, right, OperatorType.DIV);
|
||||
} else if (parser.optional(T_MOD) != null) {
|
||||
ParsedExpression right = readUnaryExpression(parser.peek().getPosition(), parser, environment);
|
||||
left = new ParsedExpressionBinary(position, left, right, OperatorType.MOD);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
private static ParsedExpression readUnaryExpression(ZenPosition position, ZenParser parser, IEnvironmentGlobal environment) {
|
||||
switch (parser.peek().getType()) {
|
||||
case T_NOT:
|
||||
parser.next();
|
||||
return new ParsedExpressionUnary(
|
||||
position,
|
||||
readUnaryExpression(parser.peek().getPosition(), parser, environment),
|
||||
OperatorType.NOT);
|
||||
case T_MINUS:
|
||||
parser.next();
|
||||
return new ParsedExpressionUnary(
|
||||
position,
|
||||
readUnaryExpression(parser.peek().getPosition(), parser, environment),
|
||||
OperatorType.NEG);
|
||||
default:
|
||||
return readPostfixExpression(position, parser, environment);
|
||||
}
|
||||
}
|
||||
|
||||
private static ParsedExpression readPostfixExpression(ZenPosition position, ZenParser parser, IEnvironmentGlobal environment) {
|
||||
ParsedExpression base = readPrimaryExpression(position, parser, environment);
|
||||
|
||||
while (true) {
|
||||
if (parser.optional(T_DOT) != null) {
|
||||
Token indexString = parser.optional(T_ID);
|
||||
if (indexString != null) {
|
||||
base = new ParsedExpressionMember(position, base, indexString.getValue());
|
||||
} else {
|
||||
Token indexString2 = parser.optional(T_STRING);
|
||||
if (indexString2 != null) {
|
||||
base = new ParsedExpressionMember(position, base, unescapeString(indexString2.getValue()));
|
||||
} else {
|
||||
Token last = parser.next();
|
||||
throw new ParseException(last, "Invalid expression, last token: " + last.getValue());
|
||||
}
|
||||
}
|
||||
} else if (parser.optional(T_DOT2) != null) {
|
||||
//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);
|
||||
base = new ParsedExpressionIndex(position, base, index);
|
||||
parser.required(T_SQBRCLOSE, "] expected");
|
||||
} else if (parser.optional(T_BROPEN) != null) {
|
||||
ArrayList<ParsedExpression> arguments = new ArrayList<ParsedExpression>();
|
||||
if (parser.optional(T_BRCLOSE) == null) {
|
||||
arguments.add(readAssignExpression(parser, environment));
|
||||
while (parser.optional(T_COMMA) != null) {
|
||||
arguments.add(readAssignExpression(parser, environment));
|
||||
}
|
||||
parser.required(T_BRCLOSE, ") expected");
|
||||
}
|
||||
base = new ParsedExpressionCall(position, base, arguments);
|
||||
} else if (parser.optional(T_AS) != null) {
|
||||
ZenType type = ZenType.read(parser, environment);
|
||||
base = new ParsedExpressionCast(position, base, type);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
private static ParsedExpression readPrimaryExpression(ZenPosition position, ZenParser parser, IEnvironmentGlobal environment) {
|
||||
switch (parser.peek().getType()) {
|
||||
case T_INTVALUE:
|
||||
return new ParsedExpressionValue(
|
||||
position,
|
||||
new ExpressionInt(position, Long.parseLong(parser.next().getValue()), ZenTypeInt.INSTANCE));
|
||||
case T_FLOATVALUE:
|
||||
return new ParsedExpressionValue(
|
||||
position,
|
||||
new ExpressionFloat(position, Double.parseDouble(parser.next().getValue()), ZenTypeDouble.INSTANCE));
|
||||
case T_STRINGVALUE:
|
||||
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_ID:
|
||||
return new ParsedExpressionVariable(position, parser.next().getValue());
|
||||
// TODO: implement function values
|
||||
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 {
|
||||
String name = parser.required(T_ID, "identifier expected").getValue();
|
||||
ZenType type = ZenTypeAny.INSTANCE;
|
||||
|
||||
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");
|
||||
|
||||
Statement[] statements;
|
||||
if (parser.optional(T_ACLOSE) != null) {
|
||||
statements = new Statement[0];
|
||||
} else {
|
||||
ArrayList<Statement> statementsAL = new ArrayList<Statement>();
|
||||
|
||||
while (parser.optional(T_ACLOSE) == null) {
|
||||
statementsAL.add(Statement.read(parser, environment));
|
||||
}
|
||||
statements = statementsAL.toArray(new Statement[statementsAL.size()]);
|
||||
}
|
||||
return new ParsedExpressionValue(position, new ExpressionFunction(position, arguments, returnType, statements));
|
||||
case T_LT: {
|
||||
Token start = parser.next();
|
||||
List<Token> tokens = new ArrayList<Token>();
|
||||
Token next = parser.next();
|
||||
while (next.getType() != ZenParser.T_GT) {
|
||||
tokens.add(next);
|
||||
next = parser.next();
|
||||
}
|
||||
return new ParsedExpressionValue(
|
||||
start.getPosition(),
|
||||
parser.getEnvironment().getBracketed(tokens).instance(start.getPosition()));
|
||||
}
|
||||
case T_SQBROPEN: {
|
||||
parser.next();
|
||||
List<ParsedExpression> contents = new ArrayList<ParsedExpression>();
|
||||
if (parser.optional(T_SQBRCLOSE) == null) {
|
||||
while (parser.optional(T_SQBRCLOSE) == null) {
|
||||
contents.add(readAssignExpression(parser, environment));
|
||||
if (parser.optional(T_COMMA) == null) {
|
||||
parser.required(T_SQBRCLOSE, "] or , expected");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return new ParsedExpressionArray(position, contents);
|
||||
}
|
||||
case T_AOPEN: {
|
||||
parser.next();
|
||||
|
||||
List<ParsedExpression> keys = new ArrayList<ParsedExpression>();
|
||||
List<ParsedExpression> values = new ArrayList<ParsedExpression>();
|
||||
if (parser.optional(T_ACLOSE) == null) {
|
||||
while (parser.optional(T_ACLOSE) == null) {
|
||||
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 ParsedExpressionTable(position, keys, values);
|
||||
}
|
||||
}
|
||||
case T_TRUE:
|
||||
parser.next();
|
||||
return new ParsedExpressionBool(position, true);
|
||||
case T_FALSE:
|
||||
parser.next();
|
||||
return new ParsedExpressionBool(position, false);
|
||||
case T_NULL:
|
||||
parser.next();
|
||||
return new ParsedExpressionNull(position);
|
||||
case T_BROPEN:
|
||||
parser.next();
|
||||
ParsedExpression result = readAssignExpression(parser, environment);
|
||||
parser.required(T_BRCLOSE, ") expected");
|
||||
return result;
|
||||
default:
|
||||
Token last = parser.next();
|
||||
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);
|
||||
|
||||
public Expression compileKey(IEnvironmentMethod environment) {
|
||||
return compile(environment).eval(environment);
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.ExpressionOrOr;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
return new ExpressionOrOr(
|
||||
getPosition(),
|
||||
left.compile(environment).eval(environment),
|
||||
right.compile(environment).eval(environment));
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import java.util.List;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionArray;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ParsedExpressionArray extends ParsedExpression {
|
||||
private final List<ParsedExpression> contents;
|
||||
|
||||
public ParsedExpressionArray(ZenPosition position, List<ParsedExpression> contents) {
|
||||
super(position);
|
||||
|
||||
this.contents = contents;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
Expression[] cContents = new Expression[contents.size()];
|
||||
for (int i = 0; i < contents.size(); i++) {
|
||||
cContents[i] = contents.get(i).compile(environment).eval(environment);
|
||||
}
|
||||
return new ExpressionArray(getPosition(), cContents);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression compileKey(IEnvironmentMethod environment) {
|
||||
if (contents.size() == 1 && contents.get(0) instanceof ParsedExpressionVariable) {
|
||||
return contents.get(0).compile(environment).eval(environment);
|
||||
} else {
|
||||
return compile(environment).eval(environment);
|
||||
}
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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) {
|
||||
IPartialExpression cLeft = left.compile(environment);
|
||||
return cLeft
|
||||
.assign(getPosition(), environment,
|
||||
right.compile(environment).eval(environment));
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
Expression cLeft = left.compile(environment).eval(environment);
|
||||
Expression cRight = right.compile(environment).eval(environment);
|
||||
return cLeft.getType().binary(getPosition(), environment, cLeft, cRight, operator);
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.ExpressionBool;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ParsedExpressionBool extends ParsedExpression {
|
||||
private final boolean value;
|
||||
|
||||
public ParsedExpressionBool(ZenPosition position, boolean value) {
|
||||
super(position);
|
||||
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
return new ExpressionBool(getPosition(), value);
|
||||
}
|
||||
}
|
||||
Loaded 100 of 281 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user