-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.h
111 lines (86 loc) · 2.33 KB
/
stack.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
#ifndef PHPINTERP_STACK_H
#define PHPINTERP_STACK_H
#include <inttypes.h>
#include <stddef.h>
#include <stdio.h>
#include <assert.h>
#include "crossplatform/stdnoreturn.h"
#include "enum-util.h"
#include "array-util.h"
#define ENUM_VARIANTTYPE(ELEMENT) \
ELEMENT(TYPE_UNDEF, =0) \
ELEMENT(TYPE_STRING,) \
ELEMENT(TYPE_LONG,) \
ELEMENT(TYPE_BOOL,) \
ELEMENT(TYPE_NULL,) \
ELEMENT(TYPE_CFUNCTION,) \
ELEMENT(TYPE_FUNCTION,) \
ELEMENT(TYPE_MAX_VALUE,)
DECLARE_ENUM(VARIANTTYPE, ENUM_VARIANTTYPE);
typedef struct State State;
typedef struct Scope Scope;
typedef struct Function Function;
typedef struct Runtime Runtime;
typedef void (CFunction(Runtime*));
typedef uint8_t codepoint_t;
typedef struct Variant {
VARIANTTYPE type;
union {
char* str;
int64_t lint;
bool boolean;
Function* function;
CFunction* cfunction;
} u;
} Variant;
static const int VAR_FLAG_CONST = 1 << 1;
typedef struct Variable {
char* name;
Variant value;
int flags;
} Variable;
typedef struct Runtime {
size_t stacksize;
size_t stackcapacity;
Function* function;
codepoint_t* ip;
Variant* stack;
Scope* scope;
State* state; // non-owning ptr
bool hasError;
char* file;
} Runtime;
static _Noreturn void die(char* msg) {
puts(msg);
abort();
}
static inline void try_stack_resize(Runtime* R)
{
try_resize(&R->stackcapacity, R->stacksize, (void*)&R->stack,
sizeof(*R->stack), die);
}
Variant* stackidx(Runtime* R, long idx);
void push(Runtime* R, Variant val);
static inline Variant* top(Runtime* R)
{
assert(R->stacksize >= 1);
return stackidx(R, -1);
}
void popn(Runtime *R, size_t count);
static inline void pop(Runtime* R) {
popn(R, 1);
}
void pushstr(Runtime* R, char* str);
void pushlong(Runtime* R, int64_t n);
void pushbool(Runtime* R, bool b);
void pushnull(Runtime* R);
void pushfunction(Runtime* R, Function* fn);
void pushcfunction(Runtime* R, CFunction* fn);
char* vartostring(Variant var);
char* tostring(Runtime* R, int idx);
int64_t vartolong(Variant var);
int64_t tolong(Runtime* R, int idx);
bool vartobool(Variant var);
bool tobool(Runtime* R, int idx);
Variant vartotype(Variant var, VARIANTTYPE type);
#endif //PHPINTERP_STACK_H