-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6a2ed3
commit 701ef47
Showing
9 changed files
with
389 additions
and
48 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
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 |
---|---|---|
@@ -1,7 +0,0 @@ | ||
|
||
import marshal | ||
|
||
filename = './demo/test.py' | ||
with open(filename, 'r') as f: | ||
bytes = marshal.load(f) | ||
|
||
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 |
---|---|---|
@@ -1,5 +1 @@ | ||
# Set | ||
a = {1, 2, 3, 4, 5} | ||
# a.add(6) | ||
|
||
print(a) | ||
a = 1 |
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,58 @@ | ||
//! Generates an AST given python source code. | ||
|
||
source: [:0]const u8, | ||
tokens: TokenList.Slice, | ||
nodes: NodeList.Slice, | ||
|
||
pub const NodeList = std.MultiArrayList(Parser.Node); | ||
pub const TokenList = std.MultiArrayList(Token); | ||
|
||
pub const TokenIndex = u32; | ||
|
||
pub fn parse(source: [:0]const u8, allocator: Allocator) !Ast { | ||
var tokens: std.MultiArrayList(Token) = .{}; | ||
defer tokens.deinit(allocator); | ||
|
||
var tokenizer = Tokenizer.init(source); | ||
while (true) { | ||
const token = tokenizer.next(); | ||
log.debug("Token: {}", .{token.tag}); | ||
try tokens.append(allocator, .{ | ||
.tag = token.tag, | ||
.start = @as(u32, @intCast(token.loc.start)), | ||
}); | ||
if (token.tag == .eof) break; | ||
} | ||
|
||
var parser = Parser{ | ||
.tokens = tokens, | ||
.token_index = 0, | ||
.allocator = allocator, | ||
.nodes = .{}, | ||
.source = source, | ||
}; | ||
defer parser.tokens.deinit(allocator); | ||
defer parser.nodes.deinit(allocator); | ||
|
||
try parser.parseFile(); | ||
|
||
return Ast{ | ||
.source = source, | ||
.tokens = tokens.toOwnedSlice(), | ||
.nodes = parser.nodes.toOwnedSlice(), | ||
}; | ||
} | ||
|
||
pub const Token = struct { | ||
tag: Tokenizer.Token.Tag, | ||
start: u32, | ||
}; | ||
|
||
const Parser = @import("Parser.zig"); | ||
const Tokenizer = @import("Tokenizer.zig"); | ||
|
||
const log = std.log.scoped(.ast); | ||
|
||
const Ast = @This(); | ||
const std = @import("std"); | ||
const Allocator = std.mem.Allocator; |
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,109 @@ | ||
//! Converts a list of Tokens into an AST | ||
|
||
tokens: Ast.TokenList, | ||
nodes: Ast.NodeList, | ||
allocator: Allocator, | ||
token_index: u32 = 0, | ||
|
||
source: [:0]const u8, | ||
|
||
/// file: [statements] ENDMARKER | ||
pub fn parseFile(p: *Parser) !void { | ||
if (p.tokens.get(p.token_index).tag == .eof) return; | ||
try p.parseStatements(); | ||
_ = p.eatToken(.eof) orelse return error.NotEof; | ||
} | ||
|
||
/// statements: statement+ | ||
fn parseStatements(p: *Parser) !void { | ||
while (p.tokens.get(p.token_index).tag != .eof) { | ||
try p.parseStatement(); | ||
} | ||
} | ||
|
||
/// statement: compound_stmt | simple_stmts | ||
fn parseStatement(p: *Parser) !void { | ||
// TODO: compound_stmt | ||
try p.parseSimpleStatment(); | ||
} | ||
|
||
fn parseSimpleStatment(p: *Parser) !void { | ||
const tag = p.tokens.get(p.token_index).tag; | ||
switch (tag) { | ||
.identifier => { | ||
const next_tag = p.tokens.get(p.token_index + 1).tag; | ||
if (next_tag == .eof) { | ||
@panic("simple statment found eof after ident"); | ||
} | ||
switch (next_tag) { | ||
.assign => try p.parseAssignExpr(), | ||
else => std.debug.panic("TODO: parseSimpleStatment identifier {}", .{next_tag}), | ||
} | ||
}, | ||
else => std.debug.panic("TODO: parseSimpleStatment {}", .{tag}), | ||
} | ||
} | ||
|
||
/// assignment: | ||
/// | NAME ':' expression ['=' annotated_rhs ] | ||
/// | ('(' single_target ')' | ||
/// | single_subscript_attribute_target) ':' expression ['=' annotated_rhs ] | ||
/// | (star_targets '=' )+ (yield_expr | star_expressions) !'=' [TYPE_COMMENT] | ||
/// | single_target augassign ~ (yield_expr | star_expressions) | ||
fn parseAssignExpr(p: *Parser) !void { | ||
const maybe_ident_tok = p.eatToken(.identifier); | ||
if (maybe_ident_tok) |ident_tok| { | ||
_ = ident_tok; | ||
return; | ||
} | ||
|
||
@panic("TODO: parseAssignExpr non-ident"); | ||
} | ||
|
||
fn eatToken(p: *Parser, tag: Tokenizer.Token.Tag) ?Token { | ||
const next_tok = p.nextToken(); | ||
if (next_tok.tag == tag) return next_tok; | ||
return null; | ||
} | ||
|
||
fn nextToken(p: *Parser) Token { | ||
const tok = p.tokens.get(p.token_index); | ||
p.token_index += 1; | ||
return tok; | ||
} | ||
|
||
fn addNode(p: *Parser, elem: Node) Allocator.Error!Node.Index { | ||
const result = @as(Node.Index, @intCast(p.nodes.len)); | ||
try p.nodes.append(p.gpa, elem); | ||
return result; | ||
} | ||
|
||
pub const Node = struct { | ||
tag: Tag, | ||
main_token: Ast.TokenIndex, | ||
data: Data, | ||
|
||
pub const Index = u32; | ||
|
||
pub const Tag = enum(u8) { | ||
root, | ||
/// An assignment. | ||
/// | ||
/// `lhs = rhs`. main_token is the `=`. | ||
assign, | ||
}; | ||
|
||
pub const Data = struct { | ||
lhs: Index, | ||
rhs: Index, | ||
}; | ||
}; | ||
|
||
const Parser = @This(); | ||
|
||
const std = @import("std"); | ||
const Ast = @import("Ast.zig"); | ||
const Tokenizer = @import("Tokenizer.zig"); | ||
const Token = Ast.Token; | ||
|
||
const Allocator = std.mem.Allocator; |
Oops, something went wrong.