-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: scaffold parser into rebo runtime
- Loading branch information
1 parent
deffb58
commit b732493
Showing
6 changed files
with
146 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Parser | ||
|
||
The rebo parser parsers a string and produces an Abstract Syntax Tree (AST). The AST that is produced is used by all tools in the rebo ecosystem. | ||
|
||
```rebo-repl | ||
> rebo.lang.parse("1 + 2 * 3", { position: true }) | ||
{ kind: "exprs" | ||
, value: | ||
[ { kind: "binaryOp" | ||
, op: "+" | ||
, lhs: { kind: "literalInt", value: 1, position: [ 0, 1 ] } | ||
, rhs: | ||
{ kind: "binaryOp" | ||
, op: "*" | ||
, lhs: { kind: "literalInt", value: 2, position: [ 4, 5 ] } | ||
, rhs: { kind: "literalInt", value: 3, position: [ 8, 9 ] } | ||
, position: [ 4, 9 ] | ||
} | ||
, position: [ 0, 9 ] | ||
} | ||
] | ||
, position: [ 0, 9 ] | ||
} | ||
``` | ||
|
||
The littering of `position` fields in the AST is optional and, going through the different scenarios, it is best to not represent them as it creates loads of clutter. | ||
|
||
```rebo-repl | ||
> rebo.lang.parse("1 + 2 * 3") | ||
{ kind: "exprs" | ||
, value: | ||
[ { kind: "binaryOp" | ||
, op: "+" | ||
, lhs: { kind: "literalInt", value: 1 } | ||
, rhs: | ||
{ kind: "binaryOp" | ||
, op: "*" | ||
, lhs: { kind: "literalInt", value: 2 } | ||
, rhs: { kind: "literalInt", value: 3 } | ||
} | ||
} | ||
] | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
const std = @import("std"); | ||
|
||
const AST = @import("./../ast.zig"); | ||
const Helper = @import("./helper.zig"); | ||
const Parser = @import("./../ast-interpreter.zig"); | ||
|
||
pub fn parse(machine: *Helper.Runtime, numberOfArgs: usize) !void { | ||
const v = try Helper.getArgument(machine, numberOfArgs, 0, &[_]Helper.ValueKind{Helper.ValueValue.StringKind}); | ||
const options = try Helper.getArgument(machine, numberOfArgs, 1, &[_]Helper.ValueKind{ Helper.ValueValue.RecordKind, Helper.ValueValue.UnitKind }); | ||
|
||
const ast = try Parser.parse(machine, "input", v.v.StringKind.slice()); | ||
defer ast.destroy(machine.allocator); | ||
|
||
const position = try Helper.booleanOption(machine.stringPool, options, "position", false); | ||
|
||
try emit(machine, ast, position); | ||
} | ||
const pos = Helper.Errors.Position{ .start = 0, .end = 0 }; | ||
|
||
fn emit(machine: *Helper.Runtime, ast: *AST.Expression, position: bool) !void { | ||
switch (ast.kind) { | ||
.binaryOp => { | ||
try machine.pushEmptyRecordValue(); | ||
|
||
try machine.pushStringValue("kind"); | ||
try machine.pushStringValue("binaryOp"); | ||
try machine.setRecordItemBang(pos); | ||
|
||
try machine.pushStringValue("op"); | ||
try machine.pushStringValue(ast.kind.binaryOp.op.toString()); | ||
try machine.setRecordItemBang(pos); | ||
|
||
try machine.pushStringValue("lhs"); | ||
try emit(machine, ast.kind.binaryOp.left, position); | ||
try machine.setRecordItemBang(pos); | ||
|
||
try machine.pushStringValue("rhs"); | ||
try emit(machine, ast.kind.binaryOp.right, position); | ||
try machine.setRecordItemBang(pos); | ||
}, | ||
.exprs => { | ||
try machine.pushEmptyRecordValue(); | ||
|
||
try machine.pushStringValue("kind"); | ||
try machine.pushStringValue("exprs"); | ||
try machine.setRecordItemBang(pos); | ||
|
||
try machine.pushStringValue("value"); | ||
try machine.pushEmptySequenceValue(); | ||
for (ast.kind.exprs) |expr| { | ||
try emit(machine, expr, position); | ||
try machine.appendSequenceItemBang(pos); | ||
} | ||
try machine.setRecordItemBang(pos); | ||
}, | ||
.literalInt => { | ||
try machine.pushEmptyRecordValue(); | ||
|
||
try machine.pushStringValue("kind"); | ||
try machine.pushStringValue("literalInt"); | ||
try machine.setRecordItemBang(pos); | ||
|
||
try machine.pushStringValue("value"); | ||
try machine.pushIntValue(ast.kind.literalInt); | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters