|
| 1 | +package parser; |
| 2 | + |
| 3 | +import java.io.PrintWriter; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.HashMap; |
| 6 | +import structure.Evaluable; |
| 7 | +import structure.Scenario; |
| 8 | + |
| 9 | +/** |
| 10 | +<b> |
| 11 | +Purpose: assembles the parts of a configuration file for simulating.<br> |
| 12 | +Programmer: Gabriel Toban Harris<br> |
| 13 | +Date: 2021-08-04 |
| 14 | +</b> |
| 15 | +*/ |
| 16 | + |
| 17 | +//TODO: figure out how to effectively handle VERBOSE, as would like to not recheck it, maybe just use lambda as function pointer. |
| 18 | +public class Tree_Assembler<T, U> |
| 19 | +{ |
| 20 | + /** |
| 21 | + * Determines whether to output files, true for make files and false for no file creation. |
| 22 | + */ |
| 23 | + public final boolean VERBOSE; |
| 24 | + |
| 25 | + /** |
| 26 | + * Derivation of the input. |
| 27 | + */ |
| 28 | + private StringBuilder derivation; |
| 29 | + |
| 30 | + /** |
| 31 | + * stack like structure used by {@link Tree_Assembler#parse(Token)} |
| 32 | + */ |
| 33 | + private ArrayList<Semantic_Actions> semantic_stack; |
| 34 | + |
| 35 | + /** |
| 36 | + * Represents stack like structure for building the {@link Scenario} and deck list, used by {@link Tree_Assembler#parse(Token)} |
| 37 | + */ |
| 38 | + private ArrayList<Evaluable<U>> syntactical_stack; |
| 39 | + |
| 40 | + /** |
| 41 | + * Main deck which the hand will be generated from. |
| 42 | + */ |
| 43 | + private final ArrayList<T> DECK; |
| 44 | + |
| 45 | + /** |
| 46 | + * Stores the generated scenarios. |
| 47 | + */ |
| 48 | + private final HashMap<String, Scenario<U>> FOREST; |
| 49 | + |
| 50 | + /** |
| 51 | + * file to output syntactical errors to. |
| 52 | + */ |
| 53 | + private PrintWriter syntactical_error_output; |
| 54 | + |
| 55 | + /** |
| 56 | + * file to output derivation |
| 57 | + */ |
| 58 | + private PrintWriter syntactical_derivation_output; |
| 59 | + |
| 60 | + /** |
| 61 | + * Default constructor. |
| 62 | + */ |
| 63 | + public Tree_Assembler() |
| 64 | + { |
| 65 | + this.VERBOSE = false; |
| 66 | + this.DECK = new ArrayList<T>(40); //predicted average deck size |
| 67 | + this.FOREST = new HashMap<String, Scenario<U>>(10); //predicted average number of scenarios |
| 68 | + this.finish_construction(); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Parameterized constructor. Which is implicitly setting {@link #VERBOSE} to true. |
| 73 | + * |
| 74 | + * @param EXPECTED_DECK_SIZE helps with memory management, but can be wrong |
| 75 | + * @param EXPECTED_SCENARIO_COUNT helps with memory management, but can be wrong |
| 76 | + * @param SYNTACTICAL_ERROR_OUTPUT {@link #syntactical_error_output} |
| 77 | + * @param SYNTACTICAL_DERIVATION_OUTPUT {@link #syntactical_derivation_output} |
| 78 | + */ |
| 79 | + public Tree_Assembler(final int EXPECTED_DECK_SIZE, final int EXPECTED_SCENARIO_COUNT, final PrintWriter SYNTACTICAL_ERROR_OUTPUT, final PrintWriter SYNTACTICAL_DERIVATION_OUTPUT) |
| 80 | + { |
| 81 | + this.VERBOSE = true; |
| 82 | + this.DECK = new ArrayList<T>(EXPECTED_DECK_SIZE); |
| 83 | + this.FOREST = new HashMap<String, Scenario<U>>(EXPECTED_SCENARIO_COUNT); |
| 84 | + this.derivation = new StringBuilder(Semantic_Actions.START.name()); |
| 85 | + this.syntactical_error_output = SYNTACTICAL_ERROR_OUTPUT; |
| 86 | + this.syntactical_derivation_output = SYNTACTICAL_DERIVATION_OUTPUT; |
| 87 | + this.finish_construction(); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * True output of this object. |
| 92 | + * |
| 93 | + * @return culmination of this class, destroy the object afterwards |
| 94 | + */ |
| 95 | + public Simulatation<T, U> creat_result() |
| 96 | + { |
| 97 | + return new Simulatation<T, U>(this.DECK, this.FOREST); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Method for parsing tokens into an abstract syntax tree. Uses all data members. |
| 102 | + * Make sure to call {@link #finish_semantic_stack} once all the {@link Token} are are parsed. |
| 103 | + * @param INPUT current top token being looked at |
| 104 | + * @throws EmptySemanticStackException when internal semantic_stack is empty yet there is another token to parse. |
| 105 | + * @throws TerminalMatchException {@link #match_subroutine} |
| 106 | + */ |
| 107 | + public void parse(final Token INPUT) //throws EmptySemanticStackException, TerminalMatchException |
| 108 | + { |
| 109 | + //TODO: write |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Centralize shared constructor code. Should only be called once by the constructor and never again. |
| 114 | + */ |
| 115 | + private void finish_construction() |
| 116 | + { |
| 117 | + this.semantic_stack = new ArrayList<Semantic_Actions>(); |
| 118 | + this.semantic_stack.add(Semantic_Actions.START); // starting symbol |
| 119 | + this.syntactical_stack = new ArrayList<Evaluable<U>>(); |
| 120 | + } |
| 121 | +} |
0 commit comments