diff options
Diffstat (limited to 'src/check.c')
| -rw-r--r-- | src/check.c | 125 | 
1 files changed, 74 insertions, 51 deletions
| diff --git a/src/check.c b/src/check.c index 54929a1..498fcf8 100644 --- a/src/check.c +++ b/src/check.c @@ -42,7 +42,8 @@ static int analyze_list(struct state *state, struct scope *scope, struct ast *l)  	return 0;  } -static struct ast *file_scope_find_analyzed(struct state *state, struct scope *scope, char *id) +static struct ast *file_scope_find_analyzed(struct state *state, +                                            struct scope *scope, char *id)  {  	struct ast *exists = file_scope_find(scope, id);  	if (!exists) @@ -74,7 +75,8 @@ static int analyze_type(struct scope *scope, struct ast *n)  	return -1;  } -static int analyze_func_def(struct state *state, struct scope *scope, struct ast *f) +static int analyze_func_def(struct state *state, struct scope *scope, +                            struct ast *f)  {  	UNUSED(state);  	if (analyze_type(scope, f)) @@ -97,7 +99,8 @@ static int analyze_func_def(struct state *state, struct scope *scope, struct ast  	return analyze(&func_state, func_scope, func_body(f));  } -static int analyze_proc_def(struct state *state, struct scope *scope, struct ast *p) +static int analyze_proc_def(struct state *state, struct scope *scope, +                            struct ast *p)  {  	UNUSED(state);  	if (analyze_type(scope, p)) @@ -129,7 +132,8 @@ static int analyze_proc_def(struct state *state, struct scope *scope, struct ast  	return 0;  } -static int analyze_var_def(struct state *state, struct scope *scope, struct ast *n) +static int analyze_var_def(struct state *state, struct scope *scope, +                           struct ast *n)  {  	if (scope_add_var(scope, n))  		return -1; @@ -142,7 +146,8 @@ static int analyze_var_def(struct state *state, struct scope *scope, struct ast  	return 0;  } -static int analyze_formal_def(struct state *state, struct scope *scope, struct ast *n) +static int analyze_formal_def(struct state *state, struct scope *scope, +                              struct ast *n)  {  	UNUSED(state);  	if (analyze_type(scope, n)) @@ -162,9 +167,9 @@ static int analyze_neg(struct state *state, struct scope *scope, struct ast *n)  	if (expr->t != TYPE_INT) {  		semantic_error(scope, n, -				"negation requires %s, got %s\n", -				type_str(TYPE_INT), -				type_str(expr->t)); +		               "negation requires %s, got %s\n", +		               type_str(TYPE_INT), +		               type_str(expr->t));  		return -1;  	} @@ -180,9 +185,9 @@ static int analyze_pos(struct state *state, struct scope *scope, struct ast *p)  	if (expr->t != TYPE_INT) {  		semantic_error(scope, p, -				"unary pos requires %s, got %s\n", -				type_str(TYPE_INT), -				type_str(expr->t)); +		               "unary pos requires %s, got %s\n", +		               type_str(TYPE_INT), +		               type_str(expr->t));  		return -1;  	} @@ -190,7 +195,8 @@ static int analyze_pos(struct state *state, struct scope *scope, struct ast *p)  	return 0;  } -static int analyze_const_date(struct state *state, struct scope *scope, struct ast *d) +static int analyze_const_date(struct state *state, struct scope *scope, +                              struct ast *d)  {  	UNUSED(state);  	UNUSED(scope); @@ -199,7 +205,8 @@ static int analyze_const_date(struct state *state, struct scope *scope, struct a  	return 0;  } -static int analyze_const_int(struct state *state, struct scope *scope, struct ast *i) +static int analyze_const_int(struct state *state, struct scope *scope, +                             struct ast *i)  {  	UNUSED(state);  	UNUSED(scope); @@ -207,7 +214,8 @@ static int analyze_const_int(struct state *state, struct scope *scope, struct as  	return 0;  } -static int analyze_const_string(struct state *state, struct scope *scope, struct ast *s) +static int analyze_const_string(struct state *state, struct scope *scope, +                                struct ast *s)  {  	UNUSED(state);  	UNUSED(scope); @@ -227,8 +235,8 @@ static int analyze_eq(struct state *state, struct scope *scope, struct ast *e)  	if (r->t != l->t) {  		semantic_error(scope, e, "type mismatch: %s vs %s\n", -				type_str(l->t), -				type_str(r->t)); +		               type_str(l->t), +		               type_str(r->t));  		return -1;  	} @@ -248,8 +256,8 @@ static int analyze_lt(struct state *state, struct scope *scope, struct ast *e)  	if (r->t != l->t) {  		semantic_error(scope, e, "type mismatch: %s vs %s\n", -				type_str(l->t), -				type_str(r->t)); +		               type_str(l->t), +		               type_str(r->t));  		return -1;  	} @@ -308,7 +316,7 @@ static int analyze_sub(struct state *state, struct scope *scope, struct ast *s)  	}  	semantic_error(scope, s, "illegal subtraction types: %s, %s", -			type_str(l->t), type_str(r->t)); +	               type_str(l->t), type_str(r->t));  	return -1;  } @@ -320,7 +328,7 @@ static int analyze_mul(struct state *state, struct scope *scope, struct ast *m)  	if (l->t != TYPE_INT) {  		semantic_error(scope, l, "expected %s, got %s", -				type_str(TYPE_INT), type_str(l->t)); +		               type_str(TYPE_INT), type_str(l->t));  		return -1;  	} @@ -330,7 +338,7 @@ static int analyze_mul(struct state *state, struct scope *scope, struct ast *m)  	if (r->t != TYPE_INT) {  		semantic_error(scope, r, "expected %s, got %s", -				type_str(TYPE_INT), type_str(r->t)); +		               type_str(TYPE_INT), type_str(r->t));  		return -1;  	} @@ -346,7 +354,7 @@ static int analyze_div(struct state *state, struct scope *scope, struct ast *d)  	if (l->t != TYPE_INT) {  		semantic_error(scope, l, "expected %s, got %s", -				type_str(TYPE_INT), type_str(l->t)); +		               type_str(TYPE_INT), type_str(l->t));  		return -1;  	} @@ -356,7 +364,7 @@ static int analyze_div(struct state *state, struct scope *scope, struct ast *d)  	if (r->t != TYPE_INT) {  		semantic_error(scope, r, "expected %s, got %s", -				type_str(TYPE_INT), type_str(r->t)); +		               type_str(TYPE_INT), type_str(r->t));  		return -1;  	} @@ -364,7 +372,8 @@ static int analyze_div(struct state *state, struct scope *scope, struct ast *d)  	return 0;  } -static int analyze_print(struct state *state, struct scope *scope, struct ast *p) +static int analyze_print(struct state *state, struct scope *scope, +                         struct ast *p)  {  	struct ast *items = print_items(p);  	struct ast *fixups = NULL, *last_fixup = NULL; @@ -395,7 +404,8 @@ static int analyze_print(struct state *state, struct scope *scope, struct ast *p  	return 0;  } -static int analyze_return(struct state *state, struct scope *scope, struct ast *r) +static int analyze_return(struct state *state, struct scope *scope, +                          struct ast *r)  {  	struct ast *parent = state->parent;  	if (!parent) { @@ -405,7 +415,8 @@ static int analyze_return(struct state *state, struct scope *scope, struct ast *  	if (!has_type(parent)) { -		semantic_error(scope, r, "stray return in proc without return type"); +		semantic_error(scope, r, +		               "stray return in proc without return type");  		return -1;  	} @@ -415,7 +426,7 @@ static int analyze_return(struct state *state, struct scope *scope, struct ast *  	if (expr->t != parent->t) {  		semantic_error(scope, r, "return type mismatch: %s vs %s", -				type_str(parent->t), type_str(expr->t)); +		               type_str(parent->t), type_str(expr->t));  		return -1;  	} @@ -449,7 +460,7 @@ static int analyze_dot(struct state *state, struct scope *scope, struct ast *d)  	if (base->t != TYPE_DATE) {  		semantic_error(scope, d, "expected %d, got %d", -				type_str(TYPE_DATE), type_str(base->t)); +		               type_str(TYPE_DATE), type_str(base->t));  		return -1;  	} @@ -474,8 +485,8 @@ static int analyze_attr(struct state *state, struct scope *scope, struct ast *d)  		return -1;  	if (base->t != TYPE_DATE) { -		semantic_error(scope, d, "expected %d, got %d", -				type_str(TYPE_DATE), type_str(base->t)); +		semantic_error(scope, d, "expected %s, got %s", +		               type_str(TYPE_DATE), type_str(base->t));  		return -1;  	} @@ -501,7 +512,8 @@ static int analyze_attr(struct state *state, struct scope *scope, struct ast *d)  	return -1;  } -static int analyze_assign(struct state *state, struct scope *scope, struct ast *n) +static int analyze_assign(struct state *state, struct scope *scope, +                          struct ast *n)  {  	struct ast *l = assign_l(n);  	if (analyze(state, scope, l)) @@ -513,7 +525,7 @@ static int analyze_assign(struct state *state, struct scope *scope, struct ast *  	if (l->t != r->t) {  		semantic_error(scope, n, "type mismatch: %s vs %s", -				type_str(l->t), type_str(r->t)); +		               type_str(l->t), type_str(r->t));  		return -1;  	} @@ -521,12 +533,13 @@ static int analyze_assign(struct state *state, struct scope *scope, struct ast *  	return 0;  } -static int analyze_today(struct state *state, struct scope *scope, struct ast *c) +static int analyze_today(struct state *state, struct scope *scope, +                         struct ast *c)  {  	UNUSED(state);  	if (ast_list_len(func_call_args(c)) != 0) {  		semantic_error(scope, c, "expected 0 arguments, got %zd", -				ast_list_len(func_call_args(c))); +		               ast_list_len(func_call_args(c)));  		return -1;  	} @@ -535,9 +548,11 @@ static int analyze_today(struct state *state, struct scope *scope, struct ast *c  	return 0;  } -static int analyze_proc_call(struct state *state, struct scope *scope, struct ast *c) +static int analyze_proc_call(struct state *state, struct scope *scope, +                             struct ast *c)  { -	struct ast *exists = file_scope_find_analyzed(state, scope, proc_call_id(c)); +	struct ast *exists = file_scope_find_analyzed(state, scope, +	                                              proc_call_id(c));  	if (!exists || exists->k != AST_PROC_DEF) {  		semantic_error(scope, c, "no such proc");  		return -1; @@ -550,14 +565,15 @@ static int analyze_proc_call(struct state *state, struct scope *scope, struct as  	struct ast *formal = proc_formals(exists);  	if (ast_list_len(formal) != ast_list_len(args)) {  		semantic_error(scope, c, "expected %s args, got %s", -				ast_list_len(formal), ast_list_len(args)); +		               ast_list_len(formal), ast_list_len(args));  		return -1;  	}  	foreach_node(a, args) {  		if (a->t != formal->t) {  			semantic_error(scope, c, "expected %s, got %s", -					type_str(formal->t), type_str(a->t)); +			               type_str(formal->t), type_str(a->t)); +			return -1;  		}  		formal = formal->n; @@ -567,13 +583,15 @@ static int analyze_proc_call(struct state *state, struct scope *scope, struct as  	return 0;  } -static int analyze_func_call(struct state *state, struct scope *scope, struct ast *c) +static int analyze_func_call(struct state *state, struct scope *scope, +                             struct ast *c)  {  	/* handle special Today() built-in */  	if (same_id(func_call_id(c), "Today"))  		return analyze_today(state, scope, c); -	struct ast *exists = file_scope_find_analyzed(state, scope, func_call_id(c)); +	struct ast *exists = file_scope_find_analyzed(state, scope, +	                                              func_call_id(c));  	if (!exists || exists->k != AST_FUNC_DEF) {  		semantic_error(scope, c, "no such func");  		return -1; @@ -585,15 +603,16 @@ static int analyze_func_call(struct state *state, struct scope *scope, struct as  	struct ast *formal = func_formals(exists);  	if (ast_list_len(formal) != ast_list_len(args)) { -		semantic_error(scope, c, "expected %s args, got %s", -				ast_list_len(formal), ast_list_len(args)); +		semantic_error(scope, c, "expected %zd args, got %zd", +		               ast_list_len(formal), ast_list_len(args));  		return -1;  	}  	foreach_node(a, args) {  		if (a->t != formal->t) {  			semantic_error(scope, c, "expected %s, got %s", -					type_str(formal->t), type_str(a->t)); +			               type_str(formal->t), type_str(a->t)); +			return -1;  		}  		formal = formal->n; @@ -603,7 +622,8 @@ static int analyze_func_call(struct state *state, struct scope *scope, struct as  	return 0;  } -static int analyze_until(struct state *state, struct scope *scope, struct ast *n) +static int analyze_until(struct state *state, struct scope *scope, +                         struct ast *n)  {  	struct scope *until_scope = create_scope();  	scope_add_scope(scope, until_scope); @@ -618,7 +638,7 @@ static int analyze_until(struct state *state, struct scope *scope, struct ast *n  	if (cond->t != TYPE_BOOL) {  		semantic_error(scope, cond, "expected %s, got %s", -				type_str(TYPE_BOOL), type_str(cond->t)); +		               type_str(TYPE_BOOL), type_str(cond->t));  		return -1;  	} @@ -626,7 +646,8 @@ static int analyze_until(struct state *state, struct scope *scope, struct ast *n  	return 0;  } -static int analyze_unless(struct state *state, struct scope *scope, struct ast *n) +static int analyze_unless(struct state *state, struct scope *scope, +                          struct ast *n)  {  	struct scope *unless_scope = create_scope();  	struct scope *otherwise_scope = create_scope(); @@ -643,7 +664,7 @@ static int analyze_unless(struct state *state, struct scope *scope, struct ast *  	if (cond->t != TYPE_BOOL) {  		semantic_error(scope, cond, "expected %s, got %s", -				type_str(TYPE_BOOL), type_str(cond->t)); +		               type_str(TYPE_BOOL), type_str(cond->t));  		return -1;  	} @@ -655,7 +676,8 @@ static int analyze_unless(struct state *state, struct scope *scope, struct ast *  	return 0;  } -static int analyze_unless_expr(struct state *state, struct scope *scope, struct ast *n) +static int analyze_unless_expr(struct state *state, struct scope *scope, +                               struct ast *n)  {  	struct ast *body = unless_expr_body(n);  	if (analyze(state, scope, body)) @@ -667,7 +689,7 @@ static int analyze_unless_expr(struct state *state, struct scope *scope, struct  	if (cond->t != TYPE_BOOL) {  		semantic_error(scope, cond, "expected %s, got %s", -				type_str(TYPE_BOOL), type_str(cond->t)); +		               type_str(TYPE_BOOL), type_str(cond->t));  		return -1;  	} @@ -677,7 +699,7 @@ static int analyze_unless_expr(struct state *state, struct scope *scope, struct  	if (body->t != otherwise->t) {  		semantic_error(scope, n, "type mismatch: %s vs %s", -				type_str(body->t), type_str(otherwise->t)); +		               type_str(body->t), type_str(otherwise->t));  		return -1;  	} @@ -714,7 +736,8 @@ static int analyze(struct state *state, struct scope *scope, struct ast *n)  	case AST_POS: ret = analyze_pos(state, scope, n); break;  	case AST_CONST_DATE: ret = analyze_const_date(state, scope, n); break;  	case AST_CONST_INT: ret = analyze_const_int(state, scope, n); break; -	case AST_CONST_STRING: ret = analyze_const_string(state, scope, n); break; +	case AST_CONST_STRING: ret = analyze_const_string(state, scope, n); +		break;  	case AST_EQ: ret = analyze_eq(state, scope, n); break;  	case AST_LT: ret = analyze_lt(state, scope, n); break;  	case AST_ADD: ret = analyze_add(state, scope, n); break; | 
