]> git.lizzy.rs Git - nothing.git/blob - src/script/expr.h
Merge pull request #309 from tsoding/285
[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
37 void destroy_expr(struct Expr expr);
38 void print_expr_as_sexpr(struct Expr expr);
39
40 enum AtomType
41 {
42     ATOM_SYMBOL = 0,
43     ATOM_NUMBER,
44     ATOM_STRING,
45 };
46
47 struct Atom
48 {
49     enum AtomType type;
50     union
51     {
52         float num;             // ATOM_NUMBER
53         char *sym;             // ATOM_SYMBOL
54         char *str;             // ATOM_STRING
55     };
56 };
57
58 struct Atom *create_number_atom(float num);
59 struct Atom *create_string_atom(const char *str, const char *str_end);
60 struct Atom *create_symbol_atom(const char *sym, const char *sym_end);
61 void destroy_atom(struct Atom *atom);
62 void print_atom_as_sexpr(struct Atom *atom);
63
64 struct Cons
65 {
66     struct Expr car;
67     struct Expr cdr;
68 };
69
70 struct Cons *create_cons(struct Expr car, struct Expr cdr);
71 void destroy_cons(struct Cons *cons);
72 void print_cons_as_sexpr(struct Cons *cons);
73
74 #endif  // ATOM_H_