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 "kamid.h"
22 enum {
23 /* literals */
24 V_SYM,
25 V_STR,
26 V_NUM,
28 /* foreign */
29 V_QID,
31 /* casted */
32 V_U8,
33 V_U16,
34 V_U32,
35 };
37 struct value {
38 int type;
39 union {
40 char *str;
41 uint64_t num;
42 uint8_t qid[QIDSIZE];
43 } v;
44 };
46 enum {
47 OP_ASSIGN,
48 OP_ASSERT,
49 OP_FUNCALL,
50 OP_LITERAL,
51 OP_VAR,
52 OP_CAST,
53 OP_CMP_EQ,
54 };
56 struct proc;
58 struct op {
59 struct op *next;
60 int type;
61 union {
62 struct {
63 char *name;
64 struct op *expr;
65 } assign;
66 struct op *assert;
67 struct {
68 struct proc *proc;
69 struct op *argv;
70 int argc;
71 } funcall;
72 struct value literal;
73 char *var;
74 struct {
75 struct op *expr;
76 int totype;
77 } cast;
78 struct {
79 struct op *a;
80 struct op *b;
81 } cmp_eq;
82 } v;
83 };
85 TAILQ_HEAD(procs, proc);
86 struct proc {
87 TAILQ_ENTRY(proc) entry;
88 char *name;
89 int minargs;
90 int varargs;
91 char *args[MAXWELEM];
92 struct op tmp_args;
93 struct op *body;
94 int (*nativefn)(int);
95 };
97 TAILQ_HEAD(tests, test);
98 struct test {
99 TAILQ_ENTRY(test) entry;
100 char *name;
101 char *dir;
102 struct proc *proc;
103 };
105 enum {
106 TEST_PASSED,
107 TEST_FAILED,
108 TEST_SKIPPED,
109 };
111 void global_set(char *, struct op *);
113 struct op *newop(int);
114 void free_op(struct op *);
115 struct op *op_assign(char *, struct op *);
116 struct op *op_assert(struct op *);
117 struct op *op_var(char *);
118 struct op *op_lit_str(char *);
119 struct op *op_lit_num(uint64_t);
120 struct op *op_cmp_eq(struct op *, struct op *);
121 struct op *op_cast(struct op *, int);
123 void pp_val(struct value *);
124 int val_trueish(struct value *);
125 int val_eq(struct value *, struct value *);
126 void pp_op(struct op *);
127 void pp_block(struct op *);
128 int eval(struct op *);
130 /* funcall */
131 void prepare_funcall(struct op *);
132 void push_arg(struct op *);
133 struct op *op_funcall(struct proc *, struct op *);
135 /* proc */
136 void add_builtin_proc(const char *name, int (*)(int));
137 void prepare_proc(char *);
138 /* push_arg works on procs too */
139 void proc_setup_body(void);
140 void proc_done(void);
141 void block_push(struct op *);
142 struct proc *proc_by_name(const char *);
144 /* testing */
145 void prepare_test(char *, char *);
146 void test_done(void);
148 /* np.y */
149 void loadfile(const char *);
151 #endif