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