self-hosted: implement var decl align

master
Josh Wolfe 2017-12-12 19:50:43 -07:00
parent 0003cc8105
commit d295279b16
1 changed files with 40 additions and 135 deletions

View File

@ -68,22 +68,7 @@ pub const Parser = struct {
TopLevelExtern: ?Token, TopLevelExtern: ?Token,
TopLevelDecl: TopLevelDeclCtx, TopLevelDecl: TopLevelDeclCtx,
Expression: DestPtr, Expression: DestPtr,
GroupedExpression: DestPtr,
UnwrapExpression: DestPtr,
BoolOrExpression: DestPtr,
BoolAndExpression: DestPtr,
ComparisonExpression: DestPtr,
BinaryOrExpression: DestPtr,
BinaryXorExpression: DestPtr,
BinaryAndExpression: DestPtr,
BitShiftExpression: DestPtr,
AdditionExpression: DestPtr,
MultiplyExpression: DestPtr,
BraceSuffixExpression: DestPtr,
PrefixOpExpression: DestPtr,
AddrOfModifiers: &ast.NodeAddrOfExpr, AddrOfModifiers: &ast.NodeAddrOfExpr,
SuffixOpExpression: DestPtr,
PrimaryExpression: DestPtr,
TypeExpr: DestPtr, TypeExpr: DestPtr,
VarDecl: &ast.NodeVarDecl, VarDecl: &ast.NodeVarDecl,
VarDeclAlign: &ast.NodeVarDecl, VarDeclAlign: &ast.NodeVarDecl,
@ -252,11 +237,9 @@ pub const Parser = struct {
const next_token = self.getNextToken(); const next_token = self.getNextToken();
if (next_token.id == Token.Id.Keyword_align) { if (next_token.id == Token.Id.Keyword_align) {
%return stack.append(State { _ = %return self.eatToken(Token.Id.LParen);
.GroupedExpression = DestPtr { %return stack.append(State { .ExpectToken = Token.Id.RParen });
.NullableField = &var_decl.align_node %return stack.append(State { .Expression = DestPtr{.NullableField = &var_decl.align_node} });
}
});
continue; continue;
} }
@ -284,84 +267,30 @@ pub const Parser = struct {
}, },
State.Expression => |dest_ptr| { State.Expression => |dest_ptr| {
const token = self.getNextToken(); const token = self.getNextToken();
if (token.id == Token.Id.Keyword_return) { switch (token.id) {
Token.Id.Keyword_return => {
const return_node = %return self.createAttachReturn(dest_ptr, token); const return_node = %return self.createAttachReturn(dest_ptr, token);
stack.append(State {.UnwrapExpression = DestPtr {.Field = &return_node.expr} }) %% unreachable; stack.append(State {.Expression = DestPtr {.Field = &return_node.expr} }) %% unreachable;
continue;
}
self.putBackToken(token);
stack.append(State {.UnwrapExpression = dest_ptr}) %% unreachable;
continue; continue;
}, },
Token.Id.Identifier => {
State.UnwrapExpression => |dest_ptr| { _ = %return self.createAttachIdentifier(dest_ptr, token);
stack.append(State {.BoolOrExpression = dest_ptr}) %% unreachable;
continue; continue;
}, },
Token.Id.IntegerLiteral => {
State.BoolOrExpression => |dest_ptr| { _ = %return self.createAttachIntegerLiteral(dest_ptr, token);
stack.append(State {.BoolAndExpression = dest_ptr}) %% unreachable;
continue; continue;
}, },
Token.Id.FloatLiteral => {
State.BoolAndExpression => |dest_ptr| { _ = %return self.createAttachFloatLiteral(dest_ptr, token);
stack.append(State {.ComparisonExpression = dest_ptr}) %% unreachable;
continue; continue;
}, },
State.ComparisonExpression => |dest_ptr| {
stack.append(State {.BinaryOrExpression = dest_ptr}) %% unreachable;
continue;
},
State.BinaryOrExpression => |dest_ptr| {
stack.append(State {.BinaryXorExpression = dest_ptr}) %% unreachable;
continue;
},
State.BinaryXorExpression => |dest_ptr| {
stack.append(State {.BinaryAndExpression = dest_ptr}) %% unreachable;
continue;
},
State.BinaryAndExpression => |dest_ptr| {
stack.append(State {.BitShiftExpression = dest_ptr}) %% unreachable;
continue;
},
State.BitShiftExpression => |dest_ptr| {
stack.append(State {.AdditionExpression = dest_ptr}) %% unreachable;
continue;
},
State.AdditionExpression => |dest_ptr| {
stack.append(State {.MultiplyExpression = dest_ptr}) %% unreachable;
continue;
},
State.MultiplyExpression => |dest_ptr| {
stack.append(State {.BraceSuffixExpression = dest_ptr}) %% unreachable;
continue;
},
State.BraceSuffixExpression => |dest_ptr| {
stack.append(State {.PrefixOpExpression = dest_ptr}) %% unreachable;
continue;
},
State.PrefixOpExpression => |dest_ptr| {
const first_token = self.getNextToken();
switch (first_token.id) {
Token.Id.Ampersand => { Token.Id.Ampersand => {
const addr_of_expr = %return self.createAttachAddrOfExpr(dest_ptr, first_token); const addr_of_expr = %return self.createAttachAddrOfExpr(dest_ptr, token);
stack.append(State { .AddrOfModifiers = addr_of_expr }) %% unreachable; stack.append(State { .AddrOfModifiers = addr_of_expr }) %% unreachable;
continue; continue;
}, },
else => { else => return self.parseError(token, "expected primary expression, found {}", @tagName(token.id)),
self.putBackToken(first_token);
stack.append(State { .SuffixOpExpression = dest_ptr }) %% unreachable;
continue;
},
} }
}, },
@ -391,37 +320,13 @@ pub const Parser = struct {
else => { else => {
self.putBackToken(token); self.putBackToken(token);
stack.append(State { stack.append(State {
.PrefixOpExpression = DestPtr { .Field = &addr_of_expr.op_expr}, .Expression = DestPtr { .Field = &addr_of_expr.op_expr},
}) %% unreachable; }) %% unreachable;
continue; continue;
}, },
} }
}, },
State.SuffixOpExpression => |dest_ptr| {
stack.append(State { .PrimaryExpression = dest_ptr }) %% unreachable;
continue;
},
State.PrimaryExpression => |dest_ptr| {
const token = self.getNextToken();
switch (token.id) {
Token.Id.Identifier => {
_ = %return self.createAttachIdentifier(dest_ptr, token);
continue;
},
Token.Id.IntegerLiteral => {
_ = %return self.createAttachIntegerLiteral(dest_ptr, token);
continue;
},
Token.Id.FloatLiteral => {
_ = %return self.createAttachFloatLiteral(dest_ptr, token);
continue;
},
else => return self.parseError(token, "expected primary expression, found {}", @tagName(token.id)),
}
},
State.TypeExpr => |dest_ptr| { State.TypeExpr => |dest_ptr| {
const token = self.getNextToken(); const token = self.getNextToken();
if (token.id == Token.Id.Keyword_var) { if (token.id == Token.Id.Keyword_var) {
@ -429,7 +334,7 @@ pub const Parser = struct {
} }
self.putBackToken(token); self.putBackToken(token);
stack.append(State { .PrefixOpExpression = dest_ptr }) %% unreachable; stack.append(State { .Expression = dest_ptr }) %% unreachable;
continue; continue;
}, },
@ -577,8 +482,6 @@ pub const Parser = struct {
%return stack.append(State { .Expression = DestPtr{.List = &block.statements} }); %return stack.append(State { .Expression = DestPtr{.List = &block.statements} });
continue; continue;
}, },
State.GroupedExpression => @panic("TODO"),
} }
unreachable; unreachable;
} }
@ -882,7 +785,6 @@ pub const Parser = struct {
Expression: &ast.Node, Expression: &ast.Node,
AddrOfExprBit: &ast.NodeAddrOfExpr, AddrOfExprBit: &ast.NodeAddrOfExpr,
VarDecl: &ast.NodeVarDecl, VarDecl: &ast.NodeVarDecl,
VarDeclAlign: &ast.NodeVarDecl,
Statement: &ast.Node, Statement: &ast.Node,
PrintIndent, PrintIndent,
Indent: usize, Indent: usize,
@ -969,24 +871,22 @@ pub const Parser = struct {
%return stream.print("{} ", self.tokenizer.getTokenSlice(var_decl.mut_token)); %return stream.print("{} ", self.tokenizer.getTokenSlice(var_decl.mut_token));
%return stream.print("{}", self.tokenizer.getTokenSlice(var_decl.name_token)); %return stream.print("{}", self.tokenizer.getTokenSlice(var_decl.name_token));
%return stack.append(RenderState { .VarDeclAlign = var_decl }); %return stack.append(RenderState { .Text = ";" });
if (var_decl.init_node) |init_node| {
%return stack.append(RenderState { .Expression = init_node });
%return stack.append(RenderState { .Text = " = " });
}
if (var_decl.align_node) |align_node| {
%return stack.append(RenderState { .Text = ")" });
%return stack.append(RenderState { .Expression = align_node });
%return stack.append(RenderState { .Text = " align(" });
}
if (var_decl.type_node) |type_node| { if (var_decl.type_node) |type_node| {
%return stream.print(": "); %return stream.print(": ");
%return stack.append(RenderState { .Expression = type_node }); %return stack.append(RenderState { .Expression = type_node });
} }
}, },
RenderState.VarDeclAlign => |var_decl| {
if (var_decl.align_node != null) {
@panic("TODO");
}
%return stack.append(RenderState { .Text = ";" });
if (var_decl.init_node) |init_node| {
%return stream.print(" = ");
%return stack.append(RenderState { .Expression = init_node });
}
},
RenderState.ParamDecl => |base| { RenderState.ParamDecl => |base| {
const param_decl = @fieldParentPtr(ast.NodeParamDecl, "base", base); const param_decl = @fieldParentPtr(ast.NodeParamDecl, "base", base);
if (param_decl.comptime_token) |comptime_token| { if (param_decl.comptime_token) |comptime_token| {
@ -1198,6 +1098,11 @@ test "zig fmt" {
\\ \\
); );
testCanonical(
\\var foo: c_int align(1);
\\
);
testCanonical( testCanonical(
\\fn main(argc: c_int, argv: &&u8) -> c_int { \\fn main(argc: c_int, argv: &&u8) -> c_int {
\\ const a = b; \\ const a = b;