Resolve Module chain
This commit is contained in:
nganhkhoa 2021-05-19 16:45:42 +07:00
parent 2ca5c18d66
commit af4750c57b
3 changed files with 109 additions and 28 deletions

View File

@ -5,13 +5,6 @@ use full_moon::ast::*;
use crate::visitor::Visitor; use crate::visitor::Visitor;
fn is_standard_library(name: String) -> bool {
let standard_library_names: Vec<&str> = vec![
"math", "string", "table", "bit"
];
return false
}
pub struct ConstantFolder { pub struct ConstantFolder {
locals: Box<Vec<expression_reducer::Variable>> locals: Box<Vec<expression_reducer::Variable>>
} }

View File

@ -151,6 +151,7 @@ pub enum ExpressionType {
String(String), String(String),
Var(String), // name Var(String), // name
FunctionCall(String, Vec<Expression>), // name, param FunctionCall(String, Vec<Expression>), // name, param
Module(String, Vec<String>), // name, suffixes
BinOp(Box<Expression>, Box<Expression>, BinOp), // lhs, rhs, binop BinOp(Box<Expression>, Box<Expression>, BinOp), // lhs, rhs, binop
UnOp(Box<Expression>, UnOp), UnOp(Box<Expression>, UnOp),
Index(Box<Expression>, Vec<Expression>), // name, index expr Index(Box<Expression>, Vec<Expression>), // name, index expr
@ -231,13 +232,13 @@ fn reduce_expression(expr: ast::Expression, locals: &Vec<Variable>) -> Expressio
fn reduce_expression_value(value: ast::Value, locals: &Vec<Variable>) -> Expression { fn reduce_expression_value(value: ast::Value, locals: &Vec<Variable>) -> Expression {
match value { match value {
ast::Value::Function(function) => { ast::Value::Function(_function) => {
panic!("reduce Expression::Function is unsupported"); panic!("reduce Expression::Function is unsupported");
} }
ast::Value::FunctionCall(function_call) => { ast::Value::FunctionCall(function_call) => {
reduce_expression_value_function_call(function_call, locals) reduce_expression_value_function_call(function_call, locals)
} }
ast::Value::TableConstructor(table_constructor) => { ast::Value::TableConstructor(_table_constructor) => {
panic!("reduce Expression::TableConstructor is unsupported"); panic!("reduce Expression::TableConstructor is unsupported");
} }
ast::Value::Number(number) => { ast::Value::Number(number) => {
@ -278,13 +279,67 @@ fn reduce_expression_value(value: ast::Value, locals: &Vec<Variable>) -> Express
fn reduce_expression_value_var(var: ast::Var, locals: &Vec<Variable>) -> Expression { fn reduce_expression_value_var(var: ast::Var, locals: &Vec<Variable>) -> Expression {
match var { match var {
ast::Var::Expression(expr) => { ast::Var::Expression(expr) => {
panic!("reduce Var::Expression is unsupported") let prefix = {
match expr.prefix() {
ast::Prefix::Name(name) => name.token().to_string(),
_ => panic!("Var::Expression prefix expression is unsupported")
} }
ast::Var::Name(name) => { };
let local_var = locals.iter().find(|it| it.name() == name.token().to_string()); let suffixes = expr.suffixes().map(|suf| {
match suf {
ast::Suffix::Index(ast::Index::Dot{
name,
..
}) => {
name.token().to_string()
}
_ => panic!("Var::Expression Call not supported")
}
}).collect::<Vec<String>>();
let local_var = locals.iter().find(|it| it.name() == prefix);
match local_var { match local_var {
None => { None => {
panic!("local variable not found") Expression {
t: ExpressionType::Module(prefix, suffixes)
}
}
Some(var) => {
match var.v.clone().t {
ExpressionType::Module(n, _) => {
Expression {
t: ExpressionType::Module(n, suffixes)
}
}
_ => {
Expression {
t: ExpressionType::Module(var.name(), suffixes)
}
}
}
}
}
}
ast::Var::Name(name) => {
fn is_standard_library(name: &String) -> bool {
let standard_library_names: Vec<&str> = vec![
"math", "string", "table", "bit"
];
standard_library_names.contains(&name.as_str())
}
let name_str = name.token().to_string();
if is_standard_library(&name_str) {
return Expression {
t: ExpressionType::Module(name_str, vec![])
};
}
let local_var = locals.iter().find(|it| it.name() == name_str);
match local_var {
None => {
Expression {
t: ExpressionType::Var(name_str)
}
} }
Some(v) => { Some(v) => {
v.v.clone() v.v.clone()

View File

@ -1,15 +1,48 @@
local L1_1, L1_2, L41_1, L42_1, L43_1, L44_1, L45_1, L46_1, L47_1, L37_1 -- local L1_1, L1_2, L41_1, L42_1, L43_1, L44_1, L45_1, L46_1, L47_1, L37_1
L1_2 = 2 -- L1_2 = 2
L1_1 = 1 + L1_2 -- L1_1 = 1 + L1_2
L1_1 = true -- L1_1 = true
L1_2 = false -- L1_2 = false
L1_1 = L1_1 and L1_2 -- L1_1 = L1_1 and L1_2
L41_1 = L37_1 -- L41_1 = L37_1
L42_1 = 20 -- L42_1 = 20
L43_1 = 76 -- L43_1 = 76
L44_1 = 201 -- L44_1 = 201
L45_1 = 132 -- L45_1 = 132
L46_1 = 98 -- L46_1 = 98
L47_1 = 93 -- L47_1 = 93
L41_1 = (L41_1(L42_1, L43_1, L44_1, L45_1, L46_1, L47_1)) -- L41_1 = (L41_1(L42_1, L43_1, L44_1, L45_1, L46_1, L47_1))
-- L1_1 = math
-- L1_1 = math.min
local L0_1, L1_1, L2_1, L3_1, L4_1, L5_1, L6_1, L7_1, L8_1, L9_1, L10_1, L11_1, L12_1, L13_1, L14_1, L15_1, L16_1, L17_1, L18_1, L19_1, L20_1, L21_1, L22_1, L23_1, L24_1, L25_1, L26_1, L27_1, L28_1, L29_1, L30_1, L31_1, L32_1, L33_1, L34_1, L35_1, L36_1, L37_1, L38_1, L39_1, L40_1, L41_1, L42_1, L43_1, L44_1, L45_1, L46_1, L47_1, L48_1, L49_1, L50_1, L51_1, L52_1, L53_1, L54_1, L55_1, L56_1, L57_1, L58_1, L59_1, L60_1, L61_1, L62_1, L63_1, L64_1, L65_1, L66_1, L67_1, L68_1, L69_1, L70_1, L71_1, L72_1, L73_1, L74_1, L75_1, L76_1, L77_1, L78_1, L79_1, L80_1, L81_1, L82_1, L83_1, L84_1, L85_1, L86_1, L87_1, L88_1, L89_1, L90_1, L91_1, L92_1, L93_1, L94_1, L95_1, L96_1, L97_1, L98_1, L99_1, L100_1, L101_1, L102_1, L103_1, L104_1, L105_1, L106_1, L107_1, L108_1, L109_1, L110_1, L111_1, L112_1, L113_1, L114_1, L115_1, L116_1, L117_1, L118_1, L119_1, L120_1, L121_1, L122_1, L123_1, L124_1, L125_1, L126_1, L127_1, L128_1, L129_1, L130_1, L131_1, L132_1, L133_1, L134_1, L135_1, L136_1, L137_1, L138_1, L139_1, L140_1, L141_1, L142_1, L143_1, L144_1, L145_1, L146_1, L147_1, L148_1, L149_1, L150_1, L151_1, L152_1, L153_1, L154_1, L155_1, L156_1, L157_1, L158_1, L159_1, L160_1, L161_1, L162_1, L163_1, L164_1, L165_1, L166_1, L167_1, L168_1, L169_1, L170_1, L171_1, L172_1, L173_1, L174_1, L175_1, L176_1, L177_1, L178_1, L179_1, L180_1, L181_1, L182_1, L183_1, L184_1, L185_1, L186_1, L187_1, L188_1, L189_1, L190_1, L191_1, L192_1, L193_1, L194_1, L195_1, L196_1, L197_1, L198_1, L199_1, L200_1, L201_1, L202_1, L203_1, L204_1, L205_1, L206_1, L207_1, L208_1, L209_1, L210_1, L211_1, L212_1, L213_1, L214_1, L215_1, L216_1, L217_1, L218_1, L219_1, L220_1, L221_1, L222_1, L223_1, L224_1, L225_1, L226_1, L227_1, L228_1, L229_1, L230_1
L1_1 = math
L1_1 = L1_1.fmod
L2_1 = math
L2_1 = L2_1.max
L3_1 = math
L3_1 = L3_1.min
L4_1 = math
L4_1 = L4_1.floor
L5_1 = math
L5_1 = L5_1.ceil
L6_1 = math
L6_1 = L6_1.random
L7_1 = string
L7_1 = L7_1.len
L8_1 = string
L8_1 = L8_1.char
L9_1 = string
L9_1 = L9_1.byte
L10_1 = string
L10_1 = L10_1.sub
L11_1 = string
L11_1 = L11_1.upper
L12_1 = string
L12_1 = L12_1.format
L13_1 = string
L13_1 = L13_1.len
L14_1 = string
L14_1 = L14_1.gsub