]> git.lizzy.rs Git - nothing.git/blob - src/ebisp/expr.h
unpack_args -> match_list
[nothing.git] / src / ebisp / expr.h
1 #ifndef EXPR_H_
2 #define EXPR_H_
3
4 #include <stdbool.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 typedef struct Gc Gc;
9 typedef struct Scope Scope;
10
11 struct Cons;
12 struct Atom;
13
14 #define NUMBER(G, X) atom_as_expr(create_number_atom(G, X))
15 #define STRING(G, S) atom_as_expr(create_string_atom(G, S, NULL))
16 #define SYMBOL(G, S) atom_as_expr(create_symbol_atom(G, S, NULL))
17 #define NATIVE(G, F, P) atom_as_expr(create_native_atom(G, F, P))
18 #define CONS(G, CAR, CDR) cons_as_expr(create_cons(G, CAR, CDR))
19 #define NIL(G) SYMBOL(G, "nil")
20
21 #define CAR(O) ((O).cons->car)
22 #define CDR(O) ((O).cons->cdr)
23
24 enum ExprType
25 {
26     EXPR_ATOM = 0,
27     EXPR_CONS,
28     EXPR_VOID
29 };
30
31 struct Expr
32 {
33     enum ExprType type;
34     union {
35         struct Cons *cons;
36         // TODO(#500): Atom type is redundant
37         struct Atom *atom;
38     };
39 };
40
41 // Prototype to prevent https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54113
42 const char *expr_type_as_string(enum ExprType expr_type);
43
44 struct Expr atom_as_expr(struct Atom *atom);
45 struct Expr cons_as_expr(struct Cons *cons);
46 struct Expr void_expr(void);
47
48 void destroy_expr(struct Expr expr);
49 void print_expr_as_sexpr(FILE *stream, struct Expr expr);
50 int expr_as_sexpr(struct Expr expr, char *output, size_t n);
51
52 // TODO(#337): EvalResult does not belong to expr unit
53 struct EvalResult
54 {
55     bool is_error;
56     struct Expr expr;
57 };
58
59
60 typedef struct EvalResult (*NativeFunction)(void *param, Gc *gc, struct Scope *scope, struct Expr args);
61
62 struct Native
63 {
64     NativeFunction fun;
65     void *param;
66 };
67
68 enum AtomType
69 {
70     ATOM_SYMBOL = 0,
71     ATOM_NUMBER,
72     ATOM_STRING,
73     ATOM_NATIVE
74 };
75
76 const char *atom_type_as_string(enum AtomType atom_type);
77
78 struct Atom
79 {
80     enum AtomType type;
81     union
82     {
83         // TODO(#330): Atom doesn't support floats
84         long int num;           // ATOM_NUMBER
85         char *sym;              // ATOM_SYMBOL
86         char *str;              // ATOM_STRING
87         struct Native native;   // ATOM_NATIVE
88     };
89 };
90
91 struct Atom *create_number_atom(Gc *gc, long int num);
92 struct Atom *create_string_atom(Gc *gc, const char *str, const char *str_end);
93 struct Atom *create_symbol_atom(Gc *gc, const char *sym, const char *sym_end);
94 struct Atom *create_native_atom(Gc *gc, NativeFunction fun, void *param);
95 void destroy_atom(struct Atom *atom);
96 void print_atom_as_sexpr(FILE *stream, struct Atom *atom);
97
98 struct Cons
99 {
100     struct Expr car;
101     struct Expr cdr;
102 };
103
104 struct Cons *create_cons(Gc *gc, struct Expr car, struct Expr cdr);
105 void destroy_cons(struct Cons *cons);
106 void print_cons_as_sexpr(FILE *stream, struct Cons *cons);
107
108 #endif  // EXPR_H_