Skip to content

Commit 857432c

Browse files
committed
---
yaml --- r: 621 b: refs/heads/master c: 9f0eaa6 h: refs/heads/master i: 619: b146828 v: v3
1 parent 7b2c26d commit 857432c

File tree

4 files changed

+151
-18
lines changed

4 files changed

+151
-18
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 0d15ae4f7a2429e70d7085e538e7ce305ef6a468
2+
refs/heads/master: 9f0eaa65817303b8768c80454734144c176fda43

trunk/src/comp/fe/lexer.rs

Lines changed: 138 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import std._io.stdio_reader;
22
import std._str;
33
import std.map;
44
import std.map.hashmap;
5+
import util.common;
56

67
fn new_str_hash[V]() -> map.hashmap[str,V] {
78
let map.hashfn[str] hasher = _str.hash;
@@ -95,6 +96,80 @@ fn new_reader(stdio_reader rdr, str filename) -> reader
9596
keywords.insert("ret", token.RET());
9697
keywords.insert("be", token.BE());
9798

99+
keywords.insert("fail", token.FAIL());
100+
keywords.insert("drop", token.DROP());
101+
102+
keywords.insert("type", token.TYPE());
103+
keywords.insert("check", token.CHECK());
104+
keywords.insert("claim", token.CLAIM());
105+
keywords.insert("prove", token.PROVE());
106+
107+
keywords.insert("io", token.IO());
108+
keywords.insert("state", token.STATE());
109+
keywords.insert("unsafe", token.UNSAFE());
110+
111+
keywords.insert("native", token.NATIVE());
112+
keywords.insert("mutable", token.MUTABLE());
113+
keywords.insert("auto", token.AUTO());
114+
115+
keywords.insert("fn", token.FN());
116+
keywords.insert("iter", token.ITER());
117+
118+
keywords.insert("import", token.IMPORT());
119+
keywords.insert("export", token.EXPORT());
120+
121+
keywords.insert("let", token.LET());
122+
123+
keywords.insert("log", token.LOG());
124+
keywords.insert("spawn", token.SPAWN());
125+
keywords.insert("thread", token.THREAD());
126+
keywords.insert("yield", token.YIELD());
127+
keywords.insert("join", token.JOIN());
128+
129+
keywords.insert("bool", token.BOOL());
130+
131+
keywords.insert("int", token.INT());
132+
keywords.insert("uint", token.UINT());
133+
keywords.insert("float", token.FLOAT());
134+
135+
keywords.insert("char", token.CHAR());
136+
keywords.insert("str", token.STR());
137+
138+
139+
keywords.insert("rec", token.REC());
140+
keywords.insert("tup", token.TUP());
141+
keywords.insert("tag", token.TAG());
142+
keywords.insert("vec", token.VEC());
143+
keywords.insert("any", token.ANY());
144+
145+
keywords.insert("obj", token.OBJ());
146+
147+
keywords.insert("port", token.PORT());
148+
keywords.insert("chan", token.CHAN());
149+
150+
keywords.insert("task", token.TASK());
151+
152+
keywords.insert("true", token.LIT_BOOL(true));
153+
keywords.insert("false", token.LIT_BOOL(false));
154+
155+
keywords.insert("in", token.IN());
156+
157+
keywords.insert("as", token.AS());
158+
keywords.insert("with", token.WITH());
159+
160+
keywords.insert("bind", token.BIND());
161+
162+
keywords.insert("u8", token.MACH(common.ty_u8()));
163+
keywords.insert("u16", token.MACH(common.ty_u16()));
164+
keywords.insert("u32", token.MACH(common.ty_u32()));
165+
keywords.insert("u64", token.MACH(common.ty_u64()));
166+
keywords.insert("i8", token.MACH(common.ty_i8()));
167+
keywords.insert("i16", token.MACH(common.ty_i16()));
168+
keywords.insert("i32", token.MACH(common.ty_i32()));
169+
keywords.insert("i64", token.MACH(common.ty_i64()));
170+
keywords.insert("f32", token.MACH(common.ty_f32()));
171+
keywords.insert("f64", token.MACH(common.ty_f64()));
172+
98173
ret reader(rdr, filename, rdr.getc() as char, rdr.getc() as char,
99174
1u, 1u, keywords, reserved);
100175
}
@@ -125,6 +200,31 @@ fn is_bin_digit(char c) -> bool {
125200
ret c == '0' || c == '1';
126201
}
127202

203+
fn dec_digit_val(char c) -> int {
204+
ret (c as int) - ('0' as int);
205+
}
206+
207+
fn hex_digit_val(char c) -> int {
208+
if (in_range(c, '0', '9')) {
209+
ret (c as int) - ('0' as int);
210+
}
211+
212+
if (in_range(c, 'a', 'f')) {
213+
ret (c as int) - ('a' as int);
214+
}
215+
216+
if (in_range(c, 'A', 'F')) {
217+
ret (c as int) - ('A' as int);
218+
}
219+
220+
fail;
221+
}
222+
223+
fn bin_digit_value(char c) -> int {
224+
if (c == 0) { ret 0; }
225+
ret 1;
226+
}
227+
128228
fn is_whitespace(char c) -> bool {
129229
ret c == ' ' || c == '\t' || c == '\r' || c == '\n';
130230
}
@@ -159,27 +259,54 @@ fn next_token(reader rdr) -> token.token {
159259
auto c = rdr.curr();
160260

161261
if (is_alpha(c)) {
162-
while (is_alpha(rdr.curr())) {
163-
c = rdr.curr();
262+
while (is_alpha(c) || c == '_') {
164263
accum_str += (c as u8);
165264
rdr.bump();
265+
c = rdr.curr();
166266
}
267+
268+
auto kwds = rdr.get_keywords();
269+
if (kwds.contains_key(accum_str)) {
270+
ret kwds.get(accum_str);
271+
}
272+
167273
ret token.IDENT(accum_str);
168274
}
169275

170276
if (is_dec_digit(c)) {
171-
if (c == '0') {
172-
log "fixme: leading zero";
173-
fail;
174-
} else {
175-
while (is_dec_digit(c)) {
277+
auto n = rdr.next();
278+
if (c == '0' && n == 'x') {
279+
rdr.bump();
280+
rdr.bump();
281+
c = rdr.curr();
282+
while (is_hex_digit(c) || c == '_') {
283+
accum_int *= 16;
284+
accum_int += hex_digit_val(v);
285+
rdr.bump();
176286
c = rdr.curr();
177-
accum_int *= 10;
178-
accum_int += (c as int) - ('0' as int);
287+
}
288+
}
289+
290+
if (c == '0' && n == 'b') {
291+
rdr.bump();
292+
rdr.bump();
293+
c = rdr.curr();
294+
while (is_hex_digit(c) || c == '_') {
295+
accum_int *= 2;
296+
accum_int += bit_value(c);
179297
rdr.bump();
298+
c = rdr.curr();
180299
}
181-
ret token.LIT_INT(accum_int);
182300
}
301+
302+
while (is_dec_digit(c) || c == '_') {
303+
accum_int *= 10;
304+
accum_int += dec_digit_val(v);
305+
rdr.bump();
306+
c = rdr.curr();
307+
}
308+
309+
ret token.LIT_INT(accum_int);
183310
}
184311

185312

@@ -206,6 +333,7 @@ fn next_token(reader rdr) -> token.token {
206333
case (']') { rdr.bump(); ret token.RBRACKET(); }
207334
case ('@') { rdr.bump(); ret token.AT(); }
208335
case ('#') { rdr.bump(); ret token.POUND(); }
336+
case ('_') { rdr.bump(); ret token.UNDERSCORE(); }
209337

210338
// Multi-byte tokens.
211339
case ('=') {

trunk/src/comp/fe/token.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,9 @@ fn to_str(token t) -> str {
275275
}
276276
case (LIT_CHAR(c)) {
277277
// FIXME: escape and encode.
278-
auto tmp = "";
279-
tmp += (c as u8);
278+
auto tmp = "'";
279+
tmp += c as u8;
280+
tmp += '\'' as u8;
280281
ret tmp;
281282
}
282283

@@ -285,7 +286,7 @@ fn to_str(token t) -> str {
285286
}
286287

287288
/* Name components */
288-
case (IDENT(s)) { ret s; }
289+
case (IDENT(s)) { auto si = "ident:"; si += s; ret si; }
289290
case (IDX(i)) { ret "_" + _int.to_str(i, 10u); }
290291
case (UNDERSCORE()) { ret "_"; }
291292

trunk/src/comp/util/common.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11

22
type ty_mach = tag( ty_i8(), ty_i16(), ty_i32(), ty_i64(),
33
ty_u8(), ty_u16(), ty_u32(), ty_u64(),
4-
ty_f32(), ty_f16() );
4+
ty_f32(), ty_f64() );
55

66
fn ty_mach_to_str(ty_mach tm) -> str {
77
alt (tm) {
88
case (ty_u8()) { ret "u8"; }
9-
case (ty_i8()) { ret "i8"; }
109
case (ty_u16()) { ret "u16"; }
11-
case (ty_i16()) { ret "i16"; }
1210
case (ty_u32()) { ret "u32"; }
13-
case (ty_i32()) { ret "i32"; }
1411
case (ty_u64()) { ret "u64"; }
12+
13+
case (ty_i8()) { ret "i8"; }
14+
case (ty_i16()) { ret "i16"; }
15+
case (ty_i32()) { ret "i32"; }
1516
case (ty_i64()) { ret "i64"; }
17+
18+
case (ty_f32()) { ret "f32"; }
19+
case (ty_f64()) { ret "f64"; }
1620
}
1721
}
1822

0 commit comments

Comments
 (0)