Blame


1 c734c0e9 2021-08-03 op /*
2 c734c0e9 2021-08-03 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 c734c0e9 2021-08-03 op *
4 c734c0e9 2021-08-03 op * Permission to use, copy, modify, and distribute this software for any
5 c734c0e9 2021-08-03 op * purpose with or without fee is hereby granted, provided that the above
6 c734c0e9 2021-08-03 op * copyright notice and this permission notice appear in all copies.
7 c734c0e9 2021-08-03 op *
8 c734c0e9 2021-08-03 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 c734c0e9 2021-08-03 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 c734c0e9 2021-08-03 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 c734c0e9 2021-08-03 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 c734c0e9 2021-08-03 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 c734c0e9 2021-08-03 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 c734c0e9 2021-08-03 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 c734c0e9 2021-08-03 op */
16 c734c0e9 2021-08-03 op
17 c734c0e9 2021-08-03 op #ifndef SCRIPT_H
18 c734c0e9 2021-08-03 op #define SCRIPT_H
19 c734c0e9 2021-08-03 op
20 da4de0c1 2021-08-04 op #include "compat.h"
21 da4de0c1 2021-08-04 op
22 da4de0c1 2021-08-04 op #include <stdio.h>
23 da4de0c1 2021-08-04 op
24 c734c0e9 2021-08-03 op #include "kamid.h"
25 c734c0e9 2021-08-03 op
26 c734c0e9 2021-08-03 op enum {
27 c734c0e9 2021-08-03 op /* literals */
28 c734c0e9 2021-08-03 op V_SYM,
29 c734c0e9 2021-08-03 op V_STR,
30 c734c0e9 2021-08-03 op V_NUM,
31 c734c0e9 2021-08-03 op
32 c734c0e9 2021-08-03 op /* foreign */
33 9820edbf 2021-08-06 op V_MSG,
34 983e73a6 2021-08-08 op V_QIDVEC,
35 c734c0e9 2021-08-03 op V_QID,
36 c734c0e9 2021-08-03 op
37 c734c0e9 2021-08-03 op /* casted */
38 c734c0e9 2021-08-03 op V_U8,
39 c734c0e9 2021-08-03 op V_U16,
40 c734c0e9 2021-08-03 op V_U32,
41 c734c0e9 2021-08-03 op };
42 c734c0e9 2021-08-03 op
43 c734c0e9 2021-08-03 op struct value {
44 c734c0e9 2021-08-03 op int type;
45 c734c0e9 2021-08-03 op union {
46 c734c0e9 2021-08-03 op char *str;
47 ae3939af 2021-08-04 op int64_t num;
48 ae3939af 2021-08-04 op uint8_t u8;
49 ae3939af 2021-08-04 op uint16_t u16;
50 ae3939af 2021-08-04 op uint32_t u32;
51 9820edbf 2021-08-06 op struct {
52 9820edbf 2021-08-06 op uint8_t *msg;
53 9820edbf 2021-08-06 op size_t len;
54 9820edbf 2021-08-06 op } msg;
55 983e73a6 2021-08-08 op struct {
56 983e73a6 2021-08-08 op uint8_t *start;
57 983e73a6 2021-08-08 op size_t len;
58 983e73a6 2021-08-08 op } qidvec;
59 c734c0e9 2021-08-03 op uint8_t qid[QIDSIZE];
60 c734c0e9 2021-08-03 op } v;
61 c734c0e9 2021-08-03 op };
62 c734c0e9 2021-08-03 op
63 c734c0e9 2021-08-03 op enum {
64 0e62706a 2021-08-04 op OP_REST,
65 c734c0e9 2021-08-03 op OP_ASSIGN,
66 c734c0e9 2021-08-03 op OP_ASSERT,
67 c734c0e9 2021-08-03 op OP_FUNCALL,
68 c734c0e9 2021-08-03 op OP_LITERAL,
69 c734c0e9 2021-08-03 op OP_VAR,
70 c734c0e9 2021-08-03 op OP_CAST,
71 c734c0e9 2021-08-03 op OP_CMP_EQ,
72 acf1403a 2021-08-05 op OP_FACCESS,
73 8250ab19 2021-08-07 op OP_SFAIL,
74 983e73a6 2021-08-08 op OP_VARGS,
75 c734c0e9 2021-08-03 op };
76 c734c0e9 2021-08-03 op
77 c734c0e9 2021-08-03 op struct proc;
78 c734c0e9 2021-08-03 op
79 c734c0e9 2021-08-03 op struct op {
80 c734c0e9 2021-08-03 op struct op *next;
81 c734c0e9 2021-08-03 op int type;
82 c734c0e9 2021-08-03 op union {
83 c734c0e9 2021-08-03 op struct {
84 c734c0e9 2021-08-03 op char *name;
85 c734c0e9 2021-08-03 op struct op *expr;
86 c734c0e9 2021-08-03 op } assign;
87 c734c0e9 2021-08-03 op struct op *assert;
88 c734c0e9 2021-08-03 op struct {
89 c734c0e9 2021-08-03 op struct proc *proc;
90 c734c0e9 2021-08-03 op struct op *argv;
91 c734c0e9 2021-08-03 op int argc;
92 c734c0e9 2021-08-03 op } funcall;
93 c734c0e9 2021-08-03 op struct value literal;
94 c734c0e9 2021-08-03 op char *var;
95 c734c0e9 2021-08-03 op struct {
96 c734c0e9 2021-08-03 op struct op *expr;
97 c734c0e9 2021-08-03 op int totype;
98 c734c0e9 2021-08-03 op } cast;
99 c734c0e9 2021-08-03 op struct {
100 c734c0e9 2021-08-03 op struct op *a;
101 c734c0e9 2021-08-03 op struct op *b;
102 c734c0e9 2021-08-03 op } cmp_eq;
103 acf1403a 2021-08-05 op struct {
104 acf1403a 2021-08-05 op struct op *expr;
105 acf1403a 2021-08-05 op char *field;
106 acf1403a 2021-08-05 op } faccess;
107 8250ab19 2021-08-07 op struct {
108 8250ab19 2021-08-07 op char *msg;
109 8250ab19 2021-08-07 op struct op *expr;
110 8250ab19 2021-08-07 op } sfail;
111 c734c0e9 2021-08-03 op } v;
112 c734c0e9 2021-08-03 op };
113 c734c0e9 2021-08-03 op
114 f9cd3e06 2021-08-04 op TAILQ_HEAD(bindings, binding);
115 f9cd3e06 2021-08-04 op struct binding {
116 bc298316 2021-08-05 op TAILQ_ENTRY(binding) entry;
117 f9cd3e06 2021-08-04 op char *name;
118 f9cd3e06 2021-08-04 op struct value val;
119 bc298316 2021-08-05 op
120 bc298316 2021-08-05 op /*
121 bc298316 2021-08-05 op * Hack to support varargs. We set a special variable named
122 bc298316 2021-08-05 op * "..." that contains the list of ops that will evaluate to
123 bc298316 2021-08-05 op * the arguments.
124 bc298316 2021-08-05 op */
125 bc298316 2021-08-05 op struct op *raw;
126 f9cd3e06 2021-08-04 op };
127 f9cd3e06 2021-08-04 op
128 f9cd3e06 2021-08-04 op TAILQ_HEAD(envs, env);
129 f9cd3e06 2021-08-04 op struct env {
130 f9cd3e06 2021-08-04 op TAILQ_ENTRY(env) entry;
131 f9cd3e06 2021-08-04 op struct bindings bindings;
132 f9cd3e06 2021-08-04 op };
133 f9cd3e06 2021-08-04 op
134 d9d02161 2021-08-04 op TAILQ_HEAD(opstacks, opstack);
135 d9d02161 2021-08-04 op struct opstack {
136 d9d02161 2021-08-04 op TAILQ_ENTRY(opstack) entry;
137 d9d02161 2021-08-04 op struct op base;
138 d9d02161 2021-08-04 op struct op *last;
139 d9d02161 2021-08-04 op int counter;
140 d9d02161 2021-08-04 op };
141 d9d02161 2021-08-04 op
142 c734c0e9 2021-08-03 op TAILQ_HEAD(procs, proc);
143 c734c0e9 2021-08-03 op struct proc {
144 c734c0e9 2021-08-03 op TAILQ_ENTRY(proc) entry;
145 c734c0e9 2021-08-03 op char *name;
146 c734c0e9 2021-08-03 op int minargs;
147 54736a95 2021-08-04 op int vararg;
148 c734c0e9 2021-08-03 op char *args[MAXWELEM];
149 c734c0e9 2021-08-03 op struct op *body;
150 c734c0e9 2021-08-03 op int (*nativefn)(int);
151 c734c0e9 2021-08-03 op };
152 c734c0e9 2021-08-03 op
153 c734c0e9 2021-08-03 op TAILQ_HEAD(tests, test);
154 c734c0e9 2021-08-03 op struct test {
155 c734c0e9 2021-08-03 op TAILQ_ENTRY(test) entry;
156 0b453b63 2021-08-07 op int shouldfail;
157 c734c0e9 2021-08-03 op char *name;
158 c734c0e9 2021-08-03 op char *dir;
159 d9d02161 2021-08-04 op struct op *body;
160 c734c0e9 2021-08-03 op };
161 c734c0e9 2021-08-03 op
162 c734c0e9 2021-08-03 op enum {
163 333d8d7d 2021-08-04 op EVAL_OK,
164 333d8d7d 2021-08-04 op EVAL_ERR,
165 333d8d7d 2021-08-04 op EVAL_SKIP,
166 c734c0e9 2021-08-03 op };
167 c734c0e9 2021-08-03 op
168 f9cd3e06 2021-08-04 op int global_set(char *, struct op *);
169 c734c0e9 2021-08-03 op
170 c734c0e9 2021-08-03 op struct op *newop(int);
171 7c459187 2021-08-12 op void free_op_rec(struct op *);
172 c734c0e9 2021-08-03 op void free_op(struct op *);
173 0e62706a 2021-08-04 op struct op *op_rest(void);
174 c734c0e9 2021-08-03 op struct op *op_assign(char *, struct op *);
175 c734c0e9 2021-08-03 op struct op *op_assert(struct op *);
176 c734c0e9 2021-08-03 op struct op *op_var(char *);
177 c734c0e9 2021-08-03 op struct op *op_lit_str(char *);
178 c734c0e9 2021-08-03 op struct op *op_lit_num(uint64_t);
179 c734c0e9 2021-08-03 op struct op *op_cmp_eq(struct op *, struct op *);
180 c734c0e9 2021-08-03 op struct op *op_cast(struct op *, int);
181 acf1403a 2021-08-05 op struct op *op_faccess(struct op *, char *);
182 8250ab19 2021-08-07 op struct op *op_sfail(struct op *, char *);
183 983e73a6 2021-08-08 op struct op *op_vargs(void);
184 c734c0e9 2021-08-03 op
185 da4de0c1 2021-08-04 op void ppf_val(FILE *, struct value *);
186 c734c0e9 2021-08-03 op void pp_val(struct value *);
187 7a495cc0 2021-12-02 op void pp_val(struct value *);
188 7c459187 2021-08-12 op const char *val_type(struct value *);
189 c734c0e9 2021-08-03 op int val_trueish(struct value *);
190 7a495cc0 2021-12-02 op int val_isnum(struct value *);
191 7a495cc0 2021-12-02 op int64_t val_tonum(struct value *);
192 c734c0e9 2021-08-03 op int val_eq(struct value *, struct value *);
193 da4de0c1 2021-08-04 op int val_cast(struct value *, int);
194 acf1403a 2021-08-05 op int val_faccess(struct value *, const char *, struct value *);
195 c734c0e9 2021-08-03 op void pp_op(struct op *);
196 c734c0e9 2021-08-03 op void pp_block(struct op *);
197 c734c0e9 2021-08-03 op int eval(struct op *);
198 c734c0e9 2021-08-03 op
199 c734c0e9 2021-08-03 op /* funcall */
200 d9d02161 2021-08-04 op void prepare_funcall(void);
201 c734c0e9 2021-08-03 op void push_arg(struct op *);
202 d9d02161 2021-08-04 op struct op *op_funcall(struct proc *);
203 c734c0e9 2021-08-03 op
204 c734c0e9 2021-08-03 op /* proc */
205 54736a95 2021-08-04 op void add_builtin_proc(const char *name, int (*)(int), int, int);
206 d9d02161 2021-08-04 op void prepare_proc(void);
207 c734c0e9 2021-08-03 op /* push_arg works on procs too */
208 d9d02161 2021-08-04 op int proc_setup_body(void);
209 d9d02161 2021-08-04 op void proc_done(char *name);
210 c734c0e9 2021-08-03 op void block_push(struct op *);
211 c734c0e9 2021-08-03 op struct proc *proc_by_name(const char *);
212 c734c0e9 2021-08-03 op
213 c734c0e9 2021-08-03 op /* testing */
214 d9d02161 2021-08-04 op void prepare_test(void);
215 0b453b63 2021-08-07 op void test_done(int, char *, char *);
216 c734c0e9 2021-08-03 op
217 c734c0e9 2021-08-03 op /* np.y */
218 c734c0e9 2021-08-03 op void loadfile(const char *);
219 c734c0e9 2021-08-03 op
220 c734c0e9 2021-08-03 op #endif