v2: static (for translated code only)

pull/4176/head
Alexander Medvednikov 2020-04-01 13:38:05 +02:00
parent 48832200bb
commit b1b811b5ed
4 changed files with 19 additions and 8 deletions

View File

@ -450,6 +450,7 @@ pub:
mut: mut:
left_types []table.Type left_types []table.Type
right_types []table.Type right_types []table.Type
is_static bool // for translated code only
} }
pub struct AsCast { pub struct AsCast {

View File

@ -578,6 +578,9 @@ fn (g mut Gen) gen_assert_stmt(a ast.AssertStmt) {
fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
// g.write('/*assign_stmt*/') // g.write('/*assign_stmt*/')
if assign_stmt.is_static {
g.write('static ')
}
if assign_stmt.left.len > assign_stmt.right.len { if assign_stmt.left.len > assign_stmt.right.len {
// multi return // multi return
mut or_stmts := []ast.Stmt mut or_stmts := []ast.Stmt

View File

@ -52,6 +52,7 @@ pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt {
pref: &pref.Preferences{} pref: &pref.Preferences{}
scope: scope scope: scope
// scope: &ast.Scope{start_pos: 0, parent: 0} // scope: &ast.Scope{start_pos: 0, parent: 0}
} }
p.init_parse_fns() p.init_parse_fns()
p.read_first_token() p.read_first_token()
@ -269,7 +270,7 @@ pub fn (p mut Parser) top_stmt() ast.Stmt {
p.error('wrong pub keyword usage') p.error('wrong pub keyword usage')
return ast.Stmt{} return ast.Stmt{}
} }
} }
} }
.lsbr { .lsbr {
return p.attribute() return p.attribute()
@ -354,7 +355,7 @@ pub fn (p mut Parser) stmt() ast.Stmt {
pos: p.tok.position() pos: p.tok.position()
} }
} }
.key_mut { .key_mut, .key_static {
return p.assign_stmt() return p.assign_stmt()
} }
.key_for { .key_for {
@ -780,6 +781,7 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr {
node = ast.SizeOf{ node = ast.SizeOf{
typ: sizeof_type typ: sizeof_type
// type_name: type_name // type_name: type_name
} }
} }
.key_typeof { .key_typeof {
@ -1050,6 +1052,7 @@ fn (p mut Parser) infix_expr(left ast.Expr) ast.Expr {
left: left left: left
right: right right: right
// right_type: typ // right_type: typ
op: op op: op
pos: pos pos: pos
} }
@ -1454,6 +1457,7 @@ fn (p mut Parser) const_decl() ast.ConstDecl {
fields << ast.Field{ fields << ast.Field{
name: name name: name
// typ: typ // typ: typ
} }
exprs << expr exprs << expr
// TODO: once consts are fixed reg here & update in checker // TODO: once consts are fixed reg here & update in checker
@ -1676,6 +1680,10 @@ fn (p mut Parser) parse_assign_rhs() []ast.Expr {
} }
fn (p mut Parser) assign_stmt() ast.Stmt { fn (p mut Parser) assign_stmt() ast.Stmt {
is_static := p.tok.kind == .key_static
if is_static {
p.next()
}
idents := p.parse_assign_lhs() idents := p.parse_assign_lhs()
pos := p.tok.position() pos := p.tok.position()
op := p.tok.kind op := p.tok.kind
@ -1709,6 +1717,7 @@ fn (p mut Parser) assign_stmt() ast.Stmt {
right: exprs right: exprs
op: op op: op
pos: pos pos: pos
is_static: is_static
} }
} }
@ -1849,7 +1858,7 @@ fn (p mut Parser) enum_decl() ast.EnumDecl {
} }
// Allow commas after enum, helpful for // Allow commas after enum, helpful for
// enum Color { // enum Color {
// r,g,b // r,g,b
// } // }
if p.tok.kind == .comma { if p.tok.kind == .comma {
p.next() p.next()

View File

@ -1,5 +1,3 @@
fn C.strlen() int
fn test_cstring() { fn test_cstring() {
w := c'world' w := c'world'
hlen := C.strlen(c'hello') hlen := C.strlen(c'hello')