forked from cesanta/v7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v7.h
122 lines (106 loc) · 4.17 KB
/
v7.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
// Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com>
// Copyright (c) 2013-2014 Cesanta Software Limited
// All rights reserved
//
// This software is dual-licensed: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation. For the terms of this
// license, see <http://www.gnu.org/licenses/>.
//
// You are free to use this software under the terms of the GNU General
// Public License, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// Alternatively, you can license this software under a commercial
// license, as set out in <http://cesanta.com/products.html>.
#ifndef V7_HEADER_INCLUDED
#define V7_HEADER_INCLUDED
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#define V7_VERSION "1.0"
enum v7_type {
V7_UNDEF, V7_NULL, V7_OBJ, V7_NUM, V7_STR, V7_BOOL, V7_FUNC, V7_C_FUNC,
V7_RO_PROP
};
enum v7_err {
V7_OK, V7_SYNTAX_ERROR, V7_OUT_OF_MEMORY, V7_INTERNAL_ERROR,
V7_STACK_OVERFLOW, V7_STACK_UNDERFLOW, V7_UNDEFINED_VARIABLE,
V7_TYPE_MISMATCH, V7_RECURSION_TOO_DEEP, V7_CALLED_NON_FUNCTION
};
struct v7;
struct v7_val;
struct v7_prop;
typedef void (*v7_func_t)(struct v7 *, struct v7_val *this_obj,
struct v7_val *result,
struct v7_val **params, int num_params);
typedef void (*v7_prop_func_t)(struct v7_val *this_obj, struct v7_val *result);
// A string.
struct v7_str {
char *buf; // Pointer to buffer with string data
unsigned long len; // String length
};
union v7_scalar {
char *func;
struct v7_str str;
double num;
v7_func_t c_func;
v7_prop_func_t prop_func;
struct v7_prop *props;
};
struct v7_prop {
struct v7_prop *next;
struct v7_val *key;
struct v7_val *val;
};
struct v7_val {
struct v7_val *next;
struct v7_val *proto; // Prototype
enum v7_type type; // Value type
unsigned short ref_count; // Reference counter XXX make it signed
unsigned short flags;
union v7_scalar v; // The value itself
};
struct v7 {
struct v7_val *stack[200];
struct v7_val scopes[20]; // Namespace objects (scopes)
int sp; // Stack pointer
int current_scope; // Pointer to the current scope
const char *source_code; // Pointer to the source codeing
const char *cursor; // Current parsing position
const char *tok; // Parsed terminal token (ident, number, string)
unsigned long tok_len; // Length of the parsed terminal token
int line_no; // Line number
int no_exec; // No-execute flag. For parsing function defs
struct v7_val *cur_obj; // Current namespace object ('x=1; x.y=1;', etc)
struct v7_val *values;
};
struct v7 *v7_create(void);
void v7_destroy(struct v7 **);
enum v7_err v7_exec(struct v7 *, const char *source_code);
enum v7_err v7_exec_file(struct v7 *, const char *path);
enum v7_err v7_push(struct v7 *v7, struct v7_val *v);
enum v7_err v7_make_and_push(struct v7 *v7, enum v7_type type);
enum v7_err v7_call(struct v7 *v7, int num_args);
enum v7_err v7_set(struct v7 *v7, struct v7_val *obj, struct v7_val *key,
struct v7_val *val);
enum v7_err v7_set_func(struct v7 *, struct v7_val *, const char *, v7_func_t);
enum v7_err v7_set_num(struct v7 *, struct v7_val *, const char *, double);
enum v7_err v7_set_str(struct v7 *, struct v7_val *, const char *,
const char *str, unsigned long str_len);
enum v7_err v7_set_obj(struct v7 *, struct v7_val *, const char *,
struct v7_val *);
struct v7_val *v7_mkval(struct v7 *v7, enum v7_type type);
struct v7_val *v7_lookup(struct v7_val *obj, const char *key);
struct v7_val *v7_get_root_namespace(struct v7 *);
int v7_sp(struct v7 *v7);
struct v7_val **v7_top(struct v7 *);
const char *v7_to_string(const struct v7_val *v, char *buf, int bsiz);
struct v7_val v7_str_to_val(const char *buf);
const char *v7_err_to_str(enum v7_err);
void v7_init_stdlib(struct v7 *);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // V7_HEADER_INCLUDED