- Added documentation to some classes

- Created script provider system
- Did some refactoring to improve the API
This commit is contained in:
Stan Hebben committed 2014-06-10 21:12:55 +02:00
1 parent 38b78ce305
commit 97c7f42400
50 files changed
+1311 -318

No files matched your search

@@ -1,19 +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;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
* Error logger. Implementations can forward errors to their own error logging
* system.
*
* @author Stan Hebben
*/
public interface IZenErrorLogger {
/**
* Called when an error is detected during compilation.
*
* @param position error position
* @param message error message
*/
public void error(ZenPosition position, String message);
/**
* Called when a warning is generated during compilation.
*
* @param position warning position
* @param message warning message
*/
public void warning(ZenPosition position, String message);
}
@@ -1,9 +1,3 @@
/*
* 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;
@@ -23,7 +17,6 @@ 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;
@@ -33,8 +26,11 @@ import stanhebben.zenscript.type.natives.ZenNativeOperator;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
* Type expansions provide additional members for existing types. They can
* add members, casters or operators and these will be available to all compiling
* scripts.
*
* @author Stan Hebben
*/
public class TypeExpansion {
private final Map<String, ZenExpandMember> members;
@@ -44,6 +40,10 @@ public class TypeExpansion {
private final List<ZenNativeOperator> binaryOperators;
private final List<ZenNativeOperator> unaryOperators;
/**
* Creates an empty type expansion. Use the expand method to register the
* actual expansions.
*/
public TypeExpansion() {
members = new HashMap<String, ZenExpandMember>();
staticMembers = new HashMap<String, ZenExpandMember>();
@@ -53,6 +53,15 @@ public class TypeExpansion {
unaryOperators = new ArrayList<ZenNativeOperator>();
}
/**
* Added a native type expansion. The provided class must contain the proper
* annotations for the required expansions, else nothing will happen. Each
* expansion method must be static and accept the expanded type as first
* argument.
*
* @param cls expanding class
* @param types type registry
*/
public void expand(Class cls, ITypeRegistry types) {
for (Method method : cls.getMethods()) {
String methodName = method.getName();
@@ -149,7 +158,17 @@ public class TypeExpansion {
}
}
public ZenExpandCaster getCaster(ZenType type, IEnvironmentGlobal environment) {
/**
* Retrieves a caster from this expansion. May return null if no suitable
* caster was available.
*
* @param type target type
* @param environment compilation environment
* @return caster, or null if there is no suitable caster
*/
public ZenExpandCaster getCaster(
ZenType type,
IEnvironmentGlobal environment) {
for (ZenExpandCaster caster : casters) {
if (caster.getTarget().equals(type)) {
return caster;
@@ -164,7 +183,21 @@ public class TypeExpansion {
return null;
}
public Expression unary(ZenPosition position, IEnvironmentGlobal environment, Expression value, OperatorType operator) {
/**
* Retrieves a unary operator from this expansion. May return null if no
* suitable operator was available.
*
* @param position calling position
* @param environment compile environment
* @param value argument value
* @param operator unary operator
* @return the resulting expression, or null if no operator was available
*/
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);
@@ -174,7 +207,23 @@ public class TypeExpansion {
return null;
}
public Expression binary(ZenPosition position, IEnvironmentGlobal environment, Expression left, Expression right, OperatorType operator) {
/**
* Retrieves a binary operator from this expansion. May return null if no
* suitable operator was available.
*
* @param position calling position
* @param environment compile environment
* @param left first argument value (left side)
* @param right second argument value (right side)
* @param operator binary operator
* @return the resulting expression, or null if no operator was available
*/
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);
@@ -184,7 +233,25 @@ public class TypeExpansion {
return null;
}
public Expression trinary(ZenPosition position, IEnvironmentGlobal environment, Expression first, Expression second, Expression third, OperatorType operator) {
/**
* Retrieves a ternary operator from this expansion. May return null if no
* suitable operator was available.
*
* @param position calling position
* @param environment compile environment
* @param first first argument value
* @param second second argument value
* @param third third arugment value
* @param operator ternary operator
* @return the resulting expression, or null if no operator was available
*/
public Expression ternary(
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);
@@ -194,7 +261,21 @@ public class TypeExpansion {
return null;
}
public IPartialExpression member(ZenPosition position, IEnvironmentGlobal environment, Expression value, String member) {
/**
* Retrieves an instance member from this expansion. May return null if no
* suitable member was available.
*
* @param position calling position
* @param environment compile environment
* @param value instance value
* @param member member name
* @return resulting member expression, or null if no such member was available
*/
public IPartialExpression instanceMember(
ZenPosition position,
IEnvironmentGlobal environment,
Expression value,
String member) {
if (members.containsKey(member)) {
return members.get(member).instance(position, environment, value);
}
@@ -202,6 +283,15 @@ public class TypeExpansion {
return null;
}
/**
* Retrieves a static member from this expansion. May return null if no
* suitable member was available.
*
* @param position calling position
* @param environment compile environment
* @param member member name
* @return resulting static member expression, or null if no such member was available
*/
public IPartialExpression staticMember(ZenPosition position, IEnvironmentGlobal environment, String member) {
if (staticMembers.containsKey(member)) {
return staticMembers.get(member).instance(position, environment);
@@ -210,6 +300,15 @@ public class TypeExpansion {
return null;
}
// #######################
// ### Private methods ###
// #######################
/**
* Checks if the given method is static. Throws an exception if not.
*
* @param method metod to validate
*/
private void checkStatic(Method method) {
if ((method.getModifiers() & Modifier.STATIC) == 0) {
throw new RuntimeException("Expansion method " + method.getName() + " must be static");
@@ -39,13 +39,26 @@ import stanhebben.zenscript.util.StringUtil;
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
/**
*
* @author Stanneke
* Main module class. Contains a compiled module. The static methods of this
* class can be used to compile a specific file or files.
*
* Modules may contain statements in their source, or define functions. Functions
* in different scripts within the same module are accessible to each other, but
* not to other modules.
*
* @author Stan Hebben
*/
public class ZenModule {
public static void compileScripts(String mainclass, String mainFileName, List<ZenParsedFile> scripts, IEnvironmentGlobal environmentGlobal) {
/**
* Compiles a set of parsed files into a module.
*
* @param mainFileName main filename (used for debug info)
* @param scripts scripts to compile
* @param environmentGlobal global compile environment
*/
public static void compileScripts(String mainFileName, List<ZenParsedFile> scripts, IEnvironmentGlobal environmentGlobal) {
ClassWriter clsMain = new ClassWriter(ClassWriter.COMPUTE_MAXS);
clsMain.visitSource(mainclass, null);
clsMain.visitSource(mainFileName, 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);
@@ -159,6 +172,14 @@ public class ZenModule {
environmentGlobal.putClass("__ZenMain__", clsMain.toByteArray());
}
/**
* Compiles a single script file.
*
* @param single file to be compiled
* @param environment compile environment
* @return compiled module
* @throws IOException if the file could not be read
*/
public static ZenModule compileScriptFile(File single, IZenCompileEnvironment environment) throws IOException {
Map<String, byte[]> classes = new HashMap<String, byte[]>();
ClassNameGenerator nameGen = new ClassNameGenerator();
@@ -173,14 +194,14 @@ public class ZenModule {
FileInputStream input = new FileInputStream(single);
Reader reader = new InputStreamReader(new BufferedInputStream(input));
ZenParser parser = new ZenParser(reader, environment);
ZenTokener parser = new ZenTokener(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);
compileScripts(filename, files, environmentGlobal);
// debug: output classes
File outputDir = new File("generated");
@@ -196,6 +217,16 @@ public class ZenModule {
return new ZenModule(classes);
}
/**
* Compiles a zip file as module. All containing files (inside the given
* subdirectory) will be compiled.
*
* @param file zip file
* @param subdir subdirectory (use empty string to compile all)
* @param environment compile environment
* @return compiled module
* @throws IOException if the file could not be read properly
*/
public static ZenModule compileZip(File file, String subdir, IZenCompileEnvironment environment) throws IOException {
Map<String, byte[]> classes = new HashMap<String, byte[]>();
ClassNameGenerator nameGen = new ClassNameGenerator();
@@ -217,7 +248,7 @@ public class ZenModule {
String className = extractClassName(filename);
Reader reader = new InputStreamReader(new BufferedInputStream(zipFile.getInputStream(entry)));
ZenParser parser = new ZenParser(reader, environment);
ZenTokener parser = new ZenTokener(reader, environment);
ZenParsedFile pfile = new ZenParsedFile(filename, className, parser, environmentGlobal);
files.add(pfile);
reader.close();
@@ -225,8 +256,7 @@ public class ZenModule {
}
String filename = file.getName();
String className = extractClassName(filename);
compileScripts(className, filename, files, environmentGlobal);
compileScripts(filename, files, environmentGlobal);
// debug: output classes
File outputDir = new File("generated");
@@ -245,11 +275,23 @@ public class ZenModule {
private final Map<String, byte[]> classes;
private final MyClassLoader classLoader;
private ZenModule(Map<String, byte[]> classes) {
/**
* Constructs a module for the given set of classes. Mostly intended for
* internal use.
*
* @param classes
*/
public ZenModule(Map<String, byte[]> classes) {
this.classes = classes;
classLoader = new MyClassLoader();
}
/**
* Retrieves the main runnable. Running this runnable will execute the content
* of the given module.
*
* @return main runnable
*/
public Runnable getMain() {
try {
return (Runnable) classLoader.loadClass("__ZenMain__").newInstance();
@@ -262,18 +304,17 @@ public class ZenModule {
}
}
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);
}
}
// ######################
// ### Static methods ###
// ######################
private static String extractClassName(String filename) {
/**
* Converts a filename into a class name.
*
* @param filename filename to convert
* @return class name
*/
public static String extractClassName(String filename) {
filename = filename.replace('\\', '/');
if (filename.startsWith("/")) filename = filename.substring(1);
@@ -291,4 +332,22 @@ public class ZenModule {
return filename.substring(0, 1).toUpperCase() + filename.substring(1);
}
}
// #############################
// ### Private inner classes ###
// #############################
/**
* Custom class loader. Loads classes from this module.
*/
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);
}
}
}
@@ -1,16 +1,10 @@
/*
* 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 static stanhebben.zenscript.ZenTokener.*;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.definitions.Import;
import stanhebben.zenscript.definitions.ParsedFunction;
@@ -18,8 +12,19 @@ import stanhebben.zenscript.parser.Token;
import stanhebben.zenscript.statements.Statement;
/**
*
* @author Stanneke
* Contains a parsed file.
*
* A parsed file contains:
* <ul>
* <li>A set of imports</li>
* <li>A set of parsed functions</li>
* <li>A set of statuments</li>
* </ul>
*
* This parsed file cannot be executed by itself, but it can be compiled into
* a module, possibly together with other files.
*
* @author Stan Hebben
*/
public class ZenParsedFile {
private final String filename;
@@ -28,7 +33,15 @@ public class ZenParsedFile {
private final Map<String, ParsedFunction> functions;
private final List<Statement> statements;
public ZenParsedFile(String filename, String classname, ZenParser parser, IEnvironmentGlobal environment) {
/**
* Constructs and parses a given file.
*
* @param filename parsed filename
* @param classname output class name
* @param tokener input tokener
* @param environment compile environment
*/
public ZenParsedFile(String filename, String classname, ZenTokener tokener, IEnvironmentGlobal environment) {
this.filename = filename;
this.classname = classname;
@@ -36,61 +49,86 @@ public class ZenParsedFile {
functions = new HashMap<String, ParsedFunction>();
statements = new ArrayList<Statement>();
parser.setFile(this);
tokener.setFile(this);
while (parser.peek() != null && parser.peek().getType() == T_IMPORT) {
Token start = parser.next();
while (tokener.peek() != null && tokener.peek().getType() == T_IMPORT) {
Token start = tokener.next();
List<String> importName = new ArrayList<String>();
Token tName = parser.required(T_ID, "identifier expected");
Token tName = tokener.required(T_ID, "identifier expected");
importName.add(tName.getValue());
while (parser.optional(T_DOT) != null) {
Token tNamePart = parser.required(T_ID, "identifier expected");
while (tokener.optional(T_DOT) != null) {
Token tNamePart = tokener.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");
if (tokener.optional(T_AS) != null) {
Token tRename = tokener.required(T_ID, "identifier expected");
rename = tRename.getValue();
}
parser.required(T_SEMICOLON, "; expected");
tokener.required(T_SEMICOLON, "; expected");
imports.add(new Import(start.getPosition(), importName, rename));
}
while (parser.hasNext()) {
Token next = parser.peek();
while (tokener.hasNext()) {
Token next = tokener.peek();
if (next.getType() == T_FUNCTION) {
ParsedFunction function = ParsedFunction.parse(parser, environment);
ParsedFunction function = ParsedFunction.parse(tokener, 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));
statements.add(Statement.read(tokener, environment));
}
}
}
/**
* Gets the output classname for this file.
*
* @return output classname
*/
public String getClassName() {
return classname;
}
/**
* Gets the input filename for this file.
*
* @return input filename
*/
public String getFileName() {
return filename;
}
/**
* Gets the imports list.
*
* @return imports list
*/
public List<Import> getImports() {
return imports;
}
/**
* Gets this file's script statements.
*
* @return script statement list
*/
public List<Statement> getStatements() {
return statements;
}
/**
* Gets the functions defined inside this file.
*
* @return script functions
*/
public Map<String, ParsedFunction> getFunctions() {
return functions;
}
@@ -1,17 +0,0 @@
/*
* 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;
}
@@ -1,14 +1,11 @@
/*
* 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
* ZenRuntimeExceptions are thrown when an internal error occurs during
* execution of scripts. Since ZenScript is only partially typesafe, errors
* may occur at runtime due to impossible operations being executed.
*
* @author Stan Hebben
*/
public class ZenRuntimeException extends RuntimeException {
public ZenRuntimeException(String message) {
@@ -10,7 +10,13 @@ import stanhebben.zenscript.parser.Token;
import stanhebben.zenscript.parser.TokenStream;
import stanhebben.zenscript.symbols.IZenCompileEnvironment;
public class ZenParser extends TokenStream {
/**
* A tokener is capable of splitting a single file into tokens. It's intended
* for use by LL(*) parsers.
*
* @author Stan Hebben
*/
public class ZenTokener extends TokenStream {
private static final HashMap<String, Integer> KEYWORDS;
public static final int T_ID = 1;
@@ -222,22 +228,45 @@ public class ZenParser extends TokenStream {
private final IZenCompileEnvironment environment;
public ZenParser(Reader contents, IZenCompileEnvironment environment) throws IOException {
/**
* Constructs a tokener from the given reader.
*
* @param contents file reader
* @param environment compile environment
* @throws IOException if the file could not be read properly
*/
public ZenTokener(Reader contents, IZenCompileEnvironment environment) throws IOException {
super(contents, DFA);
this.environment = environment;
}
public ZenParser(String contents, IZenCompileEnvironment environment) throws IOException {
/**
* Constructs a tokener from the given string.
*
* @param contents content string
* @param environment compile environment
* @throws IOException shouldn't happen
*/
public ZenTokener(String contents, IZenCompileEnvironment environment) throws IOException {
super(new StringReader(contents), DFA);
this.environment = environment;
}
/**
* Retrieves the compile environment of this tokener.
*
* @return compile environment
*/
public IZenCompileEnvironment getEnvironment() {
return environment;
}
// ##################################
// ### TokenStream implementation ###
// ##################################
@Override
public Token process(Token token) {
if (token.getType() == T_ID) {
@@ -1,17 +1,11 @@
/*
* 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
* Used to indicate comparison types.
*
* @author Stan Hebben
*/
public enum CompareType {
FULL,
LT,
GT,
EQ,
@@ -1,9 +1,3 @@
/*
* 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;
@@ -1,9 +1,3 @@
/*
* 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;
@@ -1,9 +1,3 @@
/*
* 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;
@@ -12,9 +6,10 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Indicates a simple iterable. Should implement the iterable interface.
* Indicates a simple iterable. Annotated class must implement the iterable
* interface.
*
* @author Stanneke
* @author Stan Hebben
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@@ -1,9 +1,3 @@
/*
* 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;
@@ -12,8 +6,9 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* @author Stanneke
* Indicates that the given method parameter cannot be null.
*
* @author Stan Hebben
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
@@ -1,14 +1,9 @@
/*
* 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
* Enum used to indicate operator type.
*
* @author Stan Hebben
*/
public enum OperatorType {
ADD,
@@ -1,9 +1,3 @@
/*
* 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;
@@ -1,9 +1,3 @@
/*
* 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;
@@ -15,9 +9,13 @@ import java.lang.annotation.Target;
* Denotes a caster. Casters are capable of converting one type into another.
* They implement the 'as' operator.
*
* For a native class, no arguments are provided. For an expansion method, the
* argument is the source value.
*
* @author Stan Hebben
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ZenCaster {
}
@@ -1,9 +1,3 @@
/*
* 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;
@@ -20,5 +14,11 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface ZenClass {
/**
* Contains the class' package and name. If omitted, the java package and
* name will be used.
*
* @return
*/
String value() default "";
}
@@ -1,9 +1,3 @@
/*
* 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;
@@ -12,11 +6,29 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* @author Stanneke
* Indicates that the given class contains an expansion for a certain type.
* The expansions themselves are given through annotated static methods.
*
* @author Stan Hebben
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface ZenExpansion {
/**
* Contains the type of the annotated class. May be any kind of ZenScript type.
*
* Examples:
* <ul>
* <li>int</li>
* <li>my.package.MyClass</li>
* <li>my.package.MyClass[]</li>
* <li>any</li>
* <li>any[]</li>
* <li>any[any]</li>
* </li>
* </ul>
*
* @return expanded type
*/
public String value();
}
@@ -1,9 +1,3 @@
/*
* 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;
@@ -15,10 +9,18 @@ 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.
*
* For a native class, no arguments are provided. For an expansion, a single
* argument with the source value is provided.
*
* @author Stan Hebben
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ZenGetter {
/**
* Getter name. If omitted, the method name will be used as value name.
*
* @return getter name
*/
String value() default "";
}
@@ -1,9 +1,3 @@
/*
* 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;
@@ -12,8 +6,15 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* @author Stan
* Indicates a member getter. A member getter is a special kind of operator which
* is a fallback if a member could not be resolved. Can be used to make contents
* available as if they were members.
*
* If this method is a native class, it accepts a single string parameter with
* the member name. If this method is an expansion method, it accepts the source
* value and the member name.
*
* @author Stan Hebben
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@@ -1,9 +1,3 @@
/*
* 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;
@@ -12,8 +6,15 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* @author Stan
* Used to indicate a member setter. Companion to member getters, but allow
* setting contents of this object as if they were members.
*
* If this method is a native class, it accepts a string parameter with
* the member name and a second parameter with the value. If this method is an
* expansion method, it accepts the source value, the member name and the value
* to be assigned.
*
* @author Stan Hebben
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@@ -1,9 +1,3 @@
/*
* 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;
@@ -12,11 +6,21 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* @author Stanneke
* Used to indicate an exposed method.
*
* If this method is part of a native class, it will simply accept the parameters
* as provided. If it is part of an expansion method, it will also accept the
* instance value as first parameter.
*
* @author Stan Hebben
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ZenMethod {
/**
* Method name. If omitted, the original method name will be used.
*
* @return method name
*/
public String value() default "";
}
@@ -12,11 +12,18 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* @author Stanneke
* Used to indicate static methods for expansions. For native classes, use ZenMethod
* instead on a static method.
*
* @author Stan Hebben
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ZenMethodStatic {
/**
* Method name. If omitted, the annotated method name will be used.
*
* @return method name
*/
public String value() default "";
}
@@ -1,9 +1,3 @@
/*
* 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;
@@ -12,11 +6,17 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* @author Stanneke
* Used to indicate an operator. The arguments depend on the kind of operator.
*
* @author Stan Hebben
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ZenOperator {
/**
* Operator type for this operator.
*
* @return operator type
*/
OperatorType value();
}
@@ -1,9 +1,3 @@
/*
* 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;
@@ -12,11 +6,21 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* @author Stanneke
* Used to annotate setters. Will make the given method accessible as setter, that is,
* it's called when value.name is being assinged a value.
*
* For a native class, a single argument with the assinged value is provided.
* For an expansion, the target object and assigned value are provided.
*
* @author Stan Hebben
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ZenSetter {
/**
* Setter name. If omitted, the method name is used as property name.
*
* @return setter name
*/
String value() default "";
}
@@ -6,13 +6,11 @@
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;
/**
@@ -6,11 +6,11 @@
package stanhebben.zenscript.definitions;
import static stanhebben.zenscript.ZenParser.*;
import static stanhebben.zenscript.ZenTokener.*;
import java.util.ArrayList;
import java.util.List;
import stanhebben.zenscript.ZenParser;
import stanhebben.zenscript.ZenTokener;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.parser.Token;
import stanhebben.zenscript.statements.Statement;
@@ -23,9 +23,9 @@ import stanhebben.zenscript.util.ZenPosition;
* @author Stanneke
*/
public class ParsedFunction {
public static ParsedFunction parse(ZenParser parser, IEnvironmentGlobal environment) {
public static ParsedFunction parse(ZenTokener parser, IEnvironmentGlobal environment) {
parser.next();
Token tName = parser.required(ZenParser.T_ID, "identifier expected");
Token tName = parser.required(ZenTokener.T_ID, "identifier expected");
// function (argname [as type], argname [as type], ...) [as type] { ...contents... }
parser.required(T_BROPEN, "( expected");
@@ -0,0 +1,26 @@
package stanhebben.zenscript.docs;
/*
* 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.
*/
import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.RootDoc;
/**
*
* @author Stan
*/
public class ZenScriptDoclet {
public static boolean start(RootDoc root) {
ClassDoc[] classes = root.classes();
for (int i = 0; i < classes.length; ++i) {
System.out.println(classes[i]);
}
return true;
}
}
@@ -1,7 +1,7 @@
package stanhebben.zenscript.expression;
import org.objectweb.asm.Label;
import stanhebben.zenscript.ZenParser;
import stanhebben.zenscript.ZenTokener;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.partial.IPartialExpression;
@@ -12,7 +12,7 @@ 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) {
public static final Expression parse(ZenTokener parser, IEnvironmentMethod environment) {
return ParsedExpression.read(parser, environment)
.compile(environment)
.eval(environment);
@@ -0,0 +1,40 @@
/**
* <h1>ZenScript library</h1>
*
* The goal of ZenScript was to provide a scripting system that fit several
* requirements:
* <ul>
* <li>Be usable by people without any prior programming knowledge, for simple tasks</li>
* <li>Be usable for advanced tasks by experienced programmers</li>
* <li>Integrate easily with Java classes</li>
* <li>Provide sufficient syntax and libraries to integrate with its environment</li>
* <li>Be clear and concise in its operation</li>
* <li>Be typesafe, such that both small and large applications can be made with it</li>
* <li>It should be efficient to run</li>
* </ul>
*
* ZenScript originated from MineTweaker where I needed a simple programming
* language that would allow users without programming knowledge to be able
* to execute simple commands by following the tutorials for it.
*
* Originally MineTweaker had a simple one-line-at-a-time parsed scripting system,
* but it quickly became clear that it wasn't flexible enough, so a simple parsed
* language was created. This parsed language worked quite well but was very
* inefficient as each value was wrapped into its own object. As I wanted to
* expand the capabilities, this language was not very efficient.
*
* Additionally, it wasn't type-safe. There were no type definitions. On one hand,
* this is handy, since I didn't want to bother people with types. Following the
* tutorial was easy and the code was short. However, this gives a problem for the
* more experienced user, wanting to go beyond the basic tutorials: what are these
* values I'm working with, which properties does it have and which methods are
* available no it? It's not clear.
*
* As a balance, ZenScript allows mixed typed and typeless behavior. You don't
* need to define types anywhere - the compile will infer them where possible
* and exhibit typeless behavior when the type is effectively unknown. In nearly
* all the cases, the type is perfectly known and execution runs at native java
* speed. Since there are types, they can be documented and enforced.
*/
package stanhebben.zenscript;
@@ -8,8 +8,8 @@ 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.ZenTokener;
import static stanhebben.zenscript.ZenTokener.*;
import stanhebben.zenscript.annotations.CompareType;
import stanhebben.zenscript.annotations.OperatorType;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
@@ -36,11 +36,11 @@ import stanhebben.zenscript.util.ZenPosition;
* @author Stanneke
*/
public abstract class ParsedExpression {
public static ParsedExpression read(ZenParser parser, IEnvironmentGlobal environment) {
public static ParsedExpression read(ZenTokener parser, IEnvironmentGlobal environment) {
return readAssignExpression(parser, environment);
}
private static ParsedExpression readAssignExpression(ZenParser parser, IEnvironmentGlobal environment) {
private static ParsedExpression readAssignExpression(ZenTokener parser, IEnvironmentGlobal environment) {
ZenPosition position = parser.peek().getPosition();
ParsedExpression left = readConditionalExpression(position, parser, environment);
@@ -80,7 +80,7 @@ public abstract class ParsedExpression {
return left;
}
private static ParsedExpression readConditionalExpression(ZenPosition position, ZenParser parser, IEnvironmentGlobal environment) {
private static ParsedExpression readConditionalExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
ParsedExpression left = readOrOrExpression(position, parser, environment);
if (parser.optional(T_QUEST) != null) {
@@ -93,7 +93,7 @@ public abstract class ParsedExpression {
return left;
}
private static ParsedExpression readOrOrExpression(ZenPosition position, ZenParser parser, IEnvironmentGlobal environment) {
private static ParsedExpression readOrOrExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
ParsedExpression left = readAndAndExpression(position, parser, environment);
while (parser.optional(T_OR2) != null) {
@@ -103,7 +103,7 @@ public abstract class ParsedExpression {
return left;
}
private static ParsedExpression readAndAndExpression(ZenPosition position, ZenParser parser, IEnvironmentGlobal environment) {
private static ParsedExpression readAndAndExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
ParsedExpression left = readOrExpression(position, parser, environment);
while (parser.optional(T_AND2) != null) {
@@ -113,7 +113,7 @@ public abstract class ParsedExpression {
return left;
}
private static ParsedExpression readOrExpression(ZenPosition position, ZenParser parser, IEnvironmentGlobal environment) {
private static ParsedExpression readOrExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
ParsedExpression left = readXorExpression(position, parser, environment);
while (parser.optional(T_OR) != null) {
@@ -123,7 +123,7 @@ public abstract class ParsedExpression {
return left;
}
private static ParsedExpression readXorExpression(ZenPosition position, ZenParser parser, IEnvironmentGlobal environment) {
private static ParsedExpression readXorExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
ParsedExpression left = readAndExpression(position, parser, environment);
while (parser.optional(T_XOR) != null) {
@@ -133,7 +133,7 @@ public abstract class ParsedExpression {
return left;
}
private static ParsedExpression readAndExpression(ZenPosition position, ZenParser parser, IEnvironmentGlobal environment) {
private static ParsedExpression readAndExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
ParsedExpression left = readCompareExpression(position, parser, environment);
while (parser.optional(T_AND) != null) {
@@ -143,7 +143,7 @@ public abstract class ParsedExpression {
return left;
}
private static ParsedExpression readCompareExpression(ZenPosition position, ZenParser parser, IEnvironmentGlobal environment) {
private static ParsedExpression readCompareExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
ParsedExpression left = readAddExpression(position, parser, environment);
switch (parser.peek() == null ? -1 : parser.peek().getType()) {
@@ -187,7 +187,7 @@ public abstract class ParsedExpression {
return left;
}
private static ParsedExpression readAddExpression(ZenPosition position, ZenParser parser, IEnvironmentGlobal environment) {
private static ParsedExpression readAddExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
ParsedExpression left = readMulExpression(position, parser, environment);
while (true) {
@@ -207,7 +207,7 @@ public abstract class ParsedExpression {
return left;
}
private static ParsedExpression readMulExpression(ZenPosition position, ZenParser parser, IEnvironmentGlobal environment) {
private static ParsedExpression readMulExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
ParsedExpression left = readUnaryExpression(position, parser, environment);
while (true) {
@@ -228,7 +228,7 @@ public abstract class ParsedExpression {
return left;
}
private static ParsedExpression readUnaryExpression(ZenPosition position, ZenParser parser, IEnvironmentGlobal environment) {
private static ParsedExpression readUnaryExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
switch (parser.peek().getType()) {
case T_NOT:
parser.next();
@@ -247,7 +247,7 @@ public abstract class ParsedExpression {
}
}
private static ParsedExpression readPostfixExpression(ZenPosition position, ZenParser parser, IEnvironmentGlobal environment) {
private static ParsedExpression readPostfixExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
ParsedExpression base = readPrimaryExpression(position, parser, environment);
while (true) {
@@ -293,7 +293,7 @@ public abstract class ParsedExpression {
return base;
}
private static ParsedExpression readPrimaryExpression(ZenPosition position, ZenParser parser, IEnvironmentGlobal environment) {
private static ParsedExpression readPrimaryExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
switch (parser.peek().getType()) {
case T_INTVALUE:
return new ParsedExpressionValue(
@@ -361,7 +361,7 @@ public abstract class ParsedExpression {
Token start = parser.next();
List<Token> tokens = new ArrayList<Token>();
Token next = parser.next();
while (next.getType() != ZenParser.T_GT) {
while (next.getType() != ZenTokener.T_GT) {
tokens.add(next);
next = parser.next();
}
@@ -3,8 +3,8 @@ package stanhebben.zenscript.statements;
import java.util.ArrayList;
import java.util.List;
import stanhebben.zenscript.ZenParser;
import static stanhebben.zenscript.ZenParser.*;
import stanhebben.zenscript.ZenTokener;
import static stanhebben.zenscript.ZenTokener.*;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.parser.Token;
@@ -13,7 +13,7 @@ import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
public abstract class Statement {
public static Statement read(ZenParser parser, IEnvironmentGlobal environment) {
public static Statement read(ZenTokener parser, IEnvironmentGlobal environment) {
Token next = parser.peek();
switch (next.getType()) {
case T_AOPEN: {
@@ -3,7 +3,7 @@ package stanhebben.zenscript.type;
import java.io.IOException;
import org.objectweb.asm.Type;
import stanhebben.zenscript.TypeExpansion;
import stanhebben.zenscript.ZenParser;
import stanhebben.zenscript.ZenTokener;
import stanhebben.zenscript.annotations.CompareType;
import stanhebben.zenscript.annotations.OperatorType;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
@@ -37,52 +37,52 @@ public abstract class ZenType {
public static ZenType parse(String type, IEnvironmentGlobal environment) {
try {
ZenParser parser = new ZenParser(type, environment.getEnvironment());
ZenTokener parser = new ZenTokener(type, environment.getEnvironment());
return read(parser, environment);
} catch (IOException ex) {
return null;
}
}
public static ZenType read(ZenParser parser, IEnvironmentGlobal environment) {
public static ZenType read(ZenTokener parser, IEnvironmentGlobal environment) {
ZenType base;
Token next = parser.next();
switch (next.getType()) {
case ZenParser.T_ANY:
case ZenTokener.T_ANY:
base = ANY;
break;
case ZenParser.T_VOID:
case ZenTokener.T_VOID:
base = VOID;
break;
case ZenParser.T_BOOL:
case ZenTokener.T_BOOL:
base = BOOL;
break;
case ZenParser.T_BYTE:
case ZenTokener.T_BYTE:
base = BYTE;
break;
case ZenParser.T_SHORT:
case ZenTokener.T_SHORT:
base = SHORT;
break;
case ZenParser.T_INT:
case ZenTokener.T_INT:
base = INT;
break;
case ZenParser.T_LONG:
case ZenTokener.T_LONG:
base = LONG;
break;
case ZenParser.T_FLOAT:
case ZenTokener.T_FLOAT:
base = FLOAT;
break;
case ZenParser.T_DOUBLE:
case ZenTokener.T_DOUBLE:
base = DOUBLE;
break;
case ZenParser.T_STRING:
case ZenTokener.T_STRING:
base = STRING;
break;
case ZenParser.T_ID:
case ZenTokener.T_ID:
IPartialExpression partial = environment.getValue(next.getValue(), next.getPosition());
while (parser.optional(ZenParser.T_DOT) != null) {
Token member = parser.required(ZenParser.T_ID, "identifier expected");
while (parser.optional(ZenTokener.T_DOT) != null) {
Token member = parser.required(ZenTokener.T_ID, "identifier expected");
partial = partial.getMember(member.getPosition(), environment, member.getValue());
}
base = partial.toType(environment);
@@ -91,10 +91,10 @@ public abstract class ZenType {
throw new ParseException(next, "Unknown type: " + next.getValue());
}
while (parser.optional(ZenParser.T_SQBROPEN) != null) {
if (parser.optional(ZenParser.T_SQBRCLOSE) == null) {
while (parser.optional(ZenTokener.T_SQBROPEN) != null) {
if (parser.optional(ZenTokener.T_SQBRCLOSE) == null) {
base = new ZenTypeAssociative(base, read(parser, environment));
parser.required(ZenParser.T_SQBRCLOSE, "] expected");
parser.required(ZenTokener.T_SQBRCLOSE, "] expected");
} else {
base = new ZenTypeArrayBasic(base);
}
@@ -213,7 +213,7 @@ public abstract class ZenType {
OperatorType operator) {
TypeExpansion expansion = environment.getExpansion(getName());
if (expansion != null) {
return expansion.trinary(position, environment, first, second, third, operator);
return expansion.ternary(position, environment, first, second, third, operator);
}
return null;
}
@@ -221,7 +221,7 @@ public abstract class ZenType {
protected IPartialExpression memberExpansion(ZenPosition position, IEnvironmentGlobal environment, Expression value, String member) {
TypeExpansion expansion = environment.getExpansion(getName());
if (expansion != null) {
return expansion.member(position, environment, value, member);
return expansion.instanceMember(position, environment, value, member);
}
return null;
}