Skip to content

Commit

Permalink
Merge pull request #34 from ratel-rust/class_expressions
Browse files Browse the repository at this point in the history
Fixes and some refactor
  • Loading branch information
maciejhirsz authored Nov 6, 2016
2 parents 7793122 + 3f9a279 commit 72e7266
Show file tree
Hide file tree
Showing 12 changed files with 346 additions and 312 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.0"
version = "0.6.1"
authors = ["Maciej Hirsz <[email protected]>"]
license = "MIT/Apache-2.0"
description = "JavaScript transpiler in Rust"
Expand Down
55 changes: 33 additions & 22 deletions core/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::ptr;
use std::io::Write;

use grammar::*;
use grammar::OperatorType::*;
use operator::OperatorKind;
use owned_slice::OwnedSlice;

/// The `Generator` is a wrapper around an owned `String` that's used to
Expand Down Expand Up @@ -160,9 +160,11 @@ impl<T: Code> Code for Option<T> {
}
}

impl Code for OperatorType {
impl Code for OperatorKind {
#[inline]
fn to_code(&self, gen: &mut Generator) {
use ::operator::OperatorKind::*;

gen.write_bytes(match *self {
FatArrow => b"=>",
Accessor => b".",
Expand Down Expand Up @@ -451,6 +453,7 @@ impl Code for Expression {
..
} => {
let bp = self.binding_power();
let spacing = operator.is_word() || !gen.minify;

if left.binding_power() < bp {
gen.write_byte(b'(');
Expand All @@ -459,9 +462,14 @@ impl Code for Expression {
} else {
gen.write(left);
}
gen.write_min(b" ", b"");

if spacing {
gen.write_byte(b' ');
}
gen.write(operator);
gen.write_min(b" ", b"");
if spacing {
gen.write_byte(b' ');
}

if right.needs_parens(bp) {
gen.write_byte(b'(');
Expand All @@ -477,6 +485,9 @@ impl Code for Expression {
ref operand,
} => {
gen.write(operator);
if operator.is_word() {
gen.write_byte(b' ');
}
gen.write(operand);
},

Expand Down Expand Up @@ -729,6 +740,24 @@ impl Code for Statement {
gen.new_line();
},

Statement::Class {
ref name,
ref extends,
ref body,
} => {
gen.new_line();
gen.write_bytes(b"class ");
gen.write(name);
if let &Some(ref super_class) = extends {
gen.write_bytes(b" extends ");
gen.write(super_class);
}
gen.write_min(b" {", b"{");
gen.write_block(body);
gen.write_byte(b'}');
gen.new_line();
},

Statement::If {
ref test,
ref consequent,
Expand Down Expand Up @@ -799,24 +828,6 @@ impl Code for Statement {
gen.write(body);
},

Statement::Class {
ref name,
ref extends,
ref body,
} => {
gen.new_line();
gen.write_bytes(b"class ");
gen.write(name);
if let &Some(ref super_class) = extends {
gen.write_bytes(b" extends ");
gen.write(super_class);
}
gen.write_min(b" {", b"{");
gen.write_block(body);
gen.write_byte(b'}');
gen.new_line();
},

Statement::Throw {
ref value,
} => {
Expand Down
227 changes: 10 additions & 217 deletions core/src/grammar.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use owned_slice::OwnedSlice;
use operator::OperatorKind;

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Value {
Expand All @@ -18,214 +19,6 @@ pub struct Parameter {
pub default: Option<Box<Expression>>
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum OperatorType {
FatArrow, // … => …
Accessor, // … . …
New, // new …
Increment, // ++ … | … ++
Decrement, // -- … | … --
LogicalNot, // ! …
BitwiseNot, // ~ …
Typeof, // typeof …
Void, // void …
Delete, // delete …
Multiplication, // … * …
Division, // … / …
Remainder, // … % …
Exponent, // … ** …
Addition, // … + … | + …
Substraction, // … - … | - …
BitShiftLeft, // … << …
BitShiftRight, // … >> …
UBitShiftRight, // … >>> …
Lesser, // … < …
LesserEquals, // … <= …
Greater, // … > …
GreaterEquals, // … >= …
Instanceof, // … instanceof …
In, // … in …
StrictEquality, // … === …
StrictInequality, // … !== …
Equality, // … == …
Inequality, // … != …
BitwiseAnd, // … & …
BitwiseXor, // … ^ …
BitwiseOr, // … | …
LogicalAnd, // … && …
LogicalOr, // … || …
Conditional, // … ? … : …
Assign, // … = …
AddAssign, // … += …
SubstractAssign, // … -= …
ExponentAssign, // … **= …
MultiplyAssign, // … *= …
DivideAssign, // … /= …
RemainderAssign, // … %= …
BSLAssign, // … <<= …
BSRAssign, // … >>= …
UBSRAssign, // … >>>= …
BitAndAssign, // … &= …
BitXorAssign, // … ^= …
BitOrAssign, // … |= …
Spread, // ... …
}
use self::OperatorType::*;

impl OperatorType {
/// According to the Operator Precedence Table
/// Note: Unary opearotrs default to 15!
pub fn binding_power(&self) -> u8 {
match *self {
FatArrow |
Accessor => 18,

New => 17,

Increment |
Decrement => 16,

LogicalNot |
BitwiseNot |
Typeof |
Void |
Delete => 15,

Multiplication |
Division |
Remainder |
Exponent => 14,

Addition |
Substraction => 13,

BitShiftLeft |
BitShiftRight |
UBitShiftRight => 12,

Lesser |
LesserEquals |
Greater |
GreaterEquals |
Instanceof |
In => 11,

StrictEquality |
StrictInequality |
Equality |
Inequality => 10,

BitwiseAnd => 9,
BitwiseXor => 8,
BitwiseOr => 7,
LogicalAnd => 6,
LogicalOr => 5,
Conditional => 4,

Assign |
AddAssign |
SubstractAssign |
ExponentAssign |
MultiplyAssign |
DivideAssign |
RemainderAssign |
BSLAssign |
BSRAssign |
UBSRAssign |
BitAndAssign |
BitXorAssign |
BitOrAssign => 3,

Spread => 1,
}
}

pub fn prefix(&self) -> bool {
match *self {
LogicalNot |
BitwiseNot |
Typeof |
Void |
Delete |
New |
Spread |
Increment |
Decrement |
Addition |
Substraction => true,

_ => false
}
}

pub fn infix(&self) -> bool {
match *self {
FatArrow |
Accessor |
Multiplication |
Division |
Remainder |
Exponent |
StrictEquality |
StrictInequality |
Equality |
Inequality |
Lesser |
LesserEquals |
Greater |
GreaterEquals |
Instanceof |
In |
BitShiftLeft |
BitShiftRight |
UBitShiftRight |
BitwiseAnd |
BitwiseXor |
BitwiseOr |
LogicalAnd |
LogicalOr |
Conditional |
Addition |
Substraction |
Assign |
AddAssign |
SubstractAssign |
ExponentAssign |
MultiplyAssign |
DivideAssign |
RemainderAssign |
BSLAssign |
BSRAssign |
UBSRAssign |
BitAndAssign |
BitXorAssign |
BitOrAssign => true,

_ => false
}
}

pub fn assignment(&self) -> bool {
match *self {
Assign |
AddAssign |
SubstractAssign |
ExponentAssign |
MultiplyAssign |
DivideAssign |
RemainderAssign |
BSLAssign |
BSRAssign |
UBSRAssign |
BitAndAssign |
BitXorAssign |
BitOrAssign => true,

_ => false
}
}
}

#[derive(Debug, PartialEq, Clone)]
pub enum Expression {
This,
Expand Down Expand Up @@ -257,16 +50,16 @@ pub enum Expression {
},
Binary {
parenthesized: bool,
operator: OperatorType,
operator: OperatorKind,
left: Box<Expression>,
right: Box<Expression>,
},
Prefix {
operator: OperatorType,
operator: OperatorKind,
operand: Box<Expression>,
},
Postfix {
operator: OperatorType,
operator: OperatorKind,
operand: Box<Expression>,
},
Conditional {
Expand Down Expand Up @@ -323,7 +116,7 @@ impl Expression {
}

#[inline]
pub fn binary<E: Into<Expression>>(left: E, operator: OperatorType, right: E) -> Self {
pub fn binary<E: Into<Expression>>(left: E, operator: OperatorKind, right: E) -> Self {
Expression::Binary {
parenthesized: false,
operator: operator,
Expand Down Expand Up @@ -511,6 +304,11 @@ pub enum Statement {
params: Vec<Parameter>,
body: Vec<Statement>,
},
Class {
name: OwnedSlice,
extends: Option<OwnedSlice>,
body: Vec<ClassMember>,
},
If {
test: Expression,
consequent: Box<Statement>,
Expand All @@ -536,11 +334,6 @@ pub enum Statement {
right: Expression,
body: Box<Statement>,
},
Class {
name: OwnedSlice,
extends: Option<OwnedSlice>,
body: Vec<ClassMember>,
},
Throw {
value: Expression
},
Expand Down
Loading

0 comments on commit 72e7266

Please sign in to comment.