summaryrefslogtreecommitdiff
path: root/appl/cmd/mathcalc.b
blob: 4f4f475f35df9f9b41b9064cc25c9f9744b0a2a5 (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
implement MathCalc;

include "sys.m";
	sys: Sys;

include "draw.m";

include "tk.m";
	tk: Tk;

include "bufio.m";
	bufmod : Bufio;
Iobuf : import bufmod;

include "../lib/tcl.m";

include "tcllib.m";


MathCalc : module 
{
	init: fn(nil: ref Draw->Context, nil: list of string);
};

CALCPATH: con "/dis/lib/tcl_calc.dis";

init(nil: ref Draw->Context, nil: list of string)
{
	sys = load Sys Sys->PATH;
	cal := load TclLib CALCPATH;
	if (cal==nil){
		sys->print("mathcalc: can't load %s: %r\n", CALCPATH);
		exit;
	}
	bufmod = load Bufio Bufio->PATH;
	if (bufmod==nil){
		sys->print("bufmod load %r\n");
		exit;
	}
	iob := bufmod->fopen(sys->fildes(0),bufmod->OREAD);
	if (iob==nil){
		sys->print("mathcalc: cannot open stdin for reading: %r\n");
		return;
	}
	input : string;
	new_inp := "calc%";
	sys->print("%s ", new_inp);
	while((input=iob.gets('\n'))!=nil){
		input=input[0:len input -1];
		if (input=="quit")
			exit;
		arr:=array[] of {input};
		(i,msg):=cal->exec(nil,arr);
		if (msg!=nil)
			sys->print("%s\n",msg);
		sys->print("%s ", new_inp);
	}
	
}


# expr0 : expr1
#	| expr0 '+' expr0
#	| expr0 '-' expr0
#	;
#
# expr1 : expr2
#	| expr1 '*' expr1
#	| expr1 '/' expr1
#	;
#
# expr2 : '-' expr2
#	| '+' expr2
#	| expr3
#	;
#
# expr3 : INT
#	| REAL
#	;