fmt: struct pub/mut fields, map init

pull/3810/head
Alexander Medvednikov 2020-02-22 14:13:19 +01:00
parent b250ded3fa
commit cd41967aa1
6 changed files with 128 additions and 17 deletions

View File

@ -11,7 +11,7 @@ import (
pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral |
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr |
AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr |
CastExpr | EnumVal | Assoc | SizeOf | None
CastExpr | EnumVal | Assoc | SizeOf | None | MapInit
pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt |
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
@ -97,6 +97,9 @@ pub:
name string
fields []Field
is_pub bool
mut_pos int // mut:
pub_pos int // pub:
pub_mut_pos int // pub mut:
}
pub struct StructInit {
@ -440,6 +443,15 @@ mut:
typ table.Type
}
pub struct MapInit {
pub:
pos token.Position
keys []Expr
vals []Expr
mut:
typ table.Type
}
// s[10..20]
pub struct RangeExpr {
pub:
@ -456,6 +468,8 @@ pub:
pub struct Assoc {
pub:
name string
fields []string
exprs []Expr
}
pub struct SizeOf {

View File

@ -12,7 +12,7 @@ import (
)
const (
max_nr_errors = 10
max_nr_errors = 30
)
pub struct Checker {

View File

@ -198,7 +198,16 @@ fn (f mut Fmt) struct_decl(node ast.StructDecl) {
max = field.name.len
}
}
for field in node.fields {
for i, field in node.fields {
if i == node.mut_pos {
f.writeln('mut:')
}
else if i == node.pub_pos {
f.writeln('pub:')
}
else if i == node.pub_mut_pos {
f.writeln('pub mut:')
}
f.write('\t$field.name ')
f.write(strings.repeat(` `, max - field.name.len))
f.writeln(f.type_to_str(field.typ))
@ -298,6 +307,28 @@ fn (f mut Fmt) expr(node ast.Expr) {
ast.IntegerLiteral {
f.write(it.val.str())
}
ast.MapInit {
f.writeln('{')
f.indent++
/*
mut max := 0
for i, key in it.keys {
if key.len > max {
max = key.len
}
}
*/
for i, key in it.keys {
f.expr(key)
// f.write(strings.repeat(` `, max - field.name.len))
f.write(': ')
f.expr(it.vals[i])
f.writeln('')
}
f.indent--
f.write('}')
}
ast.MethodCallExpr {
f.expr(it.expr)
f.write('.' + it.name + '(')

View File

@ -78,3 +78,25 @@ fn get_user_ptr() &User {
return &User{
}
}
struct Foo {
field1 int
field2 string
pub:
public_field1 int
public_field2 f64
mut:
mut_field string
pub mut:
pub_mut_field string
}
const (
reserved_types = {
'i8': true
'i16': true
'int': true
'i64': true
'i128': true
}
)

View File

@ -81,3 +81,26 @@ fn get_user() ? User {
fn get_user_ptr() & User {
return &User{}
}
struct Foo {
field1 int
field2 string
pub:
public_field1 int
public_field2 f64
mut:
mut_field string
pub mut:
pub_mut_field string
}
const (
reserved_types = {
'i8': true
'i16': true
'int': true
'i64': true
'i128': true
}
)

View File

@ -663,14 +663,24 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
.lcbr {
p.next()
if p.tok.kind == .str {
mut keys := []ast.Expr
mut vals := []ast.Expr
for p.tok.kind != .rcbr && p.tok.kind != .eof {
p.check(.str)
//p.check(.str)
key, _ := p.expr(0)
keys << key
p.check(.colon)
p.expr(0)
val,_ := p.expr(0)
vals << val
if p.tok.kind == .comma {
p.next()
}
}
node = ast.MapInit {
keys:keys
vals: vals
pos: p.tok.position()
}
}
else {
name := p.check_name()
@ -1313,17 +1323,25 @@ fn (p mut Parser) struct_decl() ast.StructDecl {
p.check(.lcbr)
mut ast_fields := []ast.Field
mut fields := []table.Field
mut mut_pos := -1
mut pub_pos := -1
mut pub_mut_pos := -1
for p.tok.kind != .rcbr {
if p.tok.kind == .key_pub {
p.check(.key_pub)
if p.tok.kind == .key_mut {
p.check(.key_mut)
pub_mut_pos = fields.len
}
else {
pub_pos = fields.len
}
p.check(.colon)
}
else if p.tok.kind == .key_mut {
p.check(.key_mut)
p.check(.colon)
mut_pos = fields.len
}
field_name := p.check_name()
// p.warn('field $field_name')
@ -1368,6 +1386,9 @@ fn (p mut Parser) struct_decl() ast.StructDecl {
is_pub: is_pub
fields: ast_fields
pos: p.tok.position()
mut_pos: mut_pos
pub_pos: pub_pos
pub_mut_pos: pub_mut_pos
}
}