Skip to content

Commit

Permalink
Merge branch 'dev' into yoav/super_calls2
Browse files Browse the repository at this point in the history
  • Loading branch information
yoav-steinberg committed Jul 21, 2023
2 parents cd523c9 + fe9447d commit d13b737
Show file tree
Hide file tree
Showing 48 changed files with 2,128 additions and 98 deletions.
2 changes: 1 addition & 1 deletion docs/contributing/01-start-here/01-contributing-to-wing.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ There are many ways to contribute to Wing:
* Writing [documentation and guides](https://github.com/winglang/wing/issues?q=is:issue+is:open+sort:updated-desc+label:documentation) or adding [examples](/contributing/start-here/docs#%EF%B8%8F-how-do-i-add-an-example)
* Setting up your [development environment](/contributing/start-here/development) and working on the code
* Submitting [pull requests](/contributing/start-here/pull_requests) for new features or helping with [reviews](https://github.com/winglang/wing/pulls)
* Picking up a [good first issue](https://github.com/winglang/wing/issues?q=is%3Aissue+is%3Aopen+label%3A%221%EF%B8%8F%E2%83%A3+good+first+issue%22+no%3Aassignee+sort%3Aupdated-desc) to work on
* Picking up a [good first issue](https://github.com/winglang/wing/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22+no%3Aassignee+sort%3Aupdated-desc+) to work on
* Reporting and fixing [bugs](/contributing/start-here/bugs)
* Contributing to the [Wing SDK (standard library)](/contributing/start-here/wingsdk)
* Find solutions to common issues in our [troubleshooting guide](/contributing/start-here/troubleshooting)
Expand Down
12 changes: 8 additions & 4 deletions docs/docs/01-start-here/03-hello.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@ entrypoint of your Wing application.
bring cloud;

let bucket = new cloud.Bucket();
let counter = new cloud.Counter(
initial: 1
);
let queue = new cloud.Queue();

queue.setConsumer(inflight (message: str) => {
bucket.put("wing.txt", "Hello, ${message}");
log("file created");
let index = counter.inc();
bucket.put("wing-${index}.txt", "Hello, ${message}");
log("file wing-${index}.txt created");
});
```

This code should be mostly self explanatory. We define a queue, and every time a
message is added to the queue, a handler is triggered and creates a file named `wing.txt` with `"Hello, ${message}!"` content .
This code should be mostly self explanatory. We define a queue and a counter, and every time a
message is added to the queue, a handler is triggered and creates a file named `wing-<counter-index>.txt` with `"Hello, ${message}!"` content, and the counter is incremented by 1.

Now that we've written this program, let's run and test it using the **Wing Console**.
2 changes: 1 addition & 1 deletion docs/docs/01-start-here/04-run-locally.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ On the interaction panel, Type a message in the `Push Message` text area (let's

Now, click on the bucket in the map view. Notice that the interaction panel has changed and now shows the bucket's interaction view.

When you click on the `wing.txt` file, you should be able to see its contents.
When you click on the `wing-1.txt` file, you should be able to see its contents.

![Bucket resource view in Wing Console](./console-bucket-1.png 'Bucket resource')

Expand Down
13 changes: 8 additions & 5 deletions docs/docs/01-start-here/05-aws.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ plan. Resource actions are indicated with the following symbols:

Terraform will perform the following actions:

# aws_dynamodb_table.cloudCounter will be created
+ resource "aws_dynamodb_table" "cloudCounter" {...}

# aws_iam_role.cloudQueue-SetConsumer-cdafee6e_IamRole_2548D828 will be created
+ resource "aws_iam_role" "cloudQueue-SetConsumer-cdafee6e_IamRole_2548D828" {...}

Expand Down Expand Up @@ -123,7 +126,7 @@ Terraform will perform the following actions:
# aws_sqs_queue.cloudQueue will be created
+ resource "aws_sqs_queue" "cloudQueue" {...}

Plan: 11 to add, 0 to change, 0 to destroy.
Plan: 12 to add, 0 to change, 0 to destroy.
```

> This is a good opportunity to observe how much complexity the Wing compiler
Expand All @@ -134,7 +137,7 @@ If you choose to proceed, Terraform will do its magic and will create all of the
account.

```
Apply complete! Resources: 11 added, 0 changed, 0 destroyed.
Apply complete! Resources: 12 added, 0 changed, 0 destroyed.
```

## Explore your app on AWS
Expand All @@ -149,15 +152,15 @@ through the AWS Management Console.
5. In the **Message Body** box type `cloud` and hit **Send message**.
6. Jump over to the [S3 Console](https://s3.console.aws.amazon.com/s3/buckets)
7. There should be some buckets prefixed with `cloud-bucket-`.
8. Cycle through the buckets until you find one that contains `wing.txt`.
9. Click `wing.txt` then click the `Open` button.
8. Cycle through the buckets until you find one that contains `wing-1.txt`.
9. Click `wing-1.txt` then click the `Open` button.
10. The file should contain `Hello, cloud`.

## Cleanup

Terraform doesn't allow destroying a non-empty bucket by default. To prepare for
easy cleanup, you may delete the newly created file by marking the checkbox next
to `wing.txt`, clicking the `Delete` button, typing `permanently delete` in the
to `wing-1.txt`, clicking the `Delete` button, typing `permanently delete` in the
confirmation box and clicking the `Delete objects` button.

Once you're done, you can destroy all of the resources that were created on your AWS account by running:
Expand Down
Binary file modified docs/docs/01-start-here/console-app.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/docs/01-start-here/console-bucket-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/docs/01-start-here/console-queue.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions docs/docs/04-standard-library/04-util/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Utility functions.

| **Name** | **Description** |
| --- | --- |
| <code><a href="#@winglang/sdk.util.Util.base64Decode">base64Decode</a></code> | Converts a string from base64 to UTF-8. |
| <code><a href="#@winglang/sdk.util.Util.base64Encode">base64Encode</a></code> | Converts a string from UTF-8 to base64. |
| <code><a href="#@winglang/sdk.util.Util.env">env</a></code> | Returns the value of an environment variable. |
| <code><a href="#@winglang/sdk.util.Util.nanoid">nanoid</a></code> | Generates a unique ID using the nanoid library. |
| <code><a href="#@winglang/sdk.util.Util.sha256">sha256</a></code> | Computes the SHA256 hash of the given data. |
Expand All @@ -33,6 +35,56 @@ Utility functions.

---

##### `base64Decode` <a name="base64Decode" id="@winglang/sdk.util.Util.base64Decode"></a>

```wing
bring util;
util.base64Decode(stringToDecode: str, url?: bool);
```

Converts a string from base64 to UTF-8.

###### `stringToDecode`<sup>Required</sup> <a name="stringToDecode" id="@winglang/sdk.util.Util.base64Decode.parameter.stringToDecode"></a>

- *Type:* str

base64 string to decode.

---

###### `url`<sup>Optional</sup> <a name="url" id="@winglang/sdk.util.Util.base64Decode.parameter.url"></a>

- *Type:* bool

If `true`, the source is expected to be a URL-safe base64 string.

---

##### `base64Encode` <a name="base64Encode" id="@winglang/sdk.util.Util.base64Encode"></a>

```wing
bring util;
util.base64Encode(stringToEncode: str, url?: bool);
```

Converts a string from UTF-8 to base64.

###### `stringToEncode`<sup>Required</sup> <a name="stringToEncode" id="@winglang/sdk.util.Util.base64Encode.parameter.stringToEncode"></a>

- *Type:* str

---

###### `url`<sup>Optional</sup> <a name="url" id="@winglang/sdk.util.Util.base64Encode.parameter.url"></a>

- *Type:* bool

If `true`, a URL-safe base64 is returned.

---

##### `env` <a name="env" id="@winglang/sdk.util.Util.env"></a>

```wing
Expand Down
2 changes: 2 additions & 0 deletions examples/tests/invalid/bring_local_self.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bring "./bring_local_self.w" as foo;
// ^ error: Cannot bring a module into itself
3 changes: 3 additions & 0 deletions examples/tests/invalid/bring_local_variables.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bring "./file_with_variables.w" as stuff;

new stuff.Bar();
12 changes: 12 additions & 0 deletions examples/tests/invalid/file_with_variables.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// used by bring_local_variables.w

bring cloud;

let x = 5;
let y = ["hello", "world"];
let z = new cloud.Bucket();

class Bar {
x: num;
}

25 changes: 25 additions & 0 deletions examples/tests/sdk_tests/util/base64.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
bring util;

let string = "https://www.winglang.io/docs";
let base64Encode = util.base64Encode(string);
let base64urlEncode = util.base64Encode(string, true);
let base64Decode = util.base64Decode("aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw==");
let base64urlDecode = util.base64Decode("aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw", true);

assert(base64Encode == "aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw==");
assert(base64urlEncode == "aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw");
assert(base64Decode == string);
assert(base64urlDecode == string);

test "inflight base64" {
let string = "https://www.winglang.io/docs";
let base64Encode = util.base64Encode(string);
let base64urlEncode = util.base64Encode(string, true);
let base64Decode = util.base64Decode("aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw==");
let base64urlDecode = util.base64Decode("aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw", true);

assert(base64Encode == "aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw==");
assert(base64urlEncode == "aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw");
assert(base64Decode == string);
assert(base64urlDecode == string);
}
29 changes: 29 additions & 0 deletions examples/tests/valid/bring_local.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
bring "./store.w" as file1;
bring "./subdir/subfile.w" as file2;
bring "./subdir/empty.w" as file3;

// classes from other files can be used
let store = new file1.Store();
let q = new file2.Q();

test "add data to store" {
store.store("foo");
}

// structs from other files can be used
let s = file1.Point {
x: 1,
y: 2,
};

// enums from other files can be used
let c = file1.Color.BLUE;
assert(c != file1.Color.RED);

// interfaces from other files can be used
class Triangle impl file1.Shape {
area(): num {
return 1;
}
}
let t = new Triangle();
26 changes: 26 additions & 0 deletions examples/tests/valid/store.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
bring cloud;

class Store {
b: cloud.Bucket;
init() {
this.b = new cloud.Bucket();
}
inflight store(data: str) {
this.b.put("data.txt", data);
}
}

enum Color {
RED,
GREEN,
BLUE,
}

struct Point {
x: num;
y: num;
}

interface Shape {
area(): num;
}
Empty file.
1 change: 1 addition & 0 deletions examples/tests/valid/subdir/subfile.w
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class Q {}
4 changes: 2 additions & 2 deletions libs/tree-sitter-wing/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ module.exports = grammar({
_statement: ($) =>
choice(
$.test_statement,
$.short_import_statement,
$.import_statement,
$.expression_statement,
$.variable_definition_statement,
$.variable_assignment_statement,
Expand All @@ -117,7 +117,7 @@ module.exports = grammar({
$.super_constructor_statement
),

short_import_statement: ($) =>
import_statement: ($) =>
seq(
"bring",
field("module_name", choice($.identifier, $.string)),
Expand Down
12 changes: 8 additions & 4 deletions libs/wingc/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use indexmap::{Equivalent, IndexMap, IndexSet};
use itertools::Itertools;

use crate::diagnostic::WingSpan;
use crate::type_check::symbol_env::SymbolEnv;
use crate::type_check::symbol_env::SymbolEnvRef;
use crate::type_check::CLOSURE_CLASS_HANDLE_METHOD;

static EXPR_COUNTER: AtomicUsize = AtomicUsize::new(0);
Expand Down Expand Up @@ -424,6 +424,10 @@ pub enum StmtKind {
module_name: Symbol, // Reference?
identifier: Option<Symbol>,
},
Module {
name: Symbol,
statements: Scope,
},
SuperConstructor {
arg_list: ArgList,
},
Expand Down Expand Up @@ -645,13 +649,13 @@ pub enum InterpolatedStringPart {
Expr(Expr),
}

#[derive(Derivative)]
#[derive(Derivative, Default)]
#[derivative(Debug)]
pub struct Scope {
pub statements: Vec<Stmt>,
pub span: WingSpan,
#[derivative(Debug = "ignore")]
pub env: RefCell<Option<SymbolEnv>>, // None after parsing, set to Some during type checking phase
pub env: RefCell<Option<SymbolEnvRef>>, // None after parsing, set to Some during type checking phase
}

impl Scope {
Expand All @@ -663,7 +667,7 @@ impl Scope {
}
}

pub fn set_env(&self, new_env: SymbolEnv) {
pub fn set_env(&self, new_env: SymbolEnvRef) {
let mut env = self.env.borrow_mut();
assert!((*env).is_none());
*env = Some(new_env);
Expand Down
4 changes: 4 additions & 0 deletions libs/wingc/src/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ where
module_name: f.fold_symbol(module_name),
identifier: identifier.map(|id| f.fold_symbol(id)),
},
StmtKind::Module { name, statements } => StmtKind::Module {
name: f.fold_symbol(name),
statements: f.fold_scope(statements),
},
StmtKind::Let {
reassignable,
var_name,
Expand Down
Loading

0 comments on commit d13b737

Please sign in to comment.