summaryrefslogtreecommitdiff
path: root/minic/parse.c
blob: c23a4a25e146d00bcabf5508ed80e72da49867bd (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
#include "parse.h"

#include "io.h"
#include "string.h"
#include "stdlib.h"

void parser_init( Parser *p, Scanner *s )
{
	p->s = s;
}

void parser_done( Parser *p )
{
}

void parser_debug( Parser *p, int enable )
{
	p->debug = enable;
	
	if( p->debug ) {
		print( "PARSER DEBUGGING ENABLED" );
	}
}

static void print_symbol( scanner_Symbol sym )
{
	char s[MAX_TMP_LEN];
	char s2[MAX_TMP_LEN];
	
	s[0] = '\0';
	s2[0] = '\0';
	
	switch( sym.sym ) {
		case S_eof:
			print( "PARSER(EOF)" );
			break;
		case S_INT:
			print( "PARSER(INT)" );
			break;
		case S_VOID:
			print( "PARSER(VOID)" );
			break;
		case S_RETURN:
			print( "PARSER(RETURN)" );
			break;
		case S_DIV:
			print( "PARSER(DIV)" );
			break;
		case S_IDENT:
			strcat( s, "PARSER(IDENT," );
			strcat( s, sym.data.s );
			strcat_c( s, ')' );
			print( s );
			break;
		case S_INT_CONST:
			strcat( s, "PARSER(INT_CONST," );
			itoa( sym.data.i, s2, 10 );
			strcat( s, s2 );
			strcat_c( s, ')' );
			print( s );
			break;
		case S_SEMICOLON:
			print( "PARSER(SEMICOLON)" );
			break;
		case S_LPARENT:
			print( "PARSER(LPARENT)" );		
			break;
		case S_RPARENT:
			print( "PARSER(RPARENT)" );		
			break;
		case S_LCURL:
			print( "PARSER(LCURL)" );		
			break;
		case S_RCURL:
			print( "PARSER(RCURL)" );		
			break;
		default:
			print( "PARSER(<unknown symbol>)" );
	}
}

void parser_parse( Parser *p )
{
	scanner_Symbol s;

	s = scanner_scan( p->s );
	
	while( s.sym != S_eof ) {
		if( p->debug ) {
			print_symbol( s );
		}
		s = scanner_scan( p->s );
	}
}