Debian Codegraph @

contrib/toacme/src/hypra.c

raw symbols SCIP
1 // ToACME - converts other source codes to ACME format.
2 // Copyright (C) 1999-2006 Marco Baye
3 // Have a look at "main.c" for further info
4 //
5 // "HypraAss" stuff
6
7 #include <stdio.h>
8 #include "config.h"
9 #include "acme.h"
10 #include "gighyp.h"
11 #include "io.h"
12 #include "pet2iso.h"
13
14
15 // functions
16
17 // complain about unknown pseudo opcodes
18 static void complain(char a, char b)
19 {
20 IO_put_string("; ToACME: .");
21 if (a)
22 IO_put_byte(a);
23 if (b)
24 IO_put_byte(b);
25 IO_put_string(" cannot be converted\n");
26 }
27
28
29 // handle ".ba" and ".by"
30 static int process_po_b(char second)
31 {
32 int flags = 0;
33
34 switch (second) {
35 case 'a': // ".ba" = set base address
36 IO_put_string(ACME_set_pc);
37 break;
38 case 'y': // ".by" = insert bytes
39 IO_put_string(ACME_po_byte);
40 flags |= FLAG_INSERT_SPACE;
41 break;
42 default:
43 complain('b', second);
44 }
45 return flags;
46 }
47
48
49 // handle ".ei", ".el", ".en" and ".eq"
50 static int process_po_e(char second)
51 {
52 int flags = 0;
53
54 switch (second) {
55 case 'i': // ".ei" = endif
56 IO_put_string(ACME_endif);
57 break;
58 case 'l': // ".el" = else
59 IO_put_string(ACME_else);
60 break;
61 case 'n': // ".en" = end
62 IO_put_string(ACME_po_eof);
63 flags |= FLAG_INSERT_SPACE;
64 break;
65 case 'q': // ".eq" = label def
66 break;
67 default:
68 complain('e', second);
69 }
70 return flags;
71 }
72
73
74 // handle ".tx" and ".ts"
75 static int process_po_t(char second)
76 {
77 int flags = 0;
78
79 switch (second) {
80 case 'x': // ".tx" = insert string
81 IO_put_string(ACME_po_pet);
82 flags |= FLAG_INSERT_SPACE;
83 break;
84 case 's': // ".ts" = screen code string
85 IO_put_string(ACME_po_scr);
86 flags |= FLAG_INSERT_SPACE;
87 break;
88 default:
89 complain('t', second);
90 }
91 return flags;
92 }
93
94
95 #define ARE(a, b) ((first == a) && (second == b))
96 // process pseudo opcode
97 static int process_pseudo_opcode(void) // '.' was last read
98 {
99 int first,
100 second;
101
102 // get first byte. if illegal, complain and exit immediately
103 first = PET2ISO(IO_get_byte());
104 if ((first == SPACE) || (first == ';') || (first == '\0')) {
105 complain(first, '\0');
106 return 0;
107 }
108 // get second byte. if illegal, complain and exit immediately
109 second = PET2ISO(IO_get_byte());
110 if ((second == SPACE) || (second == ';') || (second == '\0')) {
111 complain(first, second);
112 return 0;
113 }
114 IO_get_byte();// pre-read unused byte
115 // check pseudo opcodes (switch/case was actually harder to read)
116 if (first == 'b') { // handle ".ba" and ".by"
117 process_po_b(second);
118 return FLAG_INSERT_SPACE;
119 }
120 if (first == 'e') // handle ".ei", ".el", ".en" and ".eq"
121 return process_po_e(second);
122
123 if (first == 't') // handle ".tx" and ".ts"
124 return process_po_t(second);
125
126 if (ARE('.', '.')) { // "..." = macro call
127 IO_put_string(ACME_macro_call);
128 return FLAG_INSERT_SPACE | FLAG_SKIP_OPENING;
129 }
130 if (ARE('m', 'a')) { // ".ma" = macro definition
131 IO_put_string(ACME_po_macro);
132 return FLAG_INSERT_SPACE | FLAG_SKIP_OPENING | FLAG_ADD_LEFT_BRACE;
133 }
134 if (ARE('o', 'b')) { // ".ob" = output to file
135 IO_put_string(ACME_po_to);
136 return FLAG_INSERT_SPACE | FLAG_ADD_CBM;
137 }
138 if (ARE('s', 'y')) { // ".sy" = symbol dump
139 IO_put_string(ACME_po_sl);
140 IO_put_string("\"symboldump.txt\";");
141 return 0;
142 }
143 if (ARE('i', 'f')) { // ".if" = cond. assembly
144 IO_put_string(ACME_po_if);
145 return FLAG_INSERT_SPACE | FLAG_ADD_LEFT_BRACE;
146 }
147 if (ARE('g', 'l')) // ".gl" = global label def
148 return 0;
149
150 if (ARE('a', 'p')) // ".ap" = append source
151 IO_put_string(ACME_po_source);
152 else if (ARE('r', 't')) // ".rt" = end of macro def
153 IO_put_string(ACME_endmacro);
154 else if (ARE('w', 'o')) // ".wo" = insert words
155 IO_put_string(ACME_po_word);
156 else
157 complain(first, second);
158 return FLAG_INSERT_SPACE;
159 }
160
161
162 // process opcode
163 static void real_opcode(void) // character was last read
164 {
165 IO_put_byte(PET2ISO(GotByte));
166 IO_get_byte();
167 if ((GotByte == SPACE) || (GotByte == ';') || (GotByte == '\0'))
168 return;
169
170 IO_put_byte(PET2ISO(GotByte));
171 IO_get_byte();
172 if ((GotByte == SPACE) || (GotByte == ';') || (GotByte == '\0'))
173 return;
174
175 IO_put_byte(PET2ISO(GotByte));
176 IO_get_byte(); // exit with unused byte pre-read
177 }
178
179
180 // main routine for HypraAss conversion
181 void hypra_main(void)
182 {
183 int indent;
184
185 IO_set_input_padding(0);
186 IO_process_load_address();
187 ACME_switch_to_pet();
188 // loop: once for every line in the file
189 while (!IO_reached_eof) {
190 // skip link pointer (if it's zero, report as end marker)
191 if (IO_get_le16() == 0)
192 IO_put_string("; ToACME: Found BASIC end marker.\n");
193 IO_get_le16(); // skip line number
194 // process line
195 IO_get_byte();
196 indent = 0;
197 if ((GotByte != SPACE) && (GotByte != ';') && (GotByte != '\0'))
198 indent = GigaHypra_label_definition();
199 // skip spaces
200 while (GotByte == SPACE)
201 IO_get_byte();
202 // if there is an opcode, process it
203 if ((GotByte != ';') && (GotByte != '\0')) {
204 GigaHypra_indent(indent);
205 // branch to relevant routine
206 if (GotByte == '.') {
207 GigaHypra_argument(process_pseudo_opcode());
208 } else {
209 real_opcode();
210 GigaHypra_argument(FLAG_INSERT_SPACE);
211 }
212 }
213 // skip comment, if there is one
214 if (GotByte == ';')
215 GigaHypra_comment();
216 // end of line
217 IO_put_byte('\n');
218 }
219 }