summaryrefslogtreecommitdiff
path: root/main.c
blob: 9acd56886164a19e3a1c331bb19962d25da63dbb (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
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <bio.h>
#include <pool.h>

#include "apl9.h"

Biobuf *stdin;

void
threadmain(int argc, char *argv[])
{
	int off = 0;
	stdin = Bfdopen(0, OREAD);
	initthreads();
	initsymtab();
	initquadnames();
	traceeval = 0;
	debugmem = 0;

	ARGBEGIN{
	case 't':
			traceeval = 1;
			break;
	case 'm':
			debugmem = 1;
			mainmem->flags |= POOL_NOREUSE;
			break;
	}ARGEND

	int errorcode;
restart:
	SETUPERROR(errorcode);
	if(errorcode){
		/* remove aborted dfn frames */
		while(getcurrentdfn())
			popdfnframe();

		if(globalerror.msg)
			print("%S: %S\n", errorstrs[errorcode], globalerror.msg);
		else
			print("%S\n", errorstrs[errorcode]);
		goto restart;
	}

	while(!off){
		checkmem("main loop");
		if(needsnewline){
			print("\n");
			needsnewline = 0;
		}
		print("\t");
		Datum *result = evalline(nil, stdin, 1);
		if(result && !result->shy)
			print("%S\n", ppdatum(result));	
		freedatum(result);
/*
		print("Unfreed arrays: %d\n", arrayalloccounts);
		print("Unfreed datums: %d\n", datumalloccounts);
*/
	}
	threadexitsall(nil);
}

Datum *
evalline(Rune *line, Biobuf *bio, int toplevel)
{
	Statement *stmts;
	if(line)
		stmts = lexlinestr(line, toplevel);
	else if(bio)
		stmts = lexlinebio(bio, toplevel);
	else
		stmts = lexlinebio(stdin, toplevel);

	Datum *result = eval(stmts, toplevel);
	if(result)
		incdatumref(result);
	
	Statement *s = stmts;
	while(s != nil){
		Statement *tmp = s;
		s = s->next;
		freestatement(*tmp);
		free(tmp);
	}
	return result;
}