Skip to content

Commit

Permalink
feature: parser catch and dot
Browse files Browse the repository at this point in the history
  • Loading branch information
graeme-lockley committed May 19, 2024
1 parent 98c9ac4 commit de88706
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 10 deletions.
32 changes: 32 additions & 0 deletions docs/parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,35 @@ Now that we have the basic mechanism in place, let's systematically go through t
]
}
```

## Catch

```rebo-repl
> rebo.lang.parse("10 catch e -> e")
{ kind: "exprs"
, value:
[ { kind: "catche"
, value: { kind: "literalInt", value: 10 }
, cases:
[ { pattern: { kind: "identifier", value: "e" }
, body: { kind: "identifier", value: "e" }
}
]
}
]
}
```

## Dot

```rebo-repl
> rebo.lang.parse("a.b")
{ kind: "exprs"
, value:
[ { kind: "dot"
, record: { kind: "identifier", value: "a" }
, field: "b"
}
]
}
```
20 changes: 10 additions & 10 deletions src/ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,6 @@ pub const CallExpression = struct {
args: []*Expression,
};

pub const IdDeclarationExpression = struct {
name: *SP.String,
value: *Expression,

pub fn deinit(self: *IdDeclarationExpression, allocator: std.mem.Allocator) void {
self.name.decRef();
destroyExpr(allocator, self.value);
}
};

pub const CatchExpression = struct {
value: *Expression,
cases: []MatchCase,
Expand All @@ -158,6 +148,16 @@ pub const DotExpression = struct {
}
};

pub const IdDeclarationExpression = struct {
name: *SP.String,
value: *Expression,

pub fn deinit(self: *IdDeclarationExpression, allocator: std.mem.Allocator) void {
self.name.decRef();
destroyExpr(allocator, self.value);
}
};

pub const IfCouple = struct {
condition: ?*Expression,
then: *Expression,
Expand Down
73 changes: 73 additions & 0 deletions src/builtins/parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,49 @@ fn emit(machine: *Helper.Runtime, ast: *AST.Expression, position: bool) !void {
}
try machine.setRecordItemBang(pos);
},
.catche => {
try machine.pushEmptyRecordValue();

try machine.pushStringValue("kind");
try machine.pushStringValue("catche");
try machine.setRecordItemBang(pos);

try machine.pushStringValue("value");
try emit(machine, ast.kind.catche.value, position);
try machine.setRecordItemBang(pos);

try machine.pushStringValue("cases");
try machine.pushEmptySequenceValue();
for (ast.kind.catche.cases) |case| {
try machine.pushEmptyRecordValue();

try machine.pushStringValue("pattern");
try emitPattern(machine, case.pattern, position);
try machine.setRecordItemBang(pos);

try machine.pushStringValue("body");
try emit(machine, case.body, position);
try machine.setRecordItemBang(pos);

try machine.appendSequenceItemBang(pos);
}
try machine.setRecordItemBang(pos);
},
.dot => {
try machine.pushEmptyRecordValue();

try machine.pushStringValue("kind");
try machine.pushStringValue("dot");
try machine.setRecordItemBang(pos);

try machine.pushStringValue("record");
try emit(machine, ast.kind.dot.record, position);
try machine.setRecordItemBang(pos);

try machine.pushStringValue("field");
try machine.pushStringValue(ast.kind.dot.field.slice());
try machine.setRecordItemBang(pos);
},
.exprs => {
try machine.pushEmptyRecordValue();

Expand Down Expand Up @@ -125,3 +168,33 @@ fn emit(machine: *Helper.Runtime, ast: *AST.Expression, position: bool) !void {
try machine.setRecordItemBang(pos);
}
}

fn emitPattern(machine: *Helper.Runtime, ast: *AST.Pattern, position: bool) !void {
switch (ast.kind) {
.identifier => {
try machine.pushEmptyRecordValue();

try machine.pushStringValue("kind");
try machine.pushStringValue("identifier");
try machine.setRecordItemBang(pos);

try machine.pushStringValue("value");
try machine.pushStringValue(ast.kind.identifier.slice());
try machine.setRecordItemBang(pos);
},
else => {
std.io.getStdErr().writer().print("unreachable: {}\n", .{ast.kind}) catch {};
unreachable;
},
}

if (position) {
try machine.pushStringValue("position");
try machine.pushEmptySequenceValue();
try machine.pushIntValue(@intCast(ast.position.start));
try machine.appendSequenceItemBang(pos);
try machine.pushIntValue(@intCast(ast.position.end));
try machine.appendSequenceItemBang(pos);
try machine.setRecordItemBang(pos);
}
}

0 comments on commit de88706

Please sign in to comment.