├── .gitignore ├── LICENSE ├── build.gradle ├── readme.md ├── run └── script.zs └── src ├── main └── java │ └── stanhebben │ └── zenscript │ ├── IZenCompileEnvironment.java │ ├── IZenErrorLogger.java │ ├── IZenLogger.java │ ├── IZenRegistry.java │ ├── TypeExpansion.java │ ├── ZenMain.java │ ├── ZenModule.java │ ├── ZenParsedFile.java │ ├── ZenRuntimeException.java │ ├── ZenTokener.java │ ├── annotations │ ├── CompareType.java │ ├── IterableList.java │ ├── IterableMap.java │ ├── IterableSimple.java │ ├── NotNull.java │ ├── OperatorType.java │ ├── Optional.java │ ├── ReturnsSelf.java │ ├── ZenCaster.java │ ├── ZenClass.java │ ├── ZenConstructor.java │ ├── ZenExpansion.java │ ├── ZenGetter.java │ ├── ZenMemberGetter.java │ ├── ZenMemberSetter.java │ ├── ZenMethod.java │ ├── ZenMethodStatic.java │ ├── ZenOperator.java │ ├── ZenProperty.java │ └── ZenSetter.java │ ├── compiler │ ├── ClassNameGenerator.java │ ├── EnvironmentClass.java │ ├── EnvironmentGlobal.java │ ├── EnvironmentMethod.java │ ├── EnvironmentMethodLambda.java │ ├── EnvironmentScope.java │ ├── EnvironmentScript.java │ ├── IEnvironmentClass.java │ ├── IEnvironmentGlobal.java │ ├── IEnvironmentMethod.java │ ├── ITypeRegistry.java │ ├── TypeRegistry.java │ └── ZenClassWriter.java │ ├── definitions │ ├── Import.java │ ├── ParsedExpansion.java │ ├── ParsedFunction.java │ ├── ParsedFunctionArgument.java │ ├── ParsedGlobalValue.java │ └── zenclasses │ │ ├── ParsedClassConstructor.java │ │ ├── ParsedZenClass.java │ │ ├── ParsedZenClassField.java │ │ └── ParsedZenClassMethod.java │ ├── dump │ ├── DumpUtils.java │ ├── GSONDumpableSerializer.java │ ├── IDumpConvertable.java │ ├── IDumpable.java │ ├── TreeNode.java │ ├── TreeNodeIter.java │ └── types │ │ ├── DumpClassBase.java │ │ ├── DumpDummy.java │ │ ├── DumpIJavaMethod.java │ │ ├── DumpZenType.java │ │ └── DumpZenTypeNative.java │ ├── expression │ ├── Expression.java │ ├── ExpressionAndAnd.java │ ├── ExpressionArgument.java │ ├── ExpressionArithmeticBinary.java │ ├── ExpressionArithmeticCompare.java │ ├── ExpressionArithmeticUnary.java │ ├── ExpressionArray.java │ ├── ExpressionArrayAdd.java │ ├── ExpressionArrayContains.java │ ├── ExpressionArrayGet.java │ ├── ExpressionArrayLength.java │ ├── ExpressionArrayList.java │ ├── ExpressionArrayListAdd.java │ ├── ExpressionArrayListGet.java │ ├── ExpressionArrayListRemove.java │ ├── ExpressionArrayListSet.java │ ├── ExpressionArraySet.java │ ├── ExpressionAs.java │ ├── ExpressionBool.java │ ├── ExpressionCallStatic.java │ ├── ExpressionCallVirtual.java │ ├── ExpressionCompareGeneric.java │ ├── ExpressionConditional.java │ ├── ExpressionEntryGet.java │ ├── ExpressionFloat.java │ ├── ExpressionFunction.java │ ├── ExpressionFunctionCall.java │ ├── ExpressionGetZenClassDefaultParameter.java │ ├── ExpressionGlobalGet.java │ ├── ExpressionGlobalSet.java │ ├── ExpressionInstanceOf.java │ ├── ExpressionInt.java │ ├── ExpressionIntegerRange.java │ ├── ExpressionInvalid.java │ ├── ExpressionJavaLambda.java │ ├── ExpressionJavaLambdaSimpleGeneric.java │ ├── ExpressionJavaMethodCurled.java │ ├── ExpressionJavaMethodStatic.java │ ├── ExpressionLocalGet.java │ ├── ExpressionLocalSet.java │ ├── ExpressionMap.java │ ├── ExpressionMapContains.java │ ├── ExpressionMapEntrySet.java │ ├── ExpressionMapIndexGet.java │ ├── ExpressionMapIndexSet.java │ ├── ExpressionMapSize.java │ ├── ExpressionNothing.java │ ├── ExpressionNull.java │ ├── ExpressionOrOr.java │ ├── ExpressionString.java │ ├── ExpressionStringConcat.java │ ├── ExpressionStringContains.java │ ├── ExpressionStringIndex.java │ ├── ExpressionStringMethod.java │ ├── ExpressionThis.java │ └── partial │ │ ├── ExpressionJavaStaticField.java │ │ ├── IPartialExpression.java │ │ ├── PartialExpansionCall.java │ │ ├── PartialGlobalValue.java │ │ ├── PartialJavaClass.java │ │ ├── PartialLocal.java │ │ ├── PartialPackage.java │ │ ├── PartialScriptReference.java │ │ ├── PartialStaticGenerated.java │ │ ├── PartialStaticGetter.java │ │ ├── PartialStaticMethod.java │ │ ├── PartialType.java │ │ └── PartialZSClass.java │ ├── impl │ ├── BracketHandler.java │ ├── GenericCompileEnvironment.java │ ├── GenericErrorLogger.java │ ├── GenericFunctions.java │ ├── GenericGlobalEnvironment.java │ ├── GenericLogger.java │ ├── GenericRegistry.java │ └── IBracketHandler.java │ ├── package-info.java │ ├── parser │ ├── ArrayListI.java │ ├── CharStream.java │ ├── CompiledDFA.java │ ├── DFA.java │ ├── HashMapI.java │ ├── HashMapII.java │ ├── HashSetI.java │ ├── IteratorI.java │ ├── NFA.java │ ├── ParseException.java │ ├── Token.java │ ├── TokenException.java │ ├── TokenStream.java │ └── expression │ │ ├── ParsedExpression.java │ │ ├── ParsedExpressionAndAnd.java │ │ ├── ParsedExpressionArray.java │ │ ├── ParsedExpressionAssign.java │ │ ├── ParsedExpressionBinary.java │ │ ├── ParsedExpressionBool.java │ │ ├── ParsedExpressionCall.java │ │ ├── ParsedExpressionCast.java │ │ ├── ParsedExpressionCompare.java │ │ ├── ParsedExpressionConditional.java │ │ ├── ParsedExpressionFunction.java │ │ ├── ParsedExpressionIndex.java │ │ ├── ParsedExpressionIndexSet.java │ │ ├── ParsedExpressionInstanceOf.java │ │ ├── ParsedExpressionInvalid.java │ │ ├── ParsedExpressionMap.java │ │ ├── ParsedExpressionMember.java │ │ ├── ParsedExpressionNull.java │ │ ├── ParsedExpressionOpAssign.java │ │ ├── ParsedExpressionOrOr.java │ │ ├── ParsedExpressionUnary.java │ │ ├── ParsedExpressionValue.java │ │ └── ParsedExpressionVariable.java │ ├── statements │ ├── Statement.java │ ├── StatementBlock.java │ ├── StatementBreak.java │ ├── StatementContinue.java │ ├── StatementExpression.java │ ├── StatementForeach.java │ ├── StatementIf.java │ ├── StatementNull.java │ ├── StatementReturn.java │ ├── StatementVar.java │ └── StatementWhileDo.java │ ├── symbols │ ├── IZenSymbol.java │ ├── SymbolArgument.java │ ├── SymbolCaptured.java │ ├── SymbolGlobalValue.java │ ├── SymbolJavaClass.java │ ├── SymbolJavaStaticField.java │ ├── SymbolJavaStaticGetter.java │ ├── SymbolJavaStaticMethod.java │ ├── SymbolLocal.java │ ├── SymbolPackage.java │ ├── SymbolScriptReference.java │ ├── SymbolType.java │ ├── SymbolZenClass.java │ └── SymbolZenStaticMethod.java │ ├── type │ ├── IZenIterator.java │ ├── ZenType.java │ ├── ZenTypeAny.java │ ├── ZenTypeArray.java │ ├── ZenTypeArrayBasic.java │ ├── ZenTypeArrayList.java │ ├── ZenTypeAssociative.java │ ├── ZenTypeBool.java │ ├── ZenTypeBoolObject.java │ ├── ZenTypeByte.java │ ├── ZenTypeByteObject.java │ ├── ZenTypeDouble.java │ ├── ZenTypeDoubleObject.java │ ├── ZenTypeEntry.java │ ├── ZenTypeFloat.java │ ├── ZenTypeFloatObject.java │ ├── ZenTypeFunction.java │ ├── ZenTypeFunctionCallable.java │ ├── ZenTypeInt.java │ ├── ZenTypeIntObject.java │ ├── ZenTypeIntRange.java │ ├── ZenTypeLong.java │ ├── ZenTypeLongObject.java │ ├── ZenTypeNative.java │ ├── ZenTypeNull.java │ ├── ZenTypeShort.java │ ├── ZenTypeShortObject.java │ ├── ZenTypeString.java │ ├── ZenTypeVoid.java │ ├── ZenTypeZenClass.java │ ├── casting │ │ ├── BaseCastingRule.java │ │ ├── CastingAnyBool.java │ │ ├── CastingAnyByte.java │ │ ├── CastingAnyDouble.java │ │ ├── CastingAnyFloat.java │ │ ├── CastingAnyInt.java │ │ ├── CastingAnyLong.java │ │ ├── CastingAnyShort.java │ │ ├── CastingAnyString.java │ │ ├── CastingAnySubtype.java │ │ ├── CastingNotNull.java │ │ ├── CastingRuleAny.java │ │ ├── CastingRuleAnyAs.java │ │ ├── CastingRuleArrayArray.java │ │ ├── CastingRuleArrayList.java │ │ ├── CastingRuleD2F.java │ │ ├── CastingRuleD2I.java │ │ ├── CastingRuleD2L.java │ │ ├── CastingRuleDelegateArray.java │ │ ├── CastingRuleDelegateList.java │ │ ├── CastingRuleDelegateMap.java │ │ ├── CastingRuleDelegateStaticMethod.java │ │ ├── CastingRuleF2D.java │ │ ├── CastingRuleF2I.java │ │ ├── CastingRuleF2L.java │ │ ├── CastingRuleI2B.java │ │ ├── CastingRuleI2D.java │ │ ├── CastingRuleI2F.java │ │ ├── CastingRuleI2L.java │ │ ├── CastingRuleI2S.java │ │ ├── CastingRuleL2D.java │ │ ├── CastingRuleL2F.java │ │ ├── CastingRuleL2I.java │ │ ├── CastingRuleListArray.java │ │ ├── CastingRuleListList.java │ │ ├── CastingRuleMap.java │ │ ├── CastingRuleMatchedFunction.java │ │ ├── CastingRuleNone.java │ │ ├── CastingRuleNullableStaticMethod.java │ │ ├── CastingRuleNullableVirtualMethod.java │ │ ├── CastingRuleStaticMethod.java │ │ ├── CastingRuleVirtualMethod.java │ │ ├── ICastingRule.java │ │ └── ICastingRuleDelegate.java │ ├── expand │ │ ├── ZenExpandCaster.java │ │ └── ZenExpandMember.java │ ├── iterator │ │ ├── IteratorIterable.java │ │ ├── IteratorList.java │ │ ├── IteratorMap.java │ │ ├── IteratorMapKeys.java │ │ └── IteratorWhileDo.java │ └── natives │ │ ├── IJavaMethod.java │ │ ├── JavaMethod.java │ │ ├── JavaMethodGenerated.java │ │ ├── ZenFieldMethod.java │ │ ├── ZenNativeCaster.java │ │ ├── ZenNativeMember.java │ │ └── ZenNativeOperator.java │ ├── util │ ├── AnyClassWriter.java │ ├── ArrayUtil.java │ ├── IAnyDefinition.java │ ├── MappingIterator.java │ ├── MethodCompiler.java │ ├── MethodOutput.java │ ├── Pair.java │ ├── StringUtil.java │ ├── ZenPosition.java │ └── ZenTypeUtil.java │ └── value │ ├── IAny.java │ ├── IntRange.java │ └── builtin │ └── expansion │ └── IByteExpansion.java └── test └── java └── stanhebben └── zenscript ├── MainTest.java ├── TestAssertions.java ├── TestErrorLogger.java ├── TestHelper.java └── tests ├── TestArithmeticOperators.java ├── TestArrayCasts.java ├── TestBoolOperators.java ├── TestClasses.java ├── TestDefaultArguments.java ├── TestExpansion.java ├── TestFunctionTypes.java ├── TestFunctionTypesInSignatures.java ├── TestFunctions.java ├── TestGlobals.java ├── TestLambdas.java ├── TestLoops.java ├── TestMaps.java ├── TestZenProperty.java ├── TestZenTypeArray.java └── Tests.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Package Files # 4 | *.war 5 | *.ear 6 | /build 7 | /eclipse 8 | /bin 9 | /out 10 | 11 | *.classpath 12 | *.project 13 | /.gradle 14 | /gradle 15 | /.settings 16 | /.idea 17 | *.iml 18 | *.ipr 19 | *.iws 20 | /.metadata/* 21 | *.eml 22 | */run/ 23 | usernamecache.json 24 | gradlew 25 | *.stackdump 26 | *.log 27 | */.settings 28 | */build 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Jared Luboff 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /run/script.zs: -------------------------------------------------------------------------------- 1 | var x = 5; 2 | print(x+5); 3 | 4 | for i in 0 to 10 { 5 | 6 | print(10 - i); 7 | 8 | } 9 | for i in 10 .. 20 { 10 | if i %2 == 0{ 11 | print(i); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/IZenCompileEnvironment.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript; 2 | 3 | import stanhebben.zenscript.compiler.*; 4 | import stanhebben.zenscript.parser.Token; 5 | import stanhebben.zenscript.symbols.IZenSymbol; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * @author Stanneke 11 | */ 12 | public interface IZenCompileEnvironment { 13 | 14 | IZenErrorLogger getErrorLogger(); 15 | 16 | IZenSymbol getGlobal(String name); 17 | 18 | IZenSymbol getBracketed(IEnvironmentGlobal environment, List tokens); 19 | 20 | TypeRegistry getTypeRegistry(); 21 | 22 | TypeExpansion getExpansion(String type); 23 | 24 | void setRegistry(IZenRegistry registry); 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/IZenErrorLogger.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript; 2 | 3 | import stanhebben.zenscript.util.ZenPosition; 4 | 5 | /** 6 | * Error logger. Implementations can forward errors to their own error logging 7 | * system. 8 | * 9 | * @author Stan Hebben 10 | */ 11 | public interface IZenErrorLogger extends IZenLogger { 12 | 13 | /** 14 | * Called when an error is detected during compilation. 15 | * 16 | * @param position error position 17 | * @param message error message 18 | */ 19 | void error(ZenPosition position, String message); 20 | 21 | /** 22 | * Called when a warning is generated during compilation. 23 | * 24 | * @param position warning position 25 | * @param message warning message 26 | */ 27 | void warning(ZenPosition position, String message); 28 | 29 | /** 30 | * Called to generate a info during compilation. 31 | * 32 | * @param position info position 33 | * @param message info message 34 | */ 35 | void info(ZenPosition position, String message); 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/IZenLogger.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript; 2 | 3 | public interface IZenLogger { 4 | 5 | /** 6 | * Called when an error is detected. 7 | * 8 | * @param message error message 9 | */ 10 | void error(String message); 11 | /** 12 | * Called when an error is detected. 13 | * 14 | * @param e exception to throw 15 | * @param message error message 16 | */ 17 | void error(String message, Throwable e); 18 | 19 | 20 | /** 21 | * Called when a warning is generated. 22 | * 23 | * @param message warning message 24 | */ 25 | void warning(String message); 26 | 27 | /** 28 | * Called to generate a info. 29 | * 30 | * @param message info message 31 | */ 32 | void info(String message); 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/ZenRuntimeException.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript; 2 | 3 | /** 4 | * ZenRuntimeExceptions are thrown when an internal error occurs during 5 | * execution of scripts. Since ZenScript is only partially typesafe, errors may 6 | * occur at runtime due to impossible operations being executed. 7 | * 8 | * @author Stan Hebben 9 | */ 10 | public class ZenRuntimeException extends RuntimeException { 11 | 12 | public ZenRuntimeException(String message) { 13 | super(message); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/annotations/CompareType.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.annotations; 2 | 3 | /** 4 | * Used to indicate comparison types. 5 | * 6 | * @author Stan Hebben 7 | */ 8 | public enum CompareType { 9 | LT, GT, EQ, NE, LE, GE 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/annotations/IterableList.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.annotations; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * Makes a class implementing List expose itself as iterable over keys and 7 | * values. 8 | * 9 | * @author Stan Hebben 10 | */ 11 | @Retention(RetentionPolicy.RUNTIME) 12 | @Target(ElementType.TYPE) 13 | public @interface IterableList { 14 | 15 | String value(); 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/annotations/IterableMap.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.annotations; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * Makes a class implementing Map expose itself as iterable over keys and 7 | * values. 8 | * 9 | * @author Stan Hebben 10 | */ 11 | @Retention(RetentionPolicy.RUNTIME) 12 | @Target(ElementType.TYPE) 13 | public @interface IterableMap { 14 | 15 | String key(); 16 | 17 | String value(); 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/annotations/IterableSimple.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.annotations; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * Indicates a simple iterable. Annotated class must implement the iterable 7 | * interface. 8 | * 9 | * @author Stan Hebben 10 | */ 11 | @Retention(RetentionPolicy.RUNTIME) 12 | @Target(ElementType.TYPE) 13 | public @interface IterableSimple { 14 | 15 | String value(); 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/annotations/NotNull.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.annotations; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * Indicates that the given method parameter cannot be null. 7 | * 8 | * @author Stan Hebben 9 | */ 10 | @Retention(RetentionPolicy.RUNTIME) 11 | @Target(ElementType.PARAMETER) 12 | public @interface NotNull { 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/annotations/OperatorType.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.annotations; 2 | 3 | /** 4 | * Enum used to indicate operator type. 5 | * 6 | * @author Stan Hebben 7 | */ 8 | public enum OperatorType { 9 | ADD, SUB, MUL, DIV, MOD, CAT, OR, AND, XOR, NEG, NOT, INDEXSET, INDEXGET, RANGE, CONTAINS, COMPARE, MEMBERGETTER, MEMBERSETTER, EQUALS 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/annotations/Optional.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.annotations; 2 | 3 | 4 | import java.lang.annotation.*; 5 | 6 | /** 7 | * Denotes that this parameter is optional. Inside the script, this parameter 8 | * may be omitted, in which case it is automatically filled with the default 9 | * value of that parameter type (false, null or 0). Invalid for @NonNull type. 10 | *

11 | * Alternatively, you can provide a default value using either the respecting values or a String value, a class and if needed a String methodName 12 | * In the latter case it will search for a static method named methodName in the given class that uses a String as input parameter and uses a call to that method as default value. 13 | * 14 | * @author Stanneke 15 | */ 16 | @Retention(RetentionPolicy.RUNTIME) 17 | @Target(ElementType.PARAMETER) 18 | public @interface Optional { 19 | 20 | /** 21 | * If this is provided, then the default value will be a call to methodClass.methodName(String value) 22 | */ 23 | Class methodClass() default Optional.class; 24 | 25 | /** 26 | * Value to be used as default. 27 | * If omitted, will be the Type's default value (0, null, false) 28 | *

29 | * Can either be used on its own (for Strings) or together with the other two members (for all java Objects and primitives) 30 | */ 31 | String value() default ""; 32 | 33 | /** 34 | * Used to change the name of the method for optional method call 35 | */ 36 | String methodName() default "getValue"; 37 | 38 | /** 39 | * Used for int, short, long and byte parameters 40 | */ 41 | long valueLong() default 0L; 42 | 43 | /** 44 | * Used for boolean parameters 45 | */ 46 | boolean valueBoolean() default false; 47 | 48 | /** 49 | * Used for float and double parameters 50 | */ 51 | double valueDouble() default 0.0d; 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/annotations/ReturnsSelf.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.annotations; 2 | 3 | import java.lang.annotation.*; 4 | 5 | @Retention(RetentionPolicy.RUNTIME) 6 | @Target(ElementType.METHOD) 7 | public @interface ReturnsSelf {} 8 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/annotations/ZenCaster.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.annotations; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * Denotes a caster. Casters are capable of converting one type into another. 7 | * They implement the 'as' operator. 8 | *

9 | * For a native class, no arguments are provided. For an expansion method, the 10 | * argument is the source value. 11 | * 12 | * @author Stan Hebben 13 | */ 14 | @Retention(RetentionPolicy.RUNTIME) 15 | @Target(ElementType.METHOD) 16 | public @interface ZenCaster { 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/annotations/ZenClass.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.annotations; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * Exposes this class to ZenScript. The class can then be imported and accessed 7 | * into any script. 8 | * 9 | * @author Stan Hebben 10 | */ 11 | @Retention(RetentionPolicy.RUNTIME) 12 | @Target({ElementType.TYPE, ElementType.METHOD}) 13 | public @interface ZenClass { 14 | 15 | /** 16 | * Contains the class' package and name. If omitted, the java package and 17 | * name will be used. 18 | * 19 | * @return 20 | */ 21 | String value() default ""; 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/annotations/ZenConstructor.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.annotations; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * Used to indicate an exposed constructor. 7 | *

8 | * @author kindlich 9 | */ 10 | @Retention(RetentionPolicy.RUNTIME) 11 | @Target(ElementType.CONSTRUCTOR) 12 | public @interface ZenConstructor {} 13 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/annotations/ZenExpansion.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.annotations; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * Indicates that the given class contains an expansion for a certain type. The 7 | * expansions themselves are given through annotated static methods. 8 | * 9 | * @author Stan Hebben 10 | */ 11 | @Retention(RetentionPolicy.RUNTIME) 12 | @Target({ElementType.TYPE, ElementType.METHOD}) 13 | public @interface ZenExpansion { 14 | 15 | /** 16 | * Contains the type of the annotated class. May be any kind of ZenScript 17 | * type. 18 | *

19 | * Examples: 20 | *

29 | * 30 | * @return expanded type 31 | */ 32 | String value(); 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/annotations/ZenGetter.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.annotations; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * Indicates a getter. Will make the given method accessible as getter, that is, 7 | * it's called when value.name is being used. 8 | *

9 | * For a native class, no arguments are provided. For an expansion, a single 10 | * argument with the source value is provided. 11 | * 12 | * @author Stan Hebben 13 | */ 14 | @Retention(RetentionPolicy.RUNTIME) 15 | @Target(ElementType.METHOD) 16 | public @interface ZenGetter { 17 | 18 | /** 19 | * Getter name. If omitted, the method name will be used as value name. 20 | * 21 | * @return getter name 22 | */ 23 | String value() default ""; 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/annotations/ZenMemberGetter.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.annotations; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * Indicates a member getter. A member getter is a special kind of operator 7 | * which is a fallback if a member could not be resolved. Can be used to make 8 | * contents available as if they were members. 9 | *

10 | * If this method is a native class, it accepts a single string parameter with 11 | * the member name. If this method is an expansion method, it accepts the source 12 | * value and the member name. 13 | * 14 | * @author Stan Hebben 15 | */ 16 | @Retention(RetentionPolicy.RUNTIME) 17 | @Target(ElementType.METHOD) 18 | public @interface ZenMemberGetter { 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/annotations/ZenMemberSetter.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.annotations; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * Used to indicate a member setter. Companion to member getters, but allow 7 | * setting contents of this object as if they were members. 8 | *

9 | * If this method is a native class, it accepts a string parameter with the 10 | * member name and a second parameter with the value. If this method is an 11 | * expansion method, it accepts the source value, the member name and the value 12 | * to be assigned. 13 | * 14 | * @author Stan Hebben 15 | */ 16 | @Retention(RetentionPolicy.RUNTIME) 17 | @Target(ElementType.METHOD) 18 | public @interface ZenMemberSetter { 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/annotations/ZenMethod.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.annotations; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * Used to indicate an exposed method. 7 | *

8 | * If this method is part of a native class, it will simply accept the 9 | * parameters as provided. If it is part of an expansion method, it will also 10 | * accept the instance value as first parameter. 11 | * 12 | * @author Stan Hebben 13 | */ 14 | @Retention(RetentionPolicy.RUNTIME) 15 | @Target(ElementType.METHOD) 16 | public @interface ZenMethod { 17 | 18 | /** 19 | * Method name. If omitted, the original method name will be used. 20 | * 21 | * @return method name 22 | */ 23 | String value() default ""; 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/annotations/ZenMethodStatic.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.annotations; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * Used to indicate static methods for expansions. For native classes, use 7 | * ZenMethod instead on a static method. 8 | * 9 | * @author Stan Hebben 10 | */ 11 | @Retention(RetentionPolicy.RUNTIME) 12 | @Target(ElementType.METHOD) 13 | public @interface ZenMethodStatic { 14 | 15 | /** 16 | * Method name. If omitted, the annotated method name will be used. 17 | * 18 | * @return method name 19 | */ 20 | String value() default ""; 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/annotations/ZenOperator.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.annotations; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * Used to indicate an operator. The arguments depend on the kind of operator. 7 | * 8 | * @author Stan Hebben 9 | */ 10 | @Retention(RetentionPolicy.RUNTIME) 11 | @Target(ElementType.METHOD) 12 | public @interface ZenOperator { 13 | 14 | /** 15 | * Operator type for this operator. 16 | * 17 | * @return operator type 18 | */ 19 | OperatorType value(); 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/annotations/ZenProperty.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.annotations; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * Used to annotate Properties. Will find and set the setter and getter methods, 10 | * that is, it will set up the methods for calling foo.property and for foo.property = bar. 11 | * 12 | * Will error if it can't find the setter and getter methods 13 | * 14 | * @author Stan Hebben 15 | */ 16 | @Retention(RetentionPolicy.RUNTIME) 17 | @Target(ElementType.FIELD) 18 | public @interface ZenProperty { 19 | 20 | /** 21 | * Property name. If omitted, the field name is used. 22 | * 23 | * @return property name 24 | */ 25 | String value() default ""; 26 | 27 | /** 28 | * returns the method name used to get the setter, an empty string will use set plus the variable name, 29 | * if null, will not try to grab a setter. 30 | * 31 | * @return setter method name 32 | */ 33 | String setter() default ""; 34 | 35 | /** 36 | * returns the method name used to get the getter, an empty string will use get plus the variable name, 37 | * if null, will not try to grab a getter. 38 | * 39 | * @return getter method name 40 | */ 41 | String getter() default ""; 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/annotations/ZenSetter.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.annotations; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * Used to annotate setters. Will make the given method accessible as setter, 7 | * that is, it's called when value.name is being assinged a value. 8 | *

9 | * For a native class, a single argument with the assinged value is provided. 10 | * For an expansion, the target object and assigned value are provided. 11 | * 12 | * @author Stan Hebben 13 | */ 14 | @Retention(RetentionPolicy.RUNTIME) 15 | @Target(ElementType.METHOD) 16 | public @interface ZenSetter { 17 | 18 | /** 19 | * Setter name. If omitted, the method name is used as property name. 20 | * 21 | * @return setter name 22 | */ 23 | String value() default ""; 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/compiler/ClassNameGenerator.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.compiler; 2 | 3 | /** 4 | * @author Stanneke 5 | */ 6 | public class ClassNameGenerator { 7 | 8 | private String prefix; 9 | private int counter = 0; 10 | 11 | public ClassNameGenerator() { 12 | this("ZenClass"); 13 | } 14 | 15 | public ClassNameGenerator(String prefix) { 16 | this.prefix = prefix; 17 | } 18 | 19 | public String getPrefix() { 20 | return prefix; 21 | } 22 | 23 | public void setPrefix(String prefix) { 24 | this.prefix = prefix; 25 | } 26 | 27 | public String generate() { 28 | return this.prefix + counter++; 29 | } 30 | 31 | public String generate(String customPrefix) { 32 | return customPrefix + counter++; 33 | } 34 | 35 | public String generateWithMiddleName(String customMiddleName) { 36 | return prefix + customMiddleName + counter++; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/compiler/IEnvironmentClass.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.compiler; 2 | 3 | import org.objectweb.asm.ClassVisitor; 4 | 5 | /** 6 | * @author Stan 7 | */ 8 | public interface IEnvironmentClass extends IEnvironmentGlobal { 9 | 10 | ClassVisitor getClassOutput(); 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/compiler/IEnvironmentGlobal.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.compiler; 2 | 3 | import stanhebben.zenscript.*; 4 | import stanhebben.zenscript.expression.partial.IPartialExpression; 5 | import stanhebben.zenscript.symbols.IZenSymbol; 6 | import stanhebben.zenscript.util.ZenPosition; 7 | 8 | import java.util.Set; 9 | 10 | /** 11 | * @author Stan 12 | */ 13 | public interface IEnvironmentGlobal extends ITypeRegistry, IZenErrorLogger { 14 | 15 | IZenCompileEnvironment getEnvironment(); 16 | 17 | TypeExpansion getExpansion(String name); 18 | 19 | ClassNameGenerator getClassNameGenerator(); 20 | 21 | String makeClassName(); 22 | 23 | String makeClassNameWithMiddleName(String middleName); 24 | 25 | boolean containsClass(String name); 26 | 27 | Set getClassNames(); 28 | 29 | byte[] getClass(String name); 30 | 31 | void putClass(String name, byte[] data); 32 | 33 | IPartialExpression getValue(String name, ZenPosition position); 34 | 35 | void putValue(String name, IZenSymbol value, ZenPosition position); 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/compiler/IEnvironmentMethod.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.compiler; 2 | 3 | import stanhebben.zenscript.symbols.SymbolLocal; 4 | import stanhebben.zenscript.util.MethodOutput; 5 | 6 | /** 7 | * @author Stan 8 | */ 9 | public interface IEnvironmentMethod extends IEnvironmentClass { 10 | 11 | MethodOutput getOutput(); 12 | 13 | int getLocal(SymbolLocal variable); 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/compiler/ITypeRegistry.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.compiler; 2 | 3 | import stanhebben.zenscript.type.ZenType; 4 | 5 | import java.lang.reflect.Type; 6 | 7 | /** 8 | * @author Stan 9 | */ 10 | public interface ITypeRegistry { 11 | 12 | ZenType getType(Type type); 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/compiler/ZenClassWriter.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.compiler; 2 | 3 | import org.objectweb.asm.ClassWriter; 4 | 5 | /** 6 | * In some cases visitMaxs threw ClassNotFound Exceptions due to different class loaders/path 7 | * Using this one should fix that. 8 | * Issue was mostly found in MCF due to a class loader override that didn't affect ASM classes. 9 | */ 10 | public class ZenClassWriter extends ClassWriter { 11 | 12 | public ZenClassWriter(int flags) { 13 | super(flags); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/definitions/Import.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.definitions; 2 | 3 | import stanhebben.zenscript.util.ZenPosition; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author Stanneke 9 | */ 10 | public class Import { 11 | 12 | private final ZenPosition position; 13 | private final List name; 14 | private final String rename; 15 | 16 | public Import(ZenPosition position, List name, String rename) { 17 | this.position = position; 18 | this.name = name; 19 | this.rename = rename; 20 | } 21 | 22 | public ZenPosition getPosition() { 23 | return position; 24 | } 25 | 26 | public List getName() { 27 | return name; 28 | } 29 | 30 | public String getRename() { 31 | return rename == null ? name.get(name.size() - 1) : rename; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/definitions/ParsedFunctionArgument.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.definitions; 2 | 3 | import stanhebben.zenscript.parser.expression.ParsedExpression; 4 | import stanhebben.zenscript.type.ZenType; 5 | 6 | /** 7 | * @author Stanneke 8 | */ 9 | public class ParsedFunctionArgument { 10 | 11 | private final String name; 12 | private final ZenType type; 13 | private final ParsedExpression defaultExpression; 14 | 15 | public ParsedFunctionArgument(String name, ZenType type, ParsedExpression defaultExpression) { 16 | this.name = name; 17 | this.type = type; 18 | this.defaultExpression = defaultExpression; 19 | } 20 | 21 | @Deprecated 22 | public ParsedFunctionArgument(String name, ZenType type) { 23 | this(name, type, null); 24 | } 25 | 26 | public String getName() { 27 | return name; 28 | } 29 | 30 | public ZenType getType() { 31 | return type; 32 | } 33 | 34 | public ParsedExpression getDefaultExpression() { 35 | return defaultExpression; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/dump/DumpUtils.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.dump; 2 | 3 | public class DumpUtils { 4 | 5 | 6 | public static String getClassNameFromPath(String path){ 7 | int lastChar = path.lastIndexOf('.'); 8 | if (lastChar < 0) { 9 | lastChar = path.lastIndexOf('\\'); 10 | } 11 | 12 | if (lastChar < 0) { 13 | lastChar = path.lastIndexOf('/'); 14 | } 15 | 16 | if (lastChar < 0) { 17 | return path; 18 | } 19 | 20 | return path.substring(lastChar + 1, path.length()); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/dump/GSONDumpableSerializer.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.dump; 2 | 3 | import com.google.gson.*; 4 | 5 | import java.lang.reflect.Type; 6 | 7 | public class GSONDumpableSerializer implements JsonSerializer { 8 | public static final GSONDumpableSerializer INSTANCE = new GSONDumpableSerializer(); 9 | 10 | @Override 11 | public JsonElement serialize(IDumpable src, Type typeOfSrc, JsonSerializationContext context) { 12 | return src.serialize(context); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/dump/IDumpConvertable.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.dump; 2 | 3 | import java.util.*; 4 | 5 | public interface IDumpConvertable { 6 | 7 | /** 8 | * Gets a List of dumpable objects that can easily serialized into JSON 9 | */ 10 | default List asDumpedObject() {return Collections.emptyList();} 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/dump/IDumpable.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.dump; 2 | 3 | import com.google.gson.*; 4 | 5 | public interface IDumpable { 6 | default JsonElement serialize(JsonSerializationContext context) {return JsonNull.INSTANCE;} 7 | } -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/dump/types/DumpClassBase.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.dump.types; 2 | 3 | import com.google.gson.*; 4 | import stanhebben.zenscript.dump.*; 5 | 6 | public class DumpClassBase implements IDumpable { 7 | private String classNameJava; 8 | private String fullPathNameJava; 9 | private String zsAliasPath; 10 | private String zsAliasClassName; 11 | 12 | public DumpClassBase(String fullPathName, String zsAliasPath) { 13 | this.fullPathNameJava = fullPathName; 14 | this.zsAliasPath = zsAliasPath; 15 | this.zsAliasClassName = DumpUtils.getClassNameFromPath(zsAliasPath); 16 | this.classNameJava = DumpUtils.getClassNameFromPath(fullPathNameJava); 17 | } 18 | 19 | public String getClassNameJava() { 20 | return classNameJava; 21 | } 22 | 23 | public String getFullPathNameJava() { 24 | return fullPathNameJava; 25 | } 26 | 27 | public String getZsAliasPath() { 28 | return zsAliasPath; 29 | } 30 | 31 | public String getZsAliasClassName() { 32 | return zsAliasClassName; 33 | } 34 | 35 | @Override 36 | public JsonObject serialize(JsonSerializationContext context) { 37 | JsonObject obj = new JsonObject(); 38 | obj.addProperty("javaPath", getFullPathNameJava()); 39 | obj.addProperty("zsPath", getZsAliasPath()); 40 | 41 | return obj; 42 | } 43 | 44 | @Override 45 | public String toString() { 46 | return "DumpClassBase: {" + fullPathNameJava + "}"; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/dump/types/DumpDummy.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.dump.types; 2 | 3 | import com.google.gson.*; 4 | import stanhebben.zenscript.dump.IDumpable; 5 | 6 | public class DumpDummy implements IDumpable { 7 | private String className; 8 | private String toString; 9 | 10 | public DumpDummy(Object self) { 11 | this.className = self.getClass().getCanonicalName(); 12 | this.toString = self.toString(); 13 | } 14 | 15 | @Override 16 | public JsonElement serialize(JsonSerializationContext context) { 17 | return new JsonPrimitive(className + " : {" + toString + "}"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/dump/types/DumpIJavaMethod.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.dump.types; 2 | 3 | import com.google.gson.*; 4 | import stanhebben.zenscript.dump.IDumpable; 5 | import stanhebben.zenscript.type.natives.*; 6 | 7 | public class DumpIJavaMethod implements IDumpable { 8 | 9 | private transient IJavaMethod method; 10 | private boolean staticOverride = false; 11 | 12 | public DumpIJavaMethod(IJavaMethod method) { 13 | this.method = method; 14 | } 15 | 16 | @Override 17 | public JsonObject serialize(JsonSerializationContext context) { 18 | JsonObject obj = new JsonObject(); 19 | 20 | obj.addProperty("static", method.isStatic() || staticOverride); 21 | obj.addProperty("returnClass", method.getReturnType().toJavaClass().getCanonicalName()); 22 | obj.addProperty("name", method.toString()); 23 | 24 | if (method instanceof JavaMethodGenerated) { 25 | obj.addProperty("isSynthetic", true); 26 | } 27 | 28 | return obj; 29 | } 30 | 31 | public DumpIJavaMethod withStaticOverride(boolean override){ 32 | staticOverride = override; 33 | return this; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/dump/types/DumpZenType.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.dump.types; 2 | 3 | public class DumpZenType extends DumpClassBase { 4 | private transient Class clazz; 5 | 6 | public DumpZenType(Class clazz, String zsAliasPath) { 7 | super(clazz.getCanonicalName(), zsAliasPath); 8 | 9 | this.clazz = clazz; 10 | } 11 | 12 | public Class getClazz() { 13 | return clazz; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionAndAnd.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import org.objectweb.asm.Label; 4 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 5 | import stanhebben.zenscript.type.ZenType; 6 | import stanhebben.zenscript.util.*; 7 | 8 | public class ExpressionAndAnd extends Expression { 9 | 10 | private final Expression a; 11 | private final Expression b; 12 | 13 | public ExpressionAndAnd(ZenPosition position, Expression a, Expression b) { 14 | super(position); 15 | 16 | this.a = a; 17 | this.b = b; 18 | } 19 | 20 | @Override 21 | public ZenType getType() { 22 | return a.getType(); 23 | } 24 | 25 | @Override 26 | public void compile(boolean result, IEnvironmentMethod environment) { 27 | // if not a: return false 28 | // if not b: return false 29 | // return true 30 | 31 | MethodOutput output = environment.getOutput(); 32 | 33 | final Label onFalse = new Label(); 34 | final Label end = new Label(); 35 | a.compile(true, environment); 36 | output.ifEQ(onFalse); 37 | b.compile(true, environment); 38 | output.ifEQ(onFalse); 39 | output.iConst1(); 40 | output.goTo(end); 41 | output.label(onFalse); 42 | output.iConst0(); 43 | output.label(end); 44 | 45 | if(!result) { 46 | output.pop(); 47 | } 48 | } 49 | 50 | @Override 51 | public void compileIf(Label onElse, IEnvironmentMethod environment) { 52 | a.compile(true, environment); 53 | environment.getOutput().ifEQ(onElse); 54 | b.compile(true, environment); 55 | environment.getOutput().ifEQ(onElse); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionArgument.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | import stanhebben.zenscript.util.ZenPosition; 6 | 7 | /** 8 | * @author Stanneke 9 | */ 10 | public class ExpressionArgument extends Expression { 11 | 12 | private final int id; 13 | private final ZenType type; 14 | 15 | public ExpressionArgument(ZenPosition position, int id, ZenType type) { 16 | super(position); 17 | 18 | this.id = id; 19 | this.type = type; 20 | } 21 | 22 | @Override 23 | public ZenType getType() { 24 | return type; 25 | } 26 | 27 | @Override 28 | public void compile(boolean result, IEnvironmentMethod environment) { 29 | environment.getOutput().load(type.toASMType(), id); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionArrayAdd.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentGlobal; 4 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 5 | import stanhebben.zenscript.type.ZenType; 6 | import stanhebben.zenscript.type.ZenTypeArrayBasic; 7 | import stanhebben.zenscript.util.ArrayUtil; 8 | import stanhebben.zenscript.util.MethodOutput; 9 | import stanhebben.zenscript.util.ZenPosition; 10 | 11 | public class ExpressionArrayAdd extends Expression { 12 | 13 | private final Expression array, value; 14 | private final ZenTypeArrayBasic type; 15 | 16 | public ExpressionArrayAdd(ZenPosition position, IEnvironmentGlobal environment, Expression array, Expression val) { 17 | super(position); 18 | this.array = array; 19 | this.value = val; 20 | this.type = (ZenTypeArrayBasic) array.getType(); 21 | } 22 | 23 | @Override 24 | public ZenType getType() { 25 | return type; 26 | } 27 | 28 | @Override 29 | public void compile(boolean result, IEnvironmentMethod environment) { 30 | MethodOutput output = environment.getOutput(); 31 | 32 | array.compile(true, environment); 33 | value.cast(getPosition(), environment, type.getBaseType()).compile(true, environment); 34 | 35 | if (type.getBaseType().toJavaClass().isPrimitive()) { 36 | Class arrayType = getType().toJavaClass(); 37 | output.invokeStatic(ArrayUtil.class, "add", arrayType, arrayType, type.getBaseType().toJavaClass()); 38 | } else { 39 | output.invokeStatic(ArrayUtil.class, "add", Object[].class, Object[].class, Object.class); 40 | output.checkCast(type.getSignature()); 41 | } 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionArrayGet.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.*; 4 | import stanhebben.zenscript.type.*; 5 | import stanhebben.zenscript.util.ZenPosition; 6 | 7 | /** 8 | * @author Stan 9 | */ 10 | public class ExpressionArrayGet extends Expression { 11 | 12 | private final Expression array; 13 | private final Expression index; 14 | private final ZenType baseType; 15 | 16 | public ExpressionArrayGet(ZenPosition position, Expression array, Expression index) { 17 | super(position); 18 | 19 | this.array = array; 20 | this.index = index; 21 | this.baseType = ((ZenTypeArray) array.getType()).getBaseType(); 22 | } 23 | 24 | @Override 25 | public ZenType getType() { 26 | return baseType; 27 | } 28 | 29 | @Override 30 | public void compile(boolean result, IEnvironmentMethod environment) { 31 | array.compile(result, environment); 32 | index.compile(result, environment); 33 | 34 | if(result) { 35 | environment.getOutput().arrayLoad(baseType.toASMType()); 36 | } 37 | } 38 | 39 | @Override 40 | public Expression assign(ZenPosition position, IEnvironmentGlobal environment, Expression other) { 41 | return new ExpressionArraySet(position, array, index, other); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionArrayLength.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.*; 5 | import stanhebben.zenscript.util.ZenPosition; 6 | 7 | /** 8 | * @author Stanneke 9 | */ 10 | public class ExpressionArrayLength extends Expression { 11 | 12 | private final Expression value; 13 | 14 | public ExpressionArrayLength(ZenPosition position, Expression value) { 15 | super(position); 16 | 17 | this.value = value; 18 | } 19 | 20 | @Override 21 | public ZenType getType() { 22 | return ZenTypeInt.INSTANCE; 23 | } 24 | 25 | @Override 26 | public void compile(boolean result, IEnvironmentMethod environment) { 27 | if(result) { 28 | value.compile(true, environment); 29 | environment.getOutput().arrayLength(); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionArrayList.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.*; 5 | import stanhebben.zenscript.util.*; 6 | 7 | import java.util.*; 8 | 9 | public class ExpressionArrayList extends Expression { 10 | 11 | 12 | private final Expression[] contents; 13 | private final ZenTypeArrayList type; 14 | 15 | public ExpressionArrayList(ZenPosition position, ZenTypeArrayList type, Expression[] contents) { 16 | super(position); 17 | this.contents = contents; 18 | this.type = type; 19 | } 20 | 21 | @Override 22 | public void compile(boolean result, IEnvironmentMethod environment) { 23 | if(!result) return; 24 | final MethodOutput methodOutput = environment.getOutput(); 25 | methodOutput.newObject(ArrayList.class); 26 | methodOutput.dup(); 27 | methodOutput.invokeSpecial("java/util/ArrayList", "", "()V"); 28 | 29 | for(Expression content : contents) { 30 | methodOutput.dup(); 31 | content.cast(getPosition(), environment, ZenTypeUtil.checkPrimitive(content.getType())).compile(true, environment); 32 | methodOutput.invokeInterface(Collection.class, "add", boolean.class, Object.class); 33 | methodOutput.pop(); 34 | } 35 | } 36 | 37 | @Override 38 | public ZenType getType() { 39 | return type; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionArrayListAdd.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import java.util.List; 4 | 5 | import stanhebben.zenscript.compiler.IEnvironmentGlobal; 6 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 7 | import stanhebben.zenscript.type.ZenType; 8 | import stanhebben.zenscript.type.ZenTypeArrayList; 9 | import stanhebben.zenscript.util.MethodOutput; 10 | import stanhebben.zenscript.util.ZenPosition; 11 | 12 | public class ExpressionArrayListAdd extends Expression { 13 | 14 | private final Expression array, value; 15 | private final ZenTypeArrayList type; 16 | 17 | public ExpressionArrayListAdd(ZenPosition position, IEnvironmentGlobal environment, Expression array, Expression val) { 18 | super(position); 19 | this.array = array; 20 | this.value = val; 21 | this.type = (ZenTypeArrayList) array.getType(); 22 | } 23 | 24 | @Override 25 | public ZenType getType() { 26 | return type; 27 | } 28 | 29 | @Override 30 | public void compile(boolean result, IEnvironmentMethod environment) { 31 | MethodOutput output = environment.getOutput(); 32 | array.compile(true, environment); 33 | output.dup(); 34 | value.compile(true, environment); 35 | output.invokeInterface(List.class, "add", boolean.class, Object.class); 36 | output.pop(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionArrayListRemove.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | import stanhebben.zenscript.util.ZenPosition; 6 | import stanhebben.zenscript.util.ZenTypeUtil; 7 | 8 | import java.util.Collection; 9 | 10 | public class ExpressionArrayListRemove extends Expression { 11 | 12 | private final Expression list; 13 | private final Expression val; 14 | 15 | public ExpressionArrayListRemove(ZenPosition position, Expression list, Expression val) { 16 | super(position); 17 | this.list = list; 18 | this.val = val; 19 | } 20 | 21 | @Override 22 | public void compile(boolean result, IEnvironmentMethod environment) { 23 | list.compile(true, environment); 24 | val.cast(getPosition(), environment, ZenTypeUtil.checkPrimitive(val.getType())).compile(true, environment); 25 | 26 | 27 | environment.getOutput().invokeInterface(Collection.class, "remove", boolean.class, Object.class); 28 | if (!result) 29 | environment.getOutput().pop(); 30 | } 31 | 32 | @Override 33 | public ZenType getType() { 34 | return ZenType.BOOL; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionArrayListSet.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import java.util.List; 4 | 5 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 6 | import stanhebben.zenscript.type.ZenType; 7 | import stanhebben.zenscript.type.ZenTypeVoid; 8 | import stanhebben.zenscript.util.ZenPosition; 9 | 10 | public class ExpressionArrayListSet extends Expression { 11 | 12 | private final Expression array; 13 | private final Expression index; 14 | private final Expression value; 15 | private final ZenType type; 16 | private final ZenPosition position; 17 | 18 | public ExpressionArrayListSet(ZenPosition position, Expression array, Expression index, Expression value) { 19 | super(position); 20 | this.array = array; 21 | this.index = index; 22 | this.value = value; 23 | this.type = ZenTypeVoid.INSTANCE; 24 | this.position = position; 25 | } 26 | 27 | @Override 28 | public ZenType getType() { 29 | return type; 30 | } 31 | 32 | @Override 33 | public void compile(boolean result, IEnvironmentMethod environment) { 34 | array.compile(result, environment); 35 | index.compile(result, environment); 36 | value.compile(result, environment); 37 | //environment.warning(position, "Compiling EALS with result = "+String.valueOf(result)); 38 | if(result){ 39 | environment.getOutput().invokeInterface(List.class, "set", Object.class, int.class, Object.class); 40 | //environment.getOutput().checkCast(type.toASMType().getInternalName()); 41 | 42 | } 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionArraySet.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | import stanhebben.zenscript.util.ZenPosition; 6 | 7 | /** 8 | * @author Stan 9 | */ 10 | public class ExpressionArraySet extends Expression { 11 | 12 | private final Expression array; 13 | private final Expression index; 14 | private final Expression value; 15 | 16 | public ExpressionArraySet(ZenPosition position, Expression array, Expression index, Expression value) { 17 | super(position); 18 | 19 | this.array = array; 20 | this.index = index; 21 | this.value = value; 22 | } 23 | 24 | @Override 25 | public ZenType getType() { 26 | return ZenType.VOID; 27 | } 28 | 29 | @Override 30 | public void compile(boolean result, IEnvironmentMethod environment) { 31 | array.compile(result, environment); 32 | index.compile(result, environment); 33 | value.compile(result, environment); 34 | 35 | if(result) { 36 | environment.getOutput().arrayStore(value.getType().toASMType()); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionAs.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | import stanhebben.zenscript.type.casting.ICastingRule; 6 | import stanhebben.zenscript.util.ZenPosition; 7 | 8 | public class ExpressionAs extends Expression { 9 | 10 | private final Expression value; 11 | private final ICastingRule castingRule; 12 | 13 | public ExpressionAs(ZenPosition position, Expression value, ICastingRule castingRule) { 14 | super(position); 15 | 16 | this.value = value; 17 | this.castingRule = castingRule; 18 | } 19 | 20 | /* 21 | * @Override public Expression cast(ZenPosition position, IEnvironmentGlobal 22 | * environment, ZenType type) { if 23 | * (castingRule.getResultingType().equals(type)) { return this; } 24 | * 25 | * ICastingRule newCastingRule = type.getCastingRule(type, environment); if 26 | * (newCastingRule == null) { environment.error(position, "Cannot cast " + 27 | * getType() + " to " + type); return new ExpressionInvalid(position, type); 28 | * } else { return new ExpressionAs(position, this, newCastingRule); } } 29 | */ 30 | 31 | @Override 32 | public ZenType getType() { 33 | return castingRule.getResultingType(); 34 | } 35 | 36 | @Override 37 | public void compile(boolean result, IEnvironmentMethod environment) { 38 | value.compile(result, environment); 39 | 40 | if(result) { 41 | castingRule.compile(environment); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionBool.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import org.objectweb.asm.Label; 4 | import stanhebben.zenscript.compiler.*; 5 | import stanhebben.zenscript.type.ZenType; 6 | import stanhebben.zenscript.util.ZenPosition; 7 | 8 | /** 9 | * @author Stanneke 10 | */ 11 | public class ExpressionBool extends Expression { 12 | 13 | private final boolean value; 14 | 15 | public ExpressionBool(ZenPosition position, boolean value) { 16 | super(position); 17 | 18 | this.value = value; 19 | } 20 | 21 | @Override 22 | public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) { 23 | if(type == ZenType.BOOL) { 24 | return this; 25 | } else { 26 | return super.cast(position, environment, type); 27 | } 28 | } 29 | 30 | @Override 31 | public Expression getMember(ZenPosition position, IEnvironmentGlobal environment, String name) { 32 | environment.error(position, "Bool constants do not have members"); 33 | return new ExpressionInvalid(position, ZenType.BOOL); 34 | } 35 | 36 | @Override 37 | public ZenType getType() { 38 | return ZenType.BOOL; 39 | } 40 | 41 | @Override 42 | public void compile(boolean result, IEnvironmentMethod environment) { 43 | if(result) { 44 | if(value) { 45 | environment.getOutput().iConst1(); 46 | } else { 47 | environment.getOutput().iConst0(); 48 | } 49 | } 50 | } 51 | 52 | @Override 53 | public void compileIf(Label onElse, IEnvironmentMethod environment) { 54 | if(!value) { 55 | environment.getOutput().goTo(onElse); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionConditional.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import org.objectweb.asm.Label; 4 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 5 | import stanhebben.zenscript.type.ZenType; 6 | import stanhebben.zenscript.util.ZenPosition; 7 | 8 | public class ExpressionConditional extends Expression { 9 | 10 | private final Expression condition; 11 | private final Expression onIf; 12 | private final Expression onElse; 13 | 14 | public ExpressionConditional(ZenPosition position, Expression condition, Expression onIf, Expression onElse) { 15 | super(position); 16 | 17 | this.condition = condition; 18 | this.onIf = onIf; 19 | this.onElse = onElse; 20 | } 21 | 22 | @Override 23 | public ZenType getType() { 24 | return onIf.getType(); // TODO: improve 25 | } 26 | 27 | @Override 28 | public void compile(boolean result, IEnvironmentMethod environment) { 29 | Label lblElse = new Label(); 30 | Label lblExit = new Label(); 31 | 32 | condition.compileIf(lblElse, environment); 33 | onIf.cast(getPosition(), environment, getType()).compile(result, environment); 34 | environment.getOutput().goTo(lblExit); 35 | environment.getOutput().label(lblElse); 36 | onElse.cast(getPosition(), environment, getType()).compile(result, environment); 37 | 38 | environment.getOutput().label(lblExit); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionEntryGet.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.*; 5 | import stanhebben.zenscript.util.ZenPosition; 6 | 7 | import java.util.Map; 8 | 9 | import static stanhebben.zenscript.util.ZenTypeUtil.internal; 10 | 11 | public class ExpressionEntryGet extends Expression { 12 | 13 | 14 | private final Expression entry; 15 | private final ZenTypeEntry type; 16 | private final boolean isKey; 17 | 18 | public ExpressionEntryGet(ZenPosition position, Expression entry, boolean isKey) { 19 | super(position); 20 | this.entry = entry; 21 | this.type = (ZenTypeEntry) entry.getType(); 22 | this.isKey = isKey; 23 | } 24 | 25 | @Override 26 | public void compile(boolean result, IEnvironmentMethod environment) { 27 | entry.compile(result, environment); 28 | environment.getOutput().invokeInterface(Map.Entry.class, isKey ? "getKey" : "getValue", Object.class); 29 | environment.getOutput().checkCast(internal(getType().toJavaClass())); 30 | } 31 | 32 | @Override 33 | public ZenType getType() { 34 | return isKey ? type.getKeyType() : type.getValueType(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionFloat.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.*; 5 | import stanhebben.zenscript.util.MethodOutput; 6 | import stanhebben.zenscript.util.ZenPosition; 7 | import stanhebben.zenscript.util.ZenTypeUtil; 8 | 9 | /** 10 | * @author Stanneke 11 | */ 12 | public class ExpressionFloat extends Expression { 13 | 14 | private final double value; 15 | private final ZenType type; 16 | 17 | public ExpressionFloat(ZenPosition position, double value, ZenType type) { 18 | super(position); 19 | 20 | this.value = value; 21 | this.type = type; 22 | } 23 | 24 | @Override 25 | public ZenType getType() { 26 | return type; 27 | } 28 | 29 | @Override 30 | public void compile(boolean result, IEnvironmentMethod environment) { 31 | if(!result) 32 | return; 33 | 34 | MethodOutput output = environment.getOutput(); 35 | if(type == ZenTypeFloat.INSTANCE) { 36 | output.constant((float) value); 37 | } else if(type == ZenTypeDouble.INSTANCE) { 38 | output.constant(value); 39 | } else if(type == ZenTypeFloatObject.INSTANCE) { 40 | output.constant((float)value); 41 | output.invokeStatic(ZenTypeUtil.internal(Float.class), "valueOf", "(F)Ljava/lang/Float;"); 42 | } else if(type == ZenTypeDoubleObject.INSTANCE) { 43 | output.constant(value); 44 | output.invokeSpecial(ZenTypeUtil.internal(Double.class), "valueOf", "(D)Ljava/lang/Double;"); 45 | } else { 46 | throw new RuntimeException("Internal compiler error: source type is not a floating point type"); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionGetZenClassDefaultParameter.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.definitions.zenclasses.ParsedZenClassMethod; 5 | import stanhebben.zenscript.type.ZenType; 6 | import stanhebben.zenscript.util.MethodOutput; 7 | import stanhebben.zenscript.util.ZenPosition; 8 | 9 | public class ExpressionGetZenClassDefaultParameter extends Expression { 10 | private final ParsedZenClassMethod.ZenClassMethod method; 11 | private final int parameterNumber; 12 | 13 | public ExpressionGetZenClassDefaultParameter(ZenPosition position, ParsedZenClassMethod.ZenClassMethod method, int parameterNumber) { 14 | super(position); 15 | this.method = method; 16 | this.parameterNumber = parameterNumber; 17 | } 18 | 19 | @Override 20 | public void compile(boolean result, IEnvironmentMethod environment) { 21 | if (result) { 22 | MethodOutput output = environment.getOutput(); 23 | output.getStaticField(method.getOwner(), method.getFunction().getDefaultParameterFieldName(parameterNumber), getType().toASMType().getDescriptor()); 24 | } 25 | } 26 | 27 | @Override 28 | public ZenType getType() { 29 | return method.getParameterTypes()[parameterNumber]; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionGlobalGet.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import org.objectweb.asm.Type; 4 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 5 | import stanhebben.zenscript.symbols.SymbolGlobalValue; 6 | import stanhebben.zenscript.type.*; 7 | 8 | public class ExpressionGlobalGet extends Expression { 9 | 10 | private final SymbolGlobalValue global; 11 | 12 | public ExpressionGlobalGet(SymbolGlobalValue value) { 13 | super(value.getPosition()); 14 | this.global = value; 15 | } 16 | 17 | @Override 18 | public ZenType getType() { 19 | return global.getType(); 20 | } 21 | 22 | @Override 23 | public void compile(boolean result, IEnvironmentMethod environment) { 24 | environment.getOutput().getStaticField(getOwner(), getName(), getASMDescriptor()); 25 | if(getType().toASMType().equals(Type.getType(Object.class))) 26 | environment.getOutput().checkCast(getType().getSignature()); 27 | } 28 | 29 | public String getOwner() { 30 | return global.getOwner(); 31 | } 32 | 33 | public String getName() { 34 | return global.getName(); 35 | } 36 | 37 | public String getASMDescriptor() { 38 | return global.getASMDescriptor(); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionGlobalSet.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import org.objectweb.asm.Type; 4 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 5 | import stanhebben.zenscript.symbols.SymbolGlobalValue; 6 | import stanhebben.zenscript.type.ZenType; 7 | 8 | public class ExpressionGlobalSet extends Expression { 9 | 10 | private final SymbolGlobalValue global; 11 | private Expression newVal; 12 | 13 | public ExpressionGlobalSet(SymbolGlobalValue value, Expression newVal) { 14 | super(value.getPosition()); 15 | this.global = value; 16 | this.newVal = newVal; 17 | } 18 | 19 | @Override 20 | public ZenType getType() { 21 | return ZenType.VOID; 22 | } 23 | 24 | @Override 25 | public void compile(boolean result, IEnvironmentMethod environment) { 26 | newVal.compile(true, environment); 27 | if (getType().toASMType().equals(Type.getType(Object.class))) { 28 | environment.getOutput().checkCast(getType().getSignature()); 29 | } 30 | environment.getOutput().putStaticField(getOwner(), getName(), getASMDescriptor()); 31 | } 32 | 33 | public String getOwner() { 34 | return global.getOwner(); 35 | } 36 | 37 | public String getName() { 38 | return global.getName(); 39 | } 40 | 41 | public String getASMDescriptor() { 42 | return global.getASMDescriptor(); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionInstanceOf.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | import stanhebben.zenscript.util.ZenPosition; 6 | 7 | import static stanhebben.zenscript.util.ZenTypeUtil.internal; 8 | 9 | public class ExpressionInstanceOf extends Expression { 10 | 11 | private final Expression expression; 12 | private final ZenType type; 13 | 14 | public ExpressionInstanceOf(ZenPosition position, Expression expression, ZenType type) { 15 | super(position); 16 | this.expression = expression; 17 | this.type = type; 18 | } 19 | 20 | @Override 21 | public void compile(boolean result, IEnvironmentMethod environment) { 22 | if(!result) 23 | return; 24 | expression.compile(result, environment); 25 | environment.getOutput().instanceOf(internal(type.toJavaClass())); 26 | } 27 | 28 | @Override 29 | public ZenType getType() { 30 | return ZenType.BOOL; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionIntegerRange.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package stanhebben.zenscript.expression; 6 | 7 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 8 | import stanhebben.zenscript.type.ZenType; 9 | import stanhebben.zenscript.util.*; 10 | import stanhebben.zenscript.value.IntRange; 11 | 12 | /** 13 | * @author Stanneke 14 | */ 15 | public class ExpressionIntegerRange extends Expression { 16 | 17 | private final Expression from; 18 | private final Expression to; 19 | 20 | public ExpressionIntegerRange(ZenPosition position, Expression from, Expression to) { 21 | super(position); 22 | 23 | this.from = from; 24 | this.to = to; 25 | } 26 | 27 | @Override 28 | public ZenType getType() { 29 | return ZenType.INTRANGE; 30 | } 31 | 32 | @Override 33 | public void compile(boolean result, IEnvironmentMethod environment) { 34 | MethodOutput output = environment.getOutput(); 35 | output.newObject(IntRange.class); 36 | output.dup(); 37 | from.compile(true, environment); 38 | to.compile(true, environment); 39 | output.construct(IntRange.class, int.class, int.class); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionInvalid.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import org.objectweb.asm.Label; 4 | import stanhebben.zenscript.compiler.*; 5 | import stanhebben.zenscript.type.*; 6 | import stanhebben.zenscript.util.ZenPosition; 7 | 8 | /** 9 | * @author Stanneke 10 | */ 11 | public class ExpressionInvalid extends Expression { 12 | 13 | private final ZenType type; 14 | 15 | public ExpressionInvalid(ZenPosition position) { 16 | super(position); 17 | 18 | type = ZenTypeAny.INSTANCE; 19 | } 20 | 21 | public ExpressionInvalid(ZenPosition position, ZenType type) { 22 | super(position); 23 | 24 | this.type = type == null ? ZenType.ANY : type; 25 | 26 | // XXX: remove before release 27 | // throw new RuntimeException("Constructing invalid expression"); 28 | } 29 | 30 | @Override 31 | public Expression getMember(ZenPosition position, IEnvironmentGlobal errors, String name) { 32 | return new ExpressionInvalid(position); 33 | } 34 | 35 | @Override 36 | public Expression cast(ZenPosition position, IEnvironmentGlobal errors, ZenType type) { 37 | return new ExpressionInvalid(position, type); 38 | } 39 | 40 | @Override 41 | public ZenType getType() { 42 | return type; 43 | } 44 | 45 | @Override 46 | public void compile(boolean result, IEnvironmentMethod environment) { 47 | type.defaultValue(getPosition()).compile(result, environment); 48 | } 49 | 50 | @Override 51 | public void compileIf(Label onElse, IEnvironmentMethod environment) { 52 | environment.getOutput().goTo(onElse); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionJavaMethodCurled.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.*; 4 | import stanhebben.zenscript.definitions.ParsedFunctionArgument; 5 | import stanhebben.zenscript.type.*; 6 | import stanhebben.zenscript.type.natives.JavaMethod; 7 | import stanhebben.zenscript.util.ZenPosition; 8 | 9 | import java.util.*; 10 | 11 | /** 12 | * @author Stanneke 13 | */ 14 | @Deprecated 15 | public class ExpressionJavaMethodCurled extends Expression { 16 | 17 | private final JavaMethod method; 18 | private final ZenType type; 19 | private final Expression receiver; 20 | 21 | public ExpressionJavaMethodCurled(ZenPosition position, JavaMethod method, IEnvironmentGlobal environment, Expression receiver) { 22 | super(position); 23 | 24 | this.method = method; 25 | this.receiver = receiver; 26 | 27 | List arguments = new ArrayList<>(); 28 | for(int i = 0; i < method.getMethod().getParameterTypes().length; i++) { 29 | arguments.add(new ParsedFunctionArgument("p" + i, environment.getType(method.getMethod().getGenericParameterTypes()[i]))); 30 | } 31 | 32 | this.type = new ZenTypeFunction(environment.getType(method.getMethod().getGenericReturnType()), arguments); 33 | } 34 | 35 | @Override 36 | public ZenType getType() { 37 | return type; 38 | } 39 | 40 | @Override 41 | public void compile(boolean result, IEnvironmentMethod environment) { 42 | // TODO: compile 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionJavaMethodStatic.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.*; 4 | import stanhebben.zenscript.definitions.ParsedFunctionArgument; 5 | import stanhebben.zenscript.type.*; 6 | import stanhebben.zenscript.type.natives.JavaMethod; 7 | import stanhebben.zenscript.util.ZenPosition; 8 | 9 | import java.util.*; 10 | 11 | /** 12 | * @author Stanneke 13 | */ 14 | 15 | @Deprecated 16 | public class ExpressionJavaMethodStatic extends Expression { 17 | 18 | private final JavaMethod method; 19 | private final ZenType type; 20 | 21 | public ExpressionJavaMethodStatic(ZenPosition position, JavaMethod method, IEnvironmentGlobal environment) { 22 | super(position); 23 | 24 | this.method = method; 25 | 26 | List arguments = new ArrayList<>(); 27 | for(int i = 0; i < method.getParameterTypes().length; i++) { 28 | arguments.add(new ParsedFunctionArgument("p" + i, environment.getType(method.getMethod().getGenericParameterTypes()[i]))); 29 | } 30 | 31 | this.type = new ZenTypeFunction(environment.getType(method.getMethod().getGenericReturnType()), arguments); 32 | } 33 | 34 | @Override 35 | public ZenType getType() { 36 | return type; 37 | } 38 | 39 | @Override 40 | public void compile(boolean result, IEnvironmentMethod environment) { 41 | // TODO: compile 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionLocalGet.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.symbols.SymbolLocal; 5 | import stanhebben.zenscript.type.ZenType; 6 | import stanhebben.zenscript.util.ZenPosition; 7 | 8 | /** 9 | * @author Stanneke 10 | */ 11 | public class ExpressionLocalGet extends Expression { 12 | 13 | private final SymbolLocal variable; 14 | 15 | public ExpressionLocalGet(ZenPosition position, SymbolLocal variable) { 16 | super(position); 17 | 18 | this.variable = variable; 19 | } 20 | 21 | @Override 22 | public ZenType getType() { 23 | return variable.getType(); 24 | } 25 | 26 | @Override 27 | public void compile(boolean result, IEnvironmentMethod environment) { 28 | int local = environment.getLocal(variable); 29 | environment.getOutput().load(variable.getType().toASMType(), local); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionLocalSet.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.symbols.SymbolLocal; 5 | import stanhebben.zenscript.type.ZenType; 6 | import stanhebben.zenscript.util.ZenPosition; 7 | 8 | /** 9 | * @author Stanneke 10 | */ 11 | public class ExpressionLocalSet extends Expression { 12 | 13 | private final SymbolLocal variable; 14 | private final Expression value; 15 | 16 | public ExpressionLocalSet(ZenPosition position, SymbolLocal variable, Expression value) { 17 | super(position); 18 | 19 | this.variable = variable; 20 | this.value = value; 21 | } 22 | 23 | @Override 24 | public ZenType getType() { 25 | return variable.getType(); 26 | } 27 | 28 | @Override 29 | public void compile(boolean result, IEnvironmentMethod environment) { 30 | int local = environment.getLocal(variable); 31 | 32 | value.cast(getPosition(), environment, variable.getType()).compile(true, environment); 33 | if(result) { 34 | environment.getOutput().dup(); 35 | } 36 | environment.getOutput().store(variable.getType().toASMType(), local); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionMapContains.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | import stanhebben.zenscript.type.ZenTypeAssociative; 6 | import stanhebben.zenscript.util.ZenPosition; 7 | 8 | import java.util.Map; 9 | 10 | import static stanhebben.zenscript.util.ZenTypeUtil.internal; 11 | 12 | /** 13 | * @author Stanneke 14 | */ 15 | public class ExpressionMapContains extends Expression { 16 | 17 | private final Expression map; 18 | private final Expression key; 19 | 20 | public ExpressionMapContains(ZenPosition position, Expression map, Expression key) { 21 | super(position); 22 | 23 | this.map = map; 24 | this.key = key; 25 | } 26 | 27 | @Override 28 | public ZenType getType() { 29 | return ZenType.BOOL; 30 | } 31 | 32 | @Override 33 | public void compile(boolean result, IEnvironmentMethod environment) { 34 | if(result) { 35 | ZenTypeAssociative mapType = (ZenTypeAssociative) map.getType(); 36 | map.compile(result, environment); 37 | key.cast(getPosition(), environment, mapType.getKeyType()).compile(result, environment); 38 | 39 | environment.getOutput().invokeInterface(internal(Map.class), "containsKey", "(Ljava/lang/Object;)Z"); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionMapIndexGet.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.*; 5 | import stanhebben.zenscript.util.ZenPosition; 6 | 7 | import java.util.Map; 8 | 9 | import static stanhebben.zenscript.util.ZenTypeUtil.internal; 10 | 11 | /** 12 | * @author Stanneke 13 | */ 14 | public class ExpressionMapIndexGet extends Expression { 15 | 16 | private final Expression map; 17 | private final Expression index; 18 | 19 | private final ZenType type; 20 | 21 | public ExpressionMapIndexGet(ZenPosition position, Expression map, Expression index) { 22 | super(position); 23 | 24 | this.map = map; 25 | this.index = index; 26 | 27 | type = ((ZenTypeAssociative) map.getType()).getValueType(); 28 | } 29 | 30 | @Override 31 | public ZenType getType() { 32 | return type; 33 | } 34 | 35 | @Override 36 | public void compile(boolean result, IEnvironmentMethod environment) { 37 | if(result) { 38 | map.compile(result, environment); 39 | index.cast(getPosition(), environment, ((ZenTypeAssociative) map.getType()).getKeyType()).compile(result, environment); 40 | environment.getOutput().invokeInterface(internal(Map.class), "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); 41 | environment.getOutput().checkCast(type.toASMType().getInternalName()); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionMapIndexSet.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.*; 5 | import stanhebben.zenscript.util.ZenPosition; 6 | 7 | import java.util.Map; 8 | 9 | import static stanhebben.zenscript.util.ZenTypeUtil.internal; 10 | 11 | /** 12 | * @author Stanneke 13 | */ 14 | public class ExpressionMapIndexSet extends Expression { 15 | 16 | private final Expression map; 17 | private final Expression index; 18 | private final Expression value; 19 | 20 | private final ZenType type; 21 | 22 | public ExpressionMapIndexSet(ZenPosition position, Expression map, Expression index, Expression value) { 23 | super(position); 24 | 25 | this.map = map; 26 | this.index = index; 27 | this.value = value; 28 | 29 | type = ZenTypeVoid.INSTANCE; 30 | } 31 | 32 | @Override 33 | public ZenType getType() { 34 | return type; 35 | } 36 | 37 | @Override 38 | public void compile(boolean result, IEnvironmentMethod environment) { 39 | if(result) { 40 | map.compile(result, environment); 41 | index.cast(getPosition(), environment, ((ZenTypeAssociative)map.getType()).getKeyType()).compile(result, environment); 42 | value.cast(getPosition(), environment, ((ZenTypeAssociative)map.getType()).getValueType()).compile(result, environment); 43 | environment.getOutput().invokeInterface(internal(Map.class), "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); 44 | environment.getOutput().pop(); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionMapSize.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.*; 5 | import stanhebben.zenscript.util.ZenPosition; 6 | 7 | import java.util.Map; 8 | 9 | import static stanhebben.zenscript.util.ZenTypeUtil.internal; 10 | 11 | /** 12 | * @author Stanneke 13 | */ 14 | public class ExpressionMapSize extends Expression { 15 | 16 | private final Expression map; 17 | 18 | public ExpressionMapSize(ZenPosition position, Expression map) { 19 | super(position); 20 | 21 | this.map = map; 22 | } 23 | 24 | @Override 25 | public ZenType getType() { 26 | return ZenTypeInt.INSTANCE; 27 | } 28 | 29 | @Override 30 | public void compile(boolean result, IEnvironmentMethod environment) { 31 | if(result) { 32 | map.compile(true, environment); 33 | environment.getOutput().invokeInterface(internal(Map.class), "size", "()I"); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionNothing.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | import stanhebben.zenscript.util.ZenPosition; 6 | 7 | public class ExpressionNothing extends Expression { 8 | 9 | 10 | public ExpressionNothing(ZenPosition position) { 11 | super(position); 12 | } 13 | 14 | @Override 15 | public void compile(boolean result, IEnvironmentMethod environment) { 16 | 17 | } 18 | 19 | @Override 20 | public ZenType getType() { 21 | return ZenType.NULL; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionNull.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import org.objectweb.asm.Label; 4 | import stanhebben.zenscript.compiler.*; 5 | import stanhebben.zenscript.type.*; 6 | import stanhebben.zenscript.util.ZenPosition; 7 | 8 | /** 9 | * @author Stanneke 10 | */ 11 | public class ExpressionNull extends Expression { 12 | 13 | public ExpressionNull(ZenPosition position) { 14 | super(position); 15 | } 16 | 17 | @Override 18 | public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) { 19 | if(type.isPointer()) { 20 | return this; 21 | } else { 22 | environment.error(position, "Cannot convert null to " + type); 23 | return new ExpressionInvalid(position); 24 | } 25 | } 26 | 27 | @Override 28 | public ZenType getType() { 29 | return ZenTypeNull.INSTANCE; 30 | } 31 | 32 | @Override 33 | public void compile(boolean result, IEnvironmentMethod environment) { 34 | if(result) { 35 | environment.getOutput().aConstNull(); 36 | } 37 | } 38 | 39 | @Override 40 | public void compileIf(Label onElse, IEnvironmentMethod environment) { 41 | environment.getOutput().goTo(onElse); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionOrOr.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import org.objectweb.asm.Label; 4 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 5 | import stanhebben.zenscript.type.ZenType; 6 | import stanhebben.zenscript.util.MethodOutput; 7 | import stanhebben.zenscript.util.ZenPosition; 8 | 9 | public class ExpressionOrOr extends Expression { 10 | 11 | private final Expression a; 12 | private final Expression b; 13 | 14 | public ExpressionOrOr(ZenPosition position, Expression a, Expression b) { 15 | super(position); 16 | 17 | this.a = a; 18 | this.b = b; 19 | } 20 | 21 | @Override 22 | public ZenType getType() { 23 | return a.getType(); 24 | } 25 | 26 | @Override 27 | public void compile(boolean result, IEnvironmentMethod environment) { 28 | final MethodOutput output = environment.getOutput(); 29 | 30 | final Label onTrue = new Label(); 31 | final Label end = new Label(); 32 | 33 | a.compile(true, environment); 34 | output.ifNE(onTrue); 35 | b.compile(true, environment); 36 | output.ifNE(onTrue); 37 | output.iConst0(); 38 | output.goTo(end); 39 | output.label(onTrue); 40 | output.iConst1(); 41 | output.label(end); 42 | 43 | if(!result) 44 | output.pop(); 45 | 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionString.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.*; 5 | import stanhebben.zenscript.util.ZenPosition; 6 | 7 | /** 8 | * @author Stanneke 9 | */ 10 | public class ExpressionString extends Expression { 11 | 12 | private final String value; 13 | 14 | public ExpressionString(ZenPosition position, String value) { 15 | super(position); 16 | 17 | this.value = value; 18 | } 19 | 20 | @Override 21 | public ZenType getType() { 22 | return ZenTypeString.INSTANCE; 23 | } 24 | 25 | @Override 26 | public void compile(boolean result, IEnvironmentMethod environment) { 27 | if(result) { 28 | environment.getOutput().constant(value); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionStringConcat.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.*; 5 | import stanhebben.zenscript.util.*; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * @author Stanneke 11 | */ 12 | public class ExpressionStringConcat extends Expression { 13 | 14 | private final List values; 15 | 16 | public ExpressionStringConcat(ZenPosition position, List values) { 17 | super(position); 18 | 19 | this.values = values; 20 | } 21 | 22 | public void add(Expression value) { 23 | values.add(value); 24 | } 25 | 26 | @Override 27 | public ZenType getType() { 28 | return ZenTypeString.INSTANCE; 29 | } 30 | 31 | @Override 32 | public void compile(boolean result, IEnvironmentMethod environment) { 33 | if(result) { 34 | MethodOutput output = environment.getOutput(); 35 | 36 | // Step 1: construct StringBuilder 37 | output.newObject(StringBuilder.class); 38 | output.dup(); 39 | output.construct(StringBuilder.class); 40 | 41 | // Step 2: concatenate Strings 42 | for(Expression value : values) { 43 | value.compile(true, environment); 44 | output.invoke(StringBuilder.class, "append", StringBuilder.class, String.class); 45 | } 46 | 47 | // Step 3: return String 48 | output.invoke(StringBuilder.class, "toString", String.class); 49 | } else { 50 | for(Expression value : values) { 51 | value.compile(false, environment); 52 | } 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionStringContains.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | import stanhebben.zenscript.util.ZenPosition; 6 | 7 | /** 8 | * @author Stan 9 | */ 10 | public class ExpressionStringContains extends Expression { 11 | 12 | private final Expression haystack; 13 | private final Expression needle; 14 | 15 | public ExpressionStringContains(ZenPosition position, Expression haystack, Expression needle) { 16 | super(position); 17 | 18 | this.haystack = haystack; 19 | this.needle = needle; 20 | } 21 | 22 | @Override 23 | public ZenType getType() { 24 | return ZenType.BOOL; 25 | } 26 | 27 | @Override 28 | public void compile(boolean result, IEnvironmentMethod environment) { 29 | 30 | haystack.compile(result, environment); 31 | needle.compile(result, environment); 32 | 33 | if(result) { 34 | environment.getOutput().invokeVirtual(String.class, "contains", boolean.class, CharSequence.class); 35 | } 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionStringIndex.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | import stanhebben.zenscript.util.ZenPosition; 6 | 7 | /** 8 | * @author Stan 9 | */ 10 | public class ExpressionStringIndex extends Expression { 11 | 12 | private final Expression source; 13 | private final Expression index; 14 | 15 | public ExpressionStringIndex(ZenPosition position, Expression source, Expression index) { 16 | super(position); 17 | 18 | this.source = source; 19 | this.index = index; 20 | } 21 | 22 | @Override 23 | public ZenType getType() { 24 | return ZenType.STRING; 25 | } 26 | 27 | @Override 28 | public void compile(boolean result, IEnvironmentMethod environment) { 29 | source.compile(result, environment); 30 | index.compile(result, environment); 31 | 32 | if(result) { 33 | environment.getOutput().dup(); 34 | environment.getOutput().iConst1(); 35 | environment.getOutput().iAdd(); 36 | environment.getOutput().invokeVirtual(String.class, "substring", String.class, int.class, int.class); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/ExpressionThis.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | import stanhebben.zenscript.util.ZenPosition; 6 | 7 | public class ExpressionThis extends Expression { 8 | 9 | private final ZenType type; 10 | 11 | public ExpressionThis(ZenPosition position, ZenType type) { 12 | super(position); 13 | this.type = type; 14 | } 15 | 16 | @Override 17 | public void compile(boolean result, IEnvironmentMethod environment) { 18 | if(result) 19 | environment.getOutput().loadObject(0); 20 | } 21 | 22 | @Override 23 | public ZenType getType() { 24 | return type; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/partial/ExpressionJavaStaticField.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression.partial; 2 | 3 | import stanhebben.zenscript.compiler.*; 4 | import stanhebben.zenscript.expression.Expression; 5 | import stanhebben.zenscript.type.ZenType; 6 | import stanhebben.zenscript.util.ZenPosition; 7 | 8 | import java.lang.reflect.Field; 9 | 10 | /** 11 | * @author Stan 12 | */ 13 | public class ExpressionJavaStaticField extends Expression { 14 | 15 | private final Class cls; 16 | private final Field field; 17 | private final ITypeRegistry types; 18 | 19 | public ExpressionJavaStaticField(ZenPosition position, Class cls, Field field, ITypeRegistry types) { 20 | super(position); 21 | 22 | this.cls = cls; 23 | this.field = field; 24 | this.types = types; 25 | } 26 | 27 | @Override 28 | public ZenType getType() { 29 | return types.getType(field.getGenericType()); 30 | } 31 | 32 | @Override 33 | public void compile(boolean result, IEnvironmentMethod environment) { 34 | if(result) { 35 | environment.getOutput().getStaticField(cls, field); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/partial/IPartialExpression.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression.partial; 2 | 3 | import stanhebben.zenscript.compiler.*; 4 | import stanhebben.zenscript.expression.Expression; 5 | import stanhebben.zenscript.symbols.IZenSymbol; 6 | import stanhebben.zenscript.type.ZenType; 7 | import stanhebben.zenscript.util.ZenPosition; 8 | 9 | /** 10 | * @author Stanneke 11 | */ 12 | public interface IPartialExpression { 13 | 14 | Expression eval(IEnvironmentGlobal environment); 15 | 16 | Expression assign(ZenPosition position, IEnvironmentGlobal environment, Expression other); 17 | 18 | IPartialExpression getMember(ZenPosition position, IEnvironmentGlobal environment, String name); 19 | 20 | Expression call(ZenPosition position, IEnvironmentMethod environment, Expression... values); 21 | 22 | ZenType[] predictCallTypes(int numArguments); 23 | 24 | IZenSymbol toSymbol(); 25 | 26 | ZenType getType(); 27 | 28 | ZenType toType(IEnvironmentGlobal environment); 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/partial/PartialExpansionCall.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression.partial; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.definitions.ParsedFunctionArgument; 5 | import stanhebben.zenscript.expression.Expression; 6 | import stanhebben.zenscript.type.ZenType; 7 | import stanhebben.zenscript.util.ZenPosition; 8 | 9 | import java.util.Arrays; 10 | import java.util.List; 11 | 12 | /** 13 | * @author youyihj 14 | */ 15 | public class PartialExpansionCall extends PartialStaticGenerated { 16 | private final Expression instance; 17 | 18 | public PartialExpansionCall(ZenPosition position, String owner, String method, String signature, List arguments, ZenType returnType, Expression instance) { 19 | super(position, owner, method, signature, arguments, returnType); 20 | this.instance = instance; 21 | } 22 | 23 | @Override 24 | public Expression call(ZenPosition position, IEnvironmentMethod environment, Expression... values) { 25 | Expression[] expressions = Arrays.copyOf(new Expression[] {instance}, values.length + 1); 26 | System.arraycopy(values, 0, expressions, 1, values.length); 27 | return super.call(position, environment, expressions); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/expression/partial/PartialZSClass.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.expression.partial; 2 | 3 | import stanhebben.zenscript.compiler.*; 4 | import stanhebben.zenscript.expression.*; 5 | import stanhebben.zenscript.symbols.IZenSymbol; 6 | import stanhebben.zenscript.type.*; 7 | import stanhebben.zenscript.util.ZenPosition; 8 | 9 | public class PartialZSClass implements IPartialExpression { 10 | 11 | private final ZenTypeZenClass type; 12 | 13 | public PartialZSClass(ZenTypeZenClass type) { 14 | 15 | this.type = type; 16 | } 17 | 18 | @Override 19 | public Expression eval(IEnvironmentGlobal environment) { 20 | return new ExpressionNothing(type.zenClass.position); 21 | } 22 | 23 | @Override 24 | public Expression assign(ZenPosition position, IEnvironmentGlobal environment, Expression other) { 25 | throw new UnsupportedOperationException("Cannot assign to a class"); 26 | } 27 | 28 | @Override 29 | public IPartialExpression getMember(ZenPosition position, IEnvironmentGlobal environment, String name) { 30 | return type.getMember(position, environment, this, name); 31 | } 32 | 33 | @Override 34 | public Expression call(ZenPosition position, IEnvironmentMethod environment, Expression... values) { 35 | return type.call(position, environment, this.eval(environment), values); 36 | } 37 | 38 | @Override 39 | public ZenType[] predictCallTypes(int numArguments) { 40 | return type.predictCallTypes(numArguments); 41 | } 42 | 43 | @Override 44 | public IZenSymbol toSymbol() { 45 | return position -> this; 46 | } 47 | 48 | @Override 49 | public ZenType getType() { 50 | return type; 51 | } 52 | 53 | @Override 54 | public ZenType toType(IEnvironmentGlobal environment) { 55 | return type; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/impl/BracketHandler.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.impl; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * Marks a bracket handler. The marked class should have an empty constructor. 7 | * 8 | * @author Stan Hebben 9 | */ 10 | @Retention(RetentionPolicy.RUNTIME) 11 | @Target({ElementType.TYPE}) 12 | public @interface BracketHandler { 13 | 14 | /** 15 | * Indicates priority. A lower value means a higher priority. Only change if 16 | * you have issues, default value is 10. 17 | * 18 | * @return priority 19 | */ 20 | int priority() default 10; 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/impl/GenericCompileEnvironment.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.impl; 2 | 3 | import stanhebben.zenscript.*; 4 | import stanhebben.zenscript.compiler.*; 5 | import stanhebben.zenscript.parser.Token; 6 | import stanhebben.zenscript.symbols.IZenSymbol; 7 | 8 | import java.util.List; 9 | 10 | public class GenericCompileEnvironment implements IZenCompileEnvironment { 11 | 12 | private IZenRegistry registry; 13 | 14 | public GenericCompileEnvironment() { 15 | } 16 | 17 | @Override 18 | public IZenErrorLogger getErrorLogger() { 19 | return registry.getErrorLogger(); 20 | } 21 | 22 | @Override 23 | public IZenSymbol getGlobal(String name) { 24 | final IZenSymbol symbol = registry.getGlobals().get(name); 25 | if(symbol != null) { 26 | return symbol; 27 | } 28 | return registry.getRoot().get(name); 29 | } 30 | 31 | @Override 32 | public IZenSymbol getBracketed(IEnvironmentGlobal environment, List tokens) { 33 | return registry.resolveBracket(environment, tokens); 34 | } 35 | 36 | @Override 37 | public TypeRegistry getTypeRegistry() { 38 | return registry.getTypes(); 39 | } 40 | 41 | @Override 42 | public TypeExpansion getExpansion(String type) { 43 | return registry.getExpansions().computeIfAbsent(type, TypeExpansion::new); 44 | } 45 | 46 | public IZenRegistry getRegistry() { 47 | return registry; 48 | } 49 | 50 | public void setRegistry(IZenRegistry registry) { 51 | this.registry = registry; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/impl/GenericErrorLogger.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.impl; 2 | 3 | import stanhebben.zenscript.IZenErrorLogger; 4 | import stanhebben.zenscript.util.ZenPosition; 5 | 6 | import java.io.*; 7 | 8 | public class GenericErrorLogger implements IZenErrorLogger { 9 | 10 | private final PrintStream output; 11 | 12 | public GenericErrorLogger(PrintStream output) { 13 | this.output = output; 14 | } 15 | 16 | @Override 17 | public void error(ZenPosition position, String message) { 18 | if(position == null) { 19 | output.println("System> " + message); 20 | } else { 21 | output.println(position + "> " + message); 22 | } 23 | } 24 | 25 | @Override 26 | public void warning(ZenPosition position, String message) { 27 | if(position == null) { 28 | output.println("system> " + message); 29 | } else { 30 | output.println(position + "> " + message); 31 | } 32 | } 33 | 34 | @Override 35 | public void info(ZenPosition position, String message) { 36 | if(position == null) { 37 | output.println("system> " + message); 38 | } else { 39 | output.println(position + "> " + message); 40 | } 41 | } 42 | 43 | @Override 44 | public void error(String message) { 45 | error(null, message); 46 | } 47 | @Override 48 | public void error(String message, Throwable e) { 49 | error(null, message); 50 | e.printStackTrace(output); 51 | } 52 | 53 | @Override 54 | public void warning(String message) { 55 | warning(null, message); 56 | } 57 | 58 | @Override 59 | public void info(String message) { 60 | info(null, message); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/impl/GenericFunctions.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.impl; 2 | 3 | public class GenericFunctions { 4 | 5 | public static void print(String message) { 6 | System.out.println(message); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/impl/GenericLogger.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.impl; 2 | 3 | import stanhebben.zenscript.IZenLogger; 4 | 5 | public class GenericLogger implements IZenLogger { 6 | 7 | @Override 8 | public void error(String message) { 9 | System.out.println("[ERROR] " + message); 10 | } 11 | 12 | @Override 13 | public void error(String message, Throwable e) { 14 | System.out.println("[ERROR] " + message); 15 | e.printStackTrace(); 16 | } 17 | 18 | @Override 19 | public void warning(String message) { 20 | System.out.println("[WARNING] " + message); 21 | } 22 | 23 | @Override 24 | public void info(String message) { 25 | System.out.println("[INFO] " + message); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/impl/IBracketHandler.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.impl; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentGlobal; 4 | import stanhebben.zenscript.parser.Token; 5 | import stanhebben.zenscript.symbols.IZenSymbol; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * Bracket handlers enable processing of the bracket syntax. 11 | *

12 | * Inside brackets, any kind of token is acceptable except the closing bracket. 13 | * (>) Bracket handlers (multiple handlers can be registered) will resolve 14 | * these tokens into actual values. Values have to be ZenScript symbols and will 15 | * resolve at compile-time. 16 | *

17 | * These may of course return an expression that are executed on run-time, but 18 | * the type of the resulting value must be known compile-time. 19 | * 20 | * @author Stan Hebben 21 | */ 22 | public interface IBracketHandler { 23 | 24 | /** 25 | * Resolves a set of tokens. 26 | *

27 | * If the series of tokens is unrecognized, this method should return null. 28 | * 29 | * @param environment global compilation environment 30 | * @param tokens token stream to be detected 31 | * 32 | * @return the resolved symbol, or null 33 | */ 34 | IZenSymbol resolve(IEnvironmentGlobal environment, List tokens); 35 | 36 | default String getRegexMatchingString(){ return ".*"; } 37 | 38 | default Class getReturnedClass() { return null; } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/parser/ArrayListI.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.parser; 2 | 3 | /** 4 | * This class is similar to an ArrayList, but is optimized for integers. 5 | * 6 | * @author Stan Hebben 7 | */ 8 | public class ArrayListI { 9 | 10 | private int[] data; 11 | private int size; 12 | 13 | /** 14 | * Creates a new, empty ArrayList of integers. 15 | */ 16 | public ArrayListI() { 17 | data = new int[8]; 18 | } 19 | 20 | /** 21 | * Adds a value. 22 | * 23 | * @param value integer value 24 | */ 25 | public void add(int value) { 26 | if(size == data.length) 27 | expand(); 28 | data[size++] = value; 29 | } 30 | 31 | /** 32 | * Gets the value at the specified position. 33 | * 34 | * @param index position 35 | * 36 | * @return value at this position 37 | */ 38 | public int get(int index) { 39 | if(index >= size) 40 | throw new ArrayIndexOutOfBoundsException(); 41 | return data[index]; 42 | } 43 | 44 | /** 45 | * Gets the size of this ArrayList. 46 | * 47 | * @return size 48 | */ 49 | public int size() { 50 | return size; 51 | } 52 | 53 | // ////////////////// 54 | // Private methods 55 | // ////////////////// 56 | 57 | /** 58 | * Doubles the size of this array. 59 | */ 60 | private void expand() { 61 | int[] newdata = new int[data.length * 2]; 62 | System.arraycopy(data, 0, newdata, 0, size); 63 | data = newdata; 64 | } 65 | 66 | public int[] toArray() { 67 | int[] result = new int[size]; 68 | System.arraycopy(data, 0, result, 0, size); 69 | return result; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/parser/IteratorI.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.parser; 2 | 3 | /** 4 | * This interface represents an iterator over integer values. 5 | * 6 | * @author Stan Hebben 7 | */ 8 | public interface IteratorI { 9 | 10 | /** 11 | * Returns true if the iteration has more elements. 12 | * 13 | * @return true if the iteration has more elements 14 | */ 15 | boolean hasNext(); 16 | 17 | /** 18 | * Returns the next element in this iteration. 19 | * 20 | * @return the next element in this iteration 21 | */ 22 | int next(); 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/parser/ParseException.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.parser; 2 | 3 | import stanhebben.zenscript.ZenParsedFile; 4 | 5 | /** 6 | * @author Stan 7 | */ 8 | public class ParseException extends RuntimeException { 9 | 10 | private final ZenParsedFile file; 11 | private final int line; 12 | private final int lineOffset; 13 | 14 | private final Token token; 15 | private final String message; 16 | 17 | public ParseException(Token token, String error) { 18 | super("Error parsing line " + token.getPosition().getLine() + ":" + token.getPosition().getLineOffset() + " - " + error + " (last token: " + token.getValue() + ")"); 19 | 20 | this.file = token.getPosition().getFile(); 21 | this.line = token.getPosition().getLine(); 22 | this.lineOffset = token.getPosition().getLineOffset(); 23 | 24 | this.token = token; 25 | this.message = error; 26 | } 27 | 28 | public ParseException(ZenParsedFile file, int line, int lineOffset, String error) { 29 | super("Error parsing line " + line + ":" + lineOffset + " - " + error); 30 | this.file = file; 31 | this.line = line; 32 | this.lineOffset = lineOffset; 33 | 34 | token = null; 35 | message = error; 36 | } 37 | 38 | public ZenParsedFile getFile() { 39 | return file; 40 | } 41 | 42 | public int getLine() { 43 | return line; 44 | } 45 | 46 | public int getLineOffset() { 47 | return lineOffset; 48 | } 49 | 50 | public String getExplanation() { 51 | return message; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/parser/Token.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.parser; 2 | 3 | import stanhebben.zenscript.util.ZenPosition; 4 | 5 | /** 6 | * Represents a token in a token stream. 7 | * 8 | * @author Stan Hebben 9 | */ 10 | public class Token { 11 | 12 | private final ZenPosition position; 13 | private final String value; 14 | private final int type; 15 | 16 | /** 17 | * Constructs a new token. 18 | * 19 | * @param value token string value 20 | * @param type token type 21 | * @param position token position 22 | */ 23 | public Token(String value, int type, ZenPosition position) { 24 | this.value = value; 25 | this.type = type; 26 | this.position = position; 27 | } 28 | 29 | public ZenPosition getPosition() { 30 | return position; 31 | } 32 | 33 | /** 34 | * Returns the string value of this token. 35 | * 36 | * @return token value 37 | */ 38 | public String getValue() { 39 | return value; 40 | } 41 | 42 | /** 43 | * Returns the token type of this token. 44 | * 45 | * @return token type 46 | */ 47 | public int getType() { 48 | return type; 49 | } 50 | 51 | @Override 52 | public String toString() { 53 | return position.getLine() + ":" + position.getLineOffset() + " (" + type + ") " + value; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/parser/TokenException.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.parser; 2 | 3 | import stanhebben.zenscript.ZenParsedFile; 4 | 5 | /** 6 | * Thrown when an exception occurs while tokening a character stream. 7 | * 8 | * @author Stan Hebben 9 | */ 10 | public class TokenException extends RuntimeException { 11 | 12 | public TokenException(ZenParsedFile file, int line, int lineOffset, char value) { 13 | super("Invalid character at " + file + ":" + line + " - " + value); 14 | 15 | if(line < 0) 16 | throw new IllegalArgumentException("Line cannot be negative"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/parser/expression/ParsedExpressionAndAnd.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.parser.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.expression.ExpressionAndAnd; 5 | import stanhebben.zenscript.expression.partial.IPartialExpression; 6 | import stanhebben.zenscript.type.ZenType; 7 | import stanhebben.zenscript.util.ZenPosition; 8 | 9 | /** 10 | * @author Stanneke 11 | */ 12 | public class ParsedExpressionAndAnd extends ParsedExpression { 13 | 14 | private final ParsedExpression left; 15 | private final ParsedExpression right; 16 | 17 | public ParsedExpressionAndAnd(ZenPosition position, ParsedExpression left, ParsedExpression right) { 18 | super(position); 19 | 20 | this.left = left; 21 | this.right = right; 22 | } 23 | 24 | @Override 25 | public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) { 26 | return new ExpressionAndAnd(getPosition(), left.compile(environment, predictedType).eval(environment), right.compile(environment, predictedType).eval(environment)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/parser/expression/ParsedExpressionAssign.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.parser.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.expression.partial.IPartialExpression; 5 | import stanhebben.zenscript.type.ZenType; 6 | import stanhebben.zenscript.util.ZenPosition; 7 | 8 | /** 9 | * @author Stanneke 10 | */ 11 | public class ParsedExpressionAssign extends ParsedExpression { 12 | 13 | private final ParsedExpression left; 14 | private final ParsedExpression right; 15 | 16 | public ParsedExpressionAssign(ZenPosition position, ParsedExpression left, ParsedExpression right) { 17 | super(position); 18 | 19 | this.left = left; 20 | this.right = right; 21 | } 22 | 23 | @Override 24 | public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) { 25 | IPartialExpression cLeft = left.compile(environment, predictedType); 26 | return cLeft.assign(getPosition(), environment, right.compile(environment, cLeft.getType()).eval(environment)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/parser/expression/ParsedExpressionBinary.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.parser.expression; 2 | 3 | import stanhebben.zenscript.annotations.OperatorType; 4 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 5 | import stanhebben.zenscript.expression.Expression; 6 | import stanhebben.zenscript.expression.partial.IPartialExpression; 7 | import stanhebben.zenscript.type.ZenType; 8 | import stanhebben.zenscript.util.ZenPosition; 9 | 10 | /** 11 | * @author Stanneke 12 | */ 13 | public class ParsedExpressionBinary extends ParsedExpression { 14 | 15 | private final ParsedExpression left; 16 | private final ParsedExpression right; 17 | private final OperatorType operator; 18 | 19 | public ParsedExpressionBinary(ZenPosition position, ParsedExpression left, ParsedExpression right, OperatorType operator) { 20 | super(position); 21 | 22 | this.left = left; 23 | this.right = right; 24 | this.operator = operator; 25 | } 26 | 27 | @Override 28 | public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) { 29 | // TODO: make better predictions 30 | Expression cLeft = left.compile(environment, predictedType).eval(environment); 31 | Expression cRight = right.compile(environment, predictedType).eval(environment); 32 | return cLeft.getType().binary(getPosition(), environment, cLeft, cRight, operator); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/parser/expression/ParsedExpressionBool.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.parser.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.expression.ExpressionBool; 5 | import stanhebben.zenscript.expression.partial.IPartialExpression; 6 | import stanhebben.zenscript.type.ZenType; 7 | import stanhebben.zenscript.util.ZenPosition; 8 | 9 | /** 10 | * @author Stanneke 11 | */ 12 | public class ParsedExpressionBool extends ParsedExpression { 13 | 14 | private final boolean value; 15 | 16 | public ParsedExpressionBool(ZenPosition position, boolean value) { 17 | super(position); 18 | 19 | this.value = value; 20 | } 21 | 22 | @Override 23 | public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) { 24 | return new ExpressionBool(getPosition(), value); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/parser/expression/ParsedExpressionCall.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.parser.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.expression.Expression; 5 | import stanhebben.zenscript.expression.partial.IPartialExpression; 6 | import stanhebben.zenscript.type.ZenType; 7 | import stanhebben.zenscript.util.ZenPosition; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * @author Stanneke 13 | */ 14 | public class ParsedExpressionCall extends ParsedExpression { 15 | 16 | private final ParsedExpression receiver; 17 | private final List arguments; 18 | 19 | public ParsedExpressionCall(ZenPosition position, ParsedExpression receiver, List arguments) { 20 | super(position); 21 | 22 | this.receiver = receiver; 23 | this.arguments = arguments; 24 | } 25 | 26 | @Override 27 | public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) { 28 | IPartialExpression cReceiver = receiver.compile(environment, predictedType); 29 | ZenType[] predictedTypes = cReceiver.predictCallTypes(arguments.size()); 30 | 31 | Expression[] cArguments = new Expression[arguments.size()]; 32 | for(int i = 0; i < cArguments.length; i++) { 33 | IPartialExpression cArgument = arguments.get(i).compile(environment, predictedTypes[i]); 34 | cArguments[i] = cArgument.eval(environment); 35 | } 36 | 37 | return cReceiver.call(getPosition(), environment, cArguments); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/parser/expression/ParsedExpressionCast.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.parser.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.expression.partial.IPartialExpression; 5 | import stanhebben.zenscript.type.ZenType; 6 | import stanhebben.zenscript.util.ZenPosition; 7 | 8 | /** 9 | * @author Stanneke 10 | */ 11 | public class ParsedExpressionCast extends ParsedExpression { 12 | 13 | private final ParsedExpression value; 14 | private final ZenType type; 15 | 16 | public ParsedExpressionCast(ZenPosition position, ParsedExpression value, ZenType type) { 17 | super(position); 18 | 19 | this.value = value; 20 | this.type = type; 21 | } 22 | 23 | @Override 24 | public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) { 25 | return value.compile(environment, type).eval(environment).cast(getPosition(), environment, type); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/parser/expression/ParsedExpressionCompare.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.parser.expression; 2 | 3 | import stanhebben.zenscript.annotations.CompareType; 4 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 5 | import stanhebben.zenscript.expression.Expression; 6 | import stanhebben.zenscript.expression.partial.IPartialExpression; 7 | import stanhebben.zenscript.type.ZenType; 8 | import stanhebben.zenscript.util.ZenPosition; 9 | 10 | /** 11 | * @author Stanneke 12 | */ 13 | public class ParsedExpressionCompare extends ParsedExpression { 14 | 15 | private final ParsedExpression left; 16 | private final ParsedExpression right; 17 | private final CompareType type; 18 | 19 | public ParsedExpressionCompare(ZenPosition position, ParsedExpression left, ParsedExpression right, CompareType type) { 20 | super(position); 21 | 22 | this.left = left; 23 | this.right = right; 24 | this.type = type; 25 | } 26 | 27 | @Override 28 | public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) { 29 | Expression cLeft = left.compile(environment, null).eval(environment); 30 | Expression cRight = right.compile(environment, null).eval(environment); 31 | return cLeft.getType().compare(getPosition(), environment, cLeft, cRight, type); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/parser/expression/ParsedExpressionConditional.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.parser.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.expression.ExpressionConditional; 5 | import stanhebben.zenscript.expression.partial.IPartialExpression; 6 | import stanhebben.zenscript.type.ZenType; 7 | import stanhebben.zenscript.util.ZenPosition; 8 | 9 | /** 10 | * @author Stanneke 11 | */ 12 | public class ParsedExpressionConditional extends ParsedExpression { 13 | 14 | private final ParsedExpression condition; 15 | private final ParsedExpression ifThen; 16 | private final ParsedExpression ifElse; 17 | 18 | public ParsedExpressionConditional(ZenPosition position, ParsedExpression condition, ParsedExpression ifThen, ParsedExpression ifElse) { 19 | super(position); 20 | 21 | this.condition = condition; 22 | this.ifThen = ifThen; 23 | this.ifElse = ifElse; 24 | } 25 | 26 | @Override 27 | public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) { 28 | return new ExpressionConditional(getPosition(), condition.compile(environment, ZenType.BOOL).eval(environment), ifThen.compile(environment, predictedType).eval(environment), ifElse.compile(environment, predictedType).eval(environment)); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/parser/expression/ParsedExpressionIndex.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.parser.expression; 2 | 3 | import stanhebben.zenscript.annotations.OperatorType; 4 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 5 | import stanhebben.zenscript.expression.Expression; 6 | import stanhebben.zenscript.expression.partial.IPartialExpression; 7 | import stanhebben.zenscript.type.ZenType; 8 | import stanhebben.zenscript.util.ZenPosition; 9 | 10 | /** 11 | * @author Stanneke 12 | */ 13 | public class ParsedExpressionIndex extends ParsedExpression { 14 | 15 | private final ParsedExpression value; 16 | private final ParsedExpression index; 17 | 18 | public ParsedExpressionIndex(ZenPosition position, ParsedExpression value, ParsedExpression index) { 19 | super(position); 20 | 21 | this.value = value; 22 | this.index = index; 23 | } 24 | 25 | @Override 26 | public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) { 27 | // TODO: improve type prediction for this 28 | Expression cValue = value.compile(environment, null).eval(environment); 29 | Expression cIndex = index.compile(environment, null).eval(environment); 30 | return cValue.getType().binary(getPosition(), environment, cValue, cIndex, OperatorType.INDEXGET); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/parser/expression/ParsedExpressionIndexSet.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.parser.expression; 2 | 3 | import stanhebben.zenscript.annotations.OperatorType; 4 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 5 | import stanhebben.zenscript.expression.Expression; 6 | import stanhebben.zenscript.expression.partial.IPartialExpression; 7 | import stanhebben.zenscript.type.ZenType; 8 | import stanhebben.zenscript.util.ZenPosition; 9 | 10 | /** 11 | * @author Stanneke 12 | */ 13 | public class ParsedExpressionIndexSet extends ParsedExpression { 14 | 15 | private final ParsedExpression value; 16 | private final ParsedExpression index; 17 | private final ParsedExpression setValue; 18 | 19 | public ParsedExpressionIndexSet(ZenPosition position, ParsedExpression value, ParsedExpression index, ParsedExpression setValue) { 20 | super(position); 21 | 22 | this.value = value; 23 | this.index = index; 24 | this.setValue = setValue; 25 | } 26 | 27 | @Override 28 | public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) { 29 | // TODO: improve prediction in this expression 30 | Expression cValue = value.compile(environment, null).eval(environment); 31 | Expression cIndex = index.compile(environment, null).eval(environment); 32 | Expression cSetValue = setValue.compile(environment, null).eval(environment); 33 | return cValue.getType().trinary(getPosition(), environment, cValue, cIndex, cSetValue, OperatorType.INDEXSET); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/parser/expression/ParsedExpressionInstanceOf.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.parser.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.expression.ExpressionInstanceOf; 5 | import stanhebben.zenscript.expression.partial.IPartialExpression; 6 | import stanhebben.zenscript.type.ZenType; 7 | import stanhebben.zenscript.util.ZenPosition; 8 | 9 | public class ParsedExpressionInstanceOf extends ParsedExpression { 10 | 11 | private final ParsedExpression base; 12 | private final ZenType type; 13 | 14 | public ParsedExpressionInstanceOf(ZenPosition position, ParsedExpression base, ZenType type) { 15 | super(position); 16 | this.base = base; 17 | this.type = type; 18 | } 19 | 20 | @Override 21 | public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) { 22 | IPartialExpression ex = base.compile(environment, null); 23 | return new ExpressionInstanceOf(getPosition(), ex.eval(environment), type); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/parser/expression/ParsedExpressionInvalid.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.parser.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.expression.ExpressionInvalid; 5 | import stanhebben.zenscript.expression.partial.IPartialExpression; 6 | import stanhebben.zenscript.type.ZenType; 7 | import stanhebben.zenscript.util.ZenPosition; 8 | 9 | /** 10 | * @author Stanneke 11 | */ 12 | public class ParsedExpressionInvalid extends ParsedExpression { 13 | 14 | public ParsedExpressionInvalid(ZenPosition position) { 15 | super(position); 16 | } 17 | 18 | @Override 19 | public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) { 20 | return new ExpressionInvalid(getPosition(), predictedType == null ? ZenType.ANY : predictedType); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/parser/expression/ParsedExpressionMember.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.parser.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.expression.partial.IPartialExpression; 5 | import stanhebben.zenscript.type.ZenType; 6 | import stanhebben.zenscript.util.ZenPosition; 7 | 8 | /** 9 | * @author Stanneke 10 | */ 11 | public class ParsedExpressionMember extends ParsedExpression { 12 | 13 | private final ParsedExpression value; 14 | private final String member; 15 | 16 | public ParsedExpressionMember(ZenPosition position, ParsedExpression value, String member) { 17 | super(position); 18 | 19 | this.value = value; 20 | this.member = member; 21 | } 22 | 23 | @Override 24 | public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) { 25 | return value.compile(environment, null).getMember(getPosition(), environment, member); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/parser/expression/ParsedExpressionNull.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.parser.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.expression.ExpressionNull; 5 | import stanhebben.zenscript.expression.partial.IPartialExpression; 6 | import stanhebben.zenscript.type.ZenType; 7 | import stanhebben.zenscript.util.ZenPosition; 8 | 9 | /** 10 | * @author Stanneke 11 | */ 12 | public class ParsedExpressionNull extends ParsedExpression { 13 | 14 | public ParsedExpressionNull(ZenPosition position) { 15 | super(position); 16 | } 17 | 18 | @Override 19 | public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) { 20 | return new ExpressionNull(getPosition()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/parser/expression/ParsedExpressionOpAssign.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.parser.expression; 2 | 3 | import stanhebben.zenscript.annotations.OperatorType; 4 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 5 | import stanhebben.zenscript.expression.Expression; 6 | import stanhebben.zenscript.expression.partial.IPartialExpression; 7 | import stanhebben.zenscript.type.ZenType; 8 | import stanhebben.zenscript.util.ZenPosition; 9 | 10 | /** 11 | * @author Stanneke 12 | */ 13 | public class ParsedExpressionOpAssign extends ParsedExpression { 14 | 15 | private final ParsedExpression left; 16 | private final ParsedExpression right; 17 | private final OperatorType operator; 18 | 19 | public ParsedExpressionOpAssign(ZenPosition position, ParsedExpression left, ParsedExpression right, OperatorType operator) { 20 | super(position); 21 | 22 | this.left = left; 23 | this.right = right; 24 | this.operator = operator; 25 | } 26 | 27 | @Override 28 | public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) { 29 | // TODO: validate if the prediction rules are sound 30 | Expression cLeft = left.compile(environment, predictedType).eval(environment); 31 | Expression cRight = right.compile(environment, cLeft.getType()).eval(environment); 32 | 33 | Expression value = cLeft.getType().binary(getPosition(), environment, cLeft, cRight, operator); 34 | 35 | return left.compile(environment, predictedType).assign(getPosition(), environment, value); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/parser/expression/ParsedExpressionOrOr.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.parser.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.expression.*; 5 | import stanhebben.zenscript.expression.partial.IPartialExpression; 6 | import stanhebben.zenscript.type.ZenType; 7 | import stanhebben.zenscript.util.ZenPosition; 8 | 9 | /** 10 | * @author Stanneke 11 | */ 12 | public class ParsedExpressionOrOr extends ParsedExpression { 13 | 14 | private final ParsedExpression left; 15 | private final ParsedExpression right; 16 | 17 | public ParsedExpressionOrOr(ZenPosition position, ParsedExpression left, ParsedExpression right) { 18 | super(position); 19 | 20 | this.left = left; 21 | this.right = right; 22 | } 23 | 24 | @Override 25 | public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) { 26 | Expression cLeft = left.compile(environment, predictedType).eval(environment); 27 | Expression cRight = right.compile(environment, predictedType).eval(environment); 28 | 29 | ZenType type; 30 | if(cRight.getType().canCastImplicit(cLeft.getType(), environment)) { 31 | type = cLeft.getType(); 32 | } else if(cLeft.getType().canCastImplicit(cRight.getType(), environment)) { 33 | type = cRight.getType(); 34 | } else { 35 | environment.error(getPosition(), "These types could not be unified: " + cLeft.getType() + " and " + cRight.getType()); 36 | type = ZenType.ANY; 37 | } 38 | 39 | return new ExpressionOrOr(getPosition(), cLeft.cast(getPosition(), environment, type), cRight.cast(getPosition(), environment, type)); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/parser/expression/ParsedExpressionUnary.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.parser.expression; 2 | 3 | import stanhebben.zenscript.annotations.OperatorType; 4 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 5 | import stanhebben.zenscript.expression.Expression; 6 | import stanhebben.zenscript.expression.partial.IPartialExpression; 7 | import stanhebben.zenscript.type.ZenType; 8 | import stanhebben.zenscript.util.ZenPosition; 9 | 10 | /** 11 | * @author Stanneke 12 | */ 13 | public class ParsedExpressionUnary extends ParsedExpression { 14 | 15 | private final ParsedExpression value; 16 | private final OperatorType operator; 17 | 18 | public ParsedExpressionUnary(ZenPosition position, ParsedExpression value, OperatorType operator) { 19 | super(position); 20 | 21 | this.value = value; 22 | this.operator = operator; 23 | } 24 | 25 | @Override 26 | public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) { 27 | // TODO: improve type predictions? 28 | Expression cValue = value.compile(environment, predictedType).eval(environment); 29 | return cValue.getType().unary(getPosition(), environment, cValue, operator); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/parser/expression/ParsedExpressionValue.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.parser.expression; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.expression.partial.IPartialExpression; 5 | import stanhebben.zenscript.type.ZenType; 6 | import stanhebben.zenscript.util.ZenPosition; 7 | 8 | /** 9 | * @author Stanneke 10 | */ 11 | public class ParsedExpressionValue extends ParsedExpression { 12 | 13 | private final IPartialExpression value; 14 | 15 | public ParsedExpressionValue(ZenPosition position, IPartialExpression value) { 16 | super(position); 17 | 18 | if(value == null) 19 | throw new IllegalArgumentException("value cannot be null"); 20 | 21 | this.value = value; 22 | } 23 | 24 | @Override 25 | public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) { 26 | return value; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/statements/StatementBlock.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.statements; 2 | 3 | import stanhebben.zenscript.compiler.*; 4 | import stanhebben.zenscript.util.ZenPosition; 5 | 6 | import java.util.*; 7 | 8 | public class StatementBlock extends Statement { 9 | 10 | private final List statements; 11 | 12 | public StatementBlock(ZenPosition position, List statements) { 13 | super(position); 14 | 15 | this.statements = statements; 16 | } 17 | 18 | @Override 19 | public void compile(IEnvironmentMethod environment) { 20 | environment.getOutput().position(getPosition()); 21 | IEnvironmentMethod local = new EnvironmentScope(environment); 22 | for(Statement statement : statements) { 23 | statement.compile(local); 24 | if(statement.isReturn()) { 25 | return; 26 | } 27 | } 28 | } 29 | 30 | @Override 31 | public void compile(IEnvironmentMethod environment, boolean forced) { 32 | environment.getOutput().position(getPosition()); 33 | IEnvironmentMethod local = new EnvironmentScope(environment); 34 | for(Statement statement : statements) { 35 | statement.compile(local, forced); 36 | if(statement.isReturn()) { 37 | return; 38 | } 39 | } 40 | } 41 | 42 | @Override 43 | public List getSubStatements() { 44 | List out = new ArrayList<>(); 45 | out.add(this); 46 | for (Statement statement : statements) 47 | out.addAll(statement.getSubStatements()); 48 | return out; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/statements/StatementBreak.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.statements; 2 | 3 | import org.objectweb.asm.Label; 4 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 5 | import stanhebben.zenscript.util.ZenPosition; 6 | 7 | public class StatementBreak extends Statement { 8 | 9 | private Label exit; 10 | 11 | public StatementBreak(ZenPosition position) { 12 | super(position); 13 | } 14 | 15 | public void setExit(Label exit) { 16 | this.exit = exit; 17 | } 18 | 19 | @Override 20 | public void compile(IEnvironmentMethod environment) { 21 | environment.getOutput().position(getPosition()); 22 | if(exit != null) 23 | environment.getOutput().goTo(exit); 24 | else 25 | environment.error(getPosition(), "Skipping break statement as it has no proper label. Only use breaks in loops!"); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/statements/StatementContinue.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.statements; 2 | 3 | import org.objectweb.asm.Label; 4 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 5 | import stanhebben.zenscript.util.ZenPosition; 6 | 7 | public class StatementContinue extends Statement { 8 | 9 | private Label exit; 10 | 11 | public StatementContinue(ZenPosition position) { 12 | super(position); 13 | } 14 | 15 | public void setExit(Label exit) { 16 | this.exit = exit; 17 | } 18 | 19 | @Override 20 | public void compile(IEnvironmentMethod environment) { 21 | environment.getOutput().position(getPosition()); 22 | if(exit != null) 23 | environment.getOutput().goTo(exit); 24 | else 25 | environment.error(getPosition(), "Skipping continue statement as it has no proper label. Only use them in loops!"); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/statements/StatementExpression.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.statements; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.parser.expression.*; 5 | import stanhebben.zenscript.util.ZenPosition; 6 | 7 | public class StatementExpression extends Statement { 8 | 9 | private final ParsedExpression expression; 10 | 11 | public StatementExpression(ZenPosition position, ParsedExpression expression) { 12 | super(position); 13 | 14 | this.expression = expression; 15 | } 16 | 17 | @Override 18 | public void compile(IEnvironmentMethod environment) { 19 | compile(environment, false); 20 | } 21 | 22 | @Override 23 | public void compile(IEnvironmentMethod environment, boolean forced) { 24 | environment.getOutput().position(getPosition()); 25 | //boolean shouldCompile = expression.getClass().getName().equals("stanhebben.zenscript.parser.expression.ParsedExpressionIndexSet"); 26 | boolean shouldCompile = ParsedExpressionIndexSet.class.isInstance(expression); 27 | expression.compile(environment, null).eval(environment).compile(shouldCompile || forced, environment); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/statements/StatementNull.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.statements; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.util.ZenPosition; 5 | 6 | /** 7 | * @author Stanneke 8 | */ 9 | public class StatementNull extends Statement { 10 | 11 | public StatementNull(ZenPosition position) { 12 | super(position); 13 | } 14 | 15 | @Override 16 | public void compile(IEnvironmentMethod environment) { 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/statements/StatementReturn.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.statements; 2 | 3 | import org.objectweb.asm.Type; 4 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 5 | import stanhebben.zenscript.expression.Expression; 6 | import stanhebben.zenscript.parser.expression.ParsedExpression; 7 | import stanhebben.zenscript.type.ZenType; 8 | import stanhebben.zenscript.util.ZenPosition; 9 | 10 | public class StatementReturn extends Statement { 11 | 12 | private final ZenType returnType; 13 | private final ParsedExpression expression; 14 | 15 | public StatementReturn(ZenPosition position, ZenType returnType, ParsedExpression expression) { 16 | super(position); 17 | 18 | this.returnType = returnType; 19 | this.expression = expression; 20 | } 21 | 22 | public ParsedExpression getExpression() { 23 | return expression; 24 | } 25 | 26 | @Override 27 | public boolean isReturn() { 28 | return false; 29 | } 30 | 31 | @Override 32 | public void compile(IEnvironmentMethod environment) { 33 | environment.getOutput().position(getPosition()); 34 | 35 | if(expression == null) { 36 | environment.getOutput().ret(); 37 | } else { 38 | Expression cExpression = expression.compile(environment, returnType).eval(environment); 39 | cExpression.compile(true, environment); 40 | 41 | Type returnType = cExpression.getType().toASMType(); 42 | environment.getOutput().returnType(returnType); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/statements/StatementVar.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.statements; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.expression.Expression; 5 | import stanhebben.zenscript.parser.expression.ParsedExpression; 6 | import stanhebben.zenscript.symbols.SymbolLocal; 7 | import stanhebben.zenscript.type.*; 8 | import stanhebben.zenscript.util.ZenPosition; 9 | 10 | /** 11 | * @author Stanneke 12 | */ 13 | public class StatementVar extends Statement { 14 | 15 | private final String name; 16 | private final ZenType type; 17 | private final ParsedExpression initializer; 18 | private final boolean isFinal; 19 | 20 | public StatementVar(ZenPosition position, String name, ZenType type, ParsedExpression initializer, boolean isFinal) { 21 | super(position); 22 | 23 | this.name = name; 24 | this.type = type; 25 | this.initializer = initializer; 26 | this.isFinal = isFinal; 27 | } 28 | 29 | @Override 30 | public void compile(IEnvironmentMethod environment) { 31 | environment.getOutput().position(getPosition()); 32 | 33 | Expression cInitializer = initializer == null ? null : initializer.compile(environment, type).eval(environment); 34 | ZenType cType = type == null ? (cInitializer == null ? ZenTypeAny.INSTANCE : cInitializer.getType()) : type; 35 | SymbolLocal symbol = new SymbolLocal(cType, isFinal); 36 | 37 | environment.putValue(name, symbol, getPosition()); 38 | 39 | if(cInitializer != null) { 40 | cInitializer.compile(true, environment); 41 | environment.getOutput().store(symbol.getType().toASMType(), environment.getLocal(symbol)); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/symbols/IZenSymbol.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.symbols; 2 | 3 | import stanhebben.zenscript.dump.*; 4 | import stanhebben.zenscript.dump.types.DumpDummy; 5 | import stanhebben.zenscript.expression.partial.IPartialExpression; 6 | import stanhebben.zenscript.util.ZenPosition; 7 | 8 | import java.util.*; 9 | 10 | /** 11 | * @author Stanneke 12 | */ 13 | public interface IZenSymbol extends IDumpConvertable { 14 | 15 | IPartialExpression instance(ZenPosition position); 16 | 17 | @Override 18 | default List asDumpedObject() { 19 | return Collections.singletonList(new DumpDummy(this)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/symbols/SymbolArgument.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.symbols; 2 | 3 | import stanhebben.zenscript.expression.ExpressionArgument; 4 | import stanhebben.zenscript.expression.partial.IPartialExpression; 5 | import stanhebben.zenscript.type.ZenType; 6 | import stanhebben.zenscript.util.ZenPosition; 7 | 8 | /** 9 | * @author Stanneke 10 | */ 11 | public class SymbolArgument implements IZenSymbol { 12 | 13 | private final int id; 14 | private final ZenType type; 15 | 16 | public SymbolArgument(int id, ZenType type) { 17 | this.id = id; 18 | this.type = type; 19 | } 20 | 21 | @Override 22 | public IPartialExpression instance(ZenPosition position) { 23 | return new ExpressionArgument(position, id, type); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/symbols/SymbolJavaClass.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.symbols; 2 | 3 | import stanhebben.zenscript.expression.partial.*; 4 | import stanhebben.zenscript.util.ZenPosition; 5 | 6 | import java.lang.reflect.Type; 7 | 8 | /** 9 | * @author Stanneke 10 | */ 11 | public class SymbolJavaClass implements IZenSymbol { 12 | 13 | private final Type cls; 14 | 15 | public SymbolJavaClass(Type cls) { 16 | this.cls = cls; 17 | } 18 | 19 | @Override 20 | public IPartialExpression instance(ZenPosition position) { 21 | return new PartialJavaClass(position, cls); 22 | } 23 | 24 | @Override 25 | public String toString() { 26 | return "SymbolJavaClass: " + cls.toString(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/symbols/SymbolJavaStaticField.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.symbols; 2 | 3 | import stanhebben.zenscript.compiler.ITypeRegistry; 4 | import stanhebben.zenscript.expression.partial.*; 5 | import stanhebben.zenscript.util.ZenPosition; 6 | 7 | import java.lang.reflect.Field; 8 | 9 | /** 10 | * @author Stan 11 | */ 12 | public class SymbolJavaStaticField implements IZenSymbol { 13 | 14 | private final Class cls; 15 | private final Field field; 16 | private final ITypeRegistry types; 17 | 18 | public SymbolJavaStaticField(Class cls, Field field, ITypeRegistry types) { 19 | this.cls = cls; 20 | this.field = field; 21 | this.types = types; 22 | } 23 | 24 | @Override 25 | public IPartialExpression instance(ZenPosition position) { 26 | return new ExpressionJavaStaticField(position, cls, field, types); 27 | } 28 | 29 | @Override 30 | public String toString() { 31 | return "SymbolJavaStaticField: " + field.toString(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/symbols/SymbolJavaStaticGetter.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.symbols; 2 | 3 | import stanhebben.zenscript.expression.partial.*; 4 | import stanhebben.zenscript.type.natives.IJavaMethod; 5 | import stanhebben.zenscript.util.ZenPosition; 6 | 7 | /** 8 | * @author Stanneke 9 | */ 10 | public class SymbolJavaStaticGetter implements IZenSymbol { 11 | 12 | private final IJavaMethod method; 13 | 14 | public SymbolJavaStaticGetter(IJavaMethod method) { 15 | this.method = method; 16 | } 17 | 18 | @Override 19 | public IPartialExpression instance(ZenPosition position) { 20 | return new PartialStaticGetter(position, method); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/symbols/SymbolJavaStaticMethod.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.symbols; 2 | 3 | import stanhebben.zenscript.expression.partial.*; 4 | import stanhebben.zenscript.type.natives.IJavaMethod; 5 | import stanhebben.zenscript.util.ZenPosition; 6 | 7 | /** 8 | * @author Stanneke 9 | */ 10 | public class SymbolJavaStaticMethod implements IZenSymbol { 11 | 12 | private final IJavaMethod method; 13 | 14 | public SymbolJavaStaticMethod(IJavaMethod method) { 15 | this.method = method; 16 | } 17 | 18 | @Override 19 | public IPartialExpression instance(ZenPosition position) { 20 | return new PartialStaticMethod(position, method); 21 | } 22 | 23 | @Override 24 | public String toString() { 25 | return "SymbolJavaStaticMethod: " + method.toString(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/symbols/SymbolLocal.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.symbols; 2 | 3 | import stanhebben.zenscript.expression.partial.*; 4 | import stanhebben.zenscript.type.ZenType; 5 | import stanhebben.zenscript.util.ZenPosition; 6 | 7 | /** 8 | * @author Stanneke 9 | */ 10 | public class SymbolLocal implements IZenSymbol { 11 | 12 | private final ZenType type; 13 | private final boolean isFinal; 14 | 15 | public SymbolLocal(ZenType type, boolean isFinal) { 16 | this.type = type; 17 | this.isFinal = isFinal; 18 | } 19 | 20 | public ZenType getType() { 21 | return type; 22 | } 23 | 24 | public boolean isFinal() { 25 | return isFinal; 26 | } 27 | 28 | @Override 29 | public IPartialExpression instance(ZenPosition position) { 30 | return new PartialLocal(position, this); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/symbols/SymbolScriptReference.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.symbols; 2 | 3 | import stanhebben.zenscript.compiler.*; 4 | import stanhebben.zenscript.expression.partial.*; 5 | import stanhebben.zenscript.util.ZenPosition; 6 | 7 | import java.util.Arrays; 8 | 9 | public class SymbolScriptReference implements IZenSymbol { 10 | private final PartialScriptReference instance; 11 | 12 | 13 | private SymbolScriptReference() { 14 | this.instance = new PartialScriptReference(); 15 | } 16 | 17 | public SymbolScriptReference(PartialScriptReference instance) { 18 | this.instance = instance; 19 | } 20 | 21 | @Override 22 | public PartialScriptReference instance(ZenPosition position) { 23 | return instance; 24 | } 25 | 26 | public static PartialScriptReference getOrCreateReference(IEnvironmentGlobal environmentGlobal) { 27 | IPartialExpression reference = environmentGlobal.getValue("scripts", null); 28 | if (reference == null) { 29 | environmentGlobal.putValue("scripts", new SymbolScriptReference(), null); 30 | reference = environmentGlobal.getValue("scripts", null); 31 | } 32 | return (PartialScriptReference) reference; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/symbols/SymbolType.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.symbols; 2 | 3 | import stanhebben.zenscript.dump.IDumpable; 4 | import stanhebben.zenscript.expression.partial.*; 5 | import stanhebben.zenscript.type.ZenType; 6 | import stanhebben.zenscript.util.ZenPosition; 7 | 8 | import java.util.*; 9 | 10 | /** 11 | * @author Stan 12 | */ 13 | public class SymbolType implements IZenSymbol { 14 | 15 | private final ZenType type; 16 | 17 | public SymbolType(ZenType type) { 18 | this.type = type; 19 | } 20 | 21 | @Override 22 | public IPartialExpression instance(ZenPosition position) { 23 | return new PartialType(position, type); 24 | } 25 | 26 | @Override 27 | public String toString() { 28 | return "SymbolType: " + type.toString(); 29 | } 30 | 31 | public ZenType getType() { 32 | return type; 33 | } 34 | 35 | @Override 36 | public List asDumpedObject() { 37 | return type.asDumpedObject(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/symbols/SymbolZenClass.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.symbols; 2 | 3 | import stanhebben.zenscript.expression.partial.*; 4 | import stanhebben.zenscript.type.*; 5 | import stanhebben.zenscript.util.*; 6 | 7 | public class SymbolZenClass implements IZenSymbol { 8 | 9 | 10 | private final ZenTypeZenClass type; 11 | 12 | public SymbolZenClass(ZenTypeZenClass type) { 13 | this.type = type; 14 | } 15 | 16 | @Override 17 | public IPartialExpression instance(ZenPosition position) { 18 | return new PartialZSClass(type); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/symbols/SymbolZenStaticMethod.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.symbols; 2 | 3 | import stanhebben.zenscript.definitions.ParsedFunctionArgument; 4 | import stanhebben.zenscript.expression.partial.IPartialExpression; 5 | import stanhebben.zenscript.expression.partial.PartialStaticGenerated; 6 | import stanhebben.zenscript.type.ZenType; 7 | import stanhebben.zenscript.util.ZenPosition; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * @author Stan 13 | */ 14 | public class SymbolZenStaticMethod implements IZenSymbol { 15 | 16 | private final String className; 17 | private final String methodName; 18 | private final String signature; 19 | private final List arguments; 20 | private final ZenType returnType; 21 | 22 | public SymbolZenStaticMethod(String className, String methodName, String signature, List arguments, ZenType returnType) { 23 | this.className = className; 24 | this.methodName = methodName; 25 | this.signature = signature; 26 | this.arguments = arguments; 27 | this.returnType = returnType; 28 | } 29 | 30 | @Override 31 | public IPartialExpression instance(ZenPosition position) { 32 | return new PartialStaticGenerated(position, className, methodName, signature, arguments, returnType); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/IZenIterator.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type; 2 | 3 | import org.objectweb.asm.Label; 4 | 5 | /** 6 | * @author Stanneke 7 | */ 8 | public interface IZenIterator { 9 | 10 | /** 11 | * Compiles the header before the iteration. The list is on the top of the 12 | * stack. 13 | * 14 | * @param locals 15 | */ 16 | void compileStart(int[] locals); 17 | 18 | /** 19 | * Compiles the start of an iteration. The stack is unmodified from the 20 | * previous iteration and from the iteration start. 21 | * 22 | * @param locals 23 | * @param exit 24 | */ 25 | void compilePreIterate(int[] locals, Label exit); 26 | 27 | /** 28 | * Compiles the end of an iteration. The stack is the same as it was after 29 | * preIterate. 30 | * 31 | * @param locals 32 | * @param exit 33 | * @param repeat 34 | */ 35 | void compilePostIterate(int[] locals, Label exit, Label repeat); 36 | 37 | /** 38 | * Compiles the end of the whole iteration. 39 | */ 40 | void compileEnd(); 41 | 42 | ZenType getType(int i); 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/BaseCastingRule.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | 6 | /** 7 | * @author Stan 8 | */ 9 | public abstract class BaseCastingRule implements ICastingRule { 10 | 11 | private final ICastingRule baseRule; 12 | 13 | public BaseCastingRule(ICastingRule baseRule) { 14 | this.baseRule = baseRule; 15 | } 16 | 17 | @Override 18 | public final void compile(IEnvironmentMethod method) { 19 | if(baseRule != null) 20 | baseRule.compile(method); 21 | 22 | compileInner(method); 23 | } 24 | 25 | @Override 26 | public final ZenType getInputType() { 27 | return baseRule == null ? getInnerInputType() : baseRule.getInputType(); 28 | } 29 | 30 | public abstract ZenType getInnerInputType(); 31 | 32 | public abstract void compileInner(IEnvironmentMethod method); 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingAnyBool.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | import stanhebben.zenscript.value.IAny; 6 | 7 | /** 8 | * @author Stan 9 | */ 10 | public class CastingAnyBool implements ICastingRule { 11 | 12 | public static final CastingAnyBool INSTANCE = new CastingAnyBool(); 13 | 14 | private CastingAnyBool() { 15 | } 16 | 17 | @Override 18 | public void compile(IEnvironmentMethod method) { 19 | method.getOutput().invokeInterface(IAny.class, "asBool", boolean.class); 20 | } 21 | 22 | @Override 23 | public ZenType getInputType() { 24 | return ZenType.ANY; 25 | } 26 | 27 | @Override 28 | public ZenType getResultingType() { 29 | return ZenType.BOOL; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingAnyByte.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | import stanhebben.zenscript.value.IAny; 6 | 7 | /** 8 | * @author Stan 9 | */ 10 | public class CastingAnyByte implements ICastingRule { 11 | 12 | public static final CastingAnyByte INSTANCE = new CastingAnyByte(); 13 | 14 | private CastingAnyByte() { 15 | } 16 | 17 | @Override 18 | public void compile(IEnvironmentMethod method) { 19 | method.getOutput().invokeInterface(IAny.class, "asByte", byte.class); 20 | } 21 | 22 | @Override 23 | public ZenType getInputType() { 24 | return ZenType.ANY; 25 | } 26 | 27 | @Override 28 | public ZenType getResultingType() { 29 | return ZenType.BYTE; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingAnyDouble.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | import stanhebben.zenscript.value.IAny; 6 | 7 | /** 8 | * @author Stan 9 | */ 10 | public class CastingAnyDouble implements ICastingRule { 11 | 12 | public static final CastingAnyDouble INSTANCE = new CastingAnyDouble(); 13 | 14 | private CastingAnyDouble() { 15 | } 16 | 17 | @Override 18 | public void compile(IEnvironmentMethod method) { 19 | method.getOutput().invokeInterface(IAny.class, "asDouble", double.class); 20 | } 21 | 22 | @Override 23 | public ZenType getInputType() { 24 | return ZenType.ANY; 25 | } 26 | 27 | @Override 28 | public ZenType getResultingType() { 29 | return ZenType.DOUBLE; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingAnyFloat.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | import stanhebben.zenscript.value.IAny; 6 | 7 | /** 8 | * @author Stan 9 | */ 10 | public class CastingAnyFloat implements ICastingRule { 11 | 12 | public static final CastingAnyFloat INSTANCE = new CastingAnyFloat(); 13 | 14 | private CastingAnyFloat() { 15 | } 16 | 17 | @Override 18 | public void compile(IEnvironmentMethod method) { 19 | method.getOutput().invokeInterface(IAny.class, "asFloat", float.class); 20 | } 21 | 22 | @Override 23 | public ZenType getInputType() { 24 | return ZenType.ANY; 25 | } 26 | 27 | @Override 28 | public ZenType getResultingType() { 29 | return ZenType.FLOAT; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingAnyInt.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | import stanhebben.zenscript.value.IAny; 6 | 7 | /** 8 | * @author Stan 9 | */ 10 | public class CastingAnyInt implements ICastingRule { 11 | 12 | public static final CastingAnyInt INSTANCE = new CastingAnyInt(); 13 | 14 | private CastingAnyInt() { 15 | } 16 | 17 | @Override 18 | public void compile(IEnvironmentMethod method) { 19 | method.getOutput().invokeInterface(IAny.class, "asInt", int.class); 20 | } 21 | 22 | @Override 23 | public ZenType getInputType() { 24 | return ZenType.ANY; 25 | } 26 | 27 | @Override 28 | public ZenType getResultingType() { 29 | return ZenType.INT; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingAnyLong.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | import stanhebben.zenscript.value.IAny; 6 | 7 | /** 8 | * @author Stan 9 | */ 10 | public class CastingAnyLong implements ICastingRule { 11 | 12 | public static final CastingAnyLong INSTANCE = new CastingAnyLong(); 13 | 14 | private CastingAnyLong() { 15 | } 16 | 17 | @Override 18 | public void compile(IEnvironmentMethod method) { 19 | method.getOutput().invokeInterface(IAny.class, "asLong", long.class); 20 | } 21 | 22 | @Override 23 | public ZenType getInputType() { 24 | return ZenType.ANY; 25 | } 26 | 27 | @Override 28 | public ZenType getResultingType() { 29 | return ZenType.LONG; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingAnyShort.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | import stanhebben.zenscript.value.IAny; 6 | 7 | /** 8 | * @author Stan 9 | */ 10 | public class CastingAnyShort implements ICastingRule { 11 | 12 | public static final CastingAnyShort INSTANCE = new CastingAnyShort(); 13 | 14 | private CastingAnyShort() { 15 | } 16 | 17 | @Override 18 | public void compile(IEnvironmentMethod method) { 19 | method.getOutput().invokeInterface(IAny.class, "asShort", short.class); 20 | } 21 | 22 | @Override 23 | public ZenType getInputType() { 24 | return ZenType.ANY; 25 | } 26 | 27 | @Override 28 | public ZenType getResultingType() { 29 | return ZenType.SHORT; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingAnyString.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import org.objectweb.asm.Label; 4 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 5 | import stanhebben.zenscript.type.ZenType; 6 | import stanhebben.zenscript.util.MethodOutput; 7 | import stanhebben.zenscript.value.IAny; 8 | 9 | /** 10 | * @author Stan 11 | */ 12 | public class CastingAnyString implements ICastingRule { 13 | 14 | public static final CastingAnyString INSTANCE = new CastingAnyString(); 15 | 16 | private CastingAnyString() { 17 | } 18 | 19 | @Override 20 | public void compile(IEnvironmentMethod method) { 21 | //IAny is not and probably will not be implemented, so let's at least use a method that works! 22 | method.getOutput().invokeVirtual(Object.class, "toString", String.class); 23 | } 24 | 25 | @Override 26 | public ZenType getInputType() { 27 | return ZenType.ANY; 28 | } 29 | 30 | @Override 31 | public ZenType getResultingType() { 32 | return ZenType.STRING; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingAnySubtype.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | 6 | import static stanhebben.zenscript.util.ZenTypeUtil.internal; 7 | 8 | public class CastingAnySubtype implements ICastingRule { 9 | 10 | private final ZenType fromType; 11 | private final ZenType toType; 12 | 13 | public CastingAnySubtype(ZenType fromType, ZenType toType) { 14 | this.fromType = fromType; 15 | this.toType = toType; 16 | } 17 | 18 | @Override 19 | public void compile(IEnvironmentMethod method) { 20 | method.getOutput().checkCast(internal(toType.toJavaClass())); 21 | } 22 | 23 | @Override 24 | public ZenType getInputType() { 25 | return fromType; 26 | } 27 | 28 | @Override 29 | public ZenType getResultingType() { 30 | return toType; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingNotNull.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import org.objectweb.asm.Label; 4 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 5 | import stanhebben.zenscript.type.ZenType; 6 | import stanhebben.zenscript.util.MethodOutput; 7 | 8 | /** 9 | * @author Stan 10 | */ 11 | public class CastingNotNull implements ICastingRule { 12 | 13 | public ZenType fromType; 14 | 15 | public CastingNotNull(ZenType fromType) { 16 | this.fromType = fromType; 17 | } 18 | 19 | @Override 20 | public void compile(IEnvironmentMethod method) { 21 | MethodOutput output = method.getOutput(); 22 | Label labelElse = new Label(); 23 | Label labelAfter = new Label(); 24 | 25 | output.ifNull(labelElse); 26 | output.iConst1(); 27 | output.goTo(labelAfter); 28 | output.label(labelElse); 29 | output.iConst0(); 30 | output.label(labelAfter); 31 | } 32 | 33 | @Override 34 | public ZenType getInputType() { 35 | return fromType; 36 | } 37 | 38 | @Override 39 | public ZenType getResultingType() { 40 | return ZenType.BOOL; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleAny.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | import stanhebben.zenscript.value.IAny; 6 | 7 | /** 8 | * @author Stan 9 | */ 10 | public class CastingRuleAny implements ICastingRule { 11 | 12 | private final ZenType toType; 13 | 14 | public CastingRuleAny(ZenType toType) { 15 | this.toType = toType; 16 | } 17 | 18 | @Override 19 | public void compile(IEnvironmentMethod method) { 20 | method.getOutput().constant(toType); 21 | method.getOutput().invokeInterface(IAny.class, "as", Class.class, Object.class); 22 | } 23 | 24 | @Override 25 | public ZenType getInputType() { 26 | return ZenType.ANY; 27 | } 28 | 29 | @Override 30 | public ZenType getResultingType() { 31 | return toType; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleAnyAs.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import org.objectweb.asm.Type; 4 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 5 | import stanhebben.zenscript.type.ZenType; 6 | import stanhebben.zenscript.value.IAny; 7 | 8 | /** 9 | * @author Stan 10 | */ 11 | public class CastingRuleAnyAs implements ICastingRule { 12 | 13 | private final ZenType type; 14 | 15 | public CastingRuleAnyAs(ZenType type) { 16 | this.type = type; 17 | } 18 | 19 | @Override 20 | public void compile(IEnvironmentMethod method) { 21 | method.getOutput().constant(Type.getType(type.toJavaClass())); 22 | method.getOutput().invokeInterface(IAny.class, "as", Class.class, Object.class); 23 | } 24 | 25 | @Override 26 | public ZenType getInputType() { 27 | return ZenType.ANY; 28 | } 29 | 30 | @Override 31 | public ZenType getResultingType() { 32 | return type; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleD2F.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | 6 | /** 7 | * @author Stan 8 | */ 9 | public class CastingRuleD2F extends BaseCastingRule { 10 | 11 | public CastingRuleD2F(ICastingRule baseRule) { 12 | super(baseRule); 13 | } 14 | 15 | @Override 16 | public void compileInner(IEnvironmentMethod method) { 17 | method.getOutput().d2f(); 18 | } 19 | 20 | @Override 21 | public ZenType getInnerInputType() { 22 | return ZenType.DOUBLE; 23 | } 24 | 25 | @Override 26 | public ZenType getResultingType() { 27 | return ZenType.FLOAT; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleD2I.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | 6 | /** 7 | * @author Stan 8 | */ 9 | public class CastingRuleD2I extends BaseCastingRule { 10 | 11 | public CastingRuleD2I(ICastingRule baseRule) { 12 | super(baseRule); 13 | } 14 | 15 | @Override 16 | public void compileInner(IEnvironmentMethod method) { 17 | method.getOutput().d2i(); 18 | } 19 | 20 | @Override 21 | public ZenType getInnerInputType() { 22 | return ZenType.DOUBLE; 23 | } 24 | 25 | @Override 26 | public ZenType getResultingType() { 27 | return ZenType.INT; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleD2L.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | 6 | /** 7 | * @author Stan 8 | */ 9 | public class CastingRuleD2L extends BaseCastingRule { 10 | 11 | public CastingRuleD2L(ICastingRule baseRule) { 12 | super(baseRule); 13 | } 14 | 15 | @Override 16 | public void compileInner(IEnvironmentMethod method) { 17 | method.getOutput().d2l(); 18 | } 19 | 20 | @Override 21 | public ZenType getInnerInputType() { 22 | return ZenType.DOUBLE; 23 | } 24 | 25 | @Override 26 | public ZenType getResultingType() { 27 | return ZenType.LONG; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleDelegateArray.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.type.*; 4 | 5 | /** 6 | * @author Stan 7 | */ 8 | public class CastingRuleDelegateArray implements ICastingRuleDelegate { 9 | 10 | private final ICastingRuleDelegate base; 11 | private final ZenTypeArrayBasic from; 12 | 13 | public CastingRuleDelegateArray(ICastingRuleDelegate base, ZenTypeArrayBasic from) { 14 | this.base = base; 15 | this.from = from; 16 | } 17 | 18 | @Override 19 | public void registerCastingRule(ZenType type, ICastingRule rule) { 20 | base.registerCastingRule(new ZenTypeArrayBasic(type), new CastingRuleArrayArray(rule, from, new ZenTypeArrayBasic(type))); 21 | base.registerCastingRule(new ZenTypeArrayList(type), new CastingRuleArrayList(rule, from, new ZenTypeArrayList(type))); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleDelegateList.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.type.*; 4 | 5 | /** 6 | * @author Stan 7 | */ 8 | public class CastingRuleDelegateList implements ICastingRuleDelegate { 9 | 10 | private final ICastingRuleDelegate base; 11 | private final ZenTypeArrayList from; 12 | 13 | public CastingRuleDelegateList(ICastingRuleDelegate base, ZenTypeArrayList from) { 14 | this.base = base; 15 | this.from = from; 16 | } 17 | 18 | @Override 19 | public void registerCastingRule(ZenType type, ICastingRule rule) { 20 | base.registerCastingRule(new ZenTypeArrayBasic(type), new CastingRuleListArray(rule, from, new ZenTypeArrayBasic(type))); 21 | base.registerCastingRule(new ZenTypeArrayList(type), new CastingRuleListList(rule, from, new ZenTypeArrayList(type))); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleDelegateMap.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.type.ZenType; 4 | 5 | import java.util.Map; 6 | 7 | /** 8 | * @author Stan 9 | */ 10 | public class CastingRuleDelegateMap implements ICastingRuleDelegate { 11 | 12 | private final ZenType forType; 13 | private final Map target; 14 | 15 | public CastingRuleDelegateMap(ZenType forType, Map target) { 16 | this.forType = forType; 17 | this.target = target; 18 | } 19 | 20 | @Override 21 | public void registerCastingRule(ZenType type, ICastingRule rule) { 22 | // XXX: remove before release 23 | // System.out.println("Registering casting rule to convert " + forType + 24 | // " to " + type + ": " + rule); 25 | 26 | target.put(type, rule); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleDelegateStaticMethod.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.type.ZenType; 4 | import stanhebben.zenscript.type.natives.IJavaMethod; 5 | 6 | /** 7 | * @author Stan 8 | */ 9 | public class CastingRuleDelegateStaticMethod implements ICastingRuleDelegate { 10 | 11 | private final ICastingRuleDelegate target; 12 | private final IJavaMethod method; 13 | 14 | public CastingRuleDelegateStaticMethod(ICastingRuleDelegate target, IJavaMethod method) { 15 | this.target = target; 16 | this.method = method; 17 | } 18 | 19 | @Override 20 | public void registerCastingRule(ZenType type, ICastingRule rule) { 21 | target.registerCastingRule(type, new CastingRuleStaticMethod(method, rule)); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleF2D.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | 6 | /** 7 | * @author Stan 8 | */ 9 | public class CastingRuleF2D extends BaseCastingRule { 10 | 11 | public CastingRuleF2D(ICastingRule baseRule) { 12 | super(baseRule); 13 | } 14 | 15 | @Override 16 | public void compileInner(IEnvironmentMethod method) { 17 | method.getOutput().f2d(); 18 | } 19 | 20 | @Override 21 | public ZenType getInnerInputType() { 22 | return ZenType.FLOAT; 23 | } 24 | 25 | @Override 26 | public ZenType getResultingType() { 27 | return ZenType.DOUBLE; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleF2I.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | 6 | /** 7 | * @author Stan 8 | */ 9 | public class CastingRuleF2I extends BaseCastingRule { 10 | 11 | public CastingRuleF2I(ICastingRule baseRule) { 12 | super(baseRule); 13 | } 14 | 15 | @Override 16 | public void compileInner(IEnvironmentMethod method) { 17 | method.getOutput().f2i(); 18 | } 19 | 20 | @Override 21 | public ZenType getInnerInputType() { 22 | return ZenType.FLOAT; 23 | } 24 | 25 | @Override 26 | public ZenType getResultingType() { 27 | return ZenType.INT; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleF2L.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | 6 | /** 7 | * @author Stan 8 | */ 9 | public class CastingRuleF2L extends BaseCastingRule { 10 | 11 | public CastingRuleF2L(ICastingRule baseRule) { 12 | super(baseRule); 13 | } 14 | 15 | @Override 16 | public void compileInner(IEnvironmentMethod method) { 17 | method.getOutput().f2l(); 18 | } 19 | 20 | @Override 21 | public ZenType getInnerInputType() { 22 | return ZenType.FLOAT; 23 | } 24 | 25 | @Override 26 | public ZenType getResultingType() { 27 | return ZenType.LONG; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleI2B.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | 6 | /** 7 | * @author Stan 8 | */ 9 | public class CastingRuleI2B extends BaseCastingRule { 10 | 11 | public CastingRuleI2B(ICastingRule baseRule) { 12 | super(baseRule); 13 | } 14 | 15 | @Override 16 | public void compileInner(IEnvironmentMethod method) { 17 | method.getOutput().i2b(); 18 | } 19 | 20 | @Override 21 | public ZenType getInnerInputType() { 22 | return ZenType.INT; 23 | } 24 | 25 | @Override 26 | public ZenType getResultingType() { 27 | return ZenType.BYTE; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleI2D.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | 6 | /** 7 | * @author Stan 8 | */ 9 | public class CastingRuleI2D extends BaseCastingRule { 10 | 11 | public CastingRuleI2D(ICastingRule baseRule) { 12 | super(baseRule); 13 | } 14 | 15 | @Override 16 | public void compileInner(IEnvironmentMethod method) { 17 | method.getOutput().i2d(); 18 | } 19 | 20 | @Override 21 | public ZenType getInnerInputType() { 22 | return ZenType.INT; 23 | } 24 | 25 | @Override 26 | public ZenType getResultingType() { 27 | return ZenType.DOUBLE; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleI2F.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | 6 | /** 7 | * @author Stan 8 | */ 9 | public class CastingRuleI2F extends BaseCastingRule { 10 | 11 | public CastingRuleI2F(ICastingRule baseRule) { 12 | super(baseRule); 13 | } 14 | 15 | @Override 16 | public void compileInner(IEnvironmentMethod method) { 17 | method.getOutput().i2f(); 18 | } 19 | 20 | @Override 21 | public ZenType getInnerInputType() { 22 | return ZenType.INT; 23 | } 24 | 25 | @Override 26 | public ZenType getResultingType() { 27 | return ZenType.FLOAT; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleI2L.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | 6 | /** 7 | * @author Stan 8 | */ 9 | public class CastingRuleI2L extends BaseCastingRule { 10 | 11 | public CastingRuleI2L(ICastingRule baseRule) { 12 | super(baseRule); 13 | } 14 | 15 | @Override 16 | public void compileInner(IEnvironmentMethod method) { 17 | method.getOutput().i2l(); 18 | } 19 | 20 | @Override 21 | public ZenType getInnerInputType() { 22 | return ZenType.INT; 23 | } 24 | 25 | @Override 26 | public ZenType getResultingType() { 27 | return ZenType.LONG; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleI2S.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | 6 | /** 7 | * @author Stan 8 | */ 9 | public class CastingRuleI2S extends BaseCastingRule { 10 | 11 | public CastingRuleI2S(ICastingRule baseRule) { 12 | super(baseRule); 13 | } 14 | 15 | @Override 16 | public void compileInner(IEnvironmentMethod method) { 17 | method.getOutput().i2s(); 18 | } 19 | 20 | @Override 21 | public ZenType getInnerInputType() { 22 | return ZenType.INT; 23 | } 24 | 25 | @Override 26 | public ZenType getResultingType() { 27 | return ZenType.SHORT; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleL2D.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | 6 | /** 7 | * @author Stan 8 | */ 9 | public class CastingRuleL2D extends BaseCastingRule { 10 | 11 | public CastingRuleL2D(ICastingRule baseRule) { 12 | super(baseRule); 13 | } 14 | 15 | @Override 16 | public void compileInner(IEnvironmentMethod method) { 17 | method.getOutput().l2d(); 18 | } 19 | 20 | @Override 21 | public ZenType getInnerInputType() { 22 | return ZenType.LONG; 23 | } 24 | 25 | @Override 26 | public ZenType getResultingType() { 27 | return ZenType.DOUBLE; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleL2F.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | 6 | /** 7 | * @author Stan 8 | */ 9 | public class CastingRuleL2F extends BaseCastingRule { 10 | 11 | public CastingRuleL2F(ICastingRule baseRule) { 12 | super(baseRule); 13 | } 14 | 15 | @Override 16 | public void compileInner(IEnvironmentMethod method) { 17 | method.getOutput().l2f(); 18 | } 19 | 20 | @Override 21 | public ZenType getInnerInputType() { 22 | return ZenType.LONG; 23 | } 24 | 25 | @Override 26 | public ZenType getResultingType() { 27 | return ZenType.FLOAT; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleL2I.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | 6 | /** 7 | * @author Stan 8 | */ 9 | public class CastingRuleL2I extends BaseCastingRule { 10 | 11 | public CastingRuleL2I(ICastingRule baseRule) { 12 | super(baseRule); 13 | } 14 | 15 | @Override 16 | public void compileInner(IEnvironmentMethod method) { 17 | method.getOutput().l2i(); 18 | } 19 | 20 | @Override 21 | public ZenType getInnerInputType() { 22 | return ZenType.LONG; 23 | } 24 | 25 | @Override 26 | public ZenType getResultingType() { 27 | return ZenType.INT; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleListList.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.*; 5 | 6 | /** 7 | * @author Stan 8 | */ 9 | public class CastingRuleListList implements ICastingRule { 10 | 11 | private final ICastingRule base; 12 | private final ZenTypeArrayList from; 13 | private final ZenTypeArrayList to; 14 | 15 | public CastingRuleListList(ICastingRule base, ZenTypeArrayList from, ZenTypeArrayList to) { 16 | this.base = base; 17 | this.from = from; 18 | this.to = to; 19 | } 20 | 21 | @Override 22 | public void compile(IEnvironmentMethod method) { 23 | 24 | // TODO: implement this 25 | throw new UnsupportedOperationException("Not yet implemented"); 26 | 27 | /* 28 | * Type fromType = componentFrom.toASMType(); Type toType = 29 | * componentTo.toASMType(); 30 | * 31 | * int result = output.local(List.class); int iterator = 32 | * output.local(Iterator.class); 33 | * 34 | * // construct new list output.dup(); 35 | * 36 | * 37 | * output.invoke(List.class, "iterator", void.class); 38 | * output.storeObject(iterator); 39 | * 40 | * Label loop = new Label(); output.label(loop); 41 | * output.loadObject(iterator); 42 | * 43 | * 44 | * if (base != null) base.compile(method); 45 | * 46 | * 47 | * 48 | * output.pop(); 49 | * 50 | * output.loadObject(result); 51 | */ 52 | } 53 | 54 | @Override 55 | public ZenType getInputType() { 56 | return from; 57 | } 58 | 59 | @Override 60 | public ZenType getResultingType() { 61 | return to; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleMap.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.*; 5 | 6 | /** 7 | * @author Stan 8 | */ 9 | public class CastingRuleMap implements ICastingRule { 10 | 11 | private final ICastingRule keyRule; 12 | private final ICastingRule valueRule; 13 | private final ZenTypeAssociative fromType; 14 | private final ZenTypeAssociative toType; 15 | 16 | public CastingRuleMap(ICastingRule keyRule, ICastingRule valueRule, ZenTypeAssociative fromType, ZenTypeAssociative toType) { 17 | this.keyRule = keyRule; 18 | this.valueRule = valueRule; 19 | this.fromType = fromType; 20 | this.toType = toType; 21 | } 22 | 23 | @Override 24 | public void compile(IEnvironmentMethod method) { 25 | throw new UnsupportedOperationException("Not supported yet."); 26 | } 27 | 28 | @Override 29 | public ZenType getInputType() { 30 | return fromType; 31 | } 32 | 33 | @Override 34 | public ZenType getResultingType() { 35 | return toType; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleNone.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | 6 | /** 7 | * @author Stan 8 | */ 9 | public class CastingRuleNone implements ICastingRule { 10 | 11 | private final ZenType fromType; 12 | private final ZenType toType; 13 | 14 | public CastingRuleNone(ZenType fromType, ZenType toType) { 15 | this.fromType = fromType; 16 | this.toType = toType; 17 | } 18 | 19 | @Override 20 | public void compile(IEnvironmentMethod method) { 21 | // nothing to do 22 | } 23 | 24 | @Override 25 | public ZenType getInputType() { 26 | return fromType; 27 | } 28 | 29 | @Override 30 | public ZenType getResultingType() { 31 | return toType; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleNullableStaticMethod.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import org.objectweb.asm.Label; 4 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 5 | import stanhebben.zenscript.type.ZenType; 6 | import stanhebben.zenscript.type.natives.IJavaMethod; 7 | import stanhebben.zenscript.util.MethodOutput; 8 | 9 | /** 10 | * @author Stan 11 | */ 12 | public class CastingRuleNullableStaticMethod implements ICastingRule { 13 | 14 | private final IJavaMethod method; 15 | private final ICastingRule base; 16 | 17 | public CastingRuleNullableStaticMethod(IJavaMethod method) { 18 | this.method = method; 19 | base = null; 20 | } 21 | 22 | public CastingRuleNullableStaticMethod(IJavaMethod method, ICastingRule base) { 23 | this.method = method; 24 | this.base = base; 25 | } 26 | 27 | @Override 28 | public void compile(IEnvironmentMethod method) { 29 | MethodOutput output = method.getOutput(); 30 | 31 | Label lblNotNull = new Label(); 32 | Label lblAfter = new Label(); 33 | 34 | output.dup(); 35 | output.ifNonNull(lblNotNull); 36 | output.pop(); 37 | output.aConstNull(); 38 | output.goTo(lblAfter); 39 | 40 | output.label(lblNotNull); 41 | 42 | if(base != null) { 43 | base.compile(method); 44 | } 45 | 46 | this.method.invokeStatic(method.getOutput()); 47 | 48 | output.label(lblAfter); 49 | } 50 | 51 | @Override 52 | public ZenType getInputType() { 53 | return method.getParameterTypes()[0]; 54 | } 55 | 56 | @Override 57 | public ZenType getResultingType() { 58 | return method.getReturnType(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleNullableVirtualMethod.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import org.objectweb.asm.Label; 4 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 5 | import stanhebben.zenscript.type.ZenType; 6 | import stanhebben.zenscript.type.natives.IJavaMethod; 7 | import stanhebben.zenscript.util.MethodOutput; 8 | 9 | /** 10 | * @author Stan 11 | */ 12 | public class CastingRuleNullableVirtualMethod implements ICastingRule { 13 | 14 | private final ZenType type; 15 | private final IJavaMethod method; 16 | 17 | public CastingRuleNullableVirtualMethod(ZenType type, IJavaMethod method) { 18 | this.type = type; 19 | this.method = method; 20 | } 21 | 22 | @Override 23 | public void compile(IEnvironmentMethod method) { 24 | MethodOutput output = method.getOutput(); 25 | 26 | Label lblNotNull = new Label(); 27 | Label lblAfter = new Label(); 28 | 29 | output.dup(); 30 | output.ifNonNull(lblNotNull); 31 | output.pop(); 32 | output.aConstNull(); 33 | output.goTo(lblAfter); 34 | 35 | output.label(lblNotNull); 36 | this.method.invokeVirtual(method.getOutput()); 37 | 38 | output.label(lblAfter); 39 | } 40 | 41 | @Override 42 | public ZenType getInputType() { 43 | return type; 44 | } 45 | 46 | @Override 47 | public ZenType getResultingType() { 48 | return method.getReturnType(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleStaticMethod.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | import stanhebben.zenscript.type.natives.IJavaMethod; 6 | 7 | /** 8 | * @author Stan 9 | */ 10 | public class CastingRuleStaticMethod implements ICastingRule { 11 | 12 | private final IJavaMethod method; 13 | private final ICastingRule base; 14 | 15 | public CastingRuleStaticMethod(IJavaMethod method) { 16 | this.method = method; 17 | this.base = null; 18 | } 19 | 20 | public CastingRuleStaticMethod(IJavaMethod method, ICastingRule base) { 21 | this.method = method; 22 | this.base = base; 23 | } 24 | 25 | @Override 26 | public void compile(IEnvironmentMethod method) { 27 | if(base != null) 28 | base.compile(method); 29 | 30 | this.method.invokeStatic(method.getOutput()); 31 | } 32 | 33 | @Override 34 | public ZenType getInputType() { 35 | return method.getParameterTypes()[0]; 36 | } 37 | 38 | @Override 39 | public ZenType getResultingType() { 40 | return method.getReturnType(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/CastingRuleVirtualMethod.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | import stanhebben.zenscript.type.natives.IJavaMethod; 6 | 7 | /** 8 | * @author Stan 9 | */ 10 | public class CastingRuleVirtualMethod implements ICastingRule { 11 | 12 | private final IJavaMethod method; 13 | 14 | public CastingRuleVirtualMethod(IJavaMethod method) { 15 | this.method = method; 16 | } 17 | 18 | @Override 19 | public void compile(IEnvironmentMethod method) { 20 | this.method.invokeVirtual(method.getOutput()); 21 | } 22 | 23 | @Override 24 | public ZenType getInputType() { 25 | return method.getParameterTypes()[0]; 26 | } 27 | 28 | @Override 29 | public ZenType getResultingType() { 30 | return method.getReturnType(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/ICastingRule.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 4 | import stanhebben.zenscript.type.ZenType; 5 | 6 | /** 7 | * @author Stan 8 | */ 9 | public interface ICastingRule { 10 | 11 | void compile(IEnvironmentMethod method); 12 | 13 | ZenType getInputType(); 14 | 15 | ZenType getResultingType(); 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/casting/ICastingRuleDelegate.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.casting; 2 | 3 | import stanhebben.zenscript.type.ZenType; 4 | 5 | /** 6 | * @author Stan 7 | */ 8 | public interface ICastingRuleDelegate { 9 | 10 | void registerCastingRule(ZenType type, ICastingRule rule); 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/expand/ZenExpandCaster.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.expand; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentGlobal; 4 | import stanhebben.zenscript.expression.Expression; 5 | import stanhebben.zenscript.expression.ExpressionCallStatic; 6 | import stanhebben.zenscript.type.ZenType; 7 | import stanhebben.zenscript.type.casting.CastingRuleDelegateStaticMethod; 8 | import stanhebben.zenscript.type.casting.CastingRuleStaticMethod; 9 | import stanhebben.zenscript.type.casting.ICastingRuleDelegate; 10 | import stanhebben.zenscript.type.natives.IJavaMethod; 11 | import stanhebben.zenscript.util.MethodOutput; 12 | import stanhebben.zenscript.util.ZenPosition; 13 | 14 | /** 15 | * @author Stanneke 16 | */ 17 | public class ZenExpandCaster { 18 | 19 | private final IJavaMethod method; 20 | 21 | public ZenExpandCaster(IJavaMethod method) { 22 | this.method = method; 23 | } 24 | 25 | public ZenType getTarget() { 26 | return method.getReturnType(); 27 | } 28 | 29 | public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules) { 30 | ZenType type = method.getReturnType(); 31 | rules.registerCastingRule(type, new CastingRuleStaticMethod(method)); 32 | 33 | type.constructCastingRules(environment, new CastingRuleDelegateStaticMethod(rules, method), false); 34 | } 35 | 36 | public Expression cast(ZenPosition position, IEnvironmentGlobal environment, Expression expression) { 37 | return new ExpressionCallStatic(position, environment, method, expression); 38 | } 39 | 40 | public void compile(MethodOutput output) { 41 | method.invokeStatic(output); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/iterator/IteratorIterable.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.iterator; 2 | 3 | import org.objectweb.asm.*; 4 | import stanhebben.zenscript.type.*; 5 | import stanhebben.zenscript.util.MethodOutput; 6 | 7 | import java.util.Iterator; 8 | 9 | /** 10 | * @author Stan 11 | */ 12 | public class IteratorIterable implements IZenIterator { 13 | 14 | private final MethodOutput methodOutput; 15 | private final ZenType iteratorType; 16 | private int iterator; 17 | 18 | public IteratorIterable(MethodOutput methodOutput, ZenType iteratorType) { 19 | this.methodOutput = methodOutput; 20 | this.iteratorType = iteratorType; 21 | } 22 | 23 | @Override 24 | public void compileStart(int[] locals) { 25 | iterator = methodOutput.local(Type.getType(Iterator.class)); 26 | methodOutput.invokeInterface(Iterable.class, "iterator", Iterator.class); 27 | methodOutput.storeObject(iterator); 28 | } 29 | 30 | @Override 31 | public void compilePreIterate(int[] locals, Label exit) { 32 | methodOutput.loadObject(iterator); 33 | methodOutput.invokeInterface(Iterator.class, "hasNext", boolean.class); 34 | methodOutput.ifEQ(exit); 35 | 36 | methodOutput.loadObject(iterator); 37 | methodOutput.invokeInterface(Iterator.class, "next", Object.class); 38 | methodOutput.checkCast(iteratorType.toASMType().getInternalName()); 39 | methodOutput.store(iteratorType.toASMType(), locals[0]); 40 | } 41 | 42 | @Override 43 | public void compilePostIterate(int[] locals, Label exit, Label repeat) { 44 | methodOutput.goTo(repeat); 45 | } 46 | 47 | @Override 48 | public ZenType getType(int i) { 49 | return iteratorType; 50 | } 51 | 52 | @Override 53 | public void compileEnd() { 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/iterator/IteratorWhileDo.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.iterator; 2 | 3 | import org.objectweb.asm.Label; 4 | import stanhebben.zenscript.compiler.IEnvironmentMethod; 5 | import stanhebben.zenscript.parser.expression.ParsedExpression; 6 | import stanhebben.zenscript.type.*; 7 | import stanhebben.zenscript.util.MethodOutput; 8 | 9 | public class IteratorWhileDo implements IZenIterator { 10 | 11 | private ParsedExpression condition; 12 | private MethodOutput output; 13 | private IEnvironmentMethod environment; 14 | 15 | public IteratorWhileDo(ParsedExpression condition, IEnvironmentMethod environment) { 16 | this.condition = condition; 17 | this.output = environment.getOutput(); 18 | this.environment = environment; 19 | } 20 | 21 | @Override 22 | public void compileStart(int[] locals) { 23 | //Nothing required since we don't need to call an entrySet or something 24 | } 25 | 26 | @Override 27 | public void compilePreIterate(int[] locals, Label exit) { 28 | condition.compile(environment, ZenType.BOOL).eval(environment).cast(condition.getPosition(), environment, ZenType.BOOL).compile(true, environment); 29 | output.ifEQ(exit); 30 | } 31 | 32 | @Override 33 | public void compilePostIterate(int[] locals, Label exit, Label repeat) { 34 | output.goTo(repeat); 35 | } 36 | 37 | @Override 38 | public void compileEnd() { 39 | //Nothing required since we don't need to close anything. 40 | } 41 | 42 | @Override 43 | public ZenType getType(int i) { 44 | return ZenType.VOID; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/natives/IJavaMethod.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.natives; 2 | 3 | import stanhebben.zenscript.compiler.IEnvironmentGlobal; 4 | import stanhebben.zenscript.expression.Expression; 5 | import stanhebben.zenscript.type.ZenType; 6 | import stanhebben.zenscript.util.MethodOutput; 7 | 8 | /** 9 | * @author Stan 10 | */ 11 | public interface IJavaMethod { 12 | 13 | boolean isStatic(); 14 | 15 | boolean accepts(int numArguments); 16 | 17 | boolean accepts(IEnvironmentGlobal environment, Expression... arguments); 18 | 19 | int getPriority(IEnvironmentGlobal environment, Expression... arguments); 20 | 21 | void invokeVirtual(MethodOutput output); 22 | 23 | void invokeStatic(MethodOutput output); 24 | 25 | ZenType[] getParameterTypes(); 26 | 27 | ZenType getReturnType(); 28 | 29 | boolean isVarargs(); 30 | 31 | String getErrorDescription(); 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/type/natives/ZenNativeOperator.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.type.natives; 2 | 3 | import com.google.gson.*; 4 | import stanhebben.zenscript.annotations.OperatorType; 5 | import stanhebben.zenscript.dump.IDumpable; 6 | import stanhebben.zenscript.dump.types.DumpIJavaMethod; 7 | 8 | /** 9 | * @author Stanneke 10 | */ 11 | public class ZenNativeOperator implements IDumpable { 12 | 13 | private final OperatorType operator; 14 | private final IJavaMethod method; 15 | 16 | public ZenNativeOperator(OperatorType operator, IJavaMethod method) { 17 | this.operator = operator; 18 | this.method = method; 19 | } 20 | 21 | public OperatorType getOperator() { 22 | return operator; 23 | } 24 | 25 | public IJavaMethod getMethod() { 26 | return method; 27 | } 28 | 29 | @Override 30 | public JsonElement serialize(JsonSerializationContext context) { 31 | JsonObject obj = new DumpIJavaMethod(method).serialize(context); 32 | obj.addProperty("operator", operator.toString()); 33 | 34 | return obj; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/util/MappingIterator.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.util; 2 | 3 | import java.util.Iterator; 4 | 5 | /** 6 | * @author Stan Hebben 7 | */ 8 | public abstract class MappingIterator implements Iterator { 9 | 10 | private final Iterator iterator; 11 | 12 | public MappingIterator(Iterator iterator) { 13 | this.iterator = iterator; 14 | } 15 | 16 | public abstract U map(T value); 17 | 18 | @Override 19 | public boolean hasNext() { 20 | return iterator.hasNext(); 21 | } 22 | 23 | @Override 24 | public U next() { 25 | return map(iterator.next()); 26 | } 27 | 28 | @Override 29 | public void remove() { 30 | iterator.remove(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/util/MethodCompiler.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.util; 2 | 3 | /** 4 | * @author Stanneke 5 | */ 6 | public interface MethodCompiler { 7 | 8 | void compile(MethodOutput output); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/util/Pair.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.util; 2 | 3 | import java.util.Objects; 4 | 5 | public class Pair { 6 | 7 | private final K key; 8 | private final V value; 9 | 10 | public Pair(K key, V value) { 11 | this.key = key; 12 | this.value = value; 13 | } 14 | 15 | public K getKey() { 16 | return key; 17 | } 18 | 19 | public V getValue() { 20 | return value; 21 | } 22 | 23 | @Override 24 | public boolean equals(Object o) { 25 | if(this == o) 26 | return true; 27 | if(o == null || getClass() != o.getClass()) 28 | return false; 29 | Pair pair = (Pair) o; 30 | return Objects.equals(key, pair.key) && Objects.equals(value, pair.value); 31 | } 32 | 33 | @Override 34 | public int hashCode() { 35 | return Objects.hash(key, value); 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/util/ZenPosition.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.util; 2 | 3 | import stanhebben.zenscript.ZenParsedFile; 4 | 5 | /** 6 | * @author Stanneke 7 | */ 8 | public class ZenPosition { 9 | 10 | private final ZenParsedFile file; 11 | private final int line; 12 | private final int offset; 13 | private final String fileNameFallback; 14 | public ZenPosition(ZenParsedFile file, int line, int offset, String fileNameFallback) { 15 | if(file != null && line <= 0) 16 | throw new IllegalArgumentException("Line must be positive"); 17 | 18 | this.file = file; 19 | this.line = line; 20 | this.offset = offset; 21 | this.fileNameFallback = fileNameFallback; 22 | } 23 | 24 | public ZenParsedFile getFile() { 25 | return file; 26 | } 27 | 28 | public String getFileName() { 29 | return (file == null ? (fileNameFallback == null ? "?" : fileNameFallback) : file.getFileName()); 30 | } 31 | 32 | public int getLine() { 33 | return line; 34 | } 35 | 36 | public int getLineOffset() { 37 | return offset; 38 | } 39 | 40 | @Override 41 | public String toString() { 42 | return getFileName() + ":" + line; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/value/IAny.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.value; 2 | 3 | import java.util.Iterator; 4 | 5 | /** 6 | * @author Stan Hebben 7 | */ 8 | public interface IAny { 9 | 10 | String NAME = "stanhebben/zenscript/value/IAny"; 11 | 12 | int NUM_BYTE = 1; 13 | int NUM_SHORT = 2; 14 | int NUM_INT = 3; 15 | int NUM_LONG = 4; 16 | int NUM_FLOAT = 5; 17 | int NUM_DOUBLE = 6; 18 | 19 | IAny not(); 20 | 21 | IAny neg(); 22 | 23 | IAny add(IAny value); 24 | 25 | IAny sub(IAny value); 26 | 27 | IAny cat(IAny value); 28 | 29 | IAny mul(IAny value); 30 | 31 | IAny div(IAny value); 32 | 33 | IAny mod(IAny value); 34 | 35 | IAny and(IAny value); 36 | 37 | IAny or(IAny value); 38 | 39 | IAny xor(IAny value); 40 | 41 | IAny range(IAny value); 42 | 43 | int compareTo(IAny value); 44 | 45 | boolean contains(IAny value); 46 | 47 | IAny memberGet(String member); 48 | 49 | void memberSet(String member, IAny value); 50 | 51 | IAny memberCall(String member, IAny... values); 52 | 53 | IAny indexGet(IAny key); 54 | 55 | void indexSet(IAny key, IAny value); 56 | 57 | IAny call(IAny... values); 58 | 59 | boolean asBool(); 60 | 61 | byte asByte(); 62 | 63 | short asShort(); 64 | 65 | int asInt(); 66 | 67 | long asLong(); 68 | 69 | float asFloat(); 70 | 71 | double asDouble(); 72 | 73 | String asString(); 74 | 75 | T as(Class cls); 76 | 77 | boolean is(Class cls); 78 | 79 | boolean canCastImplicit(Class cls); 80 | 81 | int getNumberType(); 82 | 83 | Iterator iteratorSingle(); 84 | 85 | Iterator iteratorMulti(int n); 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/value/IntRange.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.value; 2 | 3 | /** 4 | * @author Stan Hebben 5 | */ 6 | public class IntRange { 7 | 8 | private final int from; 9 | private final int to; 10 | 11 | public IntRange(int from, int to) { 12 | this.from = from; 13 | this.to = to; 14 | } 15 | 16 | public int getFrom() { 17 | return from; 18 | } 19 | 20 | public int getTo() { 21 | return to; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/stanhebben/zenscript/value/builtin/expansion/IByteExpansion.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.value.builtin.expansion; 2 | 3 | import stanhebben.zenscript.value.IAny; 4 | 5 | /** 6 | * @author Stan 7 | */ 8 | public interface IByteExpansion { 9 | 10 | IAny member(byte value, String member); 11 | } 12 | -------------------------------------------------------------------------------- /src/test/java/stanhebben/zenscript/TestAssertions.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | 5 | import java.util.function.IntFunction; 6 | 7 | public class TestAssertions { 8 | 9 | public static void assertMany(String... lines) { 10 | if(lines == null || lines.length == 0) 11 | throw new IllegalArgumentException("Calling assertMany with a null or empty vararg is useless!"); 12 | for(int i = 0; i < lines.length; i++) { 13 | Assertions.assertEquals(lines[i], TestHelper.prints.get(i)); 14 | } 15 | } 16 | 17 | public static void assertOne(String content, int logLine) { 18 | Assertions.assertEquals(content, TestHelper.prints.get(logLine)); 19 | } 20 | 21 | public static void assertCalculated(IntFunction function) { 22 | assertCalculated(function, TestHelper.prints.size()); 23 | } 24 | 25 | public static void assertCalculated(IntFunction function, int count) { 26 | assertCalculated(function, 0, count); 27 | } 28 | 29 | public static void assertCalculated(IntFunction function, int startIndexLog, int count) { 30 | assertCalculated(function, startIndexLog, count, 0); 31 | } 32 | 33 | public static void assertCalculated(IntFunction function, int startIndexLog, int count, int startIndexFunction) { 34 | for(int i = 0; i < count; i++) { 35 | Assertions.assertEquals(function.apply(i + startIndexFunction), TestHelper.prints.get(i + startIndexLog)); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/stanhebben/zenscript/TestErrorLogger.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript; 2 | 3 | import stanhebben.zenscript.util.ZenPosition; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | public class TestErrorLogger implements IZenErrorLogger { 9 | 10 | final List listInfo; 11 | final List listWarning; 12 | final List listError; 13 | 14 | TestErrorLogger() { 15 | listInfo = new ArrayList<>(); 16 | listError = new ArrayList<>(); 17 | listWarning = new ArrayList<>(); 18 | } 19 | 20 | @Override 21 | public void error(ZenPosition position, String message) { 22 | listError.add(position + ": " + message); 23 | } 24 | 25 | @Override 26 | public void warning(ZenPosition position, String message) { 27 | listWarning.add(position + ": " + message); 28 | } 29 | 30 | @Override 31 | public void info(ZenPosition position, String message) { 32 | listInfo.add(position + ": " + message); 33 | } 34 | 35 | @Override 36 | public void error(String message) { 37 | listError.add(message); 38 | } 39 | 40 | @Override 41 | public void error(String message, Throwable e) { 42 | listError.add(message); 43 | } 44 | 45 | @Override 46 | public void warning(String message) { 47 | listWarning.add(message); 48 | } 49 | 50 | @Override 51 | public void info(String message) { 52 | listInfo.add(message); 53 | } 54 | 55 | void clear() { 56 | this.listInfo.clear(); 57 | this.listWarning.clear(); 58 | this.listError.clear(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/test/java/stanhebben/zenscript/tests/TestArithmeticOperators.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.tests; 2 | 3 | import org.junit.jupiter.api.BeforeAll; 4 | import org.junit.jupiter.api.BeforeEach; 5 | import org.junit.jupiter.api.Test; 6 | import stanhebben.zenscript.TestHelper; 7 | 8 | import static stanhebben.zenscript.TestAssertions.assertMany; 9 | 10 | @SuppressWarnings("WeakerAccess") 11 | public class TestArithmeticOperators { 12 | 13 | 14 | @BeforeAll 15 | public static void setupEnvironment() { 16 | TestHelper.setupEnvironment(); 17 | } 18 | 19 | @BeforeEach 20 | public void beforeEch() { 21 | TestHelper.beforeEach(); 22 | } 23 | 24 | @Test 25 | public void testBrackets() { 26 | TestHelper.run("print(((1+2)*3) - 5); print((1*(2+3)) - 5);"); 27 | 28 | assertMany("4", "0"); 29 | } 30 | 31 | @Test 32 | public void testD2LCast_Function() { 33 | TestHelper.run("val x = 10 as long; function pLong(lo as long) as void {print(lo);} pLong((1.8 * x) as long);"); 34 | 35 | assertMany("18"); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/test/java/stanhebben/zenscript/tests/TestDefaultArguments.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.tests; 2 | 3 | import org.junit.jupiter.api.BeforeAll; 4 | import org.junit.jupiter.api.BeforeEach; 5 | import org.junit.jupiter.api.Test; 6 | import stanhebben.zenscript.TestAssertions; 7 | import stanhebben.zenscript.TestHelper; 8 | 9 | import java.util.StringJoiner; 10 | 11 | public class TestDefaultArguments { 12 | @BeforeAll 13 | public static void setupEnvironment() { 14 | TestHelper.setupEnvironment(); 15 | } 16 | 17 | @BeforeEach 18 | public void beforeEach() { 19 | TestHelper.beforeEach(); 20 | } 21 | 22 | @Test 23 | public void testFunctionDefaultArguments() { 24 | TestHelper.run("function test(a as int, b as int = 2, c as int = 3) {print(a + b + c);} test(1); test(2, 4); test(10, 10, 7);"); 25 | TestAssertions.assertMany("6", "9", "27"); 26 | } 27 | 28 | @Test 29 | public void testZenClassDefaultArguments() { 30 | StringJoiner joiner = new StringJoiner("\n") 31 | .add("zenClass Test {") 32 | .add(" zenConstructor() {}") 33 | .add(" function foo(a as int, b as int = 2, c as int = 3) as void {print(a + b + c);}") 34 | .add("}") 35 | .add("Test().foo(1);") 36 | .add("Test().foo(2, 3);") 37 | .add("Test().foo(4, 5, 6);"); 38 | TestHelper.run(joiner.toString()); 39 | TestAssertions.assertMany("6", "8", "15"); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/stanhebben/zenscript/tests/TestExpansion.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.tests; 2 | 3 | import org.junit.jupiter.api.BeforeAll; 4 | import org.junit.jupiter.api.BeforeEach; 5 | import org.junit.jupiter.api.Test; 6 | import stanhebben.zenscript.TestAssertions; 7 | import stanhebben.zenscript.TestHelper; 8 | 9 | import java.util.StringJoiner; 10 | 11 | public class TestExpansion { 12 | @BeforeAll 13 | public static void setupEnvironment() { 14 | TestHelper.setupEnvironment(); 15 | } 16 | 17 | @BeforeEach 18 | public void beforeEach() { 19 | TestHelper.beforeEach(); 20 | } 21 | 22 | @Test 23 | public void testExpansion() { 24 | StringJoiner joiner = new StringJoiner("\n") 25 | .add("$expand string$wrap(prefix as string, suffix as string) as string {") 26 | .add(" return prefix ~ this ~ suffix;") 27 | .add("}") 28 | .add("print(\"test\".wrap(\"[\", \"]\"));"); 29 | TestHelper.run(joiner.toString()); 30 | TestAssertions.assertMany("[test]"); 31 | } 32 | 33 | @Test 34 | public void testExpansionDefaultArguments() { 35 | StringJoiner joiner = new StringJoiner("\n") 36 | .add("$expand string$wrap(prefix as string = \"(\", suffix as string = \")\") as string {") 37 | .add(" return prefix ~ this ~ suffix;") 38 | .add("}") 39 | .add("print(\"test\".wrap(\"[\", \"]\"));") 40 | .add("print(\"test\".wrap());"); 41 | TestHelper.run(joiner.toString()); 42 | TestAssertions.assertMany("[test]", "(test)"); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/stanhebben/zenscript/tests/TestFunctions.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.tests; 2 | 3 | import org.junit.jupiter.api.BeforeAll; 4 | import org.junit.jupiter.api.BeforeEach; 5 | import org.junit.jupiter.api.Test; 6 | import stanhebben.zenscript.TestHelper; 7 | 8 | import static stanhebben.zenscript.TestAssertions.assertCalculated; 9 | import static stanhebben.zenscript.TestAssertions.assertMany; 10 | 11 | @SuppressWarnings("WeakerAccess") 12 | public class TestFunctions { 13 | 14 | @BeforeAll 15 | public static void setupEnvironment() { 16 | TestHelper.setupEnvironment(); 17 | } 18 | 19 | @BeforeEach 20 | public void beforeEach() { 21 | TestHelper.beforeEach(); 22 | } 23 | 24 | @Test 25 | public void Test_NestedFunctions() { 26 | TestHelper.run("tens(); realTens(\"Hello World!\"); function tens(){ realTens(\"a\"); } function realTens(a as string){ for i in 1 to 11{ print(a); } }"); 27 | 28 | assertCalculated(value -> value < 10 ? "a" : "Hello World!"); 29 | } 30 | 31 | @Test 32 | public void Test_FunctionDeclaredAfterCall() { 33 | TestHelper.run("val result = add(1,99); print(result); print(add(2,64)); function add(a as int,b as int) as int{ return a+b; }"); 34 | 35 | assertMany("100", "66"); 36 | } 37 | 38 | @Test 39 | public void Test_FunctionLargeType() { 40 | TestHelper.run("function test(a as double, b as bool) as double {return b ? a : 10.0D;} print(test(3.0D, true)); print(test(3.0D, false));"); 41 | 42 | assertMany("3.0", "10.0"); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/stanhebben/zenscript/tests/TestGlobals.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.tests; 2 | 3 | import org.junit.jupiter.api.BeforeAll; 4 | import org.junit.jupiter.api.BeforeEach; 5 | import org.junit.jupiter.api.Test; 6 | import stanhebben.zenscript.TestHelper; 7 | 8 | import static stanhebben.zenscript.TestAssertions.assertMany; 9 | 10 | public class TestGlobals { 11 | 12 | @BeforeAll 13 | public static void setupEnvironment() { 14 | TestHelper.setupEnvironment(); 15 | } 16 | 17 | @BeforeEach 18 | public void beforeEch() { 19 | TestHelper.beforeEach(); 20 | } 21 | 22 | @Test 23 | public void testAssign() { 24 | TestHelper.run("static a as string = 'A'; print(a); a = 'B'; print(a);"); 25 | assertMany("A", "B"); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/test/java/stanhebben/zenscript/tests/TestLoops.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.tests; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.junit.jupiter.api.BeforeAll; 5 | import org.junit.jupiter.api.BeforeEach; 6 | import stanhebben.zenscript.TestHelper; 7 | 8 | import static stanhebben.zenscript.TestAssertions.assertMany; 9 | 10 | @SuppressWarnings("WeakerAccess") 11 | public class TestLoops { 12 | @BeforeAll 13 | public static void setupEnvironment() { 14 | TestHelper.setupEnvironment(); 15 | } 16 | 17 | @BeforeEach 18 | public void beforeEch() { 19 | TestHelper.beforeEach(); 20 | } 21 | 22 | @Test 23 | public void testBreak() { 24 | TestHelper.run("val arr = ['a', 'b', 'c'] as string[]; for x in arr {print(x); if (x == 'b') break;}"); 25 | assertMany("a", "b"); 26 | } 27 | 28 | @Test 29 | public void testContinue() { 30 | TestHelper.run("val arr = ['a', 'b', 'c'] as string[]; for x in arr {if (x == 'b') continue; print(x);}"); 31 | assertMany("a", "c"); 32 | } 33 | 34 | @Test 35 | public void testBreakInner(){ 36 | TestHelper.run("val arr = ['a', 'b', 'c'] as string[]; for x in arr {print(x); if(x == 'b') break; for y in arr { if(y=='c') break; print(x~y);}}"); 37 | assertMany("a", "aa", "ab", "b"); 38 | } 39 | 40 | @Test 41 | public void testContinueInner() { 42 | TestHelper.run("val arr = ['a', 'b', 'c'] as string[]; for x in arr {print(x); if(x == 'b') continue; for y in arr { if(y=='c') continue; print(x~y);}}"); 43 | assertMany("a", "aa", "ab", "b", "c", "ca", "cb"); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/test/java/stanhebben/zenscript/tests/TestMaps.java: -------------------------------------------------------------------------------- 1 | package stanhebben.zenscript.tests; 2 | 3 | import org.junit.jupiter.api.*; 4 | import stanhebben.zenscript.*; 5 | 6 | import java.util.*; 7 | 8 | import static stanhebben.zenscript.TestAssertions.assertMany; 9 | 10 | public class TestMaps { 11 | 12 | @BeforeAll 13 | public static void setupEnvironment() { 14 | TestHelper.setupEnvironment(); 15 | TestHelper.registry.registerNativeClass(TestLambdas.FunInterface.class); 16 | TestHelper.registry.registerNativeClass(TestLambdas.FunInterfaceWithDefaultMethod.class); 17 | TestHelper.registry.registerNativeClass(TestLambdas.FunInterfaceWithStaticMethod.class); 18 | TestHelper.registry.registerNativeClass(TestLambdas.FunInterfaceWithZenMethod.class); 19 | TestHelper.registry.registerGlobal("applyFun", TestHelper.registry.getStaticFunction(TestLambdas.class, "applyFun", TestLambdas.FunInterface.class)); 20 | } 21 | 22 | @BeforeEach 23 | public void beforeEach() { 24 | TestHelper.beforeEach(); 25 | } 26 | 27 | @Test 28 | public void Test_NestedMap_Unboxing() { 29 | final StringJoiner joiner = new StringJoiner("\n"); 30 | joiner.add("var outerMap = {'outer': {inner: 10.0D}} as double[string][string];"); 31 | joiner.add("for outerName, innerMap in outerMap {"); 32 | joiner.add(" for innerName, dValue in innerMap {"); 33 | joiner.add(" var x = dValue;"); 34 | joiner.add(" print('[' ~ outerName ~ '][' ~ innerName ~ '][' ~ dValue ~']');"); 35 | joiner.add(" }"); 36 | joiner.add("}"); 37 | TestHelper.run(joiner.toString()); 38 | assertMany("[outer][inner][10.0]"); 39 | } 40 | } 41 | --------------------------------------------------------------------------------