-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.h
639 lines (564 loc) · 19.9 KB
/
interpreter.h
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
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#ifndef INTERPRETER_H
#define INTERPRETER_H
#include <boost/shared_ptr.hpp>
#include <memory>
#include <function.h>
#include <types.h>
#include <QStringList>
#include <QMessageBox>
#include <QDebug>
typedef std::string::const_iterator iterator_type;
namespace interpreter {
enum byte_code
{
op_neg, //0 negate the top stack entry
op_pos, //1 make the top stack entry positive
op_add, //2 add top two stack entries
op_sub, //3 subtract top two stack entries
op_mul, //4 multiply top two stack entries
op_div, //5 divide top two stack entries
op_select_point,//6 select struct member after dereferencing with ->
op_select_ref, //7 select struct member with .
op_increment, //8 postfix increment ++
op_decrement, //9 postfix decrement --
op_dereference, //10 dereference address with *
op_address, //11 get stack index with &
op_not, //12 boolean negate the top stack entry
op_eq, //13 compare the top two stack entries for ==
op_neq, //14 compare the top two stack entries for !=
op_lt, //15 compare the top two stack entries for <
op_lte, //16 compare the top two stack entries for <=
op_gt, //17 compare the top two stack entries for >
op_gte, //18 compare the top two stack entries for >=
op_and, //19 logical and top two stack entries
op_or, //20 logical or top two stack entries
op_load, //21 load a variable
op_store, //22 store a variable
op_int, //23 push constant integer into the stack
op_true, //24 push constant 0 into the stack
op_false, //25 push constant 1 into the stack
op_jump_if, //26 jump to a relative position in the code if top stack
// evaluates to false
op_jump, //27 jump to a relative position in the code
op_stk_adj, //28 adjust the stack (for args and locals)
op_call, //29 function call
op_return, //30 return from function
op_void, //31 void (no return)
op_alloc //32 allocate stack entry
};
class null_ptr : public std::exception
{
public:
virtual const char* what() const throw()
{
return "null pointer";
}
};
enum type_code
{
type_void,
type_int,
type_pointer,
type_struct
};
struct variable;
//variable operators for VM
//arithmetic
variable operator+(variable const& a, variable const& b);
variable operator-(variable const& a, variable const& b);
variable operator*(variable const& a, variable const& b);
variable operator/(variable const& a, variable const& b);
//relational
bool operator||(variable const& a, variable const& b);
bool operator&&(variable const& a, variable const& b);
bool operator==(variable const& a, variable const& b);
bool operator!=(variable const& a, variable const& b);
bool operator<(variable const& a, variable const& b);
bool operator<=(variable const& a, variable const& b);
bool operator>(variable const& a, variable const& b);
bool operator>=(variable const& a, variable const& b);
//unary
variable operator+(variable const& a);
variable operator-(variable const& a);
variable operator!(variable const& a);
variable& operator*(variable const& a);
variable& operator&(variable const& a);
//new & delete shall come later
struct variable : ast::Typed
{
variable() : var(0) {}
variable(int var, ast::Type type) : var(var)
{
this->type = type;
type.lvalue = true;
}
int var;
variable(variable const& b)
{
if(!type.is_set && b.type.is_set) {
type = b.type;
}
var = b.var;
}
void operator=(variable const& b)
{
if(!type.is_set && b.type.is_set) {
type = b.type;
}
var = b.var;
}
void operator=(int b)
{
var = b;
if(b != 0) {
type = ast::Int;
}
}
void operator=(bool b)
{
var = b;
//type = ast::Bool;
}
void operator+=(variable const& b)
{
operator=((*this) + b);
}
void operator-=(variable const& b)
{
operator=((*this) - b);
}
void operator*=(variable const& b)
{
operator=((*this) * b);
}
void operator/=(variable const& b)
{
operator=((*this) / b);
}
explicit operator bool()
{
return bool(var);
}
const int* dereference(std::vector<int> const& stack)
{
return &stack[var];
}
int size()
{
return type.width;
}
friend std::ostream& operator<<(std::ostream& out, variable const& var);
friend QDebug& operator<<(QDebug& out, variable const& var);
};
struct cstruct : ast::Typed
{
typedef std::map<std::string, ast::Type> members_type;
cstruct(ast::struct_specifier& ss, std::vector<variable>& stack)
: name(ss.type_name.name), stack(stack)
{
qDebug() << "Creating new struct";
int offset = 1;
for(auto& member_ : ss.members) {
auto& member = member_.get();
//stack[offset].type = member.type;
qDebug() << member.type.type_str.c_str();
if(member.type.pointer) {
member.type.width = 1;
}
member_specs[member.dec.name.name] = member.type;
members[member.dec.name.name] = offset;
offset += member.type.width;
}
type = ast::Type("struct " + name, size(), false, true);
}
std::string name;
members_type member_specs;
std::map<std::string, int> members;
std::vector<variable>& stack;
int member_offset(std::string const& member, int offset)
{
auto i = members.find(member);
if(i != members.end()) {
int rv = i->second + offset;
if(!stack[rv].type.is_set) {
stack[rv].type = member_specs[i->first];
}
return i->second + offset;
}
return -1;
}
int size()
{
int sum = 1;
for(members_type::value_type a : member_specs) {
sum += a.second.width;
}
return sum;
}
};
struct Address
{
Address(size_t addr) : location(addr)
{}
Address operator&()
{
return Address(location);
}
size_t location;
};
typedef boost::variant<
ast::Int_Value
, ast::Bool_Value
, cstruct
, Address
> variable_type;
struct scope : QObject
{
scope(std::vector<int>& code, std::vector<variable>& stack, int& offset,
std::list<ast::Type>& dyn_types)
: code(code), stack(stack), offset(offset), held_value(-1), dyn_types(dyn_types)
{}
scope(scope* parent)
: parent(parent)
, code(parent->code)
, stack(parent->stack)
, offset(parent->offset)
, held_value(-1)
, dyn_types(parent->dyn_types)
{}
void op(int a);
void op(int a, int b);
void op(int a, int b, int c);
const int* lookup_var(std::string const& name);
bool exists(std::string const& name)
{
return bool(lookup_var(name));
}
ast::Type lookup_struct_type(std::string const& name);
cstruct *lookup_struct(std::string const& name);
void add_var(std::string const& name, ast::Type& type);
std::map<std::string, int> table;
boost::shared_ptr<scope> parent;
std::map<std::string, cstruct> structs;
std::vector<int>& code;
std::vector<variable>& stack;
int& offset;
int held_value;
std::list<ast::Type>& dyn_types;
public slots:
void addStruct(std::string const& name, cstruct const& s)
{
qDebug() << "scope: addStruct";
structs.insert(std::pair<std::string,cstruct>(name, s));
emit newStructDefinition(s);
}
signals:
void newStructDefinition(cstruct const& s);
private:
Q_OBJECT
};
struct type_resolver : boost::static_visitor<ast::Type>
{
type_resolver(error_handler& error_, scope* env)
: env(env), error_(error_)
{}
ast::Type operator()(ast::Type& t)
{
return t;
}
ast::Type operator()(ast::struct_specifier& ss)
{
qDebug() << "Processing: ast::struct_specifier";
auto t = env->lookup_struct_type(ss.type_name.name);
if(ss.members.size() > 0) {
if(t != ast::Error) {
error(ss.id, "Duplicate struct type");
return ast::Error;
}
std::list<ast::struct_member_declaration> marked;
for(auto& member_ : ss.members) {
auto& member = member_.get();
member.type = member.type_spec.apply_visitor(*this);
if(member.type == ast::Error) {
if(member.type_spec.which() == 1) {
auto mss = boost::get<ast::struct_specifier&>(member.type_spec);
if(mss.type_name.name == ss.type_name.name) {
//no recursive types
error(member.id, "Recursive types not supported");
return ast::Error;
}
}
error(member.id, "Type error");
return ast::Error;
}
member.type.pointer = member.dec.pointer;
}
env->addStruct(ss.type_name.name, cstruct(ss, env->stack));
return env->lookup_struct_type(ss.type_name.name);
}
else if(t == ast::Error) {
error(ss.type_name.id, "No such struct");
return t;
}
return t;
}
void error(int id, std::string const& what)
{
qDebug() << "id:" << id;
error_("Error! ", what, error_.iters[id]);
}
scope* env;
error_handler& error_;
};
struct PrimaryExpressionTypeResolver : boost::static_visitor<ast::Type>
{
template <typename T>
ast::Type& operator()(T& a, typename boost::enable_if<boost::is_base_of<ast::Typed, T> >::type* dummy = 0)
{
return a.type;
}
template <typename T>
ast::Type& operator()(T& /*a*/, typename boost::disable_if<boost::is_base_of<ast::Typed, T> >::type* dummy = 0)
{
return ast::Error;
}
};
struct Expression : boost::static_visitor<bool>
{
Expression(error_handler& error_, scope* env) : env(env), error_(error_)
{
BOOST_ASSERT(env);
}
Expression(error_handler& error_, scope* env, ast::Type type, bool lvalue = false)
: lvalue(lvalue), type(type), env(env), error_(error_)
{
BOOST_ASSERT(env);
}
void error(int id, std::string const& what)
{
error_("Error! ", what, error_.iters[id]);
}
bool operator()(ast::assignment_expression& ast);
bool operator()(ast::logical_OR_expression& ast);
bool operator()(ast::logical_AND_expression& ast);
bool operator()(ast::equality_expression& ast);
bool operator()(ast::relational_expression& ast);
bool operator()(ast::additive_expression& ast);
bool operator()(ast::multiplicative_expression& ast);
bool operator()(ast::unary_expression& ast);
bool operator()(ast::struct_expr& ast);
bool operator()(ast::postfix_expression& ast);
bool operator()(ast::optoken& ast);
bool operator()(ast::identifier& ast);
bool operator()(bool ast);
bool operator()(ast::Bool_Value ast);
bool operator()(unsigned int ast);
bool operator()(ast::Int_Value ast);
bool operator()(ast::nil) { assert(0); return false; }
ast::Type operator()(ast::primary_expression& ast);
bool lvalue;
ast::Type type;
scope* env;
error_handler& error_;
PrimaryExpressionTypeResolver petr;
};
// struct function
// {
// function(std::size_t nargs, std::size_t offset_, scope* current_scope) :
// offset(offset_),
// size_(0),
// nargs_(nargs)/*,
// env(new scope(current_scope,))*/
// {}
// int& operator[](std::size_t i) { return code[address+i]; }
// int const& operator[](std::size_t i) const { return code[address+i]; }
// std::size_t size() const { return size_; }
// std::size_t get_address() const { return address; }
// std::size_t nargs() const { return nargs_; }
//// std::size_t nvars() const { return variables.size(); }
// void link_to(std::string const& name, std::size_t address);
// std::size_t offset;
// ast::Type return_type;
// protected:
// std::size_t size_;
// std::size_t nargs_;
// std::map<std::size_t, std::string> function_calls;
// scope* env;
// std::vector<int> code;
// std::size_t address;
// };
#define DDVS_STACK_SIZE 8192
struct global : public QObject, public boost::static_visitor<bool> {
global(std::vector<int>& code, error_handler& error__, std::list<ast::Type>& dyn_types)
: stack_offset(0),
stack(DDVS_STACK_SIZE),
error_(error__),
global_scope(code, stack, stack_offset, dyn_types),
current_scope(&global_scope),
dyn_types(dyn_types)
{
connect(&global_scope, SIGNAL(newStructDefinition(cstruct)), this, SLOT(structDefined(cstruct)));
}
void error(int id, std::string const& what)
{
error_("Error! ", what, error_.iters[id]);
}
bool operator()(ast::nil) { assert(0); return false; }
bool operator()(ast::identifier& ast);
bool operator()(ast::function_call& ast);
bool operator()(ast::assignment_expression& ast);
bool operator()(ast::logical_OR_expression& ast);
bool operator()(ast::declaration& ast);
bool operator()(ast::declarator& ast);
bool operator()(ast::init_declarator& ast);
bool operator()(ast::compound_statement& ast);
bool operator()(ast::statement& ast);
bool operator()(ast::if_statement& ast);
bool operator()(ast::while_statement& ast);
bool operator()(ast::return_statement& ast);
bool operator()(ast::struct_member_declaration& ast);
bool operator()(ast::struct_specifier& ast);
bool operator()(ast::function_definition& ast);
bool operator()(ast::translation_unit& ast);
ast::Type operator()(ast::type_specifier& t);
std::vector<int> const& get_code()
{
return global_scope.code;
}
std::vector<variable>& get_stack()
{
return stack;
}
std::vector<variable>::iterator get_stack_pos()
{
return stack.begin() + stack_offset;
}
std::map<std::string, int> const& get_vars()
{
return global_scope.table;
}
std::map<std::string, cstruct> const& get_structs() {
return global_scope.structs;
}
int get_offset()
{
return stack_offset;
}
int stack_offset;
public slots:
void structDefined(cstruct const& s)
{
qDebug() << "global: structDefined";
emit newStructDefinition(s);
}
signals:
void newStructDefinition(cstruct const& s);
private:
Q_OBJECT
std::vector<variable> stack;
// typedef std::map<std::string, boost::shared_ptr<function> > function_table;
// function_table functions;
error_handler& error_;
scope global_scope;
scope* current_scope;
std::list<ast::Type>& dyn_types;
};
class Interpreter : public QObject
{
Q_OBJECT
public:
Interpreter() :
error(start, end, error_buf),
compiler(code, error, dyn_types),
stack_offset(compiler.stack_offset)
{
connect(&compiler, SIGNAL(newStructDefinition(cstruct)), this, SLOT(structDefined(cstruct)));
#ifdef _WIN32
msg_box.setStyleSheet("QAbstractScrollArea { font-family: \"Courier\"; }");
#else
msg_box.setStyleSheet("QAbstractScrollArea { font-family: \"monospace\"; }");
#endif
}
bool parse(std::string input);
bool parse(QString input);
std::vector<variable>& getStack()
{
return compiler.get_stack();
}
std::vector<variable>::iterator getStackPos()
{
return compiler.get_stack_pos();
}
int getOffset()
{
return compiler.get_offset();
}
std::map<std::string, int> const& getGlobals()
{
return compiler.get_vars();
}
std::map<std::string, cstruct> const& getStructs() {
return compiler.get_structs();
}
int execute() {
int r = execute(code, code.begin(), getStack().begin(), getStackPos());
code.clear();
dyn_types.clear();
return r;
}
public slots:
void structDefined(cstruct const& s)
{
qDebug() << "Interpreter: structDefined";
emit newStructDefinition(s);
}
signals:
void newStructDefinition(cstruct const& s);
private:
std::list<ast::Type> dyn_types;
QMessageBox msg_box;
iterator_type start;
iterator_type end;
std::string src;
error_handler error;
global compiler;
ast::translation_unit ast;
std::vector<int> code;
boost::shared_ptr<QString> error_buf;
int& stack_offset;
parser::skipper skip;
int execute(
std::vector<int> const& code // the program code
, std::vector<int>::const_iterator pc // program counter
, std::vector<variable>::iterator frame_ptr // start of whole stack
, std::vector<variable>::iterator stack_ptr // start of emptiness
);
};
template <typename Iterator>
struct Range
{
Range(Iterator const& begin_, Iterator const& end_)
: begin_(begin_), end_(end_) {}
Iterator const& begin()
{
return begin_;
}
Iterator const& end()
{
return end_;
}
Iterator const& begin_;
Iterator const& end_;
};
template <typename Iterator>
Range<Iterator> makeRange(Iterator const& begin, Iterator const& end)
{
return Range<Iterator>(begin, end);
}
}
#endif // INTERPRETER_H