-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlval.h
94 lines (74 loc) · 1.97 KB
/
lval.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* File: lval.h
* Author: sam
*
* Created on 18 May 2014, 21:22
*/
#ifndef LVAL_H
#define LVAL_H
#ifdef __cplusplus
extern "C" {
#endif
//Forward declarations (if thats what they are called)
struct lval;
typedef struct lval lval;
struct lval_func;
typedef struct lval_func lval_func;
#include "main.h"
#include "lenv.h"
#define LVAL_IS_TRUE(val) (val->type == LVAL_NUM && fabs(val->data.num) > DBL_EPSILON)
#define LVAL_IS_FALSE(val) (val->type == LVAL_NUM && fabs(val->data.num) <= DBL_EPSILON)
enum VAL_TYPE { LVAL_ERR, LVAL_NUM, LVAL_SYM, LVAL_FUNC, LVAL_S_EXPR, LVAL_Q_EXPR, LVAL_EXIT, LVAL_STR, LVAL_OK };
enum VAL_ERROR { LERR_DIV_ZERO, LERR_BAD_OP, LERR_BAD_NUM, LERR_BAD_SYM, LERR_OTHER, LERR_SYNTAX, LERR_USER };
typedef enum VAL_TYPE VAL_TYPE;
typedef enum VAL_ERROR VAL_ERROR;
typedef lval*(*lbuiltin)(lenv*, lval*);
struct lval_func {
char* name;
lbuiltin builtin;
lenv* env;
lval* formals;
lval* body;
lval* va;
};
struct lval {
enum VAL_TYPE type;
union {
double_t num;
char* sym;
char* str;
short exitcode;
struct lval_func* func;
struct {
enum VAL_ERROR num;
char* detail;
} err;
} data;
int cell_count;
struct lval** cell_list;
};
lval* lval_new(int type);
lval* lval_num(double_t x);
lval* lval_sym(char* x);
lval* lval_s_expr();
lval* lval_q_expr();
lval* lval_builtin(lbuiltin func, char* name);
lval* lval_lambda(lval* formals, lval* body);
lval* lval_exit(short exitcode);
lval* lval_str(char* str);
lval* lval_ok();
lval* lval_add(lval* val, lval* x);
lval* lval_pop(lval* val, int i);
lval* lval_take(lval* val, int i);
lval* lval_join(lval* a, lval* b);
lval* lval_call(lenv* env, lval* function, lval* args);
BOOL lval_equal(lval* a, lval* b);
void lval_delete(lval* val);
lval* lval_copy(lval* current);
lval* lval_err(VAL_ERROR x);
lval* lval_err_detail(VAL_ERROR x, char* format, ...);
char* lval_str_name(VAL_TYPE type);
#ifdef __cplusplus
}
#endif
#endif /* LVAL_H */