Update static functions, test and grammar

This commit is contained in:
Stephan Hadinger 2021-11-25 20:09:04 +01:00
parent 8faa144d04
commit 498aa9b2b3
2 changed files with 29 additions and 3 deletions

View File

@ -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)

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] } | 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';