summaryrefslogtreecommitdiff
path: root/ast.h
blob: 99aa2d8a51b1daec08a8a1b4701f63648bd9da7d (plain)
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
#pragma once

#include <stdbool.h>

typedef struct InterState InterState;
typedef struct InterRet InterRet;
typedef struct AST AST;

typedef InterRet (*lambdafunc_t)(InterState *is,int nargs,AST **args);


typedef enum ASTtype{
	AST_LIST,
	AST_LAMBDA,
	AST_LAMBDAARG,
	AST_WORD,
	AST_NUMBER,
	AST_STRING,
	AST_SYMBOL,
	AST_QUOTED,
} ASTtype;


typedef struct ASTlist{
	int len;
	AST **nodes;
} ASTlist;

typedef struct ASTlambda{
	//exactly one is non-NULL
	lambdafunc_t cfunc;
	AST *body;
} ASTlambda;

typedef struct ASTlambdaArg{
	int idx;
} ASTlambdaArg;

typedef struct ASTword{
	char *word;
} ASTword;

typedef struct ASTnumber{
	double num;
} ASTnumber;

typedef struct ASTstring{
	char *str;
	int len;
} ASTstring;

typedef struct ASTsymbol{
	char *name;
	int symid;
	//if you're not the interpreter:
	// if you just allocated the ASTsymbol yourself, set symid to -1;
	// else, leave symid alone.
	//You should probably use ast_symbol(), in which case you don't have to do anything.
} ASTsymbol;

typedef struct ASTquoted{
	AST *ast;
} ASTquoted;

struct AST{
	ASTtype type;
	union {
		ASTlist li;
		ASTlambda la;
		ASTlambdaArg ar;
		ASTword wo;
		ASTnumber nu;
		ASTstring st;
		ASTsymbol sy;
		ASTquoted qu;
	};
};


void ast_free(AST *ast);

AST* ast_copy(const AST *ast);

char* ast_stringify(const AST *ast);

AST* ast_list(int len,AST **nodes); //these convenience functions DO NOT copy their arguments
AST* ast_lambda(lambdafunc_t cfunc,AST *body);
AST* ast_lambdaarg(int idx);
AST* ast_word(char *word);
AST* ast_number(double num);
AST* ast_string(char *str,int len);
AST* ast_symbol(char *name);
AST* ast_quoted(AST *ast);