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