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:
left_types []table.Type
right_types []table.Type
is_static bool // for translated code only
}
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) {
// g.write('/*assign_stmt*/')
if assign_stmt.is_static {
g.write('static ')
}
if assign_stmt.left.len > assign_stmt.right.len {
// multi return
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{}
scope: scope
// scope: &ast.Scope{start_pos: 0, parent: 0}
}
p.init_parse_fns()
p.read_first_token()
@ -70,13 +71,13 @@ pub fn parse_file(path string, table &table.Table, comments_mode scanner.Comment
table: table
file_name: path
pref: pref // &pref.Preferences{}
scope: &ast.Scope{
start_pos: 0
parent: 0
}
// comments_mode: comments_mode
}
p.read_first_token()
// p.scope = &ast.Scope{start_pos: p.tok.position(), parent: 0}
@ -269,7 +270,7 @@ pub fn (p mut Parser) top_stmt() ast.Stmt {
p.error('wrong pub keyword usage')
return ast.Stmt{}
}
}
}
}
.lsbr {
return p.attribute()
@ -354,7 +355,7 @@ pub fn (p mut Parser) stmt() ast.Stmt {
pos: p.tok.position()
}
}
.key_mut {
.key_mut, .key_static {
return p.assign_stmt()
}
.key_for {
@ -690,7 +691,7 @@ pub fn (p mut Parser) name_expr() ast.Expr {
p.expr_mod = ''
return ast.EnumVal{
enum_name: enum_name // lp.prepend_mod(enum_name)
val: val
pos: p.tok.position()
mod: mod
@ -780,6 +781,7 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr {
node = ast.SizeOf{
typ: sizeof_type
// type_name: type_name
}
}
.key_typeof {
@ -1050,6 +1052,7 @@ fn (p mut Parser) infix_expr(left ast.Expr) ast.Expr {
left: left
right: right
// right_type: typ
op: op
pos: pos
}
@ -1454,6 +1457,7 @@ fn (p mut Parser) const_decl() ast.ConstDecl {
fields << ast.Field{
name: name
// typ: typ
}
exprs << expr
// 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 {
is_static := p.tok.kind == .key_static
if is_static {
p.next()
}
idents := p.parse_assign_lhs()
pos := p.tok.position()
op := p.tok.kind
@ -1709,6 +1717,7 @@ fn (p mut Parser) assign_stmt() ast.Stmt {
right: exprs
op: op
pos: pos
is_static: is_static
}
}
@ -1849,7 +1858,7 @@ fn (p mut Parser) enum_decl() ast.EnumDecl {
}
// Allow commas after enum, helpful for
// enum Color {
// r,g,b
// r,g,b
// }
if p.tok.kind == .comma {
p.next()

View File

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