]> git.lizzy.rs Git - nothing.git/blob - src/script/expr.h
Introduce ids for rigid_rects
[nothing.git] / src / script / expr.h
1 #ifndef EXPR_H_
2 #define EXPR_H_
3
4 #include <stdlib.h>
5 #include <stdbool.h>
6
7 typedef struct Gc Gc;
8 typedef struct Scope Scope;
9
10 struct Cons;
11 struct Atom;
12
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 NATIVE(G, F) atom_as_expr(create_native_atom(G, F))
17 #define CONS(G, CAR, CDR) cons_as_expr(create_cons(G, CAR, CDR))
18 #define NIL(G) SYMBOL(G, "nil")
19
20 enum ExprType
21 {
22     EXPR_ATOM = 0,
23     EXPR_CONS,
24     EXPR_VOID
25 };
26
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 // TODO(#337): EvalResult does not belong to expr unit
44 struct EvalResult
45 {
46     bool is_error;
47     struct Expr expr;
48 };
49
50 typedef struct EvalResult (*NativeFunction)(Gc *gc, struct Scope *scope, struct Expr args);
51
52 enum AtomType
53 {
54     ATOM_SYMBOL = 0,
55     ATOM_NUMBER,
56     ATOM_STRING,
57     ATOM_NATIVE
58 };
59
60 struct Atom
61 {
62     enum AtomType type;
63     union
64     {
65         // TODO(#330): Atom doesn't support floats
66         long int num;           // ATOM_NUMBER
67         char *sym;              // ATOM_SYMBOL
68         char *str;              // ATOM_STRING
69         NativeFunction fun;     // ATOM_NATIVE
70     };
71 };
72
73 struct Atom *create_number_atom(Gc *gc, long int num);
74 struct Atom *create_string_atom(Gc *gc, const char *str, const char *str_end);
75 struct Atom *create_symbol_atom(Gc *gc, const char *sym, const char *sym_end);
76 struct Atom *create_native_atom(Gc *gc, NativeFunction fun);
77 void destroy_atom(struct Atom *atom);
78 void print_atom_as_sexpr(struct Atom *atom);
79
80 struct Cons
81 {
82     struct Expr car;
83     struct Expr cdr;
84 };
85
86 struct Cons *create_cons(Gc *gc, struct Expr car, struct Expr cdr);
87 void destroy_cons(struct Cons *cons);
88 void print_cons_as_sexpr(struct Cons *cons);
89
90 #endif  // EXPR_H_