Skip to content

Commit 8030757

Browse files
committed
Switch machine-type lexemes to use suffixes. Remove support for foo(bar) as a cast notation. Closes #129.
1 parent 6662aeb commit 8030757

24 files changed

+156
-207
lines changed

src/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,8 @@ TEST_XFAILS_LLVM := $(addprefix test/run-pass/, \
416416
generic-recursive-tag.rs \
417417
generic-tag-alt.rs \
418418
generic-tag.rs \
419+
i32-sub.rs \
420+
i8-incr.rs \
419421
import.rs \
420422
inner-module.rs \
421423
iter-range.rs \

src/boot/driver/main.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ let (sess:Session.sess) =
5050
Session.sess_log_obj = false;
5151
Session.sess_log_lib = false;
5252
Session.sess_log_out = stdout;
53+
Session.sess_log_err = stderr;
5354
Session.sess_trace_block = false;
5455
Session.sess_trace_drop = false;
5556
Session.sess_trace_tag = false;

src/boot/driver/session.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type sess =
3232
mutable sess_log_obj: bool;
3333
mutable sess_log_lib: bool;
3434
mutable sess_log_out: out_channel;
35+
mutable sess_log_err: out_channel;
3536
mutable sess_trace_block: bool;
3637
mutable sess_trace_drop: bool;
3738
mutable sess_trace_tag: bool;
@@ -79,7 +80,7 @@ let log name flag chan =
7980

8081
let fail sess =
8182
sess.sess_failed <- true;
82-
Printf.fprintf sess.sess_log_out
83+
Printf.fprintf sess.sess_log_err
8384
;;
8485

8586

src/boot/fe/ast.ml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,11 @@ and expr =
333333
and lit =
334334
| LIT_nil
335335
| LIT_bool of bool
336-
| LIT_mach of (ty_mach * int64 * string)
337-
| LIT_int of (int64 * string)
338-
| LIT_uint of (int64 * string)
336+
| LIT_mach_int of (ty_mach * int64)
337+
| LIT_int of int64
338+
| LIT_uint of int64
339339
| LIT_char of int
340+
(* FIXME: No support for LIT_mach_float or LIT_float yet. *)
340341

341342

342343
and lval_component =
@@ -835,13 +836,15 @@ and fmt_lit (ff:Format.formatter) (l:lit) : unit =
835836
| LIT_nil -> fmt ff "()"
836837
| LIT_bool true -> fmt ff "true"
837838
| LIT_bool false -> fmt ff "false"
838-
| LIT_mach (m, _, s) ->
839+
| LIT_mach_int (m, i) ->
839840
begin
841+
fmt ff "%Ld" i;
840842
fmt_mach ff m;
841-
fmt ff "(%s)" s
842843
end
843-
| LIT_int (_,s) -> fmt ff "%s" s
844-
| LIT_uint (_,s) -> fmt ff "%s" s
844+
| LIT_int i -> fmt ff "%Ld" i
845+
| LIT_uint i ->
846+
fmt ff "%Ld" i;
847+
fmt ff "u"
845848
| LIT_char c -> fmt ff "'%s'" (Common.escaped_char c)
846849

847850
and fmt_domain (ff:Format.formatter) (d:domain) : unit =

src/boot/fe/cexp.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,9 @@ and eval_pexp (env:env) (exp:Pexp.pexp) : pval =
527527
| Pexp.PEXP_lit (Ast.LIT_bool b) ->
528528
PVAL_bool b
529529

530-
| Pexp.PEXP_lit (Ast.LIT_int (i, _)) ->
530+
| Pexp.PEXP_lit (Ast.LIT_int i)
531+
| Pexp.PEXP_lit (Ast.LIT_uint i)
532+
| Pexp.PEXP_lit (Ast.LIT_mach_int (_, i)) ->
531533
PVAL_num i
532534

533535
| Pexp.PEXP_str s ->

src/boot/fe/lexer.mll

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,24 @@
2222
Lexing.pos_bol = p.Lexing.pos_cnum }
2323
;;
2424

25+
let mach_suf_table = Hashtbl.create 0
26+
;;
27+
let _ =
28+
List.iter (fun (suf, ty) -> Common.htab_put mach_suf_table suf ty)
29+
[ ("u8", Common.TY_u8);
30+
("i8", Common.TY_i8);
31+
("u16", Common.TY_u16);
32+
("i16", Common.TY_i16);
33+
("u32", Common.TY_u32);
34+
("i32", Common.TY_i32);
35+
("u64", Common.TY_u64);
36+
("i64", Common.TY_i64);
37+
("f32", Common.TY_f32);
38+
("f64", Common.TY_f64); ]
39+
;;
40+
2541
let keyword_table = Hashtbl.create 100
42+
;;
2643
let _ =
2744
List.iter (fun (kwd, tok) -> Common.htab_put keyword_table kwd tok)
2845
[ ("mod", MOD);
@@ -129,6 +146,9 @@ let dec = decdig ['0'-'9' '_']*
129146
let exp = ['e''E']['-''+']? dec
130147
let flo = (dec '.' dec (exp?)) | (dec exp)
131148

149+
let mach_float_suf = "f32"|"f64"
150+
let mach_int_suf = ['u''i']('8'|"16"|"32"|"64")
151+
132152
let ws = [ ' ' '\t' '\r' ]
133153

134154
let id = ['a'-'z' 'A'-'Z' '_']['a'-'z' 'A'-'Z' '0'-'9' '_']*
@@ -197,19 +217,29 @@ rule token = parse
197217
{ try
198218
Hashtbl.find keyword_table i
199219
with
200-
Not_found -> IDENT (i)
201-
}
220+
Not_found -> IDENT (i) }
221+
222+
| (bin|hex|dec) as n { LIT_INT (Int64.of_string n) }
223+
| ((bin|hex|dec) as n) 'u' { LIT_UINT (Int64.of_string n) }
224+
| ((bin|hex|dec) as n)
225+
(mach_int_suf as s) { try
226+
let tm =
227+
Hashtbl.find mach_suf_table s
228+
in
229+
LIT_MACH_INT
230+
(tm, Int64.of_string n)
231+
with
232+
Not_found ->
233+
fail lexbuf
234+
"bad mach-int suffix" }
202235

203-
| bin as n { LIT_INT (Int64.of_string n, n) }
204-
| hex as n { LIT_INT (Int64.of_string n, n) }
205-
| dec as n { LIT_INT (Int64.of_string n, n) }
206-
| flo as n { LIT_FLO n }
236+
| flo as n { LIT_FLOAT (float_of_string n) }
207237

208-
| '\'' { char lexbuf }
209-
| '"' { let buf = Buffer.create 32 in
210-
str buf lexbuf }
238+
| '\'' { char lexbuf }
239+
| '"' { let buf = Buffer.create 32 in
240+
str buf lexbuf }
211241

212-
| eof { EOF }
242+
| eof { EOF }
213243

214244
and str buf = parse
215245
_ as ch

src/boot/fe/pexp.ml

Lines changed: 3 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,9 @@ and parse_rec_body (ps:pstate) : pexp' = (*((Ast.ident * pexp) array) =*)
466466

467467
and parse_lit (ps:pstate) : Ast.lit =
468468
match peek ps with
469-
LIT_INT (n,s) -> (bump ps; Ast.LIT_int (n,s))
469+
LIT_INT i -> (bump ps; Ast.LIT_int i)
470+
| LIT_UINT i -> (bump ps; Ast.LIT_uint i)
471+
| LIT_MACH_INT (tm, i) -> (bump ps; Ast.LIT_mach_int (tm, i))
470472
| LIT_CHAR c -> (bump ps; Ast.LIT_char c)
471473
| LIT_BOOL b -> (bump ps; Ast.LIT_bool b)
472474
| _ -> raise (unexpected ps)
@@ -602,106 +604,6 @@ and parse_bottom_pexp (ps:pstate) : pexp =
602604
let bpos = lexpos ps in
603605
span ps apos bpos (PEXP_lval (PLVAL_ext_deref inner))
604606

605-
| (INT | UINT | CHAR | BOOL) as tok ->
606-
begin
607-
bump ps;
608-
expect ps LPAREN;
609-
match peek ps with
610-
(LIT_INT _ | LIT_CHAR _ | LIT_BOOL _) as tok2 ->
611-
bump ps;
612-
expect ps RPAREN;
613-
let i = match tok2 with
614-
LIT_INT i -> i
615-
| LIT_CHAR c -> (Int64.of_int c,
616-
Common.escaped_char c)
617-
| LIT_BOOL b -> if b then (1L, "1") else (0L, "0")
618-
| _ -> bug () "expected int/char literal"
619-
in
620-
let bpos = lexpos ps in
621-
span ps apos bpos
622-
(PEXP_lit
623-
(match tok with
624-
INT -> Ast.LIT_int i
625-
| UINT -> Ast.LIT_uint i
626-
| CHAR ->
627-
Ast.LIT_char
628-
(Int64.to_int (fst i))
629-
| BOOL -> Ast.LIT_bool (fst i <> 0L)
630-
| _ -> bug () "expected int/uint/char/bool token"))
631-
632-
| _ ->
633-
let pexp = parse_pexp ps in
634-
expect ps RPAREN;
635-
let bpos = lexpos ps in
636-
let t =
637-
match tok with
638-
INT -> Ast.TY_int
639-
| UINT -> Ast.TY_uint
640-
| CHAR -> Ast.TY_char
641-
| BOOL -> Ast.TY_bool
642-
| _ -> bug () "expected int/uint/char/bool token"
643-
in
644-
let t = span ps apos bpos t in
645-
span ps apos bpos
646-
(PEXP_unop ((Ast.UNOP_cast t), pexp))
647-
end
648-
649-
| MACH m ->
650-
let literal (num, str) =
651-
let _ = bump ps in
652-
let _ = expect ps RPAREN in
653-
let bpos = lexpos ps in
654-
let check_range (lo:int64) (hi:int64) : unit =
655-
if (num < lo) or (num > hi)
656-
then raise (err (Printf.sprintf
657-
"integral literal %Ld out of range [%Ld,%Ld]"
658-
num lo hi) ps)
659-
else ()
660-
in
661-
begin
662-
match m with
663-
TY_u8 -> check_range 0L 0xffL
664-
| TY_u16 -> check_range 0L 0xffffL
665-
| TY_u32 -> check_range 0L 0xffffffffL
666-
(* | TY_u64 -> ... *)
667-
| TY_i8 -> check_range (-128L) 127L
668-
| TY_i16 -> check_range (-32768L) 32767L
669-
| TY_i32 -> check_range (-2147483648L) 2147483647L
670-
(*
671-
| TY_i64 -> ...
672-
| TY_f32 -> ...
673-
| TY_f64 -> ...
674-
*)
675-
| _ -> ()
676-
end;
677-
span ps apos bpos
678-
(PEXP_lit
679-
(Ast.LIT_mach
680-
(m, num, str)))
681-
682-
in
683-
begin
684-
bump ps;
685-
expect ps LPAREN;
686-
match peek ps with
687-
LIT_INT (n,s) -> literal (n,s)
688-
| MINUS ->
689-
begin
690-
bump ps;
691-
match peek ps with
692-
LIT_INT (n,s) ->
693-
literal (Int64.neg n, "-" ^ s)
694-
| _ -> raise (unexpected ps)
695-
end
696-
| _ ->
697-
let pexp = parse_pexp ps in
698-
expect ps RPAREN;
699-
let bpos = lexpos ps in
700-
let t = span ps apos bpos (Ast.TY_mach m) in
701-
span ps apos bpos
702-
(PEXP_unop ((Ast.UNOP_cast t), pexp))
703-
end
704-
705607
| POUND ->
706608
bump ps;
707609
let name = parse_name ps in

src/boot/fe/token.ml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,11 @@ type token =
103103
| JOIN
104104

105105
(* Literals *)
106-
| LIT_INT of (int64 * string)
107-
| LIT_FLO of string
106+
| LIT_INT of int64
107+
| LIT_UINT of int64
108+
| LIT_FLOAT of float
109+
| LIT_MACH_INT of Common.ty_mach * int64
110+
| LIT_MACH_FLOAT of Common.ty_mach * float
108111
| LIT_STR of string
109112
| LIT_CHAR of int
110113
| LIT_BOOL of bool
@@ -253,8 +256,13 @@ let rec string_of_tok t =
253256
| JOIN -> "join"
254257

255258
(* Literals *)
256-
| LIT_INT (_,s) -> s
257-
| LIT_FLO n -> n
259+
| LIT_INT i -> Int64.to_string i
260+
| LIT_UINT i -> (Int64.to_string i) ^ "u"
261+
| LIT_FLOAT s -> string_of_float s
262+
| LIT_MACH_INT (tm, i) ->
263+
(Int64.to_string i) ^ (Common.string_of_ty_mach tm)
264+
| LIT_MACH_FLOAT (tm, f) ->
265+
(string_of_float f) ^ (Common.string_of_ty_mach tm)
258266
| LIT_STR s -> ("\"" ^ (String.escaped s) ^ "\"")
259267
| LIT_CHAR c -> ("'" ^ (Common.escaped_char c) ^ "'")
260268
| LIT_BOOL b -> if b then "true" else "false"

src/boot/llvm/lltrans.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -746,12 +746,12 @@ let trans_crate
746746
Ast.LIT_nil -> llnil
747747
| Ast.LIT_bool value ->
748748
Llvm.const_int (Llvm.i1_type llctx) (if value then 1 else 0)
749-
| Ast.LIT_mach (mty, value, _) ->
749+
| Ast.LIT_mach_int (mty, value) ->
750750
let llty = trans_mach_ty mty in
751751
Llvm.const_of_int64 llty value (mach_is_signed mty)
752-
| Ast.LIT_int (value, _) ->
752+
| Ast.LIT_int value ->
753753
Llvm.const_of_int64 (Llvm.i32_type llctx) value true
754-
| Ast.LIT_uint (value, _) ->
754+
| Ast.LIT_uint value ->
755755
Llvm.const_of_int64 (Llvm.i32_type llctx) value false
756756
| Ast.LIT_char ch ->
757757
Llvm.const_int (Llvm.i32_type llctx) ch

src/boot/me/semant.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,7 @@ let rec atom_type (cx:ctxt) (at:Ast.atom) : Ast.ty =
12041204
| Ast.ATOM_literal {node=(Ast.LIT_bool _); id=_} -> Ast.TY_bool
12051205
| Ast.ATOM_literal {node=(Ast.LIT_char _); id=_} -> Ast.TY_char
12061206
| Ast.ATOM_literal {node=(Ast.LIT_nil); id=_} -> Ast.TY_nil
1207-
| Ast.ATOM_literal {node=(Ast.LIT_mach (m,_,_)); id=_} -> Ast.TY_mach m
1207+
| Ast.ATOM_literal {node=(Ast.LIT_mach_int (m,_)); id=_} -> Ast.TY_mach m
12081208
| Ast.ATOM_lval lv -> lval_ty cx lv
12091209
;;
12101210

src/boot/me/trans.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,9 +1268,9 @@ let trans_visitor
12681268
| Ast.LIT_bool false -> imm_false
12691269
| Ast.LIT_bool true -> imm_true
12701270
| Ast.LIT_char c -> imm_of_ty (Int64.of_int c) TY_u32
1271-
| Ast.LIT_int (i, _) -> simm i
1272-
| Ast.LIT_uint (i, _) -> imm i
1273-
| Ast.LIT_mach (m, n, _) -> imm_of_ty n m
1271+
| Ast.LIT_int i -> simm i
1272+
| Ast.LIT_uint i -> imm i
1273+
| Ast.LIT_mach_int (m, n) -> imm_of_ty n m
12741274

12751275
and trans_atom (atom:Ast.atom) : Il.operand =
12761276
iflog

src/boot/me/type.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ let check_stmt (cx:Semant.ctxt) : (fn_ctx -> Ast.stmt -> unit) =
223223
match lit with
224224
Ast.LIT_nil -> Ast.TY_nil
225225
| Ast.LIT_bool _ -> Ast.TY_bool
226-
| Ast.LIT_mach (mty, _, _) -> Ast.TY_mach mty
226+
| Ast.LIT_mach_int (mty, _) -> Ast.TY_mach mty
227227
| Ast.LIT_int _ -> Ast.TY_int
228228
| Ast.LIT_uint _ -> Ast.TY_uint
229229
| Ast.LIT_char _ -> Ast.TY_char

src/comp/fe/lexer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import std._io.buf_reader;
33
iter buffers(buf_reader rdr) -> vec[u8] {
44
while (true) {
55
let vec[u8] v = rdr.read();
6-
if (std._vec.len[u8](v) == uint(0)) {
6+
if (std._vec.len[u8](v) == 0u) {
77
ret;
88
}
99
put v;

src/lib/_int.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ iter range(mutable int lo, int hi) -> int {
2323
iter urange(mutable uint lo, uint hi) -> uint {
2424
while (lo < hi) {
2525
put lo;
26-
lo += uint(1);
26+
lo += 1u;
2727
}
2828
}
2929

3030
fn next_power_of_two(uint n) -> uint {
3131
// FIXME change |* uint(4)| below to |* uint(8) / uint(2)| and watch the
3232
// world explode.
33-
let uint halfbits = sys.rustrt.size_of[uint]() * uint(4);
34-
let uint tmp = n - uint(1);
35-
let uint shift = uint(1);
33+
let uint halfbits = sys.rustrt.size_of[uint]() * 4u;
34+
let uint tmp = n - 1u;
35+
let uint shift = 1u;
3636
while (shift <= halfbits) {
3737
tmp |= tmp >> shift;
38-
shift <<= uint(1);
38+
shift <<= 1u;
3939
}
40-
ret tmp + uint(1);
40+
ret tmp + 1u;
4141
}

0 commit comments

Comments
 (0)