From aaa87365f07a27d902e6b1f4a8bbdc1c5fe62481 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Fri, 1 Jul 2022 19:56:09 +0200 Subject: [PATCH] Berry prefer `static var` syntax --- lib/libesp32/berry/src/be_parser.c | 38 ++++++++++++--------- lib/libesp32/berry/tests/class_static.be | 4 +-- lib/libesp32/berry/tools/grammar/berry.ebnf | 7 ++-- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/lib/libesp32/berry/src/be_parser.c b/lib/libesp32/berry/src/be_parser.c index d0c35bb52..3f2fc20d0 100644 --- a/lib/libesp32/berry/src/be_parser.c +++ b/lib/libesp32/berry/src/be_parser.c @@ -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"); + } } } diff --git a/lib/libesp32/berry/tests/class_static.be b/lib/libesp32/berry/tests/class_static.be index e8f4c1920..c86a4a4b3 100644 --- a/lib/libesp32/berry/tests/class_static.be +++ b/lib/libesp32/berry/tests/class_static.be @@ -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) diff --git a/lib/libesp32/berry/tools/grammar/berry.ebnf b/lib/libesp32/berry/tools/grammar/berry.ebnf index 1b8889f80..1b387ae0d 100644 --- a/lib/libesp32/berry/tools/grammar/berry.ebnf +++ b/lib/libesp32/berry/tools/grammar/berry.ebnf @@ -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;