Skip to content

Commit

Permalink
fix: Changed block expression syntax
Browse files Browse the repository at this point in the history
`<{ ... }` to `from { ... }`

Because `<{ ... }` was ambiguous with a type value of map `<{K: V}>`
  • Loading branch information
giann committed Feb 2, 2024
1 parent ff33748 commit ba5fc88
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const person = Person {
- Sandboxing build options `memory_limit` and `cycle_limit` (https://github.com/buzz-language/buzz/issues/182)
- Block expression (https://github.com/buzz-language/buzz/issues/105):
```buzz
var value = <{
var value = from {
| ...
out result;
Expand All @@ -41,6 +41,7 @@ var value = <{
- Map type notation has changed from `{K, V}` to `{K: V}`. Similarly map expression with specified typed went from `{<K, V>, ...}` to `{<K: V>, ...}` (https://github.com/buzz-language/buzz/issues/253)
- `File.readLine`, `File.readAll`, `Socket.readLine`, `Socket.readAll` have now an optional `maxSize` argument
- Tail call optimization (https://github.com/buzz-language/buzz/issues/9). The effect should be limited for recursive calls since the JIT should kick in pretty quickly in those use cases.
- Empty list and map without a specified type resolve to `[any]`/`{any: any}` unless the variable declaration context provides the type (https://github.com/buzz-language/buzz/issues/86)

## Fixed

Expand Down
5 changes: 3 additions & 2 deletions src/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ const rules = [_]ParseRule{
.{ .prefix = null, .infix = null, .precedence = .None }, // Export
.{ .prefix = null, .infix = null, .precedence = .None }, // Const
.{ .prefix = null, .infix = null, .precedence = .None }, // Static
.{ .prefix = null, .infix = null, .precedence = .None }, // From
.{ .prefix = blockExpression, .infix = null, .precedence = .None }, // From
.{ .prefix = null, .infix = null, .precedence = .None }, // As
.{ .prefix = null, .infix = as, .precedence = .IsAs }, // AsQuestion
.{ .prefix = null, .infix = null, .precedence = .None }, // Extern
Expand All @@ -343,7 +343,6 @@ const rules = [_]ParseRule{
.{ .prefix = null, .infix = null, .precedence = .None }, // zdef
.{ .prefix = typeOfExpression, .infix = null, .precedence = .Unary }, // typeof
.{ .prefix = null, .infix = null, .precedence = .None }, // var
.{ .prefix = blockExpression, .infix = null, .precedence = .None }, // <{
.{ .prefix = null, .infix = null, .precedence = .None }, // out
};

Expand Down Expand Up @@ -5389,6 +5388,8 @@ fn typeOfExpression(self: *Self, _: bool) Error!Ast.Node.Index {
fn blockExpression(self: *Self, _: bool) Error!Ast.Node.Index {
const start_location = self.current_token.? - 1;

try self.consume(.LeftBrace, "Expected `{` at start of block expression");

self.beginScope();
self.current.?.in_block_expression = self.current.?.scope_depth;

Expand Down
1 change: 0 additions & 1 deletion src/Token.zig
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ pub const Type = enum {
Zdef, // zdef
TypeOf, // typeof
Var, // var
Blk, // <{
Out, // out
};

Expand Down
3 changes: 0 additions & 3 deletions src/scanner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ pub const Scanner = struct {
self.makeToken(.ShiftLeft, null, null, null)
else if (self.match('='))
self.makeToken(.LessEqual, null, null, null)
else if (self.match('{'))
self.makeToken(.Blk, null, null, null)
else
self.makeToken(.Less, null, null, null),
'~' => self.makeToken(.Bnot, null, null, null),
Expand Down Expand Up @@ -688,7 +686,6 @@ pub const Scanner = struct {
.Arrow,
.Ampersand,
.Spread,
.Blk,
=> if (true_color) Color.punctuation else Color.magenta,
.IntegerValue,
.FloatValue,
Expand Down
10 changes: 10 additions & 0 deletions tests/004-lists.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,14 @@ test "list.clone" {
foreach (int i, int el in copy) {
assert(list[i] == el, message: "Could clone list");
}
}

test "empty list type inferring" {
var list = [];

assert(typeof list == <[any]>);

[str] slist = [];

assert(typeof slist == <[str]>);
}
10 changes: 10 additions & 0 deletions tests/005-maps.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,14 @@ test "map.clone" {
foreach (str key, int value in copy) {
assert(first[key] == value, message: "Could clone map");
}
}

test "empty map type inferring" {
var map = {};

assert(typeof map == <{any: any}>);

{str: int} smap = {};

assert(typeof smap == <{str: int}>);
}
2 changes: 1 addition & 1 deletion tests/070-block-expression.buzz
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "std";

test "block expression" {
var value = <{
var value = from {
print("doing stuff in my block...");

out "my value";
Expand Down
2 changes: 1 addition & 1 deletion tests/compile_errors/025-multiple-out.buzz
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
| Only one `out` statement is allowed in block expression
test "multiple out statements" {
<{
from {
out "one";
out "two";
};
Expand Down
2 changes: 1 addition & 1 deletion tests/compile_errors/026-out-last-statement.buzz
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
| Last block expression statement must be `out`
test "out must be last statement" {
<{
from {
out "i should be last";

"hello world";
Expand Down

0 comments on commit ba5fc88

Please sign in to comment.