summaryrefslogtreecommitdiff
path: root/rpn.c
blob: 010afde8a986bd73d6d11513f66eecf013cf3375 (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
#include <u.h>
#include <libc.h>
#include <ctype.h>
#include <bio.h>

int stk[256], sp;
Biobuf *bin;

void
push(int n)
{
	if(sp >= sizeof stk)
		sp = sizeof(stk)-1;
	fprint(2, "push [%d] %d\n", sp, n);
	stk[sp++] = n;
}

int
pop(void)
{
	if(sp <= 0)
		sp = 1;
	fprint(2, "pop [%d] %d\n", sp-1, stk[sp-1]);
	return stk[--sp];
}

void
main()
{
	int x;
	char c;

	bin = Bfdopen(0, OREAD);
	if(bin == nil)
		sysfatal("Bfdopen: %r");
	while((c = Bgetc(bin)) != Beof){
		x = 0;
		switch(c){
		case '+': x = pop() + pop(); break;
		case '*': x = pop() * pop(); break;
		default:
			while(isdigit(c)){
				x = x*10 + c-'0';
				c = Bgetc(bin);
			}
		}
		push(x);
	}
	print("%d\n", pop());
	exits(nil);
}