Skip to content

Commit 73058aa

Browse files
author
andrewleverette
committed
Merge branch 'master' of https://github.com/findoraorg/codegen into findoraorg-master
# Conflicts: # tests/codegen.rs
2 parents a537858 + 8aa7221 commit 73058aa

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/lib.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub struct Scope {
4848
enum Item {
4949
Module(Module),
5050
Struct(Struct),
51+
Function(Function),
5152
Trait(Trait),
5253
Enum(Enum),
5354
Impl(Impl),
@@ -372,6 +373,22 @@ impl Scope {
372373
self
373374
}
374375

376+
/// Push a new function definition, returning a mutable reference to it.
377+
pub fn new_fn(&mut self, name: &str) -> &mut Function {
378+
self.push_fn(Function::new(name));
379+
380+
match *self.items.last_mut().unwrap() {
381+
Item::Function(ref mut v) => v,
382+
_ => unreachable!(),
383+
}
384+
}
385+
386+
/// Push a function definition
387+
pub fn push_fn(&mut self, item: Function) -> &mut Self {
388+
self.items.push(Item::Function(item));
389+
self
390+
}
391+
375392
/// Push a new trait definition, returning a mutable reference to it.
376393
pub fn new_trait(&mut self, name: &str) -> &mut Trait {
377394
self.push_trait(Trait::new(name));
@@ -458,6 +475,7 @@ impl Scope {
458475
match *item {
459476
Item::Module(ref v) => v.fmt(fmt)?,
460477
Item::Struct(ref v) => v.fmt(fmt)?,
478+
Item::Function(ref v) => v.fmt(false, fmt)?,
461479
Item::Trait(ref v) => v.fmt(fmt)?,
462480
Item::Enum(ref v) => v.fmt(fmt)?,
463481
Item::Impl(ref v) => v.fmt(fmt)?,
@@ -623,6 +641,17 @@ impl Module {
623641
self
624642
}
625643

644+
/// Push a new function definition, returning a mutable reference to it.
645+
pub fn new_fn(&mut self, name: &str) -> &mut Function {
646+
self.scope.new_fn(name)
647+
}
648+
649+
/// Push a function definition
650+
pub fn push_fn(&mut self, item: Function) -> &mut Self {
651+
self.scope.push_fn(item);
652+
self
653+
}
654+
626655
/// Push a new enum definition, returning a mutable reference to it.
627656
pub fn new_enum(&mut self, name: &str) -> &mut Enum {
628657
self.scope.new_enum(name)
@@ -1509,7 +1538,7 @@ impl Import {
15091538
}
15101539
}
15111540

1512-
// ===== impl Func =====
1541+
// ===== impl Function =====
15131542

15141543
impl Function {
15151544
/// Return a new function definition.

tests/codegen.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
extern crate codegen;
22

3-
use codegen::{Field, Scope, Struct, Variant};
3+
use codegen::*;
44
#[test]
55
fn empty_scope() {
66
let scope = Scope::new();
@@ -83,6 +83,25 @@ struct Foo {
8383
assert_eq!(scope.to_string(), &expect[1..]);
8484
}
8585

86+
#[test]
87+
fn single_fn() {
88+
let mut scope = Scope::new();
89+
scope.new_fn("my_fn")
90+
.vis("pub")
91+
.arg("foo", Type::new("uint"))
92+
.ret(Type::new("uint"))
93+
.line("let res = foo + 1;")
94+
.line("res");
95+
96+
let expect = r#"
97+
pub fn my_fn(foo: uint) -> uint {
98+
let res = foo + 1;
99+
res
100+
}"#;
101+
102+
assert_eq!(scope.to_string(), &expect[1..]);
103+
}
104+
86105
#[test]
87106
fn empty_struct() {
88107
let mut scope = Scope::new();

0 commit comments

Comments
 (0)