From 498aa9b2b3a7b958e69b6748d0f187f628c9f9d2 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Thu, 25 Nov 2021 20:09:04 +0100 Subject: [PATCH] Update static functions, test and grammar --- lib/libesp32/Berry/tests/class_const.be | 30 +++++++++++++++++++-- lib/libesp32/Berry/tools/grammar/berry.ebnf | 2 +- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/libesp32/Berry/tests/class_const.be b/lib/libesp32/Berry/tests/class_const.be index dda433174..7c986bff0 100644 --- a/lib/libesp32/Berry/tests/class_const.be +++ b/lib/libesp32/Berry/tests/class_const.be @@ -65,8 +65,26 @@ assert(type(a.g) == 'function') assert(type(a.h) == 'function') assert_attribute_error("a.g(1,2)") -assert(a.h(1) == 'instance') -# A.h(1) - error +assert(a.h(1) == 'int') +assert(A.h(1) == 'int') + + +class A + var a + static def g(x, y) return [x,y] end + static h = def (x, y) return [x,y] end + def init() self.a = 1 end + def f(x, y) return type(self) end +end +a=A() +assert(type(a.g) == 'function') +assert(type(a.h) == 'function') +assert(type(A.g) == 'function') +assert(type(A.h) == 'function') +assert(a.g(1,2) == [1,2]) +assert(a.h(1,2) == [1,2]) +assert(A.g(1,2) == [1,2]) +assert(A.h(1,2) == [1,2]) #- test static initializers -# class A @@ -91,3 +109,11 @@ assert(a.f == [1]) assert(a.g == A.g) assert(a.aa == nil) assert(a.ab == nil) + +#- used to fail for subclasses -# +class A static a=1 end +class B:A static a=A def f() end static b=1 static c=A end +assert(A.a == 1) +assert(B.a == A) +assert(B.b == 1) +assert(B.c == A) diff --git a/lib/libesp32/Berry/tools/grammar/berry.ebnf b/lib/libesp32/Berry/tools/grammar/berry.ebnf index 1ca79a763..1b8889f80 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] } | func_stmt}; +class_block = {'var' ID {',' ID} | 'static' 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';