Author: paultcochrane
Date: Fri Mar 30 15:49:26 2007
New Revision: 17871
Modified:
trunk/languages/cola/gen.c
trunk/languages/cola/semant.c
trunk/languages/cola/sym.c
trunk/languages/cola/type.c
Log:
[cola] Fixing parentheses to agree with coding standards.
Modified: trunk/languages/cola/gen.c
==============================================================================
--- trunk/languages/cola/gen.c (original)
+++ trunk/languages/cola/gen.c Fri Mar 30 15:49:26 2007
@@ -29,9 +29,9 @@
void gen_ast(AST * ast) {
AST * p;
- for(p = ast; p; p = p->next) {
- if(p->kind == KIND_DECL)
- switch(p->asttype) {
+ for (p = ast; p; p = p->next) {
+ if (p->kind == KIND_DECL)
+ switch (p->asttype) {
case ASTT_NAMESPACE_DECL:
gen_namespace_decl(p);
printf("#.namespace\n#End of namespace %s\n", p->sym->name);
@@ -53,20 +53,20 @@
void gen_namespace_decl(AST * p) {
printf("#.namespace %s\n", p->sym->name);
- if(p->arg1)
+ if (p->arg1)
gen_ast(p->arg1);
}
void gen_class_decl(AST * p) {
printf("#.class %s\n", p->sym->name);
- if(p->Attr.Class.body) {
+ if (p->Attr.Class.body) {
gen_class_body(p->Attr.Class.body);
}
}
void gen_class_body(AST * p) {
- while(p) {
- switch(p->asttype) {
+ while (p) {
+ switch (p->asttype) {
case ASTT_CONSTANT_DECL:
gen_constant_decl(p);
break;
@@ -96,15 +96,15 @@
void gen_block(AST * p) {
/* First declare locals. */
- if(!p) {
+ if (!p) {
fprintf(stderr, "WARNING: Null block in AST tree.\n");
return;
}
- if(p->vars) {
+ if (p->vars) {
Symbol * s = p->vars;
- while(s) {
+ while (s) {
/* Don't emit local types that are in symbol table */
- if(s->kind != TYPE)
+ if (s->kind != TYPE)
printf("\t.local %s\t%s\n", s->typename->name, s->name);
s = s->tnext;
}
@@ -117,13 +117,13 @@
#if DEBUG
printf("#gen_statement\n");
#endif
- if(p->kind == KIND_EXPR) {
- if(!eval_expr(p))
+ if (p->kind == KIND_EXPR) {
+ if (!eval_expr(p))
gen_expr(p, NULL, NULL);
goto END;
}
- switch(p->asttype) {
+ switch (p->asttype) {
case ASTT_CONSTANT_DECL:
gen_constant_decl(p);
break;
@@ -145,43 +145,43 @@
gen_for(p);
break;
case ASTT_BREAK:
- if(get_cur_primary_block() == NULL) {
+ if (get_cur_primary_block() == NULL) {
printf("break statement not within loop or switch\n");
exit(0);
}
printf("\tgoto %s\n", get_cur_primary_block()->end_label);
break;
case ASTT_CONTINUE:
- if((b = get_cur_primary_block()) == NULL
+ if ((b = get_cur_primary_block()) == NULL
|| (b->asttype != ASTT_FOR && b->asttype != ASTT_WHILE)) {
printf("continue statement not within a loop\n");
exit(0);
}
- if(b->asttype == ASTT_WHILE)
+ if (b->asttype == ASTT_WHILE)
printf("\tgoto %s\n", b->start_label);
else {
- if(b->Attr.Loop.iteration)
+ if (b->Attr.Loop.iteration)
printf("\tgoto %s\n", b->Attr.Loop.iteration->start_label);
else
printf("\tgoto %s\n", b->start_label);
}
break;
case ASTT_RETURN:
- if(cur_method == NULL) {
+ if (cur_method == NULL) {
printf("return statement not within a method\n");
exit(0);
}
- if(cur_method->sym->type != t_void) {
- if(p->arg1 == NULL) {
+ if (cur_method->sym->type != t_void) {
+ if (p->arg1 == NULL) {
printf("ERROR: Method '%s' must return a value.\n",
cur_method->sym->name);
exit(0);
}
- if(!eval_expr(p->arg1))
+ if (!eval_expr(p->arg1))
gen_expr(p->arg1, NULL, cur_method->sym->type);
printf("\t# return value\n\t.return(%s)\n",
- NAME(p->arg1->targ) );
+ NAME(p->arg1->targ));
}
/* Simple optimization that works most of the time.
* If this statement is the last statement in the method
@@ -189,15 +189,15 @@
* the jump to end for the return. The optimizer should
* really be used to pick up all instances of this.
*/
- if(get_cur_primary_block() != NULL || p->next != NULL)
+ if (get_cur_primary_block() != NULL || p->next != NULL)
printf("\tgoto %s\n", cur_method->end_label);
break;
default:
- printf( "UNKNOWN AST node : %d/%d\n", p->kind, p->asttype);
+ printf("UNKNOWN AST node : %d/%d\n", p->kind, p->asttype);
}
END:
- if(p->next)
+ if (p->next)
gen_statement(p->next);
}
@@ -205,13 +205,13 @@
/* Don't generate the identifier in arg1, it was collected for ordering
* in phase 2 and declared at the top of the block.
*/
- if(p->arg2) {
+ if (p->arg2) {
gen_expr(p->arg2, NULL, NULL);
}
}
void gen_param_list(Symbol * paramlist) {
- if(paramlist->tnext)
+ if (paramlist->tnext)
gen_param_list(paramlist->tnext);
printf("\t.param %s\t%s\n", paramlist->typename->name, paramlist->name);
}
@@ -222,21 +222,21 @@
reset_temps();
cur_method = p;
p->end_label = make_label();
- if(cur_method->sym == main_method)
+ if (cur_method->sym == main_method)
attr = " :main";
- if(p->sym->namespace && p->sym->namespace != global_namespace)
+ if (p->sym->namespace && p->sym->namespace != global_namespace)
printf(".sub _%s__%s%s\n", p->sym->namespace->name, p->sym->name,
attr);
else
printf(".sub __%s%s\n", p->sym->name, attr);
#if 0
check_id_redecl(global_symbol_table, p->sym->name);
#endif
- if((s = p->Attr.Method.params) != NULL)
+ if ((s = p->Attr.Method.params) != NULL)
gen_param_list(s);
/*printf("\tsaveall\n");*/
- if(p->Attr.Method.body) {
+ if (p->Attr.Method.body) {
gen_block(p->Attr.Method.body);
}
@@ -249,7 +249,7 @@
#if DEBUG
printf("#gen_assign\n");
#endif
- if(p->arg1->asttype == ASTT_IDENTIFIER ||
+ if (p->arg1->asttype == ASTT_IDENTIFIER ||
p->arg1->asttype == ASTT_LITERAL) {
p->arg1->targ = p->arg1->sym;
}
@@ -259,7 +259,7 @@
* a target lval is passed to it. This gets rid of some
* temporaries of type (temp1 = source; target = temp1),
*/
- if(p->arg1->targ) {
+ if (p->arg1->targ) {
/* target is an identifier or address, pass the
* symbol to gen_expr() which will emit the expression
* and assignment.
@@ -275,32 +275,32 @@
* have parsed to a assignable lvalue
*/
/* Now assign the temporary to the lvalue expression */
- if(p->arg1->asttype == ASTT_INDEX) {
+ if (p->arg1->asttype == ASTT_INDEX) {
char buf[4096];
AST * deref = p->arg1;
#if DEBUG
printf("# gen_assign: assign to index\n");
#endif
- if(deref->arg1 == NULL || deref->arg2 == NULL) {
+ if (deref->arg1 == NULL || deref->arg2 == NULL) {
printf("Internal error: invalid array expression or
indirection as lvalue\n");
abort();
}
/* Assign to an array */
- if(!eval_expr(deref->arg1))
+ if (!eval_expr(deref->arg1))
gen_expr(deref->arg1, NULL, NULL);
/* Require array subscript to be int */
- if(!eval_expr(deref->arg2))
+ if (!eval_expr(deref->arg2))
gen_expr(deref->arg2, NULL, t_int32);
#if 0
/* If destination indirection uses a variable, copy it to a
* temporary in case it is modified by source expression code
- * ( d[i] = s[++i] )
+ * (d[i] = s[++i])
* ANSI C says this is undefined, but not sure about C#/Java
* so we try to do the right thing.
*/
- if(deref->arg2->asttype == ASTT_IDENTIFIER) {
+ if (deref->arg2->asttype == ASTT_IDENTIFIER) {
deref->arg2->targ = new_temp(deref->arg2->sym->type);
printf("\t(%s)%s = %s\n", type_name(deref->arg2->targ->type),
deref->arg2->targ->name, deref->arg2->sym->name);
@@ -348,9 +348,9 @@
#endif
/* Expression is a simple identifier or literal */
- if(p->asttype == ASTT_IDENTIFIER ||
+ if (p->asttype == ASTT_IDENTIFIER ||
p->asttype == ASTT_LITERAL) {
- if(lval)
+ if (lval)
p->targ = lval;
else
p->targ = new_temp(p->sym->type);
@@ -366,9 +366,9 @@
}
/* Expression is a new() expression */
- if(p->asttype == ASTT_NEW_OBJECT) {
+ if (p->asttype == ASTT_NEW_OBJECT) {
printf("\t# new()\n");
- if(lval != NULL)
+ if (lval != NULL)
p->targ = lval;
else
p->targ = new_temp(p->type);
@@ -381,14 +381,14 @@
}
/* Expression has no operands */
- if(p->arg1 == NULL && p->arg2 == NULL) {
+ if (p->arg1 == NULL && p->arg2 == NULL) {
printf("#gen_expr: no operands\n");
exit(0);
}
/* Expression is a method call */
- if(p->asttype == ASTT_METHOD_CALL) {
- if(lval)
+ if (p->asttype == ASTT_METHOD_CALL) {
+ if (lval)
p->targ = lval;
gen_method_call(p);
#if DEBUG
@@ -397,14 +397,14 @@
return;
}
/* Expression is an assignment */
- else if(p->asttype == ASTT_ASSIGN) {
+ else if (p->asttype == ASTT_ASSIGN) {
gen_assign(p);
#if DEBUG
goto LAST;
#endif
return;
}
- else if(p->asttype == ASTT_CONDITIONAL_EXPR) {
+ else if (p->asttype == ASTT_CONDITIONAL_EXPR) {
/* Ternary conditional: i > j ? i : j */
/* This needs work */
#if DEBUG
@@ -416,7 +416,7 @@
gen_boolean(p->Attr.Conditional.condition, tl1, tl2, 1);
printf("%s:\n", tl1);
p->targ = lval;
- if(p->targ == NULL) {
+ if (p->targ == NULL) {
gen_expr(p->arg1, NULL, type);
p->targ = p->arg1->targ;
}
@@ -430,42 +430,42 @@
*/
gen_expr(p->arg2, p->targ, p->targ->type);
printf("%s:\n", tl3);
- if(!p->arg1->targ || !p->arg2->targ) {
+ if (!p->arg1->targ || !p->arg2->targ) {
fprintf(stderr, "Error: ternary conditionals must generate rvalues
to be a statement.\n");
exit(0);
}
return;
}
- if(!eval_expr(p->arg1))
+ if (!eval_expr(p->arg1))
gen_expr(p->arg1, NULL, NULL);
- if(p->arg2) {
- if(!eval_expr(p->arg2))
+ if (p->arg2) {
+ if (!eval_expr(p->arg2))
gen_expr(p->arg2, NULL, NULL);
}
/* Very limited type coercion here */
- if(p->arg1 && p->arg2) {
+ if (p->arg1 && p->arg2) {
Type * type1 = p->arg1->targ->type,
* type2 = p->arg2->targ->type;
- if(lval)
+ if (lval)
p->targ = lval;
- if(p->op == INDEX) {
+ if (p->op == INDEX) {
#if DEBUG
printf("# gen_expr: index operator\n");
#endif
- if(type2 != t_int32) {
+ if (type2 != t_int32) {
fprintf(stderr, "Array subscript expression must be type
integer.\n");
exit(0);
}
- if(type1 != t_string) {
+ if (type1 != t_string) {
fprintf(stderr, "Index operators not yet implemented for
non-string types.\n");
exit(0);
}
- if(p->targ == NULL)
+ if (p->targ == NULL)
p->targ = new_temp(type1);
#if 0
printf("\t(%s)%s = %s[%s]\n", p->targ->typename->name,
@@ -480,14 +480,14 @@
return;
}
- if(type1 != type2) {
+ if (type1 != type2) {
/* Generate required casting assignments to temporaries */
#if DEBUG
fprintf(stderr, "#typeof(%s) != typeof(%s)\n",
p->arg1->targ->name, p->arg2->targ->name);
#endif
coerce_operands(&type1, &type2);
- if(type1 != p->arg1->targ->type) {
+ if (type1 != p->arg1->targ->type) {
Symbol * cast = new_temp(type1);
#if 0
printf("\t(%s)%s = %s\n", cast->typename->name,
@@ -496,7 +496,7 @@
printf("\t%s = %s\n", cast->name, p->arg1->targ->name);
p->arg1->targ = cast;
}
- if(type2 != p->arg2->targ->type) {
+ if (type2 != p->arg2->targ->type) {
Symbol * cast = new_temp(type2);
#if 0
printf("\t(%s)%s = %s\n", cast->typename->name,
@@ -513,9 +513,9 @@
* might also need a coercion. Generator really should not
* pass in an lvalue that isn't compatible.
*/
- if(p->targ == NULL)
+ if (p->targ == NULL)
p->targ = new_temp(type1);
- if(p->targ->type != type1) {
+ if (p->targ->type != type1) {
Symbol * temp = new_temp(type1);
emit_op_expr(temp, p->arg1->targ, op_name(p->op), p->arg2->targ);
#if 0
@@ -530,7 +530,7 @@
}
else {
/* Unary */
- if(p->asttype == ASTT_PREINC) {
+ if (p->asttype == ASTT_PREINC) {
/* j = ++i ->
* inc $0
* $1 = $0
@@ -538,7 +538,7 @@
p->targ = p->arg1->targ;
printf("\t%s %s\n", op_name(p->op), p->targ->name);
}
- else if(p->asttype == ASTT_POSTINC) {
+ else if (p->asttype == ASTT_POSTINC) {
/*
* j = i++ ->
* $1 = $0
@@ -554,7 +554,7 @@
printf("\t%s %s\n", op_name(p->op), p->arg1->targ->name);
}
else {
- if(lval)
+ if (lval)
p->targ = lval;
else
p->targ = new_temp(p->arg1->targ->type);
@@ -568,18 +568,18 @@
}
void gen_arg_list_expr(AST * p) {
- if(p == NULL)
+ if (p == NULL)
return;
/* FIXME: Here we should check the method signature and find out
* what type is expected.
*/
- if(!eval_expr(p))
+ if (!eval_expr(p))
gen_expr(p, NULL, NULL);
- if(!p->targ) {
+ if (!p->targ) {
fprintf(stderr, "Internal compiler error: argument expression didn't
generate an rvalue\n");
exit(0);
}
- if(p->next) {
+ if (p->next) {
gen_arg_list_expr(p->next);
}
}
@@ -588,18 +588,18 @@
* Generate arguments in reverse order on stack.
*/
void gen_arg_list(AST * p) {
- if(p == NULL)
+ if (p == NULL)
return;
/* FIXME: Here we should check the method signature and find out
* what type is expected.
*/
- if(p->targ)
+ if (p->targ)
printf("%s", NAME(p->targ));
else {
fprintf(stderr, "Internal compiler error: argument expression didn't
generate an rvalue\n");
exit(0);
}
- if(p->next) {
+ if (p->next) {
printf(", ");
gen_arg_list(p->next);
}
@@ -607,18 +607,18 @@
void gen_method_call(AST * p) {
/* FIXME: Should check that expression evaluates to a method */
- if(!eval_expr(p->arg1))
+ if (!eval_expr(p->arg1))
gen_expr(p->arg1, NULL, NULL);
/* Argument list expressions */
- if(p->arg2)
+ if (p->arg2)
gen_arg_list_expr(p->arg2);
/*
* if p is instance method, push implicit object reference onto
* calling stack.
*/
- if(p->arg1->targ->namespace && p->arg1->targ->namespace !=
global_namespace) {
+ if (p->arg1->targ->namespace && p->arg1->targ->namespace !=
global_namespace) {
printf("\t_%s__%s(", p->arg1->targ->namespace->name,
p->arg1->targ->name);
}
else {
@@ -626,21 +626,21 @@
}
/* Argument list */
- if(p->arg2)
+ if (p->arg2)
gen_arg_list(p->arg2);
printf(")\n");
- if(p->arg1->targ->type == t_void) {
- if(p->targ != NULL) {
+ if (p->arg1->targ->type == t_void) {
+ if (p->targ != NULL) {
/* If caller wants a return value, method can't be void. */
fprintf(stderr, "ERROR: void function [%s] does not return a
value.\n",
- p->arg1->targ->name );
+ p->arg1->targ->name);
exit(0);
}
}
else {
- if(p->targ == NULL)
+ if (p->targ == NULL)
p->targ = new_temp(p->arg1->targ->type);
printf("\t#Get return val in %s\n", p->targ->name);
printf("\trestore %s\n", p->targ->name);
@@ -655,19 +655,19 @@
if_label = make_label();
else_label = make_label();
end_label = make_label();
- if(!p->Attr.Conditional.condition
+ if (!p->Attr.Conditional.condition
|| p->Attr.Conditional.condition->kind != KIND_EXPR) {
fprintf(stderr, "gen_if: Null or invalid CONDITION node\n");
exit(0);
}
- if(!p->arg1) {
+ if (!p->arg1) {
fprintf(stderr, "gen_if: Null THEN node\n");
exit(0);
}
/* If Then Else */
- if(p->arg2) {
+ if (p->arg2) {
gen_boolean(p->Attr.Conditional.condition, if_label, else_label, 1);
printf("%s:\n", if_label);
gen_block(p->arg1);
@@ -705,11 +705,11 @@
#endif
p->start_label = make_label();
p->end_label = make_label();
- if(p->Attr.Loop.iteration)
+ if (p->Attr.Loop.iteration)
p->Attr.Loop.iteration->start_label = make_label();
push_primary_block(p);
- if(p->Attr.Loop.init != NULL) {
+ if (p->Attr.Loop.init != NULL) {
#if DEBUG
printf("#gen_for: generate init statement\n");
#endif
@@ -719,7 +719,7 @@
gen_boolean(p->Attr.Loop.condition, p->start_label, p->end_label, 1);
printf("%s:\n", p->start_label);
gen_block(p->Attr.Loop.body);
- if(p->Attr.Loop.iteration) {
+ if (p->Attr.Loop.iteration) {
printf("%s:\n", p->Attr.Loop.iteration->start_label);
gen_statement(p->Attr.Loop.iteration);
}
@@ -740,7 +740,7 @@
printf("#gen_boolean\n");
#endif
- switch(p->asttype) {
+ switch (p->asttype) {
/* If literal or identifier generate a comparison to 0
* Fix this to skip the conditional for constant expressions.
*/
@@ -750,12 +750,12 @@
* either optimize the compare away or generate a comparison
* to the boolean false equivalent to the data type.
*/
- if(p->sym == NULL) {
+ if (p->sym == NULL) {
printf("Internal error: gen_bool(): NULL symbol for
condition\n");
abort();
}
p->targ = p->sym;
- if(invert) {
+ if (invert) {
inverse_label = false_label;
op_inv = LOGICAL_EQ;
}
@@ -763,7 +763,7 @@
inverse_label = true_label;
op_inv = LOGICAL_NE;
}
- if(p->targ->type == t_string) {
+ if (p->targ->type == t_string) {
printf("\tif %s %s \"\" goto %s\n", NAME(p->targ),
op_name(op_inv), inverse_label);
}
@@ -774,7 +774,7 @@
return;
case ASTT_LOGICAL:
printf("#Logical expression\n");
- if(p->op == LOGICAL_AND) {
+ if (p->op == LOGICAL_AND) {
/* AND */
const char * next_logical = make_label();
gen_boolean(p->arg1, next_logical, false_label, 1);
@@ -790,15 +790,15 @@
}
return;
case ASTT_COMPARISON:
- if(!p->arg1 || !p->arg2) {
+ if (!p->arg1 || !p->arg2) {
printf("#gen_boolean(comparison): Need 2 operands\n");
exit(0);
}
- if(!eval_expr(p->arg1))
+ if (!eval_expr(p->arg1))
gen_expr(p->arg1, NULL, NULL);
- if(!eval_expr(p->arg2))
+ if (!eval_expr(p->arg2))
gen_expr(p->arg2, NULL, NULL);
- if(invert) {
+ if (invert) {
op_inv = op_inverse(p->op);
inverse_label = false_label;
}
@@ -816,22 +816,22 @@
}
void coerce_operands(Type ** t1, Type ** t2) {
- if(!*t1 || !*t2) {
+ if (!*t1 || !*t2) {
fprintf(stderr, "Internal error: coerce_operands: NULL type(s)\n");
abort();
}
- if(*t1 == *t2)
+ if (*t1 == *t2)
return;
- if(*t1 == t_int32) {
- if(*t2 == t_float)
+ if (*t1 == t_int32) {
+ if (*t2 == t_float)
*t1 = t_float;
else {
printf("Can't coerce types (int, %s)\n", type_name(*t2));
exit(0);
}
}
- else if(*t1 == t_float) {
- if(*t2 == t_int32)
+ else if (*t1 == t_float) {
+ if (*t2 == t_int32)
*t2 = t_float;
else {
printf("Can't coerce types (float, %s)\n", type_name(*t2));
@@ -866,7 +866,7 @@
return ptr;
}
- switch(operator) {
+ switch (operator) {
case INC: return "inc";
case DEC: return "dec";
case LOGICAL_OR: return "||";
@@ -885,7 +885,7 @@
}
int op_inverse(int operator) {
- switch(operator) {
+ switch (operator) {
case LOGICAL_EQ: return LOGICAL_NE;
case LOGICAL_NE: return LOGICAL_EQ;
case '<': return LOGICAL_GTE;
@@ -925,20 +925,20 @@
/* Create a temporary rval */
Symbol * new_temp(Type * type) {
Symbol * s;
- if(!type) {
+ if (!type) {
abort();
}
/* Map a Cola type to a primitive type */
- if(type == t_int32 || type == t_uint32 || type == t_short
+ if (type == t_int32 || type == t_uint32 || type == t_short
|| type == t_ushort || type == t_char || type == t_byte
|| type == t_sbyte || type == t_bool)
s = new_identifier_symbol(new_itemp());
- else if(type == t_float || type == t_double || type == t_decimal)
+ else if (type == t_float || type == t_double || type == t_decimal)
s = new_identifier_symbol(new_ntemp());
- else if(type == t_string)
+ else if (type == t_string)
s = new_identifier_symbol(new_stemp());
- else if(type == t_object)
+ else if (type == t_object)
s = new_identifier_symbol(new_ptemp());
else {
fprintf(stderr, "Internal error: Can't map type %s to a primitive
type.\n", type->sym->name);
@@ -975,10 +975,10 @@
#endif
#if 0
- printf( "\t(%s)%s = %s %s %s\n", r->typename->name, r->name,
+ printf("\t(%s)%s = %s %s %s\n", r->typename->name, r->name,
NAME(a1), op, NAME(a2));
#endif
- printf( "\t%s = %s %s %s\n", r->name, NAME(a1), op, NAME(a2));
+ printf("\t%s = %s %s %s\n", r->name, NAME(a1), op, NAME(a2));
}
void emit_unary_expr(Symbol * res, Symbol * arg1, char * op) {
@@ -986,18 +986,18 @@
printf("#emit_unary_expr\n");
#endif
- if(op)
+ if (op)
#if 0
- printf( "\t(%s)%s = %s %s\n", res->typename->name, res->name,
+ printf("\t(%s)%s = %s %s\n", res->typename->name, res->name,
op, NAME(arg1));
#endif
- printf( "\t%s = %s %s\n", res->name, op, NAME(arg1));
+ printf("\t%s = %s %s\n", res->name, op, NAME(arg1));
else
#if 0
- printf( "\t(%s)%s = %s\n", res->typename->name, res->name,
+ printf("\t(%s)%s = %s\n", res->typename->name, res->name,
NAME(arg1));
#endif
- printf( "\t%s = %s\n", res->name,
+ printf("\t%s = %s\n", res->name,
NAME(arg1));
#if DEBUG
Modified: trunk/languages/cola/semant.c
==============================================================================
--- trunk/languages/cola/semant.c (original)
+++ trunk/languages/cola/semant.c Fri Mar 30 15:49:26 2007
@@ -21,8 +21,8 @@
* 3) Check for invalid assignments and operations
*/
void build_ast(AST * tree) {
- if(!tree) return;
- switch(tree->asttype) {
+ if (!tree) return;
+ switch (tree->asttype) {
case ASTT_CLASS_DECL:
build_class_decl(tree);
break;
@@ -35,21 +35,21 @@
}
void build_class_decl(AST * c) {
- if(!c) return;
+ if (!c) return;
#if DEBUG
fprintf(stderr, "Pass 2: class [%s]\n", c->sym->name);
#endif
push_namespace(c->sym);
- if(c->Attr.Class.body)
+ if (c->Attr.Class.body)
build_class_body(c->Attr.Class.body);
pop_namespace();
}
void build_class_body(AST * b) {
- if(!b) return;
+ if (!b) return;
fprintf(stderr, "Pass 2: class body\n");
- while(b) {
- switch(b->asttype) {
+ while (b) {
+ switch (b->asttype) {
case ASTT_CONSTANT_DECL:
build_field_decl(b);
break;
@@ -78,7 +78,7 @@
*/
void build_field_decl(AST * d) {
#if DEBUG
- if(d->asttype == ASTT_CONSTANT_DECL)
+ if (d->asttype == ASTT_CONSTANT_DECL)
fprintf(stderr, "Pass 2: class const [%s]\n", d->arg1->sym->name);
else
fprintf(stderr, "Pass 2: class field [%s]\n", d->arg1->sym->name);
@@ -98,21 +98,21 @@
fprintf(stderr, "build_method_decl\n");
#endif
push_scope();
- if(m->Attr.Method.params) {
+ if (m->Attr.Method.params) {
Symbol * s = m->Attr.Method.params;
- while(s) {
+ while (s) {
declare_local(s);
s = s->tnext;
}
}
- if(m->Attr.Method.body) {
+ if (m->Attr.Method.body) {
push_scope();
build_statement_list(m->Attr.Method.body);
m->Attr.Method.body->vars = pop_scope();
{
Symbol * s = m->vars;
fprintf(stderr, "\tPopped locals:\n");
- while(s) {
+ while (s) {
fprintf(stderr, "\t[%s]\n", s->name);
s = s->tnext;
}
@@ -131,7 +131,7 @@
*/
void build_var_decl(AST * d) {
#if DEBUG
- if(d->asttype == ASTT_CONSTANT_DECL)
+ if (d->asttype == ASTT_CONSTANT_DECL)
fprintf(stderr, "Pass 2: Local const [%s]\n", d->arg1->sym->name);
else
fprintf(stderr, "Pass 2: Local var [%s]\n", d->arg1->sym->name);
@@ -163,7 +163,7 @@
build_expr(c->arg1);
/* ELSE */
build_expr(c->arg2);
- if(c->arg1->type != c->arg2->type) {
+ if (c->arg1->type != c->arg2->type) {
fprintf(stderr, "Error: expression types not equivalent in ternary
expression\n");
exit(0);
}
@@ -182,7 +182,7 @@
void build_new_expr(AST * n) {
n->type = lookup_type_symbol(n->typename);
- if(!n->type) {
+ if (!n->type) {
fprintf(stderr, "Internal error: new called for unknown type [%s]\n",
n->typename->name);
abort();
@@ -200,13 +200,13 @@
}
void build_return(AST * r) {
- if(r->arg1)
+ if (r->arg1)
build_expr(r->arg1);
}
void build_expr_list(AST * e) {
AST * p = e;
- while(p) {
+ while (p) {
build_expr(p);
p = p->next;
}
@@ -216,8 +216,8 @@
#if DEBUG
fprintf(stderr, "build_expr\n");
#endif
- if(!e) return;
- switch(e->asttype) {
+ if (!e) return;
+ switch (e->asttype) {
case ASTT_LITERAL:
e->type = e->sym->type;
e->typename = e->sym->typename;
@@ -249,7 +249,7 @@
case ASTT_OP:
build_expr(e->arg1);
build_expr(e->arg2);
- if(e->arg1->type == t_string) {
+ if (e->arg1->type == t_string) {
/* Implicitly convert + operator on string types to
* . concat operator until the compiler can handle class
* operators.
@@ -274,7 +274,7 @@
abort();
}
- if(e->arg1) {
+ if (e->arg1) {
e->type = e->arg1->type;
e->typename = e->arg1->typename;
}
@@ -282,16 +282,16 @@
void build_statement_list(AST * s) {
static int statements;
- if(!s) return;
+ if (!s) return;
#if DEBUG
fprintf(stderr, "statements parsed [%d]\n", statements++);
#endif
- if(s->kind == KIND_EXPR) {
+ if (s->kind == KIND_EXPR) {
build_expr(s);
goto END;
}
- switch(s->asttype) {
+ switch (s->asttype) {
case ASTT_IF:
build_if(s);
break;
Modified: trunk/languages/cola/sym.c
==============================================================================
--- trunk/languages/cola/sym.c (original)
+++ trunk/languages/cola/sym.c Fri Mar 30 15:49:26 2007
@@ -37,7 +37,7 @@
/* Routines for managing symbols, attributes and AST tree */
void assert(void * p) {
- if(p == NULL) {
+ if (p == NULL) {
fprintf(stderr, "NULL pointer assertion.\n");
abort();
}
@@ -46,7 +46,7 @@
unsigned int hash_str(const char * str) {
unsigned long key = 0;
const char * s;
- for(s=str; *s; s++)
+ for (s=str; *s; s++)
key = key * 65599 + *s;
return key;
}
@@ -62,15 +62,15 @@
}
SymbolTable * new_symbol_table() {
- SymbolTable * tab = malloc(sizeof(SymbolTable));
+ SymbolTable * tab = malloc(sizeof (SymbolTable));
assert(tab);
- memset(tab, sizeof(SymbolTable), 0);
+ memset(tab, sizeof (SymbolTable), 0);
tab->scope = 1;
return tab;
}
Symbol * new_symbol(const char * name) {
- Symbol * s = malloc(sizeof(Symbol));
+ Symbol * s = malloc(sizeof (Symbol));
assert(s);
s->kind = 0;
s->name = str_dup(name);
@@ -161,7 +161,7 @@
}
AST * new_ast(enum ASTKIND kind, int asttype, AST * arg1, AST * arg2) {
- AST * ast = malloc(sizeof(AST));
+ AST * ast = malloc(sizeof (AST));
assert(ast);
ast->start_label = ast->end_label = NULL;
ast->kind = kind;
@@ -176,10 +176,10 @@
ast->vars = NULL;
ast->up = NULL;
ast->next = NULL;
- memset(&ast->Attr, 0, sizeof(ast->Attr));
- if(arg1)
+ memset(&ast->Attr, 0, sizeof (ast->Attr));
+ if (arg1)
arg1->up = ast;
- if(arg2)
+ if (arg2)
arg2->up = ast;
return ast;
}
@@ -197,12 +197,12 @@
/* "push" onto opposite end of temp stack */
void tunshift(Node ** list, Node * p) {
Node * l = *list;
- if(l && l == p) {
+ if (l && l == p) {
printf("Oops: Shifting node list onto itself!\n");
abort();
}
- if(l != NULL) {
- while(l->tnext)
+ if (l != NULL) {
+ while (l->tnext)
l = l->tnext;
l->tnext = p;
}
@@ -213,7 +213,7 @@
Node * pop(Node ** list) {
Node * top;
top = *list;
- if(*list)
+ if (*list)
*list = (*list)->next;
return top;
}
@@ -222,7 +222,7 @@
Node * tpop(Node ** list) {
Node * top;
top = *list;
- if(*list)
+ if (*list)
*list = (*list)->tnext;
return top;
}
@@ -244,12 +244,12 @@
/* "push" onto opposite end of temp stack */
void tunshift_sym(Symbol ** list, Symbol * p) {
Symbol * l = *list;
- if(l && l == p) {
+ if (l && l == p) {
printf("Oops: Shifting symbol list onto itself!\n");
abort();
}
- if(l != NULL) {
- while(l->tnext) {
+ if (l != NULL) {
+ while (l->tnext) {
l = l->tnext;
}
l->tnext = p;
@@ -263,7 +263,7 @@
Symbol * pop_sym(Symbol ** list) {
Symbol * top;
top = *list;
- if(*list)
+ if (*list)
*list = (*list)->next;
return top;
}
@@ -272,7 +272,7 @@
Symbol * tpop_sym(Symbol ** list) {
Symbol * top;
top = *list;
- if(*list)
+ if (*list)
*list = (*list)->tnext;
return top;
}
@@ -297,7 +297,7 @@
Symbol * ns;
ns = tpop_sym(&namespace_stack);
scope = scope_stack[--last_scope];
- if(last_scope < 0) {
+ if (last_scope < 0) {
fprintf(stderr, "Internal error: scope unbalanced, popped scope 0\n");
abort();
}
@@ -309,12 +309,12 @@
/* "push" onto opposite end of stack */
void unshift_ast(AST ** list, AST * p) {
AST * l = *list;
- if(l && l == p) {
+ if (l && l == p) {
printf("Oops: Shifting ast list onto itself!\n");
abort();
}
- if(l != NULL) {
- while(l->next)
+ if (l != NULL) {
+ while (l->next)
l = l->next;
l->next = p;
}
@@ -383,20 +383,20 @@
Symbol * l = NULL;
const char * p;
int len;
- if(!strstr(s, pattern))
+ if (!strstr(s, pattern))
return new_symbol(s);
p = s;
AGAIN:
- for(len = 0; p[len] && p[len] != c; len++)
+ for (len = 0; p[len] && p[len] != c; len++)
;
- if(len) {
+ if (len) {
Symbol * n = new_symbol("");
n->name = malloc(len+1);
strncpy(n->name, p, len);
n->name[len] = '\0';
tunshift_sym(&l, n);
}
- if(!len || !p[len])
+ if (!len || !p[len])
return l;
else {
len++;
@@ -415,22 +415,22 @@
#if DEBUG
fprintf(stderr, "lookup_symbol: %s split to (%s,...)\n", name, list->name);
#endif
- for(ns = current_namespace; ns; ) {
+ for (ns = current_namespace; ns; ) {
#if DEBUG
fprintf(stderr, "lookup_symbol: searching namespace[%s] for [%s]\n",
ns->name, list->name);
#endif
- if((s = lookup_symbol_in_tab(ns->table, list->name))) {
+ if ((s = lookup_symbol_in_tab(ns->table, list->name))) {
#if DEBUG
fprintf(stderr, "lookup_symbol: found [%s] in namespace[%s]\n",
list->name, ns->name);
#endif
- if(s->kind == IDENTIFIER) {
+ if (s->kind == IDENTIFIER) {
ns = s->type->sym;
}
else {
ns = s;
}
list = list->tnext;
- if(!list || !s)
+ if (!list || !s)
return s;
}
else {
@@ -444,13 +444,13 @@
Symbol * lookup_symbol_in_tab(SymbolTable * tab, const char * name) {
Symbol * sym_ptr;
unsigned int index = hash_str(name) % HASH_SIZE;
- if(!tab) {
+ if (!tab) {
fprintf(stderr, "Internal error: NULL symbol table\n");
fprintf(stderr, "while resolving [%s]\n", name);
abort();
}
- for(sym_ptr = tab->table[ index ]; sym_ptr; sym_ptr = sym_ptr->next) {
- if(!strcmp(name, sym_ptr->name))
+ for (sym_ptr = tab->table[ index ]; sym_ptr; sym_ptr = sym_ptr->next) {
+ if (!strcmp(name, sym_ptr->name))
return sym_ptr;
}
@@ -460,8 +460,8 @@
Symbol * lookup_namespace(SymbolTable * tab, const char * name) {
Symbol * sym_ptr;
unsigned int index = hash_str(name) % HASH_SIZE;
- for(sym_ptr = tab->table[ index ]; sym_ptr; sym_ptr = sym_ptr->next) {
- if(sym_ptr->kind == NAMESPACE && !strcmp(name, sym_ptr->name))
+ for (sym_ptr = tab->table[ index ]; sym_ptr; sym_ptr = sym_ptr->next) {
+ if (sym_ptr->kind == NAMESPACE && !strcmp(name, sym_ptr->name))
return sym_ptr;
}
@@ -471,8 +471,8 @@
Symbol * lookup_class(SymbolTable * tab, const char * name) {
Symbol * sym_ptr;
unsigned int index = hash_str(name) % HASH_SIZE;
- for(sym_ptr = tab->table[ index ]; sym_ptr; sym_ptr = sym_ptr->next) {
- if(sym_ptr->kind == CLASS && !strcmp(name, sym_ptr->name))
+ for (sym_ptr = tab->table[ index ]; sym_ptr; sym_ptr = sym_ptr->next) {
+ if (sym_ptr->kind == CLASS && !strcmp(name, sym_ptr->name))
return sym_ptr;
}
@@ -482,8 +482,8 @@
Symbol * lookup_symbol_scope(SymbolTable * tab, const char * name, int
scope_level) {
Symbol * sym_ptr;
unsigned int index = hash_str(name) % HASH_SIZE;
- for(sym_ptr = tab->table[ index ]; sym_ptr; sym_ptr = sym_ptr->next) {
- if(sym_ptr->scope == scope_level
+ for (sym_ptr = tab->table[ index ]; sym_ptr; sym_ptr = sym_ptr->next) {
+ if (sym_ptr->scope == scope_level
&& !strcmp(name, sym_ptr->name))
return sym_ptr;
}
@@ -495,11 +495,11 @@
unsigned int index = hash_str(sym->name) % HASH_SIZE;
#if 0
fprintf(stderr, "#store_symbol(%s)\n", sym->name);
- if(sym->table && sym->table == tab) {
+ if (sym->table && sym->table == tab) {
fprintf(stderr, "Internal error, namespace->symbol table loop.\n");
fprintf(stderr, "Symbol [%s] contains loopback namespace.\n",
sym->name);
abort();
- } else if(sym == global_namespace) {
+ } else if (sym == global_namespace) {
fprintf(stderr, "Internal error, can't store global namespace.\n");
abort();
}
@@ -543,9 +543,9 @@
void declare_local(Symbol * s) {
fprintf(stderr, "declare_local[%s]\n", s->name);
store_symbol(current_symbol_table, s);
- if(s->typename) {
+ if (s->typename) {
s->type = lookup_type_symbol(s->typename);
- if(!s->type) {
+ if (!s->type) {
fprintf(stderr, "declare_local: NULL type for ident [%s] typename
[%s]\n",
s->name, s->typename->name);
abort();
@@ -563,15 +563,15 @@
*/
void declare_field(Symbol * s) {
fprintf(stderr, "declare_field[%s]\n", s->name);
- if(!current_namespace->type || current_namespace->type->kind != CLASS) {
+ if (!current_namespace->type || current_namespace->type->kind != CLASS) {
fprintf(stderr, "Internal Error: field declarations only valid for
classes.\n");
fprintf(stderr, "Current namespace is [%s]\n",
current_namespace->name);
abort();
}
store_symbol(current_symbol_table, s);
- if(s->typename) {
+ if (s->typename) {
s->type = lookup_type_symbol(s->typename);
- if(!s->type) {
+ if (!s->type) {
fprintf(stderr, "declare_local: NULL type for ident [%s] typename
[%s]\n",
s->name, s->typename->name);
abort();
@@ -585,12 +585,12 @@
}
void dump_namespace(Symbol * ns) {
- if(ns->kind == CLASS) {
+ if (ns->kind == CLASS) {
printf("#<class %s>\n", ns->name);
- if(ns->table)
+ if (ns->table)
dump_symbol_table(ns->table);
printf("#</class>\n");
- } else if(ns->kind == NAMESPACE) {
+ } else if (ns->kind == NAMESPACE) {
printf("#<namespace %s>\n", ns->name);
dump_symbol_table(ns->table);
printf("#</namespace>\n");
@@ -601,13 +601,13 @@
Symbol * sym;
int i;
printf("# <symbol table>\n");
- for(i = 0; i < HASH_SIZE; i++) {
- for(sym = tab->table[i]; sym; sym = sym->next) {
- switch(sym->kind) {
+ for (i = 0; i < HASH_SIZE; i++) {
+ for (sym = tab->table[i]; sym; sym = sym->next) {
+ switch (sym->kind) {
case CLASS:
case NAMESPACE:
- if(sym->table) {
- if(sym->table == tab) {
+ if (sym->table) {
+ if (sym->table == tab) {
printf("Internal error, namespace->symbol
table loop.\n");
printf("Symbol [%s] contains loopback
namespace.\n", sym->name);
abort();
@@ -625,8 +625,8 @@
printf("#\tliteral: \"%s\"\n", sym->name);
break;
case TYPE: printf("#\ttype: \"%s\"\n", sym->name);
- if(sym->table) {
- if(sym->table == tab) {
+ if (sym->table) {
+ if (sym->table == tab) {
printf("Internal error, namespace->symbol
table loop.\n");
printf("Symbol [%s] contains loopback
namespace.\n", sym->name);
abort();
@@ -644,7 +644,7 @@
Symbol * check_id_redecl(SymbolTable * table, const char * name) {
Symbol * t;
- if((t = lookup_symbol_scope(table, name, scope)) != NULL) {
+ if ((t = lookup_symbol_scope(table, name, scope)) != NULL) {
printf("error (line %ld): identifier %s previously declared in this
scope, line %d.\n", line, name, t->line);
abort();
exit(0);
@@ -654,7 +654,7 @@
Symbol * check_id_decl(SymbolTable * table, const char * name) {
Symbol * t;
- if((t = lookup_symbol_in_tab(table, name)) == NULL)
+ if ((t = lookup_symbol_in_tab(table, name)) == NULL)
return NULL;
return t;
}
@@ -672,8 +672,8 @@
int i;
SymbolTable * tab = current_symbol_table;
Symbol * p = NULL;
- for(i = 0; i < HASH_SIZE; i++) {
- while(tab->table[i] && tab->table[i]->scope == scope) {
+ for (i = 0; i < HASH_SIZE; i++) {
+ while (tab->table[i] && tab->table[i]->scope == scope) {
Symbol * t;
#if DEBUG
printf("popping symbol %s: level %d\n", tab->table[i]->name,
scope);
@@ -686,7 +686,7 @@
}
}
- if(scope > 0)
+ if (scope > 0)
scope--;
else {
fprintf(stderr, "Internal error: can't pop scope 0.\n");
@@ -706,8 +706,8 @@
void discard_scope() {
int i;
SymbolTable * tab = current_symbol_table;
- for(i = 0; i < HASH_SIZE; i++) {
- while(tab->table[i] && tab->table[i]->scope == scope) {
+ for (i = 0; i < HASH_SIZE; i++) {
+ while (tab->table[i] && tab->table[i]->scope == scope) {
Symbol * t;
#ifdef DEBUG
printf("discarding symbol %s: level %d\n", tab->table[i]->name,
scope);
@@ -718,7 +718,7 @@
}
}
- if(scope > 0)
+ if (scope > 0)
scope--;
else {
fprintf(stderr, "Internal error: can't discard scope 0.\n");
@@ -737,13 +737,13 @@
}
AST * pop_primary_block() {
- if(primary_block > 0)
+ if (primary_block > 0)
return primary_block_stack[--primary_block];
return NULL;
}
AST * get_cur_primary_block() {
- if(primary_block > 0)
+ if (primary_block > 0)
return primary_block_stack[primary_block-1];
return NULL;
}
Modified: trunk/languages/cola/type.c
==============================================================================
--- trunk/languages/cola/type.c (original)
+++ trunk/languages/cola/type.c Fri Mar 30 15:49:26 2007
@@ -53,7 +53,7 @@
/* size is bytes or elements, depending on if type is variable or array */
Type * store_type(const char * name, int size) {
- Type * t = (Type *)malloc(sizeof(*t));
+ Type * t = (Type *)malloc(sizeof (*t));
Symbol * s = store_symbol(current_symbol_table, new_symbol(name));
t->type = NULL;
s->type = t;
@@ -75,10 +75,10 @@
Type * lookup_type(const char * name) {
Symbol * ns;
Symbol * s;
- for(ns = current_namespace; ns; ns = ns->tnext) {
+ for (ns = current_namespace; ns; ns = ns->tnext) {
s = lookup_symbol_in_tab(ns->table, name);
- if(s != NULL) {
- if(s->kind != TYPE) {
+ if (s != NULL) {
+ if (s->kind != TYPE) {
fprintf(stderr, "lookup_type(%s) : Error, symbol not a
type\n", name );
abort();
}
@@ -103,7 +103,7 @@
* to nested namespace.
*/
Type * lookup_type_symbol(Symbol * id) {
- if(!id) {
+ if (!id) {
fprintf(stderr, "lookup_type_symbol: NULL symbol\n");
abort();
}
@@ -115,7 +115,7 @@
}
Rank * new_rank(int dim) {
- Rank * ret = malloc(sizeof(*ret));
+ Rank * ret = malloc(sizeof (*ret));
ret->next = NULL;
ret->tnext = NULL;
ret->dim = dim;
@@ -123,7 +123,7 @@
}
Type * new_array_type(Symbol * typename, Symbol * sig) {
- Type * ret = malloc(sizeof(*ret));
+ Type * ret = malloc(sizeof (*ret));
ret->sym = symbol_concat(typename, sig);
ret->kind = TYPE_ARRAY;
ret->next = NULL;
@@ -132,11 +132,11 @@
ret->rank = rank;
{
Rank * r;
- for(ret->dim = 0, r = rank; r; r = (Rank *)r->tnext) {
+ for (ret->dim = 0, r = rank; r; r = (Rank *)r->tnext) {
ret->dim += r->dim;
}
}
- ret->bounds = (int **)malloc(sizeof(int) * ret->dim * 2);
+ ret->bounds = (int **)malloc(sizeof (int) * ret->dim * 2);
*/
return ret;
}
@@ -148,9 +148,9 @@
int i;
Symbol * ret = new_identifier_symbol("");
sprintf(buf, "%s", type_name(t->type));
- for(r = t->rank; r; r = (Rank *)r->tnext) {
+ for (r = t->rank; r; r = (Rank *)r->tnext) {
strcat(buf, "[");
- for(i = 0; i < r->dim; i++) {
+ for (i = 0; i < r->dim; i++) {
sprintf(buf + strlen(buf), "(0..)");
}
strcat(buf, "]");
@@ -163,7 +163,7 @@
void resolve_identifier(Symbol ** ps) {
Symbol * s = *ps;
Symbol * t;
- if(!s) {
+ if (!s) {
fprintf(stderr, "Internal error: resolve_identifier: NULL symbol\n");
abort();
}
@@ -171,26 +171,26 @@
fprintf(stderr, "!resolve[%s]\n", s->name);
#endif
t = lookup_symbol(s->name);
- if(!t) {
+ if (!t) {
fprintf(stderr, "Error: identifier [%s] undeclared.\n", s->name);
exit(0);
}
- if(s != t) {
+ if (s != t) {
*ps = t;
s = *ps;
}
- if(!s->typename) {
+ if (!s->typename) {
fprintf(stderr, "Internal error: identifier [%s] has no typename\n",
s->name);
abort();
}
- if(!s->type) {
+ if (!s->type) {
/* Resolve symbol */
s->type = lookup_type_symbol(s->typename);
- if(s->type) {
+ if (s->type) {
/* Just in case typename is fully qualified or aliased */
s->typename = t->typename;
/* Not sure if this is the cleanest way..
@@ -208,7 +208,7 @@
}
}
- if(s->type) {
+ if (s->type) {
s->table = s->type->sym->table;
}
}