-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.c
120 lines (102 loc) · 2.63 KB
/
test.c
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
/**
* Author: Wang Renxin, [email protected]
* For the latest info, see https://github.com/paladin-t/xpl/
* Created: Oct. 14, 2011
* Last edited: Jun. 17, 2017
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/
#include "xpl.h"
static int _xpl_is_rsolidus(unsigned char _c) {
return _c == '\\';
}
static int _xpl_parse_escape(char** _d, const char** _s) {
int ret = 0;
if(*++*_s) {
switch(*(*_s)++) {
case '"':
*(*_d)++ = '"'; ret++;
break;
case '\\':
*(*_d)++ = '\\'; ret++;
break;
case '/':
*(*_d)++ = '/'; ret++;
break;
case 'b':
*(*_d)++ = '\b'; ret++;
break;
case 'f':
*(*_d)++ = '\f'; ret++;
break;
case 'n':
*(*_d)++ = '\n'; ret++;
break;
case 'r':
*(*_d)++ = '\r'; ret++;
break;
case 't':
*(*_d)++ = '\t'; ret++;
break;
default:
break;
}
}
return ret;
}
static xpl_status_t test1(xpl_context_t* _s) {
double f = 0.0;
printf("test1\n");
if(xpl_has_param(_s) == XS_OK) {
xpl_pop_double(_s, &f);
printf("has_param %f\n", f);
}
return XS_OK;
}
static xpl_status_t test2(xpl_context_t* _s) {
char buf[64] = { '\0' };
printf("test2\n");
if(xpl_has_param(_s) == XS_OK) {
xpl_pop_string(_s, buf, 64);
printf("has_param %s\n", buf);
}
return XS_OK;
}
static xpl_status_t test3(xpl_context_t* _s) {
printf("test3\n");
return XS_OK;
}
static xpl_status_t cond1(xpl_context_t* _s) {
printf("cond1\n");
xpl_push_bool(_s, 0);
return XS_OK;
}
static xpl_status_t cond2(xpl_context_t* _s) {
printf("cond2\n");
xpl_push_bool(_s, 1);
return XS_OK;
}
static xpl_context_t xpl;
int main() {
XPL_FUNC_BEGIN(funcs)
XPL_FUNC_ADD("test3", test3)
XPL_FUNC_ADD("test2", test2)
XPL_FUNC_ADD("test1", test1)
XPL_FUNC_ADD("cond2", cond2)
XPL_FUNC_ADD("cond1", cond1)
XPL_FUNC_END
xpl_open(&xpl, funcs, NULL);
xpl.escape_detect = _xpl_is_rsolidus;
xpl.escape_parse = _xpl_parse_escape;
xpl_load(&xpl, "if cond1 then test1 3.14 elseif cond2 then test2 \"hello world\" else test3 endif");
xpl_run(&xpl);
xpl_load(&xpl, "if cond1 then if cond2 3 then test3 elseif cond2 then test3 endif test3 endif test2 \"hello world\"");
xpl_run(&xpl);
xpl_unload(&xpl);
xpl_close(&xpl);
return 0;
}