├── runtime ├── .gitignore ├── src │ ├── test │ │ ├── resources │ │ │ ├── jmpToIllegalLine.vch │ │ │ ├── wordOutput.vch │ │ │ ├── wordOutputWithSpaces.vch │ │ │ ├── eqlWithIssos.vch │ │ │ ├── eqlWithZals.vch │ │ │ ├── gtrWhenTrue.vch │ │ │ ├── jmpToLegalLine.vch │ │ │ ├── lesWhenFalse.vch │ │ │ ├── lesWhenTrue.vch │ │ │ ├── addWithNegatives.vch │ │ │ ├── addWithPositives.vch │ │ │ ├── divWithPositives.vch │ │ │ ├── eqlWithWords.vch │ │ │ ├── gtrWhenFalse.vch │ │ │ ├── modWithRemainder.vch │ │ │ ├── mulWithNegatives.vch │ │ │ ├── mulWithPositives.vch │ │ │ ├── subWithNegatives.vch │ │ │ ├── subWithPositives.vch │ │ │ ├── divWithNegatives.vch │ │ │ ├── eqlWithDifferentTypes.vch │ │ │ ├── modWithoutRemainder.vch │ │ │ ├── addWithNegativeAndPositive.vch │ │ │ ├── divWithNegativeAndPositive.vch │ │ │ ├── mulWithNegativeAndPositive.vch │ │ │ └── subWithNegativeAndPositive.vch │ │ └── java │ │ │ └── vong │ │ │ └── piler │ │ │ └── her │ │ │ └── vongvirtualmachine │ │ │ └── OpCodeTest.java │ └── main │ │ ├── java │ │ └── vong │ │ │ └── piler │ │ │ └── her │ │ │ └── vongruntime │ │ │ ├── exception │ │ │ ├── EmptyLineException.java │ │ │ ├── ReadEmptyRegisterException.java │ │ │ ├── UnsupportedNumberofArgumentsException.java │ │ │ ├── InstructionPointerOutOfBoundsException.java │ │ │ └── UnknownCommandException.java │ │ │ ├── util │ │ │ └── StringUtils.java │ │ │ ├── virtualmachine │ │ │ ├── model │ │ │ │ ├── Command.java │ │ │ │ ├── OperationEnum.java │ │ │ │ └── StackElement.java │ │ │ └── Steakmachine.java │ │ │ └── main │ │ │ └── VongRuntime.java │ │ └── resources │ │ └── Fibonacci.vch ├── .settings │ ├── org.eclipse.m2e.core.prefs │ ├── org.eclipse.core.resources.prefs │ └── org.eclipse.jdt.core.prefs ├── .project ├── .classpath └── pom.xml ├── .gitignore ├── compiler ├── src │ ├── .gitignore │ └── main │ │ ├── .gitignore │ │ ├── java │ │ ├── .gitignore │ │ └── vong │ │ │ └── piler │ │ │ └── her │ │ │ ├── .gitignore │ │ │ ├── generator │ │ │ ├── .gitignore │ │ │ ├── PreDefinedFunction.java │ │ │ ├── RegisterHandler.java │ │ │ ├── ValueModel.java │ │ │ ├── ByteCodeWriter.java │ │ │ └── Generator.java │ │ │ ├── enums │ │ │ ├── DataTypeEnum.java │ │ │ ├── OperationEnum.java │ │ │ └── TokenTypeEnum.java │ │ │ ├── Constants.java │ │ │ ├── parser │ │ │ ├── TreeNode.java │ │ │ └── Parser.java │ │ │ ├── lexer │ │ │ ├── Token.java │ │ │ └── Lexer.java │ │ │ ├── exceptions │ │ │ └── GenerationsFails.java │ │ │ └── Main.java │ │ ├── test │ │ ├── vong │ │ │ └── piler │ │ │ │ └── her │ │ │ │ └── generator │ │ │ │ ├── correct │ │ │ │ └── files │ │ │ │ │ ├── add.vch │ │ │ │ │ ├── div.vch │ │ │ │ │ ├── mod.vch │ │ │ │ │ ├── mul.vch │ │ │ │ │ ├── sub.vch │ │ │ │ │ ├── les.vch │ │ │ │ │ ├── eqz.vch │ │ │ │ │ ├── gtr.vch │ │ │ │ │ └── nqz.vch │ │ │ │ ├── ComparatorZalGenarationTest.java │ │ │ │ └── CalculationGenarationTest.java │ │ ├── ibims1code.vch │ │ ├── ibims1code.vsh │ │ ├── fibonacci.vch │ │ └── fibonacci.vsh │ │ └── resources │ │ └── log4j2.xml ├── gen │ ├── generatorTester │ │ ├── add.vch │ │ ├── div.vch │ │ ├── mod.vch │ │ ├── mul.vch │ │ ├── sub.vch │ │ ├── eqz.vch │ │ ├── gtr.vch │ │ ├── les.vch │ │ └── nqz.vch │ ├── ibims1code.vch │ └── fibonacci.vch ├── .project ├── .gitignore ├── .classpath └── pom.xml ├── .travis.yml ├── .settings └── org.eclipse.m2e.core.prefs ├── .project ├── ExamplePrograms ├── lauchTester.vsh └── fibonacci.vsh ├── pom.xml ├── LICENSE.md └── README.md /runtime/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | */.DS_Store 2 | */gen/* 3 | -------------------------------------------------------------------------------- /compiler/src/.gitignore: -------------------------------------------------------------------------------- 1 | /.DS_Store 2 | -------------------------------------------------------------------------------- /compiler/src/main/.gitignore: -------------------------------------------------------------------------------- 1 | /.DS_Store 2 | -------------------------------------------------------------------------------- /compiler/src/main/java/.gitignore: -------------------------------------------------------------------------------- 1 | /.DS_Store 2 | -------------------------------------------------------------------------------- /compiler/src/main/java/vong/piler/her/.gitignore: -------------------------------------------------------------------------------- 1 | /.DS_Store 2 | -------------------------------------------------------------------------------- /runtime/src/test/resources/jmpToIllegalLine.vch: -------------------------------------------------------------------------------- 1 | PSA 5 2 | JMP 3 | END -------------------------------------------------------------------------------- /runtime/src/test/resources/wordOutput.vch: -------------------------------------------------------------------------------- 1 | PSW swag 2 | PRT 3 | END -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | sudo: false 3 | script: mvn clean verify -------------------------------------------------------------------------------- /compiler/src/main/java/vong/piler/her/generator/.gitignore: -------------------------------------------------------------------------------- 1 | /.DS_Store 2 | -------------------------------------------------------------------------------- /runtime/src/test/resources/wordOutputWithSpaces.vch: -------------------------------------------------------------------------------- 1 | PSW s w a g 2 | PRT 3 | END -------------------------------------------------------------------------------- /runtime/src/test/resources/eqlWithIssos.vch: -------------------------------------------------------------------------------- 1 | PSI 1 2 | PSI 1 3 | EQL 4 | PRT 5 | END -------------------------------------------------------------------------------- /runtime/src/test/resources/eqlWithZals.vch: -------------------------------------------------------------------------------- 1 | PSZ 8 2 | PSZ 8 3 | EQL 4 | PRT 5 | END -------------------------------------------------------------------------------- /runtime/src/test/resources/gtrWhenTrue.vch: -------------------------------------------------------------------------------- 1 | PSZ 10 2 | PSZ 9 3 | GTR 4 | PRT 5 | END -------------------------------------------------------------------------------- /runtime/src/test/resources/jmpToLegalLine.vch: -------------------------------------------------------------------------------- 1 | PSA 3 2 | JMP 3 | END 4 | AAL 5 | END -------------------------------------------------------------------------------- /runtime/src/test/resources/lesWhenFalse.vch: -------------------------------------------------------------------------------- 1 | PSZ 10 2 | PSZ 5 3 | LES 4 | PRT 5 | END -------------------------------------------------------------------------------- /runtime/src/test/resources/lesWhenTrue.vch: -------------------------------------------------------------------------------- 1 | PSZ 7 2 | PSZ 9 3 | LES 4 | PRT 5 | END -------------------------------------------------------------------------------- /runtime/src/test/resources/addWithNegatives.vch: -------------------------------------------------------------------------------- 1 | PSZ -7 2 | PSZ -9 3 | ADD 4 | PRT 5 | END -------------------------------------------------------------------------------- /runtime/src/test/resources/addWithPositives.vch: -------------------------------------------------------------------------------- 1 | PSZ 7 2 | PSZ 9 3 | ADD 4 | PRT 5 | END -------------------------------------------------------------------------------- /runtime/src/test/resources/divWithPositives.vch: -------------------------------------------------------------------------------- 1 | PSZ 10 2 | PSZ 5 3 | DIV 4 | PRT 5 | END -------------------------------------------------------------------------------- /runtime/src/test/resources/eqlWithWords.vch: -------------------------------------------------------------------------------- 1 | PSW swag 2 | PSW swag 3 | EQL 4 | PRT 5 | END -------------------------------------------------------------------------------- /runtime/src/test/resources/gtrWhenFalse.vch: -------------------------------------------------------------------------------- 1 | PSZ 10 2 | PSZ 11 3 | GTR 4 | PRT 5 | END -------------------------------------------------------------------------------- /runtime/src/test/resources/modWithRemainder.vch: -------------------------------------------------------------------------------- 1 | PSZ 10 2 | PSZ 9 3 | MOD 4 | PRT 5 | END -------------------------------------------------------------------------------- /runtime/src/test/resources/mulWithNegatives.vch: -------------------------------------------------------------------------------- 1 | PSZ -5 2 | PSZ -8 3 | MUL 4 | PRT 5 | END -------------------------------------------------------------------------------- /runtime/src/test/resources/mulWithPositives.vch: -------------------------------------------------------------------------------- 1 | PSZ 5 2 | PSZ 7 3 | MUL 4 | PRT 5 | END -------------------------------------------------------------------------------- /runtime/src/test/resources/subWithNegatives.vch: -------------------------------------------------------------------------------- 1 | PSZ -7 2 | PSZ -9 3 | SUB 4 | PRT 5 | END -------------------------------------------------------------------------------- /runtime/src/test/resources/subWithPositives.vch: -------------------------------------------------------------------------------- 1 | PSZ 17 2 | PSZ 9 3 | SUB 4 | PRT 5 | END -------------------------------------------------------------------------------- /runtime/src/test/resources/divWithNegatives.vch: -------------------------------------------------------------------------------- 1 | PSZ -10 2 | PSZ -5 3 | DIV 4 | PRT 5 | END -------------------------------------------------------------------------------- /runtime/src/test/resources/eqlWithDifferentTypes.vch: -------------------------------------------------------------------------------- 1 | PSZ 5 2 | PSI 1 3 | EQL 4 | PRT 5 | END -------------------------------------------------------------------------------- /runtime/src/test/resources/modWithoutRemainder.vch: -------------------------------------------------------------------------------- 1 | PSZ 10 2 | PSZ 2 3 | MOD 4 | PRT 5 | END -------------------------------------------------------------------------------- /runtime/src/test/resources/addWithNegativeAndPositive.vch: -------------------------------------------------------------------------------- 1 | PSZ 7 2 | PSZ -9 3 | ADD 4 | PRT 5 | END -------------------------------------------------------------------------------- /runtime/src/test/resources/divWithNegativeAndPositive.vch: -------------------------------------------------------------------------------- 1 | PSZ -10 2 | PSZ 5 3 | DIV 4 | PRT 5 | END -------------------------------------------------------------------------------- /runtime/src/test/resources/mulWithNegativeAndPositive.vch: -------------------------------------------------------------------------------- 1 | PSZ -7 2 | PSZ 5 3 | MUL 4 | PRT 5 | END -------------------------------------------------------------------------------- /runtime/src/test/resources/subWithNegativeAndPositive.vch: -------------------------------------------------------------------------------- 1 | PSZ -7 2 | PSZ 9 3 | SUB 4 | PRT 5 | END -------------------------------------------------------------------------------- /compiler/gen/generatorTester/add.vch: -------------------------------------------------------------------------------- 1 | 1 1000.0 2 | 1 100.0 3 | 4 4 | 1 10.0 5 | 4 6 | 1 1.0 7 | 4 8 | 19 9 | 27 10 | -------------------------------------------------------------------------------- /compiler/gen/generatorTester/div.vch: -------------------------------------------------------------------------------- 1 | 1 1000.0 2 | 1 100.0 3 | 7 4 | 1 10.0 5 | 7 6 | 1 1.0 7 | 7 8 | 19 9 | 27 10 | -------------------------------------------------------------------------------- /compiler/gen/generatorTester/mod.vch: -------------------------------------------------------------------------------- 1 | 1 1000.0 2 | 1 100.0 3 | 8 4 | 1 10.0 5 | 8 6 | 1 1.0 7 | 8 8 | 19 9 | 27 10 | -------------------------------------------------------------------------------- /compiler/gen/generatorTester/mul.vch: -------------------------------------------------------------------------------- 1 | 1 1000.0 2 | 1 100.0 3 | 6 4 | 1 10.0 5 | 6 6 | 1 1.0 7 | 6 8 | 19 9 | 27 10 | -------------------------------------------------------------------------------- /compiler/gen/generatorTester/sub.vch: -------------------------------------------------------------------------------- 1 | 1 1000.0 2 | 1 100.0 3 | 5 4 | 1 10.0 5 | 5 6 | 1 1.0 7 | 5 8 | 19 9 | 27 10 | -------------------------------------------------------------------------------- /.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /runtime/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /compiler/src/main/test/vong/piler/her/generator/correct/files/add.vch: -------------------------------------------------------------------------------- 1 | 1 1000.0 2 | 1 100.0 3 | 4 4 | 1 10.0 5 | 4 6 | 1 1.0 7 | 4 8 | 19 9 | 27 10 | -------------------------------------------------------------------------------- /compiler/src/main/test/vong/piler/her/generator/correct/files/div.vch: -------------------------------------------------------------------------------- 1 | 1 1000.0 2 | 1 100.0 3 | 7 4 | 1 10.0 5 | 7 6 | 1 1.0 7 | 7 8 | 19 9 | 27 10 | -------------------------------------------------------------------------------- /compiler/src/main/test/vong/piler/her/generator/correct/files/mod.vch: -------------------------------------------------------------------------------- 1 | 1 1000.0 2 | 1 100.0 3 | 8 4 | 1 10.0 5 | 8 6 | 1 1.0 7 | 8 8 | 19 9 | 27 10 | -------------------------------------------------------------------------------- /compiler/src/main/test/vong/piler/her/generator/correct/files/mul.vch: -------------------------------------------------------------------------------- 1 | 1 1000.0 2 | 1 100.0 3 | 6 4 | 1 10.0 5 | 6 6 | 1 1.0 7 | 6 8 | 19 9 | 27 10 | -------------------------------------------------------------------------------- /compiler/src/main/test/vong/piler/her/generator/correct/files/sub.vch: -------------------------------------------------------------------------------- 1 | 1 1000.0 2 | 1 100.0 3 | 5 4 | 1 10.0 5 | 5 6 | 1 1.0 7 | 5 8 | 19 9 | 27 10 | -------------------------------------------------------------------------------- /compiler/gen/generatorTester/eqz.vch: -------------------------------------------------------------------------------- 1 | 1 1000.0 2 | 1 100.0 3 | 10 4 | 1 100.0 5 | 1 10.0 6 | 10 7 | 15 8 | 1 10.0 9 | 1 1.0 10 | 10 11 | 15 12 | 19 13 | 27 14 | -------------------------------------------------------------------------------- /compiler/gen/generatorTester/gtr.vch: -------------------------------------------------------------------------------- 1 | 1 1000.0 2 | 1 100.0 3 | 14 4 | 1 100.0 5 | 1 10.0 6 | 14 7 | 15 8 | 1 10.0 9 | 1 1.0 10 | 14 11 | 15 12 | 19 13 | 27 14 | -------------------------------------------------------------------------------- /compiler/gen/generatorTester/les.vch: -------------------------------------------------------------------------------- 1 | 1 1.0 2 | 1 10.0 3 | 9 4 | 1 10.0 5 | 1 100.0 6 | 9 7 | 15 8 | 1 100.0 9 | 1 1000.0 10 | 9 11 | 15 12 | 19 13 | 27 14 | -------------------------------------------------------------------------------- /compiler/gen/generatorTester/nqz.vch: -------------------------------------------------------------------------------- 1 | 1 1000.0 2 | 1 100.0 3 | 12 4 | 1 100.0 5 | 1 10.0 6 | 12 7 | 15 8 | 1 10.0 9 | 1 1.0 10 | 12 11 | 15 12 | 19 13 | 27 14 | -------------------------------------------------------------------------------- /compiler/src/main/java/vong/piler/her/enums/DataTypeEnum.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.enums; 2 | 3 | public enum DataTypeEnum { 4 | ZAL, 5 | ISSO, 6 | WORD; 7 | } 8 | -------------------------------------------------------------------------------- /compiler/src/main/java/vong/piler/her/Constants.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her; 2 | 3 | public class Constants { 4 | 5 | public static final String loggerName = "VongPiler"; 6 | 7 | } 8 | -------------------------------------------------------------------------------- /compiler/src/main/test/vong/piler/her/generator/correct/files/les.vch: -------------------------------------------------------------------------------- 1 | 1 1.0 2 | 1 10.0 3 | 9 4 | 1 10.0 5 | 1 100.0 6 | 9 7 | 15 8 | 1 100.0 9 | 1 1000.0 10 | 9 11 | 15 12 | 19 13 | 27 14 | -------------------------------------------------------------------------------- /compiler/src/main/test/vong/piler/her/generator/correct/files/eqz.vch: -------------------------------------------------------------------------------- 1 | 1 1000.0 2 | 1 100.0 3 | 10 4 | 1 100.0 5 | 1 10.0 6 | 10 7 | 15 8 | 1 10.0 9 | 1 1.0 10 | 10 11 | 15 12 | 19 13 | 27 14 | -------------------------------------------------------------------------------- /compiler/src/main/test/vong/piler/her/generator/correct/files/gtr.vch: -------------------------------------------------------------------------------- 1 | 1 1000.0 2 | 1 100.0 3 | 14 4 | 1 100.0 5 | 1 10.0 6 | 14 7 | 15 8 | 1 10.0 9 | 1 1.0 10 | 14 11 | 15 12 | 19 13 | 27 14 | -------------------------------------------------------------------------------- /compiler/src/main/test/vong/piler/her/generator/correct/files/nqz.vch: -------------------------------------------------------------------------------- 1 | 1 1000.0 2 | 1 100.0 3 | 12 4 | 1 100.0 5 | 1 10.0 6 | 12 7 | 15 8 | 1 10.0 9 | 1 1.0 10 | 12 11 | 15 12 | 19 13 | 27 14 | -------------------------------------------------------------------------------- /runtime/src/main/java/vong/piler/her/vongruntime/exception/EmptyLineException.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.vongruntime.exception; 2 | 3 | public class EmptyLineException extends Exception { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /runtime/src/main/java/vong/piler/her/vongruntime/exception/ReadEmptyRegisterException.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.vongruntime.exception; 2 | 3 | public class ReadEmptyRegisterException extends Exception { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /runtime/src/main/java/vong/piler/her/vongruntime/exception/UnsupportedNumberofArgumentsException.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.vongruntime.exception; 2 | 3 | public class UnsupportedNumberofArgumentsException extends Exception { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /runtime/src/main/java/vong/piler/her/vongruntime/exception/InstructionPointerOutOfBoundsException.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.vongruntime.exception; 2 | 3 | public class InstructionPointerOutOfBoundsException extends Exception { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /runtime/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/main/resources=UTF-8 4 | encoding//src/test/java=UTF-8 5 | encoding//src/test/resources=UTF-8 6 | encoding/=UTF-8 7 | -------------------------------------------------------------------------------- /runtime/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 3 | org.eclipse.jdt.core.compiler.compliance=1.8 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.8 6 | -------------------------------------------------------------------------------- /runtime/src/main/java/vong/piler/her/vongruntime/exception/UnknownCommandException.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.vongruntime.exception; 2 | 3 | public class UnknownCommandException extends Exception { 4 | 5 | public UnknownCommandException(String string) { 6 | super(string); 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /compiler/src/main/test/ibims1code.vch: -------------------------------------------------------------------------------- 1 | 0 0 2 | 1 12.0 3 | 19 4 | 0 1 5 | 2 1 6 | 19 7 | 3 DAFUQ 8 | 17 9 | 1 12.0 10 | 17 11 | 21 12 | 0 21 13 | 0 1 14 | 2 1 15 | 10 16 | 11 17 | 16 18 | 3 nope!!! 19 | 17 20 | 21 21 | 18 22 | 0 30 23 | 0 1 24 | 2 0 25 | 10 26 | 11 27 | 16 28 | 3 nope!!! 29 | 17 30 | 21 31 | 0 11 32 | 15 33 | 20 34 | -------------------------------------------------------------------------------- /compiler/gen/ibims1code.vch: -------------------------------------------------------------------------------- 1 | 0 0 2 | 1 12.0 3 | 19 4 | 0 1 5 | 2 1 6 | 19 7 | 3 DAFUQ 8 | 17 9 | 1 12.0 10 | 17 11 | 0 25 12 | 0 1 13 | 2 1 14 | 10 15 | 11 16 | 16 17 | 0 25 18 | 2 1 19 | 2 0 20 | 10 21 | 11 22 | 16 23 | 3 nope!!! 24 | 17 25 | 18 26 | 0 33 27 | 2 1 28 | 2 0 29 | 10 30 | 11 31 | 16 32 | 3 nope!!! 33 | 17 34 | 0 10 35 | 15 36 | 20 37 | -------------------------------------------------------------------------------- /runtime/src/main/java/vong/piler/her/vongruntime/util/StringUtils.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.vongruntime.util; 2 | 3 | public class StringUtils { 4 | 5 | public static String removeLastCharacters(String str, int count) { 6 | if (str != null && str.length() > count) { 7 | str = str.substring(0, str.length() - count); 8 | } 9 | return str; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /compiler/gen/fibonacci.vch: -------------------------------------------------------------------------------- 1 | 0 0 2 | 1 1.0 3 | 19 4 | 0 1 5 | 1 1.0 6 | 19 7 | 0 2 8 | 1 0.0 9 | 19 10 | 0 3 11 | 2 0 12 | 19 13 | 0 3 14 | 0 2 15 | 1 1000.0 16 | 9 17 | 19 18 | 0 42 19 | 0 3 20 | 2 1 21 | 10 22 | 11 23 | 16 24 | 0 2 25 | 0 0 26 | 0 1 27 | 4 28 | 19 29 | 0 0 30 | 17 31 | 0 1 32 | 17 33 | 0 2 34 | 17 35 | 0 0 36 | 0 1 37 | 19 38 | 0 1 39 | 0 2 40 | 19 41 | 0 12 42 | 15 43 | 20 44 | -------------------------------------------------------------------------------- /runtime/src/main/resources/Fibonacci.vch: -------------------------------------------------------------------------------- 1 | PSA 3 2 | PSZ 0 3 | SAV 4 | PSA 0 5 | PSZ 1 6 | SAV 7 | PSA 1 8 | PSZ 1 9 | SAV 10 | PSA 0 11 | PRT 12 | PSA 1 13 | PRT 14 | PSA 2 15 | PSA 0 16 | PSA 1 17 | ADD 18 | SAV 19 | PSA 0 20 | PSA 1 21 | SAV 22 | PSA 1 23 | PSA 2 24 | SAV 25 | PSA 2 26 | PRT 27 | PSA 13 28 | PSA 3 29 | PSA 3 30 | PSZ 1 31 | ADD 32 | SAV 33 | PSA 3 34 | PSZ 8 35 | LES 36 | JMT 37 | END -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | 2k18 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.m2e.core.maven2Builder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.m2e.core.maven2Nature 16 | 17 | 18 | -------------------------------------------------------------------------------- /ExamplePrograms/lauchTester.vsh: -------------------------------------------------------------------------------- 1 | was ist das für 1 code? 2 | i bims 1 zal 💪 gönn dir 0!!! 3 | i bims 1 isso lauch gönn dir yup!!! 4 | 5 | gieb "Gib d1 umfang vong bizeps her:" her? 6 | 💪 gönn dir 1gabe!!! 7 | 8 | lauch gönn dir was ist das für 1 isweniga vong 💪, 44 her? 9 | 10 | bist du lauch? yup 11 | gieb "G pumpen du Lauch!" her? 12 | real rap 13 | 14 | bist du lauch? nope 15 | gieb "Du bist k1 Lauch!" her? 16 | real rap 17 | 1 n🍦r!!! -------------------------------------------------------------------------------- /compiler/src/main/test/ibims1code.vsh: -------------------------------------------------------------------------------- 1 | was ist das für 1 code? 2 | 3 | i bims 1 zal var1 gönn dir 12 !!! 4 | 5 | i bims 1 isso varIssö gönn dir yup !!! 6 | 7 | :X was ist das für 1 sume vong var1, 12 her? :X LAWL 8 | 9 | gieb "DAFUQ", 12 her? 10 | 11 | #LOL 12 | 13 | bist du varIssö? yup :X If { 14 | gieb "nope!!!" her? 15 | halo i bims!!! 16 | real rap :X } 17 | bist du varIssö? nope :X Else { 18 | gieb "nope!!!" her? 19 | real rap :X } 20 | 21 | g zu #LOL du larry!!! 22 | 23 | 1 nicer!!! 24 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | vong.piler.her 5 | 2k18 6 | 1.0-beta 7 | pom 8 | 9 | 10 | 11 | compiler 12 | runtime 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /compiler/src/main/resources/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | %d{dd.MM.yyyy HH:mm:ss} [%p] %c in %t: %m%n 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /compiler/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | compiler 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.m2e.core.maven2Nature 21 | org.eclipse.jdt.core.javanature 22 | 23 | 24 | -------------------------------------------------------------------------------- /runtime/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | vongruntime 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /compiler/src/main/test/fibonacci.vch: -------------------------------------------------------------------------------- 1 | 0 0 2 | 1 0.0 3 | 19 4 | 0 1 5 | 1 1.0 6 | 19 7 | 0 2 8 | 1 0.0 9 | 19 10 | 0 3 11 | 1 0.0 12 | 19 13 | 0 4 14 | 1 0.0 15 | 19 16 | 0 5 17 | 2 0 18 | 19 19 | 3 gieb anz fibonacci 20 | 17 21 | 21 22 | 0 3 23 | 24 24 | 0 5 25 | 0 4 26 | 0 3 27 | 9 28 | 19 29 | 0 69 30 | 0 5 31 | 2 1 32 | 10 33 | 11 34 | 16 35 | 0 2 36 | 0 0 37 | 0 1 38 | 4 39 | 19 40 | 3 Fibonacci 41 | 17 42 | 0 4 43 | 17 44 | 3 : 45 | 17 46 | 0 0 47 | 17 48 | 3 + 49 | 17 50 | 0 1 51 | 17 52 | 3 = 53 | 17 54 | 0 2 55 | 17 56 | 21 57 | 0 0 58 | 0 1 59 | 19 60 | 0 1 61 | 0 2 62 | 19 63 | 0 4 64 | 0 4 65 | 1 1.0 66 | 4 67 | 19 68 | 0 23 69 | 15 70 | 20 71 | -------------------------------------------------------------------------------- /ExamplePrograms/fibonacci.vsh: -------------------------------------------------------------------------------- 1 | was ist das für 1 code? 2 | i bims 1 zal lauch1 gönn dir 0!!! 3 | i bims 1 zal lauch2 gönn dir 1!!! 4 | i bims 1 zal erg gönn dir 0!!! 5 | i bims 1 zal max gönn dir 0!!! 6 | i bims 1 zal zähl gönn dir 0!!! 7 | i bims 1 isso 🦄 gönn dir nope!!! 8 | 9 | gieb "gieb anz fibonacci" her? 10 | max gönn dir 1gabe!!! 11 | 12 | #start 13 | 🦄 gönn dir was ist das für 1 isweniga vong zähl, max her? 14 | bist du 🦄? yup 15 | erg gönn dir was ist das für 1 sume vong lauch1, lauch2 her? 16 | gieb "Fibonacci " + zähl +" :" + lauch1 + " + " + lauch2 + " = " + erg her? 17 | lauch1 gönn dir lauch2!!! 18 | lauch2 gönn dir erg!!! 19 | zähl gönn dir was ist das für 1 sume vong zähl, 1 her? 20 | g zu #start du larry!!! 21 | real rap 22 | 1 n🍦r!!! -------------------------------------------------------------------------------- /compiler/src/main/test/fibonacci.vsh: -------------------------------------------------------------------------------- 1 | was ist das für 1 code? 2 | 3 | i bims 1 zal lauch1 gönn dir 0!!! 4 | 5 | i bims 1 zal lauch2 gönn dir 1!!! 6 | 7 | i bims 1 zal ergebnüs gönn dir 0!!! 8 | 9 | i bims 1 zal max gönn dir 0!!! 10 | 11 | i bims 1 zal zaehl gönn dir 0!!! 12 | 13 | i bims 1 isso 🦄 gönn dir nope!!! 14 | 15 | gieb "gieb anz fibonacci" her? 16 | 17 | max gönn dir 1gabe!!! 18 | 19 | #start 20 | 21 | 🦄 gönn dir was ist das für 1 isweniga vong zaehl, max her? 22 | 23 | bist du 🦄? yup 24 | 25 | ergebnüs gönn dir was ist das für 1 sume vong lauch1, lauch2 her? 26 | 27 | gieb "Fibonacci " + zaehl +" :" + lauch1 + " + " + lauch2 + " = " + ergebnüs her? 28 | 29 | lauch1 gönn dir lauch2!!! 30 | 31 | lauch2 gönn dir ergebnüs!!! 32 | 33 | zaehl gönn dir was ist das für 1 sume vong zaehl, 1 her? 34 | 35 | g zu #start du larry!!! 36 | 37 | real rap 38 | 39 | :zipper_mouth_face: 1 n🍦r!!! 40 | 1 nicer!!! 41 | -------------------------------------------------------------------------------- /compiler/src/main/java/vong/piler/her/parser/TreeNode.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.parser; 2 | 3 | import vong.piler.her.enums.TokenTypeEnum; 4 | 5 | public class TreeNode { 6 | 7 | private TokenTypeEnum name; 8 | 9 | private TreeNode parent; 10 | 11 | private TreeNode right; 12 | 13 | private Object left; 14 | 15 | public TreeNode(TokenTypeEnum name, TreeNode parent) { 16 | this.name = name; 17 | this.parent = parent; 18 | } 19 | 20 | public TokenTypeEnum getName() { 21 | return name; 22 | } 23 | 24 | public void setParent(TreeNode parent) { 25 | this.parent = parent; 26 | } 27 | 28 | public TreeNode getParent() { 29 | return parent; 30 | } 31 | 32 | public void setRight(TreeNode right) { 33 | this.right = right; 34 | } 35 | 36 | public void setLeft(Object left) { 37 | this.left = left; 38 | } 39 | 40 | public Object getLeft() { 41 | return this.left; 42 | } 43 | 44 | public TreeNode getRight() { 45 | return right; 46 | } 47 | } -------------------------------------------------------------------------------- /compiler/.gitignore: -------------------------------------------------------------------------------- 1 | .metadata 2 | bin/ 3 | tmp/ 4 | *.tmp 5 | *.bak 6 | *.swp 7 | *~.nib 8 | local.properties 9 | .settings/ 10 | .loadpath 11 | .recommenders 12 | 13 | # External tool builders 14 | .externalToolBuilders/ 15 | 16 | # Locally stored "Eclipse launch configurations" 17 | *.launch 18 | 19 | # PyDev specific (Python IDE for Eclipse) 20 | *.pydevproject 21 | 22 | # CDT-specific (C/C++ Development Tooling) 23 | .cproject 24 | 25 | # CDT- autotools 26 | .autotools 27 | 28 | # Java annotation processor (APT) 29 | .factorypath 30 | 31 | # PDT-specific (PHP Development Tools) 32 | .buildpath 33 | 34 | # sbteclipse plugin 35 | .target 36 | 37 | # Tern plugin 38 | .tern-project 39 | 40 | # TeXlipse plugin 41 | .texlipse 42 | 43 | # STS (Spring Tool Suite) 44 | .springBeans 45 | 46 | # Code Recommenders 47 | .recommenders/ 48 | 49 | # Scala IDE specific (Scala & Java development for Eclipse) 50 | .cache-main 51 | .scala_dependencies 52 | .worksheet 53 | /.DS_Store 54 | /target/ 55 | -------------------------------------------------------------------------------- /compiler/src/main/java/vong/piler/her/enums/OperationEnum.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.enums; 2 | 3 | /* 4 | * Explanation: 5 | * arg is a value from stack, if arg1, arg2. ..., argn is used, get the last n values from stack. 6 | */ 7 | 8 | public enum OperationEnum { 9 | /*00*/ PSA, // push address to stack 10 | /*01*/ PSZ, // push number(zal) to stack 11 | /*02*/ PSI, // push boolean(isso) to stack 12 | /*03*/ PSW, 13 | /*04*/ ADD, // arg1 + arg2 14 | /*05*/ SUB, // arg1 - arg2 15 | /*06*/ MUL, // arg1 * arg2 16 | /*07*/ DIV, // arg1 / arg2 17 | /*08*/ MOD, // arg1 % arg2 18 | /*09*/ LES, // arg1 < arg2 19 | /*10*/ EQL, // arg1 == arg2 20 | /*11*/ NOT, // arg1 != arg2 21 | /*12*/ GTR, // arg1 > arg2 22 | /*13*/ AND, // arg1 && arg2 23 | /*14*/ OHR, // arg1 || arg2 24 | /*15*/ JMP, // jump to address(arg) 25 | /*16*/ JMT, // jump to address(arg) if previous compare was true 26 | /*17*/ PRT, // prints arg 27 | /*18*/ AAL, // prints "hello world!" 28 | /*19*/ SAV, 29 | /*20*/ END, // end of code 30 | /*21*/ NWL, // print with newline 31 | /*22 */ IIN, 32 | /*23*/ WIN, 33 | /*24 */ ZIN; 34 | } 35 | -------------------------------------------------------------------------------- /compiler/src/main/java/vong/piler/her/lexer/Token.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.lexer; 2 | 3 | import vong.piler.her.enums.TokenTypeEnum; 4 | 5 | public class Token { 6 | private TokenTypeEnum type; 7 | private String content = ""; 8 | private int line; 9 | 10 | public Token(int line, TokenTypeEnum type) { 11 | this.line = line; 12 | this.type = type; 13 | } 14 | 15 | public TokenTypeEnum getType() { 16 | return type; 17 | } 18 | 19 | public void setType(TokenTypeEnum type) { 20 | this.type = type; 21 | } 22 | 23 | public String getContent() { 24 | return content; 25 | } 26 | 27 | public void setContent(String content) { 28 | this.content = content; 29 | } 30 | 31 | public int getLine() { 32 | return this.line; 33 | } 34 | 35 | public String toString() { 36 | String text = this.line + ": \t" + this.type.name(); 37 | 38 | if (!this.content.isEmpty()) { 39 | text += ": " + this.content + ""; 40 | } else { 41 | text += "(" + this.type.getLabel() + ")"; 42 | } 43 | 44 | return text; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /compiler/src/main/java/vong/piler/her/exceptions/GenerationsFails.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.exceptions; 2 | 3 | import vong.piler.her.enums.TokenTypeEnum; 4 | import vong.piler.her.parser.TreeNode; 5 | 6 | public class GenerationsFails extends Exception { 7 | 8 | private static final long serialVersionUID = -6054019395134165127L; 9 | 10 | public GenerationsFails() { 11 | super(); 12 | } 13 | 14 | public GenerationsFails(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { 15 | super(message, cause, enableSuppression, writableStackTrace); 16 | } 17 | 18 | public GenerationsFails(String message, Throwable cause) { 19 | super(message, cause); 20 | } 21 | 22 | public GenerationsFails(String message) { 23 | super(message); 24 | } 25 | 26 | public GenerationsFails(Throwable cause) { 27 | super(cause); 28 | } 29 | 30 | public GenerationsFails(TreeNode node, int tokenId, Throwable cause) { 31 | super("Generation fails at token " + node.getName() + " with ID " + tokenId, cause); 32 | } 33 | 34 | public GenerationsFails(TreeNode node, int tokenId) { 35 | super("Generation fails at token " + node.getName() + " with ID " + tokenId); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /runtime/src/main/java/vong/piler/her/vongruntime/virtualmachine/model/Command.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.vongruntime.virtualmachine.model; 2 | 3 | public class Command { 4 | private OperationEnum opCode; 5 | private String firstParam, secondParam; 6 | 7 | public OperationEnum getOpCode() { 8 | return opCode; 9 | } 10 | 11 | public void setOpCode(OperationEnum opCode) { 12 | this.opCode = opCode; 13 | } 14 | 15 | public String getFirstParam() { 16 | return firstParam; 17 | } 18 | 19 | public void setFirstParam(String firstParam) { 20 | this.firstParam = firstParam; 21 | } 22 | 23 | public String getSecondParam() { 24 | return secondParam; 25 | } 26 | 27 | public void setSecondParam(String secondParam) { 28 | this.secondParam = secondParam; 29 | } 30 | 31 | public int paramcount() { 32 | if(firstParam != null && secondParam != null) { 33 | return 2; 34 | }else if (firstParam != null && secondParam == null) { 35 | return 1; 36 | }else { 37 | return 0; 38 | } 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return String.format("%s %s %s", opCode, firstParam == null? "" : firstParam , secondParam == null?"": secondParam); 44 | } 45 | 46 | 47 | 48 | } 49 | -------------------------------------------------------------------------------- /runtime/src/main/java/vong/piler/her/vongruntime/virtualmachine/model/OperationEnum.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.vongruntime.virtualmachine.model; 2 | 3 | /* 4 | * Explanation: 5 | * arg is a value from stack, if arg1, arg2. ..., argn is used, get the last n values from stack. 6 | */ 7 | 8 | public enum OperationEnum { 9 | /*00*/ PSA, // push address to stack 10 | /*01*/ PSZ, // push number(zal) to stack 11 | /*02*/ PSI, // push boolean(isso) to stack 12 | /*03*/ PSW, 13 | /*04*/ ADD, // arg1 + arg2 14 | /*05*/ SUB, // arg1 - arg2 15 | /*06*/ MUL, // arg1 * arg2 16 | /*07*/ DIV, // arg1 / arg2 17 | /*08*/ MOD, // arg1 % arg2 18 | /*09*/ LES, // arg1 < arg2 19 | /*10*/ EQL, // arg1 == arg2 20 | /*11*/ NOT, // arg1 != arg2 21 | /*12*/ GTR, // arg1 > arg2 22 | /*13*/ AND, // arg1 && arg2 23 | /*14*/ OHR, // arg1 || arg2 24 | /*15*/ JMP, // jump to address(arg) 25 | /*16*/ JMT, // jump to address(arg) if previous compare was true 26 | /*17*/ PRT, // prints arg 27 | /*18*/ AAL, // prints "hello world!" 28 | /*19*/ SAV, 29 | /*20*/ END, // end of code 30 | /*21*/ NWL, // print with newline 31 | /*22*/ IIN, // 32 | /*23*/ WIN, 33 | /*24*/ ZIN; 34 | } 35 | -------------------------------------------------------------------------------- /compiler/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # "THE BEER-WARE LICENSE" (Revision 42): 2 | 3 | ## English 4 | The Vongpiler Team wrote this compiler and thisvirtual machine. As long as you retain this notice you 5 | can do whatever you want with this stuff. If we meet some day, and you think 6 | this stuff is worth it, you can buy us a beer in return. Vongpiler Team 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 9 | 10 | ## Deutsch 11 | Das Vongpiler Team schrieb diesen Compiler und diese Virtellen Maschine. Solange Sie diesen Vermerk nicht entfernen, können 12 | Sie mit dem Material machen, was Sie möchten. Wenn wir uns eines Tages treffen und Sie 13 | denken, das Material ist es wert, können Sie uns dafür ein Bier ausgeben. Vongpiler Team 14 | 15 | ## Vong 16 | Halo i bims 1 liezense!!! 17 | Das Vongpiler Teams schrib disen Vongpiler und dise VVM (Vong Virtual Machine). Solange du Lauch dieseng Vermerk nit entfernenst, kanst 18 | du mid dems Matiral machen, was du möchteng vong Benutzung her. Weng wir uns 1es Tages trefeng unt du 19 | dängst, das Material isd es wert, gieb unse 1 🍺 aus du Larry. Vongpiler Teams her? 20 | -------------------------------------------------------------------------------- /runtime/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /runtime/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | vong.piler.her 6 | vongruntime 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | 11 | UTF-8 12 | 13 | 14 | 15 | 16 | org.apache.maven.plugins 17 | maven-compiler-plugin 18 | 3.7.0 19 | 20 | 1.8 21 | 1.8 22 | 23 | 24 | 25 | org.apache.maven.plugins 26 | maven-assembly-plugin 27 | 3.1.0 28 | 29 | 30 | jar-with-dependencies 31 | 32 | 33 | 34 | vong.piler.her.vongruntime.main.VongRuntime 35 | 36 | 37 | 38 | 39 | 40 | 41 | make-assembly 42 | package 43 | 44 | single 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | junit 54 | junit 55 | 4.12 56 | test 57 | 58 | 59 | commons-cli 60 | commons-cli 61 | 1.4 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /compiler/src/main/java/vong/piler/her/generator/PreDefinedFunction.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.generator; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import vong.piler.her.enums.OperationEnum; 7 | 8 | public class PreDefinedFunction { 9 | 10 | static void generateCalculations(OperationEnum calculationOperation, List values, ByteCodeWriter writer){ 11 | writer.addCommandResolveAdresses(values.get(0).getOperation(), values.get(0).getValue()); 12 | values.remove(0); 13 | values.forEach(value -> { 14 | writer.addCommandResolveAdresses(value.getOperation(), value.getValue()); 15 | writer.addCommand(calculationOperation); 16 | }); 17 | } 18 | 19 | static void generateComparator(OperationEnum comparatorOperation, List values, ByteCodeWriter writer){ 20 | ValueModel prevValue = null; 21 | writer.addCommandResolveAdresses(values.get(0).getOperation(), values.get(0).getValue()); 22 | values.remove(0); 23 | writer.addCommandResolveAdresses(values.get(0).getOperation(), values.get(0).getValue()); 24 | prevValue = values.get(0); 25 | values.remove(0); 26 | writer.addCommand(comparatorOperation); 27 | for (ValueModel value : values){ 28 | writer.addCommandResolveAdresses(prevValue.getOperation(), prevValue.getValue()); 29 | writer.addCommandResolveAdresses(value.getOperation(), value.getValue()); 30 | prevValue = value; 31 | writer.addCommand(comparatorOperation); 32 | writer.addCommand(OperationEnum.AND); 33 | } 34 | } 35 | 36 | static void generateLogicOperator(OperationEnum logicOperation, List values, ByteCodeWriter writer){ 37 | writer.addCommandResolveAdresses(values.get(0).getOperation(), values.get(0).getValue()); 38 | values.remove(0); 39 | values.forEach(value -> { 40 | writer.addCommandResolveAdresses(value.getOperation(), value.getValue()); 41 | writer.addCommand(logicOperation); 42 | }); 43 | } 44 | 45 | static void generatePrint(OperationEnum operation, List values, ByteCodeWriter writer) { 46 | values.forEach(value -> { 47 | writer.addCommandResolveAdresses(value.getOperation(), value.getValue()); 48 | writer.addCommand(operation); 49 | }); 50 | writer.addCommand(OperationEnum.NWL); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /runtime/src/main/java/vong/piler/her/vongruntime/virtualmachine/model/StackElement.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.vongruntime.virtualmachine.model; 2 | 3 | 4 | 5 | 6 | public class StackElement { 7 | public enum Type{ 8 | ADDRESS, ZAL, ISSO, WORD 9 | } 10 | 11 | public StackElement(Type type, Object value) { 12 | this.type = type; 13 | this.value = value; 14 | } 15 | 16 | private Type type; 17 | private Object value; 18 | 19 | public String toString() { 20 | switch(type) { 21 | case ADDRESS: 22 | return "address: " + value.toString(); 23 | case ISSO: 24 | return "isso: " + (((boolean) value)? "yup" : "nope"); 25 | case ZAL: 26 | return "zal: " + value.toString(); 27 | case WORD: 28 | return "word: " + value.toString(); 29 | default: 30 | return "unknown type on stack"; 31 | } 32 | } 33 | 34 | public String print() { 35 | switch(type) { 36 | case ADDRESS: 37 | return value.toString(); 38 | case ISSO: 39 | return (((boolean) value)? "yup" : "nope"); 40 | case ZAL: 41 | return value.toString(); 42 | case WORD: 43 | return value.toString(); 44 | default: 45 | return "unknown type on stack"; 46 | } 47 | } 48 | 49 | public Type getType() { 50 | return type; 51 | } 52 | 53 | public void setType(Type type) { 54 | this.type = type; 55 | } 56 | 57 | public Object getValue() { 58 | return value; 59 | } 60 | 61 | public void setValue(Object value) { 62 | this.value = value; 63 | } 64 | 65 | @Override 66 | public int hashCode() { 67 | final int prime = 31; 68 | int result = 1; 69 | result = prime * result + ((type == null) ? 0 : type.hashCode()); 70 | result = prime * result + ((value == null) ? 0 : value.hashCode()); 71 | return result; 72 | } 73 | 74 | @Override 75 | public boolean equals(Object obj) { 76 | if (this == obj) 77 | return true; 78 | if (obj == null) 79 | return false; 80 | if (getClass() != obj.getClass()) 81 | return false; 82 | StackElement other = (StackElement) obj; 83 | if (type != other.type) 84 | return false; 85 | if (value == null) { 86 | if (other.value != null) 87 | return false; 88 | } else if (!value.equals(other.value)) 89 | return false; 90 | return true; 91 | } 92 | 93 | 94 | 95 | } 96 | -------------------------------------------------------------------------------- /runtime/src/main/java/vong/piler/her/vongruntime/main/VongRuntime.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.vongruntime.main; 2 | 3 | import java.io.File; 4 | 5 | import org.apache.commons.cli.CommandLine; 6 | import org.apache.commons.cli.CommandLineParser; 7 | import org.apache.commons.cli.DefaultParser; 8 | import org.apache.commons.cli.HelpFormatter; 9 | import org.apache.commons.cli.Option; 10 | import org.apache.commons.cli.Options; 11 | import org.apache.commons.cli.ParseException; 12 | 13 | import vong.piler.her.vongruntime.virtualmachine.Steakmachine; 14 | 15 | public class VongRuntime { 16 | 17 | public static void main(String[] args) { 18 | Options options = createOptions(); 19 | 20 | Steakmachine vvm = new Steakmachine(); 21 | vvm.init(); 22 | 23 | if(args.length == 0) { 24 | printHelp(options); 25 | return; 26 | } 27 | 28 | CommandLineParser parser = new DefaultParser(); 29 | try { 30 | CommandLine line = parser.parse(createOptions(), args); 31 | 32 | if(line.hasOption("a")) { 33 | vvm.setReadAssembler(true); 34 | } 35 | if(line.hasOption("d")) { 36 | vvm.setDebugOutput(true); 37 | } 38 | if(line.hasOption("h")) { 39 | printHelp(createOptions()); 40 | }else { 41 | try { 42 | String filename = line.getArgs()[0]; 43 | vvm.load(new File(filename)); 44 | vvm.run(); 45 | }catch(IndexOutOfBoundsException e) { 46 | e.printStackTrace(); 47 | System.err.println("I kan di Datei nit findn"); 48 | } 49 | 50 | } 51 | 52 | } catch (ParseException e) { 53 | System.err.println("Du hamst 1 Feler gem8 du Lauch"); 54 | printHelp(createOptions()); 55 | } 56 | 57 | } 58 | 59 | private static void printHelp(Options options) { 60 | HelpFormatter helpFormatter = new HelpFormatter(); 61 | helpFormatter.printHelp("vong", "\n", options, "\n", true); 62 | } 63 | 64 | private static Options createOptions() { 65 | Options options = new Options(); 66 | 67 | options.addOption(new Option("h", "Gibd dir dise nachricht du Larry!!")); 68 | options.addOption(new Option("d", "Vongruntime talkt vong debug her!!")); 69 | options.addOption(new Option("a", "I nem jez asembla kot!!")); 70 | options.addOption(new Option("s", "I bims 1 sinloses option vong swag her")); 71 | return options; 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /compiler/src/main/java/vong/piler/her/generator/RegisterHandler.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.generator; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | import org.apache.logging.log4j.LogManager; 7 | import org.apache.logging.log4j.Logger; 8 | 9 | import vong.piler.her.enums.OperationEnum; 10 | 11 | public class RegisterHandler { 12 | 13 | private static Logger logger = LogManager.getLogger(RegisterHandler.class); 14 | 15 | private static RegisterHandler instance; 16 | 17 | private Map dataRegister; 18 | private Map addressMarkerRegister; 19 | private int addressPointer; 20 | private int dataPointer; 21 | 22 | private RegisterHandler() { 23 | this.addressPointer = 0; 24 | this.addressMarkerRegister = new HashMap<>(); 25 | this.dataPointer = 0; 26 | this.dataRegister = new HashMap<>(); 27 | logger.debug("new RegisterHandler"); 28 | } 29 | 30 | static RegisterHandler getInstance() { 31 | if (instance == null) { 32 | instance = new RegisterHandler(); 33 | } 34 | return instance; 35 | } 36 | 37 | int addVariable(String name) { 38 | if (dataRegister.containsKey(name)) { 39 | return dataRegister.get(name); 40 | } 41 | dataRegister.put(name, dataPointer); 42 | logger.debug("added Varaiable " + name + " at address " + (dataPointer)); 43 | return dataPointer++; 44 | } 45 | 46 | int getVariableAddress(String name) { 47 | if (!dataRegister.containsKey(name)) { 48 | return addVariable(name); 49 | } 50 | return dataRegister.get(name); 51 | } 52 | 53 | String getDataAddress(String name) { 54 | if (!addressMarkerRegister.containsKey(name)) { 55 | return name; 56 | } 57 | return addressMarkerRegister.get(name)+""; 58 | } 59 | 60 | String getDataAddressOrBlank(String name) { 61 | if (!addressMarkerRegister.containsKey(name)) { 62 | return ":X" + name + "X:"; 63 | } 64 | return addressMarkerRegister.get(name)+""; 65 | } 66 | 67 | boolean addJumpMarkerIfNotExists(String name) { 68 | if (!addressMarkerRegister.containsKey(name)) { 69 | addressMarkerRegister.put(name, addressPointer); 70 | return true; 71 | } 72 | else { 73 | return false; 74 | } 75 | } 76 | 77 | void operationAdded() { 78 | logger.debug("Opeartion will be added to address " + addressPointer); 79 | addressPointer ++; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /compiler/src/main/java/vong/piler/her/generator/ValueModel.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.generator; 2 | 3 | import vong.piler.her.enums.OperationEnum; 4 | 5 | public class ValueModel { 6 | 7 | private Double zal; 8 | 9 | private Boolean isso; 10 | 11 | private String var; 12 | 13 | private boolean isVar; 14 | 15 | public ValueModel(Double zal) { 16 | this.zal = zal; 17 | this.isso = null; 18 | this.var = null; 19 | this.isVar = false; 20 | } 21 | 22 | public ValueModel(Boolean isso) { 23 | this.isso = isso; 24 | this.zal = null; 25 | this.var = null; 26 | this.isVar = false; 27 | } 28 | 29 | public ValueModel(String word, boolean isVar) { 30 | this.var = word; 31 | this.zal = null; 32 | this.isso = null; 33 | this.isVar = isVar; 34 | } 35 | 36 | public Double getZal() { 37 | return zal; 38 | } 39 | 40 | public void setZal(Double zal) { 41 | this.zal = zal; 42 | this.isso = null; 43 | this.var = null; 44 | this.isVar = false; 45 | } 46 | 47 | public Boolean getIsso() { 48 | return isso; 49 | } 50 | 51 | public void setIsso(Boolean isso) { 52 | this.isso = isso; 53 | this.zal = null; 54 | this.var = null; 55 | this.isVar = false; 56 | } 57 | 58 | public String getVarName() { 59 | return (isVar)?var:null; 60 | } 61 | 62 | public void setVarName(String varName) { 63 | this.var = varName; 64 | this.zal = null; 65 | this.isso = null; 66 | this.isVar = true; 67 | } 68 | 69 | public String getWord() { 70 | return (isVar)?null:var; 71 | } 72 | 73 | public void setWord(String word) { 74 | this.var = word; 75 | this.zal = null; 76 | this.isso = null; 77 | this.isVar = false; 78 | } 79 | 80 | public OperationEnum getOperation() { 81 | if (zal != null) 82 | return OperationEnum.PSZ; 83 | if (isso != null) 84 | return OperationEnum.PSI; 85 | if (var != null && !isVar) 86 | return OperationEnum.PSW; 87 | if (var != null && isVar) { 88 | return OperationEnum.PSA; 89 | } 90 | return null; 91 | } 92 | 93 | public String getValue() { 94 | if (zal != null) 95 | return zal.toString(); 96 | if (isso != null) 97 | return (isso)?"1":"0"; 98 | if (var != null) { 99 | return var; 100 | } 101 | return null; 102 | } 103 | 104 | } 105 | -------------------------------------------------------------------------------- /compiler/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | vong.piler.her 5 | vongpiler 6 | 0.0.1-SNAPSHOT 7 | 8 | 9 | 10 | maven-compiler-plugin 11 | 3.7.0 12 | 13 | 1.8 14 | 1.8 15 | 16 | 17 | 18 | org.apache.maven.plugins 19 | maven-assembly-plugin 20 | 3.1.0 21 | 22 | 23 | jar-with-dependencies 24 | 25 | 26 | 27 | vong.piler.her.Main 28 | 29 | 30 | 31 | 32 | 33 | make-assembly 34 | package 35 | 36 | single 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | junit 46 | junit 47 | 4.12 48 | test 49 | 50 | 51 | org.apache.logging.log4j 52 | log4j-api 53 | 2.4.1 54 | 55 | 56 | org.apache.logging.log4j 57 | log4j-core 58 | 2.4.1 59 | 60 | 61 | commons-lang 62 | commons-lang 63 | 2.6 64 | 65 | 66 | commons-cli 67 | commons-cli 68 | 1.4 69 | 70 | 71 | com.vdurmont 72 | emoji-java 73 | 4.0.0 74 | 75 | 76 | -------------------------------------------------------------------------------- /compiler/src/main/test/vong/piler/her/generator/ComparatorZalGenarationTest.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.generator; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.io.BufferedReader; 6 | import java.io.File; 7 | import java.io.FileReader; 8 | import java.io.IOException; 9 | import java.util.ArrayList; 10 | import java.util.Collections; 11 | import java.util.List; 12 | 13 | import org.junit.Before; 14 | import org.junit.Test; 15 | 16 | import vong.piler.her.enums.OperationEnum; 17 | 18 | public class ComparatorZalGenarationTest { 19 | 20 | private List values = new ArrayList<>(4); 21 | private static String filePrefix = "gen/generatorTester/"; 22 | private static String correctFiles = "src/main/test/vong/piler/her/generator/correct/files/"; 23 | 24 | @Before 25 | public void setUp() throws Exception { 26 | values.add(new ValueModel(1000.0)); 27 | values.add(new ValueModel(100.0)); 28 | values.add(new ValueModel(10.0)); 29 | values.add(new ValueModel(1.0)); 30 | } 31 | 32 | @Test 33 | public void lesTest() { 34 | ByteCodeWriter writer = new ByteCodeWriter(filePrefix + "les.vsh"); 35 | Collections.reverse(values); 36 | PreDefinedFunction.generateComparator(OperationEnum.LES, values, writer); 37 | writer.addPrt(); 38 | writer.eof(); 39 | try { 40 | assertTrue("Files are not equal", testFileContent("les.vch")); 41 | } catch (IOException e) { 42 | e.printStackTrace(); 43 | fail(e.getMessage()); 44 | } 45 | } 46 | 47 | @Test 48 | public void gtrTest() { 49 | ByteCodeWriter writer = new ByteCodeWriter(filePrefix + "gtr.vsh"); 50 | PreDefinedFunction.generateComparator(OperationEnum.GTR, values, writer); 51 | writer.addPrt(); 52 | writer.eof(); 53 | try { 54 | assertTrue("Files are not equal", testFileContent("gtr.vch")); 55 | } catch (IOException e) { 56 | e.printStackTrace(); 57 | fail(e.getMessage()); 58 | } 59 | } 60 | 61 | @Test 62 | public void eqzTest() { 63 | ByteCodeWriter writer = new ByteCodeWriter(filePrefix + "eqz.vsh"); 64 | PreDefinedFunction.generateComparator(OperationEnum.EQL, values, writer); 65 | writer.addPrt(); 66 | writer.eof(); 67 | try { 68 | assertTrue("Files are not equal", testFileContent("eqz.vch")); 69 | } catch (IOException e) { 70 | e.printStackTrace(); 71 | fail(e.getMessage()); 72 | } 73 | } 74 | 75 | private boolean testFileContent(String filename) throws IOException { 76 | boolean same = true; 77 | File file1 = new File(filePrefix + filename); 78 | File file2 = new File(correctFiles + filename); 79 | FileReader fr1 = new FileReader(file1); 80 | FileReader fr2 = new FileReader(file2); 81 | BufferedReader br1 = new BufferedReader(fr1); 82 | BufferedReader br2 = new BufferedReader(fr2); 83 | String br1Line = ""; 84 | while ((br1Line = br1.readLine()) != null) { 85 | if (!br1Line.equals(br2.readLine())) { 86 | same = false; 87 | } 88 | } 89 | br1.close(); 90 | br2.close(); 91 | fr1.close(); 92 | fr2.close(); 93 | return same; 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /compiler/src/main/java/vong/piler/her/enums/TokenTypeEnum.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.enums; 2 | 3 | import org.apache.commons.lang.StringEscapeUtils; 4 | 5 | public enum TokenTypeEnum { 6 | // whitespace 7 | COMMENT("((:X|:zipper_mouth_face:).*?(\n)).*"), WHITESPACE("( |\t).*"), NEWLINE("(\n).*"), 8 | 9 | // program 10 | START("(was ist das für 1 code\\?).*", "was ist das für 1 code?"), // start 11 | END("(1 nicer!!!|1 n:icecream:r!!!).*", "1 nicer!!!|1 n:icecream:r!!!"), // end 12 | 13 | // function 14 | CMD("(was ist das für 1).*", "was ist das für 1"), // call function 15 | PSTART("(vong).*", "vong"), // start of parameters 16 | PNEXT("((,|\\?|\\+){1}).*", ",|?|+"), // next parameter 17 | PEND("(her\\?).*", "her?"), // end of parameter 18 | 19 | // print 20 | PRINT("(gieb).*", "gieb"), 21 | AAL("(halo i bims!!!).*", "halo i bims!!!"), 22 | 23 | // if 24 | IFSTART("(bist du).*", "bist du"), 25 | IFEND("(real rap).*", "real rap"), 26 | 27 | // jump 28 | HASHTAG("#\\b([a-zA-Z]{1}[0-9a-zA-Z_]{0,31})\\b.*", "#"), 29 | GOTOSTART("(g zu).*", "g zu"), 30 | GOTOEND("(du larry!!!).*", "du larry!!!"), 31 | 32 | // declare variable 33 | VSTART("(i bims 1).*", "i bims 1"), // declare variable 34 | ASSI("(gönn dir).*", "gönn dir"), // assign value 35 | VEND("(!!!).*", "!!!"), // end of variable declaration 36 | 37 | // types 38 | TYPE("(zal\\h|word\\h|isso\\h).*", "zal|word|isso"), 39 | 40 | // input 41 | INPUT("(1gabe).*", "1gabe"), // screen input 42 | 43 | // constants 44 | CONST_ZAL("([-+]?[0-9]*\\.?[0-9]+).*", "const_zal"), // constant of type zal (number) 45 | CONST_ISSO("(yup|nope).*", "const_isso"), // constant of type isso (boolean) 46 | CONST_WORD("\\\"(.*?)\\\".*", "const_word"), // constant of type word (string) 47 | 48 | // name / identifier 49 | NAME("((:{1}(\\w|\\+|\\|){2,}:{1})+|(\\b([a-zA-Z]{1}[0-9a-zäöüßA-Z_ÄÖÜ]{0,31})\\b)).*", "name"), 50 | 51 | FNAME("","fname"); 52 | 53 | private String regEx; 54 | private String label; 55 | 56 | TokenTypeEnum(String regEx) { 57 | this.regEx = regEx; 58 | } 59 | 60 | TokenTypeEnum(String regEx, String label) { 61 | this.regEx = regEx; 62 | this.label = label; 63 | } 64 | 65 | public String getRegEx() { 66 | return this.regEx; 67 | } 68 | 69 | public String getLabel() { 70 | return this.label; 71 | } 72 | 73 | public static String toMarkdown() { 74 | String ebnf = ""; 75 | ebnf += "# Lexer Grammar\n\n"; 76 | ebnf += "|Token|Regular Expression|\n"; 77 | ebnf += "|-----|------------------|\n"; 78 | for (TokenTypeEnum tokenType : TokenTypeEnum.values()) { 79 | String regex = StringEscapeUtils.escapeJava(tokenType.getRegEx()); 80 | regex = regex.replaceAll("\\|", "\\\\|"); 81 | ebnf += "|" + tokenType.name() + "|" + regex + "|\n"; 82 | } 83 | return ebnf; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /compiler/src/main/java/vong/piler/her/lexer/Lexer.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.lexer; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.regex.Matcher; 6 | import java.util.regex.Pattern; 7 | 8 | import org.apache.logging.log4j.Logger; 9 | 10 | import vong.piler.her.enums.TokenTypeEnum; 11 | 12 | import org.apache.logging.log4j.LogManager; 13 | 14 | import com.vdurmont.emoji.EmojiParser; 15 | 16 | public class Lexer { 17 | private String source; 18 | private int line; 19 | private static Logger logger = LogManager.getLogger(Lexer.class); 20 | 21 | public Lexer() { 22 | // Print Token-Grammar 23 | // System.out.println(TokenTypeEnum.toMarkdown()); 24 | } 25 | 26 | public List lex(String source) throws Exception { 27 | // replace unicode emojis by text-based emojis 28 | this.source = EmojiParser.parseToAliases(source); 29 | 30 | // we count lines from 1 to n 31 | this.line = 1; 32 | 33 | List tokenList = new ArrayList(); 34 | 35 | // return empty tokenlist, when source is empty 36 | if (this.source.isEmpty()) { 37 | return tokenList; 38 | } 39 | 40 | do { 41 | Token token = nextToken(); 42 | if (token != null) { 43 | switch (token.getType()) { 44 | case COMMENT: 45 | case WHITESPACE: 46 | break; 47 | case NEWLINE: 48 | line++; 49 | break; 50 | default: 51 | tokenList.add(token); 52 | logger.debug(token); 53 | } 54 | } else { 55 | logger.error("tokeng unbekamd:" + this.source); 56 | break; 57 | } 58 | } while (!this.source.isEmpty()); 59 | 60 | return tokenList; 61 | } 62 | 63 | private Token nextToken() { 64 | Token token = null; 65 | 66 | for (TokenTypeEnum tokenType : TokenTypeEnum.values()) { 67 | Pattern pattern = Pattern.compile(tokenType.getRegEx(), Pattern.DOTALL); 68 | 69 | Matcher matcher = pattern.matcher(source); 70 | 71 | if (matcher.matches()) { 72 | // create token 73 | token = new Token(line, tokenType); 74 | 75 | // set value or label 76 | switch (tokenType) { 77 | case TYPE: 78 | token.setContent(matcher.group(1).trim()); 79 | break; 80 | case NAME: 81 | case CONST_ZAL: 82 | case CONST_WORD: 83 | case CONST_ISSO: 84 | case HASHTAG: 85 | token.setContent(matcher.group(1)); 86 | break; 87 | default: 88 | } 89 | 90 | String content = matcher.group(1); 91 | int matchlength = content.length(); 92 | switch (tokenType) { 93 | case CONST_WORD: 94 | matchlength += 2; // skip the quotation marks at start and 95 | // end 96 | break; 97 | case HASHTAG: 98 | matchlength++; // skip "#" at beginning 99 | break; 100 | default: 101 | break; 102 | } 103 | this.source = this.source.substring(matchlength); 104 | break; 105 | } 106 | } 107 | 108 | return token; 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /compiler/src/main/test/vong/piler/her/generator/CalculationGenarationTest.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.generator; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.io.BufferedReader; 6 | import java.io.File; 7 | import java.io.FileReader; 8 | import java.io.IOException; 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | import org.junit.Before; 13 | import org.junit.Test; 14 | 15 | import vong.piler.her.enums.OperationEnum; 16 | 17 | public class CalculationGenarationTest { 18 | 19 | private List values = new ArrayList<>(4); 20 | private static String filePrefix = "gen/generatorTester/"; 21 | private static String correctFiles = "src/main/test/vong/piler/her/generator/correct/files/"; 22 | 23 | @Before 24 | public void setUp() throws Exception { 25 | values.add(new ValueModel(1000.0)); 26 | values.add(new ValueModel(100.0)); 27 | values.add(new ValueModel(10.0)); 28 | values.add(new ValueModel(1.0)); 29 | } 30 | 31 | @Test 32 | public void additionTest() { 33 | ByteCodeWriter writer = new ByteCodeWriter(filePrefix + "add.vsh"); 34 | PreDefinedFunction.generateCalculations(OperationEnum.ADD, values, writer); 35 | writer.addPrt(); 36 | writer.eof(); 37 | try { 38 | assertTrue("Files are not equal", testFileContent("add.vch")); 39 | } catch (IOException e) { 40 | e.printStackTrace(); 41 | fail(e.getMessage()); 42 | } 43 | } 44 | 45 | @Test 46 | public void substraktionTest() { 47 | ByteCodeWriter writer = new ByteCodeWriter(filePrefix + "sub.vsh"); 48 | PreDefinedFunction.generateCalculations(OperationEnum.SUB, values, writer); 49 | writer.addPrt(); 50 | writer.eof(); 51 | try { 52 | assertTrue("Files are not equal", testFileContent("sub.vch")); 53 | } catch (IOException e) { 54 | e.printStackTrace(); 55 | fail(e.getMessage()); 56 | } 57 | } 58 | 59 | @Test 60 | public void multiplicationTest() { 61 | ByteCodeWriter writer = new ByteCodeWriter(filePrefix + "mul.vsh"); 62 | PreDefinedFunction.generateCalculations(OperationEnum.MUL, values, writer); 63 | writer.addPrt(); 64 | writer.eof(); 65 | try { 66 | assertTrue("Files are not equal", testFileContent("mul.vch")); 67 | } catch (IOException e) { 68 | e.printStackTrace(); 69 | fail(e.getMessage()); 70 | } 71 | } 72 | 73 | @Test 74 | public void divisionTest() { 75 | ByteCodeWriter writer = new ByteCodeWriter(filePrefix + "div.vsh"); 76 | PreDefinedFunction.generateCalculations(OperationEnum.DIV, values, writer); 77 | writer.addPrt(); 78 | writer.eof(); 79 | try { 80 | assertTrue("Files are not equal", testFileContent("div.vch")); 81 | } catch (IOException e) { 82 | e.printStackTrace(); 83 | fail(e.getMessage()); 84 | } 85 | } 86 | 87 | @Test 88 | public void moduloTest() { 89 | ByteCodeWriter writer = new ByteCodeWriter(filePrefix + "mod.vsh"); 90 | PreDefinedFunction.generateCalculations(OperationEnum.MOD, values, writer); 91 | writer.addPrt(); 92 | writer.eof(); 93 | try { 94 | assertTrue("Files are not equal", testFileContent("mod.vch")); 95 | } catch (IOException e) { 96 | e.printStackTrace(); 97 | fail(e.getMessage()); 98 | } 99 | } 100 | 101 | private boolean testFileContent(String filename) throws IOException { 102 | boolean same = true; 103 | File file1 = new File(filePrefix + filename); 104 | File file2 = new File(correctFiles + filename); 105 | FileReader fr1 = new FileReader(file1); 106 | FileReader fr2 = new FileReader(file2); 107 | BufferedReader br1 = new BufferedReader(fr1); 108 | BufferedReader br2 = new BufferedReader(fr2); 109 | String br1Line = ""; 110 | while ((br1Line = br1.readLine()) != null) { 111 | if (!br1Line.equals(br2.readLine())) { 112 | same = false; 113 | } 114 | } 115 | br1.close(); 116 | br2.close(); 117 | fr1.close(); 118 | fr2.close(); 119 | return same; 120 | } 121 | 122 | } 123 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/MastersOfDesaster/Vongpiler.svg?branch=master)](https://travis-ci.org/MastersOfDesaster/Vongpiler) 2 | # Vongpiler 3 | 4 | Halo i bim 1 Vongpiler vong [2k18-spec](https://github.com/MastersOfDesaster/2k18-spec) her. 5 | 6 | ## Danksagung 7 | 8 | Für unserem Mutis! 9 | 10 | ## Download 11 | 12 | https://github.com/MastersOfDesaster/Vongpiler/releases/latest 13 | 14 | ## Requirements 15 | 16 | ### Ubuntu 17 | 18 | Um Unicode Emojis sehen zu können brauchst du das ttf-ancient-fonts Paket. 19 | 20 | ```shell 21 | sudo apt-get install ttf-ancient-fonts 22 | ``` 23 | 24 | ## Vongpiler 25 | 26 | Der Vongpiler ermöglicht das übersetzen des mit der [2k18-spec](https://github.com/MastersOfDesaster/2k18-spec) geschriebenen Quellcodes in Machschinencode zum ausführen in der [VRE (Vong Runtime Environment)](#vre). 27 | Die Dateiendung des Quellcodes ist .vsh was für "vong skript her" steht. 28 | 29 | ### Aufruf 30 | 31 | In der Kommandozeile kann ein Programm mit folgendem Befehl ausgeführt werden: 32 | 33 | ```shell 34 | java -jar vongc.jar [Dateiname.vsh] 35 | ``` 36 | 37 | Eine Hilfe zur Bedienung erhält man mit 38 | 39 | ```shell 40 | java -jar vongc.jar -h 41 | ``` 42 | 43 | 44 | 45 | ## VRE (Vong Runtime Environment) 46 | 47 | Die VRE ermöglicht die Ausführung von kompiliertem vongscript code (.vch). 48 | Dazu ist die VVM (Vong Virtual Machine) integriert, die den Maschinencode in der Java JVM ausführt. 49 | 50 | ### Aufruf 51 | 52 | In der Kommandozeile kann ein Programm mit folgendem Befehl ausgeführt werden: 53 | 54 | ```shell 55 | java -jar vong.jar [Dateiname.vch] 56 | ``` 57 | 58 | Eine Hilfe zur Bedienung erhält man mit 59 | 60 | ```shell 61 | java -jar vong.jar -h 62 | ``` 63 | 64 | ## Dokumentation 65 | 66 | Siehe [2k18-spec](https://github.com/MastersOfDesaster/2k18-spec) 67 | 68 | ## Beispielcode 69 | 70 | ### Fibonacci Zahlen 71 | 72 | ```2k18 73 | was ist das für 1 code? 74 | i bims 1 zal lauch1 gönn dir 0!!! 75 | i bims 1 zal lauch2 gönn dir 1!!! 76 | i bims 1 zal erg gönn dir 0!!! 77 | i bims 1 zal max gönn dir 0!!! 78 | i bims 1 zal zähl gönn dir 0!!! 79 | i bims 1 isso 🦄 gönn dir nope!!! 80 | 81 | gieb "gieb anz fibonacci" her? 82 | max gönn dir 1gabe!!! 83 | 84 | #start 85 | 🦄 gönn dir was ist das für 1 isweniga vong zähl, max her? 86 | bist du 🦄? yup 87 | erg gönn dir was ist das für 1 sume vong lauch1, lauch2 her? 88 | gieb "Fibonacci " + zähl +" :" + lauch1 + " + " + lauch2 + " = " + erg her? 89 | lauch1 gönn dir lauch2!!! 90 | lauch2 gönn dir erg!!! 91 | zähl gönn dir was ist das für 1 sume vong zähl, 1 her? 92 | g zu #start du larry!!! 93 | real rap 94 | 1 n🍦r!!! 95 | ``` 96 | 97 | ## Was isd das für ein Vongpiler vong Erschafung her? 98 | 99 | * Am **firsten day** createte Vongpiler teams 1 gramatik vong 2k18 spec her und sah das es wundern🍦e war. 100 | 101 | * Am **seconden day** ward 1 virtual steakmachine definirt. Vongpiler teams dankt hirzu Terence Paar für 1 gutes Tutorial. 102 | 103 | * Am **thirden day** makte Vongpiler teams 1 virtual steakmachine vong Färtiggestelung her. Für Vongpilirung vong codeerzeugung her, wurden lexer u paser färtig gem8. 104 | 105 | * Am **fourthen day** wurd codeerzeugung gefinished u 1stes mahl quälcode verpeilert und ausgeführt. "halo i bims!!!" speakte firstes code zu uns. 106 | 107 | * Am **fifften day** chekte Vongpiler teams "Bist du?" und "#" war neeted vong jumps her. Vongpiler konte nun kras jumpen u äntscheidungem träfen. 108 | 109 | * Am **sixten day** buildete Vongpiler teams "1gabe" von komunikation her. So konte vongpiler teams zu program sprächen. 110 | 111 | * Am **seventeen day** checkte Vongpiler teams das ergenis, das es gebuildet hat, mit lexer u parse, u code vong erzeugung her, u Vong Runtime Environment vong auführung her. 112 | U Vongpiler team sayte: 113 | **<>** 114 | -------------------------------------------------------------------------------- /compiler/src/main/java/vong/piler/her/generator/ByteCodeWriter.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.generator; 2 | 3 | import java.io.BufferedWriter; 4 | import java.io.File; 5 | import java.io.FileWriter; 6 | import java.io.IOException; 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | import org.apache.logging.log4j.Logger; 11 | 12 | import vong.piler.her.enums.OperationEnum; 13 | 14 | import org.apache.logging.log4j.LogManager; 15 | 16 | public class ByteCodeWriter { 17 | 18 | private static Logger logger = LogManager.getLogger(ByteCodeWriter.class); 19 | 20 | private List linesToWrite; 21 | private File file; 22 | private RegisterHandler registerHandler; 23 | 24 | public ByteCodeWriter(File file) { 25 | this.file = file; 26 | linesToWrite = new ArrayList<>(); 27 | this.registerHandler = RegisterHandler.getInstance(); 28 | } 29 | 30 | public ByteCodeWriter(String string) { 31 | this(new File(string)); 32 | } 33 | 34 | private void addLine(String line) { 35 | linesToWrite.add(line); 36 | registerHandler.operationAdded(); 37 | } 38 | 39 | void addCommand(OperationEnum command) { 40 | StringBuilder operationBuilder = new StringBuilder(); 41 | operationBuilder.append(command.ordinal()); 42 | addLine(operationBuilder.toString()); 43 | logger.debug("added operation " + operationBuilder.toString()); 44 | } 45 | 46 | void addCommand(OperationEnum command, String parameter) { 47 | StringBuilder operationBuilder = new StringBuilder(); 48 | operationBuilder.append(command.ordinal()); 49 | operationBuilder.append(" "); 50 | operationBuilder.append(parameter); 51 | addLine(operationBuilder.toString()); 52 | logger.debug("added operation " + operationBuilder.toString()); 53 | } 54 | 55 | void addCommandResolveAdresses(OperationEnum command, String parameter) { 56 | StringBuilder operationBuilder = new StringBuilder(); 57 | operationBuilder.append(command.ordinal()); 58 | operationBuilder.append(" "); 59 | if (command.equals(OperationEnum.PSA)) 60 | operationBuilder.append(registerHandler.getVariableAddress(parameter)); 61 | else 62 | operationBuilder.append(parameter); 63 | addLine(operationBuilder.toString()); 64 | logger.debug("added operation " + operationBuilder.toString()); 65 | } 66 | 67 | void addCommand(OperationEnum command, int address, int count) { 68 | StringBuilder operationBuilder = new StringBuilder(); 69 | operationBuilder.append(command.ordinal()); 70 | operationBuilder.append(" "); 71 | operationBuilder.append(address); 72 | operationBuilder.append(" "); 73 | operationBuilder.append(count); 74 | addLine(operationBuilder.toString()); 75 | logger.debug("added operation " + operationBuilder.toString()); 76 | } 77 | 78 | void eof() { 79 | addCommand(OperationEnum.END); 80 | writeToFile(); 81 | } 82 | 83 | void addPrt() { 84 | addCommand(OperationEnum.PRT); 85 | } 86 | 87 | void addAAL() { 88 | addCommand(OperationEnum.AAL); 89 | } 90 | 91 | void addNOT() { 92 | addCommand(OperationEnum.NOT); 93 | } 94 | 95 | void addBlank(String blankMark) { 96 | String blank = ":X" + blankMark + "X:"; 97 | addCommand(OperationEnum.PSA, blank); 98 | } 99 | 100 | void fillBlankAddress(String blank, String address, int startIndex) { 101 | blank = ":X" + blank + "X:"; 102 | for (int index=startIndex; index { 125 | try { 126 | bw.write(line); 127 | bw.newLine(); 128 | } catch (IOException e) { 129 | e.printStackTrace(); 130 | throw new RuntimeException(e); 131 | } 132 | }); 133 | bw.close(); 134 | fw.close(); 135 | } catch (IOException | RuntimeException e) { 136 | logger.error("Failed to write vong class her", e); 137 | e.printStackTrace(); 138 | return false; 139 | } 140 | logger.info("vong class her was successfully written"); 141 | return true; 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /compiler/src/main/java/vong/piler/her/Main.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.ByteArrayInputStream; 5 | import java.io.File; 6 | import java.io.InputStream; 7 | import java.io.InputStreamReader; 8 | import java.nio.file.Files; 9 | import java.util.Iterator; 10 | import java.util.List; 11 | 12 | import org.apache.commons.cli.CommandLine; 13 | import org.apache.commons.cli.CommandLineParser; 14 | import org.apache.commons.cli.DefaultParser; 15 | import org.apache.commons.cli.HelpFormatter; 16 | import org.apache.commons.cli.Option; 17 | import org.apache.commons.cli.Options; 18 | import org.apache.commons.cli.ParseException; 19 | import org.apache.logging.log4j.Level; 20 | import org.apache.logging.log4j.LogManager; 21 | import org.apache.logging.log4j.Logger; 22 | import org.apache.logging.log4j.core.config.Configurator; 23 | 24 | import vong.piler.her.generator.Generator; 25 | import vong.piler.her.lexer.Lexer; 26 | import vong.piler.her.lexer.Token; 27 | import vong.piler.her.parser.Parser; 28 | import vong.piler.her.parser.TreeNode; 29 | 30 | public class Main { 31 | 32 | private static Logger logger = LogManager.getLogger(Main.class); 33 | 34 | 35 | public static void main(String[] args) { 36 | Lexer lexer = new Lexer(); 37 | Parser parser = new Parser(); 38 | Generator generator; 39 | 40 | Options options = createOptions(); 41 | 42 | if(args.length == 0) { 43 | printHelp(options); 44 | return; 45 | } 46 | 47 | CommandLineParser cliParser = new DefaultParser(); 48 | try { 49 | CommandLine cmdLine = cliParser.parse(createOptions(), args); 50 | String output = null; 51 | 52 | if(cmdLine.hasOption("d")) { 53 | Configurator.setLevel("vong.piler.her", Level.DEBUG); 54 | } 55 | if(cmdLine.hasOption("o")) { 56 | output = cmdLine.getOptionValue("o"); 57 | } 58 | if(cmdLine.hasOption("h")) { 59 | printHelp(createOptions()); 60 | }else { 61 | try { 62 | String filename = cmdLine.getArgs()[0]; 63 | 64 | InputStream is = new ByteArrayInputStream(Files.readAllBytes(new File(filename).toPath())); 65 | BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8")); 66 | StringBuilder sourceBuilder = new StringBuilder(); 67 | Iterator sourceIterator = br.lines().iterator(); 68 | while(sourceIterator.hasNext()) { 69 | sourceBuilder.append(sourceIterator.next()).append("\n"); 70 | } 71 | String source = sourceBuilder.toString(); 72 | 73 | List tokenList = lexer.lex(source); 74 | TreeNode root = parser.parse(tokenList); 75 | 76 | if(output == null) { 77 | output = filename.replace(".vsh", ".vch"); 78 | } 79 | if(cmdLine.hasOption("d")) { 80 | testPrint(root, 2); 81 | } 82 | generator = new Generator(output); 83 | generator.start(root); 84 | 85 | }catch(IndexOutOfBoundsException e) { 86 | System.err.println("I kan di Datei nit findn"); 87 | } catch (Exception e) { 88 | System.err.println("Halo I bims 1 Ausname:"); 89 | e.printStackTrace(); 90 | } 91 | } 92 | 93 | } catch (ParseException e) { 94 | System.err.println("Du hamst 1 Feler gem8 du Lauch"); 95 | printHelp(createOptions()); 96 | } 97 | 98 | } 99 | 100 | private static Options createOptions() { 101 | Options options = new Options(); 102 | options.addOption(new Option("h", "Gibd dir dise nachricht du Larry!!")); 103 | options.addOption(new Option("d", "Vongpiler talkt vong debug her!!")); 104 | options.addOption(new Option("o", true, "Fad wo du dens fertigem Dateikompilat haben wilst!!")); 105 | return options; 106 | } 107 | 108 | private static void printHelp(Options options) { 109 | HelpFormatter helpFormatter = new HelpFormatter(); 110 | helpFormatter.printHelp("vongc", "\n", options, "\n", true); 111 | } 112 | 113 | private static void testPrint(TreeNode root, int tabCount) { 114 | System.out.println("\t\t" + root.getName()); 115 | leftPrint(root.getLeft(), tabCount-1); 116 | if (root.getRight() != null) 117 | testPrint(root.getRight(), ++tabCount); 118 | } 119 | 120 | private static void leftPrint(Object left, int tabCount) { 121 | for (int i = 0; i < tabCount; i++) { 122 | System.out.print("\t"); 123 | } 124 | if (left == null) 125 | System.out.print("null"); 126 | else if (left instanceof Double) 127 | System.out.print(((Double) left)); 128 | else if (left instanceof Boolean) 129 | System.out.print(((Boolean) left)); 130 | else if (left instanceof String) 131 | System.out.print(((String) left)); 132 | else 133 | System.out.print(left.toString()); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /runtime/src/test/java/vong/piler/her/vongvirtualmachine/OpCodeTest.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.vongvirtualmachine; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import java.io.ByteArrayOutputStream; 6 | import java.io.File; 7 | import java.io.PrintStream; 8 | 9 | import org.junit.Before; 10 | import org.junit.Test; 11 | 12 | import vong.piler.her.vongruntime.virtualmachine.Steakmachine; 13 | 14 | public class OpCodeTest { 15 | 16 | Steakmachine vvm = new Steakmachine(); 17 | ByteArrayOutputStream standardOut = new ByteArrayOutputStream(); 18 | ByteArrayOutputStream errorOut = new ByteArrayOutputStream(); 19 | 20 | 21 | @Before 22 | public void init() { 23 | vvm.setStandardOut(new PrintStream(standardOut)); 24 | vvm.setErrorOut(new PrintStream(errorOut)); 25 | vvm.setReadAssembler(true); 26 | vvm.init(); 27 | } 28 | 29 | @Test 30 | public void testWordOutputWithSpaces() { 31 | loadFile("wordOutputWithSpaces.vch"); 32 | vvm.run(); 33 | assertEquals("s w a g", getCleanVvmOutput()); 34 | } 35 | 36 | @Test 37 | public void testWordOutput() { 38 | loadFile("wordOutput.vch"); 39 | vvm.run(); 40 | assertEquals("swag", getCleanVvmOutput()); 41 | } 42 | 43 | @Test 44 | public void testAddWithPositives(){ 45 | loadFile("addWithPositives.vch"); 46 | vvm.run(); 47 | assertEquals("16.0", getCleanVvmOutput()); 48 | } 49 | 50 | @Test 51 | public void testAddWithNegatives(){ 52 | loadFile("addWithNegatives.vch"); 53 | vvm.run(); 54 | assertEquals("-16.0", getCleanVvmOutput()); 55 | } 56 | 57 | @Test 58 | public void testAddWithNegativeAndPositive(){ 59 | loadFile("addWithNegativeAndPositive.vch"); 60 | vvm.run(); 61 | assertEquals("-2.0", getCleanVvmOutput()); 62 | } 63 | 64 | @Test 65 | public void testMulWithPositives(){ 66 | loadFile("mulWithPositives.vch"); 67 | vvm.run(); 68 | assertEquals("35.0", getCleanVvmOutput()); 69 | } 70 | 71 | @Test 72 | public void testMulWithNegatives(){ 73 | loadFile("mulWithNegatives.vch"); 74 | vvm.run(); 75 | assertEquals("40.0", getCleanVvmOutput()); 76 | } 77 | 78 | @Test 79 | public void testMulWithNegativeAndPositive(){ 80 | loadFile("mulWithNegativeAndPositive.vch"); 81 | vvm.run(); 82 | assertEquals("-35.0", getCleanVvmOutput()); 83 | } 84 | 85 | @Test 86 | public void testDivWithPositives(){ 87 | loadFile("divWithPositives.vch"); 88 | vvm.run(); 89 | assertEquals("2.0", getCleanVvmOutput()); 90 | } 91 | 92 | @Test 93 | public void testDivWithNegatives(){ 94 | loadFile("divWithNegatives.vch"); 95 | vvm.run(); 96 | assertEquals("2.0", getCleanVvmOutput()); 97 | } 98 | 99 | @Test 100 | public void testDivWithNegativeAndPositive(){ 101 | loadFile("divWithNegativeAndPositive.vch"); 102 | vvm.run(); 103 | assertEquals("-2.0", getCleanVvmOutput()); 104 | } 105 | 106 | @Test 107 | public void testSubWithPositives(){ 108 | loadFile("subWithPositives.vch"); 109 | vvm.run(); 110 | assertEquals("8.0", getCleanVvmOutput()); 111 | } 112 | 113 | @Test 114 | public void testSubWithNegatives(){ 115 | loadFile("subWithNegatives.vch"); 116 | vvm.run(); 117 | assertEquals("2.0", getCleanVvmOutput()); 118 | } 119 | 120 | @Test 121 | public void testSubWithNegativeAndPositive(){ 122 | loadFile("subWithNegativeAndPositive.vch"); 123 | vvm.run(); 124 | assertEquals("-16.0", getCleanVvmOutput()); 125 | } 126 | 127 | @Test 128 | public void testModWithRemainder(){ 129 | loadFile("modWithRemainder.vch"); 130 | vvm.run(); 131 | assertEquals("1.0", getCleanVvmOutput()); 132 | } 133 | 134 | 135 | @Test 136 | public void testModWithoutRemainder(){ 137 | loadFile("modWithoutRemainder.vch"); 138 | vvm.run(); 139 | assertEquals("0.0", getCleanVvmOutput()); 140 | } 141 | 142 | @Test 143 | public void testLesWhenTrue(){ 144 | loadFile("lesWhenTrue.vch"); 145 | vvm.run(); 146 | assertEquals("yup", getCleanVvmOutput()); 147 | } 148 | 149 | @Test 150 | public void testLesWhenFalse(){ 151 | loadFile("lesWhenFalse.vch"); 152 | vvm.run(); 153 | assertEquals("nope", getCleanVvmOutput()); 154 | } 155 | 156 | @Test 157 | public void testGtrWhenTrue(){ 158 | loadFile("gtrWhenTrue.vch"); 159 | vvm.run(); 160 | assertEquals("yup", getCleanVvmOutput()); 161 | } 162 | 163 | @Test 164 | public void testGtrWhenFalse(){ 165 | loadFile("gtrWhenFalse.vch"); 166 | vvm.run(); 167 | assertEquals("nope", getCleanVvmOutput()); 168 | } 169 | 170 | @Test 171 | public void testJmpToLegalLine(){ 172 | loadFile("jmpToLegalLine.vch"); 173 | vvm.run(); 174 | assertEquals("Halo I bims 1 aal vong Halo Wörlt her", getCleanVvmOutput()); 175 | } 176 | 177 | @Test 178 | public void testJmpToIllegalLine(){ 179 | loadFile("jmpToIllegalLine.vch"); 180 | vvm.run(); 181 | assertEquals("instruction-pointer at empty register", getCleanVvmErrorOutput()); 182 | } 183 | 184 | @Test 185 | public void testEqlWithWords(){ 186 | loadFile("eqlWithWords.vch"); 187 | vvm.run(); 188 | assertEquals("yup", getCleanVvmOutput()); 189 | } 190 | 191 | @Test 192 | public void testEqlWithZals(){ 193 | loadFile("eqlWithZals.vch"); 194 | vvm.run(); 195 | assertEquals("yup", getCleanVvmOutput()); 196 | } 197 | 198 | @Test 199 | public void testEqlWithIssos(){ 200 | loadFile("eqlWithIssos.vch"); 201 | vvm.run(); 202 | assertEquals("yup", getCleanVvmOutput()); 203 | } 204 | 205 | @Test 206 | public void testEqlWithDifferentTypes(){ 207 | loadFile("eqlWithDifferentTypes.vch"); 208 | vvm.run(); 209 | assertEquals("nope", getCleanVvmOutput()); 210 | } 211 | 212 | private void loadFile(String name) { 213 | File file = new File(getClass().getClassLoader().getResource(name).getFile()); 214 | vvm.load(file); 215 | } 216 | 217 | private String getCleanVvmOutput() { 218 | String raw = standardOut.toString(); 219 | return raw.replaceAll("\n", "").replaceAll("\r", ""); 220 | } 221 | 222 | private String getCleanVvmErrorOutput() { 223 | String raw = errorOut.toString(); 224 | return raw.replaceAll("\n", "").replaceAll("\r", ""); 225 | } 226 | 227 | } 228 | -------------------------------------------------------------------------------- /compiler/src/main/java/vong/piler/her/parser/Parser.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.parser; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.EnumMap; 6 | import java.util.HashMap; 7 | import java.util.List; 8 | import java.util.Map; 9 | 10 | import org.apache.logging.log4j.LogManager; 11 | import org.apache.logging.log4j.Logger; 12 | 13 | import vong.piler.her.enums.DataTypeEnum; 14 | import vong.piler.her.enums.TokenTypeEnum; 15 | import vong.piler.her.lexer.Token; 16 | 17 | public class Parser { 18 | 19 | private static Logger logger = LogManager.getLogger(Parser.class); 20 | 21 | private TreeNode parent; 22 | private TreeNode root; 23 | 24 | Map> ruleMap = new EnumMap>( 25 | TokenTypeEnum.class); 26 | 27 | List rule = new ArrayList(); 28 | 29 | Map dataTypeVariable = new HashMap(); 30 | Map dataTypeFunction = new HashMap(); 31 | 32 | public Parser() { 33 | // Add rules to map 34 | ruleMap.put(TokenTypeEnum.START, 35 | Arrays.asList(new TokenTypeEnum[] { TokenTypeEnum.VSTART, TokenTypeEnum.HASHTAG, 36 | TokenTypeEnum.GOTOSTART, TokenTypeEnum.AAL, TokenTypeEnum.PRINT, TokenTypeEnum.END })); 37 | ruleMap.put(TokenTypeEnum.VSTART, Arrays.asList(new TokenTypeEnum[] { TokenTypeEnum.TYPE })); 38 | ruleMap.put(TokenTypeEnum.TYPE, Arrays.asList(new TokenTypeEnum[] { TokenTypeEnum.NAME })); 39 | ruleMap.put(TokenTypeEnum.NAME, Arrays.asList(new TokenTypeEnum[] { TokenTypeEnum.ASSI, TokenTypeEnum.PEND, 40 | TokenTypeEnum.PNEXT, TokenTypeEnum.PSTART, TokenTypeEnum.VEND })); 41 | ruleMap.put(TokenTypeEnum.ASSI, 42 | Arrays.asList(new TokenTypeEnum[] { TokenTypeEnum.CONST_ISSO, TokenTypeEnum.CONST_WORD, 43 | TokenTypeEnum.CONST_ZAL, TokenTypeEnum.CMD, TokenTypeEnum.NAME, TokenTypeEnum.INPUT })); 44 | ruleMap.put(TokenTypeEnum.CONST_ISSO, Arrays.asList(new TokenTypeEnum[] { TokenTypeEnum.PEND, 45 | TokenTypeEnum.PNEXT, TokenTypeEnum.VEND, TokenTypeEnum.PRINT })); 46 | ruleMap.put(TokenTypeEnum.CONST_WORD, Arrays.asList(new TokenTypeEnum[] { TokenTypeEnum.PEND, 47 | TokenTypeEnum.PNEXT, TokenTypeEnum.VEND, TokenTypeEnum.PRINT })); 48 | ruleMap.put(TokenTypeEnum.CONST_ZAL, Arrays.asList(new TokenTypeEnum[] { TokenTypeEnum.PEND, 49 | TokenTypeEnum.PNEXT, TokenTypeEnum.VEND, TokenTypeEnum.PRINT })); 50 | ruleMap.put(TokenTypeEnum.VEND, 51 | Arrays.asList(new TokenTypeEnum[] { TokenTypeEnum.VSTART, TokenTypeEnum.CMD, TokenTypeEnum.PRINT, 52 | TokenTypeEnum.AAL, TokenTypeEnum.IFSTART, TokenTypeEnum.HASHTAG, TokenTypeEnum.GOTOSTART, 53 | TokenTypeEnum.NAME, TokenTypeEnum.IFEND, TokenTypeEnum.END })); 54 | ruleMap.put(TokenTypeEnum.CMD, Arrays.asList(new TokenTypeEnum[] { TokenTypeEnum.NAME })); 55 | ruleMap.put(TokenTypeEnum.PSTART, Arrays.asList(new TokenTypeEnum[] { TokenTypeEnum.CONST_ISSO, 56 | TokenTypeEnum.CONST_WORD, TokenTypeEnum.CONST_ZAL, TokenTypeEnum.PEND, TokenTypeEnum.NAME })); 57 | ruleMap.put(TokenTypeEnum.PNEXT, Arrays.asList(new TokenTypeEnum[] { TokenTypeEnum.CONST_ISSO, 58 | TokenTypeEnum.CONST_WORD, TokenTypeEnum.CONST_ZAL, TokenTypeEnum.NAME })); 59 | ruleMap.put(TokenTypeEnum.PEND, 60 | Arrays.asList(new TokenTypeEnum[] { TokenTypeEnum.CMD, TokenTypeEnum.PRINT, TokenTypeEnum.AAL, 61 | TokenTypeEnum.IFSTART, TokenTypeEnum.IFEND, TokenTypeEnum.HASHTAG, TokenTypeEnum.GOTOSTART, 62 | TokenTypeEnum.NAME, TokenTypeEnum.END })); 63 | ruleMap.put(TokenTypeEnum.PRINT, Arrays.asList(new TokenTypeEnum[] { TokenTypeEnum.CONST_ISSO, 64 | TokenTypeEnum.CONST_WORD, TokenTypeEnum.CONST_ZAL, TokenTypeEnum.NAME })); 65 | ruleMap.put(TokenTypeEnum.AAL, 66 | Arrays.asList(new TokenTypeEnum[] { TokenTypeEnum.VSTART, TokenTypeEnum.CMD, TokenTypeEnum.PRINT, 67 | TokenTypeEnum.AAL, TokenTypeEnum.IFSTART, TokenTypeEnum.IFEND, TokenTypeEnum.HASHTAG, 68 | TokenTypeEnum.GOTOSTART, TokenTypeEnum.END })); 69 | ruleMap.put(TokenTypeEnum.IFSTART, Arrays.asList(new TokenTypeEnum[] { TokenTypeEnum.PSTART })); 70 | ruleMap.put(TokenTypeEnum.IFEND, 71 | Arrays.asList(new TokenTypeEnum[] { TokenTypeEnum.IFEND, TokenTypeEnum.CMD, TokenTypeEnum.PRINT, 72 | TokenTypeEnum.AAL, TokenTypeEnum.IFSTART, TokenTypeEnum.HASHTAG, TokenTypeEnum.GOTOSTART, 73 | TokenTypeEnum.END })); 74 | ruleMap.put(TokenTypeEnum.HASHTAG, 75 | Arrays.asList(new TokenTypeEnum[] { TokenTypeEnum.NAME, TokenTypeEnum.CMD, TokenTypeEnum.PRINT, 76 | TokenTypeEnum.IFSTART, TokenTypeEnum.GOTOSTART, TokenTypeEnum.GOTOEND, TokenTypeEnum.AAL })); 77 | ruleMap.put(TokenTypeEnum.GOTOSTART, Arrays.asList(new TokenTypeEnum[] { TokenTypeEnum.HASHTAG })); 78 | ruleMap.put(TokenTypeEnum.GOTOEND, 79 | Arrays.asList(new TokenTypeEnum[] { TokenTypeEnum.CMD, TokenTypeEnum.PRINT, TokenTypeEnum.AAL, 80 | TokenTypeEnum.IFSTART, TokenTypeEnum.HASHTAG, TokenTypeEnum.GOTOSTART, TokenTypeEnum.IFEND, 81 | TokenTypeEnum.END })); 82 | ruleMap.put(TokenTypeEnum.INPUT, Arrays.asList(new TokenTypeEnum[] { TokenTypeEnum.VEND })); 83 | ruleMap.put(TokenTypeEnum.FNAME, Arrays.asList(new TokenTypeEnum[] { TokenTypeEnum.PSTART })); 84 | ruleMap.put(TokenTypeEnum.END, Arrays.asList(new TokenTypeEnum[] {})); 85 | 86 | // Define data type for functions 87 | dataTypeFunction.put("sume", DataTypeEnum.ZAL); // Addition(+) 88 | dataTypeFunction.put("abziehung", DataTypeEnum.ZAL); // Subtraktion(-) 89 | dataTypeFunction.put("mahl", DataTypeEnum.ZAL); // Multiplikation(*) 90 | dataTypeFunction.put("teilung", DataTypeEnum.ZAL); // Division(/) 91 | dataTypeFunction.put("räst", DataTypeEnum.ZAL); // Modulo(%) 92 | dataTypeFunction.put("ismär", DataTypeEnum.ISSO); // Größer als(>) 93 | dataTypeFunction.put("isweniga", DataTypeEnum.ISSO); // Kleiner als(<) 94 | dataTypeFunction.put("same", DataTypeEnum.ISSO);// Gleich(==) 95 | dataTypeFunction.put("unt", DataTypeEnum.ISSO);// Und(&&) 96 | dataTypeFunction.put("ohr", DataTypeEnum.ISSO);// Oder(||) 97 | } 98 | 99 | public TreeNode parse(List tokenList) { 100 | 101 | String type = null; 102 | boolean vStart = false; 103 | boolean bistDu = false; 104 | 105 | // Check if the last token == END 106 | if (tokenList.get(tokenList.size() - 1).getType().equals(TokenTypeEnum.END)) { 107 | for (Token t : tokenList) { 108 | // Token != START 109 | if (!(t.getType().equals(TokenTypeEnum.START)) && !(rule.isEmpty())) { 110 | // Syntax check from ruleMap == ok 111 | if (rule.contains(t.getType())) { 112 | // Check type 113 | switch (t.getType()) { 114 | case TYPE: 115 | type = t.getContent(); 116 | break; 117 | case NAME: 118 | // Save variable name and type 119 | if (vStart) { 120 | setDataTypeVariable(t.getContent(), type, t.getLine()); 121 | } 122 | // Check after variable initialized for know variable and function names 123 | else { 124 | checkVariableFunction(t); 125 | } 126 | break; 127 | case VSTART: 128 | vStart = true; 129 | break; 130 | case VEND: 131 | vStart = false; 132 | type = null; 133 | break; 134 | case CONST_ISSO: 135 | case CONST_WORD: 136 | case CONST_ZAL: 137 | // Check data type for assignment after initialization 138 | if (!vStart && parent.getName().equals(TokenTypeEnum.ASSI) 139 | && !dataTypeVariable.get(parent.getParent().getLeft()) 140 | .equals(getDataTypeEnum(t.getType().getLabel(), 6))) { 141 | error(t, "Tipe", Arrays.asList(new TokenTypeEnum[] { 142 | getTokenTypeEnum(parent.getParent()), TokenTypeEnum.INPUT })); 143 | } 144 | // Test data type for assignment during initialization 145 | else if (vStart && !dataTypeVariable.get(parent.getParent().getLeft()) 146 | .equals(getDataTypeEnum(t.getType().getLabel(), 6))) { 147 | error(t, "Tipe", Arrays.asList(new TokenTypeEnum[] { 148 | getTokenTypeEnum(parent.getParent()), TokenTypeEnum.INPUT })); 149 | } 150 | break; 151 | case IFSTART: 152 | bistDu = true; 153 | break; 154 | default: 155 | break; 156 | } 157 | } 158 | // Syntax fail 159 | else { 160 | error(t, "Sintax", rule); 161 | } 162 | } 163 | // Token != START and first token 164 | else if (!(t.getType().equals(TokenTypeEnum.START)) && rule.isEmpty()) { 165 | error(t, "Sintax", Arrays.asList(new TokenTypeEnum[] { TokenTypeEnum.START })); 166 | } else { 167 | // TODO 168 | } 169 | 170 | // Add after IFSTART a PSTART token 171 | if (bistDu && t.getType().equals(TokenTypeEnum.IFSTART)) { 172 | Token hT = new Token(t.getLine(), TokenTypeEnum.PSTART); 173 | parseItem(t); 174 | parseItem(hT); 175 | rule = ruleMap.get(TokenTypeEnum.PSTART); 176 | } else if (bistDu && t.getType().equals(TokenTypeEnum.CONST_ISSO)) { 177 | Token hT = new Token(t.getLine(), TokenTypeEnum.PEND); 178 | parseItem(t); 179 | parseItem(hT); 180 | rule = ruleMap.get(TokenTypeEnum.PEND); 181 | bistDu = false; 182 | } // Add after PRINT a PSTART token 183 | else if (t.getType().equals(TokenTypeEnum.PRINT)) { 184 | Token hT = new Token(t.getLine(), TokenTypeEnum.PSTART); 185 | parseItem(t); 186 | parseItem(hT); 187 | rule = ruleMap.get(TokenTypeEnum.PSTART); 188 | } 189 | else{ 190 | parseItem(t); 191 | rule = ruleMap.get(t.getType()); 192 | } 193 | } 194 | } 195 | // Last token is != END 196 | else { 197 | error(tokenList.get(tokenList.size() - 1), "Sintax", 198 | Arrays.asList(new TokenTypeEnum[] { TokenTypeEnum.END })); 199 | } 200 | return root; 201 | } 202 | 203 | private DataTypeEnum getDataTypeEnum(String type, int anz) { 204 | return DataTypeEnum.valueOf(type.substring(anz).toUpperCase()); 205 | } 206 | 207 | private TokenTypeEnum getTokenTypeEnum(TreeNode node) { 208 | return TokenTypeEnum.valueOf("CONST_" + dataTypeVariable.get(node.getLeft()).toString()); 209 | } 210 | 211 | private void setDataTypeVariable(String name, String type, int line) { 212 | // check if variable name is already assigned 213 | if (!dataTypeVariable.containsKey(name)) { 214 | try { 215 | dataTypeVariable.put(name, DataTypeEnum.valueOf(type.toUpperCase())); 216 | } catch (IllegalArgumentException e) { 217 | logger.error("Datentipe nit bekannt! Du lauch!!! Schausd du Zeile " + line + ": \"" + type + "\""); 218 | System.exit(0); 219 | } 220 | } else { 221 | logger.error("Fehler voms doppelteng variablemameng her! Du lauch!!! Schausd du Zeile " + line + ": \"" 222 | + name + "\" gibd es bereits!"); 223 | System.exit(0); 224 | } 225 | } 226 | 227 | private void error(Token t, String message, List rule) { 228 | String error = new String(); 229 | for (TokenTypeEnum tte : rule) { 230 | error = error + tte.getLabel() + "|"; 231 | } 232 | if (t.getContent().isEmpty()) { 233 | logger.error("Fehler voms " + message + " her! Du lauch!!! Schausd du Zeile " + t.getLine() + ": Hab: " 234 | + t.getType().getLabel() + " --> Brauch: " + error.substring(0, (error.length() - 1))); 235 | } else { 236 | logger.error("Fehler voms " + message + " her! Du lauch!!! Schausd du Zeile " + t.getLine() + ": Hab: " 237 | + t.getContent() + " --> Brauch: " + error.substring(0, (error.length() - 1))); 238 | } 239 | System.exit(0); 240 | } 241 | 242 | private void checkVariableFunction(Token t) { 243 | // Variable not initialized 244 | if (!parent.getName().equals(TokenTypeEnum.CMD) && !dataTypeVariable.containsKey(t.getContent())) { 245 | logger.error("Variable unbekamd: " + t.getContent()); 246 | System.exit(0); 247 | } // Token before is CMD -> NAME is from a function && funktion name not unknown 248 | else if (parent.getName().equals(TokenTypeEnum.CMD) && !dataTypeFunction.containsKey(t.getContent())) { 249 | logger.error("Funktion unbekamd: " + t.getContent()); 250 | System.exit(0); 251 | } // Set token type to FNAME 252 | else if(parent.getName().equals(TokenTypeEnum.CMD) && dataTypeFunction.containsKey(t.getContent())) { 253 | t.setType(TokenTypeEnum.FNAME); 254 | rule = ruleMap.get(TokenTypeEnum.FNAME); 255 | } // Check data type CMD && NAME 256 | else if (parent.getName().equals(TokenTypeEnum.CMD) && !dataTypeFunction.get(t.getContent()) 257 | .equals(dataTypeVariable.get(parent.getParent().getParent().getLeft()))) { 258 | error(t, "Funktion Tipe", 259 | Arrays.asList(new TokenTypeEnum[] { getTokenTypeEnum(parent.getParent().getParent()) })); 260 | } 261 | 262 | } 263 | 264 | private void parseItem(Token t) { 265 | 266 | // Root node 267 | if (root == null) { 268 | root = new TreeNode(t.getType(), null); 269 | parent = root; 270 | } else if (t.getType().equals(TokenTypeEnum.INPUT)) { 271 | parent.setRight(new TreeNode(t.getType(), parent)); 272 | parent = parent.getRight(); 273 | parent.setLeft(dataTypeVariable.get(parent.getParent().getParent().getLeft().toString())); 274 | } else { 275 | parent.setRight(new TreeNode(t.getType(), parent)); 276 | parent = parent.getRight(); 277 | parent.setLeft(t.getContent()); 278 | } 279 | logger.debug(t.getLine() + ": \t" + t.getType() + "(" + t.getContent() + ")"); 280 | } 281 | } -------------------------------------------------------------------------------- /compiler/src/main/java/vong/piler/her/generator/Generator.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.generator; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | import org.apache.logging.log4j.LogManager; 9 | import org.apache.logging.log4j.Logger; 10 | 11 | import vong.piler.her.enums.DataTypeEnum; 12 | import vong.piler.her.enums.OperationEnum; 13 | import vong.piler.her.enums.TokenTypeEnum; 14 | import vong.piler.her.exceptions.GenerationsFails; 15 | import vong.piler.her.parser.TreeNode; 16 | 17 | public class Generator { 18 | 19 | private static Logger logger = LogManager.getLogger(Generator.class); 20 | private RegisterHandler registerHandler; 21 | private ByteCodeWriter writer; 22 | private int ifCounter; 23 | private List ifGenerated; 24 | private Map missingHashtags; 25 | private int tokenId; 26 | 27 | public Generator(String outputPath) { 28 | this.ifCounter = 0; 29 | this.ifGenerated = new ArrayList<>(); 30 | this.missingHashtags = new HashMap<>(); 31 | this.tokenId = 1; 32 | this.registerHandler = RegisterHandler.getInstance(); 33 | this.writer = new ByteCodeWriter(outputPath); 34 | } 35 | 36 | public TreeNode nextNode(TreeNode node) { 37 | tokenId++; 38 | return node.getRight(); 39 | } 40 | 41 | public void start(TreeNode root) throws GenerationsFails { 42 | if (!root.getName().equals(TokenTypeEnum.START)) { 43 | throw new GenerationsFails(root, tokenId); 44 | } 45 | TreeNode node = nextNode(root); 46 | while(!node.getName().equals(TokenTypeEnum.END)) { 47 | if (node.getName().equals(TokenTypeEnum.VSTART)) { 48 | node = vStart(node); 49 | } 50 | else { 51 | node = chooseNextStep(node); 52 | } 53 | } 54 | writer.eof(); 55 | } 56 | 57 | public TreeNode chooseNextStep(TreeNode node) throws GenerationsFails { 58 | switch(node.getName()) { 59 | case HASHTAG: 60 | return hashtag(node); 61 | case AAL: 62 | writer.addAAL(); 63 | return nextNode(node); 64 | case PRINT: 65 | return print(node); 66 | case NAME: 67 | return decVarName(node); 68 | case CMD: 69 | return cmd(node, null); 70 | case IFSTART: 71 | writer.addBlank("IF"+ ++ifCounter); 72 | TreeNode nextNode = ifStart(node); 73 | writer.addNOT(); 74 | writer.addCommand(OperationEnum.JMT); 75 | return nextNode; 76 | case IFEND: 77 | return ifend(node); 78 | case GOTOSTART: 79 | return gotoStart(node); 80 | default: 81 | throw new GenerationsFails(node, tokenId) ; 82 | } 83 | } 84 | 85 | public TreeNode vStart(TreeNode node) throws GenerationsFails { 86 | if (node.getRight().getName().equals(TokenTypeEnum.TYPE)) { 87 | return type(nextNode(node)); 88 | } 89 | else { 90 | throw new GenerationsFails(node, tokenId); 91 | } 92 | } 93 | 94 | private TreeNode type(TreeNode node) throws GenerationsFails { 95 | if (node.getRight().getName().equals(TokenTypeEnum.NAME)) { 96 | return decVarName(nextNode(node)); 97 | } 98 | else { 99 | throw new GenerationsFails(node, tokenId); 100 | } 101 | } 102 | 103 | private TreeNode decVarName(TreeNode node) throws GenerationsFails { 104 | if (node.getRight().getName().equals(TokenTypeEnum.ASSI)) { 105 | return assi(nextNode(node), node.getLeft().toString()); 106 | } 107 | else { 108 | throw new GenerationsFails(node, tokenId); 109 | } 110 | } 111 | 112 | private TreeNode assi(TreeNode node, String name) throws GenerationsFails { 113 | switch (node.getRight().getName()) { 114 | case CONST_ISSO: 115 | return constIsso(nextNode(node), name); 116 | case CONST_WORD: 117 | return constWord(nextNode(node), name); 118 | case CONST_ZAL: 119 | return constZal(nextNode(node), name); 120 | case INPUT: 121 | return input(nextNode(node), name); 122 | case NAME: 123 | return useVarName(nextNode(node), name); 124 | case CMD: 125 | return cmd(nextNode(node), name); 126 | default: 127 | throw new GenerationsFails(node, tokenId); 128 | } 129 | } 130 | 131 | private TreeNode constIsso(TreeNode node, String name) throws GenerationsFails { 132 | if (node.getRight().getName().equals(TokenTypeEnum.VEND)) { 133 | return vEnd(nextNode(node), name, new ValueModel((node.getLeft().equals("yup"))?true:false)); 134 | } 135 | else { 136 | throw new GenerationsFails(node, tokenId); 137 | } 138 | } 139 | 140 | private TreeNode constIsso(TreeNode node, String name, OperationEnum operation, List values) throws GenerationsFails { 141 | if (node.getRight().getName().equals(TokenTypeEnum.PNEXT)) { 142 | values.add(new ValueModel((node.getLeft().equals("yup"))?true:false)); 143 | return pNext(nextNode(node), name, operation, values); 144 | } 145 | else if (node.getRight().getName().equals(TokenTypeEnum.PEND)) { 146 | values.add(new ValueModel((node.getLeft().equals("yup"))?true:false)); 147 | return pEnd(nextNode(node), name, operation, values); 148 | } 149 | else { 150 | throw new GenerationsFails(node, tokenId); 151 | } 152 | } 153 | 154 | private TreeNode constWord(TreeNode node, String name) throws GenerationsFails { 155 | if (node.getRight().getName().equals(TokenTypeEnum.VEND)) { 156 | return vEnd(nextNode(node), name, new ValueModel(node.getLeft().toString(), false)); 157 | } 158 | else { 159 | throw new GenerationsFails(node, tokenId); 160 | } 161 | } 162 | 163 | private TreeNode constWord(TreeNode node, String name, OperationEnum operation, List values) throws GenerationsFails { 164 | if (node.getRight().getName().equals(TokenTypeEnum.PNEXT)) { 165 | values.add(new ValueModel(node.getLeft().toString(), false)); 166 | return pNext(nextNode(node), name, operation, values); 167 | } 168 | else if (node.getRight().getName().equals(TokenTypeEnum.PEND)) { 169 | values.add(new ValueModel(node.getLeft().toString(), false)); 170 | return pEnd(nextNode(node), name, operation, values); 171 | } 172 | else { 173 | throw new GenerationsFails(node, tokenId); 174 | } 175 | } 176 | 177 | private TreeNode constZal(TreeNode node, String name) throws GenerationsFails { 178 | if (node.getRight().getName().equals(TokenTypeEnum.VEND)) { 179 | return vEnd(nextNode(node), name, new ValueModel(Double.parseDouble(node.getLeft().toString()))); 180 | } 181 | else { 182 | throw new GenerationsFails(node, tokenId); 183 | } 184 | } 185 | 186 | private TreeNode constZal(TreeNode node, String name, OperationEnum operation, List values) throws GenerationsFails { 187 | if (node.getRight().getName().equals(TokenTypeEnum.PNEXT)) { 188 | values.add(new ValueModel(Double.parseDouble(node.getLeft().toString()))); 189 | return pNext(nextNode(node), name, operation, values); 190 | } 191 | else if (node.getRight().getName().equals(TokenTypeEnum.PEND)) { 192 | values.add(new ValueModel(Double.parseDouble(node.getLeft().toString()))); 193 | return pEnd(nextNode(node), name, operation, values); 194 | } 195 | else { 196 | throw new GenerationsFails(node, tokenId); 197 | } 198 | } 199 | 200 | private TreeNode input(TreeNode node, String name) throws GenerationsFails { 201 | if (node.getRight().getName().equals(TokenTypeEnum.VEND)) { 202 | if (node.getLeft() instanceof DataTypeEnum) { 203 | switch (((DataTypeEnum)node.getLeft())) { 204 | case ISSO: 205 | return vEnd(nextNode(node), name, OperationEnum.IIN); 206 | case WORD: 207 | return vEnd(nextNode(node), name, OperationEnum.WIN); 208 | case ZAL: 209 | return vEnd(nextNode(node), name, OperationEnum.ZIN); 210 | } 211 | } 212 | } 213 | throw new GenerationsFails(node, tokenId); 214 | } 215 | 216 | private TreeNode useVarName(TreeNode node, String name) throws GenerationsFails { 217 | if (node.getRight().getName().equals(TokenTypeEnum.VEND)) { 218 | return vEnd(nextNode(node), name, new ValueModel(node.getLeft().toString(), true)); 219 | } 220 | else { 221 | throw new GenerationsFails(node, tokenId); 222 | } 223 | } 224 | 225 | private TreeNode useVarName(TreeNode node, String name, OperationEnum operation, List values) throws GenerationsFails { 226 | if (node.getRight().getName().equals(TokenTypeEnum.PNEXT)) { 227 | values.add(new ValueModel(node.getLeft().toString(), true)); 228 | return pNext(nextNode(node), name, operation, values); 229 | } 230 | else if (node.getRight().getName().equals(TokenTypeEnum.PEND)) { 231 | values.add(new ValueModel(node.getLeft().toString(), true)); 232 | return pEnd(nextNode(node), name, operation, values); 233 | } 234 | else { 235 | throw new GenerationsFails(node, tokenId); 236 | } 237 | } 238 | 239 | private TreeNode vEnd(TreeNode node, String name, ValueModel value) throws GenerationsFails { 240 | int address = registerHandler.getVariableAddress(name); 241 | writer.addCommand(OperationEnum.PSA, address+""); 242 | writer.addCommandResolveAdresses(value.getOperation(), value.getValue()); 243 | writer.addCommand(OperationEnum.SAV); 244 | return nextNode(node); 245 | } 246 | 247 | private TreeNode vEnd(TreeNode node, String name, OperationEnum operation) throws GenerationsFails { 248 | int address = registerHandler.addVariable(name); 249 | writer.addCommand(OperationEnum.PSA, address+""); 250 | writer.addCommand(operation); 251 | return nextNode(node); 252 | } 253 | 254 | private TreeNode print(TreeNode node) throws GenerationsFails { 255 | if (node.getRight().getName().equals(TokenTypeEnum.PSTART)) { 256 | return pStart(nextNode(node), null, OperationEnum.PRT); 257 | } 258 | else { 259 | throw new GenerationsFails(node, tokenId); 260 | } 261 | } 262 | 263 | private TreeNode pStart(TreeNode node, String name, OperationEnum operation) throws GenerationsFails { 264 | List values = new ArrayList<>(); 265 | switch (node.getRight().getName()) { 266 | case CONST_ISSO: 267 | return constIsso(nextNode(node), name, operation, values); 268 | case CONST_WORD: 269 | return constWord(nextNode(node), name, operation, values); 270 | case CONST_ZAL: 271 | return constZal(nextNode(node), name, operation, values); 272 | case NAME: 273 | return useVarName(nextNode(node), name, operation, values); 274 | default: 275 | throw new GenerationsFails(node, tokenId); 276 | } 277 | } 278 | 279 | private TreeNode pNext(TreeNode node, String name, OperationEnum operation, List values) throws GenerationsFails { 280 | switch (node.getRight().getName()) { 281 | case CONST_ISSO: 282 | return constIsso(nextNode(node), name, operation, values); 283 | case CONST_WORD: 284 | return constWord(nextNode(node), name, operation, values); 285 | case CONST_ZAL: 286 | return constZal(nextNode(node), name, operation, values); 287 | case NAME: 288 | return useVarName(nextNode(node), name, operation, values); 289 | default: 290 | throw new GenerationsFails(node, tokenId); 291 | } 292 | } 293 | 294 | private TreeNode pEnd(TreeNode node, String name, OperationEnum operation, List values) throws GenerationsFails { 295 | if (name != null) { 296 | int address = registerHandler.getVariableAddress(name); 297 | writer.addCommand(OperationEnum.PSA, address+""); 298 | } 299 | switch(operation) { 300 | case PRT: 301 | PreDefinedFunction.generatePrint(operation, values, writer); 302 | break; 303 | case ADD: 304 | case SUB: 305 | case MUL: 306 | case DIV: 307 | case MOD: 308 | PreDefinedFunction.generateCalculations(operation, values, writer); 309 | break; 310 | case GTR: 311 | case LES: 312 | case EQL: 313 | PreDefinedFunction.generateComparator(operation, values, writer); 314 | break; 315 | case OHR: 316 | case AND: 317 | PreDefinedFunction.generateLogicOperator(operation, values, writer); 318 | break; 319 | default: 320 | throw new GenerationsFails(node, tokenId); 321 | } 322 | if (name != null) { 323 | writer.addCommand(OperationEnum.SAV); 324 | } 325 | return nextNode(node); 326 | } 327 | 328 | private TreeNode cmd(TreeNode node, String name) throws GenerationsFails { 329 | if (node.getRight().getName().equals(TokenTypeEnum.FNAME)) { 330 | return fName(nextNode(node), name); 331 | } 332 | else { 333 | throw new GenerationsFails(node, tokenId); 334 | } 335 | } 336 | 337 | private TreeNode fName(TreeNode node, String name) throws GenerationsFails { 338 | if (node.getRight().getName().equals(TokenTypeEnum.PSTART)) { 339 | OperationEnum operation; 340 | switch (node.getLeft().toString()) { 341 | case "sume": 342 | operation = OperationEnum.ADD; 343 | break; 344 | case "abziehung": 345 | operation = OperationEnum.SUB; 346 | break; 347 | case "mahl": 348 | operation = OperationEnum.MUL; 349 | break; 350 | case "teilung": 351 | operation = OperationEnum.DIV; 352 | break; 353 | case "räst": 354 | operation = OperationEnum.MOD; 355 | break; 356 | case "ismär": 357 | operation = OperationEnum.GTR; 358 | break; 359 | case "isweniga": 360 | operation = OperationEnum.LES; 361 | break; 362 | case "same": 363 | operation = OperationEnum.EQL; 364 | break; 365 | case "ohr": 366 | operation = OperationEnum.OHR; 367 | break; 368 | case "unt": 369 | operation = OperationEnum.AND; 370 | break; 371 | case "nit": 372 | operation = OperationEnum.NOT; 373 | break; 374 | default: 375 | throw new GenerationsFails(node, tokenId); 376 | } 377 | return pStart(nextNode(node), name, operation); 378 | } 379 | else { 380 | throw new GenerationsFails(node, tokenId); 381 | } 382 | } 383 | 384 | private TreeNode gotoStart(TreeNode node) throws GenerationsFails { 385 | if (node.getRight().getName().equals(TokenTypeEnum.HASHTAG)) { 386 | return hashtag(nextNode(node)); 387 | } 388 | else { 389 | throw new GenerationsFails(node, tokenId); 390 | } 391 | } 392 | 393 | private TreeNode hashtag(TreeNode node) throws GenerationsFails { 394 | if (node.getRight().getName().equals(TokenTypeEnum.GOTOEND)) { 395 | if (node.getParent().getName().equals(TokenTypeEnum.GOTOSTART)) { 396 | return gotoEnd(nextNode(node), node.getLeft().toString()); 397 | } 398 | else { 399 | throw new GenerationsFails(node, tokenId); 400 | } 401 | } 402 | else { 403 | String name = node.getLeft().toString(); 404 | registerHandler.addJumpMarkerIfNotExists(name); 405 | if (missingHashtags.containsKey(name)) { 406 | String address = registerHandler.getDataAddress(name); 407 | writer.fillBlankAddress(name, address, missingHashtags.get(name)); 408 | } 409 | return nextNode(node); 410 | } 411 | } 412 | 413 | private TreeNode gotoEnd(TreeNode node, String name) throws GenerationsFails { 414 | String address = registerHandler.getDataAddress(name); 415 | if (address == null) { 416 | throw new GenerationsFails(node, tokenId); 417 | } 418 | try{ 419 | writer.addCommand(OperationEnum.PSA, Integer.parseInt(address) + ""); 420 | } catch(NumberFormatException e) { 421 | writer.addBlank(address); 422 | missingHashtags.put(address, writer.getLineNumber()-1); 423 | } 424 | writer.addCommand(OperationEnum.JMP); 425 | return nextNode(node); 426 | } 427 | 428 | private TreeNode ifStart(TreeNode node) throws GenerationsFails { 429 | if (node.getRight().getName().equals(TokenTypeEnum.PSTART)) { 430 | return pStart(nextNode(node), null, OperationEnum.EQL); 431 | } 432 | else { 433 | throw new GenerationsFails(node, tokenId); 434 | } 435 | } 436 | 437 | private TreeNode ifend(TreeNode node) { 438 | int free = findFreeIfCounter(); 439 | registerHandler.addJumpMarkerIfNotExists("IF" + free); 440 | writer.fillBlankAddress("IF"+free, registerHandler.getDataAddress("IF"+free), 0); 441 | ifGenerated.add(free); 442 | return nextNode(node); 443 | } 444 | 445 | private int findFreeIfCounter() { 446 | for (int i=ifCounter; i>0; i--) { 447 | if (!ifGenerated.contains(i)) 448 | return i; 449 | } 450 | return -1; 451 | } 452 | } 453 | -------------------------------------------------------------------------------- /runtime/src/main/java/vong/piler/her/vongruntime/virtualmachine/Steakmachine.java: -------------------------------------------------------------------------------- 1 | package vong.piler.her.vongruntime.virtualmachine; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.File; 5 | import java.io.FileReader; 6 | import java.io.IOException; 7 | import java.io.InputStreamReader; 8 | import java.io.PrintStream; 9 | import java.util.InputMismatchException; 10 | import java.util.Scanner; 11 | import java.util.Stack; 12 | 13 | import vong.piler.her.vongruntime.virtualmachine.model.StackElement.Type; 14 | import vong.piler.her.vongruntime.exception.EmptyLineException; 15 | import vong.piler.her.vongruntime.exception.InstructionPointerOutOfBoundsException; 16 | import vong.piler.her.vongruntime.exception.ReadEmptyRegisterException; 17 | import vong.piler.her.vongruntime.exception.UnknownCommandException; 18 | import vong.piler.her.vongruntime.exception.UnsupportedNumberofArgumentsException; 19 | import vong.piler.her.vongruntime.util.StringUtils; 20 | import vong.piler.her.vongruntime.virtualmachine.model.Command; 21 | import vong.piler.her.vongruntime.virtualmachine.model.OperationEnum; 22 | import vong.piler.her.vongruntime.virtualmachine.model.StackElement; 23 | 24 | public class Steakmachine { 25 | 26 | private PrintStream standardOut = System.out; 27 | private PrintStream errorOut = System.err; 28 | private boolean debugOutput = false; 29 | private boolean readAssembler = false; 30 | 31 | private static final int PROGRAM_MEMORY_SIZE = 100; 32 | private static final int CODE_MEMORY_SIZE = 10000; 33 | private Stack stack; 34 | private StackElement[] programmMemory; 35 | private String[] codeMemory; 36 | 37 | private int instructionPointer; 38 | private boolean running; 39 | private Scanner scanner = new Scanner(System.in); 40 | 41 | 42 | public static int getProgramMemorySize() { 43 | return PROGRAM_MEMORY_SIZE; 44 | } 45 | 46 | 47 | 48 | public static int getCodeMemorySize() { 49 | return CODE_MEMORY_SIZE; 50 | } 51 | 52 | 53 | 54 | 55 | 56 | public boolean isDebugOutput() { 57 | return debugOutput; 58 | } 59 | 60 | 61 | 62 | public void setDebugOutput(boolean debugOutput) { 63 | this.debugOutput = debugOutput; 64 | } 65 | 66 | 67 | 68 | public boolean isReadAssembler() { 69 | return readAssembler; 70 | } 71 | 72 | 73 | 74 | public void setReadAssembler(boolean readAssembler) { 75 | this.readAssembler = readAssembler; 76 | } 77 | 78 | 79 | 80 | public PrintStream getStandardOut() { 81 | return standardOut; 82 | } 83 | 84 | 85 | 86 | public void setStandardOut(PrintStream standardOut) { 87 | this.standardOut = standardOut; 88 | } 89 | 90 | 91 | 92 | public PrintStream getErrorOut() { 93 | return errorOut; 94 | } 95 | 96 | 97 | 98 | public void setErrorOut(PrintStream errorOut) { 99 | this.errorOut = errorOut; 100 | } 101 | 102 | 103 | 104 | public void load(File file) { 105 | BufferedReader br = null; 106 | try { 107 | br = new BufferedReader(new FileReader(file)); 108 | String line; 109 | int i = 0; 110 | while ((line = br.readLine()) != null) { 111 | codeMemory[i] = line; 112 | i++; 113 | } 114 | } catch (IOException e) { 115 | printErrorOutput("Could not read file at: " + file.getAbsolutePath()); 116 | } catch (ArrayIndexOutOfBoundsException e) { 117 | printErrorOutput("Programmcode doesn't fit in programmregister which supports " + CODE_MEMORY_SIZE + " commands"); 118 | } 119 | } 120 | 121 | 122 | 123 | public void init() { 124 | stack = new Stack(); 125 | codeMemory = new String[CODE_MEMORY_SIZE]; 126 | programmMemory = new StackElement[PROGRAM_MEMORY_SIZE]; 127 | instructionPointer = 0; 128 | } 129 | 130 | public void run() { 131 | running = true; 132 | while (running) { 133 | try { 134 | String rawCommand = readCommand(); 135 | instructionPointer++; 136 | Command command = decodeCommand(rawCommand); 137 | executeCommand(command); 138 | } catch (UnsupportedNumberofArgumentsException e) { 139 | printErrorOutput("Wrong number of arguments submitted for operation"); 140 | running = false; 141 | } catch (InstructionPointerOutOfBoundsException e) { 142 | printErrorOutput("Instruction-pointer ran out of bounds"); 143 | running = false; 144 | } catch (UnknownCommandException e) { 145 | printErrorOutput(String.format("Encountered unknown command: %s", e.getMessage())); 146 | running = false; 147 | } catch (EmptyLineException e) { 148 | printErrorOutput("Encountered empty line in codefile"); 149 | running = false; 150 | } catch (ReadEmptyRegisterException e) { 151 | printErrorOutput("instruction-pointer at empty register"); 152 | running = false; 153 | } 154 | } 155 | scanner.close(); 156 | 157 | } 158 | 159 | private String readCommand() throws InstructionPointerOutOfBoundsException, ReadEmptyRegisterException { 160 | try { 161 | String rawCommand = codeMemory[instructionPointer]; 162 | if(rawCommand == null) { 163 | throw new ReadEmptyRegisterException(); 164 | } 165 | printDebugOutput(String.format("Read raw command: %s", rawCommand)); 166 | return rawCommand; 167 | } catch (ArrayIndexOutOfBoundsException e) { 168 | throw new InstructionPointerOutOfBoundsException(); 169 | } 170 | 171 | } 172 | 173 | private Command decodeCommand(String rawCommand) throws UnsupportedNumberofArgumentsException, EmptyLineException, UnknownCommandException { 174 | Command command = new Command(); 175 | 176 | String[] commandParts = rawCommand.split(" "); 177 | if(commandParts.length == 0) { 178 | throw new EmptyLineException(); 179 | } 180 | 181 | 182 | if(readAssembler) { 183 | command.setOpCode(OperationEnum.valueOf(commandParts[0])); 184 | }else { 185 | try { 186 | command.setOpCode(OperationEnum.values()[Integer.parseInt(commandParts[0])]); 187 | }catch(ArrayIndexOutOfBoundsException e) { 188 | throw new UnknownCommandException(commandParts[0]); 189 | } 190 | } 191 | 192 | //TODO refactor design 193 | //Hack to make words with whitespace work 194 | if(command.getOpCode() == OperationEnum.PSW) { 195 | String word = rawCommand.substring(rawCommand.indexOf(" ") + 1); 196 | command.setFirstParam(word); 197 | return command; 198 | } 199 | 200 | 201 | if(commandParts.length == 2) { 202 | command.setFirstParam(commandParts[1]); 203 | }else if(commandParts.length != 1){ 204 | throw new UnsupportedNumberofArgumentsException(); 205 | } 206 | printDebugOutput(String.format("Decoded command to: %s", command.toString())); 207 | return command; 208 | } 209 | 210 | 211 | private void executeCommand(Command command) throws UnknownCommandException { 212 | switch(command.getOpCode()) { 213 | case PSZ: 214 | psz(command.getFirstParam()); 215 | break; 216 | case ADD: 217 | add(); 218 | break; 219 | case PRT: 220 | prt(); 221 | break; 222 | case AAL: 223 | aal(); 224 | break; 225 | case AND: 226 | and(); 227 | break; 228 | case DIV: 229 | div(); 230 | break; 231 | case END: 232 | end(); 233 | break; 234 | case GTR: 235 | gtr(); 236 | break; 237 | case JMP: 238 | jmp(); 239 | break; 240 | case JMT: 241 | jmt(); 242 | break; 243 | case LES: 244 | les(); 245 | break; 246 | case MOD: 247 | mod(); 248 | break; 249 | case MUL: 250 | mul(); 251 | break; 252 | case OHR: 253 | ohr(); 254 | break; 255 | case PSA: 256 | psa(command.getFirstParam()); 257 | break; 258 | case PSI: 259 | psi(command.getFirstParam()); 260 | break; 261 | case SUB: 262 | sub(); 263 | break; 264 | case PSW: 265 | psw(command.getFirstParam()); 266 | break; 267 | case EQL: 268 | eql(); 269 | break; 270 | case NOT: 271 | not(); 272 | break; 273 | case SAV: 274 | sav(); 275 | break; 276 | case NWL: 277 | nwl(); 278 | break; 279 | case IIN: 280 | iin(); 281 | break; 282 | case WIN: 283 | win(); 284 | break; 285 | case ZIN: 286 | zin(); 287 | break; 288 | default: 289 | throw new UnknownCommandException(command.getOpCode().toString()); 290 | } 291 | printDebugOutput(String.format("Executed command: %s",command.getOpCode().toString())); 292 | printDebugOutput(String.format("Stack: %s", stack.toString())); 293 | printDebugOutput(String.format("Registers: %s", printRegisters())); 294 | } 295 | 296 | 297 | 298 | private void zin() { 299 | int address = popAddress(); 300 | double zal = readZal(); 301 | StackElement element = new StackElement(Type.ZAL, zal); 302 | programmMemory[address] = element; 303 | } 304 | 305 | 306 | 307 | private double readZal() { 308 | double zal = 0; 309 | boolean read = false; 310 | while(!read) { 311 | try { 312 | zal = scanner.nextDouble(); 313 | read = true; 314 | }catch(InputMismatchException e) { 315 | System.out.println("Du must 1 Zal 1geben du Lauch!!!"); 316 | scanner.nextLine(); 317 | } 318 | } 319 | return zal; 320 | } 321 | 322 | private String readWord() { 323 | String word = scanner.nextLine(); 324 | return word; 325 | } 326 | 327 | private boolean readIsso() { 328 | boolean isso = false; 329 | boolean read = false; 330 | while(!read) { 331 | String input = scanner.nextLine(); 332 | if(input.equals("yup")) { 333 | isso = true; 334 | read = true; 335 | }else if(input.equals("nope")) { 336 | isso = false; 337 | read = true; 338 | }else { 339 | System.out.println("Du must 1 isso 1geben du Lauch!!"); 340 | } 341 | } 342 | return isso; 343 | } 344 | 345 | 346 | private void win() { 347 | int address = popAddress(); 348 | String word = readWord(); 349 | StackElement element = new StackElement(Type.WORD, word); 350 | programmMemory[address] = element; 351 | } 352 | 353 | 354 | 355 | private void iin() { 356 | int address = popAddress(); 357 | boolean isso = readIsso(); 358 | StackElement element = new StackElement(Type.ISSO, isso); 359 | programmMemory[address] = element; 360 | } 361 | 362 | 363 | 364 | private void nwl() { 365 | String newLine = System.getProperty("line.separator"); 366 | System.out.print(newLine); 367 | } 368 | 369 | private String printRegisters() { 370 | boolean empty = true; 371 | String registers = "["; 372 | for(int i = 0; i< PROGRAM_MEMORY_SIZE; i++) { 373 | StackElement element = programmMemory[i]; 374 | if(element != null) { 375 | empty = false; 376 | String register = String.format("%d -> %s, ", i, element.toString()); 377 | registers = registers + register; 378 | } 379 | } 380 | if(!empty) { 381 | //remove last " ," from string 382 | registers = StringUtils.removeLastCharacters(registers, 2); 383 | } 384 | registers = registers + "]"; 385 | return registers; 386 | } 387 | 388 | private void sav() { 389 | StackElement element = stack.pop(); 390 | if(element.getType() == Type.ADDRESS) { 391 | element = programmMemory[(int) element.getValue()]; 392 | } 393 | int address = popAddress(); 394 | programmMemory[address] = element; 395 | } 396 | 397 | private void not() { 398 | boolean isso = popIsso(); 399 | isso = !isso; 400 | pushIsso(isso); 401 | } 402 | 403 | private void eql() { 404 | StackElement arg1 = stack.pop(); 405 | StackElement arg2 = stack.pop(); 406 | 407 | if(arg1.getType() == Type.ADDRESS) { 408 | arg1 = programmMemory[(int) arg1.getValue()]; 409 | } 410 | 411 | if(arg2.getType() == Type.ADDRESS) { 412 | arg2 = programmMemory[(int) arg2.getValue()]; 413 | } 414 | 415 | if(arg1.equals(arg2)) { 416 | pushIsso(true); 417 | }else { 418 | pushIsso(false); 419 | } 420 | } 421 | 422 | private void psw(String word) { 423 | pushWord(word); 424 | } 425 | 426 | private void sub() { 427 | double a = popZal(); 428 | double b = popZal(); 429 | double result = b - a; 430 | pushZal(result); 431 | } 432 | 433 | private void mod() { 434 | double a = popZal(); 435 | double b = popZal(); 436 | double result = b % a; 437 | pushZal(result); 438 | 439 | } 440 | 441 | private void les() { 442 | double a = popZal(); 443 | double b = popZal(); 444 | boolean result = b < a; 445 | pushIsso(result); 446 | 447 | } 448 | 449 | private void jmt() { 450 | boolean isso = popIsso(); 451 | int address = popAddress(); 452 | if (isso) { 453 | instructionPointer = address; 454 | } 455 | } 456 | 457 | private void jmp() { 458 | int address = popAddress(); 459 | instructionPointer = address; 460 | } 461 | 462 | private void ohr() { 463 | boolean a = popIsso(); 464 | boolean b = popIsso(); 465 | boolean result = b || a; 466 | pushIsso(result); 467 | 468 | } 469 | 470 | private void psa(String firstParam) { 471 | pushAddress(Integer.parseInt(firstParam)); 472 | } 473 | 474 | private void psi(String firstParam) { 475 | pushIsso(parseIsso(firstParam)); 476 | 477 | } 478 | 479 | private boolean parseIsso(String firstParam) { 480 | int isso = Integer.parseInt(firstParam); 481 | return isso != 0 ? true : false; 482 | } 483 | 484 | private void gtr() { 485 | double a = popZal(); 486 | double b = popZal(); 487 | boolean result = b > a; 488 | pushIsso(result); 489 | 490 | } 491 | 492 | private void div() { 493 | double a = popZal(); 494 | double b = popZal(); 495 | double result = b / a; 496 | pushZal(result); 497 | } 498 | 499 | private void and() { 500 | boolean a = popIsso(); 501 | boolean b = popIsso(); 502 | boolean result = b && a; 503 | pushIsso(result); 504 | } 505 | 506 | private void aal() { 507 | printOutput("Halo I bims 1 aal vong Halo Wörlt her"); 508 | } 509 | 510 | private void end() { 511 | running = false; 512 | printDebugOutput("Programm exited without error"); 513 | } 514 | 515 | private void mul() { 516 | double a = popZal(); 517 | double b = popZal(); 518 | pushZal(b * a); 519 | } 520 | 521 | private void psz(String arg) { 522 | pushZal(Double.parseDouble(arg)); 523 | } 524 | 525 | private void prt() { 526 | StackElement element = stack.pop(); 527 | String out = ""; 528 | if(debugOutput) { 529 | out = element.toString(); 530 | if (element.getType() == Type.ADDRESS) { 531 | int address = (int) element.getValue(); 532 | StackElement global = programmMemory[address]; 533 | out = out + " -> " + global.toString(); 534 | } 535 | }else { 536 | if (element.getType() == Type.ADDRESS) { 537 | int address = (int) element.getValue(); 538 | element = programmMemory[address]; 539 | } 540 | out = element.print(); 541 | } 542 | 543 | printOutput(out); 544 | } 545 | 546 | private void add() { 547 | double a = popZal(); 548 | double b = popZal(); 549 | double result = b + a; 550 | pushZal(result); 551 | } 552 | 553 | private void pushZal(double zal) { 554 | StackElement element = new StackElement(Type.ZAL, zal); 555 | stack.push(element); 556 | } 557 | 558 | private void pushAddress(int address) { 559 | StackElement element = new StackElement(Type.ADDRESS, address); 560 | stack.push(element); 561 | } 562 | 563 | private void pushIsso(boolean isso) { 564 | StackElement element = new StackElement(Type.ISSO, isso); 565 | stack.push(element); 566 | } 567 | 568 | private void pushWord(String word) { 569 | StackElement element = new StackElement(Type.WORD, word); 570 | stack.push(element); 571 | } 572 | 573 | private double popZal() { 574 | StackElement element = stack.pop(); 575 | double zal; 576 | if (element.getType() == Type.ADDRESS) { 577 | zal = loadZal((int) element.getValue()); 578 | } else { 579 | zal = (double) element.getValue(); 580 | } 581 | return zal; 582 | } 583 | 584 | private int popAddress() { 585 | StackElement element = stack.pop(); 586 | int address = (int) element.getValue(); 587 | return address; 588 | } 589 | 590 | private boolean popIsso() { 591 | StackElement element = stack.pop(); 592 | boolean isso; 593 | if (element.getType() == Type.ADDRESS) { 594 | isso = loadIsso((int) element.getValue()); 595 | } else { 596 | isso = (boolean) element.getValue(); 597 | } 598 | return isso; 599 | } 600 | 601 | private String popWord() { 602 | StackElement element = stack.pop(); 603 | String word; 604 | if (element.getType() == Type.ADDRESS) { 605 | word = loadWord((int) element.getValue()); 606 | } else { 607 | word = (String) element.getValue(); 608 | } 609 | return word; 610 | } 611 | 612 | private double loadZal(int address) { 613 | double zal = (double) programmMemory[address].getValue(); 614 | return zal; 615 | } 616 | 617 | private boolean loadIsso(int address) { 618 | boolean isso = (boolean) programmMemory[address].getValue(); 619 | return isso; 620 | } 621 | 622 | private String loadWord(int address) { 623 | String word = (String) programmMemory[address].getValue(); 624 | return word; 625 | } 626 | 627 | private void printErrorOutput(String error) { 628 | errorOut.println(error); 629 | } 630 | 631 | private void printOutput(String message) { 632 | standardOut.print(message); 633 | } 634 | 635 | private void printDebugOutput(String message) { 636 | if(debugOutput) { 637 | standardOut.println(message); 638 | } 639 | } 640 | } 641 | --------------------------------------------------------------------------------