From 8b5428748c6db34670261bcf69471a402840ec6a Mon Sep 17 00:00:00 2001 From: Sepehr Laal <5657848+3p3r@users.noreply.github.com> Date: Wed, 2 Nov 2022 08:15:24 -0700 Subject: [PATCH] Revert "feat(compiler): bring local wing files (#252)" This reverts commit 05fb820b3e5fb25b8906527c1501bf12beda0c34. --- apps/wing-language-server/src/prep.rs | 4 +- examples/invalid/circular-bring.w | 1 - examples/invalid/circular/mod.w | 1 - examples/simple/bring.w | 2 - examples/simple/modules/mod1.w | 2 - examples/simple/modules/mod2.w | 4 -- examples/simple/modules/sub/mod.w | 1 - .../test/corpus/statements.txt | 15 ----- libs/wingc/.vscode/launch.json | 19 ------ libs/wingc/Cargo.toml | 3 - libs/wingc/src/ast.rs | 5 -- libs/wingc/src/capture.rs | 6 -- libs/wingc/src/jsify.rs | 9 --- libs/wingc/src/lib.rs | 39 ++++++++++-- libs/wingc/src/parser.rs | 46 +++----------- libs/wingc/src/parser/bring.rs | 61 ------------------- libs/wingc/src/type_check.rs | 6 -- 17 files changed, 43 insertions(+), 181 deletions(-) delete mode 100644 examples/invalid/circular-bring.w delete mode 100644 examples/invalid/circular/mod.w delete mode 100644 examples/simple/bring.w delete mode 100644 examples/simple/modules/mod1.w delete mode 100644 examples/simple/modules/mod2.w delete mode 100644 examples/simple/modules/sub/mod.w delete mode 100644 libs/wingc/.vscode/launch.json delete mode 100644 libs/wingc/src/parser/bring.rs diff --git a/apps/wing-language-server/src/prep.rs b/apps/wing-language-server/src/prep.rs index d8ae42351c5..d0a915152ab 100644 --- a/apps/wing-language-server/src/prep.rs +++ b/apps/wing-language-server/src/prep.rs @@ -1,4 +1,4 @@ -use std::{cell::RefCell, collections::HashSet}; +use std::cell::RefCell; use tree_sitter::Tree; use wingc::{diagnostic::Diagnostics, parser::Parser, type_check}; @@ -21,9 +21,7 @@ pub fn parse_text(source_file: &str, text: &[u8]) -> ParseResult { } }; - let mut imports = HashSet::new(); let wing_parser = Parser { - imports: RefCell::new(&mut imports), source: &text[..], source_name: source_file.to_string(), diagnostics: RefCell::new(Diagnostics::new()), diff --git a/examples/invalid/circular-bring.w b/examples/invalid/circular-bring.w deleted file mode 100644 index bb663e04632..00000000000 --- a/examples/invalid/circular-bring.w +++ /dev/null @@ -1 +0,0 @@ -bring "./circular/mod.w"; diff --git a/examples/invalid/circular/mod.w b/examples/invalid/circular/mod.w deleted file mode 100644 index 3d5195e0dce..00000000000 --- a/examples/invalid/circular/mod.w +++ /dev/null @@ -1 +0,0 @@ -bring "../circular-bring.w"; diff --git a/examples/simple/bring.w b/examples/simple/bring.w deleted file mode 100644 index 4cf3b12eb04..00000000000 --- a/examples/simple/bring.w +++ /dev/null @@ -1,2 +0,0 @@ -bring "./modules/mod1.w"; -bring "./modules/mod2.w"; diff --git a/examples/simple/modules/mod1.w b/examples/simple/modules/mod1.w deleted file mode 100644 index fdfddbaadf6..00000000000 --- a/examples/simple/modules/mod1.w +++ /dev/null @@ -1,2 +0,0 @@ -let x = 1; -let y = 2; diff --git a/examples/simple/modules/mod2.w b/examples/simple/modules/mod2.w deleted file mode 100644 index 41ccf215dc0..00000000000 --- a/examples/simple/modules/mod2.w +++ /dev/null @@ -1,4 +0,0 @@ -bring "./sub/mod.w"; - -let z = 3; -let q = 4; diff --git a/examples/simple/modules/sub/mod.w b/examples/simple/modules/sub/mod.w deleted file mode 100644 index e717e1f04e2..00000000000 --- a/examples/simple/modules/sub/mod.w +++ /dev/null @@ -1 +0,0 @@ -let w = "what"; diff --git a/libs/tree-sitter-wing/test/corpus/statements.txt b/libs/tree-sitter-wing/test/corpus/statements.txt index ab97b797b17..795a5fc4dab 100644 --- a/libs/tree-sitter-wing/test/corpus/statements.txt +++ b/libs/tree-sitter-wing/test/corpus/statements.txt @@ -254,18 +254,3 @@ struct Test { ) ) ) - -================================== -Bring statement -================================== - -bring cloud; -bring "./mod.w"; - ---- -(source - (short_import_statement - module_name: (identifier)) - (short_import_statement - module_name: (string)) -) diff --git a/libs/wingc/.vscode/launch.json b/libs/wingc/.vscode/launch.json deleted file mode 100644 index 2807f414df2..00000000000 --- a/libs/wingc/.vscode/launch.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "version": "0.2.0", - "configurations": [ - { - "type": "lldb", - "request": "launch", - "name": "library 'wingc' debugger target", - "cargo": { - "args": ["test", "--no-run", "--lib", "--package=wingc"], - "filter": { - "name": "wingc", - "kind": "lib" - } - }, - "args": [], - "cwd": "${workspaceFolder}" - } - ] -} diff --git a/libs/wingc/Cargo.toml b/libs/wingc/Cargo.toml index 2f8f58b4067..d68e7deeb76 100644 --- a/libs/wingc/Cargo.toml +++ b/libs/wingc/Cargo.toml @@ -25,6 +25,3 @@ path = "src/bin.rs" [package.metadata] # Currently wasm-opt is not changing the size of the wasm file, so it's just adding unnecessary time to the build. wasm-opt = false - -[profile.bench] -debug = true diff --git a/libs/wingc/src/ast.rs b/libs/wingc/src/ast.rs index 8c07893380b..9d58c4f773a 100644 --- a/libs/wingc/src/ast.rs +++ b/libs/wingc/src/ast.rs @@ -79,15 +79,10 @@ pub struct Constructor { #[derive(Debug)] pub enum Statement { - // TODO: merge these statements into a single one Use { module_name: Symbol, // Reference? identifier: Option, }, - Bring { - module_path: String, - statements: Scope, - }, VariableDef { var_name: Symbol, initial_value: Expr, diff --git a/libs/wingc/src/capture.rs b/libs/wingc/src/capture.rs index dad23f84f8a..1a56950c4c4 100644 --- a/libs/wingc/src/capture.rs +++ b/libs/wingc/src/capture.rs @@ -262,12 +262,6 @@ fn scan_captures_in_scope(scope: &Scope) -> Vec { } => { todo!() } - Statement::Bring { - module_path: _, - statements, - } => { - res.extend(scan_captures_in_scope(statements)); - } Statement::Struct { name: _, extends: _, diff --git a/libs/wingc/src/jsify.rs b/libs/wingc/src/jsify.rs index f58e52c2d4a..fb16db5008f 100644 --- a/libs/wingc/src/jsify.rs +++ b/libs/wingc/src/jsify.rs @@ -405,15 +405,6 @@ fn jsify_statement(statement: &Statement, out_dir: &PathBuf) -> String { .join("\n") ) } - Statement::Bring { - module_path, - statements, - } => format!( - "/* start bring module: {module} */\n{}\n/* end bring module: {module} */", - jsify_scope(statements, &out_dir), - module = module_path - ) - .to_string(), } } diff --git a/libs/wingc/src/lib.rs b/libs/wingc/src/lib.rs index 56eadf2bb24..2b2ce4500a8 100644 --- a/libs/wingc/src/lib.rs +++ b/libs/wingc/src/lib.rs @@ -1,11 +1,11 @@ #[macro_use] extern crate lazy_static; -use crate::parser::bring; use ast::Scope; use diagnostic::{print_diagnostics, DiagnosticLevel, Diagnostics}; -use std::collections::HashSet; +use crate::parser::Parser; +use std::cell::RefCell; use std::fs; use std::path::PathBuf; @@ -22,6 +22,36 @@ pub mod jsify; pub mod parser; pub mod type_check; +pub fn parse(source_file: &str) -> (Scope, Diagnostics) { + let language = tree_sitter_wing::language(); + let mut parser = tree_sitter::Parser::new(); + parser.set_language(language).unwrap(); + + let source = match fs::read(&source_file) { + Ok(source) => source, + Err(err) => { + panic!("Error reading source file: {}: {:?}", &source_file, err); + } + }; + + let tree = match parser.parse(&source[..], None) { + Some(tree) => tree, + None => { + panic!("Failed parsing source file: {}", source_file); + } + }; + + let wing_parser = Parser { + source: &source[..], + source_name: source_file.to_string(), + diagnostics: RefCell::new(Diagnostics::new()), + }; + + let scope = wing_parser.wingit(&tree.root_node()); + + (scope, wing_parser.diagnostics.into_inner()) +} + pub fn type_check(scope: &mut Scope, types: &mut Types) -> Diagnostics { scope.set_env(TypeEnv::new(None, None, false, Flight::Pre)); let mut tc = TypeChecker::new(types); @@ -31,12 +61,11 @@ pub fn type_check(scope: &mut Scope, types: &mut Types) -> Diagnostics { } pub fn compile(source_file: &str, out_dir: Option<&str>) -> String { - // create a new hashmap to manage imports - let mut imports = HashSet::new(); // Create universal types collection (need to keep this alive during entire compilation) let mut types = Types::new(); // Build our AST - let (mut scope, parse_diagnostics) = bring::bring(source_file, None, &mut imports).unwrap(); + let (mut scope, parse_diagnostics) = parse(source_file); + // Type check everything and build typed symbol environment let type_check_diagnostics = type_check(&mut scope, &mut types); diff --git a/libs/wingc/src/parser.rs b/libs/wingc/src/parser.rs index ddd30b91944..8995d6def64 100644 --- a/libs/wingc/src/parser.rs +++ b/libs/wingc/src/parser.rs @@ -1,9 +1,5 @@ -pub mod bring; - -use bring::bring; use std::cell::RefCell; -use std::collections::{HashMap, HashSet}; -use std::path::PathBuf; +use std::collections::HashMap; use std::{str, vec}; use tree_sitter::Node; @@ -18,7 +14,6 @@ pub struct Parser<'a> { pub source: &'a [u8], pub source_name: String, pub diagnostics: RefCell, - pub imports: RefCell<&'a mut HashSet>, } impl Parser<'_> { @@ -114,39 +109,14 @@ impl Parser<'_> { fn build_statement(&self, statement_node: &Node) -> DiagnosticResult { match statement_node.kind() { - "short_import_statement" => { - let module_name_node = statement_node.child_by_field_name("module_name").unwrap(); - if module_name_node.kind() == "identifier" { - Ok(Statement::Use { - module_name: self.node_symbol(&module_name_node)?, - identifier: if let Some(identifier) = statement_node.child_by_field_name("alias") { - Some(self.node_symbol(&identifier)?) - } else { - None - }, - }) - } else if module_name_node.kind() == "string" { - let bring_target = self.node_text(&module_name_node); - let bring_target = &bring_target[1..bring_target.len() - 1]; - let context_dir = PathBuf::from(&self.source_name); - let context_dir = context_dir.parent().unwrap().to_str().unwrap(); - let brought = bring(bring_target, Some(context_dir), *self.imports.borrow_mut()); - Ok(Statement::Bring { - module_path: bring_target.to_string(), - statements: brought - .unwrap_or(( - Scope { - statements: vec![], - env: None, - }, - Diagnostics::new(), - )) - .0, - }) + "short_import_statement" => Ok(Statement::Use { + module_name: self.node_symbol(&statement_node.child_by_field_name("module_name").unwrap())?, + identifier: if let Some(identifier) = statement_node.child_by_field_name("alias") { + Some(self.node_symbol(&identifier)?) } else { - panic!("Unexpected bring type {}", module_name_node.kind()) - } - } + None + }, + }), "variable_definition_statement" => { let type_ = if let Some(type_node) = statement_node.child_by_field_name("type") { Some(self.build_type(&type_node)?) diff --git a/libs/wingc/src/parser/bring.rs b/libs/wingc/src/parser/bring.rs deleted file mode 100644 index 26c69cb8ec7..00000000000 --- a/libs/wingc/src/parser/bring.rs +++ /dev/null @@ -1,61 +0,0 @@ -use crate::Diagnostics; -use crate::Scope; - -use crate::parser::Parser; -use std::cell::RefCell; -use std::collections::HashSet; -use std::fs; -use std::path::PathBuf; - -pub fn bring(source_file: &str, context: Option<&str>, imports: &mut HashSet) -> Option<(Scope, Diagnostics)> { - // default context directory to the current directory - let context_dir = PathBuf::from(&context.unwrap_or(".")); - // turn "source_file" into a canonical path relative to context_dir - let source_file = context_dir.join(source_file).canonicalize().unwrap(); - let source_file = source_file.to_str().unwrap(); - - if imports.contains(source_file) { - return None; - } - - let language = tree_sitter_wing::language(); - let mut parser = tree_sitter::Parser::new(); - parser.set_language(language).unwrap(); - - let source = match fs::read(&source_file) { - Ok(source) => source, - Err(err) => { - panic!("Error reading source file: {}: {:?}", &source_file, err); - } - }; - - let tree = match parser.parse(&source[..], None) { - Some(tree) => tree, - None => { - println!("Failed parsing source file: {}", source_file); - std::process::exit(1); - } - }; - - // keep track of imports to break recursive import cycles - imports.insert(source_file.to_string()); - - let wing_parser = Parser { - imports: RefCell::new(imports), - source: &source[..], - source_name: source_file.to_string(), - diagnostics: RefCell::new(Diagnostics::new()), - }; - - let scope = wing_parser.wingit(&tree.root_node()); - - for diagnostic in wing_parser.diagnostics.borrow().iter() { - println!("{}", diagnostic); - } - - if wing_parser.diagnostics.borrow().len() > 0 { - std::process::exit(1); - } - - Some((scope, wing_parser.diagnostics.into_inner())) -} diff --git a/libs/wingc/src/type_check.rs b/libs/wingc/src/type_check.rs index 69335d46537..b540ebe7740 100644 --- a/libs/wingc/src/type_check.rs +++ b/libs/wingc/src/type_check.rs @@ -900,12 +900,6 @@ impl<'a> TypeChecker<'a> { let var_type = self.resolve_reference(variable, env); self.validate_type(exp_type, var_type, value); } - Statement::Bring { - module_path: _, - statements: _, - } => { - // no-op - } Statement::Use { module_name, identifier,