summaryrefslogtreecommitdiff
path: root/parser.c
blob: 96b9bf711476df5d2dda4390c5a423bd757d83e6 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "memory.h"
#include "parser.h"


typedef enum Tokentype{
	TT_NUM,
	TT_STR,
	TT_WORD,
	TT_SYM
} Tokentype;

typedef struct Token{
	const char *str;
	int len;
} Token;

Token nexttoken(const char **sourcep){
	const char *source=*sourcep;
	while(isspace(*source))source++;
	if(isdigit(*source)||(*source=='-'&&isdigit(source[1]))){
		char *endp;
		strtod(source,&endp);
		assert(endp!=source);
		Token tok={source,endp-source};
		return tok;
	}
}


int precedence(const char *op){
	switch(op[0]){
		case '!': return op[1]=='\0' ? 12 : op[1]=='='&&op[2]=='\0' ?  5 : -1;
		case '%': return op[1]=='\0' ? 11 : -1;
		case '&': return op[1]=='\0' ?  9 : op[1]=='&'&&op[2]=='\0' ?  4 : -1;
		case '*': return op[1]=='\0' ? 11 : op[1]=='*'&&op[2]=='\0' ? 14 : -1;
		case '+': return op[1]=='\0' ? 10 : -1;
		case '-': return op[1]=='\0' ? 10 : -1;
		case '/': return op[1]=='\0' ? 11 : op[1]=='/'&&op[2]=='\0' ? 11 : -1;
		case '<': return op[1]=='\0' ?  6 : op[1]=='='&&op[2]=='\0' ?  6 : -1;
		case '=': return op[1]=='\0' ?  1 : op[1]=='='&&op[2]=='\0' ?  5 : -1;
		case '>': return op[1]=='\0' ?  6 : op[1]=='='&&op[2]=='\0' ?  6 : -1;
		case '^': return op[1]=='\0' ?  8 : op[1]=='^'&&op[2]=='\0' ?  3 : -1;
		case '|': return op[1]=='\0' ?  7 : op[1]=='|'&&op[2]=='\0' ?  2 : -1;
		case '~': return op[1]=='\0' ? 12 : -1;
		default: return -1;
	}
}

int associativity(const char *op){
	switch(op[0]){
		case '!': return op[1]=='\0' ? AS_PREFIX   : op[1]=='='&&op[2]=='\0' ? AS_NONASSOC : -1;
		case '%': return op[1]=='\0' ? AS_LEFT     : -1;
		case '&': return op[1]=='\0' ? AS_LEFT     : op[1]=='&'&&op[2]=='\0' ? AS_LEFT     : -1;
		case '*': return op[1]=='\0' ? AS_LEFT     : op[1]=='*'&&op[2]=='\0' ? AS_RIGHT    : -1;
		case '+': return op[1]=='\0' ? AS_LEFT     : -1;
		case '-': return op[1]=='\0' ? AS_LEFT     : -1;
		case '/': return op[1]=='\0' ? AS_LEFT     : op[1]=='/'&&op[2]=='\0' ? AS_LEFT     : -1;
		case '<': return op[1]=='\0' ? AS_NONASSOC : op[1]=='='&&op[2]=='\0' ? AS_NONASSOC : -1;
		case '=': return op[1]=='\0' ? AS_RIGHT    : op[1]=='='&&op[2]=='\0' ? AS_NONASSOC : -1;
		case '>': return op[1]=='\0' ? AS_NONASSOC : op[1]=='='&&op[2]=='\0' ? AS_NONASSOC : -1;
		case '^': return op[1]=='\0' ? AS_LEFT     : op[1]=='^'&&op[2]=='\0' ? AS_LEFT     : -1;
		case '|': return op[1]=='\0' ? AS_LEFT     : op[1]=='|'&&op[2]=='\0' ? AS_LEFT     : -1;
		case '~': return op[1]=='\0' ? AS_PREFIX   : -1;
		default: return -1;
	}
}


static bool parsecomment(const char *source,int *reslen){
	int cursor=0;
	if(source[cursor]!='#')return false;
	if(source[cursor+1]=='#'&&source[cursor+2]=='#'){
		cursor+=3;
		while(source[cursor]&&
			  (source[cursor]!='#'||source[cursor+1]!='#'||source[cursor+2]!='#')){
			cursor++;
		}
		if(!source[cursor])return false; //unclosed block comment
		cursor+=2;
	} else {
		while(source[cursor]&&source[cursor]!='\n')cursor++;
		if(source[cursor])cursor++;
	}
	*reslen=cursor;
	return true;
}

static void parseintermediate(const char *source,int *reslen){
	int cursor=0;
	bool acted;
	do {
		acted=false;
		while(source[cursor]&&isspace(source[cursor])){
			cursor++;
			acted=true;
		}
		int partlen;
		if(parsecomment(source+cursor,&partlen)){
			cursor+=partlen;
			acted=true;
		}
	} while(acted);
	*reslen=cursor;
}

static AST* parseexpr(const char *source,int *reslen,int minprec){
	;
}

static AST* parsestmt(const char *source,int *reslen){
	return parseexpr(source,reslen,0);
}

ASTblock* parse(const char *source){
	ASTblock *bl=malloc(sizeof(ASTblock));
	int sz=32;
	bl->len=0;
	bl->exprs=calloc(sz,sizeof(AST*));
	if(!bl->exprs)outofmem();
	int reslen;
	int cursor=0;
	while(true){
		if(bl->len==sz){
			sz*=2;
			bl->exprs=realloc(bl->exprs,sz*sizeof(AST*));
			if(!bl->exprs)outofmem();
		}
		parseintermediate(source+cursor,&reslen);
		if(!source[cursor])break;
		AST *node=parsestmt(source+cursor,&reslen);
		if(!node){
			ast_free((AST*)bl);
			return NULL;
		}
		bl->exprs[bl->len++]=node;
		cursor+=reslen;
	}
	return bl;
}

void ast_free(AST *ast_){
	switch(ast_->type){
		case AST_BLOCK:{ ASTblock *ast=(ASTblock*)ast_;
			for(int i=0;i<ast->len;i++)if(ast->exprs[i])ast_free(ast->exprs[i]);
			free(ast->exprs);
			break;
		}

		case AST_OP:{ ASTop *ast=(ASTop*)ast_;
			if(ast->left)ast_free(ast->left);
			if(ast->right)ast_free(ast->right);
			break;
		}

		case AST_NUM:
			break;

		case AST_STR:{ ASTstr *ast=(ASTstr*)ast_;
			if(ast->str)free(ast->str);
			break;
		}

		case AST_VAR:{ ASTvar *ast=(ASTvar*)ast_;
			if(ast->name)free(ast->name);
			break;
		}

		case AST_CALL:{ ASTcall *ast=(ASTcall*)ast_;
			if(ast->func)free(ast->func);
			for(int i=0;i<ast->nargs;i++)if(ast->args[i])ast_free(ast->args[i]);
			free(ast->args);
			break;
		}

		case AST_IF:{ ASTif *ast=(ASTif*)ast_;
			if(ast->cond)free(ast->cond);
			if(ast->thenb)free(ast->thenb);
			if(ast->elseb)free(ast->elseb);
			break;
		}

		case AST_WHILE:{ ASTwhile *ast=(ASTwhile*)ast_;
			if(ast->cond)free(ast->cond);
			if(ast->body)free(ast->body);
			break;
		}
	}
	free(ast_);
}