Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static initializer for class members #113

Merged
merged 4 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/be_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1235,18 +1235,38 @@ static void classvar_stmt(bparser *parser, bclass *c)
}
}

static void classstatic_stmt(bparser *parser, bclass *c)
static void class_static_assignment_expr(bparser *parser, bexpdesc *e, bstring *name)
{
if (match_skip(parser, OptAssign)) { /* '=' */
bexpdesc e1, e2;
/* parse the right expression */
expr(parser, &e2);

e1 = *e; /* copy the class description */
bexpdesc key; /* build the member key */
init_exp(&key, ETSTRING, 0);
key.v.s = name;

be_code_member(parser->finfo, &e1, &key); /* compute member accessor */
be_code_setvar(parser->finfo, &e1, &e2); /* set member */
}
}

static void classstatic_stmt(bparser *parser, bclass *c, bexpdesc *e)
{
bstring *name;
/* 'static' ID {',' ID} */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, done in #115

scan_next_token(parser); /* skip 'static' */
if (match_id(parser, name) != NULL) {
check_class_attr(parser, c, name);
be_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_member_bind(parser->vm, c, name, bfalse);
class_static_assignment_expr(parser, e, name);
} else {
parser_error(parser, "class static error");
}
Expand Down Expand Up @@ -1281,13 +1301,13 @@ static void class_inherit(bparser *parser, bexpdesc *e)
}
}

static void class_block(bparser *parser, bclass *c)
static void class_block(bparser *parser, bclass *c, bexpdesc *e)
{
/* { [;] } */
while (block_follow(parser)) {
switch (next_type(parser)) {
case KeyVar: classvar_stmt(parser, c); break;
case KeyStatic: classstatic_stmt(parser, c); break;
case KeyStatic: classstatic_stmt(parser, c, e); break;
case KeyDef: classdef_stmt(parser, c); break;
case OptSemic: scan_next_token(parser); break;
default: push_error(parser,
Expand All @@ -1307,7 +1327,7 @@ static void class_stmt(bparser *parser)
new_var(parser, name, &e);
be_code_class(parser->finfo, &e, c);
class_inherit(parser, &e);
class_block(parser, c);
class_block(parser, c, &e);
be_class_compress(parser->vm, c); /* compress class size */
match_token(parser, KeyEnd); /* skip 'end' */
} else {
Expand Down
24 changes: 24 additions & 0 deletions tests/class_const.be
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,27 @@ assert(type(a.h) == 'function')
assert_attribute_error("a.g(1,2)")
assert(a.h(1) == 'instance')
# A.h(1) - error

#- test static initializers -#
class A
static a = 1, b, c = 3.5, d = 42, e = "foo", f = [1], g = {}
var aa,ab
end

assert(A.a == 1)
assert(A.b == nil)
assert(A.c == 3.5)
assert(A.d == 42)
assert(A.e == "foo")
assert(A.f == [1])

a = A()
assert(a.a == 1)
assert(a.b == nil)
assert(a.c == 3.5)
assert(a.d == 42)
assert(a.e == "foo")
assert(a.f == [1])
assert(a.g == A.g)
assert(a.aa == nil)
assert(a.ab == nil)