Skip to content

Commit

Permalink
Add fin keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
bgk- committed Jul 11, 2024
1 parent 11a1bd0 commit 97353c3
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 5 deletions.
17 changes: 14 additions & 3 deletions docs/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ As a convenience, if you don't pass in a jump to the CLI the first bough in the
# topi run file.topi // START will run in the above example
```

Be careful though, if you include other files the "first" bough will be the first in the other file.

### Nesting

Boughs can be nested and jumped to with `.` like so:
Expand Down Expand Up @@ -201,9 +203,18 @@ Visit paths can be found within scopes and don't need the full path written out.
}
```

### Flow and Jump Back Ups
### Flow, Fin, and Jump Back Ups

If the flow of the story hits a closing backet or `fin`, it will end.
`fin` can be useful to break out of a bough early.

```topi
=== START {
if (true) fin
:: "Will not get here"
}
```

If the flow of the story hits a closing backet it will end.
But sometimes we want to temporarily duck into a bough and then continue on.
We can do that by adding a caret `^` to a jump name `=> [NAME]^` (meaning jump then come back up).

Expand Down Expand Up @@ -441,7 +452,7 @@ const fib = |n| {
}
```

## Return Void
#### Return Void

If you want to return out of a function early you have to specify `return void` and not just `return` like most langauges.

Expand Down
1 change: 1 addition & 0 deletions src/ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ pub const Statement = struct {
},
return_expression: Expression,
return_void: void,
fin: void,
class: struct {
name: []const u8,
field_names: [][]const u8,
Expand Down
3 changes: 3 additions & 0 deletions src/compiler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,9 @@ pub const Compiler = struct {
.return_void => {
try self.writeOp(.return_void, token);
},
.fin => {
try self.writeOp(.fin, token);
},
else => {},
}
}
Expand Down
1 change: 1 addition & 0 deletions src/parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ pub const Parser = struct {
.tilde => try self.choiceStatement(),
.fork => try self.forkStatement(),
.@"for" => try self.forStatement(),
.fin => .{ .token = self.current_token, .type = .fin },
.@"if" => try self.ifStatement(),
.@"switch" => try self.switchStatement(),
.@"while" => try self.whileStatement(),
Expand Down
7 changes: 5 additions & 2 deletions src/token.zig
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ pub const TokenType = enum {
@"while",

bough,
fork,
divert,
fork,
fin,

comment,
eof,
Expand All @@ -105,6 +106,7 @@ pub const Keywords = std.StaticStringMap(TokenType).initComptime(.{
.{ "false", .false },
.{ "for", .@"for" },
.{ "fork", .fork },
.{ "fin", .fin },
.{ "if", .@"if" },
.{ "include", .include },
.{ "List", .list },
Expand Down Expand Up @@ -194,8 +196,9 @@ pub fn toString(token_type: TokenType) []const u8 {
.@"while" => "while",

.bough => "bough",
.fork => "fork",
.divert => "=>",
.fork => "fork",
.fin => "fin",

.comment => "[comment]",
.eof => "[eof]",
Expand Down
12 changes: 12 additions & 0 deletions src/vm.test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,18 @@ test "Jump Code" {
\\ }
,
},
.{
.input =
\\ === START {
\\ => INNER^
\\ :: "Fin executed correctly"
\\ === INNER {
\\ if (INNER == 1) fin
\\ :: "Fin did not work!"
\\ }
\\ }
,
},
};

inline for (test_cases) |case| {
Expand Down

0 comments on commit 97353c3

Please sign in to comment.