Mercurial > hg-old > index.cgi
annotate src/parse.c @ 64:aaddd47219b4
Added the 'set' directive
author | lost |
---|---|
date | Mon, 05 Jan 2009 01:27:08 +0000 |
parents | d85ba47b1e8f |
children | aa9d9fedfdf4 |
rev | line source |
---|---|
36 | 1 /* |
2 parse.c | |
3 Copyright © 2008 William Astle | |
4 | |
5 This file is part of LWASM. | |
6 | |
7 LWASM is free software: you can redistribute it and/or modify it under the | |
8 terms of the GNU General Public License as published by the Free Software | |
9 Foundation, either version 3 of the License, or (at your option) any later | |
10 version. | |
11 | |
12 This program is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
15 more details. | |
16 | |
17 You should have received a copy of the GNU General Public License along with | |
18 this program. If not, see <http://www.gnu.org/licenses/>. | |
19 */ | |
20 | |
21 /* | |
22 Contains the general parser | |
23 */ | |
24 | |
25 #define __parse_c_seen__ | |
26 | |
27 #include <ctype.h> | |
28 #include <string.h> | |
29 | |
30 #include "lwasm.h" | |
31 #include "instab.h" | |
32 #include "util.h" | |
33 | |
34 // parse a line and despatch to the appropriate handlers for opcodes | |
35 int lwasm_parse_line(asmstate_t *as, lwasm_line_t *l) | |
36 { | |
37 char *p, *p2; | |
38 char *opc; | |
39 int opnum; | |
44 | 40 char *sym = NULL; |
36 | 41 |
42 p = l -> text; | |
43 | |
44 if (!*p) | |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
45 { |
57 | 46 if (as -> inmacro == 0) |
47 as -> context = lwasm_next_context(as); | |
36 | 48 return 0; |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
49 } |
36 | 50 |
52 | 51 // for output generation later but only on pass 1 |
52 if (as -> passnum == 1) | |
53 l -> codeaddr = as -> addr; | |
44 | 54 |
36 | 55 if (!isspace(*p) && *p != '*' && *p != ';') |
56 { | |
57 // we have a symbol specified here | |
58 // parse it and define | |
59 // need to handle local symbols here... | |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
60 for (p2 = p; *p2 && !isspace(*p2); p2++) |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
61 /* do nothing */ ; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
62 |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
63 sym = lwasm_alloc((p2 - p) + 1); |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
64 sym[p2 - p] = '\0'; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
65 memcpy(sym, p, p2 - p); |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
66 |
44 | 67 p = p2; |
68 | |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
69 } |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
70 else |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
71 { |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
72 while (*p && isspace(*p)) |
38 | 73 p++; |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
74 if (!*p) |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
75 { |
57 | 76 if (as -> inmacro == 0) |
77 as -> context = lwasm_next_context(as); | |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
78 return 0; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
79 } |
36 | 80 } |
81 | |
82 // skip white space | |
83 while (*p && isspace(*p)) | |
84 p++; | |
85 | |
86 // if comment or end of line, return | |
87 if (!*p || *p == '*' || *p == ';') | |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
88 { |
44 | 89 if (sym) |
90 lwasm_free(l -> sym); | |
36 | 91 return 0; |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
92 } |
36 | 93 |
94 // parse the opcode | |
95 for (p2 = p; *p2 && !isspace(*p2); p2++) | |
96 /* do nothing */ ; | |
97 | |
98 opc = lwasm_alloc((p2 - p) + 1); | |
99 memcpy(opc, p, p2 - p); | |
100 opc[p2 - p] = '\0'; | |
101 | |
38 | 102 debug_message(2, "Found operation code: '%s'", opc); |
103 | |
36 | 104 // skip intervening whitespace if present |
105 while (*p2 && isspace(*p2)) | |
106 p2++; | |
107 | |
108 // look up instruction in insn table | |
109 for (opnum = 0; instab[opnum].opcode; opnum++) | |
110 { | |
111 if (!strcasecmp(instab[opnum].opcode, opc)) | |
112 break; | |
113 } | |
114 | |
57 | 115 if (as -> inmacro && instab[opnum].endm == 0) |
116 { | |
117 add_macro_line(as, l -> text); | |
118 } | |
119 | |
120 if (as -> inmacro == 0 && as -> skipcond == 0) | |
121 { | |
122 if (expand_macro(as, l, &p2, opc) == 0) | |
123 goto done_line; | |
124 } | |
125 | |
63
d85ba47b1e8f
Moved symbol registration so symbols that are in skipped code do not get registered and so EQU/SET can do their own registration
lost
parents:
57
diff
changeset
|
126 // register symbol if needed |
d85ba47b1e8f
Moved symbol registration so symbols that are in skipped code do not get registered and so EQU/SET can do their own registration
lost
parents:
57
diff
changeset
|
127 if (as -> skipcond == 0 && as -> inmacro == 0 && sym) |
d85ba47b1e8f
Moved symbol registration so symbols that are in skipped code do not get registered and so EQU/SET can do their own registration
lost
parents:
57
diff
changeset
|
128 { |
d85ba47b1e8f
Moved symbol registration so symbols that are in skipped code do not get registered and so EQU/SET can do their own registration
lost
parents:
57
diff
changeset
|
129 l -> sym = sym; |
d85ba47b1e8f
Moved symbol registration so symbols that are in skipped code do not get registered and so EQU/SET can do their own registration
lost
parents:
57
diff
changeset
|
130 if (as -> passnum == 1 && !(instab[opnum].opcode && instab[opnum].setsym)) |
d85ba47b1e8f
Moved symbol registration so symbols that are in skipped code do not get registered and so EQU/SET can do their own registration
lost
parents:
57
diff
changeset
|
131 { |
d85ba47b1e8f
Moved symbol registration so symbols that are in skipped code do not get registered and so EQU/SET can do their own registration
lost
parents:
57
diff
changeset
|
132 // have a symbol; now determine if it is valid and register it |
d85ba47b1e8f
Moved symbol registration so symbols that are in skipped code do not get registered and so EQU/SET can do their own registration
lost
parents:
57
diff
changeset
|
133 // at the current address of the line |
d85ba47b1e8f
Moved symbol registration so symbols that are in skipped code do not get registered and so EQU/SET can do their own registration
lost
parents:
57
diff
changeset
|
134 debug_message(1, "Registering symbol '%s' at %04X", sym, as -> addr); |
64 | 135 if (lwasm_register_symbol(as, l, sym, as -> addr, SYMBOL_NORM) < 0) |
63
d85ba47b1e8f
Moved symbol registration so symbols that are in skipped code do not get registered and so EQU/SET can do their own registration
lost
parents:
57
diff
changeset
|
136 l -> sym = NULL; |
d85ba47b1e8f
Moved symbol registration so symbols that are in skipped code do not get registered and so EQU/SET can do their own registration
lost
parents:
57
diff
changeset
|
137 } |
d85ba47b1e8f
Moved symbol registration so symbols that are in skipped code do not get registered and so EQU/SET can do their own registration
lost
parents:
57
diff
changeset
|
138 } |
d85ba47b1e8f
Moved symbol registration so symbols that are in skipped code do not get registered and so EQU/SET can do their own registration
lost
parents:
57
diff
changeset
|
139 |
57 | 140 if (!(instab[opnum].opcode) || !(instab[opnum].fn) && !(as -> skipcond || as -> inmacro)) |
36 | 141 { |
142 // invalid operation code, throw error | |
143 register_error(as, l, 1, "Invalid operation code '%s'", opc); | |
44 | 144 if (sym) |
145 lwasm_free(l -> sym); | |
36 | 146 lwasm_free(opc); |
147 return -1; | |
148 } | |
149 | |
57 | 150 // dispatch handler if we're not ignoring items |
151 if (as -> skipcond == 0 && as -> inmacro == 0 && !(instab[opnum].iscond)) | |
152 (instab[opnum].fn)(as, l, &p2, opnum); | |
153 | |
154 done_line: | |
36 | 155 lwasm_free(opc); |
44 | 156 if (sym) |
157 lwasm_free(sym); | |
36 | 158 } |