aboutsummaryrefslogtreecommitdiff
path: root/unix/boot/xyacc/debug/dc.y
blob: 0d6fe655d61dac9d628775e5442240cbb6d453fc (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# SPP/Yacc specification for a simple desk calculator.  Input consists
# of simple arithmetic expressions; output is the value of the expression.
# Operands are restricted to integer and real numeric constants.

%{
include	<ctype.h>
include	<lexnum.h>

define	YYMAXDEPTH	150		# length of parser stack

task	dc = t_dc

# Operand Structure (parser stack)
define	YYOPLEN		2		# size of operand structure
define	OPTYPE		Memi[$1]	# operand datatype
define	OPVALI		Memi[$1+1]	# integer value of operand
define	OPVALR		Memr[$1+1]	# real value of operand

%}

%token	CONST LETTER YYEOF

%left	'+' '-'
%left	'*' '/'
%left	UMINUS

%%

prog	:	# Empty
	|	prog stmt eost {
			return (OK)
		    }
	|	YYEOF {
			return (EOF)
		    }
	|	prog error '\n' {
			yyerrok
		    }
	;

stmt	:	expr {
			# Print the value of an expression.
			if (OPTYPE($1) == TY_INT) {
			    call printf ("%d\n")
				call pargi (OPVALI($1))
			} else {
			    call printf ("%g\n")
				call pargr (OPVALR($1))
			}
		    }
	|	LETTER '=' expr {
			# Set the value of a register (from a-z).
			call putreg (OPVALI($1), $3)
		    }
	;

expr	:	'(' expr ')' {
			YYMOVE ($2, $$)
		    }
	|	expr '+' opnl expr {
			call binop ($1, $4, $$, '+')
		    }
	|	expr '-' opnl expr {
			call binop ($1, $4, $$, '-')
		    }
	|	expr '*' opnl expr {
			call binop ($1, $4, $$, '*')
		    }
	|	expr '/' opnl expr {
			call binop ($1, $4, $$, '/')
		    }
	|	'-' expr %prec UMINUS {
			call unop ($2, $$, '-')
		    }
	|	LETTER {
			call getreg (OPVALI($1), $$)
		    }
	|	CONST
	;

eost	:	';'
	|	'\n'
	;

opnl	:	# Empty
	|	opnl '\n'
	;

%%


# DC -- Main routine for the desk calculator.

procedure t_dc()

bool	debug
int	status
bool	clgetb()
int	yyparse()
extern	yylex()

begin
	debug = clgetb ("debug")

	repeat {
	    status = yyparse (STDIN, debug, yylex)
	    if (status == ERR)
		call eprintf ("syntax error")
	} until (status == EOF)
end


# BINOP -- Perform an arithmetic binary operation on two operands (passed
# by pointer), returning the result in a third.

procedure binop (a, b, c, operation)

pointer	a, b, c			# c = a op b
int	operation		# i.e., '+', '-', etc.
int	i, j, k
real	x, y, z

begin
	if (OPTYPE(a) == TY_INT && OPTYPE(b) == TY_INT) {
	    # Both operands are of type int, so return an integer result.

	    i = OPVALI(a)
	    j = OPVALI(b)

	    switch (operation) {
	    case '+':
		k = i + j
	    case '-':
		k = i - j
	    case '*':
		k = i * j
	    case '/':
		k = i / j
	    default:
		call error (1, "unknown binary operator")
	    }
	    OPVALI(c) = k
	    OPTYPE(c) = TY_INT

	} else {
	    # At least one of the two operands is a real.  Perform the
	    # calculation in type real, producing a real result.

	    if (OPTYPE(a) == TY_INT)
		x = OPVALI(a)
	    else
		x = OPVALR(a)
	    if (OPTYPE(b) == TY_INT)
		y = OPVALI(b)
	    else
		y = OPVALR(b)

	    switch (operation) {
	    case '+':
		z = x + y
	    case '-':
		z = x - y
	    case '*':
		z = x * y
	    case '/':
		z = x / y
	    default:
		call error (1, "unknown binary operator")
	    }

	    OPVALR(c) = z
	    OPTYPE(c) = TY_REAL
	}
end


# UNOP -- Perform a unary operation.  Since there is only one operand, the
# datatype does not change.

procedure unop (a, b, operation)

pointer	a, b
int	operation

begin
	OPTYPE(b) = OPTYPE(a)

	switch (operation) {
	case '-':
	    switch (OPTYPE(a)) {
	    case TY_INT:
		OPVALI(b) = -OPVALI(a)
	    case TY_REAL:
		OPVALR(b) = -OPVALR(a)
	    }
	default:
	    call error (2, "unknown unary operator")
	}
end


# GETREG, PUTREG -- Fetch or store the contents of a register variable.
# Registers are referred to by letter, A-Z or a-z.

define	MAXREG		('z'-'a'+1)


procedure getreg (regchar, op)

int	regchar
pointer	op

bool	store
int	regbuf[MAXREG*YYOPLEN]
int	reg, offset

begin
	store = false
	goto 10

entry	putreg (regchar, op)
	store = true

	# Compute offset into storage.  Structures are stored in buffer
	# by a binary copy, knowing only the length of the structure.
10	if (IS_UPPER(regchar))
	    reg = regchar - 'A' + 1
	else
	    reg = regchar - 'a' + 1
	reg = max(1, min(MAXREG, reg))
	offset = (reg-1) * YYOPLEN + 1

	# Copy the operand structure either in or out.
	if (store)
	    call amovi (Memi[op], regbuf[offset], YYOPLEN)
	else
	    call amovi (regbuf[offset], Memi[op], YYOPLEN)
end


# YYLEX -- Lexical input routine.  Return next token from the input
# stream.  Recognized tokens are CONST (numeric constants), LETTER,
# and the operator characters.

int procedure yylex (fd, yylval)

int	fd
pointer	yylval
char	ch, lbuf[SZ_LINE]
int	ip, nchars, token, junk
double	dval
int	lexnum(), getline(), gctod()
data	ip /0/

begin
	# Fetch a nonempty input line, or advance to start of next token
	# if within a line.  Newline is a token.
	repeat {
	    if (ip <= 0 || lbuf[ip] == EOS) {
		if (getline (fd, lbuf) == EOF) {
		    ip = 0
		    return (YYEOF)
		} else
		    ip = 1
	    }
	    while (IS_WHITE (lbuf[ip]))
		ip = ip + 1
	} until (lbuf[ip] != EOS)

	# Determine type of token.  If numeric constant, convert to binary
	# and return value in op structure (yylval).  If letter (register
	# variable) return value and advance input one char.  If any other
	# character, return char itself as the token, and advance input one
	# character.

	if (IS_DIGIT (lbuf[ip]))
	    token = lexnum (lbuf, ip, nchars)
	else
	    token = LEX_NONNUM

	switch (token) {
	case LEX_OCTAL, LEX_DECIMAL, LEX_HEX:
	    junk = gctod (lbuf, ip, dval)
	    OPTYPE(yylval) = TY_INT
	    OPVALI(yylval) = int (dval)
	    return (CONST)

	case LEX_REAL:
	    junk = gctod (lbuf, ip, dval)
	    OPTYPE(yylval) = TY_REAL
	    OPVALR(yylval) = dval
	    return (CONST)

	default:
	    ch = lbuf[ip]
	    ip = ip + 1
	    if (IS_ALPHA (ch)) {
		OPTYPE(yylval) = LETTER
		OPVALI(yylval) = ch
		return (LETTER)
	    } else {
		OPTYPE(yylval) = ch
		return (OPTYPE(yylval))
	    }
	}
end