All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vm.h
Go to the documentation of this file.
1 #ifndef uscript_vm_h
2 #define uscript_vm_h
3 
4 #include <map>
5 #include <vector>
6 #include <stdarg.h>
7 #include <stdio.h>
8 
9 #include "chunk.h"
10 #include "value.h"
11 
12 namespace uscript {
13 
18 };
19 
20 class VM {
21  const Chunk *chunk;
22  unsigned ip;
23  std::vector<Value> stack;
24  std::map<const char *, Value> globals;
25 
26  uint8_t ReadInstruction();
28  Value Peek(unsigned distance=0) { return stack[stack.size() - distance - 1]; }
29  Value Pop() { Value v = stack[stack.size() - 1]; stack.pop_back(); return v; }
30  void Push(Value v) { stack.push_back(v); }
31  void Reset();
32 
33  void RuntimeError(const char *format, ...);
34 
35  bool CallValue(Value callee, int argCount);
36  bool IndexValue(Value callee, int index);
37 
38  bool AccessValue(Value instance, const char *name, Value *result);
39  bool GetTField(ObjTInstance instance, const char *name, Value *ret);
40  Value GetTValue(uint8_t *loc, TData data);
41  void DoAddGlobal(const char *classname, const char *name, uint8_t *data);
42 
43 public:
44  VM();
45  InterpretResult Interpret(const char* source);
47  void SetChunk(const Chunk *_chunk);
48  InterpretResult Run(Value *ret=NULL);
49 
50  template <typename TObj>
51  void AddGlobal(const char *name, const TObj *object) {
52  DoAddGlobal(std::string(type_name<TObj>()).c_str(), name, (uint8_t*)object);
53  }
54 
55  void AddGlobal(const char *name);
56 };
57 
58 #define DECLARE_ADDGLOBAL_SPECIAL(type) \
59  template<> \
60  void uscript::VM::AddGlobal<type>(const char *name, const type *obj)
61 
67 
68 #undef DECLARE_ADDGLOBAL_SPECIAL
69 } // end namespace
70 #endif
void SetChunk(const Chunk *_chunk)
Definition: vm.cc:31
void RuntimeError(const char *format,...)
Definition: vm.cc:409
std::vector< Value > stack
Definition: vm.h:23
DECLARE_ADDGLOBAL_SPECIAL(int)
static std::string format(PyObject *obj, unsigned int pos, unsigned int indent, unsigned int maxlen, unsigned int depth)
Definition: fclmodule.cxx:374
do source
const Chunk * chunk
Definition: vm.h:21
VM()
Definition: vm.cc:27
const std::string instance
Definition: vm.h:20
void Reset()
Definition: vm.cc:36
bool AccessValue(Value instance, const char *name, Value *result)
Definition: vm.cc:362
void DoAddGlobal(const char *classname, const char *name, uint8_t *data)
Definition: vm.cc:396
double distance(geo::Point_t const &point, CathodeDesc_t const &cathode)
Returns the distance of a point from the cathode.
InterpretResult Interpret(const char *source)
Definition: vm.cc:14
bool GetTField(ObjTInstance instance, const char *name, Value *ret)
Definition: vm.cc:378
Value Peek(unsigned distance=0)
Definition: vm.h:28
InterpretResult Run(Value *ret=NULL)
Definition: vm.cc:65
InterpretResult
Definition: vm.h:14
void Push(Value v)
Definition: vm.h:30
void AddGlobal(const char *name, const TObj *object)
Definition: vm.h:51
uint8_t ReadInstruction()
Definition: vm.cc:40
then echo fcl name
unsigned ip
Definition: vm.h:22
Value GetTValue(uint8_t *loc, TData data)
Definition: vm.cc:309
Value Pop()
Definition: vm.h:29
bool CallValue(Value callee, int argCount)
Definition: vm.cc:304
Value ReadConstant()
Definition: vm.cc:44
std::map< const char *, Value > globals
Definition: vm.h:24
bool IndexValue(Value callee, int index)
Definition: vm.cc:285