2 fb1a36c0 2022-01-09 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 fb1a36c0 2022-01-09 op * Permission to use, copy, modify, and distribute this software for any
5 fb1a36c0 2022-01-09 op * purpose with or without fee is hereby granted, provided that the above
6 fb1a36c0 2022-01-09 op * copyright notice and this permission notice appear in all copies.
8 fb1a36c0 2022-01-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 fb1a36c0 2022-01-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 fb1a36c0 2022-01-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 fb1a36c0 2022-01-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 fb1a36c0 2022-01-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 fb1a36c0 2022-01-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 fb1a36c0 2022-01-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 fb1a36c0 2022-01-09 op #ifndef SCRIPT_H
18 fb1a36c0 2022-01-09 op #define SCRIPT_H
21 fb1a36c0 2022-01-09 op /* literals */
37 fb1a36c0 2022-01-09 op struct value {
50 fb1a36c0 2022-01-09 op uint8_t *start;
53 fb1a36c0 2022-01-09 op uint8_t qid[QIDSIZE];
75 fb1a36c0 2022-01-09 op struct op *next;
80 fb1a36c0 2022-01-09 op struct op *expr;
82 fb1a36c0 2022-01-09 op struct op *assert;
84 fb1a36c0 2022-01-09 op struct proc *proc;
85 fb1a36c0 2022-01-09 op struct op *argv;
88 fb1a36c0 2022-01-09 op struct value literal;
91 fb1a36c0 2022-01-09 op struct op *expr;
99 fb1a36c0 2022-01-09 op struct op *expr;
104 fb1a36c0 2022-01-09 op struct op *expr;
109 fb1a36c0 2022-01-09 op TAILQ_HEAD(bindings, binding);
110 fb1a36c0 2022-01-09 op struct binding {
111 fb1a36c0 2022-01-09 op TAILQ_ENTRY(binding) entry;
113 fb1a36c0 2022-01-09 op struct value val;
116 fb1a36c0 2022-01-09 op * Hack to support varargs. We set a special variable named
117 fb1a36c0 2022-01-09 op * "..." that contains the list of ops that will evaluate to
118 fb1a36c0 2022-01-09 op * the arguments.
120 fb1a36c0 2022-01-09 op struct op *raw;
123 fb1a36c0 2022-01-09 op TAILQ_HEAD(envs, env);
125 fb1a36c0 2022-01-09 op TAILQ_ENTRY(env) entry;
126 fb1a36c0 2022-01-09 op struct bindings bindings;
129 fb1a36c0 2022-01-09 op TAILQ_HEAD(opstacks, opstack);
130 fb1a36c0 2022-01-09 op struct opstack {
131 fb1a36c0 2022-01-09 op TAILQ_ENTRY(opstack) entry;
132 fb1a36c0 2022-01-09 op struct op base;
133 fb1a36c0 2022-01-09 op struct op *last;
137 fb1a36c0 2022-01-09 op TAILQ_HEAD(procs, proc);
138 fb1a36c0 2022-01-09 op struct proc {
139 fb1a36c0 2022-01-09 op TAILQ_ENTRY(proc) entry;
143 fb1a36c0 2022-01-09 op char *args[MAXWELEM];
144 fb1a36c0 2022-01-09 op struct op *body;
145 fb1a36c0 2022-01-09 op int (*nativefn)(int);
148 fb1a36c0 2022-01-09 op TAILQ_HEAD(tests, test);
149 fb1a36c0 2022-01-09 op struct test {
150 fb1a36c0 2022-01-09 op TAILQ_ENTRY(test) entry;
151 fb1a36c0 2022-01-09 op int shouldfail;
153 fb1a36c0 2022-01-09 op struct op *body;
162 fb1a36c0 2022-01-09 op int global_set(char *, struct op *);
164 fb1a36c0 2022-01-09 op struct op *newop(int);
165 fb1a36c0 2022-01-09 op void free_op_rec(struct op *);
166 fb1a36c0 2022-01-09 op void free_op(struct op *);
167 fb1a36c0 2022-01-09 op struct op *op_rest(void);
168 fb1a36c0 2022-01-09 op struct op *op_assign(char *, struct op *);
169 fb1a36c0 2022-01-09 op struct op *op_assert(struct op *);
170 fb1a36c0 2022-01-09 op struct op *op_var(char *);
171 fb1a36c0 2022-01-09 op struct op *op_lit_str(char *);
172 fb1a36c0 2022-01-09 op struct op *op_lit_num(uint64_t);
173 fb1a36c0 2022-01-09 op struct op *op_cmp_eq(struct op *, struct op *);
174 fb1a36c0 2022-01-09 op struct op *op_cmp_leq(struct op *, struct op *);
175 fb1a36c0 2022-01-09 op struct op *op_cast(struct op *, int);
176 fb1a36c0 2022-01-09 op struct op *op_faccess(struct op *, char *);
177 fb1a36c0 2022-01-09 op struct op *op_sfail(struct op *, char *);
178 fb1a36c0 2022-01-09 op struct op *op_vargs(void);
180 fb1a36c0 2022-01-09 op void ppf_val(FILE *, struct value *);
181 fb1a36c0 2022-01-09 op void pp_val(struct value *);
182 fb1a36c0 2022-01-09 op void pp_val(struct value *);
183 fb1a36c0 2022-01-09 op const char *val_type(struct value *);
184 fb1a36c0 2022-01-09 op int val_trueish(struct value *);
185 fb1a36c0 2022-01-09 op int val_isnum(struct value *);
186 fb1a36c0 2022-01-09 op int64_t val_tonum(struct value *);
187 fb1a36c0 2022-01-09 op int val_eq(struct value *, struct value *);
188 fb1a36c0 2022-01-09 op int val_leq(struct value *, struct value *);
189 fb1a36c0 2022-01-09 op int val_cast(struct value *, int);
190 fb1a36c0 2022-01-09 op int val_faccess(struct value *, const char *, struct value *);
191 fb1a36c0 2022-01-09 op void pp_op(struct op *);
192 fb1a36c0 2022-01-09 op void pp_block(struct op *);
193 fb1a36c0 2022-01-09 op int eval(struct op *);
195 fb1a36c0 2022-01-09 op /* funcall */
196 fb1a36c0 2022-01-09 op void prepare_funcall(void);
197 fb1a36c0 2022-01-09 op void push_arg(struct op *);
198 fb1a36c0 2022-01-09 op struct op *op_funcall(struct proc *);
201 fb1a36c0 2022-01-09 op void add_builtin_proc(const char *name, int (*)(int), int, int);
202 fb1a36c0 2022-01-09 op void prepare_proc(void);
203 fb1a36c0 2022-01-09 op /* push_arg works on procs too */
204 fb1a36c0 2022-01-09 op int proc_setup_body(void);
205 fb1a36c0 2022-01-09 op void proc_done(char *name);
206 fb1a36c0 2022-01-09 op void block_push(struct op *);
207 fb1a36c0 2022-01-09 op struct proc *proc_by_name(const char *);
209 fb1a36c0 2022-01-09 op /* testing */
210 fb1a36c0 2022-01-09 op void prepare_test(void);
211 a0ec53a2 2022-01-09 op void test_done(int, char *);
214 fb1a36c0 2022-01-09 op void loadfile(const char *);