Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compiler): bring local wing files #252

Merged
merged 45 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
c9422ef
wip: Import JSII types to get `bring cloud` working.
yoav-steinberg Sep 22, 2022
e84ce2f
Removed `Nothing` from out type system.
yoav-steinberg Sep 22, 2022
fd76050
Merge branch 'main' into yoav/jsii_import
yoav-steinberg Sep 27, 2022
d7348fb
No need to double check jsii_reflection api following #159.
yoav-steinberg Sep 27, 2022
9cf1d49
WIP: JSII imports:
yoav-steinberg Sep 28, 2022
fd039f5
wip: Import JSII "datatype" interfaces as wing structs.
yoav-steinberg Sep 29, 2022
e614ca0
Handle non primitive type references in JSII imports:
yoav-steinberg Oct 4, 2022
6916688
Treat wingsdk `cloud.Inflight` as `~(any):any` inflight function.
yoav-steinberg Oct 4, 2022
7e23309
Struct literals (currently with a grammar hack `@TypeName`)
yoav-steinberg Oct 6, 2022
2b33b9a
fix: removed Yoav's @ in struct literals
3p3r Oct 6, 2022
8f53968
wingsdk cloud JSII import
yoav-steinberg Oct 9, 2022
0c2e469
oops
yoav-steinberg Oct 9, 2022
55b5433
Merge branch 'main' into yoav/jsii_import
MarkMcCulloh Oct 9, 2022
d1b2eb0
Apply suggestions from code review
yoav-steinberg Oct 10, 2022
424902f
feat(wingc): bring local wing files
3p3r Oct 11, 2022
cbe71e7
Added support for namespace aliases on JSII brings.
yoav-steinberg Oct 12, 2022
08ff9ac
jisify: Remoce hack to always add scope and id to anything objects.
yoav-steinberg Oct 12, 2022
e1271e5
cr fix
yoav-steinberg Oct 12, 2022
3dbae32
cr fix
yoav-steinberg Oct 12, 2022
2f3c6af
cr fix
yoav-steinberg Oct 12, 2022
798f1fc
CR: Fix constructor name.
yoav-steinberg Oct 12, 2022
e04275b
CR fix
yoav-steinberg Oct 12, 2022
e4388e5
cr fix
yoav-steinberg Oct 12, 2022
2842678
Merge branch 'main' into yoav/jsii_import
yoav-steinberg Oct 12, 2022
0c83f75
Update libs/wingc/src/type_check/jsii_importer.rs
yoav-steinberg Oct 12, 2022
c4c3575
Support inheriting from from wingsdk.
yoav-steinberg Oct 12, 2022
4185d9b
fix grammar test
yoav-steinberg Oct 12, 2022
cfc50fd
feat: hacked together a jsii-ify to experiment with the idea of compi…
3p3r Oct 13, 2022
9c597fe
cr
yoav-steinberg Oct 13, 2022
77e6b52
Merge branch 'main' into yoav/jsii_import
yoav-steinberg Oct 13, 2022
ba7cc09
Merge branch 'main' into yoav/jsii_import
yoav-steinberg Oct 13, 2022
b7ba6e5
feat(wingc): bring local wing files
3p3r Oct 11, 2022
feb6579
feat: hacked together a jsii-ify to experiment with the idea of compi…
3p3r Oct 13, 2022
f8518c7
Merge branch 'sep/wing_import' of github.com:monadahq/winglang into s…
3p3r Oct 13, 2022
abdb66b
PR feedback addressed
3p3r Oct 13, 2022
96a0a04
fixed builds
3p3r Oct 13, 2022
3636cb8
Merge branch 'main' into sep/wing_import
3p3r Oct 20, 2022
a9b5e12
Merge branch 'main' into sep/wing_import
3p3r Oct 26, 2022
fe60b71
Merge branch 'main' into sep/wing_import
3p3r Oct 27, 2022
d0c5fdf
Merge branch 'main' into sep/wing_import
3p3r Oct 27, 2022
5c69e88
Merge branch 'main' into sep/wing_import
3p3r Oct 28, 2022
59991af
Merge branch 'main' into sep/wing_import
3p3r Oct 28, 2022
2d648e3
Merge branch 'main' into sep/wing_import
3p3r Oct 31, 2022
10023c5
Merge branch 'main' into sep/wing_import
3p3r Oct 31, 2022
085c4a1
fix lang server's build error with the new imports on parser instances
3p3r Oct 31, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/invalid/circular-bring.w
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bring "./circular/mod.w";
1 change: 1 addition & 0 deletions examples/invalid/circular/mod.w
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bring "../circular-bring.w";
2 changes: 2 additions & 0 deletions examples/simple/bring.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bring "./modules/mod1.w";
bring "./modules/mod2.w";
2 changes: 2 additions & 0 deletions examples/simple/modules/mod1.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let x = 1;
let y = 2;
4 changes: 4 additions & 0 deletions examples/simple/modules/mod2.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bring "./sub/mod.w";

let z = 3;
let q = 4;
1 change: 1 addition & 0 deletions examples/simple/modules/sub/mod.w
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let w = "what";
3p3r marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 15 additions & 0 deletions libs/tree-sitter-wing/test/corpus/statements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,18 @@ struct Test {
)
)
)

==================================
Bring statement
==================================

bring cloud;
bring "./mod.w";

---
(source
(short_import_statement
module_name: (identifier))
(short_import_statement
module_name: (string))
)
19 changes: 19 additions & 0 deletions libs/wingc/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"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}"
}
]
}
3 changes: 3 additions & 0 deletions libs/wingc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ 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
5 changes: 5 additions & 0 deletions libs/wingc/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,15 @@ pub struct Constructor {

#[derive(Debug)]
pub enum Statement {
// TODO: merge these statements into a single one
Use {
module_name: Symbol, // Reference?
identifier: Option<Symbol>,
},
Bring {
module_path: String,
statements: Scope,
},
Chriscbr marked this conversation as resolved.
Show resolved Hide resolved
VariableDef {
var_name: Symbol,
initial_value: Expr,
Expand Down
6 changes: 6 additions & 0 deletions libs/wingc/src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ fn scan_captures_in_scope(scope: &Scope) -> Vec<Capture> {
} => {
todo!()
}
Statement::Bring {
module_path: _,
statements,
} => {
res.extend(scan_captures_in_scope(statements));
}
Statement::Struct {
name: _,
extends: _,
Expand Down
9 changes: 9 additions & 0 deletions libs/wingc/src/jsify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,15 @@ 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(),
}
}

Expand Down
196 changes: 196 additions & 0 deletions libs/wingc/src/jsiiify.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
use crate::{ast::*, type_check::Types};
use serde_json::json;
use std::{collections::BTreeMap, fs};
use wingii::jsii;

/// generates a JSII manifest for the given scope
pub fn jsiiify(scope: &Scope, types: &Types) -> String {
let mut jsii_types: BTreeMap<String, serde_json::Value> = BTreeMap::new();
for statement in scope.statements.iter() {
match statement {
Statement::VariableDef { .. } => {}
Statement::FunctionDefinition(func_def) => match func_def.signature.flight {
Flight::In => {}
Flight::Pre => {}
},
Statement::Class {
constructor,
is_resource,
members,
methods,
name,
parent,
} => {
assert!(!is_resource, "resources are not supported yet");
let manifest = jsii::ClassType {
abstract_: Some(false),
assembly: "wingc".to_string(),
base: if parent.is_some() {
None
} else {
None
},
docs: None,
fqn: format!("wingc.{}", name.name),
interfaces: None,
kind: "class".to_string(),
namespace: None,
location_in_module: None,
symbol_id: None,
name: name.name.to_string(),
initializer: Some(jsii::Callable {
docs: None,
location_in_module: None,
overrides: None,
protected: None,
variadic: None,
parameters: Some(
constructor
.parameters
.iter()
.map(|param| jsii::Parameter {
docs: None,
name: param.name.to_string(),
optional: None,
type_: json!({ "primitive": "any" }),
variadic: None,
})
.collect(),
),
}),
properties: Some(
members
.iter()
.map(|member| jsii::Property {
docs: None,
location_in_module: None,
name: member.name.name.to_string(),
optional: None,
protected: None,
abstract_: None,
const_: None,
immutable: None,
overrides: None,
static_: None,
type_: json!({ "primitive": "any" }),
})
.collect(),
),
methods: Some(
methods
.iter()
.map(|method| jsii::Method {
docs: None,
async_: None,
variadic: None,
location_in_module: None,
// remove everything after first space (?)
name: method.name.name.to_string(),
protected: None,
abstract_: None,
overrides: None,
static_: None,
returns: Some(jsii::OptionalValue {
optional: None,
type_: json!({ "primitive": "any" }),
}),
parameters: Some(
method
.parameters
.iter()
.map(|param| jsii::Parameter {
docs: None,
name: param.name.to_string(),
optional: None,
type_: json!({ "primitive": "any" }),
variadic: None,
})
.collect(),
),
})
.collect(),
),
};
jsii_types.insert(name.name.clone(), serde_json::to_value(manifest).unwrap());
}
Statement::Struct { extends, members, name } => {
let manifest = jsii::ClassType {
abstract_: Some(false),
base: match extends.len() {
0 => None,
1 => Some(extends[0].name.clone()),
_ => panic!("multi inheritance not supported yet"),
},
assembly: "wing".to_string(),
docs: None,
fqn: format!("wing.{}", name.name),
initializer: None,
interfaces: None,
kind: "class".to_string(),
location_in_module: None,
name: name.name.clone(),
namespace: None,
symbol_id: None,
methods: None,
properties: Some(
members
.iter()
.map(|member| jsii::Property {
abstract_: None,
docs: None,
immutable: Some(true),
location_in_module: None,
name: member.name.name.clone(),
optional: None,
overrides: None,
protected: None,
const_: None,
static_: None,
type_: json!({
"primitive": "any"
}),
})
.collect(),
),
};
jsii_types.insert(name.name.clone(), serde_json::to_value(manifest).unwrap());
}
_ => {}
}
}
let assembly = jsii::Assembly {
name: "wing".to_string(),
version: "0.1.0".to_string(),
description: "description".to_string(),
homepage: "homepage".to_string(),
license: "MIT".to_string(),
repository: jsii::AssemblyRepository {
directory: None,
type_: "git".to_string(),
url: "#blank".to_string(),
},
author: jsii::Person {
email: None,
name: "wingc".to_string(),
organization: None,
url: None,
roles: vec![],
},
fingerprint: "fingerprint".to_string(),
jsii_version: "3.20.120".to_string(),
schema: "jsii/0.10.0".to_string(),
targets: None,
types: Some(jsii_types),
bin: None,
bundled: None,
contributors: None,
dependencies: None,
dependency_closure: None,
docs: None,
keywords: None,
metadata: None,
readme: None,
submodules: None,
};
return serde_json::to_string_pretty(&assembly).unwrap();
}
50 changes: 10 additions & 40 deletions libs/wingc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::parser::bring;
use ast::Scope;
use diagnostic::Diagnostics;

use crate::parser::Parser;
use std::cell::RefCell;
use std::collections::HashSet;
use std::fs;
use std::path::PathBuf;

Expand All @@ -15,59 +17,23 @@ pub mod ast;
pub mod capture;
pub mod diagnostic;
pub mod jsify;
pub mod jsiiify;
pub mod parser;
pub mod type_check;

pub fn parse(source_file: &str) -> Scope {
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);
}
};

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());

for diagnostic in wing_parser.diagnostics.borrow().iter() {
println!("{}", diagnostic);
}

if wing_parser.diagnostics.borrow().len() > 0 {
std::process::exit(1);
}

scope
}

pub fn type_check(scope: &mut Scope, types: &mut Types) {
scope.set_env(TypeEnv::new(None, None, false, Flight::Pre));
let mut tc = TypeChecker::new(types);
tc.type_check_scope(scope);
}

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(source_file);
let mut scope = bring::bring(source_file, None, &mut imports).unwrap();
// Type check everything and build typed symbol environment
type_check(&mut scope, &mut types);
// Analyze inflight captures
Expand All @@ -81,6 +47,10 @@ pub fn compile(source_file: &str, out_dir: Option<&str>) -> String {
let intermediate_file = out_dir.join("intermediate.js");
fs::write(&intermediate_file, &intermediate_js).expect("Write intermediate JS to disk");

let jsii_manifest = jsiiify::jsiiify(&scope, &types);
let jsii_manifest_file = out_dir.join(".jsii");
fs::write(&jsii_manifest_file, &jsii_manifest).expect("Write JSII manifest to disk");

return intermediate_js;
}

Expand Down
Loading