10 char *str, *strp, *stre;
14 typedef struct Keyword Keyword;
19 /* both tables must be sorted */
44 Keyword *kwchar[128], *opchar[128];
51 for(kw = kwtab; kw < kwtab + nelem(kwtab); kw++)
52 if(kwchar[*kw->name] == nil)
53 kwchar[*kw->name] = kw;
54 for(kw = optab; kw < optab + nelem(optab); kw++)
55 if(opchar[*kw->name] == nil)
56 opchar[*kw->name] = kw;
63 stre = str + strlen(str);
73 fmtfdinit(&f, 2, buf, sizeof(buf));
74 fmtprint(&f, "%d ", lineno);
76 fmtvprint(&f, fmt, va);
110 static char buf[512];
116 while(ch = getch(), ch >= 0 && isspace(ch)){
125 while(ch = getch(), ch >= 0 && ch != '\n')
134 if(ch < 0) return -1;
135 if(ch == '\n') lineno++;
136 if(ch != '*') goto s1;
139 if(ch < 0) return -1;
140 if(ch == '\n') lineno++;
141 if(ch == '*') goto s2;
142 if(ch != '/') goto s1;
148 if(isalnum(ch) || ch == '_' || ch >= 0x80 || ch == ':'){
151 while(ch = getch(), isalnum(ch) || ch == '_' || ch >= 0x80 || ch == ':')
152 if(p < buf + sizeof(buf) - 1)
156 v = strtoull(buf, &p, 0);
157 if(p != buf && *p == 0){
161 if(strcmp(buf, ":") == 0)
163 if((uchar)buf[0] < 0x80 && kwchar[buf[0]] != nil)
164 for(kw = kwchar[buf[0]]; kw < kwtab + nelem(kwtab) && kw->name[0] == buf[0]; kw++)
165 if(strcmp(kw->name, buf) == 0)
167 yylval.sym = getsym(buf);
172 while(ch = getch(), ch >= 0 && ch != '"'){
174 error("unterminated string");
176 switch(ch = getch()){
177 case 'n': ch = '\n'; break;
178 case 'r': ch = '\r'; break;
179 case 't': ch = '\t'; break;
180 case 'v': ch = '\v'; break;
181 case 'b': ch = '\b'; break;
182 case 'a': ch = '\a'; break;
183 case '"': case '\\': break;
184 default: error("unknown escape code \\%c", ch);
186 if(p < buf + sizeof(buf) - 1)
189 if(ch < 0) error("unterminated string");
191 yylval.str = strdup(buf);
194 if(opchar[ch] != nil){
197 for(kw = opchar[buf[0]]; kw < optab + nelem(optab) && kw->name[0] == buf[0]; kw++)
198 if(buf[1] == kw->name[1]){
201 if(kw + 1 < optab + nelem(optab) && strcmp(kw[1].name, buf) == 0)
215 static char *nodestr[] = {
227 t = va_arg(f->args, int);
228 if(t >= nelem(nodestr) || nodestr[t] == nil)
229 return fmtprint(f, "??? (%d)", t);
231 return fmtprint(f, "%s", nodestr[t]);
240 n = emalloc(sizeof(Node));
245 n->op = va_arg(va, int);
246 n->n1 = va_arg(va, Node *);
247 n->n2 = va_arg(va, Node *);
250 n->n1 = va_arg(va, Node *);
253 n->sym = va_arg(va, Symbol *);
256 n->num = va_arg(va, s64int);
259 n->n1 = va_arg(va, Node *);
260 n->n2 = va_arg(va, Node *);
261 n->n3 = va_arg(va, Node *);
264 n->n1 = va_arg(va, Node *);
267 n->typ = va_arg(va, Type *);
268 n->n1 = va_arg(va, Node *);
271 n->str = va_arg(va, char *);
274 sysfatal("node: unknown type %α", type);
287 h = 0xcbf29ce484222325ULL;
290 h *= 0x100000001b3ULL;
302 for(sp = &globals.sym[h % SYMHASH]; s = *sp, s != nil; sp = &s->next)
303 if(strcmp(s->name, name) == 0)
305 *sp = s = emalloc(sizeof(Symbol));
306 s->name = strdup(name);
314 static char *tstr[] = {
315 [TYPINVAL] "TYPINVAL",
318 [TYPSTRING] "TYPSTRING",
321 t = va_arg(f->args, int);
322 if(t >= nelem(tstr) || tstr[t] == nil)
323 return fmtprint(f, "??? (%d)", t);
325 return fmtprint(f, "%s", tstr[t]);
333 t = va_arg(f->args, Type *);
335 case TYPINT: return fmtprint(f, "%c%d", t->sign ? 's' : 'u', t->size * 8);
336 case TYPSTRING: return fmtprint(f, "string");
337 case TYPPTR: return fmtprint(f, "%τ*", t->ref);
338 default: return fmtprint(f, "%t", t->type);
342 static Type typu8 = {.type TYPINT, .size 1, .sign 0};
343 static Type typs8 = {.type TYPINT, .size 1, .sign 1};
344 static Type typu16 = {.type TYPINT, .size 2, .sign 0};
345 static Type typs16 = {.type TYPINT, .size 2, .sign 1};
346 static Type typu32 = {.type TYPINT, .size 4, .sign 0};
347 static Type typs32 = {.type TYPINT, .size 4, .sign 1};
348 static Type typu64 = {.type TYPINT, .size 8, .sign 0};
349 static Type typs64 = {.type TYPINT, .size 8, .sign 1};
350 static Type typstr = {.type TYPSTRING, .size DTSTRMAX };
351 static Type *typereg;
358 for(s = typereg; s != nil; s = s->typenext)
359 if(s->type == TYPPTR && s->ref == t)
361 s = emalloc(sizeof(Type));
376 size = va_arg(va, int);
377 sign = va_arg(va, int);
378 switch(size << 4 | sign){
379 case 0x10: return &typu8;
380 case 0x11: return &typs8;
381 case 0x20: return &typu16;
382 case 0x21: return &typs16;
383 case 0x40: return &typu32;
384 case 0x41: return &typs32;
385 case 0x80: return &typu64;
386 case 0x81: return &typs64;
387 default: sysfatal("type: invalid (size,sign) = (%d,%d)\n", size, sign); return nil;
389 case TYPSTRING: return &typstr;
390 case TYPPTR: return mkptr(va_arg(va, Type *));
391 default: sysfatal("type: unknown %t", typ); return nil;