Skip to content

Commit

Permalink
Merge pull request #33 from ratel-rust/for_loops
Browse files Browse the repository at this point in the history
Adding support for if/for/while statements w/o body
  • Loading branch information
maciejhirsz authored Nov 6, 2016
2 parents 72e7266 + f774d37 commit 25a32ff
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 41 deletions.
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ratel"
version = "0.6.1"
version = "0.6.2"
authors = ["Maciej Hirsz <[email protected]>"]
license = "MIT/Apache-2.0"
description = "JavaScript transpiler in Rust"
Expand Down
6 changes: 5 additions & 1 deletion core/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,10 @@ impl Code for Statement {
}
},

Statement::Empty => {
gen.write_byte(b';');
}

Statement::Expression {
ref value,
} => {
Expand Down Expand Up @@ -771,7 +775,7 @@ impl Code for Statement {
if let Some(ref alternate) = *alternate {
gen.write_bytes(b" else ");
gen.write(alternate);
};
}
},

Statement::While {
Expand Down
1 change: 1 addition & 0 deletions core/src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ pub struct VariableDeclarator {

#[derive(Debug, PartialEq, Clone)]
pub enum Statement {
Empty,
Block {
body: Vec<Statement>,
},
Expand Down
57 changes: 25 additions & 32 deletions core/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,23 +225,6 @@ impl<'a> Parser<'a> {
Ok(Expression::Object(try!(self.object_member_list())))
}

#[inline]
fn block_or_statement(&mut self) -> Result<Statement> {
match peek!(self) {
BraceOpen => {
self.consume();

Ok(Statement::Block {
body: try!(self.block_body_tail())
})
},
_ => {
let token = next!(self);
self.expression_statement(token)
}
}
}

#[inline]
fn block_statement(&mut self) -> Result<Statement> {
Ok(Statement::Block {
Expand Down Expand Up @@ -757,21 +740,16 @@ impl<'a> Parser<'a> {

expect!(self, ParenClose);

let consequent = Box::new(try!(self.block_or_statement()));
let token = next!(self);
let consequent = Box::new(try!(self.statement(token)));

let alternate = match peek!(self) {
Else => {
self.consume();

match peek!(self) {
If => {
self.consume();

Some(Box::new(try!(self.if_statement())))
},
let token = next!(self);

_ => Some(Box::new(try!(self.block_or_statement())))
}
Some(Box::new(try!(self.statement(token))))
},

_ => None
Expand All @@ -792,9 +770,12 @@ impl<'a> Parser<'a> {

expect!(self, ParenClose);

let token = next!(self);
let body = Box::new(try!(self.statement(token)));

Ok(Statement::While {
test: test,
body: Box::new(try!(self.block_or_statement())),
body: body,
})
}

Expand Down Expand Up @@ -854,11 +835,14 @@ impl<'a> Parser<'a> {
expect!(self, ParenClose);
}

let token = next!(self);
let body = Box::new(try!(self.statement(token)));

Ok(Statement::For {
init: init,
test: test,
update: update,
body: Box::new(try!(self.block_or_statement())),
body: body,
})
}

Expand All @@ -868,10 +852,13 @@ impl<'a> Parser<'a> {

expect!(self, ParenClose);

let token = next!(self);
let body = Box::new(try!(self.statement(token)));

Ok(Statement::ForIn {
left: left,
right: right,
body: Box::new(try!(self.block_or_statement())),
body: body,
})
}

Expand All @@ -880,10 +867,13 @@ impl<'a> Parser<'a> {

expect!(self, ParenClose);

let token = next!(self);
let body = Box::new(try!(self.statement(token)));

Ok(Statement::ForIn {
left: left,
right: right,
body: Box::new(try!(self.block_or_statement())),
body: body,
})
}

Expand All @@ -892,10 +882,13 @@ impl<'a> Parser<'a> {

expect!(self, ParenClose);

let token = next!(self);
let body = Box::new(try!(self.statement(token)));

Ok(Statement::ForOf {
left: left,
right: right,
body: Box::new(try!(self.block_or_statement())),
body: body,
})
}

Expand Down Expand Up @@ -1058,7 +1051,7 @@ impl<'a> Parser<'a> {
#[inline]
fn statement(&mut self, token: Token) -> Result<Statement> {
match token {
Semicolon => Ok(Statement::Transparent { body: Vec::new() }),
Semicolon => Ok(Statement::Empty),
BraceOpen => self.block_statement(),
Declaration(kind) => self.variable_declaration_statement(kind),
Return => self.return_statement(),
Expand Down
4 changes: 4 additions & 0 deletions core/src/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,10 @@ impl Transformable for Statement {
return;
},

Statement::Empty => {
return;
},

Statement::Return {
ref mut value,
} => {
Expand Down
Loading

0 comments on commit 25a32ff

Please sign in to comment.