All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
api.h
Go to the documentation of this file.
1 #ifndef uscript_api_h
2 #define uscript_api_h
3 
4 #include <cassert>
5 #include <functional>
6 
7 #include "common.h"
8 #include "compile.h"
9 #include "vm.h"
10 
11 namespace uscript {
12 
13 // convert any random template type into a char
14 template<typename T>
15 using JustAChar = const char *;
16 
17 template<typename T>
18 using JustACharRef = const char *&;
19 
20 void inline SetAll(VM &vm, std::initializer_list<const char *> names) {
21  for (const char *name: names) {
22  vm.AddGlobal(name);
23  }
24 }
25 
26 template <typename... Ts>
27 void _swallow(Ts&&...) {}
28 
29 template<typename... TObjs>
30 void RegisterAll() {
31  _swallow(uscript::Compiler::Register<TObjs>...);
32 }
33 
34 template<typename... TObjs>
35 void AddAll(VM &vm) {}
36 
37 template<typename T, typename... TObjs>
38 void AddAll(VM &vm, JustACharRef<T> name, JustACharRef<TObjs> ...names, const T *&obj, const TObjs *&...objs) {
39  vm.AddGlobal(name, obj);
40  AddAll<TObjs...>(vm, names..., objs...);
41 }
42 
43 
44 template<typename... TObjs>
45 Chunk compileChunk(const char *source) {
46  Chunk chunk;
47  RegisterAll<TObjs...>();
48 
49  uscript::Compiler::Compile(source, &chunk);
50 
51  return std::move(chunk);
52 }
53 
54 template<typename... TObjs>
55 std::function<uscript::Value (const TObjs*...)> compile(JustAChar<TObjs> ...names, const char *source) {
56  Chunk chunk = compileChunk<TObjs...>(source);
57 
58  VM vm;
59  SetAll(vm, {names...});
60  return [chunk, vm, names...](const TObjs *... data) mutable {
61  vm.SetChunk(&chunk);
62  AddAll<TObjs...>(vm, names..., data...);
63  Value value;
64  vm.Run(&value);
65  return value;
66  };
67 }
68 
69 } // end namespace
70 
71 #endif
const char *& JustACharRef
Definition: api.h:18
void SetChunk(const Chunk *_chunk)
Definition: vm.cc:31
Chunk compileChunk(const char *source)
Definition: api.h:45
const char * JustAChar
Definition: api.h:15
std::function< uscript::Value(const TObjs *...)> compile(JustAChar< TObjs >...names, const char *source)
Definition: api.h:55
void _swallow(Ts &&...)
Definition: api.h:27
do source
void AddAll(VM &vm)
Definition: api.h:35
Definition: vm.h:20
void RegisterAll()
Definition: api.h:30
void SetAll(VM &vm, std::initializer_list< const char * > names)
Definition: api.h:20
static const std::vector< std::string > names
void AddGlobal(const char *name, const TObj *object)
Definition: vm.h:51
then echo fcl name
temporary value
static bool Compile(const char *source, Chunk *chunk)
Definition: compile.h:140