diff options
| author | Kevin Atkinson <kevina@cs.utah.edu> | 2012-01-25 14:47:56 -0700 |
|---|---|---|
| committer | Graydon Hoare <graydon@mozilla.com> | 2012-02-03 17:48:17 -0800 |
| commit | 75edd9ff69625292b1c4d5f05502d1fd28b39f55 (patch) | |
| tree | ce39641a83407c98b2d355e7759b629778083be2 | |
| parent | 9dcb674525c5169a8649475699e60e0cbaed8c0a (diff) | |
Move useful ast building functions into their own module.
| -rw-r--r-- | src/comp/rustc.rc | 1 | ||||
| -rw-r--r-- | src/comp/syntax/ext/build.rs | 47 | ||||
| -rw-r--r-- | src/comp/syntax/ext/fmt.rs | 41 |
3 files changed, 50 insertions, 39 deletions
diff --git a/src/comp/rustc.rc b/src/comp/rustc.rc index 6f63bb36933..40e66037f3a 100644 --- a/src/comp/rustc.rc +++ b/src/comp/rustc.rc @@ -74,6 +74,7 @@ mod syntax { mod ext { mod base; mod expand; + mod build; mod fmt; mod env; diff --git a/src/comp/syntax/ext/build.rs b/src/comp/syntax/ext/build.rs new file mode 100644 index 00000000000..821e499c4a8 --- /dev/null +++ b/src/comp/syntax/ext/build.rs @@ -0,0 +1,47 @@ +import core::{vec, str, option}; +import option::{some}; +import codemap::span; +import syntax::ext::base::ext_ctxt; + +// NOTE: Moved from fmt.rs which had this fixme: +// FIXME: Cleanup the naming of these functions + +fn make_new_lit(cx: ext_ctxt, sp: span, lit: ast::lit_) -> @ast::expr { + let sp_lit = @{node: lit, span: sp}; + ret @{id: cx.next_id(), node: ast::expr_lit(sp_lit), span: sp}; +} +fn make_new_str(cx: ext_ctxt, sp: span, s: str) -> @ast::expr { + let lit = ast::lit_str(s); + ret make_new_lit(cx, sp, lit); +} +fn make_new_int(cx: ext_ctxt, sp: span, i: int) -> @ast::expr { + let lit = ast::lit_int(i as i64, ast::ty_i); + ret make_new_lit(cx, sp, lit); +} +fn make_new_uint(cx: ext_ctxt, sp: span, u: uint) -> @ast::expr { + let lit = ast::lit_uint(u as u64, ast::ty_u); + ret make_new_lit(cx, sp, lit); +} +fn make_add_expr(cx: ext_ctxt, sp: span, lhs: @ast::expr, rhs: @ast::expr) + -> @ast::expr { + let binexpr = ast::expr_binary(ast::add, lhs, rhs); + ret @{id: cx.next_id(), node: binexpr, span: sp}; +} +fn make_path_expr(cx: ext_ctxt, sp: span, idents: [ast::ident]) -> + @ast::expr { + let path = {global: false, idents: idents, types: []}; + let sp_path = @{node: path, span: sp}; + let pathexpr = ast::expr_path(sp_path); + ret @{id: cx.next_id(), node: pathexpr, span: sp}; +} +fn make_vec_expr(cx: ext_ctxt, sp: span, exprs: [@ast::expr]) -> + @ast::expr { + let vecexpr = ast::expr_vec(exprs, ast::imm); + ret @{id: cx.next_id(), node: vecexpr, span: sp}; +} +fn make_call(cx: ext_ctxt, sp: span, fn_path: [ast::ident], + args: [@ast::expr]) -> @ast::expr { + let pathexpr = make_path_expr(cx, sp, fn_path); + let callexpr = ast::expr_call(pathexpr, args, false); + ret @{id: cx.next_id(), node: callexpr, span: sp}; +} diff --git a/src/comp/syntax/ext/fmt.rs b/src/comp/syntax/ext/fmt.rs index 0dfe2cb17d0..e9e0e2f194a 100644 --- a/src/comp/syntax/ext/fmt.rs +++ b/src/comp/syntax/ext/fmt.rs @@ -10,6 +10,7 @@ import option::{some}; import extfmt::ct::*; import base::*; import codemap::span; +import syntax::ext::build::*; export expand_syntax_ext; 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, // FIXME: A lot of these functions for producing expressions can probably // be factored out in common with other code that builds expressions. // FIXME: Cleanup the naming of these functions +// NOTE: Moved many of the common ones to build.rs --kevina fn pieces_to_expr(cx: ext_ctxt, sp: span, pieces: [piece], args: [@ast::expr]) -> @ast::expr { - fn make_new_lit(cx: ext_ctxt, sp: span, lit: ast::lit_) -> @ast::expr { - let sp_lit = @{node: lit, span: sp}; - ret @{id: cx.next_id(), node: ast::expr_lit(sp_lit), span: sp}; - } - fn make_new_str(cx: ext_ctxt, sp: span, s: str) -> @ast::expr { - let lit = ast::lit_str(s); - ret make_new_lit(cx, sp, lit); - } - fn make_new_int(cx: ext_ctxt, sp: span, i: int) -> @ast::expr { - let lit = ast::lit_int(i as i64, ast::ty_i); - ret make_new_lit(cx, sp, lit); - } - fn make_new_uint(cx: ext_ctxt, sp: span, u: uint) -> @ast::expr { - let lit = ast::lit_uint(u as u64, ast::ty_u); - ret make_new_lit(cx, sp, lit); - } - fn make_add_expr(cx: ext_ctxt, sp: span, lhs: @ast::expr, rhs: @ast::expr) - -> @ast::expr { - let binexpr = ast::expr_binary(ast::add, lhs, rhs); - ret @{id: cx.next_id(), node: binexpr, span: sp}; - } - fn make_path_expr(cx: ext_ctxt, sp: span, idents: [ast::ident]) -> - @ast::expr { - let path = {global: false, idents: idents, types: []}; - let sp_path = @{node: path, span: sp}; - let pathexpr = ast::expr_path(sp_path); - ret @{id: cx.next_id(), node: pathexpr, span: sp}; - } - fn make_vec_expr(cx: ext_ctxt, sp: span, exprs: [@ast::expr]) -> - @ast::expr { - let vecexpr = ast::expr_vec(exprs, ast::imm); - ret @{id: cx.next_id(), node: vecexpr, span: sp}; - } - fn make_call(cx: ext_ctxt, sp: span, fn_path: [ast::ident], - args: [@ast::expr]) -> @ast::expr { - let pathexpr = make_path_expr(cx, sp, fn_path); - let callexpr = ast::expr_call(pathexpr, args, false); - ret @{id: cx.next_id(), node: callexpr, span: sp}; - } fn make_rec_expr(cx: ext_ctxt, sp: span, fields: [{ident: ast::ident, ex: @ast::expr}]) -> @ast::expr { |
