File: Interpreter.h

package info (click to toggle)
basic256 2.0.0.11-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 15,076 kB
  • sloc: cpp: 16,791; yacc: 3,979; lex: 1,446; makefile: 25
file content (349 lines) | stat: -rwxr-xr-x 10,900 bytes parent folder | download
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
/** Copyright (C) 2006, Ian Paul Larsen.
 **
 **  This program is free software; you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License as published by
 **  the Free Software Foundation; either version 2 of the License, or
 **  (at your option) any later version.
 **
 **  This program is distributed in the hope that it will be useful,
 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 **  GNU General Public License for more details.
 **
 **  You should have received a copy of the GNU General Public License along
 **  with this program; if not, write to the Free Software Foundation, Inc.,
 **  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 **/

#ifndef __INTERPRETER_H
#define __INTERPRETER_H

#include <QPixmap>
#include <QImage>
#include <QThread>
#include <QFile>
#include <QDir>
#include <QTime>
#include <stdio.h>
#include <cmath>
#include <dirent.h>
#include "BasicGraph.h"
#include "Constants.h"
#include "DataElement.h"
#include "Error.h"
#include "Convert.h"
#include "Stack.h"
#include "Variables.h"
#include "Sound.h"
#include "Sleeper.h"
#include "BasicDownloader.h"

#include <QElapsedTimer>
#include <QDebug>
#include <QProcess>


#include <QtPrintSupport/QPrinter>
#include <QtPrintSupport/QPrinterInfo>

#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlRecord>
#include <QtSql/QSqlError>

#ifndef ANDROID
    // includes for all ports EXCEPT android
    #include <QSerialPort>
#endif

enum run_status {R_STOPPED, R_RUNNING, R_INPUT, R_STOPING};

#define NUMFILES 8
#define NUMSOCKETS 8
#define NUMDBCONN 8
#define NUMDBSET 8

#define STRINGMAXLEN 16777216

#define FILEWRITETIMEOUT		1			// on a file/serial write wait up to MS for the write to complete
#define FILEREADTIMEOUT			1			// on a file/serial read wait up to MS for data to be there
#define SERIALREADBUFFERSIZE	1024		// size of openserial read buffer

#define FORFRAMETYPE_INT 0
#define FORFRAMETYPE_FLOAT 1
#define FORFRAMETYPE_FOREACH_ARRAY 2
#define FORFRAMETYPE_FOREACH_MAP 3


struct byteCodeData
{
    unsigned int size;
    void *data;
};

// used by function calls, subroutine calls, and gosubs for return location
// used also by onerror
// used to track nested on-error and try/catch definitions
class addrStack {
public:
    addrStack(){
        size=0;
        pointer=0;
        stack.reserve(512);
    };
    ~addrStack(){};
    void push(int* address){
        if(pointer>=size) grow();
        stack[pointer]=address;
        pointer++;
    };
    int* pop(){
        if(pointer==0) return NULL;
        pointer--;
        return stack[pointer];
    };
    int* peek(){
        if(pointer==0) return NULL;
        return stack[pointer-1];
    };
    void drop(){
        if(pointer>0) pointer--;
    };
    int count(){
        return pointer;
    };
private:
    int size;
    int pointer;
    std::vector<int*> stack;
    void grow(){
        size=size+50;
        stack.resize(size);
    };
};

struct trycatchframe {
    trycatchframe *next;
    int *catchAddr;
    int recurseLevel;
    int stackSize;
};

// structure for the nested for statements
// if useInt then make loop integer safe
struct forframe {
    forframe *next;
    int *forAddr;   //FOR address
    int *nextAddr;  //NEXT address
    int type;		//0=integer, 1=float, 2=foreache if -
    int for_varnum;
    int for_val_varnum;		// -1 if not used (used to get map value in for each)
    double floatStart;
    double floatEnd;
    double floatStep;
    long intStart;
    long intEnd;
    long intStep;
    DataElement *iter_d;		// hold array or map pointer - delete on end of loop
    std::vector<DataElement>::iterator arrayIter;
    std::vector<DataElement>::iterator arrayIterEnd;
    std::map<QString, DataElement>::iterator mapIter;
    std::map<QString, DataElement>::iterator mapIterEnd;
};

typedef struct {
    bool visible;
    double x;
    double y;
    double r;	// rotate
    double s;	// scale
    double o;	// opacity
    QImage *image;
    QImage *transformed_image;
    QRect position;
    bool changed;
    bool was_printed;
    QRect last_position;
} sprite;

class Interpreter : public QThread
{
  Q_OBJECT;
    public:
        Interpreter(QLocale*);
        ~Interpreter();
        int compileProgram(char *);
        void initialize();
        bool isRunning();
        bool isStopped();
        bool isStopping();
        void setStatus(run_status);
        bool isAwaitingInput();
        void setInputString(QString);
        void cleanup();
        void run();
        int debugMode;					// 0=normal run, 1=step execution, 2=run to breakpoint
        QList<int> *debugBreakPoints;	// map of line numbers where break points ( pointer to breakpoint list in basicedit)
        QString returnString;			// return value from runcontroller emit
        int returnInt;					// return value from runcontroller emit
        QImage returnImage;				// return value from runcontroller emit
        int settingsAllowPort;
        int settingsAllowSystem;

    public slots:
        int execByteCode();
        void runHalted();

    signals:
        void debugNextStep();
        void fastGraphics();
        //void stopRun();
        void stopRunFinalized(bool);
        void goutputReady();
        void outputReady(QString);
        void outputError(QString);
        void getInput();
        void outputClear();
        void getKey();
        void playSounds(int, int*);
        void setVolume(int);
        void speakWords(QString);
        void goToLine(int);
        void seekLine(int);
        void varWinAssign(Variables**, int, int);
        void varWinAssign(Variables**, int, int, int, int);
        void varWinDropLevel(int);
        void varWinDimArray(Variables**, int, int, int);
        void resizeGraphWindow(int, int, qreal);
        void mainWindowsVisible(int, bool);
        void dialogAlert(QString);
        void dialogConfirm(QString, int);
        void dialogPrompt(QString, QString);
        void dialogOpenFileDialog(QString, QString, QString);
        void dialogSaveFileDialog(QString, QString, QString);
        void dialogAllowPortInOut(QString);
        void dialogAllowSystem(QString);
        void playSound(QString, bool);
        void playSound(std::vector<std::vector<double>>, bool);
        void loadSoundFromArray(QString, QByteArray*);
        void soundStop(int);
        void soundPlay(int);
        void soundFade(int, double, int, int);
        void soundVolume(int, double);
        //void soundExit();
        void soundPlayerOff(int);
        void soundSystem(int);
        void getClipboardImage();
        void getClipboardString();
        void setClipboardImage(QImage);
        void setClipboardString(QString);
        

    private:
        QLocale *locale;
        Sleeper *sleeper;
        BasicDownloader *downloader;
        //int optype(int op);
        QString opname(int);
        void waitForGraphics();
        void printError();
        int netSockClose(int);
        void netSockCloseAll();
        Variables *variables;
        Stack *stack;
        Stack *savestack;
        bool isError; //flag set if program stops because of an error
        Convert *convert;
        QIODevice **filehandle;
        int *filehandletype;		// 0=QFile (normal), 1=QFile (binary), 2=QSerialPort
        int *op;
        addrStack *callstack;
        addrStack *onerrorstack;
        trycatchframe *trycatchstack; // used to track nested try/catch definitions
        void decreaserecurse();
        forframe *forstack;                     // stack FOR/NEXT for current recurse level
        std::vector <forframe*> forstacklevel;  // stack FOR/NEXT for each recurse level
        int forstacklevelsize;                  // size for forstacklevel stack
        run_status status;
        bool fastgraphics;
        QString inputString;        // input string from user
        int inputType;				// data type to convert the input into
        double double_random_max;
        int currentLine;
        void clearsprites();
        void update_sprite_screen();
        void sprite_prepare_for_new_content(int);
        void force_redraw_all_sprites_next_time();
        bool sprite_collide(int, int, bool);
        sprite *sprites;
        int nsprites;
        void closeDatabase(int);
        int arraybase;			// 0 for 0..n-1, 1 for 1 to n array indexing
        // watch... functions trigger the variablewatch window to display
        void watchvariable(bool, int);
        void watchvariable(bool, int, int, int);
        void watchdecurse(bool);

        int listensockfd;				// temp socket used in netlisten
        int netsockfd[NUMSOCKETS];

        DIR *directorypointer;		// used by DIR function
        QTime runtimer;				// used by MSEC function
        //SoundSystem *sound;
        int includeFileNumber;
        bool regexMinimal;			// flag to tell QRegExp to be greedy (false) or minimal (true)

        bool printing;
        QPrinter *printdocument;

        QPainter *painter;
        bool painter_pen_need_update;
        bool painter_brush_need_update;
        bool painter_last_compositionModeClear;
        unsigned long painter_brush_color; //last color value for comparison
        unsigned long painter_pen_color; //last color value for comparison
        void setGraph(QString id);
        QString drawto;
        bool setPainterTo(QPaintDevice *destination);
        QPen drawingpen;
        QBrush drawingbrush;
        int CompositionModeClear;
        int PenColorIsClear;
        bool drawingOnScreen;

        QFont font;
        QString defaultfontfamily;
        int defaultfontpointsize;
        int defaultfontweight;
        bool defaultfontitalic;

        bool painter_font_need_update;
        bool painter_custom_font_flag;



        QSqlQuery *dbSet[NUMDBCONN][NUMDBSET];		// allow NUMDBSET number of sets on a database connection

        int mediaplayer_id_legacy;

        QMap <QString, QImage*> images;
        int lastImageId;
        bool imageSmooth;

        int settingsDebugSpeed;
        bool settingsAllowSetting;
        int settingsSettingsAccess;
        int settingsSettingsMax;
        QString programName;
        int settingsPrinterResolution;
        int settingsPrinterPrinter;
        int settingsPrinterPaper;
        QString settingsPrinterPdfFile;
        int settingsPrinterOrient;
        QMap<QString, QMap<QString, QString>> fakeSettings;
        QProcess *sys;

};


#endif