aboutsummaryrefslogtreecommitdiff
path: root/unix/boot/spp/rpp/rpprat/parse.r
blob: 676ee7591dd166d0c5922826fdc595fb4ac21bb8 (plain) (blame)
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
include  defs

# PARSE - parse Ratfor source program

subroutine parse

include COMMON_BLOCKS
character lexstr(MAXTOK)
integer lab, labval(MAXSTACK), lextyp(MAXSTACK), sp, token, i, t
integer lex
logical	push_stack

	sp = 1
	lextyp(1) = EOF

	for (token = lex(lexstr);  token != EOF;  token = lex(lexstr)) {
	    push_stack = .false.

	    switch (token) {
	    case LEXIF:
		call ifcode (lab)
		push_stack = .true.
	    case LEXIFERR:
		call iferrc (lab, 1)
		push_stack = .true.
	    case LEXIFNOERR:
		call iferrc (lab, 0)
		push_stack = .true.
	    case LEXDO:
		call docode (lab)
		push_stack = .true.
	    case LEXWHILE:
		call whilec (lab)
		push_stack = .true.
	    case LEXFOR:
		call forcod (lab)
		push_stack = .true.
	    case LEXREPEAT:
		call repcod (lab)
		push_stack = .true.
	    case LEXSWITCH:
		call swcode (lab)
		push_stack = .true.
	    case LEXCASE, LEXDEFAULT:
		for (i=sp;  i > 0;  i=i-1)	# find for most recent switch
		    if (lextyp(i) == LEXSWITCH)
		       break
		if (i == 0)
		    call synerr ("illegal case or default.")
		else
		    call cascod (labval (i), token)
	    case LEXDIGITS:
		call labelc (lexstr)
		push_stack = .true.
	    case LEXELSE:
		t = lextyp(sp)
		if (t == LEXIF | t == LEXIFERR | t == LEXIFNOERR)
		    call elseif (labval(sp))
		else
		    call synerr ("Illegal else.")

		t = lex (lexstr)			# check for "else if"
		call pbstr (lexstr)
		if (t == LEXIF | t == LEXIFERR | t == LEXIFNOERR) {
		    call indent (-1)			# cancel out indent +1
		    token = LEXIFELSE			# prevent -indent at end
		}
		push_stack = .true.
	    case LEXTHEN:
		if (lextyp(sp) == LEXIFERR | lextyp(sp) == LEXIFNOERR) {
		    call thenco (lextyp(sp), labval(sp))
		    lab = labval(sp)
		    token = lextyp(sp)
		    sp = sp - 1			# cancel out subsequent push
		} else
		    call synerr ("Illegal 'then' clause in iferr statement.")
		push_stack = .true.
	    case LEXLITERAL:
		call litral
	    case LEXERRCHK:
		call errchk
	    case LEXBEGIN:
		call beginc
	    case LEXEND:
		call endcod (lexstr)
		if (sp != 1) {
		    call synerr ("Missing right brace or 'begin'.")
		    sp = 1
		}
	    default:
		if (token == LBRACE)
		    push_stack = .true.
		else if (token == LEXDECL)
		    call declco (lexstr)
	    }

	    if (push_stack) {
		if (body == NO) {
		    call synerr ("Missing 'begin' keyword.")
		    call beginc
		}
		sp = sp + 1				# beginning of statement
		if (sp > MAXSTACK)
		    call baderr ("Stack overflow in parser.")
		lextyp(sp) = token			# stack type and value
		labval(sp) = lab

	    } else if (token != LEXCASE & token != LEXDEFAULT) {
		if (token == RBRACE)
		    token = LEXRBRACE

		switch (token) {
		case LEXOTHER:
		    call otherc (lexstr)
		case LEXBREAK, LEXNEXT:
		    call brknxt (sp, lextyp, labval, token)
		case LEXRETURN:
		    call retcod
		case LEXGOTO:
		    call gocode
		case LEXSTRING:
		    if (body == NO)
			call strdcl
		    else
			call otherc (lexstr)
		case LEXRBRACE:
		    if (lextyp(sp) == LBRACE)
			sp = sp - 1
		    else if (lextyp(sp) == LEXSWITCH) {
			call swend (labval(sp))
			sp = sp - 1
		    } else
		       call synerr ("Illegal right brace.")
		}

		token = lex (lexstr)	   		# peek at next token
		call pbstr (lexstr)
		call unstak (sp, lextyp, labval, token)
	    }
	}

	if (sp != 1)
	    call synerr ("unexpected EOF.")
end