add pub/mut modifiers to struct fields

This commit is contained in:
Luna 2019-08-26 21:31:01 -03:00
parent b436ad831a
commit a706f077cb
2 changed files with 34 additions and 5 deletions

View file

@ -227,8 +227,10 @@ pub const FieldList = std.ArrayList(StructField);
pub const StructField = struct {
name: Token,
typ: Token,
mutable: bool = true,
public: bool = true,
mutable: bool = false,
public: bool = false,
mutable_outside: bool = false,
};
pub const Struct = struct {

View file

@ -18,6 +18,12 @@ const Stmt = ast.Stmt;
const TokenList = std.ArrayList(Token);
const FieldState = struct {
public: bool = false,
mutable: bool = false,
mutable_outside: bool = false,
};
pub const Parser = struct {
allocator: *Allocator,
scanner: *Scanner,
@ -405,8 +411,6 @@ pub const Parser = struct {
while (self.peek().ttype != .RightParen) {
const param_name = try self.consumeSingle(.Identifier);
// TODO dedicated function to consume a type?
const param_type = try self.consumeSingle(.Identifier);
try param_list.append(ast.ParamDecl{
@ -495,14 +499,37 @@ pub const Parser = struct {
_ = try self.consumeSingle(.LeftBrace);
var field_state = FieldState{};
while (!self.check(.RightBrace)) {
// TODO mut and pub
if (self.check(.Mut)) {
_ = try self.consumeSingle(.Mut);
_ = try self.consumeSingle(.Colon);
if (field_state.mutable) {
field_state.mutable_outside = true;
} else {
field_state.mutable = true;
}
} else if (self.check(.Pub)) {
_ = try self.consumeSingle(.Pub);
_ = try self.consumeSingle(.Colon);
field_state.public = true;
}
const field_name = try self.consumeSingle(.Identifier);
const field_type = try self.consumeSingle(.Identifier);
// we could create a FieldState on the heap and copy our current
// field state into a StructField.state, but copying via this makes
// things so much nicer.
try fields.append(ast.StructField{
.name = field_name,
.typ = field_type,
.mutable = field_state.mutable,
.public = field_state.public,
.mutable_outside = field_state.mutable_outside,
});
}