|
| 1 | +/** |
| 2 | + * The MIT License |
| 3 | + * Copyright (c) 2014 Ilkka Seppälä |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + * THE SOFTWARE. |
| 22 | + */ |
| 23 | +package com.iluwatar.bytecode; |
| 24 | + |
| 25 | +import com.iluwatar.bytecode.util.InstructionConverterUtil; |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | + |
| 29 | +/** |
| 30 | + * The intention of Bytecode pattern is to give behavior the flexibility of data by encoding it as instructions |
| 31 | + * for a virtual machine. |
| 32 | + * An instruction set defines the low-level operations that can be performed. A series of instructions is encoded as |
| 33 | + * a sequence of bytes. A virtual machine executes these instructions one at a time, |
| 34 | + * using a stack for intermediate values. By combining instructions, complex high-level behavior can be defined. |
| 35 | + * |
| 36 | + * This pattern should be used when there is a need to define high number of behaviours and implementation engine |
| 37 | + * is not a good choice because |
| 38 | + * It is too lowe level |
| 39 | + * Iterating on it takes too long due to slow compile times or other tooling issues. |
| 40 | + * It has too much trust. If you want to ensure the behavior being defined can’t break the game, |
| 41 | + * you need to sandbox it from the rest of the codebase. |
| 42 | + * |
| 43 | + */ |
| 44 | +public class App { |
| 45 | + private static final Logger LOGGER = LoggerFactory.getLogger(App.class); |
| 46 | + |
| 47 | + /** |
| 48 | + * Main app method |
| 49 | + * @param args command line args |
| 50 | + */ |
| 51 | + public static void main(String[] args) { |
| 52 | + VirtualMachine vm = new VirtualMachine(); |
| 53 | + |
| 54 | + Wizard wizard = new Wizard(); |
| 55 | + wizard.setHealth(45); |
| 56 | + wizard.setAgility(7); |
| 57 | + wizard.setWisdom(11); |
| 58 | + vm.getWizards()[0] = wizard; |
| 59 | + |
| 60 | + interpretInstruction("LITERAL 0", vm); |
| 61 | + interpretInstruction( "LITERAL 0", vm); |
| 62 | + interpretInstruction( "GET_HEALTH", vm); |
| 63 | + interpretInstruction( "LITERAL 0", vm); |
| 64 | + interpretInstruction( "GET_AGILITY", vm); |
| 65 | + interpretInstruction( "LITERAL 0", vm); |
| 66 | + interpretInstruction( "GET_WISDOM ", vm); |
| 67 | + interpretInstruction( "ADD", vm); |
| 68 | + interpretInstruction( "LITERAL 2", vm); |
| 69 | + interpretInstruction( "DIVIDE", vm); |
| 70 | + interpretInstruction( "ADD", vm); |
| 71 | + interpretInstruction( "SET_HEALTH", vm); |
| 72 | + } |
| 73 | + |
| 74 | + private static void interpretInstruction(String instruction, VirtualMachine vm) { |
| 75 | + InstructionConverterUtil converter = new InstructionConverterUtil(); |
| 76 | + vm.execute(converter.convertToByteCode(instruction)); |
| 77 | + LOGGER.info(instruction + String.format("%" + (12 - instruction.length()) + "s", "" ) + vm.getStack()); |
| 78 | + } |
| 79 | +} |
0 commit comments