Skip to content

Commit

Permalink
various changes towards method calls. temporary hacks included.
Browse files Browse the repository at this point in the history
  • Loading branch information
kjs committed Jun 11, 2012
1 parent a1e78f6 commit 9a139ec
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 deletions.
9 changes: 6 additions & 3 deletions src/ast.c
Expand Up @@ -208,14 +208,17 @@ expr_set_unexpr(M1_compiler *comp, m1_expression *node, m1_expression *exp, m1_u
}

m1_expression *
funcall(M1_compiler *comp, char *name, m1_expression *args) {
funcall(M1_compiler *comp, m1_object *fun, m1_expression *args) {
m1_expression *expr = expression(comp, EXPR_FUNCALL);
expr->expr.f = (m1_funcall *)m1_malloc(sizeof(m1_funcall));
expr->expr.f->name = name;


expr->expr.f->name = fun->obj.name;
// expr->expr.f->name = name;
expr->expr.f->arguments = args;

/* enter name of function to invoke into constant table. */
sym_enter_chunk(comp, &comp->currentchunk->constants, name);
sym_enter_chunk(comp, &comp->currentchunk->constants, fun->obj.name);
return expr;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ast.h
Expand Up @@ -321,7 +321,7 @@ extern int yyget_lineno(yyscan_t yyscanner);
extern m1_chunk *chunk(M1_compiler *comp, char *rettype, char *name);

extern m1_expression *expression(M1_compiler *comp, m1_expr_type type);
extern m1_expression *funcall(M1_compiler *comp, char *name, m1_expression *args);
extern m1_expression *funcall(M1_compiler *comp, m1_object *fun, m1_expression *args);

extern m1_object *object(M1_compiler *comp, m1_object_type type);
extern void obj_set_ident(m1_object *node, char *ident);
Expand Down
14 changes: 9 additions & 5 deletions src/gencode.c
Expand Up @@ -81,8 +81,8 @@ use_reg(M1_compiler *comp, m1_valuetype type) {
i++;
}

if (i >= REG_NUM) fprintf(stderr, "Out of registers!\n");
else fprintf(stderr, "Allocating register %d\n", i);
//if (i >= REG_NUM) fprintf(stderr, "Out of registers!\n");
//else fprintf(stderr, "Allocating register %d\n", i);

/* set the register to "used". */
registers[type][i] = REG_USED;
Expand Down Expand Up @@ -113,10 +113,11 @@ static void
unuse_reg(M1_compiler *comp, m1_reg r) {

if (registers[r.type][r.no] != REG_SYMBOL) {
fprintf(stderr, "Unusing %d for good\n", r.no);
//fprintf(stderr, "Unusing %d for good\n", r.no);
registers[r.type][r.no] = REG_UNUSED;
}
/* XXX this is for debugging. */
/*
int i;
for (i = 0; i < 40; i++)
fprintf(stderr, "%2d ", i);
Expand All @@ -126,6 +127,7 @@ unuse_reg(M1_compiler *comp, m1_reg r) {
fprintf(stderr, "%2d ", registers[r.type][i]);
}
fprintf(stderr, "\n\n");
*/
}

/*
Expand Down Expand Up @@ -1404,14 +1406,16 @@ gencode_break(M1_compiler *comp) {

static void
gencode_funcall(M1_compiler *comp, m1_funcall *f) {
m1_symbol *fun = sym_find_chunk(&comp->currentchunk->constants, f->name);
m1_symbol *fun;
fun = sym_find_chunk(&comp->currentchunk->constants, f->name);
m1_reg pc_reg, cont_offset, return_reg;

if (fun == NULL) { /* XXX need to check in semcheck */
if (fun == NULL) { // XXX need to check in semcheck
fprintf(stderr, "Cant find function '%s'\n", f->name);
++comp->errors;
return;
}



m1_reg cf_reg = use_reg(comp, VAL_CHUNK);
Expand Down
7 changes: 3 additions & 4 deletions src/m1.y
Expand Up @@ -719,10 +719,9 @@ default_case: /* empty */
| "default" ':' statements
{ $$ = $3; }
;

/* TODO: at some point we want x.y(); replace TK_IDENT with "lhs". Get this working first though*/
function_call_expr : TK_IDENT '(' arguments ')'
{ $$ = funcall((M1_compiler *)yyget_extra(yyscanner), $1, $3); }

function_call_expr : lhs '(' arguments ')'
{ $$ = funcall((M1_compiler *)yyget_extra(yyscanner), $1->expr.t, $3); }
;

function_call_stat : function_call_expr ';'
Expand Down
10 changes: 6 additions & 4 deletions src/semcheck.c
Expand Up @@ -52,7 +52,8 @@ type_error_extra(M1_compiler *comp, unsigned line, char *msg, ...) {

for (p = msg; *p != '\0'; p++) {
if (*p != '%') {
putchar(*p);
putc(*p, stderr);

continue;
}
switch (*++p) {
Expand All @@ -76,6 +77,7 @@ type_error_extra(M1_compiler *comp, unsigned line, char *msg, ...) {
}

}
fprintf(stderr, "\n");

va_end(argp);
}
Expand Down Expand Up @@ -409,7 +411,7 @@ check_switch(M1_compiler *comp, m1_switch *s, unsigned line) {
(void)pop(comp->breakstack);
}

static void
static m1_decl *
check_newexpr(M1_compiler *comp, m1_newexpr *n, unsigned line) {
assert(comp != NULL);
assert(n != NULL);
Expand All @@ -419,7 +421,7 @@ check_newexpr(M1_compiler *comp, m1_newexpr *n, unsigned line) {
if (n->typedecl == NULL) {
type_error_extra(comp, line, "Cannot find type '%s' requested for in new-statement\n", n->type);
}

return n->typedecl;
}

static void
Expand Down Expand Up @@ -551,7 +553,7 @@ check_expr(M1_compiler *comp, m1_expression *e) {
break;

case EXPR_NEW:
check_newexpr(comp, e->expr.n, e->line);
return check_newexpr(comp, e->expr.n, e->line);
break;
case EXPR_PRINT:
check_expr(comp, e->expr.e);
Expand Down
3 changes: 3 additions & 0 deletions t/pmc.m1
Expand Up @@ -11,6 +11,9 @@ int main() {
print("1..1\n");
print("ok 1 - PMC parsed correctly\n");

integer i;
i = new integer();
// i.init();
}


0 comments on commit 9a139ec

Please sign in to comment.