aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: dba3b7f0a24f71b0916f0b729176b72b929be2ed (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

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

extern char *yytext;
extern int line_num;

scope *cur_scope;

char* pretty_type(t)
int t;
{
	if (t == ARRAY) {
		return "ARRAY";
	} else if (t == INT - ARRAY) {
		return "ARRAY of INT";
	} else if (t == INT - ARRAY) {
		return "ARRAY of INT";
	} else if (t == INT) {
		return "INT";
	} else if (t == REAL) {
		return "REAL";
	} else if (t == BOOL) {
		return "BOOL";
	} else if (t == -1) {
		return "NO TYPE";
	} else {
		return "Unknown type";
	}
}

void debug_print(d, y)
int d;
union YYSTYPE* y;
{
#ifdef DEBUG
	if (d == PROG){
		fprintf(stderr, "[PROG]");
	} else if (d == VAR){
		fprintf(stderr, "[VAR]");
	} else if (d == PROC){
		fprintf(stderr, "[PROC]");
	} else if (d == FUNC){
		fprintf(stderr, "[FUNC]");
	} else if (d == BEG){
		fprintf(stderr, "[BEG]");
	} else if (d == END){
		fprintf(stderr, "[END]");
	} else if (d == ARRAY){
		fprintf(stderr, "[ARRAY]");
	} else if (d == OF){
		fprintf(stderr, "[OF]");
	} else if (d == INT){
		fprintf(stderr, "[INT]");
	} else if (d == REAL){
		fprintf(stderr, "[REAL]");
	} else if (d == IF){
		fprintf(stderr, "[IF]");
	} else if (d == THEN){
		fprintf(stderr, "[THEN]");
	} else if (d == ELSE){
		fprintf(stderr, "[ELSE]");
	} else if (d == WHILE){
		fprintf(stderr, "[WHILE]");
	} else if (d == DO){
		fprintf(stderr, "[DO]");
	} else if (d == FOR){
		fprintf(stderr, "[FOR]");
	} else if (d == TO){
		fprintf(stderr, "[TO]");
	} else if (d == DT){
		fprintf(stderr, "[DOWNTO]");
	} else if (d == NOT){
		fprintf(stderr, "[NOT]");
	} else if (d == DOTS){
		fprintf(stderr, "[ .. ]");
	} else if (d == ASSIGNOP){
		fprintf(stderr, "[ASSIGN]");
	} else if (d == RELOP){
		fprintf(stderr, "[RELOP:");
		if (y->opval == EQ){
			fprintf(stderr, "EQ");
		} else if (y->opval == NE){
			fprintf(stderr, "NE");
		} else if (y->opval == LT){
			fprintf(stderr, "LT");
		} else if (y->opval == LE){
			fprintf(stderr, "LE");
		} else if (y->opval == GT){
			fprintf(stderr, "GT");
		} else if (y->opval == GE){
			fprintf(stderr, "GE");
		} else if (y->opval == ADD){
			fprintf(stderr, "ADD");
		} else if (y->opval == SUB){
			fprintf(stderr, "SUB");
		} else if (y->opval == OR){
			fprintf(stderr, "OR");
		} else if (y->opval == MUL){
			fprintf(stderr, "MUL");
		} else if (y->opval == DIV){
			fprintf(stderr, "DIV");
		} else if (y->opval == AND){
			fprintf(stderr, "AND");
		}
		fprintf(stderr,"]");
	} else if (d == INUM) {
		fprintf(stderr, "[INUM: %d]", y->ival);
	} else if (d == RNUM) {
		fprintf(stderr, "[INUM: %f]", y->rval);
	} else if (d == ID) {
		fprintf(stderr, "[ID: %s]", y->sval);
	} else if (d == '\n') {
		fprintf(stderr, "\n");
	} else {
		fprintf(stderr, "{%c}", d);
	}
	fprintf(stderr, " ");
#endif
	return;
}

int yyerror(msg)
char* msg;
{
	fprintf(stderr, "\nError, line %d: %s\n", line_num, msg);
#ifdef DEBUG
	fprintf(stderr, "%s\n", yytext);
	exit(1);
#endif
	return 0;
}

int main()
{
	cur_scope = mkscope();
	assert(cur_scope);

	yyparse();

	return 0;
}