MysoreScript
interpreter.hh
Go to the documentation of this file.
1 #pragma once
2 #include <string>
3 #include <unordered_map>
4 #include <forward_list>
5 #include <vector>
6 #include "runtime.hh"
7 
8 namespace Interpreter
9 {
15  extern bool forceCompiler;
16  using MysoreScript::Obj;
25  class Value
26  {
32  Obj *holder = nullptr;
36  Obj object;
42  void set(Obj o);
43  public:
51  {
52  return &object;
53  }
57  Value() : object(nullptr) {}
61  Value(Obj o) : object(nullptr) { set(o); }
65  Value(Value &o) : object(nullptr) { set(static_cast<Obj>(o)); }
69  Value(Value &&o) : object(nullptr)
70  {
71  set(static_cast<Obj>(o));
72  o.set(nullptr);
73  }
77  ~Value();
78  void operator=(Obj o) { set(o); }
79  void operator=(Value &o) { set(static_cast<Obj>(o)); }
80  operator Obj()
81  {
82  return object;
83  }
87  bool isInteger()
88  {
89  return ((reinterpret_cast<intptr_t>(object)) & 7) == 1;
90  }
95  {
96  assert(isInteger());
97  return reinterpret_cast<intptr_t>(object) >> 3;
98  }
99  };
103  typedef std::unordered_map<std::string, Obj*> SymbolTable;
104  class Context
105  {
111  std::forward_list<Value> globals;
116  std::vector<SymbolTable*> symbols;
117  public:
121  SymbolTable globalSymbols;
128  Value retVal = nullptr;
132  bool isReturning = false;
136  void pushSymbols(SymbolTable &s) { symbols.push_back(&s); }
140  void popSymbols() { symbols.pop_back(); }
144  Obj *lookupSymbol(const std::string &name);
149  void setSymbol(const std::string &name, Obj *val);
154  void setSymbol(const std::string &name, Obj val);
155  };
160 };
Object * Obj
Object pointer.
Definition: runtime.hh:33
void popSymbols()
Pop the top symbol table off the stack.
Definition: interpreter.hh:140
Value(Value &&o)
Move constructor.
Definition: interpreter.hh:69
std::unordered_map< std::string, Obj * > SymbolTable
A symbol table stores the address of each allocation.
Definition: interpreter.hh:103
void operator=(Value &o)
Definition: interpreter.hh:79
Object *(* ClosureInvoke)(Closure *,...)
A compiled closure invoke function.
Definition: runtime.hh:80
Value()
Default constructor, the value is null.
Definition: interpreter.hh:57
int getIntValue()
Returns the integer value of this object, if it is a small integer.
Definition: interpreter.hh:94
bool isInteger()
Returns true if this object is a small integer, false otherwise.
Definition: interpreter.hh:87
bool forceCompiler
Force the compiler to run.
Definition: interpreter.cc:7
Value wraps an object pointer.
Definition: interpreter.hh:25
ClosureInvoke closureTrampolines[]
Array of trampolines, indexed by number or arguments.
Definition: interpreter.cc:277
Obj * address()
Returns the address of the object.
Definition: interpreter.hh:50
void operator=(Obj o)
Definition: interpreter.hh:78
Value(Value &o)
Copy constructor.
Definition: interpreter.hh:65
Value(Obj o)
Construct a new value from an object pointer.
Definition: interpreter.hh:61
~Value()
Destructor, responsible for removing the reference from the GC&#39;s view.
Definition: interpreter.cc:313
void pushSymbols(SymbolTable &s)
Push a new symbol table on top of the stack.
Definition: interpreter.hh:136
SymbolTable globalSymbols
Global symbols.
Definition: interpreter.hh:121