2 c734c0e9 2021-08-03 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
17 c734c0e9 2021-08-03 op #ifndef SCRIPT_H
18 c734c0e9 2021-08-03 op #define SCRIPT_H
20 da4de0c1 2021-08-04 op #include "compat.h"
22 da4de0c1 2021-08-04 op #include <stdio.h>
24 c734c0e9 2021-08-03 op #include "kamid.h"
27 c734c0e9 2021-08-03 op /* literals */
43 c734c0e9 2021-08-03 op struct value {
56 983e73a6 2021-08-08 op uint8_t *start;
59 c734c0e9 2021-08-03 op uint8_t qid[QIDSIZE];
80 c734c0e9 2021-08-03 op struct op *next;
85 c734c0e9 2021-08-03 op struct op *expr;
87 c734c0e9 2021-08-03 op struct op *assert;
89 c734c0e9 2021-08-03 op struct proc *proc;
90 c734c0e9 2021-08-03 op struct op *argv;
93 c734c0e9 2021-08-03 op struct value literal;
96 c734c0e9 2021-08-03 op struct op *expr;
100 c734c0e9 2021-08-03 op struct op *a;
101 c734c0e9 2021-08-03 op struct op *b;
104 acf1403a 2021-08-05 op struct op *expr;
109 8250ab19 2021-08-07 op struct op *expr;
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;
118 f9cd3e06 2021-08-04 op struct value val;
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.
125 bc298316 2021-08-05 op struct op *raw;
128 f9cd3e06 2021-08-04 op TAILQ_HEAD(envs, env);
130 f9cd3e06 2021-08-04 op TAILQ_ENTRY(env) entry;
131 f9cd3e06 2021-08-04 op struct bindings bindings;
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;
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;
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);
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;
159 d9d02161 2021-08-04 op struct op *body;
168 f9cd3e06 2021-08-04 op int global_set(char *, struct 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);
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 *);
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 *);
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 *);
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 *);
218 c734c0e9 2021-08-03 op void loadfile(const char *);