diff --git a/compiler/forget/crates/forget_build_hir/src/build.rs b/compiler/forget/crates/forget_build_hir/src/build.rs index a7913e6495..eea36ffd00 100644 --- a/compiler/forget/crates/forget_build_hir/src/build.rs +++ b/compiler/forget/crates/forget_build_hir/src/build.rs @@ -2,9 +2,8 @@ use std::collections::HashSet; use forget_diagnostics::Diagnostic; use forget_estree::{ - AssignmentTarget, BinaryExpression, BlockStatement, Expression, ExpressionOrSpread, - ExpressionOrSuper, ForInit, ForStatement, Function, FunctionExpression, IfStatement, - IntoFunction, JsValue, Literal, Number, Pattern, Statement, VariableDeclaration, + AssignmentTarget, BlockStatement, Expression, ExpressionOrSpread, ExpressionOrSuper, ForInit, + Function, IntoFunction, JsValue, Pattern, Statement, VariableDeclaration, VariableDeclarationKind, }; use forget_hir::{ @@ -13,7 +12,7 @@ use forget_hir::{ Operand, PlaceOrSpread, TerminalValue, }; -use crate::builder::{Binding, Builder, LoopScope}; +use crate::builder::{Builder, LoopScope}; use crate::context::get_context_identifiers; use crate::error::BuildHIRError; @@ -490,9 +489,7 @@ fn lower_function( _builder: &mut Builder, function: &T, ) -> Result { - println!("get_context_identifiers() ..."); let context_identifiers = get_context_identifiers(env, function); - println!("ok"); let mut context = Vec::new(); let mut seen = HashSet::new(); for declaration_id in context_identifiers { @@ -526,7 +523,7 @@ fn lower_jsx_element( .iter() .map(|attr| lower_jsx_attribute(env, builder, attr)) .collect(); - let props = props?; + let _props = props?; let children: Result, Diagnostic> = expr .children .iter() @@ -535,7 +532,7 @@ fn lower_jsx_element( Ok(Operand { effect: None, ix }) }) .collect(); - let children = children?; + let _children = children?; todo!("lower jsx element"); // Ok(JSXElement { // tag: todo!(), @@ -549,17 +546,17 @@ fn lower_jsx_element( } fn lower_jsx_attribute( - env: &Environment, - builder: &mut Builder, - attr: &forget_estree::JSXAttributeOrSpread, + _env: &Environment, + _builder: &mut Builder, + _attr: &forget_estree::JSXAttributeOrSpread, ) -> Result { todo!("lower jsx attribute") } fn lower_jsx_child( - env: &Environment, - builder: &mut Builder, - child: &forget_estree::JSXChildItem, + _env: &Environment, + _builder: &mut Builder, + _child: &forget_estree::JSXChildItem, ) -> Result { todo!("lower jsx child") } @@ -603,7 +600,7 @@ fn lower_assignment_pattern( fn lower_identifier_for_assignment( env: &Environment, - builder: &mut Builder, + _builder: &mut Builder, kind: InstructionKind, node: &forget_estree::Identifier, ) -> Result { diff --git a/compiler/forget/crates/forget_build_hir/src/builder.rs b/compiler/forget/crates/forget_build_hir/src/builder.rs index cf035976bf..f74ae80b75 100644 --- a/compiler/forget/crates/forget_build_hir/src/builder.rs +++ b/compiler/forget/crates/forget_build_hir/src/builder.rs @@ -3,9 +3,9 @@ use std::rc::Rc; use forget_diagnostics::Diagnostic; use forget_hir::{ - initialize_hir, BasicBlock, BlockId, BlockKind, Blocks, Environment, GotoKind, Identifier, - IdentifierData, InstrIx, Instruction, InstructionIdGenerator, InstructionValue, Terminal, - TerminalValue, Type, HIR, + initialize_hir, BasicBlock, BlockId, BlockKind, Blocks, Environment, GotoKind, IdentifierData, + InstrIx, Instruction, InstructionIdGenerator, InstructionValue, Terminal, TerminalValue, Type, + HIR, }; use crate::BuildHIRError; @@ -42,12 +42,6 @@ pub(crate) struct WipBlock { pub instructions: Vec, } -pub(crate) enum Binding { - Local(Identifier), - Module(Identifier), - Global, -} - #[derive(Clone, PartialEq, Eq, Debug)] enum ControlFlowScope { Loop(LoopScope), diff --git a/compiler/forget/crates/forget_build_hir/src/context.rs b/compiler/forget/crates/forget_build_hir/src/context.rs index f23ed9b76c..6bfaa65ddd 100644 --- a/compiler/forget/crates/forget_build_hir/src/context.rs +++ b/compiler/forget/crates/forget_build_hir/src/context.rs @@ -9,10 +9,6 @@ pub(crate) fn get_context_identifiers( node: &T, ) -> Vec { let function_scope = env.scope(node.function()).unwrap(); - println!( - "get_context_identifiers for function scope {:?}", - function_scope.id() - ); let mut free = FreeVariables::default(); let mut seen = HashSet::new(); populate_free_variable_references(&mut free, &mut seen, function_scope); @@ -28,28 +24,11 @@ fn populate_free_variable_references( ) { for reference in scope.references() { if !seen.insert(reference.declaration().id()) { - println!( - "skip {}${:?}", - reference.declaration().name(), - reference.declaration().id() - ); continue; } let declaration_scope = reference.declaration().scope(); if !declaration_scope.is_descendant_of(scope) { - println!( - "free variable: not descendant {}${:?}", - reference.declaration().name(), - reference.declaration().id() - ); free.push(reference.declaration().id()) - } else { - println!( - "local variable: descendant {}${:?} scope={:?}", - reference.declaration().name(), - reference.declaration().id(), - reference.declaration().scope().id() - ); } } for child in scope.children() { diff --git a/compiler/forget/crates/forget_estree/src/generated.rs b/compiler/forget/crates/forget_estree/src/generated.rs index d135569dba..1caca408d2 100644 --- a/compiler/forget/crates/forget_estree/src/generated.rs +++ b/compiler/forget/crates/forget_estree/src/generated.rs @@ -1,5 +1,8 @@ // @generated #![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(dead_code)] +#![allow(unused_variables)] +#![allow(non_snake_case)] use std::num::NonZeroU32; use serde::ser::{Serializer, SerializeMap}; use serde::{Serialize, Deserialize}; diff --git a/compiler/forget/crates/forget_estree/src/old.lib.rs b/compiler/forget/crates/forget_estree/src/old.lib.rs deleted file mode 100644 index b7c9188767..0000000000 --- a/compiler/forget/crates/forget_estree/src/old.lib.rs +++ /dev/null @@ -1,1358 +0,0 @@ -use serde::{Deserialize, Serialize}; -use static_assertions::assert_eq_size; -use std::{fmt::Display, num::NonZeroU32}; - -#[derive(Serialize, Deserialize, Debug)] -pub struct SourceLocation { - pub source: Option, - - pub start: Position, - - pub end: Position, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct Position { - /// >= 1 - pub line: NonZeroU32, - /// >= 0 - pub column: u32, -} -assert_eq_size!(Option, u64); - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct SourceRange { - pub start: u32, - // end is exclusive so it can always be non-zero. This allows - // Option to not take any additional bytes. - pub end: NonZeroU32, -} -assert_eq_size!(Option, u64); - -#[derive(Serialize, Deserialize, Debug)] -pub struct Program { - /// sourceType - #[serde(rename = "sourceType")] - #[serde(default)] - pub source_type: SourceType, - - pub body: Vec, - - #[serde(default)] - pub comments: Option>, - - pub loc: Option, - - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub enum SourceType { - /// "module" - #[serde(rename = "module")] - Module, - /// "script" - #[serde(rename = "script")] - Script, -} - -impl Default for SourceType { - fn default() -> Self { - Self::Module - } -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct Comment { - pub type_: CommentType, - pub value: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub enum CommentType { - /// "Line" - Line, - /// "Block" - Block, -} - -#[derive(Serialize, Deserialize, Debug)] -#[serde(untagged)] -pub enum ModuleItem { - Statement(Box), - ImportDeclaration(Box), -} - -#[derive(Serialize, Deserialize, Debug)] -#[serde(tag = "type")] -pub enum ImportExportDeclaration { - ImportDeclaration(Box), - // TODO: - // ExportNamedDeclaration(Box), - // ExportDefaultDeclaration(Box), - // ExportAllDeclaration(Box), -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ImportDeclaration { - pub specifiers: Vec, - pub source: Literal, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -#[serde(tag = "type")] -pub enum ImportSpecifiers { - ImportSpecifier(Box), - ImportDefaultSpecifier(Box), -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ImportSpecifier { - pub imported: Identifier, - - pub local: Identifier, - - pub loc: Option, - - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ImportDefaultSpecifier { - pub local: Identifier, - - pub loc: Option, - - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -#[serde(tag = "type")] -pub enum Statement { - BlockStatement(Box), - BreakStatement(Box), - ClassDeclaration(Box), - ContinueStatement(Box), - DebuggerStatement(Box), - DoWhileStatement(Box), - EmptyStatement(Box), - ExpressionStatement(Box), - ForInStatement(Box), - ForOfStatement(Box), - ForStatement(Box), - FunctionDeclaration(Box), - IfStatement(Box), - LabeledStatement(Box), - ReturnStatement(Box), - StaticBlock(Box), - SwitchStatement(Box), - ThrowStatement(Box), - TryStatement(Box), - VariableDeclaration(Box), - WhileStatement(Box), - WithStatement(Box), -} -// Prevent unboxed variants from increasing the size -assert_eq_size!(Statement, u128); - -#[derive(Serialize, Deserialize, Debug)] -pub struct BlockStatement { - pub body: Vec, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct BreakStatement { - #[serde(default)] - pub label: Option, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ClassDeclaration { - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ContinueStatement { - #[serde(default)] - pub label: Option, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct DebuggerStatement { - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct DoWhileStatement { - pub body: Statement, - pub test: ExpressionLike, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct EmptyStatement { - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct FunctionDeclaration { - pub id: Option, - - pub params: Vec, - - #[serde(rename = "generator")] - #[serde(default)] - pub is_generator: bool, - - #[serde(rename = "async")] - #[serde(default)] - pub is_async: bool, - - // TODO: BlockStatement - pub body: Option, - - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct StaticBlock { - pub body: Vec, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ExpressionStatement { - pub expression: ExpressionLike, - - #[serde(default)] - pub directive: Option, - - pub loc: Option, - - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ForInStatement { - pub left: ForPattern, - pub right: ExpressionLike, - pub body: Statement, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ForOfStatement { - #[serde(rename = "await")] - #[serde(default)] - pub is_await: bool, - - pub left: ForPattern, - - pub right: ExpressionLike, - - pub body: Statement, - - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -#[serde(tag = "type")] -pub enum ForPattern { - VariableDeclaration(Box), - Expression(Box), -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ForStatement { - pub init: Option, - pub test: Option, - pub update: Option, - pub body: Statement, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -#[serde(tag = "type")] -pub enum ForInit { - VariableDeclaration(Box), - Expression(Box), -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct IfStatement { - pub test: ExpressionLike, - pub consequent: Statement, - pub alternate: Option, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct LabeledStatement { - pub label: Identifier, - pub body: Statement, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ReturnStatement { - pub argument: Option, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct SwitchStatement { - pub discriminant: ExpressionLike, - pub cases: Vec, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct SwitchCase { - pub test: Option, - pub consequent: Vec, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ThrowStatement { - pub argument: ExpressionLike, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct TryStatement { - // TODO: block: BlockStatement - pub block: Statement, - pub handler: Option, - // TODO: finalizer: BlockStatement - pub finalizer: Option, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct CatchClause { - pub param: Option, - pub body: Statement, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct VariableDeclaration { - pub declarations: Vec, - pub kind: VariableDeclarationKind, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct VariableDeclarator { - pub id: Pattern, - pub init: Option, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub enum VariableDeclarationKind { - #[serde(rename = "const")] - Const, - #[serde(rename = "let")] - Let, - #[serde(rename = "var")] - Var, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct WhileStatement { - pub test: ExpressionLike, - pub body: Statement, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct WithStatement { - pub object: ExpressionLike, - pub body: Statement, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -/// Expressions and expression-like nodes -/// we flatten these into a single enum to work around limits -/// of serde enum format with handling arbitrary unions -#[derive(Serialize, Deserialize, Debug)] -#[serde(tag = "type")] -pub enum ExpressionLike { - ArrayExpression(Box), - ArrowFunctionExpression(Box), - AssignmentExpression(Box), - AwaitExpression(Box), - BinaryExpression(Box), - CallExpression(Box), - ChainExpression(Box), - ClassExpression(Box), - ConditionalExpression(Box), - FunctionExpression(Box), - Identifier(Box), - ImportExpression(Box), - Literal(Box), - LogicalExpression(Box), - MemberExpression(Box), - MetaProperty(Box), - NewExpression(Box), - ObjectExpression(Box), - SequenceExpression(Box), - TaggedTemplateExpression(Box), - TemplateLiteral(Box), - ThisExpression(Box), - UnaryExpression(Box), - UpdateExpression(Box), - YieldExpression(Box), - - // pseudo-expressions to work with serde - Super(Box), - - // patterns to work with serde - ArrayPattern(Box), - AssignmentPattern(Box), - ObjectPattern(Box), - Property(Box), - RestElement(Box), - SpreadElement(Box), - - // jsx expression-ish types to work with serde - JSXClosingElement(Box), - JSXElement(Box), - JSXExpressionContainer(Box), - JSXIdentifier(Box), - JSXMemberExpression(Box), - JSXNamedspacedName(Box), - JSXOpeningElement(Box), - JSXText(Box), -} -// Prevent unboxed variants from increasing the size -assert_eq_size!(ExpressionLike, u128); - -#[derive(Serialize, Deserialize, Debug)] -pub struct ArrayExpression { - pub elements: Vec, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ArrowFunctionExpression { - pub params: Vec, - - #[serde(rename = "generator")] - #[serde(default)] - pub is_generator: bool, - - #[serde(rename = "async")] - #[serde(default)] - pub is_async: bool, - - #[serde(rename = "expression")] - #[serde(default)] - pub is_expression: bool, - - pub body: BlockOrExpression, - - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -#[serde(tag = "type")] -pub enum BlockOrExpression { - BlockStatement(Box), - Expression(Box), -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct AssignmentExpression { - pub operator: AssignmentOperator, - pub left: AssignmentTarget, - pub right: ExpressionLike, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub enum AssignmentOperator { - /// = - #[serde(rename = "=")] - Equals, - /// += - #[serde(rename = "+=")] - PlusEquals, - // -= - #[serde(rename = "-=")] - MinusEquals, - /// *= - #[serde(rename = "*=")] - AsteriskEquals, - /// /= - #[serde(rename = "/=")] - SlashEquals, - /// %= - #[serde(rename = "%=")] - PercentEquals, - /// **= - #[serde(rename = "**=")] - AsteriskAsteriskEquals, - /// <<= - #[serde(rename = "<<=")] - LtLtEquals, - /// >>= - #[serde(rename = ">>=")] - GtGtEquals, - /// >>>= - #[serde(rename = ">>>=")] - GtGtGtEquals, - /// |= - #[serde(rename = "|=")] - PipeEquals, - /// ^= - #[serde(rename = "^=")] - CaretEquals, - /// &= - #[serde(rename = "&&=")] - AmpersandEquals, - /// ||= - #[serde(rename = "||=")] - PipePipeEquals, - /// &&= - #[serde(rename = "&&=")] - AmpersandAmpersandEquals, - // ??= - #[serde(rename = "??=")] - QuestionQuestionEquals, -} - -impl AssignmentOperator { - pub fn is_simple_equals(&self) -> bool { - matches!(self, AssignmentOperator::Equals) - } -} - -#[derive(Serialize, Deserialize, Debug)] -#[serde(untagged)] -pub enum AssignmentTarget { - Pattern(Box), - MemberExpression(Box), -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct AwaitExpression { - pub argument: ExpressionLike, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct BinaryExpression { - pub operator: BinaryOperator, - pub left: ExpressionLike, - pub right: ExpressionLike, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub enum BinaryOperator { - /// == - #[serde(rename = "==")] - EqualsEquals, - /// != - #[serde(rename = "!=")] - NotEquals, - /// === - #[serde(rename = "===")] - TripleEquals, - /// !== - #[serde(rename = "!==")] - NotTripleEquals, - /// < - #[serde(rename = "<")] - LessThan, - /// <= - #[serde(rename = "<=")] - LessThanEquals, - /// > - #[serde(rename = ">")] - GreaterThan, - /// >= - #[serde(rename = ">=")] - GreaterThanEquals, - /// << - #[serde(rename = "<<")] - LtLt, - /// >> - #[serde(rename = ">>")] - GtGt, - /// >>> - #[serde(rename = ">>>")] - GtGtGt, - /// + - #[serde(rename = "+")] - Plus, - /// - - #[serde(rename = "-")] - Minus, - /// * - #[serde(rename = "*")] - Asterisk, - /// / - #[serde(rename = "/")] - Slash, - /// % - #[serde(rename = "%")] - Percent, - /// ** - #[serde(rename = "**")] - AsteriskAsterisk, - /// | - #[serde(rename = "|")] - Pipe, - /// ^ - #[serde(rename = "^")] - Caret, - /// & - #[serde(rename = "&")] - Ampersand, - /// in - #[serde(rename = "in")] - In, - /// instanceof - #[serde(rename = "instanceof")] - Instanceof, -} - -impl Display for BinaryOperator { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let name = match self { - Self::Plus => "+", - Self::LessThan => "<", - _ => todo!("display for operator: {:#?}", self), - }; - f.write_str(name) - } -} - -impl std::str::FromStr for BinaryOperator { - type Err = String; - - fn from_str(s: &str) -> Result { - match s { - "+" => Ok(Self::Plus), - "-" => Ok(Self::Minus), - _ => Err(format!("Invalid BinaryOperator: `{}`", s)), - } - } -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct CallExpression { - #[serde(rename = "optional")] - #[serde(default)] - pub is_optional: bool, - - pub callee: ExpressionLike, - - pub arguments: Vec, - - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ChainExpression { - pub expression: ChainElement, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -#[serde(tag = "type")] -pub enum ChainElement { - CallExpression(Box), - MemberExpression(Box), -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ClassExpression { - // TODO - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ConditionalExpression { - pub test: ExpressionLike, - pub alternate: ExpressionLike, - pub consequent: ExpressionLike, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct FunctionExpression { - // TODO - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ImportExpression { - // TODO - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct Literal { - pub value: LiteralValue, - - #[serde(default)] - pub raw: Option, - - pub loc: Option, - - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -#[serde(untagged)] -pub enum LiteralValue { - BigInt(String), - Boolean(bool), - Null, - Number(Number), - RegExp(RegExp), - String(String), -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct Number(u64); - -impl From for Number { - fn from(value: u64) -> Self { - Number(value) - } -} - -impl From for Number { - fn from(value: f64) -> Self { - Number(value.to_bits()) - } -} - -impl From for f64 { - fn from(value: Number) -> Self { - f64::from_bits(value.0) - } -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct RegExp { - pub raw: Option, - pub regex: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct RegExpValue { - pub pattern: String, - pub flags: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct LogicalExpression { - pub operator: LogicalOperator, - pub left: ExpressionLike, - pub right: ExpressionLike, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub enum LogicalOperator { - /// || - #[serde(rename = "||")] - PipePipe, - /// && - #[serde(rename = "&&")] - AmpersandAmpersand, - /// ?? - #[serde(rename = "??")] - QuestionQuestion, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct MetaProperty { - // TODO - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct NewExpression { - pub callee: ExpressionLike, - pub arguments: Vec, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ObjectExpression { - pub properties: Vec, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct SequenceExpression { - pub expressions: Vec, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct TaggedTemplateExpression { - pub tag: ExpressionLike, - pub quasi: TemplateLiteral, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct TemplateLiteral { - pub quasis: Vec, - pub expressions: Vec, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct TemplateElement { - #[serde(rename = "tail")] - #[serde(default)] - pub is_tail: bool, - - // TODO: add value: {cooked, raw} wrapper object - pub cooked: Option, - - pub raw: String, - - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ThisExpression { - // TODO - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct UnaryExpression { - pub operator: UnaryOperator, - - #[serde(rename = "prefix")] - #[serde(default)] - pub is_prefix: bool, - - pub argument: ExpressionLike, - - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub enum UnaryOperator { - /// - - #[serde(rename = "+")] - Minus, - /// + - #[serde(rename = "+")] - Plus, - /// ! - #[serde(rename = "!")] - Exclamation, - /// ~ - #[serde(rename = "~")] - Tilde, - /// typeof - #[serde(rename = "typeof")] - Typeof, - /// void - #[serde(rename = "void")] - Void, - /// delete - #[serde(rename = "delete")] - Delete, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct UpdateExpression { - pub operator: UpdateOperator, - - pub argument: ExpressionLike, - - #[serde(rename = "prefix")] - #[serde(default)] - pub is_prefix: bool, - - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub enum UpdateOperator { - /// ++ - #[serde(rename = "++")] - PlusPlus, - /// -- - #[serde(rename = "--")] - MinusMinus, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct YieldExpression { - pub argument: Option, - - #[serde(rename = "delegate")] - #[serde(default)] - pub is_delegate: bool, - - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -// Pattern etc - -#[derive(Serialize, Deserialize, Debug)] -#[serde(tag = "type")] -pub enum Pattern { - ArrayPattern(Box), - AssignmentPattern(Box), - Identifier(Box), - MemberExpression(Box), - ObjectPattern(Box), - RestElement(Box), -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ArrayPattern { - pub elements: Vec, - - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct AssignmentPattern { - pub left: Pattern, - pub right: ExpressionLike, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct Identifier { - pub name: String, - - #[serde(default)] - pub binding: Option, - - pub loc: Option, - - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -#[serde(tag = "type")] -pub enum Binding { - Local(BindingId), - Module(BindingId), - Global, -} - -#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash, Clone, Copy)] -#[serde(transparent)] -pub struct BindingId(NonZeroU32); - -impl BindingId { - pub fn new(id: NonZeroU32) -> Self { - Self(id) - } -} - -impl From for u32 { - fn from(value: BindingId) -> Self { - value.0.into() - } -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct MemberExpression { - pub object: ExpressionLike, - - pub property: ExpressionLike, // ExpressionOrPrivateIdentifier - - #[serde(rename = "computed")] - #[serde(default)] - pub is_computed: bool, - - #[serde(rename = "optional")] - #[serde(default)] - pub is_optional: bool, - - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct Super { - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct PrivateIdentifier { - pub name: String, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ObjectPattern { - pub properties: Vec, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct Property { - pub key: ExpressionLike, - - pub value: PropertyValue, - - pub kind: PropertyKind, - - #[serde(rename = "method")] - #[serde(default)] - pub is_method: bool, - - #[serde(rename = "shorthand")] - #[serde(default)] - pub is_shorthand: bool, - - #[serde(rename = "computed")] - #[serde(default)] - pub is_computed: bool, - - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -#[serde(untagged)] -pub enum PropertyValue { - Expression(Box), - Pattern(Box), -} - -#[derive(Serialize, Deserialize, Debug)] -pub enum PropertyKind { - #[serde(rename = "init")] - Init, - #[serde(rename = "get")] - Get, - #[serde(rename = "set")] - Set, -} - -impl Default for PropertyKind { - fn default() -> Self { - Self::Init - } -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct RestElement { - pub argument: Pattern, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct SpreadElement { - pub argument: ExpressionLike, - pub loc: Option, - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct JSXElement { - #[serde(rename = "openingElement")] - pub opening_element: ExpressionLike, - - pub children: Vec, - - #[serde(rename = "closingElement")] - pub closing_element: Option, - - pub loc: Option, - - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct JSXOpeningElement { - pub name: ExpressionLike, - - pub attributes: Vec, - - #[serde(rename = "selfClosing")] - #[serde(default)] - pub self_closing: bool, - - pub loc: Option, - - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct JSXClosingElement { - pub name: ExpressionLike, - - pub loc: Option, - - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct JSXMemberExpression { - pub object: ExpressionLike, - - pub property: ExpressionLike, - - pub loc: Option, - - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct JSXIdentifier { - pub name: String, - - pub loc: Option, - - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct JSXNamedspacedName { - pub namespace: ExpressionLike, - - pub name: ExpressionLike, - - pub loc: Option, - - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct JSXAttribute { - pub name: ExpressionLike, - - pub value: ExpressionLike, - - pub loc: Option, - - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct JSXExpressionContainer { - expression: ExpressionLike, - - pub loc: Option, - - #[serde(default)] - pub range: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct JSXText { - value: String, - raw: String, - - pub loc: Option, - - #[serde(default)] - pub range: Option, -} - -#[cfg(test)] -mod tests { - use super::*; - use insta::{assert_snapshot, glob}; - use serde_json; - - #[test] - fn for_statement() { - let source = include_str!("for-statement.json"); - let ast: Program = serde_json::from_str(&source).unwrap(); - println!("deserialized:\n{:#?}", ast); - let serialized = serde_json::to_string_pretty(&ast).unwrap(); - println!("serialized:\n{}", serialized); - } - - #[test] - fn simple() { - let source = include_str!("simple.json"); - let ast: Program = serde_json::from_str(&source).unwrap(); - println!("deserialized:\n{:#?}", ast); - let serialized = serde_json::to_string_pretty(&ast).unwrap(); - println!("serialized:\n{}", serialized); - } - - /// TODO: enable once this deserializes - fn _kitchen_sink() { - let source = include_str!("kitchen-sink.json"); - let ast: Program = match serde_json::from_str(&source) { - Ok(ast) => ast, - Err(err) => panic!("{:#?}", err), - }; - println!("deserialized:\n{:#?}", ast); - let serialized = serde_json::to_string_pretty(&ast).unwrap(); - println!("serialized:\n{}", serialized); - } - - #[test] - fn import() { - let source = include_str!("import.json"); - let ast: Program = serde_json::from_str(&source).unwrap(); - println!("deserialized:\n{:#?}", ast); - let serialized = serde_json::to_string_pretty(&ast).unwrap(); - println!("serialized:\n{}", serialized); - } - - #[test] - fn test() { - let source = include_str!("test.json"); - let ast: Program = serde_json::from_str(&source).unwrap(); - println!("deserialized:\n{:#?}", ast); - let serialized = serde_json::to_string_pretty(&ast).unwrap(); - println!("serialized:\n{}", serialized); - } - - #[test] - fn fixtures() { - glob!("fixtures/**.json", |path| { - let input = std::fs::read_to_string(path).unwrap(); - let ast: Program = serde_json::from_str(&input).unwrap(); - let serialized = serde_json::to_string_pretty(&ast).unwrap(); - assert_snapshot!(format!("Input:\n{input}\n\nOutput:\n{serialized}")); - }); - } -} diff --git a/compiler/forget/crates/forget_estree/src/visit.rs b/compiler/forget/crates/forget_estree/src/visit.rs index 4196cb7f66..ebe2ac2da5 100644 --- a/compiler/forget/crates/forget_estree/src/visit.rs +++ b/compiler/forget/crates/forget_estree/src/visit.rs @@ -145,7 +145,7 @@ pub trait Visitor_DEPRECATED<'ast> { self.visit_variable_declarator(declarator) } } - Declaration::TSTypeAliasDeclaration(declaration) => { + Declaration::TSTypeAliasDeclaration(_declaration) => { todo!("visit TSTypeAliasDeclaration") } } @@ -268,7 +268,7 @@ pub trait Visitor_DEPRECATED<'ast> { self.visit_expression(&stmt.object); self.visit_statement(&stmt.body); } - Statement::TSTypeAliasDeclaration(stmt) => { + Statement::TSTypeAliasDeclaration(_stmt) => { todo!("visit TSTypeAliasDeclaration") } } diff --git a/compiler/forget/crates/forget_estree_codegen/src/codegen.rs b/compiler/forget/crates/forget_estree_codegen/src/codegen.rs index 7b28b2bb3b..2fafd91b8f 100644 --- a/compiler/forget/crates/forget_estree_codegen/src/codegen.rs +++ b/compiler/forget/crates/forget_estree_codegen/src/codegen.rs @@ -88,10 +88,11 @@ impl Grammar { .map(|(name, operator)| operator.codegen(name)) .collect(); - // println!("{}", quote! {#(#node_visitors)*}); - // println!("{}", quote! {#(#enum_visitors)*}); - quote! { + #![allow(dead_code)] + #![allow(unused_variables)] + #![allow(non_snake_case)] + use std::num::NonZeroU32; use serde::ser::{Serializer, SerializeMap}; use serde::{Serialize,Deserialize}; @@ -406,9 +407,7 @@ impl Node { if let Some(convert_with) = &field.hermes_convert_with { let convert_with = format_ident!("{}", convert_with); return quote! { - // println!("read {}.{}", #name_str, #name); let #field_name = #convert_with(cx, unsafe { hermes::parser::#helper(node) } ); - // println!("ok"); } } match type_kind { @@ -416,30 +415,22 @@ impl Node { match type_name_str.as_ref() { "bool" => { quote! { - // println!("read {}.{}", #name_str, #name); let #field_name = unsafe { hermes::parser::#helper(node) }; - // println!("ok"); } } "Number" => { quote! { - // println!("read {}.{}", #name_str, #name); let #field_name = convert_number(unsafe { hermes::parser::#helper(node) }); - // println!("ok"); } } "String" => { quote! { - // println!("read {}.{}", #name_str, #name); let #field_name = convert_string(cx, unsafe { hermes::parser::#helper(node) }); - // println!("ok"); } } _ => { quote! { - // println!("read {}.{}", #name_str, #name); let #field_name = #type_name::convert(cx, unsafe { hermes::parser::#helper(node) }); - // println!("ok"); } } } @@ -448,32 +439,24 @@ impl Node { match type_name_str.as_ref() { "String" => { quote! { - // println!("read {}.{}", #name_str, #name); let #field_name = convert_option_string(cx, unsafe { hermes::parser::#helper(node) }); - // println!("ok"); } } _ => { quote! { - // println!("read {}.{}", #name_str, #name); let #field_name = convert_option(unsafe { hermes::parser::#helper(node) }, |node| #type_name::convert(cx, node)); - // println!("ok"); } } } } TypeKind::Vec => { quote! { - // println!("read {}.{}", #name_str, #name); let #field_name = convert_vec(unsafe { hermes::parser::#helper(node) }, |node| #type_name::convert(cx, node)); - // println!("ok"); } } TypeKind::VecOfOption => { quote! { - // println!("read {}.{}", #name_str, #name); let #field_name = convert_vec_of_option(unsafe { hermes::parser::#helper(node) }, |node| #type_name::convert(cx, node)); - // println!("ok"); } } } diff --git a/compiler/forget/crates/forget_fixtures/tests/fixtures_test.rs b/compiler/forget/crates/forget_fixtures/tests/fixtures_test.rs index b951333055..964fccda30 100644 --- a/compiler/forget/crates/forget_fixtures/tests/fixtures_test.rs +++ b/compiler/forget/crates/forget_fixtures/tests/fixtures_test.rs @@ -22,7 +22,6 @@ fn fixtures() { let mut output = String::new(); let analysis = analyze(&ast); - // println!("{:#?}", analysis.debug()); let environment = Environment::new( Features { validate_frozen_lambdas: true, @@ -38,10 +37,6 @@ fn fixtures() { } match build(&environment, &fun.function) { Ok(mut fun) => { - // println!("{fun:#?}"); - // let mut out = String::new(); - // fun.print(&fun.body, &mut out).unwrap(); - // println!("{out}"); println!("ok build"); enter_ssa(&environment, &mut fun).unwrap(); println!("ok enter_ssa"); diff --git a/compiler/forget/crates/forget_hir/src/environment.rs b/compiler/forget/crates/forget_hir/src/environment.rs index 4ecc4939d9..bcc7218dca 100644 --- a/compiler/forget/crates/forget_hir/src/environment.rs +++ b/compiler/forget/crates/forget_hir/src/environment.rs @@ -1,10 +1,9 @@ use std::cell::{Cell, RefCell}; use std::collections::HashMap; -use std::process::id; use std::rc::Rc; -use forget_estree::{BindingId, ESTreeNode}; -use forget_semantic_analysis::{AstNode, DeclarationId, ScopeManager, ScopeView}; +use forget_estree::ESTreeNode; +use forget_semantic_analysis::{DeclarationId, ScopeManager, ScopeView}; use crate::{ BlockId, Features, Identifier, IdentifierData, IdentifierId, Registry, Type, TypeVarId, diff --git a/compiler/forget/crates/forget_semantic_analysis/src/analyzer.rs b/compiler/forget/crates/forget_semantic_analysis/src/analyzer.rs index 6a06909637..872cd72a3c 100644 --- a/compiler/forget/crates/forget_semantic_analysis/src/analyzer.rs +++ b/compiler/forget/crates/forget_semantic_analysis/src/analyzer.rs @@ -1,9 +1,8 @@ use forget_diagnostics::Diagnostic; use forget_estree::{ AssignmentOperator, AssignmentPropertyOrRestElement, AssignmentTarget, Expression, - ExpressionOrSuper, ForInInit, ForInit, Function, FunctionBody, Identifier, IntoFunction, - JSXElementName, Pattern, Program, SourceRange, SourceType, Statement, VariableDeclarationKind, - Visitor, + ExpressionOrSuper, ForInInit, ForInit, FunctionBody, Identifier, IntoFunction, JSXElementName, + Pattern, Program, SourceRange, SourceType, Statement, VariableDeclarationKind, Visitor, }; use crate::{AstNode, DeclarationKind, LabelKind, ReferenceKind, ScopeId, ScopeKind, ScopeManager}; diff --git a/compiler/forget/crates/forget_semantic_analysis/src/scope_manager.rs b/compiler/forget/crates/forget_semantic_analysis/src/scope_manager.rs index 7711dfa363..412e973d93 100644 --- a/compiler/forget/crates/forget_semantic_analysis/src/scope_manager.rs +++ b/compiler/forget/crates/forget_semantic_analysis/src/scope_manager.rs @@ -180,11 +180,11 @@ impl ScopeManager { } } - pub fn lookup_break(&self, scope: ScopeId) -> Option<&Label> { + pub fn lookup_break(&self, _scope: ScopeId) -> Option<&Label> { todo!() } - pub fn lookup_continue(&self, scope: ScopeId) -> Option<&Label> { + pub fn lookup_continue(&self, _scope: ScopeId) -> Option<&Label> { todo!() } diff --git a/compiler/forget/crates/forget_semantic_analysis/src/scope_view.rs b/compiler/forget/crates/forget_semantic_analysis/src/scope_view.rs index 7a137218e9..de4270b1da 100644 --- a/compiler/forget/crates/forget_semantic_analysis/src/scope_view.rs +++ b/compiler/forget/crates/forget_semantic_analysis/src/scope_view.rs @@ -142,6 +142,7 @@ impl<'m> std::fmt::Debug for ScopeView<'m> { #[derive(Clone, Copy)] pub struct LabelView<'m> { + #[allow(dead_code)] pub(crate) manager: &'m ScopeManager, pub(crate) label: &'m Label, } diff --git a/compiler/forget/crates/forget_ssa/src/enter.rs b/compiler/forget/crates/forget_ssa/src/enter.rs index df8c8cf167..eeae6d6278 100644 --- a/compiler/forget/crates/forget_ssa/src/enter.rs +++ b/compiler/forget/crates/forget_ssa/src/enter.rs @@ -184,9 +184,9 @@ impl<'e, 'f> Builder<'e, 'f> { // Else we have to look at predecessor blocks: bail if no predecessors let block = self.blocks.block(block_id); if block.predecessors.is_empty() { - println!("Unable to find previous id for {old_identifier:?}"); - self.unknown.insert(old_identifier.id); - return old_identifier.clone(); + panic!("Unable to find previous id for {old_identifier:?}"); + // self.unknown.insert(old_identifier.id); + // return old_identifier.clone(); } // If we haven't visited all predecessors, synthesize a new identifier // and save it as an incomplete phi