Berry prefer static var syntax

This commit is contained in:
Stephan Hadinger 2022-07-01 19:56:09 +02:00
parent a7d2c69947
commit aaa87365f0
3 changed files with 28 additions and 21 deletions

View File

@ -1446,11 +1446,16 @@ static void classdef_stmt(bparser *parser, bclass *c, bbool is_static)
static void classstatic_stmt(bparser *parser, bclass *c, bexpdesc *e) static void classstatic_stmt(bparser *parser, bclass *c, bexpdesc *e)
{ {
bstring *name; 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' */ scan_next_token(parser); /* skip 'static' */
if (next_type(parser) == KeyDef) { /* 'static' 'def' ... */ if (next_type(parser) == KeyDef) { /* 'static' 'def' ... */
classdef_stmt(parser, c, btrue); classdef_stmt(parser, c, btrue);
} else if (match_id(parser, name) != NULL) { } else {
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); check_class_attr(parser, c, name);
be_class_member_bind(parser->vm, c, name, bfalse); be_class_member_bind(parser->vm, c, name, bfalse);
class_static_assignment_expr(parser, e, name); class_static_assignment_expr(parser, e, name);
@ -1467,6 +1472,7 @@ static void classstatic_stmt(bparser *parser, bclass *c, bexpdesc *e)
} else { } else {
parser_error(parser, "class static error"); parser_error(parser, "class static error");
} }
}
} }
static void class_inherit(bparser *parser, bexpdesc *e) static void class_inherit(bparser *parser, bexpdesc *e)

View File

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

View File

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