-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbstate.h
141 lines (121 loc) · 3.68 KB
/
bstate.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
#ifndef _BEAN_STATE_H
#define _BEAN_STATE_H
#include "bzio.h"
#include "common.h"
#include "bobject.h"
#include "bhash.h"
#include "barray.h"
typedef struct Mbuffer Mbuffer;
typedef struct Ibuffer Ibuffer;
typedef struct stringtable {
struct TString ** hash;
int nuse;
int size;
} stringtable;
#define G(B) (B->l_G)
#define CS(B) (B->l_G->cScope)
#define GS(B) (B->l_G->globalScope)
#define GCSV(B, key) (hash_get(B, CS(B)->variables, key))
#define SCSV(B, key, value) (hash_set(B, CS(B)->variables, key, value))
#define DCSV(B, key) (hash_remove(B, CS(B)->variables, key))
#define SGSV(B, key, value) (hash_set(B, GS(B)->variables, key, value))
#define GGSV(B, key) (hash_get(B, GS(B)->variables, key))
#define TV_MALLOC malloc(sizeof(TValue))
typedef struct bean_State {
size_t allocateBytes;
struct global_State *l_G;
struct LexState * ls;
} bean_State;
typedef struct Scope {
struct Scope *previous;
Hash * variables;
} Scope;
typedef struct callStack {
TValue * retVal;
bool target;
struct callStack * next;
} callStack;
typedef struct loopStack {
TValue * retVal;
bool target;
struct loopStack * next;
} loopStack;
typedef struct global_State {
unsigned int seed; /* randomized seed for hashes */
GCObject *allgc; /* list of all collectable objects */
stringtable strt;
Scope * globalScope;
Scope * cScope;
TValue * nil; // nil
TValue * tVal;
TValue * fVal;
TValue * meta; // Prototype for Meta
TValue * nproto; // Prototype for Number
TValue * sproto; // Prototype for String
TValue * aproto; // Prototype for Array
TValue * hproto; // Prototype for Hash
TValue * mproto; // Prototype for Math
TValue * rproto; // Prototype for Regex
TValue * dproto; // Prototype for Date
TValue * fproto; // Prototype for Function
TValue * bproto; // Prototype for Boolean
TValue * netproto; // Prototype for Http
callStack * callStack;
loopStack * loopStack;
Mbuffer * instructionStream;
Ibuffer * linenomap;
char * instructionIndex;
} global_State;
typedef struct Proto {
TString * name;
TString ** args;
bu_byte arity;
bu_byte assign;
} Proto;
typedef TValue * (*primitive_Fn) (bean_State * B, TValue * this, TValue * args, int argc);
typedef struct Tool {
primitive_Fn function;
TValue * context;
bool getter;
} Tool;
typedef struct Fn {
TString * name;
size_t address;
TValue * context;
} Fn;
#define add(a, b) (a + b)
#define sub(a, b) (a - b)
#define mul(a, b) (a * b)
#define div(a, b) (a / b)
#define eq(a, b) (a == b)
#define gte(a, b) (a >= b)
#define gt(a, b) (a > b)
#define lte(a, b) (a <= b)
#define lt(a, b) (a < b)
#define neq(a, b) (a != b)
#define shr(a, b) ((long)a >> (long)b)
#define shl(a, b) ((long)a << (long)b)
#define and(a, b) (a && b)
#define or(a, b) (a || b)
#define logic_and(a, b) ((long)a & (long)b)
#define logic_or(a, b) ((long)a | (long)b)
#define logic_xor(a, b) ((long)a ^ (long)b)
#define mod(a, b) ((long)a % (long)b)
#define BEAN_OK 1
#define BEAN_FAIL 0
void global_init(bean_State * B);
void run_file(const char * path);
void run();
void enter_scope(bean_State * B);
void leave_scope(bean_State * B);
void call_stack_create_frame(bean_State * B);
void call_stack_restore_frame(bean_State * B);
char call_stack_peek(bean_State * B);
TValue * call_stack_peek_return(bean_State * B);
Tool * initialize_tool_by_fn(primitive_Fn fn, bool getter);
void set_prototype_function(bean_State *B, const char * method, uint32_t len, primitive_Fn fn, Hash * h);
void set_prototype_getter(bean_State *B, const char * method, uint32_t len, primitive_Fn fn, Hash * h);
void set_self_before_caling(bean_State * B, TValue * context);
void exception();
void assert_with_message(bool condition, char * message);
#endif