Skip to content

Commit 75edd9f

Browse files
kevinagraydon
authored andcommitted
Move useful ast building functions into their own module.
1 parent 9dcb674 commit 75edd9f

File tree

3 files changed

+50
-39
lines changed

3 files changed

+50
-39
lines changed

src/comp/rustc.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ mod syntax {
7474
mod ext {
7575
mod base;
7676
mod expand;
77+
mod build;
7778

7879
mod fmt;
7980
mod env;

src/comp/syntax/ext/build.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import core::{vec, str, option};
2+
import option::{some};
3+
import codemap::span;
4+
import syntax::ext::base::ext_ctxt;
5+
6+
// NOTE: Moved from fmt.rs which had this fixme:
7+
// FIXME: Cleanup the naming of these functions
8+
9+
fn make_new_lit(cx: ext_ctxt, sp: span, lit: ast::lit_) -> @ast::expr {
10+
let sp_lit = @{node: lit, span: sp};
11+
ret @{id: cx.next_id(), node: ast::expr_lit(sp_lit), span: sp};
12+
}
13+
fn make_new_str(cx: ext_ctxt, sp: span, s: str) -> @ast::expr {
14+
let lit = ast::lit_str(s);
15+
ret make_new_lit(cx, sp, lit);
16+
}
17+
fn make_new_int(cx: ext_ctxt, sp: span, i: int) -> @ast::expr {
18+
let lit = ast::lit_int(i as i64, ast::ty_i);
19+
ret make_new_lit(cx, sp, lit);
20+
}
21+
fn make_new_uint(cx: ext_ctxt, sp: span, u: uint) -> @ast::expr {
22+
let lit = ast::lit_uint(u as u64, ast::ty_u);
23+
ret make_new_lit(cx, sp, lit);
24+
}
25+
fn make_add_expr(cx: ext_ctxt, sp: span, lhs: @ast::expr, rhs: @ast::expr)
26+
-> @ast::expr {
27+
let binexpr = ast::expr_binary(ast::add, lhs, rhs);
28+
ret @{id: cx.next_id(), node: binexpr, span: sp};
29+
}
30+
fn make_path_expr(cx: ext_ctxt, sp: span, idents: [ast::ident]) ->
31+
@ast::expr {
32+
let path = {global: false, idents: idents, types: []};
33+
let sp_path = @{node: path, span: sp};
34+
let pathexpr = ast::expr_path(sp_path);
35+
ret @{id: cx.next_id(), node: pathexpr, span: sp};
36+
}
37+
fn make_vec_expr(cx: ext_ctxt, sp: span, exprs: [@ast::expr]) ->
38+
@ast::expr {
39+
let vecexpr = ast::expr_vec(exprs, ast::imm);
40+
ret @{id: cx.next_id(), node: vecexpr, span: sp};
41+
}
42+
fn make_call(cx: ext_ctxt, sp: span, fn_path: [ast::ident],
43+
args: [@ast::expr]) -> @ast::expr {
44+
let pathexpr = make_path_expr(cx, sp, fn_path);
45+
let callexpr = ast::expr_call(pathexpr, args, false);
46+
ret @{id: cx.next_id(), node: callexpr, span: sp};
47+
}

src/comp/syntax/ext/fmt.rs

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import option::{some};
1010
import extfmt::ct::*;
1111
import base::*;
1212
import codemap::span;
13+
import syntax::ext::build::*;
1314
export expand_syntax_ext;
1415

1516
fn expand_syntax_ext(cx: ext_ctxt, sp: span, arg: @ast::expr,
@@ -41,47 +42,9 @@ fn expand_syntax_ext(cx: ext_ctxt, sp: span, arg: @ast::expr,
4142
// FIXME: A lot of these functions for producing expressions can probably
4243
// be factored out in common with other code that builds expressions.
4344
// FIXME: Cleanup the naming of these functions
45+
// NOTE: Moved many of the common ones to build.rs --kevina
4446
fn pieces_to_expr(cx: ext_ctxt, sp: span, pieces: [piece], args: [@ast::expr])
4547
-> @ast::expr {
46-
fn make_new_lit(cx: ext_ctxt, sp: span, lit: ast::lit_) -> @ast::expr {
47-
let sp_lit = @{node: lit, span: sp};
48-
ret @{id: cx.next_id(), node: ast::expr_lit(sp_lit), span: sp};
49-
}
50-
fn make_new_str(cx: ext_ctxt, sp: span, s: str) -> @ast::expr {
51-
let lit = ast::lit_str(s);
52-
ret make_new_lit(cx, sp, lit);
53-
}
54-
fn make_new_int(cx: ext_ctxt, sp: span, i: int) -> @ast::expr {
55-
let lit = ast::lit_int(i as i64, ast::ty_i);
56-
ret make_new_lit(cx, sp, lit);
57-
}
58-
fn make_new_uint(cx: ext_ctxt, sp: span, u: uint) -> @ast::expr {
59-
let lit = ast::lit_uint(u as u64, ast::ty_u);
60-
ret make_new_lit(cx, sp, lit);
61-
}
62-
fn make_add_expr(cx: ext_ctxt, sp: span, lhs: @ast::expr, rhs: @ast::expr)
63-
-> @ast::expr {
64-
let binexpr = ast::expr_binary(ast::add, lhs, rhs);
65-
ret @{id: cx.next_id(), node: binexpr, span: sp};
66-
}
67-
fn make_path_expr(cx: ext_ctxt, sp: span, idents: [ast::ident]) ->
68-
@ast::expr {
69-
let path = {global: false, idents: idents, types: []};
70-
let sp_path = @{node: path, span: sp};
71-
let pathexpr = ast::expr_path(sp_path);
72-
ret @{id: cx.next_id(), node: pathexpr, span: sp};
73-
}
74-
fn make_vec_expr(cx: ext_ctxt, sp: span, exprs: [@ast::expr]) ->
75-
@ast::expr {
76-
let vecexpr = ast::expr_vec(exprs, ast::imm);
77-
ret @{id: cx.next_id(), node: vecexpr, span: sp};
78-
}
79-
fn make_call(cx: ext_ctxt, sp: span, fn_path: [ast::ident],
80-
args: [@ast::expr]) -> @ast::expr {
81-
let pathexpr = make_path_expr(cx, sp, fn_path);
82-
let callexpr = ast::expr_call(pathexpr, args, false);
83-
ret @{id: cx.next_id(), node: callexpr, span: sp};
84-
}
8548
fn make_rec_expr(cx: ext_ctxt, sp: span,
8649
fields: [{ident: ast::ident, ex: @ast::expr}]) ->
8750
@ast::expr {

0 commit comments

Comments
 (0)