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

#include "apl9.h"

Biobuf *stdin;
static int booted = 0;
static Rune *startfile = L"runtime/start.apl";

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

	ErrorGuard *eg = newerrorguard(mkscalarint(0), nil);
restart:
	if(setjmp(eg->jmp)){
		ThreadData *td = getthreaddata();
		Rune *msg = errorstr(td->lasterror);
		if(td->lasterrormsg){
			if(runestrlen(msg) == 0)
				print("%S\n", td->lasterrormsg);
			else
				print("%S: %S\n", errorstr(td->lasterror), td->lasterrormsg);
		}else
			print("%S\n", errorstr(td->lasterror));
		while(getcurrentdfn())
			popdfnframe();
		goto restart;
	}

	if(!booted){
		booted = 1;
		Array *path = mkrunearray(startfile);
		runfile(path);
		freearray(path);
	}

	while(!off){
		checkmem("main loop");
		/* if(needsnewline){
			print("\n");
			needsnewline = 0;
		} */
		Datum *result = evalline(nil, stdin, 1);
		if(result && !result->shy)
			setquotequad(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;
}