summaryrefslogtreecommitdiff
path: root/main.c
blob: f23ea9ef6b2bb7026436925c9fa4ef6788fecdb7 (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
#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";
static Rune *stdlibfile = L"runtime/stdlib.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)){
		displayerror();
		while(getcurrentdfn())
			popdfnframe();
		goto restart;
	}

	if(!booted){
		booted = 1;
		runfile(mkrunearray(stdlibfile));
		runfile(mkrunearray(startfile));
	}

	while(!off){
		checkmem("main loop");
		Datum *result = evalline(nil, stdin, 1);
		if(!result){
			result = allocdatum(ArrayTag, 1);
			result->array = mkscalarint(0);
		}
		outputmain(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;
}