Mercurial > hg-old > index.cgi
annotate src/parse.c @ 52:b9856da2674a
Added file inclusion
author | lost |
---|---|
date | Sun, 04 Jan 2009 20:16:38 +0000 |
parents | 2330b88f9600 |
children | 035b95a3690f |
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 { |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
46 as -> context += 1; |
36 | 47 return 0; |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
48 } |
36 | 49 |
52 | 50 // for output generation later but only on pass 1 |
51 if (as -> passnum == 1) | |
52 l -> codeaddr = as -> addr; | |
44 | 53 |
36 | 54 if (!isspace(*p) && *p != '*' && *p != ';') |
55 { | |
56 // we have a symbol specified here | |
57 // parse it and define | |
58 // 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
|
59 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
|
60 /* do nothing */ ; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
61 |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
62 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
|
63 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
|
64 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
|
65 |
44 | 66 p = p2; |
67 | |
68 if (as -> passnum == 1) | |
69 { | |
70 l -> sym = sym; | |
71 // have a symbol; now determine if it is valid and register it | |
72 // at the current address of the line | |
73 debug_message(1, "Registering symbol '%s' at %04X", sym, as -> addr); | |
74 if (lwasm_register_symbol(as, l, sym, as -> addr) < 0) | |
75 l -> sym = NULL; | |
76 } | |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
77 } |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
78 else |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
79 { |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
80 while (*p && isspace(*p)) |
38 | 81 p++; |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
82 if (!*p) |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
83 { |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
84 as -> context += 1; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
85 return 0; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
86 } |
36 | 87 } |
88 | |
89 // skip white space | |
90 while (*p && isspace(*p)) | |
91 p++; | |
92 | |
93 // if comment or end of line, return | |
94 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
|
95 { |
44 | 96 if (sym) |
97 lwasm_free(l -> sym); | |
36 | 98 return 0; |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
99 } |
36 | 100 |
101 // parse the opcode | |
102 for (p2 = p; *p2 && !isspace(*p2); p2++) | |
103 /* do nothing */ ; | |
104 | |
105 opc = lwasm_alloc((p2 - p) + 1); | |
106 memcpy(opc, p, p2 - p); | |
107 opc[p2 - p] = '\0'; | |
108 | |
38 | 109 debug_message(2, "Found operation code: '%s'", opc); |
110 | |
36 | 111 // skip intervening whitespace if present |
112 while (*p2 && isspace(*p2)) | |
113 p2++; | |
114 | |
115 // check for macro (pass 1) | |
116 | |
117 // look up instruction in insn table | |
118 for (opnum = 0; instab[opnum].opcode; opnum++) | |
119 { | |
120 if (!strcasecmp(instab[opnum].opcode, opc)) | |
121 break; | |
122 } | |
123 | |
124 if (!(instab[opnum].opcode) || !(instab[opnum].fn)) | |
125 { | |
126 // invalid operation code, throw error | |
127 register_error(as, l, 1, "Invalid operation code '%s'", opc); | |
44 | 128 if (sym) |
129 lwasm_free(l -> sym); | |
36 | 130 lwasm_free(opc); |
131 return -1; | |
132 } | |
133 | |
134 // dispatch handler | |
135 (instab[opnum].fn)(as, l, &p2, opnum); | |
136 | |
137 lwasm_free(opc); | |
44 | 138 if (sym) |
139 lwasm_free(sym); | |
36 | 140 } |