aboutsummaryrefslogtreecommitdiffstats
path: root/runner.h
blob: e1faeda70f37a17f60153a467b46669d82668c2b (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
#ifndef INCLUDE_BLASSIC_RUNNER_H
#define INCLUDE_BLASSIC_RUNNER_H

// runner.h
// Revision 6-feb-2005

//#include "program.h"

class Program;

#include "blassic.h"

#include "error.h"
#include "file.h"
#include "codeline.h"
#include "var.h"
#include "element.h"

//#include "runnerline.h"

#include <iostream>
#include <stack>
#include <vector>
#include <map>


// ********************* GlobalRunner **********************


namespace blassic {


namespace onbreak {

enum BreakState { BreakStop, BreakCont, BreakGosub };

} // namespace onbreak


} // namespace blassic


enum TrigonometricMode { TrigonometricRad, TrigonometricDeg };

// We encapsulate the random generator in a class to have all code
// related to it in a place, and to be able to change it easily.

// random in some systems may not use the RAND_MAX value,
// then we use it only in linux.

class RandomGenerator {
public:
	BlNumber operator () ()
	{
		#if defined __linux__
		return BlNumber (random () ) / (RAND_MAX + 1.0);
		#else
		return BlNumber (rand () ) / (RAND_MAX + 1.0);
		#endif
	}
	void seed (unsigned int value)
	{
		#if defined __linux__
		srandom (value);
		#else
		srand (value);
		#endif
	}
};

class GlobalRunner {
public:
	GlobalRunner (Program & prog);
	~GlobalRunner ();

	// Program
private:
	Program & program;
public:
	Program & getprogram () { return program; }

	// TRON stuff
private:
	bool fTron, fTronLine;
	BlChannel blcTron;
	void do_tronline (const CodeLine & line);
public:
	void tron (bool fLine, BlChannel blc);
	void troff ();
	void tronline (const CodeLine & codeline)
	{
		if (fTron)
			do_tronline (codeline);
	}

	// GlobalRunner Channel stuff
public:
	typedef std::map <BlChannel, blassic::file::BlFile *> ChanFile;
	typedef std::map <BlChannel, BlLineNumber> ChanPolled;
private:
	ChanFile chanfile;
	ChanPolled chanpolled;
	bool alreadypolled;
	bool clearingpolled;
public:
	bool assign_channel_var
		(const std::string & var, const Dimension & dim,
			const std::string & value,
			blassic::file::BlFile::Align align);
	bool assign_mid_channel_var
		(const std::string & var, const Dimension & dim,
			const std::string & value,
			size_t inipos, std::string::size_type len);
	bool isfileopen (BlChannel channel) const
	{ return chanfile.find (channel) != chanfile.end (); }
	BlChannel freefile () const;
	blassic::file::BlFile & getfile (BlChannel channel) const;
	void setfile (BlChannel channel, blassic::file::BlFile * npfile);
	void resetfile0 ();
	void resetfileprinter ();
	void close_all ();
	void destroy_windows ();
	void closechannel (BlChannel channel);
	void windowswap (BlChannel ch1, BlChannel ch2);

	void pollchannel (BlChannel ch, BlLineNumber line);
	bool channelspolled ();
	BlLineNumber getpollnumber ();
	void setpolled ();
	void clearpolled ();

	// GlobalRunner DATA / READ / RESTORE stuff
private:
	BlLineNumber datanumline;
	BlChunk datachunk;
	unsigned short dataelem;
public:
	BlLineNumber & getdatanumline () { return datanumline; }
	BlChunk & getdatachunk () { return datachunk; }
	unsigned short & getdataelem () { return dataelem; }
	void setreadline (BlLineNumber bln)
	{
		datanumline= bln;
		datachunk= 0;
		dataelem= 0;
	}
	void clearreadline ()
	{
		datanumline= LineBeginProgram;
		datachunk= 0;
		dataelem= 0;
	}

	// GlobalRunner ON ERROR GOTO stuff.
private:
	BlLineNumber blnErrorGoto;
	BlLineNumber blnErrorGotoSource;
public:
	void clearerrorgoto ();
	void seterrorgoto (BlLineNumber line, BlLineNumber source);
	BlLineNumber geterrorgoto () const
	{ return blnErrorGoto; }
	BlLineNumber geterrorgotosource () const
	{ return blnErrorGotoSource; }

	// GlobalRunner ON BREAK
private:
	blassic::onbreak::BreakState breakstate;
	BlLineNumber breakgosubline;
public:
	void setbreakstate (blassic::onbreak::BreakState newstate)
	{
		breakstate= newstate;
	}
	blassic::onbreak::BreakState getbreakstate () const
	{ return breakstate; }
	void setbreakgosub (BlLineNumber bln)
	{
		breakstate= blassic::onbreak::BreakGosub;
		breakgosubline= bln;
	}
	BlLineNumber getbreakgosub () { return breakgosubline; }

	// Control of depth of fn calls.
private:
	size_t fn_current_level;
public:
	size_t fn_level () const { return fn_current_level; }
	void inc_fn_level ();
	void dec_fn_level ();

	// Trigonometric mode.
private:
	TrigonometricMode trigmode;
public:
	TrigonometricMode trigonometric_mode () const { return trigmode; }
	void trigonometric_default () { trigmode= TrigonometricRad; }
	void trigonometric_mode (TrigonometricMode trigmode_n)
	{ trigmode= trigmode_n; }

	// Random number generator.
private:
	RandomGenerator randgen;
public:
	BlNumber getrandom () { return randgen (); }
	void seedrandom (unsigned int value) { randgen.seed (value); }
};


// ********************* Runner **********************


class GosubStack {
private:
	GlobalRunner & globalrunner;
	typedef std::stack <GosubElement> st_t;
	st_t st;
public:
	typedef st_t::size_type size_type;
public:
	GosubStack (GlobalRunner & globalrunner);	
	~GosubStack ();
	bool empty () const
	{
		return st.empty ();
	}
	size_type size () const
	{
		return st.size ();
	}
	void check_fn () const
	{
		if (st.empty () )
			throw ErrUnexpectedFnEnd;
		if (st.top ().isgosub () )
			throw ErrGosubWithoutReturn;
	}
	void erase ();
	void push (ProgramPos pos, bool is_polled)
	{
		st.push (GosubElement (pos, is_polled) );
		if (is_polled)
			globalrunner.setpolled ();
	}
	void push (LocalLevel & ll)
	{
		st.push (GosubElement (ll) );
	}
	void pop (ProgramPos & ppos)
	{
		if (st.empty () )
			throw ErrReturnWithoutGosub;
		GosubElement & go= st.top ();
		if (! go.isgosub () )
			throw ErrReturnWithoutGosub;
		ppos= go.getpos ();
		go.freelocalvars ();
		if (go.ispolled () )
			globalrunner.clearpolled ();
		st.pop ();
	}
	void popfn ()
	{
		if (st.empty () )
			throw ErrReturnWithoutGosub;
		GosubElement & go= st.top ();
		if (go.isgosub () )
			throw ErrGosubWithoutReturn;
		go.freelocalvars ();
		st.pop ();
	}
	void addlocalvar (const std::string & name)
	{
		if (st.empty () )
			throw ErrMisplacedLocal;
		GosubElement go= st.top ();
		go.addlocalvar (name);
	}
};

class Runner {
public:
	enum RunnerStatus {
		Ended,
		FnEnded,
		ReadyToRun,
		Running,
		Stopped,
		Jump,
		Goto,
		//InitingCommand,
		//Command,
		JumpResumeNext,
		LastStatus= JumpResumeNext,
	};
private:
	enum StatusRun { KeepRunning, StopNow };

	typedef StatusRun (Runner::* checkstatusfunc)
		(CodeLine &, const CodeLine &);
	static checkstatusfunc checkstatus [LastStatus + 1];

	StatusRun checkstatusEnded
		(CodeLine & line, const CodeLine & line0);
	StatusRun checkstatusFnEnded
		(CodeLine & line, const CodeLine & line0);
	StatusRun checkstatusReadyToRun
		(CodeLine & line, const CodeLine & line0);
	StatusRun checkstatusRunning
		(CodeLine & line, const CodeLine & line0);
	StatusRun checkstatusStopped
		(CodeLine & line, const CodeLine & line0);
	StatusRun checkstatusJump
		(CodeLine & line, const CodeLine & line0);
	StatusRun checkstatusGoto
		(CodeLine & line, const CodeLine & line0);
	//StatusRun checkstatusInitingCommand
	//	(CodeLine & line, const CodeLine & line0);
	//StatusRun checkstatusCommand
	//	(CodeLine & line, const CodeLine & line0);
	StatusRun checkstatusJumpResumeNext
		(CodeLine & line, const CodeLine & line0);
	bool is_run_status (CodeLine & line, const CodeLine & line0);


	GlobalRunner & globalrunner;
	Program & program;
	RunnerStatus status;
	bool fInElse;
	bool fInWend;
public:
	//Runner (Program & prog);
	Runner (GlobalRunner & gr);
	Runner (const Runner & runner);
	~Runner ();

	Program & getprogram () { return program; }
	void clear ();
	void getline (std::string & line);
	void runline_catch (const CodeLine & codeline, ProgramPos pos,
		BlError & berr, bool & endloop, bool & dobreak);
	void runline (const CodeLine & codeline0);
	void run ();
	bool editline (BlLineNumber bln, std::string & str);
	void interactive ();

	// Runner *********** Errors **************
private:
	BlCode codprev;
	BlError berrLast;
public:
	BlErrNo geterr () const;
	ProgramPos geterrpos () const;
	BlLineNumber geterrline () const;
	void clearerror ();
	BlError geterror () const;
	void seterror (const BlError & er);

	// Runner *********** Flow control **************
private:
	ProgramPos posgoto;
public:
	void setstatus (RunnerStatus stat) { status= stat; }
	void run_to (BlLineNumber line)
	{
		posgoto= line;
		status= ReadyToRun;
	}
	void run_to (ProgramPos pos)
	{
		posgoto= pos;
		status= ReadyToRun;
	}
	void jump_to (BlLineNumber line)
	{
		posgoto= line;
		status= Jump;
	}
	void jump_to (ProgramPos pos)
	{
		posgoto= pos;
		status= Jump;
	}
	void goto_to (BlLineNumber line)
	{
		posgoto= line;
		status= Goto;
	}
	void goto_to (ProgramPos pos)
	{
		posgoto= pos;
		status= Goto;
	}
	void resume_next (ProgramPos pos)
	{
		posgoto= pos;
		status= JumpResumeNext;
	}

	// Runner *********** FOR / NEXT **************
private:
	std::stack <ForElement *, std::vector <ForElement *> > forstack;
public:
	void push_for (ForElement * pfe)
	{
		forstack.push (pfe);
	}
	//bool for_empty () const { return forstack.empty (); }
	ForElement & for_top ()
	{
		if (forstack.empty () )
			throw ErrNextWithoutFor;
		return * forstack.top ();
	}
	void for_pop () { delete forstack.top (); forstack.pop (); }
	// New version, supposed faster.
	bool next ();
	bool next (const std::string & varname);

	// Runner *********** GOSUB / RETURN / FN **************
private:
	GosubStack gosubstack;
public:
	bool gosub_empty () const
	{ return gosubstack.empty (); }
	size_t gosub_size () const
	{ return gosubstack.size (); }
	void gosub_check_fn () const
	{ gosubstack.check_fn (); }
	void gosub_pop (ProgramPos & pos)
	{ gosubstack.pop (pos); }
	void fn_pop () { gosubstack.popfn (); }
	void gosub_addlocalvar (const std::string & str)
	{
		gosubstack.addlocalvar (str);
	}
	void gosub_push (LocalLevel & ll)
	{
		gosubstack.push (ll);
	}
	void gosub_line (BlLineNumber dest, ProgramPos posgosub,
		bool is_polled= false)
	{
		gosubstack.push (posgosub, is_polled);
		goto_to (dest);
	}
	size_t fn_level () const { return globalrunner.fn_level (); }
	void inc_fn_level ()
	{ globalrunner.inc_fn_level (); }
	void dec_fn_level ()
	{ globalrunner.dec_fn_level (); }

	// Runner *********** REPEAT / UNTIL **************
private:
	std::stack <RepeatElement> repeatstack;
public:
	bool repeat_empty () const { return repeatstack.empty (); }
	void repeat_pop () { repeatstack.pop (); }
	RepeatElement & repeat_top () { return repeatstack.top (); }
	void repeat_push (const RepeatElement & re) { repeatstack.push (re); }

	// Runner *********** WHILE / WEND **************
private:
	std::stack <WhileElement> whilestack;
public:
	bool in_wend () { return fInWend; }
	void in_wend (bool f) { fInWend= f; }
	bool while_empty () { return whilestack.empty (); }
	void while_pop () { whilestack.pop (); }
	WhileElement & while_top () { return whilestack.top (); }
	void while_push (const WhileElement & we) { whilestack.push (we); }

	// Runner *********** TRON **************

	void tron (bool fLine, BlChannel blc);
	void troff ();
	void tronline (const CodeLine & line)
	{ globalrunner.tronline (line); }

	// Runner *********** BREAK / CONT **************
private:
	ProgramPos posbreak;
public:
	void jump_break ()
	{
		//if (! posbreak)
		if (posbreak.getnum ()  == LineEndProgram ||
				posbreak.getnum () == LineDirectCommand)
			throw ErrNoContinue;
		posgoto= posbreak;
		//posbreak= 0;
		posbreak= LineEndProgram;
		status= Jump;
	}
	void set_break (ProgramPos pos) { posbreak= pos; }

	#if 0
	void setbreakstate (BreakState newstate)
	{
		breakstate= newstate;
	}
	BreakState getbreakstate () { return breakstate; }
	void setbreakgosub (BlLineNumber bln)
	{
		breakstate= BreakGosub;
		breakgosubline= bln;
	}
	BlLineNumber getbreakgosub () { return breakgosubline; }
	#else

	void setbreakstate (blassic::onbreak::BreakState newstate)
	{
		globalrunner.setbreakstate (newstate);
	}
	blassic::onbreak::BreakState getbreakstate () const
	{
		return globalrunner.getbreakstate ();
	}
	void setbreakgosub (BlLineNumber bln)
	{
		globalrunner.setbreakgosub (bln);
	}
	BlLineNumber getbreakgosub ()
	{
		return globalrunner.getbreakgosub ();
	}

	#endif


	// Runner *********** DATA / READ / RESTORE **************

	BlLineNumber & getdatanumline ()
	{ return globalrunner.getdatanumline (); }
	BlChunk & getdatachunk ()
	{ return globalrunner.getdatachunk (); }
	unsigned short & getdataelem ()
	{ return globalrunner.getdataelem (); }

	// Runner *********** ON ERROR GOTO **************

	void showfailerrorgoto () const;
	void clearerrorgoto ()
	{ globalrunner.clearerrorgoto (); }
	void seterrorgoto (BlLineNumber line, BlLineNumber source)
	{ globalrunner.seterrorgoto (line, source); }
	BlLineNumber geterrorgoto () const
	{ return globalrunner.geterrorgoto (); }
	BlLineNumber geterrorgotosource () const
	{ return globalrunner.geterrorgotosource (); }

	// Runner *********** Channels **************

	#if 0
	void assign_channel_var
		(const std::string & var, const std::string & value,
			blassic::file::BlFile::Align align)
	{
		for (ChanFile::iterator it= chanfile.begin ();
			it != chanfile.end ();
			++it)
		{
			it->second->assign (var, value, align);
		}
	}
	#else
	bool assign_channel_var
		(const std::string & var, const Dimension & dim,
			const std::string & value,
			blassic::file::BlFile::Align align)
	{ return globalrunner.assign_channel_var (var, dim, value, align); }
	#endif
	bool assign_mid_channel_var
		(const std::string & var, const Dimension & dim,
			const std::string & value,
			size_t inipos, std::string::size_type len)
	{
		return globalrunner.assign_mid_channel_var
			(var, dim, value, inipos, len);
	}
	//bool isfileopen (BlChannel channel) const
	//{ return chanfile.find (channel) != chanfile.end (); }
	bool isfileopen (BlChannel channel) const
	{ return globalrunner.isfileopen (channel); }
	BlChannel freefile () const
	{ return globalrunner.freefile (); }
	blassic::file::BlFile & getfile (BlChannel channel) const
	{ return globalrunner.getfile (channel); }
	blassic::file::BlFile & getfile0 () const
	{ return globalrunner.getfile (DefaultChannel); }
	void setfile (BlChannel channel, blassic::file::BlFile * npfile)
	{ globalrunner.setfile (channel, npfile); }
	void resetfile0 ()
	{ globalrunner.resetfile0 (); }
	void close_all ()
	{ globalrunner.close_all (); }
	void destroy_windows ()
	{ globalrunner.destroy_windows (); }
	void closechannel (BlChannel channel)
	{ globalrunner.closechannel (channel); }
	void windowswap (BlChannel ch1, BlChannel ch2)
	{ globalrunner.windowswap (ch1, ch2); }

	void pollchannel (BlChannel ch, BlLineNumber line)
	{ globalrunner.pollchannel (ch, line); }
	bool channelspolled ()
	{ return globalrunner.channelspolled (); }
	BlLineNumber getpollnumber ()
	{ return globalrunner.getpollnumber (); }

	void spectrumwindows ();

	// Runner *********** DATA / READ / RESTORE **************

	void setreadline (BlLineNumber bln)
	{ globalrunner.setreadline (bln); }
	void clearreadline ()
	{ globalrunner.clearreadline (); }

	// Runner *********** AUTO **************
private:
	BlLineNumber blnAuto, blnAutoInc;
public:
	void setauto (BlLineNumber line, BlLineNumber inc)
	{ blnAuto= line; blnAutoInc= inc; }

	// Runner *********** Trigonometric mode **************
	TrigonometricMode trigonometric_mode ()
	{ return globalrunner.trigonometric_mode (); }
	void trigonometric_default ()
	{ globalrunner.trigonometric_default (); }
	void trigonometric_mode (TrigonometricMode trigmode_n)
	{ globalrunner.trigonometric_mode (trigmode_n); }

private:

	bool processline (const std::string & line);

public:
	// Runner *********** Random number generator **************

	BlNumber getrandom ()
	{ return globalrunner.getrandom (); }
	void seedrandom (unsigned int value)
	{ globalrunner.seedrandom (value); }

	// Runner *********** Auxiliars **************

	void clean_input ();
	void ring ();
	void set_title (const std::string & str);
};

std::ostream & operator << (std::ostream & os, Runner::RunnerStatus status);

#endif

// Fin de runner.h
Un proyecto texto-plano.xyz