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