-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.h
53 lines (42 loc) · 1.36 KB
/
parser.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
#ifndef PARSER_H
#define PARSER_H
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define LAMBDA_SYMBOL 'L'
#define DOT_SYMBOL '.'
#define OPENPAREN_SYMBOL '('
#define CLOSEPAREN_SYMBOL ')'
#define COMMENT_SYMBOL '#'
#define RAISE(RET, ...) {fprintf(stderr, "error: "); fprintf(stderr, __VA_ARGS__); return RET; }
#define _MALLOC(X) { \
X = (Expr*) malloc(sizeof(Expr)); \
if (!(X)) RAISE(NULL, "could not allocate memory") \
(X)->type = NULL_T; }
#ifdef DEBUG
# define P_DEBUG(...) { printf("debug: "); printf(__VA_ARGS__); }
# define MALLOC(X) { _MALLOC(X); P_DEBUG("allocated %p\n", (void*) (X)); }
#else
# define MALLOC(X) _MALLOC(X)
# define P_DEBUG(...) ;
#endif
struct Expr;
typedef struct Expr {
enum ExprType {NULL_T = 0, APP, FUNC, BOUND, FREE} type;
union {
struct App {
const struct Expr *arg, *func;
} app;
char freeVar;
const struct Expr *func;
} data;
} Expr;
Expr* parse();
void freeExpr(const Expr* expr);
static char getsymbol();
static const Expr* getExprList(const Expr *vars[], int* parenLevel);
static const Expr* getExpr(const Expr *vars[], int *parenLevel);
static const Expr* getFunction(const Expr *vars[], int *parenLevel);
static const Expr* getVar(const Expr *vars[], char c);
static const Expr* getExpr(const Expr *vars[], int *parenLevel);
#endif // PARSER_H