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 |
@ -93,10 +93,13 @@ pub:
pub struct StructDecl {
pub:
pos token.Position
name string
fields []Field
is_pub bool
pos token.Position
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:
@ -455,7 +467,9 @@ pub:
pub struct Assoc {
pub:
name string
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

@ -56,7 +56,7 @@ pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt {
pref: &pref.Preferences{}
scope: scope
// scope: &ast.Scope{start_pos: 0, parent: 0}
}
p.init_parse_fns()
p.read_first_token()
@ -320,7 +320,7 @@ pub fn (p mut Parser) stmt() ast.Stmt {
return ast.ExprStmt{
expr: expr
// typ: typ
}
}
}
@ -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()
@ -1073,10 +1083,10 @@ fn (p mut Parser) if_expr() ast.Expr {
stmts: stmts
else_stmts: else_stmts
// typ: typ
pos: pos
// left: left
}
return node
}
@ -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
}
}
@ -1464,10 +1485,10 @@ fn (p mut Parser) var_decl() ast.VarDecl {
node := ast.VarDecl{
name: name
expr: expr // p.expr(token.lowest_prec)
is_mut: is_mut
// typ: typ
pos: p.tok.position()
}
p.scope.register_var(node)
@ -1586,7 +1607,7 @@ fn (p mut Parser) match_expr() ast.Expr {
blocks: blocks
match_exprs: match_exprs
// typ: typ
cond: cond
}
return node