Import baZic by David Given
[bazic.git] / bazic.inf
1 include "malloc.h";
2 include "tokeniser.h";
3 include "store.h";
4 include "string.h";
5 include "interpreter.h";
6 include "utils.h";
7 include "script.h";
8
9 constant HEAP_SIZE $7FFF;
10 array heap -> HEAP_SIZE;
11 array parsebuffer -> 256;
12
13 ! Draw the status line.
14
15 [ draw_status;
16         @split_window 1;
17         @set_window 1;
18         @set_cursor 1 1;
19         style reverse;
20         spaces (0->33)-1;
21         @set_cursor 1 2;
22         print "baZic 0.1 (C) 2001 David Given.   ";
23         ! We add on some stuff in the next line, because we don't want to
24         ! include the input buffer in the memory count.
25         print (mem_countfree()+255+AB__SIZE), " bytes free.";
26         @set_cursor 1 1;
27         style roman;
28         @set_window 0;
29 ];
30
31 ! Read in a command from the user into the parse buffer.
32
33 [ read_interactive_command  buf in;
34         buf = mem_alloc(255);
35         if (buf == 0)
36                 return -2;
37
38         ! Prompt the user for input.
39
40         buf->0 = 255;
41         print "> ";
42         read buf 0 draw_status;
43
44         ! Ensure the string is zero-terminated.
45
46         in = buf+2;
47         in->(buf->1) = 0;
48
49         ! Tokenise the stream.
50
51         in = tokenise_stream(in, parsebuffer);
52
53         mem_free(buf);
54         return in;
55 ];
56         
57 ! Clear all variables and reinit the heap.
58
59 [ cmd_clear;
60         mem_init(store_eop+1, heap+HEAP_SIZE);
61         store_heapclean();
62         return ESTATE_NORMAL;
63 ];
64
65 ! Invoke a command loop.
66
67 [ command_loop func  i;
68         cmd_clear();
69         do {
70                 string_gc();
71                 i = indirect(func);
72                 switch (i)
73                 {
74                         -1: ! success
75                                 !detokenise_stream(parsebuffer+1);
76                                 i = execute_command(parsebuffer);
77                                 break;
78
79                         -2: ! out of memory
80                                 print "Insufficient memory for interactive prompt!^";
81                                 print "Dumping variables and trying again.^";
82                                 i = ESTATE_DANGEROUS;
83                                 break;
84
85                         -3: ! end of program or script or whatever
86                                 i = ESTATE_ERROR;
87                                 break;
88
89                         default: ! parse error
90                                 print "Parse error at offset ", i, ".^";
91                                 i = ESTATE_ERROR;
92                                 break;
93                 }
94         } until (i < 0);
95         return i;
96 ];
97
98 [ Main  i;
99         print "^^^^^^^^baZic v0.1^(C) 2001 David Given^^";
100         print "To play ~Hunt the Wumpus~, type ~script 2~ to load ";
101         print "the program, and then ~run~ to start it. Try ~script~ on its ";
102         print "own to see what else is available.^^";
103
104         do {
105                 store_init(heap, heap+HEAP_SIZE);
106                 do {
107                         i = command_loop(read_interactive_command);
108                 } until ((i == ESTATE_QUIT) || (i == ESTATE_NEW));
109         } until (i == ESTATE_QUIT);
110 ];
111