use crate::data::*;
pub fn get(expr: Expr, property_name: &str) -> Expr {
Expr::Get(Box::new(GetExpr {
expr,
property_expr: Expr::StringLiteral(String::from(property_name)),
}))
}
pub fn call_method(expr: Expr, method_name: &str, parameter_list: Vec<Expr>) -> Expr {
Expr::Call(Box::new(CallExpr {
expr: get(expr, method_name),
parameter_list,
}))
}
pub fn minus(expr: Expr) -> Expr {
Expr::UnaryOperator(Box::new(UnaryOperatorExpr {
operator: UnaryOperator::Minus,
expr,
}))
}
pub fn bitwise_not(expr: Expr) -> Expr {
Expr::UnaryOperator(Box::new(UnaryOperatorExpr {
operator: UnaryOperator::BitwiseNot,
expr,
}))
}
pub fn logical_not(expr: Expr) -> Expr {
Expr::UnaryOperator(Box::new(UnaryOperatorExpr {
operator: UnaryOperator::LogicalNot,
expr,
}))
}
pub fn exponentiation(left: Expr, right: Expr) -> Expr {
Expr::BinaryOperator(Box::new(BinaryOperatorExpr {
operator: BinaryOperator::Multiplication,
left,
right,
}))
}
pub fn multiplication(left: Expr, right: Expr) -> Expr {
Expr::BinaryOperator(Box::new(BinaryOperatorExpr {
operator: BinaryOperator::Multiplication,
left,
right,
}))
}
pub fn division(left: Expr, right: Expr) -> Expr {
Expr::BinaryOperator(Box::new(BinaryOperatorExpr {
operator: BinaryOperator::Division,
left,
right,
}))
}
pub fn modulo(left: Expr, right: Expr) -> Expr {
Expr::BinaryOperator(Box::new(BinaryOperatorExpr {
operator: BinaryOperator::Remainder,
left,
right,
}))
}
pub fn addition(left: Expr, right: Expr) -> Expr {
Expr::BinaryOperator(Box::new(BinaryOperatorExpr {
operator: BinaryOperator::Addition,
left,
right,
}))
}
pub fn subtraction(left: Expr, right: Expr) -> Expr {
Expr::BinaryOperator(Box::new(BinaryOperatorExpr {
operator: BinaryOperator::Subtraction,
left,
right,
}))
}
pub fn left_shift(left: Expr, right: Expr) -> Expr {
Expr::BinaryOperator(Box::new(BinaryOperatorExpr {
operator: BinaryOperator::LeftShift,
left,
right,
}))
}
pub fn signed_right_shift(left: Expr, right: Expr) -> Expr {
Expr::BinaryOperator(Box::new(BinaryOperatorExpr {
operator: BinaryOperator::SignedRightShift,
left,
right,
}))
}
pub fn unsigned_right_shift(left: Expr, right: Expr) -> Expr {
Expr::BinaryOperator(Box::new(BinaryOperatorExpr {
operator: BinaryOperator::UnsignedRightShift,
left,
right,
}))
}
pub fn less_than(left: Expr, right: Expr) -> Expr {
Expr::BinaryOperator(Box::new(BinaryOperatorExpr {
operator: BinaryOperator::LessThan,
left,
right,
}))
}
pub fn less_than_or_equal(left: Expr, right: Expr) -> Expr {
Expr::BinaryOperator(Box::new(BinaryOperatorExpr {
operator: BinaryOperator::LessThanOrEqual,
left,
right,
}))
}
pub fn equal(left: Expr, right: Expr) -> Expr {
Expr::BinaryOperator(Box::new(BinaryOperatorExpr {
operator: BinaryOperator::Equal,
left,
right,
}))
}
pub fn not_equal(left: Expr, right: Expr) -> Expr {
Expr::BinaryOperator(Box::new(BinaryOperatorExpr {
operator: BinaryOperator::NotEqual,
left,
right,
}))
}
pub fn bitwise_and(left: Expr, right: Expr) -> Expr {
Expr::BinaryOperator(Box::new(BinaryOperatorExpr {
operator: BinaryOperator::BitwiseAnd,
left,
right,
}))
}
pub fn bitwise_xor(left: Expr, right: Expr) -> Expr {
Expr::BinaryOperator(Box::new(BinaryOperatorExpr {
operator: BinaryOperator::BitwiseXOr,
left,
right,
}))
}
pub fn bitwise_or(left: Expr, right: Expr) -> Expr {
Expr::BinaryOperator(Box::new(BinaryOperatorExpr {
operator: BinaryOperator::BitwiseOr,
left,
right,
}))
}
pub fn logical_and(left: Expr, right: Expr) -> Expr {
Expr::BinaryOperator(Box::new(BinaryOperatorExpr {
operator: BinaryOperator::LogicalAnd,
left,
right,
}))
}
pub fn logical_or(left: Expr, right: Expr) -> Expr {
Expr::BinaryOperator(Box::new(BinaryOperatorExpr {
operator: BinaryOperator::LogicalOr,
left,
right,
}))
}
pub fn call_number_method(module_name: &str, parameter_list: Vec<Expr>) -> Expr {
call_method(
Expr::GlobalObjects(identifer::from_string("Number")),
module_name,
parameter_list,
)
}
pub fn call_math_method(method_name: &str, parameter_list: Vec<Expr>) -> Expr {
call_method(
Expr::GlobalObjects(identifer::from_string("Math")),
method_name,
parameter_list,
)
}
pub fn new_date() -> Expr {
Expr::New(Box::new(CallExpr {
expr: Expr::GlobalObjects(identifer::from_string("Date")),
parameter_list: vec![],
}))
}
pub fn new_uint8array(length_or_iterable: Expr) -> Expr {
Expr::New(Box::new(CallExpr {
expr: Expr::GlobalObjects(identifer::from_string("Uint8Array")),
parameter_list: vec![length_or_iterable],
}))
}
pub fn new_map(init_key_value_list: Expr) -> Expr {
Expr::New(Box::new(CallExpr {
expr: Expr::GlobalObjects(identifer::from_string("Map")),
parameter_list: vec![init_key_value_list],
}))
}
pub fn new_set(init_value_list: Expr) -> Expr {
Expr::New(Box::new(CallExpr {
expr: Expr::GlobalObjects(identifer::from_string("Set")),
parameter_list: vec![init_value_list],
}))
}
pub fn console_log(expr: Expr) -> Statement {
Statement::EvaluateExpr(call_method(
Expr::GlobalObjects(identifer::from_string("console")),
"log",
vec![expr],
))
}
pub fn array_type(element_type: Type) -> Type {
Type::WithTypeParameter(Box::new(TypeWithTypeParameter {
r#type: Type::ScopeInGlobal(identifer::from_string("Array")),
type_parameter_list: vec![element_type],
}))
}
pub fn readonly_array_type(element_type: Type) -> Type {
Type::WithTypeParameter(Box::new(TypeWithTypeParameter {
r#type: Type::ScopeInGlobal(identifer::from_string("ReadonlyArray")),
type_parameter_list: vec![element_type],
}))
}
pub fn uint8array_type() -> Type {
Type::ScopeInGlobal(identifer::from_string("Uint8Array"))
}
pub fn promise_type(return_type: Type) -> Type {
Type::WithTypeParameter(Box::new(TypeWithTypeParameter {
r#type: Type::ScopeInGlobal(identifer::from_string("Promise")),
type_parameter_list: vec![return_type],
}))
}
pub fn date_type() -> Type {
Type::ScopeInGlobal(identifer::from_string("Date"))
}
pub fn map_type(key_type: Type, value_type: Type) -> Type {
Type::WithTypeParameter(Box::new(TypeWithTypeParameter {
r#type: Type::ScopeInGlobal(identifer::from_string("Map")),
type_parameter_list: vec![key_type, value_type],
}))
}
pub fn readonly_map_type(key_type: Type, value_type: Type) -> Type {
Type::WithTypeParameter(Box::new(TypeWithTypeParameter {
r#type: Type::ScopeInGlobal(identifer::from_string("ReadonlyMap")),
type_parameter_list: vec![key_type, value_type],
}))
}
pub fn set_type(element_type: Type) -> Type {
Type::WithTypeParameter(Box::new(TypeWithTypeParameter {
r#type: Type::ScopeInGlobal(identifer::from_string("Set")),
type_parameter_list: vec![element_type],
}))
}
pub fn readonly_set_type(element_type: Type) -> Type {
Type::WithTypeParameter(Box::new(TypeWithTypeParameter {
r#type: Type::ScopeInGlobal(identifer::from_string("ReadonlySet")),
type_parameter_list: vec![element_type],
}))
}