changed author email
[guish.git] / src / evaluator.h
blob6aaf3b65fbbb761a02a7daf5babe26c6c55d7396
1 /*************************************************************************
2 * Copyright (C) 2024 Francesco Palumbo <phranz.dev@gmail.com>, Naples (Italy)
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 *************************************************************************/
18 #ifndef EVALUATOR_H
19 #define EVALUATOR_H
21 #define EV_QUITCALLED 1
22 #define EV_BREAK (1 << 1)
23 #define EV_RETURN (1 << 2)
25 #define err(S, L, M, ...) \
26 do { \
27 debug("-- ERROR --"); \
28 if (strace->count) { \
29 fprintf(stderr, "<<<< stack trace >>>:\n"); \
30 strline_t* trace; \
31 each(strace, trace, \
32 fprintf(stderr, " '%s(...)' at '%lu'\n", trace->key, trace->val); \
33 ); \
34 } \
35 if (S && L) \
36 sinfo(S, L); \
37 fprintf(stderr, M, ## __VA_ARGS__); \
38 exit(EXIT_FAILURE); \
39 } while (0)
41 #define warn(S, L, M, ...) \
42 do { \
43 if (S && L) \
44 sinfo(S, L); \
45 fprintf(stderr, M, ## __VA_ARGS__); \
46 } while (0)
48 #define assert_block(A, I, F) if (A->type != T_BLOCK) err(A->source, A->lineno, "error, '%s' %s argument must be a block!\n", F, I)
49 #define assert_notblock(A, I, F) if (A->type == T_BLOCK) err(A->source, A->lineno, "error, '%s' %s argument cannot be a block!\n", F, I)
51 typedef struct token_t token_t;
52 typedef struct vec_token_t phrase_t;
53 typedef struct vec_str_t vec_str_t;
54 typedef struct vec_strline_t vec_strlint_t;
56 typedef struct map_strstr_t map_strstr_t;
57 typedef struct map_strint_t map_strint_t;
59 typedef struct scope_t {
60 map_strtoken_t* memos;
61 void (*free)(struct scope_t*);
62 } scope_t;
64 typedef struct block_t {
65 vec_token_t* data;
66 scope_t* env;
67 void (*free)(struct block_t*);
68 } block_t;
70 scope_t* scope_t_init();
71 scope_t* copy_scope(scope_t*);
73 block_t* block_t_init();
74 block_t* copy_block(block_t*);
76 void evaluator_init();
77 void evaluator_free();
79 vec_token_t* parse(phrase_t*);
80 size_t eval_expressions(vec_token_t*, vec_token_t*);
81 size_t eval_statements(vec_token_t*, vec_token_t*);
82 token_t* repr(token_t*, int);
83 long tolong(token_t*);
84 void define(token_t*, token_t*, scope_t*, vec_token_t*);
85 void schedule();
86 int trigger(unsigned long, int);
88 #endif