aboutsummaryrefslogtreecommitdiff
path: root/sem_check.c
blob: ae1fa4c5a4b6cb0dd2e2dc3d53dad6c5ffe97248 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <assert.h>
#include <stdio.h>

#include "node.h"
#include "scope.h"
#include "tree.h"
#include "y.tab.h"
#include "pc.h"
#include "sem_check.h"

void check_id(s, n)
scope *s;
char *n;
{
	char buf[100];

	if (scope_search(s, n)) {
		snprintf(buf, 100, "\"%s\" already defined in scope...\n", n);
		yyerror(buf);
	}
}

node* check_exists(s, n)
scope *s;
char *n;
{
	node *tmp;
	char buf[100];

	if(!(tmp = scope_search(s,n))) {
		snprintf(buf, 100, "Cannot find \"%s\"\n", n);
		yyerror(buf);
	}

	return tmp;
}

int check_ret_type(t)
ptree *t;
{
	char buf[100];
	int type;

	if (!t)
	printf("TYPE: %d\n", t->type);

	switch (t->type) {
	case ID:
		if (!(t->attr.nval))
			yyerror("Missing ID\n");

		return t->attr.nval->var_type;

	case ADDOP :
	case MULOP :
	case RELOP :
		if (!(t->r && t->l))
			yyerror("Missing nodes\n");

		if (t->r->ret_type == t->l->ret_type)
			return BOOL;
		else
			yyerror("Misssing nodes\n");
		break;
	case NOT:
		if (t->ret_type == BOOL)
			return BOOL;
		yyerror("NOT needs bool input\n");
		break;
	case INUM:
		printf("INUM: %d", t->attr.ival);
		return INT;
	case RNUM:
		return REAL;
	case ASSIGNOP:
		fprintf(stderr, "HERE\n");
		if (!(t->r && t->l && t->r->attr.nval))
			yyerror("Incomplete parse tree\n");

		if (t->l->attr.nval->var_type == t->r->ret_type)
			return 0;
		else {
			snprintf(buf, 100, "Mismached types: "
					"Cannot assign type %s "
					"to variable \"%s\" of type %s\n",
					pretty_type(t->r->ret_type),
					t->l->attr.nval->name,
					pretty_type(t->l->attr.nval->var_type));
			yyerror(buf);
		}


		break;
	case ARRAY_ACCESS:
		if (!(t->r && t->l && t->l->attr.nval))
			yyerror("Incorrect Array Access\n");

		type = t->l->attr.nval -> var_type;
		if (type == ARRAY - INT || type == ARRAY - REAL)
			return 0;
		break;
	default:
		return -200;
		snprintf(buf, 101, "Unknown tree node: %d...\n", t->type);
		yyerror(buf);
	}


	return -1;

}