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 c734c0e9 2021-08-03 op V_QID,
35 c734c0e9 2021-08-03 op
36 c734c0e9 2021-08-03 op /* casted */
37 c734c0e9 2021-08-03 op V_U8,
38 c734c0e9 2021-08-03 op V_U16,
39 c734c0e9 2021-08-03 op V_U32,
40 c734c0e9 2021-08-03 op };
41 c734c0e9 2021-08-03 op
42 c734c0e9 2021-08-03 op struct value {
43 c734c0e9 2021-08-03 op int type;
44 c734c0e9 2021-08-03 op union {
45 c734c0e9 2021-08-03 op char *str;
46 ae3939af 2021-08-04 op int64_t num;
47 ae3939af 2021-08-04 op uint8_t u8;
48 ae3939af 2021-08-04 op uint16_t u16;
49 ae3939af 2021-08-04 op uint32_t u32;
50 9820edbf 2021-08-06 op struct {
51 9820edbf 2021-08-06 op uint8_t *msg;
52 9820edbf 2021-08-06 op size_t len;
53 9820edbf 2021-08-06 op } msg;
54 c734c0e9 2021-08-03 op uint8_t qid[QIDSIZE];
55 c734c0e9 2021-08-03 op } v;
56 c734c0e9 2021-08-03 op };
57 c734c0e9 2021-08-03 op
58 c734c0e9 2021-08-03 op enum {
59 0e62706a 2021-08-04 op OP_REST,
60 c734c0e9 2021-08-03 op OP_ASSIGN,
61 c734c0e9 2021-08-03 op OP_ASSERT,
62 c734c0e9 2021-08-03 op OP_FUNCALL,
63 c734c0e9 2021-08-03 op OP_LITERAL,
64 c734c0e9 2021-08-03 op OP_VAR,
65 c734c0e9 2021-08-03 op OP_CAST,
66 c734c0e9 2021-08-03 op OP_CMP_EQ,
67 acf1403a 2021-08-05 op OP_FACCESS,
68 8250ab19 2021-08-07 op OP_SFAIL,
69 c734c0e9 2021-08-03 op };
70 c734c0e9 2021-08-03 op
71 c734c0e9 2021-08-03 op struct proc;
72 c734c0e9 2021-08-03 op
73 c734c0e9 2021-08-03 op struct op {
74 c734c0e9 2021-08-03 op struct op *next;
75 c734c0e9 2021-08-03 op int type;
76 c734c0e9 2021-08-03 op union {
77 c734c0e9 2021-08-03 op struct {
78 c734c0e9 2021-08-03 op char *name;
79 c734c0e9 2021-08-03 op struct op *expr;
80 c734c0e9 2021-08-03 op } assign;
81 c734c0e9 2021-08-03 op struct op *assert;
82 c734c0e9 2021-08-03 op struct {
83 c734c0e9 2021-08-03 op struct proc *proc;
84 c734c0e9 2021-08-03 op struct op *argv;
85 c734c0e9 2021-08-03 op int argc;
86 c734c0e9 2021-08-03 op } funcall;
87 c734c0e9 2021-08-03 op struct value literal;
88 c734c0e9 2021-08-03 op char *var;
89 c734c0e9 2021-08-03 op struct {
90 c734c0e9 2021-08-03 op struct op *expr;
91 c734c0e9 2021-08-03 op int totype;
92 c734c0e9 2021-08-03 op } cast;
93 c734c0e9 2021-08-03 op struct {
94 c734c0e9 2021-08-03 op struct op *a;
95 c734c0e9 2021-08-03 op struct op *b;
96 c734c0e9 2021-08-03 op } cmp_eq;
97 acf1403a 2021-08-05 op struct {
98 acf1403a 2021-08-05 op struct op *expr;
99 acf1403a 2021-08-05 op char *field;
100 acf1403a 2021-08-05 op } faccess;
101 8250ab19 2021-08-07 op struct {
102 8250ab19 2021-08-07 op char *msg;
103 8250ab19 2021-08-07 op struct op *expr;
104 8250ab19 2021-08-07 op } sfail;
105 c734c0e9 2021-08-03 op } v;
106 c734c0e9 2021-08-03 op };
107 c734c0e9 2021-08-03 op
108 f9cd3e06 2021-08-04 op TAILQ_HEAD(bindings, binding);
109 f9cd3e06 2021-08-04 op struct binding {
110 bc298316 2021-08-05 op TAILQ_ENTRY(binding) entry;
111 f9cd3e06 2021-08-04 op char *name;
112 f9cd3e06 2021-08-04 op struct value val;
113 bc298316 2021-08-05 op
114 bc298316 2021-08-05 op /*
115 bc298316 2021-08-05 op * Hack to support varargs. We set a special variable named
116 bc298316 2021-08-05 op * "..." that contains the list of ops that will evaluate to
117 bc298316 2021-08-05 op * the arguments.
118 bc298316 2021-08-05 op */
119 bc298316 2021-08-05 op struct op *raw;
120 f9cd3e06 2021-08-04 op };
121 f9cd3e06 2021-08-04 op
122 f9cd3e06 2021-08-04 op TAILQ_HEAD(envs, env);
123 f9cd3e06 2021-08-04 op struct env {
124 f9cd3e06 2021-08-04 op TAILQ_ENTRY(env) entry;
125 f9cd3e06 2021-08-04 op struct bindings bindings;
126 f9cd3e06 2021-08-04 op };
127 f9cd3e06 2021-08-04 op
128 d9d02161 2021-08-04 op TAILQ_HEAD(opstacks, opstack);
129 d9d02161 2021-08-04 op struct opstack {
130 d9d02161 2021-08-04 op TAILQ_ENTRY(opstack) entry;
131 d9d02161 2021-08-04 op struct op base;
132 d9d02161 2021-08-04 op struct op *last;
133 d9d02161 2021-08-04 op int counter;
134 d9d02161 2021-08-04 op };
135 d9d02161 2021-08-04 op
136 c734c0e9 2021-08-03 op TAILQ_HEAD(procs, proc);
137 c734c0e9 2021-08-03 op struct proc {
138 c734c0e9 2021-08-03 op TAILQ_ENTRY(proc) entry;
139 c734c0e9 2021-08-03 op char *name;
140 c734c0e9 2021-08-03 op int minargs;
141 54736a95 2021-08-04 op int vararg;
142 c734c0e9 2021-08-03 op char *args[MAXWELEM];
143 c734c0e9 2021-08-03 op struct op *body;
144 c734c0e9 2021-08-03 op int (*nativefn)(int);
145 c734c0e9 2021-08-03 op };
146 c734c0e9 2021-08-03 op
147 c734c0e9 2021-08-03 op TAILQ_HEAD(tests, test);
148 c734c0e9 2021-08-03 op struct test {
149 c734c0e9 2021-08-03 op TAILQ_ENTRY(test) entry;
150 0b453b63 2021-08-07 op int shouldfail;
151 c734c0e9 2021-08-03 op char *name;
152 c734c0e9 2021-08-03 op char *dir;
153 d9d02161 2021-08-04 op struct op *body;
154 c734c0e9 2021-08-03 op };
155 c734c0e9 2021-08-03 op
156 c734c0e9 2021-08-03 op enum {
157 333d8d7d 2021-08-04 op EVAL_OK,
158 333d8d7d 2021-08-04 op EVAL_ERR,
159 333d8d7d 2021-08-04 op EVAL_SKIP,
160 c734c0e9 2021-08-03 op };
161 c734c0e9 2021-08-03 op
162 f9cd3e06 2021-08-04 op int global_set(char *, struct op *);
163 c734c0e9 2021-08-03 op
164 c734c0e9 2021-08-03 op struct op *newop(int);
165 c734c0e9 2021-08-03 op void free_op(struct op *);
166 0e62706a 2021-08-04 op struct op *op_rest(void);
167 c734c0e9 2021-08-03 op struct op *op_assign(char *, struct op *);
168 c734c0e9 2021-08-03 op struct op *op_assert(struct op *);
169 c734c0e9 2021-08-03 op struct op *op_var(char *);
170 c734c0e9 2021-08-03 op struct op *op_lit_str(char *);
171 c734c0e9 2021-08-03 op struct op *op_lit_num(uint64_t);
172 c734c0e9 2021-08-03 op struct op *op_cmp_eq(struct op *, struct op *);
173 c734c0e9 2021-08-03 op struct op *op_cast(struct op *, int);
174 acf1403a 2021-08-05 op struct op *op_faccess(struct op *, char *);
175 8250ab19 2021-08-07 op struct op *op_sfail(struct op *, char *);
176 c734c0e9 2021-08-03 op
177 da4de0c1 2021-08-04 op void ppf_val(FILE *, struct value *);
178 c734c0e9 2021-08-03 op void pp_val(struct value *);
179 c734c0e9 2021-08-03 op int val_trueish(struct value *);
180 c734c0e9 2021-08-03 op int val_eq(struct value *, struct value *);
181 da4de0c1 2021-08-04 op int val_cast(struct value *, int);
182 acf1403a 2021-08-05 op int val_faccess(struct value *, const char *, struct value *);
183 c734c0e9 2021-08-03 op void pp_op(struct op *);
184 c734c0e9 2021-08-03 op void pp_block(struct op *);
185 c734c0e9 2021-08-03 op int eval(struct op *);
186 c734c0e9 2021-08-03 op
187 c734c0e9 2021-08-03 op /* funcall */
188 d9d02161 2021-08-04 op void prepare_funcall(void);
189 c734c0e9 2021-08-03 op void push_arg(struct op *);
190 d9d02161 2021-08-04 op struct op *op_funcall(struct proc *);
191 c734c0e9 2021-08-03 op
192 c734c0e9 2021-08-03 op /* proc */
193 54736a95 2021-08-04 op void add_builtin_proc(const char *name, int (*)(int), int, int);
194 d9d02161 2021-08-04 op void prepare_proc(void);
195 c734c0e9 2021-08-03 op /* push_arg works on procs too */
196 d9d02161 2021-08-04 op int proc_setup_body(void);
197 d9d02161 2021-08-04 op void proc_done(char *name);
198 c734c0e9 2021-08-03 op void block_push(struct op *);
199 c734c0e9 2021-08-03 op struct proc *proc_by_name(const char *);
200 c734c0e9 2021-08-03 op
201 c734c0e9 2021-08-03 op /* testing */
202 d9d02161 2021-08-04 op void prepare_test(void);
203 0b453b63 2021-08-07 op void test_done(int, char *, char *);
204 c734c0e9 2021-08-03 op
205 c734c0e9 2021-08-03 op /* np.y */
206 c734c0e9 2021-08-03 op void loadfile(const char *);
207 c734c0e9 2021-08-03 op
208 c734c0e9 2021-08-03 op #endif