├── .gitignore ├── manifest.txt ├── doc └── BASICCompilerLanguage.pdf ├── samples ├── info.txt ├── 3DPLOT.BAS ├── LUNAR.BAS └── INKBLOT.BAS ├── .classpath ├── .project ├── makejar.bat └── src └── de └── lorenzwiest └── basiccompiler ├── parser ├── nodes │ ├── INode.java │ ├── NodeType.java │ └── impl │ │ ├── StrNode.java │ │ ├── LocalVariableNode.java │ │ ├── TokenNode.java │ │ ├── NumNode.java │ │ ├── UnaryNode.java │ │ ├── FnFunctionNode.java │ │ ├── FunctionNode.java │ │ ├── BinaryNode.java │ │ └── VariableNode.java └── statements │ ├── Statement.java │ └── impl │ ├── RemStatement.java │ ├── EndStatement.java │ ├── GotoStatement.java │ ├── StopStatement.java │ ├── WendStatement.java │ ├── GosubStatement.java │ ├── RestoreStatement.java │ ├── ReturnStatement.java │ ├── WhileStatement.java │ ├── PrintStatement.java │ ├── DimStatement.java │ ├── ReadStatement.java │ ├── NextStatement.java │ ├── LineNumberStatement.java │ ├── DataStatement.java │ ├── OnGosubStatement.java │ ├── OnGotoStatement.java │ ├── SwapStatement.java │ ├── LetStatement.java │ ├── DefFnStatement.java │ ├── IfStatement.java │ ├── InputStatement.java │ └── ForStatement.java ├── classfile ├── info │ ├── InterfaceInfo.java │ ├── AttributeInfo.java │ ├── ExceptionTableInfo.java │ ├── MethodInfo.java │ └── FieldInfo.java ├── constantpoolinfo │ ├── ConstantPoolInfo.java │ └── impl │ │ ├── ConstantPoolInfo_Long.java │ │ ├── ConstantPoolInfo_Integer.java │ │ ├── ConstantPoolInfo_Float.java │ │ ├── ConstantPoolInfo_Double.java │ │ ├── ConstantPoolInfo_String.java │ │ └── ConstantPoolInfo_Class.java └── ConstantPool.java ├── bytecode └── BytecodeException.java └── compiler ├── etc ├── CompileException.java └── LocalVariableTable.java └── library └── methods ├── functions ├── Method_Len.java ├── Method_Abs.java ├── Method_Str.java ├── Method_Pos.java ├── Method_Atn.java ├── Method_Cos.java ├── Method_Sin.java ├── Method_Int.java ├── Method_Asc.java ├── Method_Sgn.java ├── Method_Log.java ├── Method_Val.java ├── Method_Sqr.java ├── Method_Exp.java ├── Method_Chr.java ├── Method_Tan.java └── Method_Fix.java ├── helper ├── Method_StringToChars.java ├── Method_ReadNumFromDataToStack.java ├── Method_GosubStackInitialize.java ├── Method_PrintStringFromStack.java ├── Method_PrintFloatFromStack.java ├── Method_RoundToInt.java ├── Method_CheckOnGotoGosubArg.java ├── Method_ThrowRuntimeException.java ├── Method_PrintCharsFromStack.java ├── Method_Substring.java └── Method_GosubStackPop.java ├── operators ├── Method_Or.java ├── Method_And.java ├── Method_Xor.java ├── Method_DivisionByZero.java ├── Method_Division.java ├── Method_Not.java ├── Method_StringEqual.java ├── Method_StringNotEqual.java └── Method_Power.java └── arrays ├── Method_Dim1DCheckSize.java ├── Method_LoadFloatFrom1DArray.java ├── Method_LoadStringFrom1DArray.java ├── Method_StoreFloatIn1DArray.java └── Method_StoreStringIn1DArray.java /.gitignore: -------------------------------------------------------------------------------- 1 | bin/ 2 | internal/ 3 | -------------------------------------------------------------------------------- /manifest.txt: -------------------------------------------------------------------------------- 1 | Main-Class: de.lorenzwiest.basiccompiler.BASICCompiler 2 | -------------------------------------------------------------------------------- /doc/BASICCompilerLanguage.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lwiest/BASICCompiler/HEAD/doc/BASICCompilerLanguage.pdf -------------------------------------------------------------------------------- /samples/info.txt: -------------------------------------------------------------------------------- 1 | Sample programs are from the books 2 | o "BASIC Computer Games" by David H. Ahl 3 | o "More BASIC Computer Games" by David H. Ahl 4 | Used with permission by the author. 5 | -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | BASICCompiler 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /samples/3DPLOT.BAS: -------------------------------------------------------------------------------- 1 | 10 PRINT TAB(30);"3D-PLOT" 2 | 20 PRINT TAB(15);"CREATIVE COMPUTING MORRISTOWN, NEW JERSEY" 3 | 30 PRINT : PRINT : PRINT 4 | 40 DEF FNA(Z)=30*EXP(-Z*Z/100) 5 | 100 PRINT 6 | 110 FOR X=-30 TO 30 STEP 1.5 7 | 120 L=0 8 | 130 Y1=5*INT(SQR(900-X*X)/5) 9 | 140 FOR Y=Y1 TO -Y1 STEP -5 10 | 150 Z=INT(25+FNA(SQR(X*X+Y*Y))-.7*Y) 11 | 160 IF Z<=L THEN 190 12 | 170 L=Z 13 | 180 PRINT TAB(Z);"*"; 14 | 190 NEXT Y 15 | 200 PRINT 16 | 210 NEXT X 17 | 300 END 18 | -------------------------------------------------------------------------------- /makejar.bat: -------------------------------------------------------------------------------- 1 | @IF NOT "%JAVA_HOME%" == "" GOTO OK 2 | @ECHO Please set JAVA_HOME environment variable, for example "SET JAVA_HOME=c:\java\jdk180" 3 | @GOTO EXIT 4 | @:OK 5 | SET HOME=. 6 | SET BIN=%HOME%\bin 7 | SET SRC=%HOME%\src 8 | SET SRCLIST=~srclist.txt 9 | SET JARNAME=BASICCompiler.jar 10 | SET MANIFESTNAME=manifest.txt 11 | PUSHD . 12 | IF EXIST %BIN% RMDIR /S %BIN% 13 | MKDIR %BIN% 14 | IF EXIST %SRCLIST% DEL %SRCLIST% 15 | DIR /S /B /A-D %SRC% | FINDSTR /V /C:tests > %SRCLIST% 16 | "%JAVA_HOME%\bin\javac" -d %BIN% @%SRCLIST% 17 | DEL %SRCLIST% 18 | CD %BIN% 19 | IF EXIST %JARNAME% DEL %JARNAME% 20 | "%JAVA_HOME%\bin\jar" cfm %JARNAME% ..\%MANIFESTNAME% * 21 | POPD 22 | MOVE %BIN%\%JARNAME% . 23 | @:EXIT 24 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/nodes/INode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.nodes; 26 | 27 | public interface INode { 28 | NodeType getType(); 29 | } 30 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/classfile/info/InterfaceInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.classfile.info; 26 | 27 | public class InterfaceInfo { 28 | // not implemented 29 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/classfile/info/AttributeInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.classfile.info; 26 | 27 | public class AttributeInfo { 28 | // not implemented 29 | } 30 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/statements/Statement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.statements; 26 | 27 | public interface Statement { 28 | // empty marker interface 29 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/nodes/NodeType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.nodes; 26 | 27 | public enum NodeType { 28 | STR, // 29 | NUM, // 30 | VOID, // 31 | UNKNOWN; 32 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/bytecode/BytecodeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2018 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.bytecode; 26 | 27 | @SuppressWarnings("serial") 28 | public class BytecodeException extends RuntimeException { 29 | 30 | public BytecodeException(String message) { 31 | super(message); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/statements/impl/RemStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.statements.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.statements.Statement; 28 | 29 | public class RemStatement implements Statement { 30 | private final String comment; 31 | 32 | public RemStatement(String comment) { 33 | this.comment = comment; 34 | } 35 | 36 | public String getComment() { 37 | return this.comment; 38 | } 39 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/statements/impl/EndStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.statements.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.statements.Statement; 28 | 29 | public class EndStatement implements Statement { 30 | private static final EndStatement INSTANCE = new EndStatement(); 31 | 32 | private EndStatement() { 33 | // empty 34 | } 35 | 36 | public static EndStatement getInstance() { 37 | return INSTANCE; 38 | } 39 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/statements/impl/GotoStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.statements.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.statements.Statement; 28 | 29 | public class GotoStatement implements Statement { 30 | private final String lineNumber; 31 | 32 | public GotoStatement(String lineNumber) { 33 | this.lineNumber = lineNumber; 34 | } 35 | 36 | public String getLineNumber() { 37 | return this.lineNumber; 38 | } 39 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/statements/impl/StopStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.statements.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.statements.Statement; 28 | 29 | public class StopStatement implements Statement { 30 | private static final StopStatement INSTANCE = new StopStatement(); 31 | 32 | private StopStatement() { 33 | // empty 34 | } 35 | 36 | public static StopStatement getInstance() { 37 | return INSTANCE; 38 | } 39 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/statements/impl/WendStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.statements.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.statements.Statement; 28 | 29 | public class WendStatement implements Statement { 30 | private static final WendStatement INSTANCE = new WendStatement(); 31 | 32 | private WendStatement() { 33 | // empty 34 | } 35 | 36 | public static WendStatement getInstance() { 37 | return INSTANCE; 38 | } 39 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/statements/impl/GosubStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.statements.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.statements.Statement; 28 | 29 | public class GosubStatement implements Statement { 30 | private final String lineNumber; 31 | 32 | public GosubStatement(String lineNumber) { 33 | this.lineNumber = lineNumber; 34 | } 35 | 36 | public String getLineNumber() { 37 | return this.lineNumber; 38 | } 39 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/statements/impl/RestoreStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.statements.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.statements.Statement; 28 | 29 | public class RestoreStatement implements Statement { 30 | private final String lineNumber; 31 | 32 | public RestoreStatement(String lineNumber) { 33 | this.lineNumber = lineNumber; 34 | } 35 | 36 | public String getLineNumber() { 37 | return this.lineNumber; 38 | } 39 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/statements/impl/ReturnStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.statements.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.statements.Statement; 28 | 29 | public class ReturnStatement implements Statement { 30 | private static final ReturnStatement INSTANCE = new ReturnStatement(); 31 | 32 | private ReturnStatement() { 33 | // empty 34 | } 35 | 36 | public static ReturnStatement getInstance() { 37 | return INSTANCE; 38 | } 39 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/statements/impl/WhileStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.statements.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.nodes.INode; 28 | import de.lorenzwiest.basiccompiler.parser.statements.Statement; 29 | 30 | public class WhileStatement implements Statement { 31 | private final INode expression; 32 | 33 | public WhileStatement(INode expression) { 34 | this.expression = expression; 35 | } 36 | 37 | public INode getExpression() { 38 | return this.expression; 39 | } 40 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/statements/impl/PrintStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.statements.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.nodes.INode; 28 | import de.lorenzwiest.basiccompiler.parser.statements.Statement; 29 | 30 | public class PrintStatement implements Statement { 31 | private final INode[] expressions; 32 | 33 | public PrintStatement(INode[] expressions) { 34 | this.expressions = expressions; 35 | } 36 | 37 | public INode[] getExpressions() { 38 | return this.expressions; 39 | } 40 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/statements/impl/DimStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.statements.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.nodes.impl.VariableNode; 28 | import de.lorenzwiest.basiccompiler.parser.statements.Statement; 29 | 30 | public class DimStatement implements Statement { 31 | private final VariableNode[] variables; 32 | 33 | public DimStatement(VariableNode[] variables) { 34 | this.variables = variables; 35 | } 36 | 37 | public VariableNode[] getVariables() { 38 | return this.variables; 39 | } 40 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/statements/impl/ReadStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.statements.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.nodes.impl.VariableNode; 28 | import de.lorenzwiest.basiccompiler.parser.statements.Statement; 29 | 30 | public class ReadStatement implements Statement { 31 | private final VariableNode[] variables; 32 | 33 | public ReadStatement(VariableNode[] variables) { 34 | this.variables = variables; 35 | } 36 | 37 | public VariableNode[] getVariables() { 38 | return this.variables; 39 | } 40 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/statements/impl/NextStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.statements.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.nodes.impl.VariableNode; 28 | import de.lorenzwiest.basiccompiler.parser.statements.Statement; 29 | 30 | public class NextStatement implements Statement { 31 | private final VariableNode[] loopVariables; 32 | 33 | public NextStatement(VariableNode[] loopVariables) { 34 | this.loopVariables = loopVariables; 35 | } 36 | 37 | public VariableNode[] getLoopVariables() { 38 | return this.loopVariables; 39 | } 40 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/statements/impl/LineNumberStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.statements.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.statements.Statement; 28 | 29 | public class LineNumberStatement implements Statement { 30 | private final String lineNumber; 31 | 32 | public LineNumberStatement(String lineNumber) { 33 | this.lineNumber = lineNumber; 34 | } 35 | 36 | public String getLineNumber() { 37 | return this.lineNumber; 38 | } 39 | 40 | @Override 41 | public String toString() { 42 | return "Line number " + this.lineNumber; 43 | } 44 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/statements/impl/DataStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.statements.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.statements.Statement; 28 | 29 | public class DataStatement implements Statement { 30 | private final String lineNumber; 31 | private final String[] constants; 32 | 33 | public DataStatement(String lineNumber, String[] constants) { 34 | this.lineNumber = lineNumber; 35 | this.constants = constants; 36 | } 37 | 38 | public String getLineNumber() { 39 | return this.lineNumber; 40 | } 41 | 42 | public String[] getConstants() { 43 | return this.constants; 44 | } 45 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/nodes/impl/StrNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.nodes.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.nodes.INode; 28 | import de.lorenzwiest.basiccompiler.parser.nodes.NodeType; 29 | 30 | public class StrNode implements INode { 31 | private final String value; 32 | 33 | private StrNode(String value) { 34 | this.value = value; 35 | } 36 | 37 | public static StrNode create(String value) { 38 | return new StrNode(value); 39 | } 40 | 41 | public String getValue() { 42 | return this.value; 43 | } 44 | 45 | @Override 46 | public NodeType getType() { 47 | return NodeType.STR; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/etc/CompileException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.etc; 26 | 27 | @SuppressWarnings("serial") 28 | public class CompileException extends RuntimeException { 29 | private final int lineNumber; 30 | 31 | public CompileException(int lineNumber, String message) { 32 | super(message); 33 | this.lineNumber = lineNumber; 34 | } 35 | 36 | public CompileException(String message) { 37 | this(-1, message); 38 | } 39 | 40 | public int getLineNumber() { 41 | return this.lineNumber; 42 | } 43 | 44 | public String getFullMessage() { 45 | return "Compile error at line " + this.lineNumber + ": " + super.getMessage(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/nodes/impl/LocalVariableNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.nodes.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.nodes.NodeType; 28 | 29 | public class LocalVariableNode extends VariableNode { 30 | private int localIndex; 31 | 32 | private LocalVariableNode(String variableName, NodeType type, int localIndex) { 33 | super(variableName, type); 34 | this.localIndex = localIndex; 35 | } 36 | 37 | public static LocalVariableNode create(String variableName, NodeType type, int localIndex) { 38 | return new LocalVariableNode(variableName, type, localIndex); 39 | } 40 | 41 | public int getLocalIndex() { 42 | return this.localIndex; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/statements/impl/OnGosubStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.statements.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.nodes.INode; 28 | import de.lorenzwiest.basiccompiler.parser.statements.Statement; 29 | 30 | public class OnGosubStatement implements Statement { 31 | private final INode expression; 32 | private final String[] lineNumbers; 33 | 34 | public OnGosubStatement(INode expression, String[] lineNumbers) { 35 | this.expression = expression; 36 | this.lineNumbers = lineNumbers; 37 | } 38 | 39 | public INode getExpression() { 40 | return this.expression; 41 | } 42 | 43 | public String[] getLineNumbers() { 44 | return this.lineNumbers; 45 | } 46 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/statements/impl/OnGotoStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.statements.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.nodes.INode; 28 | import de.lorenzwiest.basiccompiler.parser.statements.Statement; 29 | 30 | public class OnGotoStatement implements Statement { 31 | private final INode expression; 32 | private final String[] lineNumbers; 33 | 34 | public OnGotoStatement(INode expression, String[] lineNumbers) { 35 | this.expression = expression; 36 | this.lineNumbers = lineNumbers; 37 | } 38 | 39 | public INode getExpression() { 40 | return this.expression; 41 | } 42 | 43 | public String[] getLineNumbers() { 44 | return this.lineNumbers; 45 | } 46 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/nodes/impl/TokenNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.nodes.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.nodes.INode; 28 | import de.lorenzwiest.basiccompiler.parser.nodes.NodeType; 29 | import de.lorenzwiest.basiccompiler.parser.tokens.Token; 30 | 31 | public class TokenNode implements INode { 32 | private final Token token; 33 | 34 | private TokenNode(Token token) { 35 | this.token = token; 36 | } 37 | 38 | public static INode create(Token token) { 39 | return new TokenNode(token); 40 | } 41 | 42 | public Token getToken() { 43 | return this.token; 44 | } 45 | 46 | @Override 47 | public NodeType getType() { 48 | return NodeType.UNKNOWN; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/statements/impl/SwapStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.statements.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.nodes.impl.VariableNode; 28 | import de.lorenzwiest.basiccompiler.parser.statements.Statement; 29 | 30 | public class SwapStatement implements Statement { 31 | private final VariableNode variable1; 32 | private final VariableNode variable2; 33 | 34 | public SwapStatement(VariableNode variable1, VariableNode variable2) { 35 | this.variable1 = variable1; 36 | this.variable2 = variable2; 37 | } 38 | 39 | public VariableNode getVariable1() { 40 | return this.variable1; 41 | } 42 | 43 | public VariableNode getVariable2() { 44 | return this.variable2; 45 | } 46 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/nodes/impl/NumNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.nodes.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.nodes.INode; 28 | import de.lorenzwiest.basiccompiler.parser.nodes.NodeType; 29 | 30 | public class NumNode implements INode { 31 | private final String strValue; 32 | 33 | private NumNode(String strValue) { 34 | this.strValue = strValue; 35 | } 36 | 37 | public static NumNode create(String strValue) { 38 | return new NumNode(strValue); 39 | } 40 | 41 | public String getStrValue() { 42 | return this.strValue; 43 | } 44 | 45 | public float getValue() { 46 | return Float.valueOf(this.strValue); 47 | } 48 | 49 | @Override 50 | public NodeType getType() { 51 | return NodeType.NUM; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/statements/impl/LetStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.statements.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.nodes.INode; 28 | import de.lorenzwiest.basiccompiler.parser.statements.Statement; 29 | 30 | public class LetStatement implements Statement { 31 | private final INode variable; 32 | private final INode expression; 33 | private final boolean isImplicit; 34 | 35 | public LetStatement(INode variable, INode expression, boolean isImplicit) { 36 | this.variable = variable; 37 | this.expression = expression; 38 | this.isImplicit = isImplicit; 39 | } 40 | 41 | public INode getVariable() { 42 | return this.variable; 43 | } 44 | 45 | public INode getExpression() { 46 | return this.expression; 47 | } 48 | 49 | public boolean isImplicit() { 50 | return this.isImplicit; 51 | } 52 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/nodes/impl/UnaryNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.nodes.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.nodes.INode; 28 | import de.lorenzwiest.basiccompiler.parser.nodes.NodeType; 29 | import de.lorenzwiest.basiccompiler.parser.tokens.Token; 30 | 31 | public class UnaryNode implements INode { 32 | private final Token op; 33 | private final INode argNode; 34 | 35 | private UnaryNode(Token op, INode argNode) { 36 | this.op = op; 37 | this.argNode = argNode; 38 | } 39 | 40 | public static INode create(Token op, INode argNode) { 41 | if (argNode == null) { 42 | return null; 43 | } 44 | return new UnaryNode(op, argNode); 45 | } 46 | 47 | public Token getOp() { 48 | return this.op; 49 | } 50 | 51 | public INode getArgNode() { 52 | return this.argNode; 53 | } 54 | 55 | @Override 56 | public NodeType getType() { 57 | return NodeType.NUM; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/statements/impl/DefFnStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.statements.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.nodes.INode; 28 | import de.lorenzwiest.basiccompiler.parser.nodes.impl.VariableNode; 29 | import de.lorenzwiest.basiccompiler.parser.statements.Statement; 30 | 31 | public class DefFnStatement implements Statement { 32 | private final String funcName; 33 | private final VariableNode[] funcVars; 34 | private final INode funcExpr; 35 | 36 | public DefFnStatement(String funcName, VariableNode[] funcVars, INode funcExpr) { 37 | this.funcName = funcName; 38 | this.funcVars = funcVars; 39 | this.funcExpr = funcExpr; 40 | } 41 | 42 | public String getFuncName() { 43 | return this.funcName; 44 | } 45 | 46 | public VariableNode[] getFuncVars() { 47 | return this.funcVars; 48 | } 49 | 50 | public INode getFuncExpr() { 51 | return this.funcExpr; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/statements/impl/IfStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.statements.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.nodes.INode; 28 | import de.lorenzwiest.basiccompiler.parser.statements.Statement; 29 | 30 | public class IfStatement implements Statement { 31 | private final INode expression; 32 | private final Statement[] thenStatements; 33 | private final Statement[] elseStatements; 34 | 35 | public IfStatement(INode expression, Statement[] thenStatements, Statement[] elseStatements) { 36 | this.expression = expression; 37 | this.thenStatements = thenStatements; 38 | this.elseStatements = elseStatements; 39 | } 40 | 41 | public INode getExpression() { 42 | return this.expression; 43 | } 44 | 45 | public Statement[] getThenStatements() { 46 | return this.thenStatements; 47 | } 48 | 49 | public Statement[] getElseStatements() { 50 | return this.elseStatements; 51 | } 52 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/statements/impl/InputStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.statements.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.nodes.impl.VariableNode; 28 | import de.lorenzwiest.basiccompiler.parser.statements.Statement; 29 | import de.lorenzwiest.basiccompiler.parser.tokens.Token; 30 | 31 | public class InputStatement implements Statement { 32 | private final String prompt; 33 | private final Token separatorToken; 34 | private final VariableNode[] variables; 35 | 36 | public InputStatement(String prompt, Token separatorToken, VariableNode[] variables) { 37 | this.prompt = prompt; 38 | this.separatorToken = separatorToken; 39 | this.variables = variables; 40 | } 41 | 42 | public String getPrompt() { 43 | return this.prompt; 44 | } 45 | 46 | public Token getSeparator() { 47 | return this.separatorToken; 48 | } 49 | 50 | public VariableNode[] getVariables() { 51 | return this.variables; 52 | } 53 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/classfile/constantpoolinfo/ConstantPoolInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.classfile.constantpoolinfo; 26 | 27 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 28 | 29 | public class ConstantPoolInfo { 30 | protected static final byte TAG_UTF8 = 1; 31 | protected static final byte TAG_INTEGER = 3; 32 | protected static final byte TAG_FLOAT = 4; 33 | protected static final byte TAG_LONG = 5; 34 | protected static final byte TAG_DOUBLE = 6; 35 | protected static final byte TAG_CLASS = 7; 36 | protected static final byte TAG_STRING = 8; 37 | protected static final byte TAG_FIELDREF = 9; 38 | protected static final byte TAG_METHODREF = 10; 39 | protected static final byte TAG_NAME_AND_TYPE = 12; 40 | 41 | private byte tag; 42 | 43 | protected ConstantPoolInfo(byte tag) { 44 | this.tag = tag; 45 | } 46 | 47 | public void write(ByteOutStream o) { 48 | o.write_u1(this.tag); 49 | } 50 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/classfile/info/ExceptionTableInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.classfile.info; 26 | 27 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 28 | 29 | public class ExceptionTableInfo { 30 | // u2 start_pc; 31 | // u2 end_pc; 32 | // u2 handler_pc; 33 | // u2 catch_type; 34 | 35 | private final int start_pc; // u2 36 | private final int end_pc; // u2 37 | private final int handler_pc; // u2 38 | private final int catch_type; // u2 39 | 40 | public ExceptionTableInfo(int start_pc, int end_pc, int handler_pc, int catch_type) { 41 | this.start_pc = start_pc; 42 | this.end_pc = end_pc; 43 | this.handler_pc = handler_pc; 44 | this.catch_type = catch_type; 45 | } 46 | 47 | public void write(ByteOutStream o) { 48 | o.write_u2(this.start_pc); 49 | o.write_u2(this.end_pc); 50 | o.write_u2(this.handler_pc); 51 | o.write_u2(this.catch_type); 52 | } 53 | 54 | public int getHandler_pc() { 55 | return this.handler_pc; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /samples/LUNAR.BAS: -------------------------------------------------------------------------------- 1 | 10 PRINT TAB(33);"LUNAR" 2 | 20 PRINT TAB(15);"CREATIVE COMPUTING MORRISTOWN, NEW JERSEY" 3 | 25 PRINT:PRINT:PRINT 4 | 30 PRINT "THIS IS A COMPUTER SIMULATION OF AN APOLLO LUNAR" 5 | 40 PRINT "LANDING CAPSULE.": PRINT: PRINT 6 | 50 PRINT "THE ON-BOARD COMPUTER HAS FAILED (IT WAS MADE BY" 7 | 60 PRINT "XEROX) SO YOU HAVE TO LAND THE CAPSULE MANUALLY." 8 | 70 PRINT: PRINT "SET BURN RATE OF RETRO ROCKETS TO ANY VALUE BETWEEN" 9 | 80 PRINT "0 (FREE FALL) AND 200 (MAXIMUM BURN) POUNDS PER SECOND." 10 | 90 PRINT "SET NEW BURN RATE EVERY 10 SECONDS.": PRINT 11 | 100 PRINT "CAPSULE WEIGHT 32,500 LBS; FUEL WEIGHT 16,500 LBS." 12 | 110 PRINT: PRINT: PRINT: PRINT "GOOD LUCK" 13 | 120 L=0 14 | 130 PRINT: PRINT "SEC","MI + FT","MPH","LB FUEL","BURN RATE":PRINT 15 | 140 A=120:V=1:M=33000:N=16500:G=1E-03:Z=1.8 16 | 150 PRINT L,INT(A);INT(5280*(A-INT(A))),3600*V,M-N,:INPUT K:T=10 17 | 160 IF M-N<1E-03 THEN 240 18 | 170 IF T<1E-03 THEN 150 19 | 180 S=T: IF M>=N+S*K THEN 200 20 | 190 S=(M-N)/K 21 | 200 GOSUB 420: IF I<=O THEN 340 22 | 210 IF V<=0 THEN 230 23 | 220 IF J<0 THEN 370 24 | 230 GOSUB 330: GOTO 160 25 | 240 PRINT "FUEL OUT AT";L;"SECONDS":S=(-V+SQR(V*V+2*A*G))/G 26 | 250 V=V+G*S: L=L+S 27 | 260 W=3600*V: PRINT "ON MOON AT";L;"SECONDS - IMPACT VELOCITY";W;"MPH" 28 | 274 IF W<=1.2 THEN PRINT "PERFECT LANDING!": GOTO 440 29 | 280 IF W<=10 THEN PRINT "GOOD LANDING (COULD RE BETTER)":GOTO 440 30 | 282 IF W>60 THEN 300 31 | 284 PRINT "CRAFT DAMAGE... YOU'RE STRANDED HERE UNTIL A RESCUE" 32 | 286 PRINT "PARTY ARRIVES. HOPE YOU HAVE ENOUGH OXYGEN!" 33 | 288 GOTO 440 34 | 300 PRINT "SORRY THERE NERE NO SURVIVORS. YOU BLOW IT!" 35 | 310 PRINT "IN FACT, YOU BLASTED A NEW LUNAR CRATER";W*.227;"FEET DEEP!" 36 | 320 GOTO 440 37 | 330 L=L+S: T=T-S: M=M-S*K: A=I: V=J: RETURN 38 | 340 IF S<5E-03 THEN 260 39 | 350 D=V+SQR(V*V+2*A*(G-Z*K/M)):S=2*A/D 40 | 360 GOSUB 420: GOSUB 330: GOTO 340 41 | 370 W=(1-M*G/(Z*K))/2: S=M*V/(Z*K*(W+SQR(W*W+V/Z)))+.05:GOSUB 420 42 | 380 IF I<=0 THEN 340 43 | 390 GOSUB 330: IF J>0 THEN 160 44 | 400 IF V>0 THEN 370 45 | 410 GOTO 160 46 | 420 Q=S*K/M: J=V+G*S+Z*(-Q-Q*Q/2-Q^3/3-Q^4/4-Q^5/5) 47 | 430 I=A-G*S*S/2-V*S+Z*S*(Q/2+Q^2/6+Q^3/12+Q^4/20+Q^5/30):RETURN 48 | 440 PRINT:PRINT:PRINT:PRINT "TRY AGAIN??": GOTO 70 49 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/nodes/impl/FnFunctionNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.nodes.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.nodes.INode; 28 | import de.lorenzwiest.basiccompiler.parser.nodes.NodeType; 29 | 30 | public class FnFunctionNode implements INode { 31 | private String funcName; 32 | private NodeType funcType; 33 | private INode[] funcArgExprs; 34 | 35 | private FnFunctionNode(String funcName, NodeType funcType, INode... funcArgExprs) { 36 | this.funcName = funcName; 37 | this.funcType = funcType; 38 | this.funcArgExprs = funcArgExprs; 39 | } 40 | 41 | public static FnFunctionNode create(String funcName, NodeType funcType, INode... funcArgExprs) { 42 | return new FnFunctionNode(funcName, funcType, funcArgExprs); 43 | } 44 | 45 | public String getFuncName() { 46 | return this.funcName; 47 | } 48 | 49 | public INode[] getFuncArgExprs() { 50 | return this.funcArgExprs; 51 | } 52 | 53 | @Override 54 | public NodeType getType() { 55 | return this.funcType; 56 | } 57 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/functions/Method_Len.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.functions; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 33 | 34 | public class Method_Len extends Method { 35 | private static final String METHOD_NAME = "Len"; 36 | private static final String DESCRIPTOR = "([C)F"; 37 | private static final int NUM_LOCALS = 1; 38 | 39 | public Method_Len(LibraryManager libraryManager) { 40 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 41 | } 42 | 43 | @Override 44 | public void addMethodBytecode(ByteOutStream o, List e) { 45 | 46 | // local 0: [C char array reference 47 | 48 | o.aload_0(); 49 | o.arraylength(); 50 | o.i2f(); 51 | o.freturn(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/nodes/impl/FunctionNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.nodes.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.nodes.INode; 28 | import de.lorenzwiest.basiccompiler.parser.nodes.NodeType; 29 | import de.lorenzwiest.basiccompiler.parser.tokens.FunctionToken; 30 | 31 | public class FunctionNode implements INode { 32 | private final FunctionToken functionToken; 33 | private final INode[] argNodes; 34 | 35 | private FunctionNode(FunctionToken functionToken, INode... argNodes) { 36 | this.functionToken = functionToken; 37 | this.argNodes = argNodes; 38 | } 39 | 40 | public static FunctionNode create(FunctionToken functionToken, INode... argNodes) { 41 | return new FunctionNode(functionToken, argNodes); 42 | } 43 | 44 | public FunctionToken getFunctionToken() { 45 | return this.functionToken; 46 | } 47 | 48 | public INode[] getArgNodes() { 49 | return this.argNodes; 50 | } 51 | 52 | @Override 53 | public NodeType getType() { 54 | return this.functionToken.getReturnType(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/functions/Method_Abs.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.functions; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 33 | 34 | public class Method_Abs extends Method { 35 | private static final String METHOD_NAME = "Abs"; 36 | private static final String DESCRIPTOR = "(F)F"; 37 | private static final int NUM_LOCALS = 1; 38 | 39 | public Method_Abs(LibraryManager libraryManager) { 40 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 41 | } 42 | 43 | @Override 44 | public void addMethodBytecode(ByteOutStream o, List e) { 45 | 46 | // local 0: F argument 47 | 48 | o.fload_0(); 49 | 50 | o.fload_0(); 51 | o.fconst_0(); 52 | o.fcmpg(); 53 | o.ifge("isPositiveArg"); 54 | 55 | o.fneg(); 56 | 57 | o.label("isPositiveArg"); 58 | o.freturn(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/functions/Method_Str.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.functions; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager.MethodEnum; 33 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 34 | 35 | public class Method_Str extends Method { 36 | private static final String METHOD_NAME = "Str"; 37 | private static final String DESCRIPTOR = "(F)[C"; 38 | private static final int NUM_LOCALS = 1; 39 | 40 | public Method_Str(LibraryManager libraryManager) { 41 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 42 | } 43 | 44 | @Override 45 | public void addMethodBytecode(ByteOutStream o, List e) { 46 | 47 | // local 0: F argument 48 | 49 | o.fload_0(); 50 | this.libraryManager.getMethod(MethodEnum.FLOAT_TO_CHARS).emitCall(o); 51 | o.areturn(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/functions/Method_Pos.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.functions; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.Compiler; 31 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 32 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 33 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 34 | 35 | public class Method_Pos extends Method { 36 | private static final String METHOD_NAME = "Pos"; 37 | private static final String DESCRIPTOR = "(F)F"; 38 | private static final int NUM_LOCALS = 1; 39 | 40 | public Method_Pos(LibraryManager libraryManager) { 41 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 42 | } 43 | 44 | @Override 45 | public void addMethodBytecode(ByteOutStream o, List e) { 46 | 47 | int posFieldRef = this.classModel.getFieldRefIndex(Compiler.FIELD_CURSOR_POS, "I"); 48 | 49 | o.getstatic(posFieldRef); 50 | o.iconst_1(); 51 | o.iadd(); 52 | o.i2f(); 53 | o.freturn(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/functions/Method_Atn.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.functions; 26 | 27 | import static de.lorenzwiest.basiccompiler.classfile.ClassModel.JavaMethod.MATH_ATAN; 28 | 29 | import java.util.List; 30 | 31 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 32 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 33 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 34 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 35 | 36 | public class Method_Atn extends Method { 37 | private static final String METHOD_NAME = "Atn"; 38 | private static final String DESCRIPTOR = "(F)F"; 39 | private static final int NUM_LOCALS = 1; 40 | 41 | public Method_Atn(LibraryManager libraryManager) { 42 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 43 | } 44 | 45 | @Override 46 | public void addMethodBytecode(ByteOutStream o, List e) { 47 | 48 | // local 0: F argument 49 | 50 | o.fload_0(); 51 | o.f2d(); 52 | o.invokestatic(this.classModel.getJavaMethodRefIndex(MATH_ATAN)); 53 | o.d2f(); 54 | o.freturn(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/functions/Method_Cos.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.functions; 26 | 27 | import static de.lorenzwiest.basiccompiler.classfile.ClassModel.JavaMethod.MATH_COS; 28 | 29 | import java.util.List; 30 | 31 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 32 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 33 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 34 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 35 | 36 | public class Method_Cos extends Method { 37 | private static final String METHOD_NAME = "Cos"; 38 | private static final String DESCRIPTOR = "(F)F"; 39 | private static final int NUM_LOCALS = 1; 40 | 41 | public Method_Cos(LibraryManager libraryManager) { 42 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 43 | } 44 | 45 | @Override 46 | public void addMethodBytecode(ByteOutStream o, List e) { 47 | 48 | // local 0: F argument 49 | 50 | o.fload_0(); 51 | o.f2d(); 52 | o.invokestatic(this.classModel.getJavaMethodRefIndex(MATH_COS)); 53 | o.d2f(); 54 | o.freturn(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/functions/Method_Sin.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.functions; 26 | 27 | import static de.lorenzwiest.basiccompiler.classfile.ClassModel.JavaMethod.MATH_SIN; 28 | 29 | import java.util.List; 30 | 31 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 32 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 33 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 34 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 35 | 36 | public class Method_Sin extends Method { 37 | private static final String METHOD_NAME = "Sin"; 38 | private static final String DESCRIPTOR = "(F)F"; 39 | private static final int NUM_LOCALS = 1; 40 | 41 | public Method_Sin(LibraryManager libraryManager) { 42 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 43 | } 44 | 45 | @Override 46 | public void addMethodBytecode(ByteOutStream o, List e) { 47 | 48 | // local 0: F argument 49 | 50 | o.fload_0(); 51 | o.f2d(); 52 | o.invokestatic(this.classModel.getJavaMethodRefIndex(MATH_SIN)); 53 | o.d2f(); 54 | o.freturn(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/classfile/info/MethodInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.classfile.info; 26 | 27 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 28 | 29 | public class MethodInfo { 30 | // u2 access_flags; 31 | // u2 name_index; 32 | // u2 descriptor_index; 33 | // u2 attributes_count; 34 | // attribute_info attributes[attributes_count]; 35 | 36 | private final int accessFlags; // u2 37 | private final int nameIndex; // u2 38 | private final int descriptorIndex; // u2 39 | private final int attributeCount; // u2 40 | private CodeAttributeInfo attributes; // attribute_info[] 41 | 42 | public MethodInfo(int nameIndex, int descriptorIndex, int accessFlags, CodeAttributeInfo codeAttributeInfo) { 43 | this.accessFlags = accessFlags; 44 | this.nameIndex = nameIndex; 45 | this.descriptorIndex = descriptorIndex; 46 | this.attributeCount = 1; // code attribute 47 | this.attributes = codeAttributeInfo; 48 | } 49 | 50 | public void write(ByteOutStream o) { 51 | o.write_u2(this.accessFlags); 52 | o.write_u2(this.nameIndex); 53 | o.write_u2(this.descriptorIndex); 54 | o.write_u2(this.attributeCount); 55 | this.attributes.write(o); 56 | } 57 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/functions/Method_Int.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.functions; 26 | 27 | import static de.lorenzwiest.basiccompiler.classfile.ClassModel.JavaMethod.MATH_FLOOR; 28 | 29 | import java.util.List; 30 | 31 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 32 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 33 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 34 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 35 | 36 | public class Method_Int extends Method { 37 | private static final String METHOD_NAME = "Int"; 38 | private static final String DESCRIPTOR = "(F)F"; 39 | private static final int NUM_LOCALS = 1; 40 | 41 | public Method_Int(LibraryManager libraryManager) { 42 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 43 | } 44 | 45 | @Override 46 | public void addMethodBytecode(ByteOutStream o, List e) { 47 | 48 | // local 0: F argument 49 | 50 | o.fload_0(); 51 | 52 | o.f2d(); 53 | o.invokestatic(this.classModel.getJavaMethodRefIndex(MATH_FLOOR)); 54 | o.d2f(); 55 | 56 | o.freturn(); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/statements/impl/ForStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.statements.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.nodes.INode; 28 | import de.lorenzwiest.basiccompiler.parser.nodes.impl.VariableNode; 29 | import de.lorenzwiest.basiccompiler.parser.statements.Statement; 30 | 31 | public class ForStatement implements Statement { 32 | private final VariableNode loopVariable; 33 | private final INode startExpression; 34 | private final INode endExpression; 35 | private final INode stepExpression; 36 | 37 | public ForStatement(VariableNode loopVariable, INode startExpression, INode endExpression, INode stepExpression) { 38 | this.loopVariable = loopVariable; 39 | this.startExpression = startExpression; 40 | this.endExpression = endExpression; 41 | this.stepExpression = stepExpression; 42 | } 43 | 44 | public VariableNode getLoopVariable() { 45 | return this.loopVariable; 46 | } 47 | 48 | public INode getStartExpression() { 49 | return this.startExpression; 50 | } 51 | 52 | public INode getEndExpression() { 53 | return this.endExpression; 54 | } 55 | 56 | public INode getStepExpression() { 57 | return this.stepExpression; 58 | } 59 | } -------------------------------------------------------------------------------- /samples/INKBLOT.BAS: -------------------------------------------------------------------------------- 1 | 100 PRINT TAB(26);"INKBLOT" 2 | 105 PRINT TAB(20);"CREATIVE CMOPUTING" 3 | 110 PRINT TAB(18);"MORRISTOWN, NEW JERSEY" 4 | 115 PRINT:PRINT:PRINT 5 | 120 REM *** WORKS BY PLOTTING ELLIPSES AND THEIR MIRROR IMAGES 6 | 130 DIM A(12,13),B$(36),A$(36) 7 | 140 REM *** CHOOSE FROM 5 TO 12 ELLIPSES 8 | 150 M=INT(8*RND(1))+5 9 | 160 REM *** CREATE SIZE, LOCATION AND ANGLE OF M ELLIPSES 10 | 170 FOR L=1 TO M 11 | 180 A(L,1)=34*RND(1) 12 | 190 A(L,2)=80*RND(1) 13 | 200 A(L,3)=(15*RND(1)+2)^2 14 | 210 A(L,4)=(15*RND(1)+2)^2 15 | 220 T=3.14159*RND(1) 16 | 230 A(L,5)=COS(T) 17 | 240 A(L,6)=SIN(T) 18 | 250 A(L,7)=A(L,5)*A(L,6) 19 | 260 A(L,5)=A(L,5)*A(L,5) 20 | 270 A(L,6)=A(L,6)*A(L,6) 21 | 280 A(L,8)=A(L,1)*A(L,1)*A(L,6) 22 | 290 A(L,9)=A(L,1)*A(L,1)*A(L,5) 23 | 300 A(L,10)=A(L,1)*A(L,7) 24 | 310 A(L,11)=-2*A(L,1)*A(L,6) 25 | 320 A(L,12)=-2*A(L,1)*A(L,5) 26 | 330 A(L,13)=A(L,6)/A(L,4)+A(L,5)/A(L,3) 27 | 340 NEXT L 28 | 350 REM *** PRINT TOP BORDER; B$ CONTAINS 36 DOLLAR SIGNS 29 | 360 B$="$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" 30 | 370 PRINT B$;B$ 31 | 380 PRINT B$;B$ 32 | 390 REM *** LOOP Y IS Y-COORDINATE OF PLOT; EACH TIME Y LOOP 33 | 400 REM *** IS EXECUTED, A LINE IS PRINTED 34 | 410 FOR Y=79.9 TO 0 STEP -1.6 35 | 420 A$="$$ " 36 | 430 REM *** LOOP E CHECKS THE EQUATION OF EACH ELLIPSE TO SEE 37 | 440 REM *** IF IT INTERSECTS THE LINE TO BE PRINTED 38 | 450 FOR E=1 TO M 39 | 460 Y1=Y-A(E,2) 40 | 470 Y2=Y1*Y1 41 | 480 Y3=Y1*A(E,10) 42 | 490 Y4=Y1*A(E,7) 43 | 500 B=(A(E,12)+Y4)/A(E,3)+(-Y4+A(E,11))/A(E,4) 44 | 510 C=(Y2*A(E,6)+A(E,9)-Y3)/A(E,3)+(Y2*A(E,5)+A(E,8)+Y3)/A(E,4)-1 45 | 520 REM *** R IS THE RADICAL IN THE STANDARD QUADRATIC FORMULA 46 | 530 R=B*B-4*A(E,13)*C 47 | 540 IF R<0 THEN 690 48 | 550 R=SQR(R) 49 | 560 REM *** FIND WHERE THE LIE INTERSECTS THE ELLIPSE 50 | 570 R1=INT(-(B+R)/2/A(E,13)+1) 51 | 580 IF R1>34 THEN 690 52 | 590 R2=INT((R-B)/2/A(E,13)) 53 | 600 IF R2<1 THEN 690 54 | 610 IF R2<35 THEN 630 55 | 620 R2=34 56 | 630 IF R1>0 THEN 660 57 | 640 R1=1 58 | 650 REM *** FILL IN THE LINE WHERE IT CROSSES THE ELLIPSE 59 | 660 FOR J=R1+2 TO R2+2 60 | 670 A$=LEFT$(A$,J-1)+"$"+RIGHT$(A$,LEN(A$)-J) 61 | 680 NEXT J 62 | 690 NEXT E 63 | 700 REM *** PRINT LINE 64 | 710 PRINT A$; 65 | 720 FOR K=36 TO 1 STEP -1 66 | 730 PRINT MID$(A$,K,1); 67 | 740 NEXT K 68 | 745 PRINT 69 | 750 NEXT Y 70 | 760 REM *** PRINT BOTTOM BORDER 71 | 770 PRINT B$;B$ 72 | 780 PRINT B$;B$ 73 | 790 END -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/helper/Method_StringToChars.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.helper; 26 | 27 | import static de.lorenzwiest.basiccompiler.classfile.ClassModel.JavaMethod.STRING_TO_CHAR_ARRAY; 28 | 29 | import java.util.List; 30 | 31 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 32 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 33 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 34 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 35 | 36 | public class Method_StringToChars extends Method { 37 | private static final String METHOD_NAME = "StringToChars"; 38 | private static final String DESCRIPTOR = "(Ljava/lang/String;)[C"; 39 | private static final int NUM_LOCALS = 1; 40 | 41 | public Method_StringToChars(LibraryManager libraryManager) { 42 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 43 | } 44 | 45 | @Override 46 | public void addMethodBytecode(ByteOutStream o, List e) { 47 | 48 | // local 0: REF string 49 | 50 | o.aload_0(); 51 | o.invokevirtual(this.classModel.getJavaMethodRefIndex(STRING_TO_CHAR_ARRAY)); 52 | o.areturn(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/functions/Method_Asc.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.functions; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 33 | 34 | public class Method_Asc extends Method { 35 | private static final String METHOD_NAME = "Asc"; 36 | private static final String DESCRIPTOR = "([C)F"; 37 | private static final int NUM_LOCALS = 1; 38 | 39 | public Method_Asc(LibraryManager libraryManager) { 40 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 41 | } 42 | 43 | @Override 44 | public void addMethodBytecode(ByteOutStream o, List e) { 45 | 46 | // local 0: F argument 47 | 48 | o.aload_0(); 49 | o.arraylength(); 50 | o.ifgt("skipNullString"); 51 | 52 | emitThrowRuntimeException(o, "ASC(): Length of string < 1."); 53 | 54 | o.label("skipNullString"); 55 | o.aload_0(); 56 | o.iconst_0(); 57 | o.caload(); 58 | o.i2f(); 59 | o.freturn(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/helper/Method_ReadNumFromDataToStack.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.helper; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager.MethodEnum; 33 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 34 | 35 | public class Method_ReadNumFromDataToStack extends Method { 36 | private static final String METHOD_NAME = "ReadNumFromDataToStack"; 37 | private static final String DESCRIPTOR = "()F"; 38 | private static final int NUM_LOCALS = 0; 39 | 40 | public Method_ReadNumFromDataToStack(LibraryManager libraryManager) { 41 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 42 | } 43 | 44 | @Override 45 | public void addMethodBytecode(ByteOutStream o, List e) { 46 | this.libraryManager.getMethod(MethodEnum.READ_STRING_FROM_DATA_TO_STACK).emitCall(o); 47 | this.libraryManager.getMethod(MethodEnum.VAL).emitCall(o); 48 | o.freturn(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/nodes/impl/BinaryNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.nodes.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.nodes.INode; 28 | import de.lorenzwiest.basiccompiler.parser.nodes.NodeType; 29 | import de.lorenzwiest.basiccompiler.parser.tokens.Token; 30 | 31 | public class BinaryNode implements INode { 32 | private final Token op; 33 | private final INode leftNode; 34 | private final INode rightNode; 35 | private final NodeType nodeType; 36 | 37 | private BinaryNode(Token op, INode leftNode, INode rightNode, NodeType nodeType) { 38 | this.op = op; 39 | this.leftNode = leftNode; 40 | this.rightNode = rightNode; 41 | this.nodeType = nodeType; 42 | } 43 | 44 | public static INode create(Token op, INode leftNode, INode rightNode, NodeType nodeType) { 45 | if ((leftNode == null) || (rightNode == null)) { 46 | return null; 47 | } 48 | return new BinaryNode(op, leftNode, rightNode, nodeType); 49 | } 50 | 51 | public Token getOp() { 52 | return this.op; 53 | } 54 | 55 | public INode getLeftNode() { 56 | return this.leftNode; 57 | } 58 | 59 | public INode getRightNode() { 60 | return this.rightNode; 61 | } 62 | 63 | @Override 64 | public NodeType getType() { 65 | return this.nodeType; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/helper/Method_GosubStackInitialize.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.helper; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.Compiler; 31 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 32 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 33 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 34 | 35 | public class Method_GosubStackInitialize extends Method { 36 | private static final String METHOD_NAME = "GosubStackInitialize"; 37 | private static final String DESCRIPTOR = "()V"; 38 | private static final int NUM_LOCALS = 0; 39 | 40 | public Method_GosubStackInitialize(LibraryManager libraryManager) { 41 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 42 | } 43 | 44 | @Override 45 | public void addMethodBytecode(ByteOutStream o, List e) { 46 | int gosubStackFieldRef = this.classModel.getFieldRefIndex(Compiler.FIELD_GOSUB_STACK, "[I"); 47 | 48 | o.iconst(Compiler.GOSUB_STACK_SIZE * Compiler.GOSUB_STACK_FRAME_SIZE); 49 | o.newarray_int(); 50 | o.putstatic(gosubStackFieldRef); 51 | o.return_(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/classfile/constantpoolinfo/impl/ConstantPoolInfo_Long.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2018 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.classfile.constantpoolinfo.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.classfile.ConstantPool; 28 | import de.lorenzwiest.basiccompiler.classfile.constantpoolinfo.ConstantPoolInfo; 29 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 30 | 31 | public class ConstantPoolInfo_Long extends ConstantPoolInfo { 32 | private final long aLong; 33 | 34 | public ConstantPoolInfo_Long(long aLong) { 35 | super(TAG_LONG); 36 | this.aLong = aLong; 37 | } 38 | 39 | public long getLong() { 40 | return this.aLong; 41 | } 42 | 43 | @Override 44 | public void write(ByteOutStream o) { 45 | super.write(o); 46 | o.write_u8(this.aLong); 47 | } 48 | 49 | public static int addAndGetIndex(ConstantPool constantPool, long aLong) { 50 | String key = getKey(aLong); 51 | if (constantPool.contains(key) == false) { 52 | constantPool.put(key, createInfo(constantPool, aLong)); 53 | } 54 | return constantPool.getIndex(key); 55 | } 56 | 57 | private static String getKey(long aLong) { 58 | return "LONG_" + aLong; 59 | } 60 | 61 | private static ConstantPoolInfo_Long createInfo(ConstantPool constantPool, long aLong) { 62 | return new ConstantPoolInfo_Long(aLong); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/helper/Method_PrintStringFromStack.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.helper; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager.MethodEnum; 33 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 34 | 35 | public class Method_PrintStringFromStack extends Method { 36 | private static final String METHOD_NAME = "PrintStringFromStack"; 37 | private static final String DESCRIPTOR = "(Ljava/lang/String;)V"; 38 | private static final int NUM_LOCALS = 1; 39 | 40 | public Method_PrintStringFromStack(LibraryManager libraryManager) { 41 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 42 | } 43 | 44 | @Override 45 | public void addMethodBytecode(ByteOutStream o, List e) { 46 | 47 | // local 0: REF String 48 | 49 | o.aload_0(); 50 | this.libraryManager.getMethod(MethodEnum.STRING_TO_CHARS).emitCall(o); 51 | this.libraryManager.getMethod(MethodEnum.PRINT_CHARS_FROM_STACK).emitCall(o); 52 | o.return_(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/functions/Method_Sgn.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.functions; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 33 | 34 | public class Method_Sgn extends Method { 35 | private static final String METHOD_NAME = "Sgn"; 36 | private static final String DESCRIPTOR = "(F)F"; 37 | private static final int NUM_LOCALS = 1; 38 | 39 | public Method_Sgn(LibraryManager libraryManager) { 40 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 41 | } 42 | 43 | @Override 44 | public void addMethodBytecode(ByteOutStream o, List e) { 45 | 46 | // local 0: F argument => I difference 47 | 48 | o.fload_0(); 49 | o.fconst_0(); 50 | o.fcmpg(); 51 | o.istore_0(); 52 | 53 | o.iload_0(); 54 | o.ifeq("isZeroArg"); 55 | 56 | o.fconst_1(); 57 | o.iload_0(); 58 | o.ifgt("isPositiveArg"); 59 | 60 | o.fneg(); 61 | 62 | o.label("isPositiveArg"); 63 | o.freturn(); 64 | 65 | o.label("isZeroArg"); 66 | o.fconst_0(); 67 | o.freturn(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/operators/Method_Or.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.operators; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager.MethodEnum; 33 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 34 | 35 | public class Method_Or extends Method { 36 | private static final String METHOD_NAME = "Or"; 37 | private static final String DESCRIPTOR = "(FF)F"; 38 | private static final int NUM_LOCALS = 2; 39 | 40 | public Method_Or(LibraryManager libraryManager) { 41 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 42 | } 43 | 44 | @Override 45 | public void addMethodBytecode(ByteOutStream o, List e) { 46 | 47 | // local 0: F=>I argument 1 48 | // local 1: F=>I argument 2 49 | 50 | o.fload_0(); 51 | o.fload_1(); 52 | this.libraryManager.getMethod(MethodEnum.CHECK_LOGICAL_OPERATION_ARGUMENTS).emitCall(o); 53 | 54 | o.fload_0(); 55 | o.f2i(); 56 | o.fload_1(); 57 | o.f2i(); 58 | o.ior(); 59 | 60 | o.i2f(); 61 | o.freturn(); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/operators/Method_And.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.operators; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager.MethodEnum; 33 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 34 | 35 | public class Method_And extends Method { 36 | private static final String METHOD_NAME = "And"; 37 | private static final String DESCRIPTOR = "(FF)F"; 38 | private static final int NUM_LOCALS = 2; 39 | 40 | public Method_And(LibraryManager libraryManager) { 41 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 42 | } 43 | 44 | @Override 45 | public void addMethodBytecode(ByteOutStream o, List e) { 46 | 47 | // local 0: F=>I argument 1 48 | // local 1: F=>I argument 2 49 | 50 | o.fload_0(); 51 | o.fload_1(); 52 | this.libraryManager.getMethod(MethodEnum.CHECK_LOGICAL_OPERATION_ARGUMENTS).emitCall(o); 53 | 54 | o.fload_0(); 55 | o.f2i(); 56 | o.fload_1(); 57 | o.f2i(); 58 | o.iand(); 59 | 60 | o.i2f(); 61 | o.freturn(); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/operators/Method_Xor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.operators; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager.MethodEnum; 33 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 34 | 35 | public class Method_Xor extends Method { 36 | private static final String METHOD_NAME = "Xor"; 37 | private static final String DESCRIPTOR = "(FF)F"; 38 | private static final int NUM_LOCALS = 2; 39 | 40 | public Method_Xor(LibraryManager libraryManager) { 41 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 42 | } 43 | 44 | @Override 45 | public void addMethodBytecode(ByteOutStream o, List e) { 46 | 47 | // local 0: F=>I argument 1 48 | // local 1: F=>I argument 2 49 | 50 | o.fload_0(); 51 | o.fload_1(); 52 | this.libraryManager.getMethod(MethodEnum.CHECK_LOGICAL_OPERATION_ARGUMENTS).emitCall(o); 53 | 54 | o.fload_0(); 55 | o.f2i(); 56 | o.fload_1(); 57 | o.f2i(); 58 | o.ixor(); 59 | 60 | o.i2f(); 61 | o.freturn(); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/helper/Method_PrintFloatFromStack.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.helper; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager.MethodEnum; 33 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 34 | 35 | public class Method_PrintFloatFromStack extends Method { 36 | private static final String METHOD_NAME = "PrintFloatFromStack"; 37 | private static final String DESCRIPTOR = "(F)V"; 38 | private static final int NUM_LOCALS = 1; 39 | 40 | public Method_PrintFloatFromStack(LibraryManager libraryManager) { 41 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 42 | } 43 | 44 | @Override 45 | public void addMethodBytecode(ByteOutStream o, List e) { 46 | 47 | o.fload_0(); 48 | this.libraryManager.getMethod(MethodEnum.FLOAT_TO_CHARS).emitCall(o); 49 | this.libraryManager.getMethod(MethodEnum.PRINT_CHARS_FROM_STACK).emitCall(o); 50 | 51 | o.iconst(' '); 52 | this.libraryManager.getMethod(MethodEnum.PRINT_CHAR_FROM_STACK).emitCall(o); 53 | o.return_(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/classfile/constantpoolinfo/impl/ConstantPoolInfo_Integer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2018 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.classfile.constantpoolinfo.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.classfile.ConstantPool; 28 | import de.lorenzwiest.basiccompiler.classfile.constantpoolinfo.ConstantPoolInfo; 29 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 30 | 31 | public class ConstantPoolInfo_Integer extends ConstantPoolInfo { 32 | private final int anInteger; 33 | 34 | public ConstantPoolInfo_Integer(int anInteger) { 35 | super(TAG_INTEGER); 36 | this.anInteger = anInteger; 37 | } 38 | 39 | public int getInteger() { 40 | return this.anInteger; 41 | } 42 | 43 | @Override 44 | public void write(ByteOutStream o) { 45 | super.write(o); 46 | o.write_u4(this.anInteger); 47 | } 48 | 49 | public static int addAndGetIndex(ConstantPool constantPool, int anInteger) { 50 | String key = getKey(anInteger); 51 | if (constantPool.contains(key) == false) { 52 | constantPool.put(key, createInfo(constantPool, anInteger)); 53 | } 54 | return constantPool.getIndex(key); 55 | } 56 | 57 | private static String getKey(int anInteger) { 58 | return "INTEGER_" + anInteger; 59 | } 60 | 61 | private static ConstantPoolInfo_Integer createInfo(ConstantPool constantPool, int anInteger) { 62 | return new ConstantPoolInfo_Integer(anInteger); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/operators/Method_DivisionByZero.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.operators; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.Compiler; 31 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 32 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 33 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 34 | 35 | public class Method_DivisionByZero extends Method { 36 | private static final String METHOD_NAME = "DivisionByZero"; 37 | private static final String DESCRIPTOR = "(F)F"; 38 | private static final int NUM_LOCALS = 1; 39 | 40 | public Method_DivisionByZero(LibraryManager libraryManager) { 41 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 42 | } 43 | 44 | @Override 45 | public void addMethodBytecode(ByteOutStream o, List e) { 46 | 47 | // local 0: F=>I division numerator 48 | 49 | emitPrint(o, "Division by zero" + Compiler.CR); 50 | 51 | o.fconst_1(); // create POSITIVE_INFINITY value 52 | o.fconst_0(); 53 | o.fdiv(); 54 | 55 | o.fload_0(); 56 | o.fconst_0(); 57 | o.fcmpg(); 58 | o.ifge("exit"); 59 | 60 | o.fneg(); 61 | 62 | o.label("exit"); 63 | o.freturn(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/parser/nodes/impl/VariableNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.parser.nodes.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.parser.nodes.INode; 28 | import de.lorenzwiest.basiccompiler.parser.nodes.NodeType; 29 | 30 | public class VariableNode implements INode { 31 | private static final INode[] EMPTY = new INode[0]; 32 | 33 | private final String varName; 34 | private final NodeType type; 35 | private final INode[] dimExpressions; 36 | 37 | private VariableNode(String variableName, NodeType type, INode[] dimExpressions) { 38 | this.varName = variableName; 39 | this.type = type; 40 | this.dimExpressions = dimExpressions; 41 | } 42 | 43 | protected VariableNode(String variableName, NodeType type) { 44 | this(variableName, type, EMPTY); 45 | } 46 | 47 | public static VariableNode create(String variableName, NodeType type, INode... dimExpressions) { 48 | return new VariableNode(variableName, type, dimExpressions); 49 | } 50 | 51 | public static VariableNode createVariableNode(String variableName, NodeType type) { 52 | return new VariableNode(variableName, type, EMPTY); 53 | } 54 | 55 | public String getVariableName() { 56 | return this.varName; 57 | } 58 | 59 | public INode[] getDimExpressions() { 60 | return this.dimExpressions; 61 | } 62 | 63 | @Override 64 | public NodeType getType() { 65 | return this.type; 66 | } 67 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/arrays/Method_Dim1DCheckSize.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.arrays; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 33 | 34 | public class Method_Dim1DCheckSize extends Method { 35 | private static final String METHOD_NAME = "Dim1DCheckSize"; 36 | private static final String DESCRIPTOR = "(I)V"; 37 | private static final int NUM_LOCALS = 1; 38 | 39 | public Method_Dim1DCheckSize(LibraryManager libraryManager) { 40 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 41 | } 42 | 43 | @Override 44 | public void addMethodBytecode(ByteOutStream o, List e) { 45 | 46 | // local 0: I dim size 47 | 48 | o.iload_0(); 49 | o.ifge("skipSizeUnderflow"); 50 | 51 | emitThrowRuntimeException(o, "DIM 1D array: Max index < 0."); 52 | 53 | o.label("skipSizeUnderflow"); 54 | o.iload_0(); 55 | o.iconst(32767); 56 | o.if_icmple("skipSizeOverflow"); 57 | 58 | emitThrowRuntimeException(o, "DIM 1D array: Max index > 32767."); 59 | 60 | o.label("skipSizeOverflow"); 61 | o.return_(); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/functions/Method_Log.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.functions; 26 | 27 | import static de.lorenzwiest.basiccompiler.classfile.ClassModel.JavaMethod.MATH_LOG; 28 | 29 | import java.util.List; 30 | 31 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 32 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 33 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 34 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 35 | 36 | public class Method_Log extends Method { 37 | private static final String METHOD_NAME = "Log"; 38 | private static final String DESCRIPTOR = "(F)F"; 39 | private static final int NUM_LOCALS = 1; 40 | 41 | public Method_Log(LibraryManager libraryManager) { 42 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 43 | } 44 | 45 | @Override 46 | public void addMethodBytecode(ByteOutStream o, List e) { 47 | 48 | // local 0: F argument 49 | 50 | o.fload_0(); 51 | o.fconst_0(); 52 | o.fcmpg(); 53 | o.ifgt("isPositiveArg"); 54 | 55 | emitThrowRuntimeException(o, "LOG(): Argument <= 0."); 56 | 57 | o.label("isPositiveArg"); 58 | o.fload_0(); 59 | o.f2d(); 60 | o.invokestatic(this.classModel.getJavaMethodRefIndex(MATH_LOG)); 61 | o.d2f(); 62 | o.freturn(); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/helper/Method_RoundToInt.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.helper; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 33 | 34 | public class Method_RoundToInt extends Method { 35 | private static final String METHOD_NAME = "RoundToInt"; 36 | private static final String DESCRIPTOR = "(F)I"; 37 | private static final int NUM_LOCALS = 1; 38 | 39 | public Method_RoundToInt(LibraryManager libraryManager) { 40 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 41 | } 42 | 43 | @Override 44 | public void addMethodBytecode(ByteOutStream o, List e) { 45 | 46 | // local 0: F argument 47 | 48 | o.fload_0(); 49 | o.fconst_0(); 50 | o.fcmpg(); 51 | o.iflt("isNegValue"); 52 | 53 | o.fload_0(); 54 | o.ldc(this.classModel.getFloatIndex(0.5f)); 55 | o.fadd(); 56 | o.f2i(); 57 | o.ireturn(); 58 | 59 | o.label("isNegValue"); 60 | o.fload_0(); 61 | o.fneg(); 62 | o.ldc(this.classModel.getFloatIndex(0.5f)); 63 | o.fadd(); 64 | o.fneg(); 65 | o.f2i(); 66 | o.ireturn(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/functions/Method_Val.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.functions; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager.MethodEnum; 33 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 34 | 35 | public class Method_Val extends Method { 36 | private static final String METHOD_NAME = "Val"; 37 | private static final String DESCRIPTOR = "([C)F"; 38 | private static final int NUM_LOCALS = 2; 39 | 40 | public Method_Val(LibraryManager libraryManager) { 41 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 42 | } 43 | 44 | @Override 45 | public void addMethodBytecode(ByteOutStream o, List e) { 46 | 47 | // local 0: [C argument 48 | // local 1: F float 49 | 50 | o.aload_0(); 51 | this.libraryManager.getMethod(MethodEnum.CHARS_TO_FLOAT).emitCall(o); 52 | o.fstore_1(); 53 | 54 | o.fload_1(); 55 | o.fload_1(); 56 | o.fcmpg(); 57 | o.ifne("isNaN"); 58 | 59 | o.fload_1(); 60 | o.freturn(); 61 | 62 | o.label("isNaN"); 63 | o.fconst_0(); 64 | o.freturn(); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/classfile/constantpoolinfo/impl/ConstantPoolInfo_Float.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.classfile.constantpoolinfo.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.classfile.ConstantPool; 28 | import de.lorenzwiest.basiccompiler.classfile.constantpoolinfo.ConstantPoolInfo; 29 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 30 | 31 | public class ConstantPoolInfo_Float extends ConstantPoolInfo { 32 | private final float aFloat; 33 | 34 | public ConstantPoolInfo_Float(float aFloat) { 35 | super(TAG_FLOAT); 36 | this.aFloat = aFloat; 37 | } 38 | 39 | public float getFloat() { 40 | return this.aFloat; 41 | } 42 | 43 | @Override 44 | public void write(ByteOutStream o) { 45 | super.write(o); 46 | int bitsOfFloat = Float.floatToRawIntBits(this.aFloat); 47 | o.write_u4(bitsOfFloat); 48 | } 49 | 50 | public static int addAndGetIndex(ConstantPool constantPool, float aFloat) { 51 | String key = getKey(aFloat); 52 | if (constantPool.contains(key) == false) { 53 | constantPool.put(key, createInfo(constantPool, aFloat)); 54 | } 55 | return constantPool.getIndex(key); 56 | } 57 | 58 | private static String getKey(float aFloat) { 59 | return "FLOAT_" + aFloat; 60 | } 61 | 62 | private static ConstantPoolInfo_Float createInfo(ConstantPool constantPool, float aFloat) { 63 | return new ConstantPoolInfo_Float(aFloat); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/functions/Method_Sqr.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.functions; 26 | 27 | import static de.lorenzwiest.basiccompiler.classfile.ClassModel.JavaMethod.MATH_SQRT; 28 | 29 | import java.util.List; 30 | 31 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 32 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 33 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 34 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 35 | 36 | public class Method_Sqr extends Method { 37 | private static final String METHOD_NAME = "Sqr"; 38 | private static final String DESCRIPTOR = "(F)F"; 39 | private static final int NUM_LOCALS = 1; 40 | 41 | public Method_Sqr(LibraryManager libraryManager) { 42 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 43 | } 44 | 45 | @Override 46 | public void addMethodBytecode(ByteOutStream o, List e) { 47 | 48 | // local 0: F argument 49 | 50 | o.fload_0(); 51 | o.fconst_0(); 52 | o.fcmpg(); 53 | o.ifge("isZeroOrPositiveArg"); 54 | 55 | emitThrowRuntimeException(o, "SQR(): Argument < 0."); 56 | 57 | o.label("isZeroOrPositiveArg"); 58 | o.fload_0(); 59 | o.f2d(); 60 | o.invokestatic(this.classModel.getJavaMethodRefIndex(MATH_SQRT)); 61 | o.d2f(); 62 | o.freturn(); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/helper/Method_CheckOnGotoGosubArg.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.helper; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 33 | 34 | public class Method_CheckOnGotoGosubArg extends Method { 35 | private static final String METHOD_NAME = "CheckOnGotoGosubArg"; 36 | private static final String DESCRIPTOR = "(I)V"; 37 | private static final int NUM_LOCALS = 1; 38 | 39 | public Method_CheckOnGotoGosubArg(LibraryManager libraryManager) { 40 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 41 | } 42 | 43 | @Override 44 | public void addMethodBytecode(ByteOutStream o, List e) { 45 | 46 | // local 0: I argument 47 | 48 | o.iload_0(); 49 | o.ifge("skipIndexUnderflow"); 50 | 51 | emitThrowRuntimeException(o, "ON GOTO/GOSUB: Index < 0."); 52 | 53 | o.label("skipIndexUnderflow"); 54 | o.iload_0(); 55 | o.iconst(0xFF); 56 | o.if_icmple("skipIndexOverflow"); 57 | 58 | emitThrowRuntimeException(o, "ON GOTO/GOSUB: Index > 255."); 59 | 60 | o.label("skipIndexOverflow"); 61 | o.return_(); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/classfile/constantpoolinfo/impl/ConstantPoolInfo_Double.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2018 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.classfile.constantpoolinfo.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.classfile.ConstantPool; 28 | import de.lorenzwiest.basiccompiler.classfile.constantpoolinfo.ConstantPoolInfo; 29 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 30 | 31 | public class ConstantPoolInfo_Double extends ConstantPoolInfo { 32 | private final double aDouble; 33 | 34 | public ConstantPoolInfo_Double(Double aDouble) { 35 | super(TAG_DOUBLE); 36 | this.aDouble = aDouble; 37 | } 38 | 39 | public double getDouble() { 40 | return this.aDouble; 41 | } 42 | 43 | @Override 44 | public void write(ByteOutStream o) { 45 | super.write(o); 46 | long bitsOfDouble = Double.doubleToRawLongBits(this.aDouble); 47 | o.write_u8(bitsOfDouble); 48 | } 49 | 50 | public static int addAndGetIndex(ConstantPool constantPool, double aDouble) { 51 | String key = getKey(aDouble); 52 | if (constantPool.contains(key) == false) { 53 | constantPool.put(key, createInfo(constantPool, aDouble)); 54 | } 55 | return constantPool.getIndex(key); 56 | } 57 | 58 | private static String getKey(double aDouble) { 59 | return "DOUBLE_" + aDouble; 60 | } 61 | 62 | private static ConstantPoolInfo_Double createInfo(ConstantPool constantPool, double aDouble) { 63 | return new ConstantPoolInfo_Double(aDouble); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/etc/LocalVariableTable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.etc; 26 | 27 | import java.util.HashMap; 28 | import java.util.Map; 29 | import java.util.TreeMap; 30 | 31 | import de.lorenzwiest.basiccompiler.parser.nodes.NodeType; 32 | import de.lorenzwiest.basiccompiler.parser.nodes.impl.LocalVariableNode; 33 | 34 | public class LocalVariableTable { 35 | private Map map = new HashMap(); 36 | 37 | public LocalVariableNode addAndGetLocalVariableNode(String variableName, NodeType type) { 38 | if (this.map.containsKey(variableName) == false) { 39 | int localIndex = this.map.size() + 1; 40 | LocalVariableNode locVarNode = LocalVariableNode.create(variableName, type, localIndex); 41 | this.map.put(variableName, locVarNode); 42 | } 43 | return this.map.get(variableName); 44 | } 45 | 46 | public LocalVariableNode get(String varName) { 47 | return this.map.get(varName); 48 | } 49 | 50 | public int size() { 51 | return this.map.size(); 52 | } 53 | 54 | public LocalVariableNode[] sortByLocalIndex() { 55 | Map sortedMap = new TreeMap(); 56 | for (LocalVariableNode locVarNode : this.map.values()) { 57 | sortedMap.put(locVarNode.getLocalIndex(), locVarNode); 58 | } 59 | return sortedMap.values().toArray(new LocalVariableNode[sortedMap.size()]); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/helper/Method_ThrowRuntimeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.helper; 26 | 27 | import static de.lorenzwiest.basiccompiler.classfile.ClassModel.JavaClass.RUNTIME_EXCEPTION; 28 | import static de.lorenzwiest.basiccompiler.classfile.ClassModel.JavaMethod.RUNTIME_EXCEPTION_INIT; 29 | 30 | import java.util.List; 31 | 32 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 33 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 34 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 35 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 36 | 37 | public class Method_ThrowRuntimeException extends Method { 38 | private static final String METHOD_NAME = "ThrowRuntimeException"; 39 | private static final String DESCRIPTOR = "(Ljava/lang/String;)V"; 40 | private static final int NUM_LOCALS = 1; 41 | 42 | public Method_ThrowRuntimeException(LibraryManager libraryManager) { 43 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 44 | } 45 | 46 | @Override 47 | public void addMethodBytecode(ByteOutStream o, List e) { 48 | 49 | // local 0: REF string 50 | 51 | o.new_(this.classModel.getJavaClassRefIndex(RUNTIME_EXCEPTION)); 52 | o.dup(); 53 | o.aload_0(); 54 | o.invokespecial(this.classModel.getJavaMethodRefIndex(RUNTIME_EXCEPTION_INIT)); 55 | o.athrow(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/classfile/constantpoolinfo/impl/ConstantPoolInfo_String.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.classfile.constantpoolinfo.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.classfile.ConstantPool; 28 | import de.lorenzwiest.basiccompiler.classfile.constantpoolinfo.ConstantPoolInfo; 29 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 30 | 31 | public class ConstantPoolInfo_String extends ConstantPoolInfo { 32 | private final int stringIndex; // u2 33 | 34 | public ConstantPoolInfo_String(int stringIndex) { 35 | super(TAG_STRING); 36 | this.stringIndex = stringIndex; 37 | } 38 | 39 | public int getStringIndex() { 40 | return this.stringIndex; 41 | } 42 | 43 | @Override 44 | public void write(ByteOutStream o) { 45 | super.write(o); 46 | o.write_u2(this.stringIndex); 47 | } 48 | 49 | public static int addAndGetIndex(ConstantPool constantPool, String string) { 50 | String key = getKey(string); 51 | if (constantPool.contains(key) == false) { 52 | constantPool.put(key, createInfo(constantPool, string)); 53 | } 54 | return constantPool.getIndex(key); 55 | } 56 | 57 | private static String getKey(String string) { 58 | return "STRING_" + string; 59 | } 60 | 61 | private static ConstantPoolInfo_String createInfo(ConstantPool constantPool, String string) { 62 | int stringIndex = ConstantPoolInfo_Utf8.addAndGetIndex(constantPool, string); 63 | return new ConstantPoolInfo_String(stringIndex); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/operators/Method_Division.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.operators; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager.MethodEnum; 33 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 34 | 35 | public class Method_Division extends Method { 36 | private static final String METHOD_NAME = "Division"; 37 | private static final String DESCRIPTOR = "(FF)F"; 38 | private static final int NUM_LOCALS = 2; 39 | 40 | public Method_Division(LibraryManager libraryManager) { 41 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 42 | } 43 | 44 | @Override 45 | public void addMethodBytecode(ByteOutStream o, List e) { 46 | 47 | // local 0: F numerator (determines sign of result when division by zero) 48 | // local 1: F denominator 49 | 50 | o.fload_1(); 51 | o.fconst_0(); 52 | o.fcmpg(); 53 | o.ifeq("divisionByZero"); 54 | 55 | o.fload_0(); 56 | o.fload_1(); 57 | o.fdiv(); 58 | o.freturn(); 59 | 60 | o.label("divisionByZero"); 61 | o.fload_0(); 62 | this.libraryManager.getMethod(MethodEnum.DIVISION_BY_ZERO).emitCall(o); 63 | o.freturn(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/arrays/Method_LoadFloatFrom1DArray.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.arrays; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager.MethodEnum; 33 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 34 | 35 | public class Method_LoadFloatFrom1DArray extends Method { 36 | private static final String METHOD_NAME = "LoadFloatFrom1DArray"; 37 | private static final String DESCRIPTOR = "([[FF)F"; 38 | private static final int NUM_LOCALS = 2; 39 | 40 | public Method_LoadFloatFrom1DArray(LibraryManager libraryManager) { 41 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 42 | } 43 | 44 | @Override 45 | public void addMethodBytecode(ByteOutStream o, List e) { 46 | 47 | // local 0: [[F reference to array reference 48 | // local 1: F=>I array index 49 | 50 | o.fload_1(); 51 | this.libraryManager.getMethod(MethodEnum.ROUND_TO_INT).emitCall(o); 52 | o.istore_1(); 53 | 54 | o.aload_0(); 55 | o.iload_1(); 56 | this.libraryManager.getMethod(MethodEnum.CHECK_1D_FLOAT_ARRAY_ACCESS).emitCall(o); 57 | 58 | o.aload_0(); 59 | o.iconst_0(); 60 | o.aaload(); 61 | o.iload_1(); 62 | o.faload(); 63 | o.freturn(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/arrays/Method_LoadStringFrom1DArray.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.arrays; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager.MethodEnum; 33 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 34 | 35 | public class Method_LoadStringFrom1DArray extends Method { 36 | private static final String METHOD_NAME = "LoadStringFrom1DArray"; 37 | private static final String DESCRIPTOR = "([[[CF)[C"; 38 | private static final int NUM_LOCALS = 2; 39 | 40 | public Method_LoadStringFrom1DArray(LibraryManager libraryManager) { 41 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 42 | } 43 | 44 | @Override 45 | public void addMethodBytecode(ByteOutStream o, List e) { 46 | 47 | // local 0: [[[C reference to array reference 48 | // local 1: F=>I array index 49 | 50 | o.fload_1(); 51 | this.libraryManager.getMethod(MethodEnum.ROUND_TO_INT).emitCall(o); 52 | o.istore_1(); 53 | 54 | o.aload_0(); 55 | o.iload_1(); 56 | this.libraryManager.getMethod(MethodEnum.CHECK_1D_STRING_ARRAY_ACCESS).emitCall(o); 57 | 58 | o.aload_0(); 59 | o.iconst_0(); 60 | o.aaload(); 61 | o.iload_1(); 62 | o.aaload(); 63 | o.areturn(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/operators/Method_Not.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.operators; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 33 | 34 | public class Method_Not extends Method { 35 | private static final String METHOD_NAME = "Not"; 36 | private static final String DESCRIPTOR = "(F)F"; 37 | private static final int NUM_LOCALS = 1; 38 | 39 | public Method_Not(LibraryManager libraryManager) { 40 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 41 | } 42 | 43 | @Override 44 | public void addMethodBytecode(ByteOutStream o, List e) { 45 | 46 | // local 0: F=>I argument 47 | 48 | o.fload_0(); 49 | o.f2i(); 50 | o.istore_0(); 51 | 52 | o.iload_0(); 53 | o.iconst(-32768); 54 | o.if_icmpge("skipArg1Underflow"); 55 | 56 | emitThrowRuntimeException(o, "NOT operator: Argument < -32768."); 57 | 58 | o.label("skipArg1Underflow"); 59 | o.iload_0(); 60 | o.iconst(32767); 61 | o.if_icmple("skipArg1Overflow"); 62 | 63 | emitThrowRuntimeException(o, "NOT operator: Argument > 32767."); 64 | 65 | o.label("skipArg1Overflow"); 66 | 67 | o.iload_0(); 68 | o.iconst_1(); 69 | o.iadd(); 70 | o.ineg(); 71 | o.i2f(); 72 | o.freturn(); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/helper/Method_PrintCharsFromStack.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.helper; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager.MethodEnum; 33 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 34 | 35 | public class Method_PrintCharsFromStack extends Method { 36 | private static final String METHOD_NAME = "PrintCharsFromStack"; 37 | private static final String DESCRIPTOR = "([C)V"; 38 | private static final int NUM_LOCALS = 2; 39 | 40 | public Method_PrintCharsFromStack(LibraryManager libraryManager) { 41 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 42 | } 43 | 44 | @Override 45 | public void addMethodBytecode(ByteOutStream o, List e) { 46 | 47 | // local 0: [C characters to print 48 | // local 1: I loop index 49 | 50 | o.iconst_0(); 51 | o.istore_1(); 52 | o.goto_("loopCond"); 53 | 54 | o.label("loop"); 55 | o.aload_0(); 56 | o.iload_1(); 57 | o.caload(); 58 | this.libraryManager.getMethod(MethodEnum.PRINT_CHAR_FROM_STACK).emitCall(o); 59 | o.iinc(1, 1); 60 | 61 | o.label("loopCond"); 62 | o.iload_1(); 63 | o.aload_0(); 64 | o.arraylength(); 65 | o.if_icmplt("loop"); 66 | 67 | o.return_(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/arrays/Method_StoreFloatIn1DArray.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.arrays; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager.MethodEnum; 33 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 34 | 35 | public class Method_StoreFloatIn1DArray extends Method { 36 | private static final String METHOD_NAME = "StoreFloatIn1DArray"; 37 | private static final String DESCRIPTOR = "(F[[FF)V"; 38 | private static final int NUM_LOCALS = 3; 39 | 40 | public Method_StoreFloatIn1DArray(LibraryManager libraryManager) { 41 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 42 | } 43 | 44 | @Override 45 | public void addMethodBytecode(ByteOutStream o, List e) { 46 | 47 | // local 0: F value 48 | // local 1: [[F reference to array reference 49 | // local 2: F=>I array index 50 | 51 | o.fload_2(); 52 | this.libraryManager.getMethod(MethodEnum.ROUND_TO_INT).emitCall(o); 53 | o.istore_2(); 54 | 55 | o.aload_1(); 56 | o.iload_2(); 57 | this.libraryManager.getMethod(MethodEnum.CHECK_1D_FLOAT_ARRAY_ACCESS).emitCall(o); 58 | 59 | o.aload_1(); 60 | o.iconst_0(); 61 | o.aaload(); 62 | o.iload_2(); 63 | o.fload_0(); 64 | o.fastore(); 65 | o.return_(); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/arrays/Method_StoreStringIn1DArray.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.arrays; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager.MethodEnum; 33 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 34 | 35 | public class Method_StoreStringIn1DArray extends Method { 36 | private static final String METHOD_NAME = "StoreStringIn1DArray"; 37 | private static final String DESCRIPTOR = "([C[[[CF)V"; 38 | private static final int NUM_LOCALS = 3; 39 | 40 | public Method_StoreStringIn1DArray(LibraryManager libraryManager) { 41 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 42 | } 43 | 44 | @Override 45 | public void addMethodBytecode(ByteOutStream o, List e) { 46 | 47 | // local 0: [C value 48 | // local 1: [[[C reference to array reference 49 | // local 2: F=>I array index 50 | 51 | o.fload_2(); 52 | this.libraryManager.getMethod(MethodEnum.ROUND_TO_INT).emitCall(o); 53 | o.istore_2(); 54 | 55 | o.aload_1(); 56 | o.iload_2(); 57 | this.libraryManager.getMethod(MethodEnum.CHECK_1D_STRING_ARRAY_ACCESS).emitCall(o); 58 | 59 | o.aload_1(); 60 | o.iconst_0(); 61 | o.aaload(); 62 | o.iload_2(); 63 | o.aload_0(); 64 | o.aastore(); 65 | o.return_(); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/classfile/constantpoolinfo/impl/ConstantPoolInfo_Class.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.classfile.constantpoolinfo.impl; 26 | 27 | import de.lorenzwiest.basiccompiler.classfile.ConstantPool; 28 | import de.lorenzwiest.basiccompiler.classfile.constantpoolinfo.ConstantPoolInfo; 29 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 30 | 31 | public class ConstantPoolInfo_Class extends ConstantPoolInfo { 32 | private final int nameIndex; // u2 33 | 34 | public ConstantPoolInfo_Class(int nameIndex) { 35 | super(TAG_CLASS); 36 | this.nameIndex = nameIndex; 37 | } 38 | 39 | public int getNameIndex() { 40 | return this.nameIndex; 41 | } 42 | 43 | @Override 44 | public void write(ByteOutStream o) { 45 | super.write(o); 46 | o.write_u2(this.nameIndex); 47 | } 48 | 49 | public static int getIndex(ConstantPool constantPool, String className) { 50 | String key = getKey(className); 51 | return constantPool.getIndex(key); 52 | } 53 | 54 | private static String getKey(String className) { 55 | return "CLASS_" + className; 56 | } 57 | 58 | public static int addAndGetIndex(ConstantPool constantPool, String className) { 59 | String key = getKey(className); 60 | if (constantPool.contains(key) == false) { 61 | constantPool.put(key, createInfo(constantPool, className)); 62 | } 63 | return constantPool.getIndex(key); 64 | } 65 | 66 | private static ConstantPoolInfo_Class createInfo(ConstantPool constantPool, String className) { 67 | int nameIndex = ConstantPoolInfo_Utf8.addAndGetIndex(constantPool, className); 68 | return new ConstantPoolInfo_Class(nameIndex); 69 | } 70 | } -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/helper/Method_Substring.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.helper; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 33 | 34 | public class Method_Substring extends Method { 35 | private static final String METHOD_NAME = "Substring"; 36 | private static final String DESCRIPTOR = "([CII)[C"; 37 | private static final int NUM_LOCALS = 5; 38 | 39 | public Method_Substring(LibraryManager libraryManager) { 40 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 41 | } 42 | 43 | @Override 44 | public void addMethodBytecode(ByteOutStream o, List e) { 45 | 46 | // local 0: [C source char[] 47 | // local 1: I start index, inclusive 48 | // local 2: I end index, exclusive 49 | // local 3: I temp length substring 50 | // local 4: [C substring char[] 51 | 52 | o.iload_2(); 53 | o.iload_1(); 54 | o.isub(); 55 | o.istore_3(); 56 | 57 | o.iload_3(); 58 | o.newarray_char(); 59 | o.astore(4); 60 | o.goto_("loopCond"); 61 | 62 | o.label("loop"); 63 | o.iinc(3, -1); 64 | o.iinc(2, -1); 65 | 66 | o.aload(4); 67 | o.iload_3(); 68 | 69 | o.aload_0(); 70 | o.iload_2(); 71 | o.caload(); 72 | o.castore(); 73 | 74 | o.label("loopCond"); 75 | o.iload_3(); 76 | o.ifgt("loop"); 77 | 78 | o.aload(4); 79 | o.areturn(); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/functions/Method_Exp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.functions; 26 | 27 | import static de.lorenzwiest.basiccompiler.classfile.ClassModel.JavaMethod.MATH_EXP; 28 | 29 | import java.util.List; 30 | 31 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 32 | import de.lorenzwiest.basiccompiler.compiler.Compiler; 33 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 34 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 35 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 36 | 37 | public class Method_Exp extends Method { 38 | private static final String METHOD_NAME = "Exp"; 39 | private static final String DESCRIPTOR = "(F)F"; 40 | private static final int NUM_LOCALS = 1; 41 | 42 | public Method_Exp(LibraryManager libraryManager) { 43 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 44 | } 45 | 46 | @Override 47 | public void addMethodBytecode(ByteOutStream o, List e) { 48 | 49 | // local 0: F argument 50 | 51 | o.fload_0(); 52 | o.ldc(this.classModel.getFloatIndex(87.3365f)); 53 | o.fcmpg(); 54 | o.ifgt("isOverflow"); 55 | 56 | // TODO: handle overflow of very large negative arguments 57 | 58 | o.fload_0(); 59 | o.f2d(); 60 | o.invokestatic(this.classModel.getJavaMethodRefIndex(MATH_EXP)); 61 | o.d2f(); 62 | o.freturn(); 63 | 64 | o.label("isOverflow"); 65 | 66 | emitPrint(o, "Overflow" + Compiler.CR); 67 | 68 | o.fconst_1(); // create POSITIVE_INFINITY value 69 | o.fconst_0(); 70 | o.fdiv(); 71 | o.freturn(); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/classfile/ConstantPool.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.classfile; 26 | 27 | import java.util.ArrayList; 28 | import java.util.HashMap; 29 | import java.util.List; 30 | import java.util.Map; 31 | 32 | import de.lorenzwiest.basiccompiler.classfile.constantpoolinfo.ConstantPoolInfo; 33 | 34 | public class ConstantPool { 35 | private List constantPoolInfos = new ArrayList(); 36 | private Map mapOfKeys = new HashMap(); 37 | 38 | public ConstantPool() { 39 | this.constantPoolInfos.add(null); // add first constant pool entry, a dummy entry 40 | } 41 | 42 | public void put(String key, ConstantPoolInfo constantPoolInfo) { 43 | int nextIndex = this.constantPoolInfos.size(); 44 | 45 | this.constantPoolInfos.add(constantPoolInfo); 46 | this.mapOfKeys.put(key, nextIndex); 47 | } 48 | 49 | public ConstantPoolInfo get(int constantPoolIndex) { 50 | return this.constantPoolInfos.get(constantPoolIndex); 51 | } 52 | 53 | public boolean contains(String key) { 54 | return this.mapOfKeys.containsKey(key); 55 | } 56 | 57 | public int getIndex(String key) { 58 | Integer index = this.mapOfKeys.get(key); 59 | if (index != null) { 60 | return index.intValue(); 61 | } 62 | return -1; 63 | } 64 | 65 | public int getCount() { 66 | return this.constantPoolInfos.size(); 67 | } 68 | 69 | public ConstantPoolInfo[] getConstantPoolInfos() { 70 | List allButFirstConstantPoolInfo = this.constantPoolInfos.subList(1, this.constantPoolInfos.size()); 71 | return allButFirstConstantPoolInfo.toArray(new ConstantPoolInfo[0]); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/functions/Method_Chr.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.functions; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager.MethodEnum; 33 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 34 | 35 | public class Method_Chr extends Method { 36 | private static final String METHOD_NAME = "Chr"; 37 | private static final String DESCRIPTOR = "(F)[C"; 38 | private static final int NUM_LOCALS = 1; 39 | 40 | public Method_Chr(LibraryManager libraryManager) { 41 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 42 | } 43 | 44 | @Override 45 | public void addMethodBytecode(ByteOutStream o, List e) { 46 | 47 | // local 0: F=>I argument 48 | 49 | o.fload_0(); 50 | this.libraryManager.getMethod(MethodEnum.ROUND_TO_INT).emitCall(o); 51 | o.istore_0(); 52 | 53 | o.iload_0(); 54 | o.ifge("skipArgUnderflow"); 55 | 56 | emitThrowRuntimeException(o, "CHR$(): Argument < 0."); 57 | 58 | o.label("skipArgUnderflow"); 59 | o.iload_0(); 60 | o.iconst(0x7F); 61 | o.if_icmple("skipArgOverflow"); 62 | 63 | emitThrowRuntimeException(o, "CHR$(): Argument > 127."); 64 | 65 | o.label("skipArgOverflow"); 66 | 67 | o.iconst_1(); 68 | o.newarray_char(); 69 | o.dup(); 70 | o.iconst_0(); 71 | o.iload_0(); 72 | o.castore(); 73 | o.areturn(); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/functions/Method_Tan.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.functions; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager.MethodEnum; 33 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 34 | 35 | public class Method_Tan extends Method { 36 | private static final String METHOD_NAME = "Tan"; 37 | private static final String DESCRIPTOR = "(F)F"; 38 | private static final int NUM_LOCALS = 2; 39 | 40 | public Method_Tan(LibraryManager libraryManager) { 41 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 42 | } 43 | 44 | @Override 45 | public void addMethodBytecode(ByteOutStream o, List e) { 46 | 47 | // local 0: F argument, sin(argument) 48 | // local 1: F cos(argument) 49 | 50 | o.fload_0(); 51 | this.libraryManager.getMethod(MethodEnum.COS).emitCall(o); 52 | o.fstore_1(); 53 | 54 | o.fload_0(); 55 | this.libraryManager.getMethod(MethodEnum.SIN).emitCall(o); 56 | o.fstore_0(); 57 | 58 | o.fload_1(); 59 | o.fconst_0(); 60 | o.fcmpg(); 61 | o.ifeq("divisionByZero"); 62 | 63 | o.fload_0(); // POSITIVE_INFINITY or NEGATIVE_INFINITY 64 | o.fload_1(); 65 | o.fdiv(); 66 | o.freturn(); 67 | 68 | o.label("divisionByZero"); 69 | o.fload_0(); 70 | this.libraryManager.getMethod(MethodEnum.DIVISION_BY_ZERO).emitCall(o); 71 | o.freturn(); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/functions/Method_Fix.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.functions; 26 | 27 | import static de.lorenzwiest.basiccompiler.classfile.ClassModel.JavaMethod.MATH_CEIL; 28 | import static de.lorenzwiest.basiccompiler.classfile.ClassModel.JavaMethod.MATH_FLOOR; 29 | 30 | import java.util.List; 31 | 32 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 33 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 34 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 35 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 36 | 37 | public class Method_Fix extends Method { 38 | private static final String METHOD_NAME = "Fix"; 39 | private static final String DESCRIPTOR = "(F)F"; 40 | private static final int NUM_LOCALS = 1; 41 | 42 | public Method_Fix(LibraryManager libraryManager) { 43 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 44 | } 45 | 46 | @Override 47 | public void addMethodBytecode(ByteOutStream o, List e) { 48 | 49 | // local 0: F argument 50 | 51 | o.fload_0(); 52 | o.f2d(); 53 | 54 | o.fload_0(); 55 | o.fconst_0(); 56 | o.fcmpg(); 57 | o.ifge("floor"); 58 | 59 | o.invokestatic(this.classModel.getJavaMethodRefIndex(MATH_CEIL)); 60 | o.goto_("skip"); 61 | 62 | o.label("floor"); 63 | o.invokestatic(this.classModel.getJavaMethodRefIndex(MATH_FLOOR)); 64 | 65 | o.label("skip"); 66 | o.d2f(); 67 | 68 | o.dup(); 69 | o.fconst_0(); 70 | o.fcmpg(); 71 | o.ifne("exit"); 72 | 73 | o.pop(); 74 | o.fconst_0(); 75 | 76 | o.label("exit"); 77 | o.freturn(); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/helper/Method_GosubStackPop.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.helper; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.Compiler; 31 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 32 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 33 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 34 | 35 | public class Method_GosubStackPop extends Method { 36 | private static final String METHOD_NAME = "GosubStackPop"; 37 | private static final String DESCRIPTOR = "()I"; 38 | private static final int NUM_LOCALS = 0; 39 | 40 | public Method_GosubStackPop(LibraryManager libraryManager) { 41 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 42 | } 43 | 44 | @Override 45 | public void addMethodBytecode(ByteOutStream o, List e) { 46 | int gosubStackFieldRef = this.classModel.getFieldRefIndex(Compiler.FIELD_GOSUB_STACK, "[I"); 47 | int gosubStackIndexFieldRef = this.classModel.getFieldRefIndex(Compiler.FIELD_GOSUB_STACK_INDEX, "I"); 48 | 49 | o.getstatic(gosubStackIndexFieldRef); 50 | o.ifge("noStackUnderflow"); 51 | 52 | emitThrowRuntimeException(o, "RETURN without GOSUB."); 53 | 54 | o.label("noStackUnderflow"); 55 | o.getstatic(gosubStackIndexFieldRef); 56 | o.iconst(Compiler.GOSUB_STACK_FRAME_SIZE); 57 | o.isub(); 58 | o.putstatic(gosubStackIndexFieldRef); 59 | 60 | o.getstatic(gosubStackFieldRef); 61 | o.getstatic(gosubStackIndexFieldRef); 62 | o.iaload(); 63 | o.ireturn(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/operators/Method_StringEqual.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.operators; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 33 | 34 | public class Method_StringEqual extends Method { 35 | private static final String METHOD_NAME = "StringEqual"; 36 | private static final String DESCRIPTOR = "([C[C)F"; 37 | private static final int NUM_LOCALS = 3; 38 | 39 | public Method_StringEqual(LibraryManager libraryManager) { 40 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 41 | } 42 | 43 | @Override 44 | public void addMethodBytecode(ByteOutStream o, List e) { 45 | 46 | // local 0: [C char array1 reference 47 | // local 1: [C char array2 reference 48 | // local 2: I loop counter 49 | 50 | o.aload_0(); 51 | o.arraylength(); 52 | o.aload_1(); 53 | o.arraylength(); 54 | o.if_icmpne("false"); 55 | 56 | o.aload_0(); 57 | o.arraylength(); 58 | o.istore_2(); 59 | o.goto_("loopCond"); 60 | 61 | o.label("loop"); 62 | o.iinc(2, -1); 63 | 64 | o.aload_0(); 65 | o.iload_2(); 66 | o.caload(); 67 | o.aload_1(); 68 | o.iload_2(); 69 | o.caload(); 70 | o.if_icmpne("false"); 71 | 72 | o.label("loopCond"); 73 | o.iload_2(); 74 | o.ifgt("loop"); 75 | 76 | o.fconst_1(); 77 | o.fneg(); 78 | o.freturn(); 79 | 80 | o.label("false"); 81 | o.fconst_0(); 82 | o.freturn(); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/operators/Method_StringNotEqual.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.operators; 26 | 27 | import java.util.List; 28 | 29 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 30 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 31 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 32 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 33 | 34 | public class Method_StringNotEqual extends Method { 35 | private static final String METHOD_NAME = "StringNotEqual"; 36 | private static final String DESCRIPTOR = "([C[C)F"; 37 | private static final int NUM_LOCALS = 3; 38 | 39 | public Method_StringNotEqual(LibraryManager libraryManager) { 40 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 41 | } 42 | 43 | @Override 44 | public void addMethodBytecode(ByteOutStream o, List e) { 45 | 46 | // local 0: [C char array1 reference 47 | // local 1: [C char array2 reference 48 | // local 2: I loop counter 49 | 50 | o.aload_0(); 51 | o.arraylength(); 52 | o.aload_1(); 53 | o.arraylength(); 54 | o.if_icmpne("true"); 55 | 56 | o.aload_0(); 57 | o.arraylength(); 58 | o.istore_2(); 59 | o.goto_("loopCond"); 60 | 61 | o.label("loop"); 62 | o.iinc(2, -1); 63 | 64 | o.aload_0(); 65 | o.iload_2(); 66 | o.caload(); 67 | o.aload_1(); 68 | o.iload_2(); 69 | o.caload(); 70 | o.if_icmpne("true"); 71 | 72 | o.label("loopCond"); 73 | o.iload_2(); 74 | o.ifgt("loop"); 75 | 76 | o.fconst_0(); 77 | o.freturn(); 78 | 79 | o.label("true"); 80 | o.fconst_1(); 81 | o.fneg(); 82 | o.freturn(); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/compiler/library/methods/operators/Method_Power.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.compiler.library.methods.operators; 26 | 27 | import static de.lorenzwiest.basiccompiler.classfile.ClassModel.JavaMethod.MATH_POW; 28 | 29 | import java.util.List; 30 | 31 | import de.lorenzwiest.basiccompiler.classfile.info.ExceptionTableInfo; 32 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 33 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager; 34 | import de.lorenzwiest.basiccompiler.compiler.library.LibraryManager.MethodEnum; 35 | import de.lorenzwiest.basiccompiler.compiler.library.methods.Method; 36 | 37 | public class Method_Power extends Method { 38 | private static final String METHOD_NAME = "Power"; 39 | private static final String DESCRIPTOR = "(FF)F"; 40 | private static final int NUM_LOCALS = 2; 41 | 42 | public Method_Power(LibraryManager libraryManager) { 43 | super(libraryManager, METHOD_NAME, DESCRIPTOR, NUM_LOCALS); 44 | } 45 | 46 | @Override 47 | public void addMethodBytecode(ByteOutStream o, List e) { 48 | 49 | // local 0: F base 50 | // local 1: F exponent 51 | 52 | o.fload_0(); 53 | o.fconst_0(); 54 | o.fcmpg(); 55 | o.ifne("baseNotZero"); 56 | o.fload_1(); 57 | o.fconst_0(); 58 | o.fcmpg(); 59 | o.iflt("divisionByZero"); 60 | 61 | o.label("baseNotZero"); 62 | o.fload_0(); 63 | o.f2d(); 64 | o.fload_1(); 65 | o.f2d(); 66 | o.invokestatic(this.classModel.getJavaMethodRefIndex(MATH_POW)); 67 | o.d2f(); 68 | o.freturn(); 69 | 70 | o.label("divisionByZero"); 71 | o.fconst_1(); // always POSITIVE_INFINITY 72 | this.libraryManager.getMethod(MethodEnum.DIVISION_BY_ZERO).emitCall(o); 73 | o.freturn(); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/de/lorenzwiest/basiccompiler/classfile/info/FieldInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2016 Lorenz Wiest 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a 7 | * copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | * DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | package de.lorenzwiest.basiccompiler.classfile.info; 26 | 27 | import de.lorenzwiest.basiccompiler.compiler.etc.ByteOutStream; 28 | 29 | public class FieldInfo { 30 | // u2 access_flags; 31 | // u2 name_index; 32 | // u2 descriptor_index; 33 | // u2 attributes_count; 34 | // attribute_info attributes[attributes_count]; 35 | 36 | private final int accessFlags; // u2 37 | private final int nameIndex; // u2 38 | private final int descriptorIndex; // u2 39 | // private int attributeCount; // u2 40 | private final Object[] attributes; // attribute_info[] 41 | 42 | public FieldInfo(int nameIndex, int descriptorIndex, int accessFlags) { 43 | this.accessFlags = accessFlags; 44 | this.nameIndex = nameIndex; 45 | this.descriptorIndex = descriptorIndex; 46 | // this.attributeCount calculated implicitly in write() 47 | this.attributes = new Object[0]; 48 | } 49 | 50 | public void write(ByteOutStream o) { 51 | o.write_u2(this.accessFlags); 52 | o.write_u2(this.nameIndex); 53 | o.write_u2(this.descriptorIndex); 54 | o.write_u2(this.attributes.length); 55 | for (int i = 0; i < this.attributes.length; i++) { 56 | // not implemented, not needed yet 57 | } 58 | } 59 | 60 | @Override 61 | public boolean equals(Object obj) { 62 | if (obj instanceof FieldInfo) { 63 | FieldInfo toCompare = (FieldInfo) obj; 64 | if ((this.nameIndex == toCompare.nameIndex) && (this.descriptorIndex == toCompare.descriptorIndex)) { 65 | return true; 66 | } 67 | } 68 | return false; 69 | } 70 | 71 | @Override 72 | public int hashCode() { 73 | int hashCode = 17; 74 | hashCode += 37 * this.nameIndex; 75 | hashCode += 37 * this.descriptorIndex; 76 | return hashCode; 77 | } 78 | } --------------------------------------------------------------------------------