Skip to content

Commit

Permalink
feat: Nullable variable have null default value
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Oct 4, 2023
1 parent 4e244e4 commit 68d27c8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
- Changed pattern delimiters (https://github.com/buzz-language/buzz/issues/165)
- `list.append` does not return the appended value anymore
- Generic types syntax changed from `myFunction(<K,V>, ...)` to `myFunction::<K,V>(...)`
- Nullable object fields and nullable variables have a `null` initial value if none is provided

## Fixed

Expand Down
11 changes: 9 additions & 2 deletions src/parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2579,8 +2579,15 @@ pub const Parser = struct {
var value: ?*ParseNode = null;

if (should_assign) {
try self.consume(.Equal, "Expected variable initial value");
value = try self.expression(false);
if (try self.match(.Equal)) {
value = try self.expression(false);
} else if (parsed_type == null or !parsed_type.?.optional) {
self.reporter.reportErrorAt(
.syntax,
self.parser.previous_token.?,
"Expected variable initial value",
);
}
}

if (var_type.def_type == .Placeholder and value != null and value.?.type_def != null and value.?.type_def.?.def_type == .Placeholder) {
Expand Down
14 changes: 0 additions & 14 deletions tests/063-field-null-default.buzz

This file was deleted.

20 changes: 20 additions & 0 deletions tests/063-nullable-default.buzz
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import "std";

object Person {
str name,
int? age,
}

test "Nullable object field has a default value at null" {
Person person = Person{
name = "Joe"
};

assert(person.age == null, message: "Nullable object field has a default value at null");
}

test "Nullable variable has a default value at null" {
str? hello;

assert(hello == null, message: "Nullable variable has default value at null");
}

0 comments on commit 68d27c8

Please sign in to comment.