%union tokenunion {
tokenptr_t token;
- class_signature_t*class_signature;
- class_signature_list_t*class_signature_list;
+ classinfo_t*classinfo;
+ classinfo_list_t*classinfo_list;
int number_int;
unsigned int number_uint;
%token<token> KW_IMPORT "import"
%token<token> KW_RETURN "return"
%token<token> KW_INTERFACE "interface"
-%token<token> KW_NULL
+%token<token> KW_NULL "null"
%token<token> KW_VAR "var"
%token<token> KW_DYNAMIC
%token<token> KW_OVERRIDE
%token<token> T_STAR '*'
%token<token> T_DOT '.'
+%type <token> X_IDENTIFIER VARCONST
%type <code> CODE
%type <code> CODEPIECE
%type <code> CODEBLOCK MAYBECODE
%type <token> USE_NAMESPACE
%type <code> ASSIGNMENT FOR_INIT
%type <token> IMPORT
-%type <class_signature> MAYBETYPE
+%type <classinfo> MAYBETYPE
%type <token> GETSET
%type <param> PARAM
%type <param_list> PARAM_LIST
%type <param_list> MAYBE_PARAM_LIST
%type <token> MODIFIERS
%type <token> MODIFIER_LIST
-%type <class_signature_list> IMPLEMENTS_LIST
-%type <class_signature> EXTENDS
-%type <class_signature_list> EXTENDS_LIST
-%type <class_signature> PACKAGEANDCLASS
-%type <class_signature_list> PACKAGEANDCLASS_LIST
-%type <token> MULTILEVELIDENTIFIER
-%type <class_signature> TYPE
+%type <classinfo_list> IMPLEMENTS_LIST
+%type <classinfo> EXTENDS
+%type <classinfo_list> EXTENDS_LIST
+%type <classinfo> CLASS PACKAGEANDCLASS QNAME
+%type <classinfo_list> QNAME_LIST
+%type <classinfo> TYPE
%type <token> VAR
//%type <token> VARIABLE
%type <value> VAR_READ
%type <value> NEW
-%type <token> X_IDENTIFIER
+//%type <token> T_IDENTIFIER
%type <token> MODIFIER
%type <token> PACKAGE
%type <value> FUNCTIONCALL
%left ';'
%nonassoc "else"
%left '('
+
+// needed for "return" precedence:
+%nonassoc T_STRING T_REGEXP
+%nonassoc T_INT T_UINT T_BYTE T_SHORT T_FLOAT
+%nonassoc "new" "false" "true" "null"
+
%left prec_highest
/* class data */
char*classname;
abc_class_t*cls;
+ code_t*cls_init;
+ code_t*cls_static_init;
array_t*vars;
int local_var_base;
}
char*globalclass=0;
-static void startclass(token_t*modifiers, token_t*name, class_signature_t*extends, class_signature_list_t*implements, char interface)
+static void startclass(token_t*modifiers, token_t*name, classinfo_t*extends, classinfo_list_t*implements, char interface)
{
if(state->cls) {
syntaxerror("inner classes now allowed");
state->classname = name->text;
token_list_t*t=0;
- class_signature_list_t*mlist=0;
+ classinfo_list_t*mlist=0;
/*printf("entering class %s\n", name->text);
printf(" modifiers: ");for(t=modifiers->tokens;t;t=t->next) printf("%s ", t->token->text);printf("\n");
if(extends)
printf(" extends: %s.%s\n", extends->package, extends->name);
printf(" implements (%d): ", list_length(implements));
for(mlist=implements;mlist;mlist=mlist->next) {
- printf("%s ", mlist->class_signature?mlist->class_signature->name:0);
+ printf("%s ", mlist->classinfo?mlist->classinfo->name:0);
}
printf("\n");
*/
syntaxerror("Package \"%s\" already contains a class called \"%s\"", package, state->classname);
}
- class_signature_t* classname = class_signature_register(access, package, state->classname);
+ classinfo_t* classname = classinfo_register(access, package, state->classname);
MULTINAME(classname2,classname);
if(interface) abc_class_interface(state->cls);
for(mlist=implements;mlist;mlist=mlist->next) {
- MULTINAME(m, mlist->class_signature);
+ MULTINAME(m, mlist->classinfo);
abc_class_add_interface(state->cls, &m);
}
abc_method_body_t*m = state->init->method->body;
__ getglobalscope(m);
- class_signature_t*s = extends;
+ classinfo_t*s = extends;
int count=0;
__ getlex2(m, s2);
multiname_destroy(s2);
- __ pushscope(m);
+ __ pushscope(m); count++;
m->code = m->code->prev->prev; // invert
- count++;
}
/* continue appending after last op end */
while(m->code && m->code->next) m->code = m->code->next;
if(extends2) {
__ getlex2(m, extends2);
__ dup(m);
- __ pushscope(m); // we get a Verify Error #1107 if this is not the top scope
+ /* notice: we get a Verify Error #1107 if the top elemnt on the scope
+ stack is not the superclass */
+ __ pushscope(m);count++;
} else {
__ pushnull(m);
+ /* notice: we get a verify error #1107 if the top element on the scope
+ stack is not the global object */
+ __ getlocal_0(m);
+ __ pushscope(m);count++;
}
__ newclass(m,state->cls);
while(count--) {
__ setslot(m, slotindex);
/* flash.display.MovieClip handling */
- if(!globalclass && public && class_signature_equals(registry_getMovieClip(),extends)) {
+ if(!globalclass && public && classinfo_equals(registry_getMovieClip(),extends)) {
if(state->package && state->package[0]) {
globalclass = concat3str(state->package, ".", state->classname);
} else {
static void endclass()
{
- /*printf("leaving class %s\n", state->classname);*/
+ if(state->cls_init) {
+ if(!state->cls->constructor) {
+ abc_method_body_t*m = abc_class_constructor(state->cls, 0, 0);
+ m->code = state->cls_init;
+ } else {
+ state->cls->constructor->body->code =
+ code_append(state->cls_init, state->cls->constructor->body->code);
+ }
+ }
+ if(state->cls_static_init) {
+ if(!state->cls->static_constructor) {
+ abc_method_body_t*m = abc_class_staticconstructor(state->cls, 0, 0);
+ m->code = state->cls_static_init;
+ } else {
+ state->cls->static_constructor->body->code =
+ code_append(state->cls_static_init, state->cls->static_constructor->body->code);
+ }
+ }
+
old_state();
}
static void startfunction(token_t*ns, token_t*mod, token_t*getset, token_t*name,
- param_list_t*params, class_signature_t*type)
+ param_list_t*params, classinfo_t*type)
{
token_list_t*t;
new_state();
}
multiname_t*type2 = sig2mname(type);
-
if(!strcmp(state->classname,name->text)) {
state->m = abc_class_constructor(state->cls, type2, 0);
} else {
__ getlocal_0(state->m);
__ pushscope(state->m);
-
- multiname_destroy(type2);
}
static void endfunction()
{
list->text[l1+l2+l3]=0;
}
-static int find_variable(char*name, class_signature_t**m)
+static int find_variable(char*name, classinfo_t**m)
{
state_list_t* s = state_stack;
while(s) {
}
s = s->next;
}
- syntaxerror("undefined variable: %s", name);
+ return -1;
}
+static int find_variable_safe(char*name, classinfo_t**m)
+{
+ int i = find_variable(name, m);
+ if(i<0)
+ syntaxerror("undefined variable: %s", name);
+ return i;
+}
static char variable_exists(char*name)
{
return array_contains(state->vars, name);
}
-static int new_variable(char*name, class_signature_t*type)
+static int new_variable(char*name, classinfo_t*type)
{
return array_append(state->vars, name, type) + state->local_var_base;
}
{
int t;
for(t=0;t<state->vars->num;t++) {
- class_signature_t*type = array_getvalue(state->vars, t);
+ classinfo_t*type = array_getvalue(state->vars, t);
//do this always, otherwise register types don't match
//in the verifier when doing nested loops
//if(!TYPE_IS_BUILTIN_SIMPLE(type)) {
return c;
}
-char is_subtype_of(class_signature_t*type, class_signature_t*supertype)
+char is_subtype_of(classinfo_t*type, classinfo_t*supertype)
{
return 1; // FIXME
}
}
}
-class_signature_t*join_types(class_signature_t*type1, class_signature_t*type2, char op)
+classinfo_t*join_types(classinfo_t*type1, classinfo_t*type2, char op)
{
return registry_getanytype(); // FIXME
}
-code_t*converttype(code_t*c, class_signature_t*from, class_signature_t*to)
+code_t*converttype(code_t*c, classinfo_t*from, classinfo_t*to)
{
if(from==to)
return c;
return c;
}
-code_t*defaultvalue(code_t*c, class_signature_t*type)
+code_t*defaultvalue(code_t*c, classinfo_t*type)
{
if(TYPE_IS_INT(type) || TYPE_IS_UINT(type) || TYPE_IS_FLOAT(type)) {
c = abc_pushbyte(c, 0);
return c;
}
+char is_pushundefined(code_t*c)
+{
+ return (c && !c->prev && !c->next && c->opcode == OPCODE_PUSHUNDEFINED);
+}
+
%}
CODEBLOCK : CODEPIECE ';' {$$=$1;}
CODEBLOCK : CODEPIECE %prec below_semicolon {$$=$1;}
-/* ------------ functions --------------------------- */
-
-FUNCTION_DECLARATION: MODIFIERS "function" GETSET T_IDENTIFIER '(' MAYBE_PARAM_LIST ')'
- MAYBETYPE '{' {startfunction(0,$1,$3,$4,$6,$8)} MAYBECODE '}' {
- if(!state->m) syntaxerror("internal error: undefined function");
- state->initcode = abc_nop(state->initcode);
- state->initcode = abc_nop(state->initcode);
- state->initcode = abc_nop(state->initcode);
- state->m->code = code_append(state->initcode, $11);state->initcode=0;
- endfunction()
-}
-
/* ------------ variables --------------------------- */
MAYBEEXPRESSION : '=' EXPRESSION {$$=$2;}
printf("variable %s -> %d (%s)\n", $2->text, index, $4.t?$4.t->name:"");
}
ASSIGNMENT : T_IDENTIFIER '=' EXPRESSION {
- class_signature_t*type=0;
- int i = find_variable($1->text, &type);
+ classinfo_t*type=0;
+ int i = find_variable_safe($1->text, &type);
$$ = $3.c;
if(!type && $3.t) {
// convert to "any" type, the register is untyped
/* ------------ packages and imports ---------------- */
-PACKAGE_DECLARATION : "package" MULTILEVELIDENTIFIER '{' {startpackage($2)} MAYBECODE '}' {endpackage()}
-PACKAGE_DECLARATION : "package" '{' {startpackage(0)} MAYBECODE '}' {endpackage()}
+X_IDENTIFIER: T_IDENTIFIER
+ | "package"
PACKAGE: PACKAGE '.' X_IDENTIFIER {$$ = concat3($1,$2,$3);}
PACKAGE: X_IDENTIFIER {$$=$1;}
-IMPORT : "import" PACKAGE '.' X_IDENTIFIER {
- class_signature_t*c = registry_findclass($2->text, $4->text);
+PACKAGE_DECLARATION : "package" PACKAGE '{' {startpackage($2)} MAYBECODE '}' {endpackage()}
+PACKAGE_DECLARATION : "package" '{' {startpackage(0)} MAYBECODE '}' {endpackage()}
+
+IMPORT : "import" QNAME {
+ classinfo_t*c = $2;
if(!c)
- syntaxerror("Couldn't import %s.%s\n", $2->text, $4->text);
+ syntaxerror("Couldn't import class\n");
state_has_imports();
- dict_put(state->imports, $4->text, c);
+ dict_put(state->imports, c->name, c);
$$=0;
}
IMPORT : "import" PACKAGE '.' '*' {
$$=0;
}
-/* ------------ classes and interfaces -------------- */
+/* ------------ classes and interfaces (header) -------------- */
MODIFIERS : {$$=empty_token();}
MODIFIERS : MODIFIER_LIST {$$=$1}
MODIFIER : KW_PUBLIC | KW_PRIVATE | KW_PROTECTED | KW_STATIC | KW_DYNAMIC | KW_FINAL | KW_OVERRIDE | KW_NATIVE | KW_INTERNAL
EXTENDS : {$$=registry_getobjectclass();}
-EXTENDS : KW_EXTENDS PACKAGEANDCLASS {$$=$2;}
+EXTENDS : KW_EXTENDS QNAME {$$=$2;}
EXTENDS_LIST : {$$=list_new();}
-EXTENDS_LIST : KW_EXTENDS PACKAGEANDCLASS_LIST {$$=$2;}
+EXTENDS_LIST : KW_EXTENDS QNAME_LIST {$$=$2;}
IMPLEMENTS_LIST : {$$=list_new();}
-IMPLEMENTS_LIST : KW_IMPLEMENTS PACKAGEANDCLASS_LIST {$$=$2;}
+IMPLEMENTS_LIST : KW_IMPLEMENTS QNAME_LIST {$$=$2;}
CLASS_DECLARATION : MODIFIERS "class" T_IDENTIFIER
EXTENDS IMPLEMENTS_LIST
'{' {startclass($1,$3,$4,$5, 0);}
MAYBE_DECLARATION_LIST
'}' {endclass();}
+
INTERFACE_DECLARATION : MODIFIERS "interface" T_IDENTIFIER
EXTENDS_LIST
'{' {startclass($1,$3,0,$4,1);}
MAYBE_IDECLARATION_LIST
'}' {endclass();}
+/* ------------ classes and interfaces (body) -------------- */
+
+MAYBE_DECLARATION_LIST :
+MAYBE_DECLARATION_LIST : DECLARATION_LIST
+DECLARATION_LIST : DECLARATION
+DECLARATION_LIST : DECLARATION_LIST DECLARATION
+DECLARATION : ';'
+DECLARATION : SLOT_DECLARATION
+DECLARATION : FUNCTION_DECLARATION
+
+VARCONST: "var" | "const"
+SLOT_DECLARATION: MODIFIERS VARCONST T_IDENTIFIER MAYBETYPE MAYBEEXPRESSION {
+ trait_t*t=0;
+ if($4) {
+ MULTINAME(m, $4);
+ t=abc_class_slot(state->cls, $3->text, &m);
+ } else {
+ t=abc_class_slot(state->cls, $3->text, 0);
+ }
+ if($2->type==KW_CONST) {
+ t->kind= TRAIT_CONST;
+ }
+ if($5.c && !is_pushundefined($5.c)) {
+ code_t*c = $5.c;
+ c = abc_getlocal_0(c);
+ c = converttype(c, $5.t, $4);
+ c = abc_setslot(c, t->slot_id);
+ state->cls_init = code_append(state->cls_init, c);
+ }
+}
+
+FUNCTION_DECLARATION: MODIFIERS "function" GETSET T_IDENTIFIER '(' MAYBE_PARAM_LIST ')'
+ MAYBETYPE '{' {startfunction(0,$1,$3,$4,$6,$8)} MAYBECODE '}' {
+ if(!state->m) syntaxerror("internal error: undefined function");
+ state->m->code = code_append(state->initcode, $11);state->initcode=0;
+ endfunction()
+}
+
/* ------------- package + class ids --------------- */
-PACKAGEANDCLASS : T_IDENTIFIER {
+CLASS: T_IDENTIFIER {
/* try current package */
$$ = registry_findclass(state->package, $1->text);
if($$)
break;
if(!strcmp(e->key, $1->text)) {
- $$ = (class_signature_t*)e->data;
+ $$ = (classinfo_t*)e->data;
}
e = e->next;
}
if(!$$) syntaxerror("Could not find class %s\n", $1->text);
}
+
PACKAGEANDCLASS : PACKAGE '.' T_IDENTIFIER {
$$ = registry_findclass($1->text, $3->text);
if(!$$) syntaxerror("Couldn't find class %s.%s\n", $1->text, $3->text);
}
+QNAME: PACKAGEANDCLASS
+ | CLASS
+
/* ----------function calls, constructor calls ------ */
*t = $3;
list_append($$, t);}
-NEW : "new" PACKAGEANDCLASS MAYBE_PARAM_VALUES {
+NEW : "new" CLASS MAYBE_PARAM_VALUES {
MULTINAME(m, $2);
$$.c = code_new();
$$.c = abc_findpropstrict2($$.c, &m);
$$.t = $2;
}
-FUNCTIONCALL : T_IDENTIFIER '(' MAYBE_EXPRESSION_LIST ')' {
- /* TODO: use abc_call (for calling local variables),
- abc_callstatic (for calling own methods) */
- $$.c = code_new();
- $$.c = abc_findpropstrict($$.c, $1->text);
+/* TODO: use abc_call (for calling local variables),
+ abc_callstatic (for calling own methods)
+ call (for closures)
+*/
+FUNCTIONCALL : E '(' MAYBE_EXPRESSION_LIST ')' {
typedcode_list_t*l = $3;
int len = 0;
+ code_t*paramcode = 0;
while(l) {
- $$.c = code_append($$.c, l->typedcode->c); // push parameters on stack
+ paramcode = code_append(paramcode, l->typedcode->c); // push parameters on stack
l = l->next;
len ++;
}
- $$.c = abc_callproperty($$.c, $1->text, len);
+
+ $$.c = $1.c;
+ if($$.c->opcode == OPCODE_GETPROPERTY) {
+ multiname_t*name = multiname_clone($$.c->data[0]);
+ $$.c = code_cutlast($$.c);
+ $$.c = code_append($$.c, paramcode);
+ $$.c = abc_callproperty2($$.c, name, len);
+ } else {
+ int i = find_variable_safe("this", 0);
+ $$.c = abc_getlocal($$.c, i);
+ $$.c = code_append($$.c, paramcode);
+ $$.c = abc_call($$.c, len);
+ }
/* TODO: look up the functions's return value */
$$.t = TYPE_ANY;
}
$$ = abc_returnvalue($$);
}
-TYPE : PACKAGEANDCLASS {$$=$1;}
+TYPE : QNAME {$$=$1;}
| '*' {$$=registry_getanytype();}
| "String" {$$=registry_getstringclass();}
| "int" {$$=registry_getintclass();}
jmp->branch = label;
}
+E : E '.' T_IDENTIFIER
+ {$$.c = $1.c;
+ if($$.t) {
+ namespace_t ns = {$$.t->access, (char*)$$.t->package};
+ multiname_t m = {QNAME, &ns, 0, $3->text};
+ $$.c = abc_getproperty2($$.c, &m);
+ /* FIXME: get type of ($1.t).$3 */
+ $$.t = registry_getanytype();
+ } else {
+ namespace_t ns = {ACCESS_PACKAGE, ""};
+ multiname_t m = {QNAME, &ns, 0, $3->text};
+ $$.c = abc_getproperty2($$.c, &m);
+ $$.t = registry_getanytype();
+ }
+ }
+
E : '!' E {$$.c=$2.c;
$$.c = abc_not($$.c);
$$.t = TYPE_BOOLEAN;
$$.t = join_types($1.t, $3.t, '*');
}
-E : E "as" TYPE
-E : E "is" TYPE
+E : E "as" E
+E : E "is" E
E : '(' E ')' {$$=$2;}
E : '-' E {$$=$2;}
E : LH "+=" E {$$.c = $1.read;$$.c=code_append($$.c,$3.c);$$.c=abc_add($$.c);
- class_signature_t*type = join_types($1.type, $3.t, '+');
+ classinfo_t*type = join_types($1.type, $3.t, '+');
$$.c=converttype($$.c, type, $1.type);
$$.c=abc_dup($$.c);$$.c=code_append($$.c,$1.write);
$$.t = $1.type;
}
E : LH "-=" E {$$.c = $1.read;$$.c=code_append($$.c,$3.c);$$.c=abc_add($$.c);
- class_signature_t*type = join_types($1.type, $3.t, '-');
+ classinfo_t*type = join_types($1.type, $3.t, '-');
$$.c=converttype($$.c, type, $1.type);
$$.c=abc_dup($$.c);$$.c=code_append($$.c,$1.write);
$$.t = $1.type;
// TODO: use inclocal where appropriate
E : LH "++" {$$.c = $1.read;
- class_signature_t*type = $1.type;
+ classinfo_t*type = $1.type;
if(TYPE_IS_INT(type) || TYPE_IS_UINT(type)) {
$$.c=abc_increment_i($$.c);
} else {
$$.t = $1.type;
}
E : LH "--" {$$.c = $1.read;
- class_signature_t*type = $1.type;
+ classinfo_t*type = $1.type;
if(TYPE_IS_INT(type) || TYPE_IS_UINT(type)) {
$$.c=abc_decrement_i($$.c);
} else {
$$.t = $1.type;
}
+/*LH: E {
+ $$.type = $1.t;
+ $$.read = $1.c;
+ $$.write = code_dup($1.c);
+ if($$.write->opcode == OPCODE_GETPROPERTY) {
+ $$.write->opcode = OPCODE_SETPROPERTY;
+ } else if($$.write->opcode == OPCODE_GETLOCAL) {
+ $$.write->opcode = OPCODE_SETLOCAL;
+ } else if($$.write->opcode == OPCODE_GETLOCAL_0) {
+ $$.write->opcode = OPCODE_SETLOCAL_0;
+ } else if($$.write->opcode == OPCODE_GETLOCAL_1) {
+ $$.write->opcode = OPCODE_SETLOCAL_1;
+ } else if($$.write->opcode == OPCODE_GETLOCAL_2) {
+ $$.write->opcode = OPCODE_SETLOCAL_2;
+ } else if($$.write->opcode == OPCODE_GETLOCAL_3) {
+ $$.write->opcode = OPCODE_SETLOCAL_3;
+ } else {
+ syntaxerror("illegal lvalue: can't assign a value to this expression");
+ }
+}*/
+
LH: T_IDENTIFIER {
- int i = find_variable($1->text, &$$.type);
+ $$.type = 0;
+ int i = find_variable_safe($1->text, &$$.type);
$$.read = abc_getlocal(0, i);
$$.write = abc_setlocal(0, i);
}
VAR_READ : T_IDENTIFIER {
+ $$.t = 0;
+ $$.c = 0;
int i = find_variable($1->text, &$$.t);
- $$.c = abc_getlocal(0, i);
+ if(i>=0) {
+ $$.c = abc_getlocal($$.c, i);
+ } else {
+ $$.t = 0;
+ $$.c = abc_findpropstrict($$.c, $1->text);
+ $$.c = abc_getproperty($$.c, $1->text);
+ }
}
//VARIABLE : T_IDENTIFIER
//VARIABLE : VARIABLE "::" '[' EXPRESSION ']' // qualified expression
//VARIABLE : VARIABLE '[' EXPRESSION ']' // unqualified expression
-// keywords which also may be identifiers
-X_IDENTIFIER : T_IDENTIFIER | KW_PACKAGE
-
GETSET : "get" {$$=$1;}
| "set" {$$=$1;}
| {$$=empty_token();}
PARAM: T_IDENTIFIER {$$ = malloc(sizeof(param_t));
$$->name=$1->text;$$->type = TYPE_ANY;}
-DECLARATION : VARIABLE_DECLARATION
-DECLARATION : FUNCTION_DECLARATION
-
IDECLARATION : VARIABLE_DECLARATION
IDECLARATION : FUNCTION_DECLARATION
//IDENTIFIER_LIST : T_IDENTIFIER ',' IDENTIFIER_LIST {extend($3,$1);$$=$3;}
//IDENTIFIER_LIST : T_IDENTIFIER {$$=empty_token();extend($$,$1);}
-MULTILEVELIDENTIFIER : MULTILEVELIDENTIFIER '.' X_IDENTIFIER {$$=$1;extend_s($$, ".", $3)}
-MULTILEVELIDENTIFIER : T_IDENTIFIER {$$=$1;extend($$,$1)};
-
-PACKAGEANDCLASS_LIST : PACKAGEANDCLASS {$$=list_new();list_append($$, $1);}
-PACKAGEANDCLASS_LIST : PACKAGEANDCLASS_LIST ',' PACKAGEANDCLASS {$$=$1;list_append($$,$3);}
+QNAME_LIST : QNAME {$$=list_new();list_append($$, $1);}
+QNAME_LIST : QNAME_LIST ',' QNAME {$$=$1;list_append($$,$3);}
-MAYBE_DECLARATION_LIST :
-MAYBE_DECLARATION_LIST : DECLARATION_LIST
-DECLARATION_LIST : DECLARATION
-DECLARATION_LIST : DECLARATION_LIST DECLARATION
MAYBE_IDECLARATION_LIST :
MAYBE_IDECLARATION_LIST : IDECLARATION_LIST