]> git.lizzy.rs Git - nothing.git/blob - src/script/expr.h
62db4d83922f1a86ae754b23d0b4a82f8f14a576
[nothing.git] / src / script / expr.h
1 #ifndef ATOM_H_
2 #define ATOM_H_
3
4 #include <stdlib.h>
5 #include <stdbool.h>
6
7 typedef struct Gc Gc;
8
9 struct Cons;
10 struct Atom;
11
12 #define NUMBER(G, X) atom_as_expr(create_number_atom(G, X))
13 #define STRING(G, S) atom_as_expr(create_string_atom(G, S, NULL))
14 #define SYMBOL(G, S) atom_as_expr(create_symbol_atom(G, S, NULL))
15 #define CONS(G, CAR, CDR) cons_as_expr(create_cons(G, CAR, CDR))
16 #define NIL(G) SYMBOL(G, "nil")
17
18 enum ExprType
19 {
20     EXPR_ATOM = 0,
21     EXPR_CONS,
22     EXPR_VOID
23 };
24
25 // TODO(#285): there is no way to execute struct Expr
26 struct Expr
27 {
28     enum ExprType type;
29     union {
30         struct Cons *cons;
31         struct Atom *atom;
32     };
33 };
34
35 struct Expr atom_as_expr(struct Atom *atom);
36 struct Expr cons_as_expr(struct Cons *cons);
37 struct Expr void_expr(void);
38 struct Expr clone_expr(Gc *gc, struct Expr expr);
39
40 void destroy_expr_rec(struct Expr expr);
41 void destroy_expr(struct Expr expr);
42 void print_expr_as_sexpr(struct Expr expr);
43
44 enum AtomType
45 {
46     ATOM_SYMBOL = 0,
47     ATOM_NUMBER,
48     ATOM_STRING,
49 };
50
51 struct Atom
52 {
53     enum AtomType type;
54     union
55     {
56         float num;             // ATOM_NUMBER
57         char *sym;             // ATOM_SYMBOL
58         char *str;             // ATOM_STRING
59     };
60 };
61
62 struct Atom *create_number_atom(Gc *gc, float num);
63 struct Atom *create_string_atom(Gc *gc, const char *str, const char *str_end);
64 struct Atom *create_symbol_atom(Gc *gc, const char *sym, const char *sym_end);
65 void destroy_atom(struct Atom *atom);
66 void print_atom_as_sexpr(struct Atom *atom);
67
68 struct Cons
69 {
70     struct Expr car;
71     struct Expr cdr;
72 };
73
74 struct Cons *create_cons(Gc *gc, struct Expr car, struct Expr cdr);
75 void destroy_cons(struct Cons *cons);
76 void destroy_cons_rec(struct Cons *cons);
77 void print_cons_as_sexpr(struct Cons *cons);
78
79 #endif  // ATOM_H_