From b1b811b5edfb351415d466eafe39ca73584a5b1b Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 1 Apr 2020 13:38:05 +0200 Subject: [PATCH] v2: static (for translated code only) --- vlib/v/ast/ast.v | 1 + vlib/v/gen/cgen.v | 3 +++ vlib/v/parser/parser.v | 21 +++++++++++++++------ vlib/v/tests/cstrings_test.v | 2 -- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index c03b598f02..aace55d48f 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -450,6 +450,7 @@ pub: mut: left_types []table.Type right_types []table.Type + is_static bool // for translated code only } pub struct AsCast { diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 623dffc814..eb3b156df0 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -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 diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index b08593f3f2..f11e3c5a31 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -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() diff --git a/vlib/v/tests/cstrings_test.v b/vlib/v/tests/cstrings_test.v index bf101ac5d3..e942cc41d0 100644 --- a/vlib/v/tests/cstrings_test.v +++ b/vlib/v/tests/cstrings_test.v @@ -1,5 +1,3 @@ -fn C.strlen() int - fn test_cstring() { w := c'world' hlen := C.strlen(c'hello')