commit aac50a6
shrub
·
2026-11-22 17:59:23 +0000 UTC
parent 5af3de2
char
M
check.c
+9,
-6
1@@ -194,12 +194,15 @@ checklit(struct token tok, struct type *t) {
2 u64 d;
3 usize i;
4
5- for(i = 0; i < tok.length; i++) {
6- d = tok.str[i] - '0';
7- if(n > (UINT64_MAX - d) / 10)
8- errorat(&tok.loc, "number too big");
9- n = n * 10 + d;
10- }
11+ if(tok.kind == TCHAR)
12+ n = tonum(tok);
13+ else
14+ for(i = 0; i < tok.length; i++) {
15+ d = tok.str[i] - '0';
16+ if(n > (UINT64_MAX - d) / 10)
17+ errorat(&tok.loc, "number too big");
18+ n = n * 10 + d;
19+ }
20 if(n > intmax(t))
21 errorat(&tok.loc, tokdup(tok), " doesn't fit in ", typestr(t));
22 }
M
lex.re
+2,
-0
1@@ -38,6 +38,8 @@ void lex(struct lexer *l, struct token *t) {
2 [0-9]+ { settok(l, t, TNUMBER); return; }
3 '"' ( [^"\n\x00\\] | "\\" [^\n\x00] )* '"' { settok(l, t, TSTRING); return; }
4 '"' ( [^"\n\x00\\] | "\\" [^\n\x00] )* { settok(l, t, TSTRING); errorat(&t->loc, "unterminated string"); }
5+ "'" ( [^'\n\x00\\] | "\\" [ntr0\\'] ) "'" { settok(l, t, TCHAR); return; }
6+ "'" { settok(l, t, TNONE); errorat(&t->loc, "bad character literal"); }
7
8 * { settok(l, t, TNONE); errorat(&t->loc, "unexpected character"); }
9 */
M
parse.c
+2,
-1
1@@ -122,6 +122,7 @@ primary(void) {
2
3 switch(tok.kind) {
4 case TNUMBER:
5+ case TCHAR:
6 e = mkexpr(ENUMBER, tok);
7 next();
8 return e;
9@@ -310,7 +311,7 @@ pattern(void) {
10 p = mkpat(PTUPLE, opener);
11 p->args = head;
12 }
13- } else if(tok.kind == TNUMBER) {
14+ } else if(tok.kind == TNUMBER || tok.kind == TCHAR) {
15 p = mkpat(PNUM, tok);
16 next();
17 } else {
M
tokens.h
+1,
-0
1@@ -2,6 +2,7 @@ TOKEN(TNONE, )
2 TOKEN(TEOF, )
3 TOKEN(TIDENT, )
4 TOKEN(TNUMBER, )
5+TOKEN(TCHAR, )
6 TOKEN(TSTRING, )
7 TOKEN(TUSEPATH, )
8 TOKEN(TDOC, )
M
util.c
+11,
-0
1@@ -147,6 +147,17 @@ tonum(struct token t) {
2 u64 n = 0;
3 usize i;
4
5+ if(t.kind == TCHAR) {
6+ if(t.str[1] != '\\')
7+ return (u8)t.str[1];
8+ switch(t.str[2]) {
9+ case 'n': return '\n';
10+ case 't': return '\t';
11+ case 'r': return '\r';
12+ case '0': return 0;
13+ default: return t.str[2];
14+ }
15+ }
16 for(i = 0; i < t.length; i++)
17 n = n * 10 + (t.str[i] - '0');
18 return (i64)n;