1 |
2 |
alfik |
/*
|
2 |
|
|
* Copyright (c) 2014, Aleksander Osman
|
3 |
|
|
* All rights reserved.
|
4 |
|
|
*
|
5 |
|
|
* Redistribution and use in source and binary forms, with or without
|
6 |
|
|
* modification, are permitted provided that the following conditions are met:
|
7 |
|
|
*
|
8 |
|
|
* * Redistributions of source code must retain the above copyright notice, this
|
9 |
|
|
* list of conditions and the following disclaimer.
|
10 |
|
|
*
|
11 |
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
12 |
|
|
* this list of conditions and the following disclaimer in the documentation
|
13 |
|
|
* and/or other materials provided with the distribution.
|
14 |
|
|
*
|
15 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
16 |
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
17 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
18 |
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
19 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
20 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
21 |
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
22 |
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
23 |
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
24 |
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
25 |
|
|
*/
|
26 |
|
|
|
27 |
|
|
package ao486;
|
28 |
|
|
|
29 |
|
|
import java.util.HashMap;
|
30 |
|
|
import java.util.HashSet;
|
31 |
|
|
|
32 |
|
|
public class ParseDefine extends Parse {
|
33 |
|
|
|
34 |
|
|
ParseDefine(HashMap<String, String> defines, HashSet<Integer> command_values) {
|
35 |
|
|
this.command_values = command_values;
|
36 |
|
|
this.defines = defines;
|
37 |
|
|
}
|
38 |
|
|
|
39 |
|
|
private HashSet<Integer> command_values;
|
40 |
|
|
private HashMap<String, String> defines;
|
41 |
|
|
|
42 |
|
|
int find_mod(HashSet<Integer> command_values, int mod) {
|
43 |
|
|
int index = 0;
|
44 |
|
|
|
45 |
|
|
while(true) {
|
46 |
|
|
boolean free = true;
|
47 |
|
|
for(int i=0; i<mod; i++) if(command_values.contains(index + i)) free = false;
|
48 |
|
|
if(free) break;
|
49 |
|
|
|
50 |
|
|
index += mod;
|
51 |
|
|
}
|
52 |
|
|
for(int i=0; i<mod; i++) command_values.add(index + i);
|
53 |
|
|
|
54 |
|
|
//System.out.println("index: " + index + ", mod: " + mod);
|
55 |
|
|
|
56 |
|
|
return index;
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
@Override
|
60 |
|
|
void parse(String section) throws Exception {
|
61 |
|
|
|
62 |
|
|
HashSet<String> accepted_autogen = new HashSet<>();
|
63 |
|
|
accepted_autogen.add("#AUTOGEN_NEXT_CMD");
|
64 |
|
|
accepted_autogen.add("#AUTOGEN_NEXT_CMD_MOD4");
|
65 |
|
|
accepted_autogen.add("#AUTOGEN_NEXT_CMD_LIKE_PREV");
|
66 |
|
|
accepted_autogen.add("#AUTOGEN_NEXT_CMD_MOD2");
|
67 |
|
|
accepted_autogen.add("#AUTOGEN_NEXT_CMD_MOD8");
|
68 |
|
|
|
69 |
|
|
int prev_index = -1;
|
70 |
|
|
|
71 |
|
|
String lines[] = section.split("\\n");
|
72 |
|
|
for(String line : lines) {
|
73 |
|
|
String parts[] = line.split("\\s+");
|
74 |
|
|
|
75 |
|
|
if(parts.length != 3) throw new Exception("Invalid defines line: " + line);
|
76 |
|
|
if(parts[0].equals("`define") == false) throw new Exception("Invalid defines line: " + line);
|
77 |
|
|
if(parts[2].startsWith("#AUTOGEN") && accepted_autogen.contains(parts[2]) == false) throw new Exception("Unknown #AUTOGEN: " + line);
|
78 |
|
|
|
79 |
|
|
if(parts[2].equals("#AUTOGEN_NEXT_CMD_MOD8")) {
|
80 |
|
|
prev_index = find_mod(command_values, 8);
|
81 |
|
|
parts[2] = "7'd" + prev_index;
|
82 |
|
|
}
|
83 |
|
|
if(parts[2].equals("#AUTOGEN_NEXT_CMD_MOD4")) {
|
84 |
|
|
prev_index = find_mod(command_values, 4);
|
85 |
|
|
parts[2] = "7'd" + prev_index;
|
86 |
|
|
}
|
87 |
|
|
if(parts[2].equals("#AUTOGEN_NEXT_CMD_MOD2")) {
|
88 |
|
|
prev_index = find_mod(command_values, 2);
|
89 |
|
|
parts[2] = "7'd" + prev_index;
|
90 |
|
|
}
|
91 |
|
|
if(parts[2].equals("#AUTOGEN_NEXT_CMD_LIKE_PREV")) {
|
92 |
|
|
if(prev_index == -1) throw new Exception("Invalid #AUTOGEN_NEXT_CMD_LIKE_PREV position: " + line);
|
93 |
|
|
parts[2] = "7'd" + prev_index;
|
94 |
|
|
}
|
95 |
|
|
if(parts[2].equals("#AUTOGEN_NEXT_CMD")) {
|
96 |
|
|
if(prev_index == -1) {
|
97 |
|
|
prev_index = find_mod(command_values, 1);
|
98 |
|
|
parts[2] = "7'd" + prev_index;
|
99 |
|
|
prev_index = -1;
|
100 |
|
|
}
|
101 |
|
|
else {
|
102 |
|
|
prev_index++;
|
103 |
|
|
//System.out.println("index: " + prev_index);
|
104 |
|
|
command_values.add(prev_index);
|
105 |
|
|
|
106 |
|
|
parts[2] = "7'd" + prev_index;
|
107 |
|
|
}
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
if(defines.containsKey(parts[1])) throw new Exception("Double definition of: " + line);
|
111 |
|
|
|
112 |
|
|
defines.put(parts[1], parts[2]);
|
113 |
|
|
}
|
114 |
|
|
}
|
115 |
|
|
}
|