Merge pull request #15906 from s-hadinger/berry_static_var

Berry prefer `static var` syntax
This commit is contained in:
s-hadinger 2022-07-01 20:13:00 +02:00 committed by GitHub
commit 3d4a5bb4b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 21 deletions

View File

@ -1446,26 +1446,32 @@ static void classdef_stmt(bparser *parser, bclass *c, bbool is_static)
static void classstatic_stmt(bparser *parser, bclass *c, bexpdesc *e)
{
bstring *name;
/* 'static' ID ['=' expr] {',' ID ['=' expr] } */
/* 'static' ['var'] ID ['=' expr] {',' ID ['=' expr] } */
/* 'static' 'def' ID '(' varlist ')' block 'end' */
scan_next_token(parser); /* skip 'static' */
if (next_type(parser) == KeyDef) { /* 'static' 'def' ... */
classdef_stmt(parser, c, btrue);
} else if (match_id(parser, name) != NULL) {
check_class_attr(parser, c, name);
be_class_member_bind(parser->vm, c, name, bfalse);
class_static_assignment_expr(parser, e, name);
while (match_skip(parser, OptComma)) { /* ',' */
if (match_id(parser, name) != NULL) {
check_class_attr(parser, c, name);
be_class_member_bind(parser->vm, c, name, bfalse);
class_static_assignment_expr(parser, e, name);
} else {
parser_error(parser, "class static error");
}
}
} else {
parser_error(parser, "class static error");
if (next_type(parser) == KeyVar) {
scan_next_token(parser); /* skip 'var' if any */
}
if (match_id(parser, name) != NULL) {
check_class_attr(parser, c, name);
be_class_member_bind(parser->vm, c, name, bfalse);
class_static_assignment_expr(parser, e, name);
while (match_skip(parser, OptComma)) { /* ',' */
if (match_id(parser, name) != NULL) {
check_class_attr(parser, c, name);
be_class_member_bind(parser->vm, c, name, bfalse);
class_static_assignment_expr(parser, e, name);
} else {
parser_error(parser, "class static error");
}
}
} else {
parser_error(parser, "class static error");
}
}
}

View File

@ -8,11 +8,11 @@ def assert_attribute_error(f)
end
class A
static a
static a #- deprecated syntax -#
def init() self.b = 2 end
def f() end
var b
static c, s, r
static var c, s, r #- preferred syntax -#
end
assert(A.a == nil)

View File

@ -17,7 +17,7 @@ func_body = '(' [arg_field {',' arg_field}] ')' block 'end';
arg_field = ['*'] ID;
(* class define statement *)
class_stmt = 'class' ID [':' ID] class_block 'end';
class_block = {'var' ID {',' ID} | 'static' ID ['=' expr] {',' ID ['=' expr] } | 'static' func_stmt | func_stmt};
class_block = {'var' ID {',' ID} | 'static' ['var'] ID ['=' expr] {',' ID ['=' expr] } | 'static' func_stmt | func_stmt};
import_stmt = 'import' (ID (['as' ID] | {',' ID}) | STRING 'as' ID);
(* exceptional handling statement *)
try_stmt = 'try' block except_block {except_block} 'end';
@ -28,12 +28,13 @@ throw_stmt = 'raise' expr [',' expr];
var_stmt = 'var' ID ['=' expr] {',' ID ['=' expr]};
(* expression define *)
expr_stmt = expr [assign_op expr];
expr = suffix_expr | unop expr | expr binop expr | cond_expr;
expr = suffix_expr | unop expr | expr binop expr | range_expr | cond_expr;
cond_expr = expr '?' expr ':' expr; (* conditional expression *)
assign_op = '=' | '+=' | '-=' | '*=' | '/=' |
'%=' | '&=' | '|=' | '^=' | '<<=' | '>>=';
binop = '..' | '<' | '<=' | '==' | '!=' | '>' | '>=' | '||' | '&&' |
binop = '<' | '<=' | '==' | '!=' | '>' | '>=' | '||' | '&&' |
'<<' | '>>' | '&' | '|' | '^' | '+' | '-' | '*' | '/' | '%';
range_expr = expr '..' [expr]
unop = '-' | '!' | '~';
suffix_expr = primary_expr {call_expr | ('.' ID) | '[' expr ']'};
primary_expr = '(' expr ')' | simple_expr | list_expr | map_expr | anon_func | lambda_expr;