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