-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.h
83 lines (65 loc) · 1.77 KB
/
common.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
#ifndef COMMON_H
#define COMMON_H
#include <stdbool.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#define HANDLE_NULL(ptr) \
do { \
if ((ptr) == NULL) { \
fprintf(stderr, "ERROR: Null pointer encountered at %s:%d in %s\n", \
__FILE__, __LINE__, __func__); \
abort(); \
} \
} while (0)
typedef enum {
L_PAREN,
R_PAREN,
LAMBDA,
DOT,
VARIABLE,
ERROR,
WHITESPACE,
NEWLINE,
EQ,
QUOTE,
COLON,
} tokens_t;
typedef struct AstNode AstNode;
typedef enum {
LAMBDA_EXPR,
APPLICATION,
VAR,
DEFINITION,
} AstNodeType;
typedef struct {
char *parameter;
char *type;
AstNode *body;
} LambdaExpression;
typedef struct {
AstNode *function;
AstNode *argument;
} Application;
typedef struct {
char *name;
char *type;
} Variable;
typedef union {
LambdaExpression *lambda_expr;
Application *application;
Variable *variable;
} AstNodeUnion;
struct AstNode {
AstNodeType type;
AstNodeUnion node;
};
void set_verbose(bool verbose);
void print_ast_verbose(AstNode *n);
void print_verbose(const char *format, ...);
void error(const char *msg, const char *file, int line, const char *func);
char* format(const char *fmt, ...);
void append_to_buffer(char **buffer, size_t *buffer_size, size_t *length, const char *str);
void append_ast_to_buffer(char **buffer, size_t *buffer_size, size_t *length, AstNode *node);
char *ast_to_string(AstNode *node);
#endif