From 0acb2b384cc45763ce90f16bc0cf32d9bf142402 Mon Sep 17 00:00:00 2001 From: Dean Srebnik <49134864+load1n9@users.noreply.github.com> Date: Wed, 31 Jul 2024 23:35:30 -0400 Subject: [PATCH 1/7] feat: WIP enums implementation --- .../scope_analysis.rs | 47 ++++++++++++------- nova_vm/src/engine/bytecode/executable.rs | 10 ++-- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/nova_vm/src/ecmascript/syntax_directed_operations/scope_analysis.rs b/nova_vm/src/ecmascript/syntax_directed_operations/scope_analysis.rs index 285a949d8..0c18cbf21 100644 --- a/nova_vm/src/ecmascript/syntax_directed_operations/scope_analysis.rs +++ b/nova_vm/src/ecmascript/syntax_directed_operations/scope_analysis.rs @@ -711,6 +711,9 @@ impl<'a> VarDeclaredNames<'a> for Statement<'a> { } // 4. Return the list-concatenation of names1, names2, and names3. }, + #[cfg(feature = "typescript")] + Statement::TSEnumDeclaration(_) => {}, + Statement::WhileStatement(st) => { // WhileStatement : while ( Expression ) Statement // 1. Return the VarDeclaredNames of Statement. @@ -737,7 +740,8 @@ impl<'a> VarDeclaredNames<'a> for Statement<'a> { } } }, - Statement::TSEnumDeclaration(_) | + #[cfg(not(feature = "typescript"))] + Statement::TSEnumDeclaration(_) => unreachable!(), Statement::TSExportAssignment(_) | Statement::TSImportEqualsDeclaration(_) | Statement::TSInterfaceDeclaration(_) | @@ -1080,15 +1084,18 @@ impl<'a> TopLevelLexicallyDeclaredNames<'a> for Statement<'a> { Statement::ImportDeclaration(decl) => decl.bound_names(f), Statement::ExportNamedDeclaration(decl) => decl.bound_names(f), #[cfg(feature = "typescript")] - Statement::TSTypeAliasDeclaration(_) | Statement::TSInterfaceDeclaration(_) => {} + Statement::TSTypeAliasDeclaration(_) + | Statement::TSInterfaceDeclaration(_) + | Statement::TSEnumDeclaration(_) => {} #[cfg(not(feature = "typescript"))] - Statement::TSTypeAliasDeclaration(_) | Statement::TSInterfaceDeclaration(_) => { + Statement::TSTypeAliasDeclaration(_) + | Statement::TSInterfaceDeclaration(_) + | Statement::TSEnumDeclaration(_) => { unreachable!() } // Note: No bounds names for export all and export default declarations. Statement::ExportAllDeclaration(_) | Statement::ExportDefaultDeclaration(_) => {} - Statement::TSEnumDeclaration(_) - | Statement::TSModuleDeclaration(_) + Statement::TSModuleDeclaration(_) | Statement::TSImportEqualsDeclaration(_) | Statement::TSExportAssignment(_) | Statement::TSNamespaceExportDeclaration(_) => unreachable!(), @@ -1163,13 +1170,16 @@ impl<'a> TopLevelLexicallyScopedDeclarations<'a> for Statement<'a> { Statement::ClassDeclaration(decl) => f(LexicallyScopedDeclaration::Class(decl)), Statement::UsingDeclaration(_) => todo!(), #[cfg(feature = "typescript")] - Statement::TSTypeAliasDeclaration(_) | Statement::TSInterfaceDeclaration(_) => {} + Statement::TSTypeAliasDeclaration(_) + | Statement::TSInterfaceDeclaration(_) + | Statement::TSEnumDeclaration(_) => {} #[cfg(not(feature = "typescript"))] - Statement::TSTypeAliasDeclaration(_) | Statement::TSInterfaceDeclaration(_) => { + Statement::TSTypeAliasDeclaration(_) + | Statement::TSInterfaceDeclaration(_) + | Statement::TSEnumDeclaration(_) => { unreachable!() } - Statement::TSEnumDeclaration(_) - | Statement::TSExportAssignment(_) + Statement::TSExportAssignment(_) | Statement::TSImportEqualsDeclaration(_) | Statement::TSModuleDeclaration(_) | Statement::TSNamespaceExportDeclaration(_) => unreachable!(), @@ -1250,13 +1260,16 @@ impl<'a> TopLevelVarDeclaredNames<'a> for Statement<'a> { | Statement::WhileStatement(_) | Statement::WithStatement(_) => self.var_declared_names(f), #[cfg(feature = "typescript")] + Statement::TSEnumDeclaration(_) => self.var_declared_names(f), + #[cfg(feature = "typescript")] Statement::TSTypeAliasDeclaration(_) | Statement::TSInterfaceDeclaration(_) => {} #[cfg(not(feature = "typescript"))] - Statement::TSTypeAliasDeclaration(_) | Statement::TSInterfaceDeclaration(_) => { + Statement::TSTypeAliasDeclaration(_) + | Statement::TSInterfaceDeclaration(_) + | Statement::TSEnumDeclaration(_) => { unreachable!() } - Statement::TSEnumDeclaration(_) - | Statement::TSExportAssignment(_) + Statement::TSExportAssignment(_) | Statement::TSImportEqualsDeclaration(_) | Statement::TSModuleDeclaration(_) | Statement::TSNamespaceExportDeclaration(_) => unreachable!(), @@ -1379,14 +1392,16 @@ impl<'a> TopLevelVarScopedDeclarations<'a> for Statement<'a> { // 2. Return a new empty List. } #[cfg(feature = "typescript")] - Statement::TSTypeAliasDeclaration(_) | Statement::TSInterfaceDeclaration(_) => {} + Statement::TSTypeAliasDeclaration(_) | Statement::TSInterfaceDeclaration(_) | Statement::TSEnumDeclaration(_) => {} #[cfg(not(feature = "typescript"))] - Statement::TSTypeAliasDeclaration(_) | Statement::TSInterfaceDeclaration(_) => { + Statement::TSTypeAliasDeclaration(_) + | Statement::TSInterfaceDeclaration(_) + | Statement::TSEnumDeclaration(_) => { unreachable!() } Statement::UsingDeclaration(_) => todo!(), - Statement::TSEnumDeclaration(_) - | Statement::TSExportAssignment(_) + + Statement::TSExportAssignment(_) | Statement::TSImportEqualsDeclaration(_) | Statement::TSModuleDeclaration(_) | Statement::TSNamespaceExportDeclaration(_) => unreachable!(), diff --git a/nova_vm/src/engine/bytecode/executable.rs b/nova_vm/src/engine/bytecode/executable.rs index 09939c5ba..c19282a1a 100644 --- a/nova_vm/src/engine/bytecode/executable.rs +++ b/nova_vm/src/engine/bytecode/executable.rs @@ -2752,6 +2752,8 @@ impl CompileEvaluation for ast::Statement<'_> { ast::Statement::ForStatement(x) => x.compile(ctx), ast::Statement::ThrowStatement(x) => x.compile(ctx), ast::Statement::TryStatement(x) => x.compile(ctx), + #[cfg(feature = "typescript")] + Statement::TSEnumDeclaration(_statement) => todo!(), Statement::BreakStatement(statement) => statement.compile(ctx), Statement::ContinueStatement(statement) => statement.compile(ctx), Statement::DebuggerStatement(_) => todo!(), @@ -2769,13 +2771,13 @@ impl CompileEvaluation for ast::Statement<'_> { Statement::ExportDefaultDeclaration(_) => todo!(), Statement::ExportNamedDeclaration(_) => todo!(), #[cfg(feature = "typescript")] - Statement::TSTypeAliasDeclaration(_) | Statement::TSInterfaceDeclaration(_) => {} + Statement::TSTypeAliasDeclaration(_) + | Statement::TSInterfaceDeclaration(_) => {} #[cfg(not(feature = "typescript"))] - Statement::TSTypeAliasDeclaration(_) | Statement::TSInterfaceDeclaration(_) => { + Statement::TSTypeAliasDeclaration(_) | Statement::TSInterfaceDeclaration(_) | Statement::TSEnumDeclaration(_) => { unreachable!() } - Statement::TSEnumDeclaration(_) - | Statement::TSExportAssignment(_) + Statement::TSExportAssignment(_) | Statement::TSImportEqualsDeclaration(_) | Statement::TSModuleDeclaration(_) | Statement::TSNamespaceExportDeclaration(_) => unreachable!(), From d1a21e9c22e00dfd5de41f5b52ade200f13e90ed Mon Sep 17 00:00:00 2001 From: Dean Srebnik <49134864+load1n9@users.noreply.github.com> Date: Wed, 31 Jul 2024 23:39:21 -0400 Subject: [PATCH 2/7] chore: fmt --- .../syntax_directed_operations/scope_analysis.rs | 4 +++- nova_vm/src/engine/bytecode/executable.rs | 7 ++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/nova_vm/src/ecmascript/syntax_directed_operations/scope_analysis.rs b/nova_vm/src/ecmascript/syntax_directed_operations/scope_analysis.rs index 0c18cbf21..2d57dd77d 100644 --- a/nova_vm/src/ecmascript/syntax_directed_operations/scope_analysis.rs +++ b/nova_vm/src/ecmascript/syntax_directed_operations/scope_analysis.rs @@ -1392,7 +1392,9 @@ impl<'a> TopLevelVarScopedDeclarations<'a> for Statement<'a> { // 2. Return a new empty List. } #[cfg(feature = "typescript")] - Statement::TSTypeAliasDeclaration(_) | Statement::TSInterfaceDeclaration(_) | Statement::TSEnumDeclaration(_) => {} + Statement::TSTypeAliasDeclaration(_) + | Statement::TSInterfaceDeclaration(_) + | Statement::TSEnumDeclaration(_) => {} #[cfg(not(feature = "typescript"))] Statement::TSTypeAliasDeclaration(_) | Statement::TSInterfaceDeclaration(_) diff --git a/nova_vm/src/engine/bytecode/executable.rs b/nova_vm/src/engine/bytecode/executable.rs index c19282a1a..3c26e0461 100644 --- a/nova_vm/src/engine/bytecode/executable.rs +++ b/nova_vm/src/engine/bytecode/executable.rs @@ -2771,10 +2771,11 @@ impl CompileEvaluation for ast::Statement<'_> { Statement::ExportDefaultDeclaration(_) => todo!(), Statement::ExportNamedDeclaration(_) => todo!(), #[cfg(feature = "typescript")] - Statement::TSTypeAliasDeclaration(_) - | Statement::TSInterfaceDeclaration(_) => {} + Statement::TSTypeAliasDeclaration(_) | Statement::TSInterfaceDeclaration(_) => {} #[cfg(not(feature = "typescript"))] - Statement::TSTypeAliasDeclaration(_) | Statement::TSInterfaceDeclaration(_) | Statement::TSEnumDeclaration(_) => { + Statement::TSTypeAliasDeclaration(_) + | Statement::TSInterfaceDeclaration(_) + | Statement::TSEnumDeclaration(_) => { unreachable!() } Statement::TSExportAssignment(_) From 85009f6692182e16aaa01056c22cc59ab4294f60 Mon Sep 17 00:00:00 2001 From: Dean Srebnik <49134864+load1n9@users.noreply.github.com> Date: Thu, 1 Aug 2024 00:08:18 -0400 Subject: [PATCH 3/7] feat: progress made with enums --- nova_vm/src/engine/bytecode/executable.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/nova_vm/src/engine/bytecode/executable.rs b/nova_vm/src/engine/bytecode/executable.rs index 3c26e0461..8135ab0db 100644 --- a/nova_vm/src/engine/bytecode/executable.rs +++ b/nova_vm/src/engine/bytecode/executable.rs @@ -2639,6 +2639,12 @@ impl CompileEvaluation for ast::TryStatement<'_> { } } +impl CompileEvaluation for ast::TSEnumDeclaration<'_> { + fn compile(&self, _ctx: &mut CompileContext) { + todo!(); + } +} + impl CompileEvaluation for ast::WhileStatement<'_> { fn compile(&self, ctx: &mut CompileContext) { let previous_continue = ctx.current_continue.replace(vec![]); @@ -2753,7 +2759,7 @@ impl CompileEvaluation for ast::Statement<'_> { ast::Statement::ThrowStatement(x) => x.compile(ctx), ast::Statement::TryStatement(x) => x.compile(ctx), #[cfg(feature = "typescript")] - Statement::TSEnumDeclaration(_statement) => todo!(), + Statement::TSEnumDeclaration(statement) => statement.compile(ctx), Statement::BreakStatement(statement) => statement.compile(ctx), Statement::ContinueStatement(statement) => statement.compile(ctx), Statement::DebuggerStatement(_) => todo!(), From dc848fbc34210aa82400b097bab95ea1f64de6a4 Mon Sep 17 00:00:00 2001 From: Dean Srebnik <49134864+load1n9@users.noreply.github.com> Date: Sat, 3 Aug 2024 22:04:51 -0400 Subject: [PATCH 4/7] Update nova_vm/src/engine/bytecode/executable.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Elias Sjögreen --- nova_vm/src/engine/bytecode/executable.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova_vm/src/engine/bytecode/executable.rs b/nova_vm/src/engine/bytecode/executable.rs index 8135ab0db..f01bb1f4a 100644 --- a/nova_vm/src/engine/bytecode/executable.rs +++ b/nova_vm/src/engine/bytecode/executable.rs @@ -2638,7 +2638,7 @@ impl CompileEvaluation for ast::TryStatement<'_> { ctx.exe.set_jump_target_here(jump_to_end); } } - +#[cfg(feature = "typescript")] impl CompileEvaluation for ast::TSEnumDeclaration<'_> { fn compile(&self, _ctx: &mut CompileContext) { todo!(); From ee64e7baa5548e030853ecadf7fac92b3dd1ed40 Mon Sep 17 00:00:00 2001 From: Dean Srebnik <49134864+load1n9@users.noreply.github.com> Date: Sat, 3 Aug 2024 22:55:31 -0400 Subject: [PATCH 5/7] feat: implement more of enum and add notes --- nova_cli/Cargo.toml | 2 +- nova_vm/src/engine/bytecode/executable.rs | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/nova_cli/Cargo.toml b/nova_cli/Cargo.toml index be0258da3..6ea21d2c0 100644 --- a/nova_cli/Cargo.toml +++ b/nova_cli/Cargo.toml @@ -10,7 +10,7 @@ cliclack = { workspace = true } ctrlc = { workspace = true } console = { workspace = true } miette = { workspace = true } -nova_vm = { path = "../nova_vm" } +nova_vm = { path = "../nova_vm", features = ["typescript"] } oxc_ast = { workspace = true } oxc_parser = { workspace = true } oxc_semantic = { workspace = true } diff --git a/nova_vm/src/engine/bytecode/executable.rs b/nova_vm/src/engine/bytecode/executable.rs index f01bb1f4a..a5e580790 100644 --- a/nova_vm/src/engine/bytecode/executable.rs +++ b/nova_vm/src/engine/bytecode/executable.rs @@ -2640,8 +2640,17 @@ impl CompileEvaluation for ast::TryStatement<'_> { } #[cfg(feature = "typescript")] impl CompileEvaluation for ast::TSEnumDeclaration<'_> { - fn compile(&self, _ctx: &mut CompileContext) { - todo!(); + fn compile(&self, ctx: &mut CompileContext) { + // TODO: implement enum declaration + // NOTE: remember not to forget to treat const enums differently + + // content of the enum is not evaluated, it is just a declaration + ctx.exe.add_instruction(Instruction::ObjectCreate); + for _prop in self.members.iter() { + todo!("enum member"); + } + // 3. Return enumObj. + ctx.exe.add_instruction(Instruction::Store); } } From ab2c2ba6caa9012a6e1037cef2330159c92a75ad Mon Sep 17 00:00:00 2001 From: Dean Srebnik <49134864+load1n9@users.noreply.github.com> Date: Sat, 3 Aug 2024 22:56:30 -0400 Subject: [PATCH 6/7] fix: turn off typescript by default --- nova_cli/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova_cli/Cargo.toml b/nova_cli/Cargo.toml index 6ea21d2c0..be0258da3 100644 --- a/nova_cli/Cargo.toml +++ b/nova_cli/Cargo.toml @@ -10,7 +10,7 @@ cliclack = { workspace = true } ctrlc = { workspace = true } console = { workspace = true } miette = { workspace = true } -nova_vm = { path = "../nova_vm", features = ["typescript"] } +nova_vm = { path = "../nova_vm" } oxc_ast = { workspace = true } oxc_parser = { workspace = true } oxc_semantic = { workspace = true } From 350840569e70b3216e7514e043be459035ac0ea2 Mon Sep 17 00:00:00 2001 From: Dean Srebnik <49134864+load1n9@users.noreply.github.com> Date: Sun, 4 Aug 2024 17:46:57 -0400 Subject: [PATCH 7/7] feat(cli): add typescript and pretty print option to the parse command --- nova_cli/src/main.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/nova_cli/src/main.rs b/nova_cli/src/main.rs index 9d75ef8f6..98bd43e34 100644 --- a/nova_cli/src/main.rs +++ b/nova_cli/src/main.rs @@ -37,6 +37,14 @@ enum Command { Parse { /// The path of the file to parse path: String, + + /// Whether to parse as TypeScript + #[arg(long)] + typescript: bool, + + /// Whether to pretty print the AST + #[arg(long)] + pretty: bool, }, /// Evaluates a file @@ -88,11 +96,15 @@ fn main() -> Result<(), Box> { let args = Cli::parse(); match args.command { - Command::Parse { path } => { + Command::Parse { + path, + typescript, + pretty, + } => { let file = std::fs::read_to_string(&path)?; let allocator = Default::default(); let source_type: SourceType = Default::default(); - let parser = Parser::new(&allocator, &file, source_type.with_typescript(false)); + let parser = Parser::new(&allocator, &file, source_type.with_typescript(typescript)); let result = parser.parse(); if !result.errors.is_empty() { @@ -107,7 +119,11 @@ fn main() -> Result<(), Box> { exit_with_parse_errors(result.errors, &path, &file); } - println!("{:?}", result.program); + if pretty { + println!("{:#?}", result.program); + } else { + println!("{:?}", result.program); + } } Command::Eval { verbose,