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