Just pass ModuleCompiler to compile_enum_value, static_eval_expr

This commit is contained in:
Andrew Morris
2023-07-24 11:24:07 +10:00
parent 1a12e57435
commit 267a86fb2c
4 changed files with 29 additions and 54 deletions

View File

@@ -1,20 +1,13 @@
use std::collections::HashMap;
use swc_common::Spanned;
use crate::{
asm::{Number, Object, Pointer, Value},
scope_analysis::ScopeAnalysis,
asm::{Number, Object, Value},
module_compiler::ModuleCompiler,
static_eval_expr::static_eval_expr,
Diagnostic, DiagnosticLevel,
};
pub fn compile_enum_value(
sa: &ScopeAnalysis,
cm: &HashMap<Pointer, Value>,
ts_enum: &swc_ecma_ast::TsEnumDecl,
diagnostics: &mut Vec<Diagnostic>,
) -> Value {
pub fn compile_enum_value(mc: &mut ModuleCompiler, ts_enum: &swc_ecma_ast::TsEnumDecl) -> Value {
let mut properties = Vec::<(Value, Value)>::new();
let mut next_default_id: Option<f64> = Some(0.0);
@@ -25,7 +18,7 @@ pub fn compile_enum_value(
};
let init_value = match &member.init {
Some(init) => match static_eval_expr(sa, cm, init) {
Some(init) => match static_eval_expr(mc, init) {
Some(init_value) => match init_value {
Value::Number(Number(n)) => {
next_default_id = Some(n + 1.0);
@@ -35,7 +28,7 @@ pub fn compile_enum_value(
_ => None,
},
None => {
diagnostics.push(Diagnostic {
mc.diagnostics.push(Diagnostic {
level: DiagnosticLevel::InternalError,
message: "TODO: Static eval failed".to_string(),
span: init.span(),
@@ -53,7 +46,7 @@ pub fn compile_enum_value(
let id = match next_default_id {
Some(id) => id,
None => {
diagnostics.push(Diagnostic {
mc.diagnostics.push(Diagnostic {
level: DiagnosticLevel::Error,
message: "Missing required initializer".to_string(),
span: member.span,

View File

@@ -1240,12 +1240,7 @@ impl<'a> FunctionCompiler<'a> {
}
};
let enum_value = compile_enum_value(
&self.mc.scope_analysis,
&self.mc.constants_map,
ts_enum,
&mut self.diagnostics,
);
let enum_value = compile_enum_value(self.mc, ts_enum);
self.definitions.push(Definition {
pointer,

View File

@@ -200,7 +200,7 @@ impl ModuleCompiler {
.scope_analysis
.lookup(&Ident::from_swc_ident(ident))
.map(|name| name.value.clone()),
expr => static_eval_expr(&self.scope_analysis, &self.constants_map, expr),
expr => static_eval_expr(self, expr),
};
match value {
@@ -303,7 +303,7 @@ impl ModuleCompiler {
};
if let (Some(ident), Some(init)) = (ident, init) {
let value_opt = static_eval_expr(&self.scope_analysis, &self.constants_map, init);
let value_opt = static_eval_expr(self, init);
let value = match value_opt {
Some(value) => value,
@@ -417,12 +417,7 @@ impl ModuleCompiler {
));
}
let enum_value = compile_enum_value(
&self.scope_analysis,
&self.constants_map,
ts_enum,
&mut self.diagnostics,
);
let enum_value = compile_enum_value(self, ts_enum);
self.module.definitions.push(Definition {
pointer,
@@ -921,8 +916,7 @@ impl ModuleCompiler {
let name = match &method.key {
swc_ecma_ast::PropName::Ident(ident) => Value::String(ident.sym.to_string()),
swc_ecma_ast::PropName::Computed(computed) => {
let value_opt =
static_eval_expr(&self.scope_analysis, &self.constants_map, &computed.expr);
let value_opt = static_eval_expr(self, &computed.expr);
match value_opt {
None => {

View File

@@ -1,19 +1,13 @@
use std::collections::HashMap;
use valuescript_vm::operations::to_i32;
use crate::{
asm::{Array, Builtin, Number, Object, Pointer, Value},
asm::{Array, Builtin, Number, Object, Value},
expression_compiler::value_from_literal,
ident::Ident,
scope_analysis::ScopeAnalysis,
module_compiler::ModuleCompiler,
};
pub fn static_eval_expr(
sa: &ScopeAnalysis,
cm: &HashMap<Pointer, Value>,
expr: &swc_ecma_ast::Expr,
) -> Option<Value> {
pub fn static_eval_expr(mc: &ModuleCompiler, expr: &swc_ecma_ast::Expr) -> Option<Value> {
let symbol_iterator_opt = as_symbol_iterator(expr);
if symbol_iterator_opt.is_some() {
@@ -35,7 +29,7 @@ pub fn static_eval_expr(
return None;
}
static_eval_expr(sa, cm, &item.expr)?
static_eval_expr(mc, &item.expr)?
}
None => Value::Void,
});
@@ -56,13 +50,11 @@ pub fn static_eval_expr(
swc_ecma_ast::PropName::Ident(ident) => Value::String(ident.sym.to_string()),
swc_ecma_ast::PropName::Str(str) => Value::String(str.value.to_string()),
swc_ecma_ast::PropName::Num(num) => Value::Number(Number(num.value)),
swc_ecma_ast::PropName::Computed(computed) => {
static_eval_expr(sa, cm, &computed.expr)?
}
swc_ecma_ast::PropName::Computed(computed) => static_eval_expr(mc, &computed.expr)?,
swc_ecma_ast::PropName::BigInt(bi) => Value::BigInt(bi.value.clone()),
};
let value = static_eval_expr(sa, cm, &kv.value)?;
let value = static_eval_expr(mc, &kv.value)?;
(key, value)
}
@@ -85,11 +77,12 @@ pub fn static_eval_expr(
swc_ecma_ast::Expr::SuperProp(_) => None,
swc_ecma_ast::Expr::Call(_) => None,
swc_ecma_ast::Expr::New(_) => None,
swc_ecma_ast::Expr::Ident(ident) => match sa
swc_ecma_ast::Expr::Ident(ident) => match mc
.scope_analysis
.lookup(&Ident::from_swc_ident(ident))
.map(|name| name.value.clone())
{
Some(Value::Pointer(p)) => cm.get(&p).cloned(),
Some(Value::Pointer(p)) => mc.constants_map.get(&p).cloned(),
Some(value) => Some(value),
None => None,
},
@@ -111,18 +104,18 @@ pub fn static_eval_expr(
swc_ecma_ast::Expr::Member(_) => None,
swc_ecma_ast::Expr::Cond(_) => None,
swc_ecma_ast::Expr::Unary(unary) => match unary.op {
swc_ecma_ast::UnaryOp::Minus => match static_eval_expr(sa, cm, &unary.arg)? {
swc_ecma_ast::UnaryOp::Minus => match static_eval_expr(mc, &unary.arg)? {
Value::Number(Number(x)) => Some(Value::Number(Number(-x))),
Value::BigInt(bi) => Some(Value::BigInt(-bi)),
_ => None,
},
swc_ecma_ast::UnaryOp::Plus => match static_eval_expr(sa, cm, &unary.arg)? {
swc_ecma_ast::UnaryOp::Plus => match static_eval_expr(mc, &unary.arg)? {
Value::Number(Number(x)) => Some(Value::Number(Number(x))),
Value::BigInt(bi) => Some(Value::BigInt(bi)),
_ => None,
},
swc_ecma_ast::UnaryOp::Bang => None,
swc_ecma_ast::UnaryOp::Tilde => match static_eval_expr(sa, cm, &unary.arg)? {
swc_ecma_ast::UnaryOp::Tilde => match static_eval_expr(mc, &unary.arg)? {
Value::Number(Number(x)) => Some(Value::Number(Number(!to_i32(x) as f64))),
Value::BigInt(bi) => Some(Value::BigInt(!bi)),
_ => None,
@@ -136,7 +129,7 @@ pub fn static_eval_expr(
let mut last = Value::Void;
for expr in &seq.exprs {
last = static_eval_expr(sa, cm, expr)?;
last = static_eval_expr(mc, expr)?;
}
Some(last)
@@ -151,11 +144,11 @@ pub fn static_eval_expr(
None // TODO
}
swc_ecma_ast::Expr::Paren(paren) => static_eval_expr(sa, cm, &paren.expr),
swc_ecma_ast::Expr::TsTypeAssertion(tta) => static_eval_expr(sa, cm, &tta.expr),
swc_ecma_ast::Expr::TsConstAssertion(tca) => static_eval_expr(sa, cm, &tca.expr),
swc_ecma_ast::Expr::TsNonNull(tnn) => static_eval_expr(sa, cm, &tnn.expr),
swc_ecma_ast::Expr::TsAs(ta) => static_eval_expr(sa, cm, &ta.expr),
swc_ecma_ast::Expr::Paren(paren) => static_eval_expr(mc, &paren.expr),
swc_ecma_ast::Expr::TsTypeAssertion(tta) => static_eval_expr(mc, &tta.expr),
swc_ecma_ast::Expr::TsConstAssertion(tca) => static_eval_expr(mc, &tca.expr),
swc_ecma_ast::Expr::TsNonNull(tnn) => static_eval_expr(mc, &tnn.expr),
swc_ecma_ast::Expr::TsAs(ta) => static_eval_expr(mc, &ta.expr),
}
}