Blob


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