Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
switch back to c-m0. improve naming of union members.
  • Loading branch information
kjs committed Jun 27, 2012
1 parent 0d40e57 commit d0801b5
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 36 deletions.
2 changes: 1 addition & 1 deletion run_m1.sh
Expand Up @@ -11,5 +11,5 @@ file_suffixe=${1##*.}
./m1 $1 2>/dev/null > $filename.m0
[ -s $filename.m0 ] || { echo "error: outputs a empty file $filename.m0 when compiling $1"; exit 1; }
./m0_assembler.pl $filename.m0 >/dev/null
perl m0_interp.pl $filename.m0b || { exit 1; }
./m0 $filename.m0b || { exit 1; }
exit 0
22 changes: 11 additions & 11 deletions src/ast.c
Expand Up @@ -137,7 +137,7 @@ string(M1_compiler *comp, char *str) {

static void
obj_set_index(m1_object *node, m1_expression *index) {
node->obj.index = index;
node->obj.as_index = index;
}

m1_object *
Expand Down Expand Up @@ -214,13 +214,13 @@ funcall(M1_compiler *comp, m1_object *fun, m1_expression *args) {
expr->expr.as_funcall = (m1_funcall *)m1_malloc(sizeof(m1_funcall));

/* XXX need to handle method calls. */
expr->expr.as_funcall->name = fun->obj.name;
expr->expr.as_funcall->name = fun->obj.as_name;
expr->expr.as_funcall->arguments = args;

/* enter name of function to invoke into constant table. */
// replace this somehow. Get access to the vtable of the object and copy the
// method reference from that into this chunk's const segment.
m1_symbol *chunk_entry = sym_enter_chunk(comp, &comp->currentchunk->constants, fun->obj.name);
m1_symbol *chunk_entry = sym_enter_chunk(comp, &comp->currentchunk->constants, fun->obj.as_name);
expr->expr.as_funcall->constindex = chunk_entry->constindex;
return expr;
}
Expand All @@ -244,8 +244,8 @@ constdecl(M1_compiler *comp, char *type, char *name, m1_expression *e) {
return expr;
}

void
expr_set_for(M1_compiler *comp, m1_expression *node, m1_expression *init, m1_expression *cond, m1_expression *step,
static void
expr_set_for(m1_expression *node, m1_expression *init, m1_expression *cond, m1_expression *step,
m1_expression *stat)
{
node->expr.o = (m1_forexpr *)m1_malloc(sizeof(m1_forexpr));
Expand All @@ -259,7 +259,7 @@ expr_set_for(M1_compiler *comp, m1_expression *node, m1_expression *init, m1_exp
m1_expression *
forexpr(M1_compiler *comp, m1_expression *init, m1_expression *cond, m1_expression *step, m1_expression *stat) {
m1_expression *expr = expression(comp, EXPR_FOR);
expr_set_for(comp, expr, init, cond, step, stat);
expr_set_for(expr, init, cond, step, stat);
return expr;
}

Expand Down Expand Up @@ -348,7 +348,7 @@ expr_set_obj(m1_expression *node, m1_object *obj) {
/* Set the <name> field of the union in the m1_object node. */
void
obj_set_ident(m1_object *node, char *ident) {
node->obj.name = ident;
node->obj.as_name = ident;
}


Expand All @@ -363,11 +363,11 @@ object(M1_compiler *comp, m1_object_type type) {

m1_object *
lhsobj(M1_compiler *comp, m1_object *parent, m1_object *field) {
m1_object *lhsobj = (m1_object *)m1_malloc(sizeof(m1_object));
lhsobj->type = OBJECT_LINK;
m1_object *lhsobj = (m1_object *)m1_malloc(sizeof(m1_object));
lhsobj->type = OBJECT_LINK;

lhsobj->obj.field = field;
lhsobj->parent = parent;
lhsobj->obj.as_field = field;
lhsobj->parent = parent;

assert(comp != NULL);
return lhsobj;
Expand Down
10 changes: 3 additions & 7 deletions src/ast.h
Expand Up @@ -214,9 +214,9 @@ typedef struct m1_object {
unsigned line;

union {
char *name; /* for name, field or deref access, in a.b.c for instance. */
struct m1_expression *index; /* for array index (a[42]) */
struct m1_object *field; /* if this is a linking node (OBJECT_LINK) representing "a.b" as a whole. */
char *as_name; /* for name, field or deref access, in a.b.c for instance. */
struct m1_expression *as_index; /* for array index (a[42]) */
struct m1_object *as_field; /* if this is a linking node (OBJECT_LINK) representing "a.b" as a whole. */
} obj;

enum m1_object_type type; /* selector for union */
Expand Down Expand Up @@ -362,10 +362,6 @@ extern m1_expression *whileexpr(M1_compiler *comp, m1_expression *cond, m1_expre
extern m1_expression *dowhileexpr(M1_compiler *comp, m1_expression *cond, m1_expression *block);
extern m1_expression *forexpr(M1_compiler *comp, m1_expression *init, m1_expression *cond, m1_expression *step, m1_expression *stat);

extern void expr_set_for(M1_compiler *comp, m1_expression *node, m1_expression *init,
m1_expression *cond, m1_expression *step, m1_expression *stat);


extern m1_expression *inc_or_dec(M1_compiler *comp, m1_expression *obj, m1_unop optype);
extern m1_expression *returnexpr(M1_compiler *comp, m1_expression *retexp);
extern m1_expression *assignexpr(M1_compiler *comp, m1_expression *lhs, int assignop, m1_expression *rhs);
Expand Down
8 changes: 4 additions & 4 deletions src/eval.c
Expand Up @@ -44,17 +44,17 @@ static void
eval_obj(m1_object *obj) {
switch (obj->type) {
case OBJECT_MAIN:
fprintf(OUT, "%s", obj->obj.name);
fprintf(OUT, "%s", obj->obj.as_name);
break;
case OBJECT_FIELD:
fprintf(OUT, ".%s", obj->obj.name);
fprintf(OUT, ".%s", obj->obj.as_name);
break;
case OBJECT_DEREF:
fprintf(OUT, "->%s", obj->obj.name);
fprintf(OUT, "->%s", obj->obj.as_name);
break;
case OBJECT_INDEX:
fprintf(OUT, "[");
eval_expr(obj->obj.index);
eval_expr(obj->obj.as_index);
fprintf(OUT, "]");
break;
default:
Expand Down
12 changes: 6 additions & 6 deletions src/gencode.c
Expand Up @@ -614,12 +614,12 @@ OBJECT_LINK------> L3
As we do this, keep track of how many registers were used to store the result.
*/

numregs_pushed += gencode_obj(comp, obj->obj.field, parent, dimension, is_target);
numregs_pushed += gencode_obj(comp, obj->obj.as_field, parent, dimension, is_target);

if (numregs_pushed == 3) {

/* if the field was an index. (a[b]) */
if (obj->obj.field->type == OBJECT_INDEX) {
if (obj->obj.as_field->type == OBJECT_INDEX) {
m1_reg last = popreg(comp->regstack); /* latest added; store here for now. */
m1_reg field = popreg(comp->regstack); /* 2nd latest, this one needs to be removed. */
m1_reg parentreg = popreg(comp->regstack); /* x in x[2][3]. */
Expand Down Expand Up @@ -690,7 +690,7 @@ OBJECT_LINK------> L3
/* we popped 3, and pushed 2, so effectively decrement by 1. */
--numregs_pushed;
}
else if (obj->obj.field->type == OBJECT_FIELD) {
else if (obj->obj.as_field->type == OBJECT_FIELD) {
/* field is a struct member access (a.b) */
m1_reg last = popreg(comp->regstack);
m1_reg offset = popreg(comp->regstack);
Expand All @@ -716,7 +716,7 @@ OBJECT_LINK------> L3
{
m1_reg reg;

assert(obj->obj.field != NULL);
assert(obj->obj.as_field != NULL);
assert(obj->sym != NULL);
assert(obj->sym->typedecl != NULL);

Expand Down Expand Up @@ -768,7 +768,7 @@ OBJECT_LINK------> L3
assert((*parent)->sym->typedecl != NULL);

/* parent's symbol has a typedecl node, which holds the structdef (d.s), which has a symbol table. */
m1_symbol *fieldsym = sym_lookup_symbol(&(*parent)->sym->typedecl->d.as_struct->sfields, obj->obj.name);
m1_symbol *fieldsym = sym_lookup_symbol(&(*parent)->sym->typedecl->d.as_struct->sfields, obj->obj.as_name);

assert(fieldsym != NULL);

Expand Down Expand Up @@ -801,7 +801,7 @@ OBJECT_LINK------> L3
case OBJECT_INDEX: /* b in a[b] */
{
(*dimension)++; /* increment dimension whenever we handle an index. */
numregs_pushed += gencode_expr(comp, obj->obj.index);
numregs_pushed += gencode_expr(comp, obj->obj.as_index);
break;
}
default:
Expand Down
14 changes: 7 additions & 7 deletions src/semcheck.c
Expand Up @@ -124,23 +124,23 @@ check_obj(M1_compiler *comp, m1_object *obj, unsigned line, m1_object **parent)
when it's an index, return the type of obj->parent.
*/
if (t != VOIDTYPE) /* only check fields if main object was found. */
fieldtype = check_obj(comp, obj->obj.field, line, parent);
fieldtype = check_obj(comp, obj->obj.as_field, line, parent);

/* if it's a struct member, get the member's type. If it's an array, just
return the parent's type.
*/
if (obj->obj.field->type == OBJECT_FIELD)
if (obj->obj.as_field->type == OBJECT_FIELD)
t = fieldtype;

break;
}
case OBJECT_MAIN: {

/* look up identifier's declaration. */
obj->sym = sym_lookup_symbol(comp->currentsymtab, obj->obj.name);
obj->sym = sym_lookup_symbol(comp->currentsymtab, obj->obj.as_name);

if (obj->sym == NULL) {
type_error(comp, line, "undeclared variable '%s'", obj->obj.name);
type_error(comp, line, "undeclared variable '%s'", obj->obj.as_name);
}
else { /* found symbol, now link it to the object node. */

Expand All @@ -162,11 +162,11 @@ check_obj(M1_compiler *comp, m1_object *obj, unsigned line, m1_object **parent)
assert((*parent)->sym != NULL);
assert((*parent)->sym->typedecl != NULL);
/* look up symbol for this field in parent's symbol table (which is a struct/PMC). */
obj->sym = sym_lookup_symbol( &(*parent)->sym->typedecl->d.as_struct->sfields, obj->obj.name);
obj->sym = sym_lookup_symbol( &(*parent)->sym->typedecl->d.as_struct->sfields, obj->obj.as_name);

if (obj->sym == NULL) {
type_error(comp, line, "struct %s has no member %s",
(*parent)->obj.name, obj->obj.name);
(*parent)->obj.as_name, obj->obj.as_name);
}
else {
/* find the type declaration for this field's type. */
Expand All @@ -187,7 +187,7 @@ check_obj(M1_compiler *comp, m1_object *obj, unsigned line, m1_object **parent)
assert(0);
break;
case OBJECT_INDEX:
t = check_expr(comp, obj->obj.index);
t = check_expr(comp, obj->obj.as_index);
if (t != INTTYPE) {
type_error(comp, line, "result of expression does not yield an integer value");
}
Expand Down

0 comments on commit d0801b5

Please sign in to comment.