]> git.lizzy.rs Git - nothing.git/blob - src/script/expr.h
TODO(#330)
[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 // TODO(#321): get rid of gc argument from expr macros (just assume that it's `gc` in the current scope)
13 #define NUMBER(G, X) atom_as_expr(create_number_atom(G, X))
14 #define STRING(G, S) atom_as_expr(create_string_atom(G, S, NULL))
15 #define SYMBOL(G, S) atom_as_expr(create_symbol_atom(G, S, NULL))
16 #define CONS(G, CAR, CDR) cons_as_expr(create_cons(G, CAR, CDR))
17 #define NIL(G) SYMBOL(G, "nil")
18
19 enum ExprType
20 {
21     EXPR_ATOM = 0,
22     EXPR_CONS,
23     EXPR_VOID
24 };
25
26 // TODO(#285): there is no way to execute struct Expr
27 struct Expr
28 {
29     enum ExprType type;
30     union {
31         struct Cons *cons;
32         struct Atom *atom;
33     };
34 };
35
36 struct Expr atom_as_expr(struct Atom *atom);
37 struct Expr cons_as_expr(struct Cons *cons);
38 struct Expr void_expr(void);
39
40 void destroy_expr(struct Expr expr);
41 void print_expr_as_sexpr(struct Expr expr);
42
43 enum AtomType
44 {
45     ATOM_SYMBOL = 0,
46     ATOM_NUMBER,
47     ATOM_STRING,
48 };
49
50 struct Atom
51 {
52     enum AtomType type;
53     union
54     {
55         // TODO(#330): Atom doesn't support floats
56         long int num;               // ATOM_NUMBER
57         char *sym;             // ATOM_SYMBOL
58         char *str;             // ATOM_STRING
59     };
60 };
61
62 struct Atom *create_number_atom(Gc *gc, long int 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 print_cons_as_sexpr(struct Cons *cons);
77
78 #endif  // ATOM_H_