1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
mod binary; mod field; mod function; mod index; mod number; mod prefix; mod string; mod table; mod unary; pub use binary::*; pub use field::*; pub use function::*; pub use index::*; pub use number::*; pub use prefix::*; pub use string::*; pub use table::*; pub use unary::*; use crate::lua_generator::{LuaGenerator, ToLua}; use crate::nodes::FunctionCall; #[derive(Clone, Debug, PartialEq, Eq)] pub enum Expression { Binary(Box<BinaryExpression>), Call(Box<FunctionCall>), False, Field(Box<FieldExpression>), Function(FunctionExpression), Identifier(String), Index(Box<IndexExpression>), Nil, Number(NumberExpression), Parenthese(Box<Expression>), String(StringExpression), Table(TableExpression), True, Unary(Box<UnaryExpression>), VariableArguments, } impl From<BinaryExpression> for Expression { fn from(binary: BinaryExpression) -> Expression { Expression::Binary(Box::new(binary)) } } impl From<FunctionCall> for Expression { fn from(call: FunctionCall) -> Expression { Expression::Call(Box::new(call)) } } impl From<FieldExpression> for Expression { fn from(field: FieldExpression) -> Expression { Expression::Field(Box::new(field)) } } impl From<FunctionExpression> for Expression { fn from(function: FunctionExpression) -> Self { Expression::Function(function) } } impl From<IndexExpression> for Expression { fn from(index: IndexExpression) -> Self { Self::Index(Box::new(index)) } } impl From<NumberExpression> for Expression { fn from(number: NumberExpression) -> Self { Self::Number(number) } } impl From<Prefix> for Expression { fn from(prefix: Prefix) -> Self { match prefix { Prefix::Call(call) => Self::Call(Box::new(call)), Prefix::Field(field) => Self::Field(field), Prefix::Identifier(name) => Self::Identifier(name), Prefix::Index(index) => Self::Index(index), Prefix::Parenthese(expression) => Self::Parenthese(Box::new(expression)), } } } impl From<StringExpression> for Expression { fn from(string: StringExpression) -> Self { Self::String(string) } } impl From<TableExpression> for Expression { fn from(table: TableExpression) -> Self { Self::Table(table) } } impl From<UnaryExpression> for Expression { fn from(unary: UnaryExpression) -> Self { Self::Unary(Box::new(unary)) } } impl ToLua for Expression { fn to_lua(&self, generator: &mut LuaGenerator) { match self { Self::Binary(binary_expression) => binary_expression.to_lua(generator), Self::Call(call) => call.to_lua(generator), Self::False => generator.push_str("false"), Self::Field(field) => field.to_lua(generator), Self::Function(function) => function.to_lua(generator), Self::Identifier(identifier) => generator.push_str(identifier), Self::Index(index) => index.to_lua(generator), Self::Nil => generator.push_str("nil"), Self::Number(number) => number.to_lua(generator), Self::Parenthese(expression) => { generator.push_char('('); expression.to_lua(generator); generator.push_char(')'); } Self::String(string) => string.to_lua(generator), Self::Table(table) => table.to_lua(generator), Self::True => generator.push_str("true"), Self::Unary(unary_expression) => unary_expression.to_lua(generator), Self::VariableArguments => generator.push_str("..."), } } } #[cfg(test)] mod test { use super::*; mod to_lua { use super::*; #[test] fn generate_false_expression() { let output = Expression::False.to_lua_string(); assert_eq!(output, "false"); } #[test] fn generate_nil_expression() { let output = Expression::Nil.to_lua_string(); assert_eq!(output, "nil"); } #[test] fn generate_parenthese_expression() { let inner_expression = Box::new(Expression::True); let output = Expression::Parenthese(inner_expression).to_lua_string(); assert_eq!(output, "(true)"); } #[test] fn generate_true_expression() { let output = Expression::True.to_lua_string(); assert_eq!(output, "true"); } #[test] fn generate_variable_arguments_expression() { let output = Expression::VariableArguments.to_lua_string(); assert_eq!(output, "..."); } } }