Skip to content

Commit 15e6b43

Browse files
committed
---
yaml --- r: 664 b: refs/heads/master c: 7287d3a h: refs/heads/master v: v3
1 parent 2c8f445 commit 15e6b43

File tree

11 files changed

+47
-1
lines changed

11 files changed

+47
-1
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: 43ec78636fdbe3a01a0d2f97cbf1025c603b8b3e
2+
refs/heads/master: 7287d3aaa0545a9b22cea874f2d751de4fefd23d

trunk/src/boot/fe/ast.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ and mod_item' =
415415
| MOD_ITEM_mod of (mod_view * mod_items)
416416
| MOD_ITEM_fn of fn
417417
| MOD_ITEM_obj of obj
418+
| MOD_ITEM_const of (ty * expr option)
418419

419420
and mod_item_decl =
420421
{
@@ -1438,6 +1439,18 @@ and fmt_mod_item (ff:Format.formatter) (id:ident) (item:mod_item) : unit =
14381439

14391440
| MOD_ITEM_obj obj ->
14401441
fmt_obj ff id params obj
1442+
1443+
| MOD_ITEM_const (ty,e) ->
1444+
fmt ff "const ";
1445+
fmt_ty ff ty;
1446+
begin
1447+
match e with
1448+
None -> ()
1449+
| Some e ->
1450+
fmt ff " = ";
1451+
fmt_expr ff e
1452+
end;
1453+
fmt ff ";"
14411454
end
14421455

14431456
and fmt_import (ff:Format.formatter) (ident:ident) (name:name) : unit =

trunk/src/boot/fe/item.ml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ let rec parse_expr (ps:pstate) : (Ast.stmt array * Ast.expr) =
1919
let pexp = ctxt "expr" Pexp.parse_pexp ps in
2020
Pexp.desugar_expr ps pexp
2121

22+
and parse_prim_expr (ps:pstate) : Ast.expr =
23+
let pexp = ctxt "expr" Pexp.parse_pexp ps in
24+
let (stmts, expr) = Pexp.desugar_expr ps pexp in
25+
if Array.length stmts = 0
26+
then expr
27+
else raise (Parse_err (ps, "expected primitive expression"))
28+
2229
and parse_expr_atom (ps:pstate) : (Ast.stmt array * Ast.atom) =
2330
let pexp = ctxt "expr" Pexp.parse_pexp ps in
2431
Pexp.desugar_expr_atom ps pexp
@@ -944,6 +951,17 @@ and parse_mod_item (ps:pstate)
944951
(decl params (Ast.MOD_ITEM_fn fn))) |]
945952
end
946953

954+
| CONST ->
955+
bump ps;
956+
let ty = Pexp.parse_ty ps in
957+
let ident = Pexp.parse_ident ps in
958+
expect ps EQ;
959+
let expr = parse_prim_expr ps in
960+
expect ps SEMI;
961+
let bpos = lexpos ps in
962+
[| (ident, span ps apos bpos
963+
(decl [||] (Ast.MOD_ITEM_const (ty, Some expr)))) |]
964+
947965
| MOD ->
948966
bump ps;
949967
let (ident, params) = parse_ident_and_params ps "mod" in

trunk/src/boot/fe/lexer.mll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
("export", EXPORT);
111111

112112
("let", LET);
113+
("const", CONST);
113114

114115
("log", LOG);
115116
("spawn", SPAWN);

trunk/src/boot/fe/token.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ type token =
9393

9494
(* Value / stmt declarators *)
9595
| LET
96+
| CONST
9697

9798
(* Magic runtime services *)
9899
| LOG
@@ -246,6 +247,7 @@ let rec string_of_tok t =
246247

247248
(* Value / stmt declarators. *)
248249
| LET -> "let"
250+
| CONST -> "const"
249251

250252
(* Magic runtime services *)
251253
| LOG -> "log"

trunk/src/boot/me/semant.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,7 @@ let ty_of_mod_item (item:Ast.mod_item) : Ast.ty =
13661366
Ast.MOD_ITEM_type _ -> Ast.TY_type
13671367
| Ast.MOD_ITEM_fn f -> (Ast.TY_fn (ty_fn_of_fn f))
13681368
| Ast.MOD_ITEM_mod _ -> bug () "Semant.ty_of_mod_item on mod"
1369+
| Ast.MOD_ITEM_const (ty, _) -> ty
13691370
| Ast.MOD_ITEM_obj ob ->
13701371
let taux = { Ast.fn_effect = Ast.PURE;
13711372
Ast.fn_is_iter = false }

trunk/src/boot/me/type.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ let check_stmt (cx:Semant.ctxt) : (fn_ctx -> Ast.stmt -> unit) =
295295
LTYPE_mono ty
296296
else
297297
LTYPE_poly ((Array.map (fun p -> p.Common.node) params), ty)
298+
| Ast.MOD_ITEM_const (ty, _) -> LTYPE_mono ty
298299
| Ast.MOD_ITEM_type _ ->
299300
Common.err None "Type-item used in non-type context"
300301
in

trunk/src/boot/me/walk.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ and walk_mod_item
173173
let children _ =
174174
match item.node.Ast.decl_item with
175175
Ast.MOD_ITEM_type (_, ty) -> walk_ty v ty
176+
| Ast.MOD_ITEM_const (ty, e) ->
177+
walk_ty v ty;
178+
walk_option (walk_expr v) e
176179
| Ast.MOD_ITEM_fn f -> walk_fn v f item.id
177180
| Ast.MOD_ITEM_tag (hdr, _, _) ->
178181
walk_header_slots v hdr

trunk/src/comp/fe/lexer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ fn new_reader(stdio_reader rdr, str filename) -> reader
137137
keywords.insert("export", token.EXPORT());
138138

139139
keywords.insert("let", token.LET());
140+
keywords.insert("const", token.CONST());
140141

141142
keywords.insert("log", token.LOG());
142143
keywords.insert("spawn", token.SPAWN());

trunk/src/comp/fe/token.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ tag token {
103103

104104
/* Value / stmt declarators */
105105
LET();
106+
CONST();
106107

107108
/* Magic runtime services */
108109
LOG();
@@ -261,6 +262,7 @@ fn to_str(token t) -> str {
261262

262263
/* Value / stmt declarators */
263264
case (LET()) { ret "let"; }
265+
case (CONST()) { ret "const"; }
264266

265267
/* Magic runtime services */
266268
case (LOG()) { ret "log"; }

trunk/src/test/run-pass/const.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const int i = 10;
2+
3+
fn main() {
4+
}

0 commit comments

Comments
 (0)