comparison src/parse.c @ 37:538e15927776

Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
author lost
date Sat, 03 Jan 2009 04:20:49 +0000
parents 99e3b3310bac
children 9bd584bb6296
comparison
equal deleted inserted replaced
36:99e3b3310bac 37:538e15927776
35 int lwasm_parse_line(asmstate_t *as, lwasm_line_t *l) 35 int lwasm_parse_line(asmstate_t *as, lwasm_line_t *l)
36 { 36 {
37 char *p, *p2; 37 char *p, *p2;
38 char *opc; 38 char *opc;
39 int opnum; 39 int opnum;
40 char *sym;
40 41
41 p = l -> text; 42 p = l -> text;
42 43
43 if (!*p) 44 if (!*p)
45 {
46 as -> context += 1;
44 return 0; 47 return 0;
48 }
45 49
46 if (!isspace(*p) && *p != '*' && *p != ';') 50 if (!isspace(*p) && *p != '*' && *p != ';')
47 { 51 {
48 // we have a symbol specified here 52 // we have a symbol specified here
49 // parse it and define 53 // parse it and define
50 // need to handle local symbols here... 54 // need to handle local symbols here...
55 for (p2 = p; *p2 && !isspace(*p2); p2++)
56 /* do nothing */ ;
57
58 sym = lwasm_alloc((p2 - p) + 1);
59 sym[p2 - p] = '\0';
60 memcpy(sym, p, p2 - p);
61
62 l -> sym = sym;
63 // have a symbol; now determine if it is valid and register it
64 // at the current address of the line
65 if (lwasm_register_symbol(as, l, sym, as -> addr) < 0)
66 l -> sym = NULL;
67 }
68 else
69 {
70 while (*p && isspace(*p))
71 (*p)++;
72 if (!*p)
73 {
74 as -> context += 1;
75 return 0;
76 }
51 } 77 }
52 78
53 // skip white space 79 // skip white space
54 while (*p && isspace(*p)) 80 while (*p && isspace(*p))
55 p++; 81 p++;
56 82
57 // if comment or end of line, return 83 // if comment or end of line, return
58 if (!*p || *p == '*' || *p == ';') 84 if (!*p || *p == '*' || *p == ';')
85 {
86 lwasm_free(l -> sym);
59 return 0; 87 return 0;
88 }
60 89
61 // parse the opcode 90 // parse the opcode
62 for (p2 = p; *p2 && !isspace(*p2); p2++) 91 for (p2 = p; *p2 && !isspace(*p2); p2++)
63 /* do nothing */ ; 92 /* do nothing */ ;
64 93
81 110
82 if (!(instab[opnum].opcode) || !(instab[opnum].fn)) 111 if (!(instab[opnum].opcode) || !(instab[opnum].fn))
83 { 112 {
84 // invalid operation code, throw error 113 // invalid operation code, throw error
85 register_error(as, l, 1, "Invalid operation code '%s'", opc); 114 register_error(as, l, 1, "Invalid operation code '%s'", opc);
115 lwasm_free(l -> sym);
86 lwasm_free(opc); 116 lwasm_free(opc);
87 return -1; 117 return -1;
88 } 118 }
89 119
90 // dispatch handler 120 // dispatch handler
91 (instab[opnum].fn)(as, l, &p2, opnum); 121 (instab[opnum].fn)(as, l, &p2, opnum);
92 122
93 lwasm_free(opc); 123 lwasm_free(opc);
124 lwasm_free(sym);
94 } 125 }