Skip to content

Commit

Permalink
feat: Changed function yield type prefix
Browse files Browse the repository at this point in the history
closes #257
  • Loading branch information
giann committed Feb 2, 2024
1 parent ba5fc88 commit f4fa2e1
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var value = from {
- `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)
- Function yield type is now prefixed with `*>`: `fun willYield() > T > Y?` becomes `fun willYield() > T *> Y?` (https://github.com/buzz-language/buzz/issues/257)

## Fixed

Expand Down
4 changes: 2 additions & 2 deletions examples/sqlite.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export object Query {
return db.prepare(this);
}

fun execute(Database db) > [[Boxed]] > [Boxed]? !> SQLiteError {
fun execute(Database db) > [[Boxed]] *> [Boxed]? !> SQLiteError {
return db.prepare(this).execute();
}
}
Expand All @@ -151,7 +151,7 @@ export object Statement {
ud db,
ud stmt,

fun execute() > [[Boxed]] > [Boxed]? !> SQLiteError {
fun execute() > [[Boxed]] *> [Boxed]? !> SQLiteError {
Logger.instance?.debug("Executing query `{this.query}`", namespace: "SQL");
ResultCode code = ResultCode.Ok;

Expand Down
13 changes: 8 additions & 5 deletions src/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2666,10 +2666,11 @@ fn parseFunctionType(self: *Self, parent_generic_types: ?std.AutoArrayHashMap(*o
else
null;

const yield_type = if (try self.match(.Greater))
try self.parseTypeDef(null, true)
else
null;
var yield_type: ?Ast.Node.Index = null;
if (try self.match(.Star)) {
try self.consume(.Greater, "Expected `>` before yield type");
yield_type = try self.parseTypeDef(null, true);
}

var error_types_list = std.ArrayList(Ast.Node.Index).init(self.gc.allocator);
defer error_types_list.shrinkAndFree(error_types_list.items.len);
Expand Down Expand Up @@ -4887,7 +4888,9 @@ fn function(
}

// Parse yield type
const yield_type_node = if (return_type_node != null and function_type.canYield() and (try self.match(.Greater))) yield: {
const yield_type_node = if (return_type_node != null and function_type.canYield() and (try self.match(.Star))) yield: {
try self.consume(.Greater, "Expected `>` before yield type");

const yield_type_node = try self.parseTypeDef(function_typedef.resolved_type.?.Function.generic_types, true);
const yield_type = self.ast.nodes.items(.type_def)[yield_type_node].?;

Expand Down
2 changes: 1 addition & 1 deletion src/lib/http.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export object Client {
};
}

fun send(Request request) > Response > void !> HttpError, InvalidArgumentError, HttpParseError {
fun send(Request request) > Response *> void !> HttpError, InvalidArgumentError, HttpParseError {
_ = this.start(request);

yield void;
Expand Down
2 changes: 1 addition & 1 deletion tests/038-fibers.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test "fiber" {
}

| returns str, yields int
fun count(int n) > str > int? {
fun count(int n) > str *> int? {
assert(currentFiber() is fib<str, int?>, message: "Can get current fiber");
assert(!currentFiber().isMain(), message: "Can know if fiber is main one");

Expand Down
4 changes: 2 additions & 2 deletions tests/041-iterator.buzz
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "std";

fun fibonnaci(int n) > void > int? {
fun fibonnaci(int n) > void *> int? {
int n1 = 0;
int n2 = 1;
int? next = null;
Expand All @@ -24,7 +24,7 @@ test "finobacci generator" {
}

object Hello {
fun range() > [int] > int? {
fun range() > [int] *> int? {
var list = [<int>];
foreach (int i in 0..10) {
_ = yield i;
Expand Down
2 changes: 1 addition & 1 deletion tests/048-generics.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ test "generic return" {
assert(genericReturn::<int>(12) == 12, message: "could use return of generic type");
}

fun fiber::<T>([T] data) > void > T? {
fun fiber::<T>([T] data) > void *> T? {
foreach (T element in data) {
_ = yield element;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/059-types-as-value.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test "typeof" {
assert(typeof 1 == <int>, message: "typeof operator int");
assert(typeof 3.14 == <float>, message: "typeof operator float");
assert(typeof $"hello" == <pat>, message: "typeof operator pattern");
assert(typeof dumpType == <Function(type myType) > void > void>, message: "typeof operator");
assert(typeof dumpType == <Function(type myType) > void *> void>, message: "typeof operator");
}

test "type argument" {
Expand Down
2 changes: 1 addition & 1 deletion tests/compile_errors/006-deep-yield.buzz
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
| Bad function yield type
import "std";

fun one() > void > str? {
fun one() > void *> str? {
yield "hello";
}

Expand Down
2 changes: 1 addition & 1 deletion tests/compile_errors/021-fiber-error-location.buzz
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
| :7:13
import "std";

fun count(int n) > str > int? {
fun count(int n) > str *> int? {
for (int i = 0; i < n; i = i + 1) {
if (i == 2) {
throw "an error occured";
Expand Down

0 comments on commit f4fa2e1

Please sign in to comment.