summaryrefslogtreecommitdiff
path: root/lexer.c
blob: bff9a40e72c3a3db06b40c78ec62d999dee7287a (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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "lexer.h"

token_t init_token(tokenType_t type, void* value) {
	token_t t;
	t.type = type;
	t.value = value;
	return t;
}
linkList_t* init_linklist(void* value, linkList_t* next) {
	linkList_t* ll = calloc(sizeof(linkList_t), 1);
	ll->value = value;
	ll->next = next;
	return ll;
}
int advance_whitespace(char** linePointer) {
	int64_t a = (int64_t)linePointer;
	while (**linePointer == ' ' || **linePointer == '	') {
		(*linePointer)++;
	}
	return (int)((int64_t)linePointer - a);
}
linkList_t* lex_line(char* line) {
	char** p = malloc(sizeof(char*));
	*p = line;
	linkList_t* root = init_linklist(NULL, NULL);
	linkList_t* ll = root;
	while (1) {
		token_t t = lex_first_token(p);
		if (t.type == EOL) {
			printf("eol\n");
			break;
		}
		if (t.type == ERR) {
			exit(1);
		}
		if (ll->value != NULL) {
			ll->next = init_linklist(NULL, NULL);
			ll = ll->next;
		}
		token_t* tp = malloc(sizeof(token_t));
		printf("good\n");
		*tp = t;
		ll->value = tp;
	}
	free(p);
	return root;
}
token_t lex_first_token(char** content) {
	int m = 5;
	char* s = malloc(sizeof(char) * m);
	int c = 0;
	advance_whitespace(content);
	while (**content != '\n') {
		s[c] = **content;
		s[c+1] = '\0';
		(*content)++;
		printf("lexing: %c\n", s[c]);
		/* check if we need to parse a string or an int */
		if (s[c] >= 65 && s[c] <= 90) {
			free(s);
			(*content)--;
			return lex_first_string(content);
		}
		else if (s[c] >= 48 && s[c] <= 57) {
			free(s);
			(*content)--;
			return lex_first_int(content);
		}
		/* otherwise, check to see if a symbol has been matched */
		if (strcmp(s, "(") == 0) {
			free(s);
			return init_token(OPEN_PAREN, NULL);
		}
		else if (strcmp(s, ")") == 0) {
			free(s);
			return init_token(CLOSE_PAREN, NULL);
		}
		else if (strcmp(s, "#") == 0) {
			free(s);
			return init_token(START_COMMENT, NULL);
		}
		else if (strcmp(s, "|") == 0) {
			free(s);
			return init_token(JUSTIFICATION, NULL);
		}
		else if (strcmp(s, "pr") == 0) {
			free(s);
			return init_token(PREMISE, NULL);
		}
		else if (strcmp(s, "as") == 0) {
			free(s);
			return init_token(ASSUMPTION, NULL);
		}
		else if (strcmp(s, "^") == 0) {
			free(s);
			return init_token(CONJUNCTION, NULL);
		}
		else if (strcmp(s, "v") == 0) {
			free(s);
			return init_token(DISJUNCTION, NULL);
		}
		else if (strcmp(s, "->") == 0) {
			free(s);
			return init_token(IMPLICATION, NULL);
		}
		else if (strcmp(s, "<-") == 0) {
			free(s);
			return init_token(BI_IMPLICATION, NULL);
		}
		else if (c <= m) {
			printf("c: %d\n", c);
			c++;
			continue;
		}
		else {
			free(s);
			return init_token(ERR, NULL);
		}
	}
	return init_token(EOL, NULL);
}
token_t lex_first_string(char** content) {
	int m = 50;
	char* s = malloc(sizeof(char) * m);
	int c = 0;
	advance_whitespace(content);
	while (**content >= 65 && **content <= 90) {
		s[c] = **content;
		(*content)++;
		c++;
	}
	s[c] = '\0';
	return init_token(STRING, s);
}
token_t lex_first_int(char** content) {
	int m = 10;
	char* s = malloc(sizeof(char) * m);
	int c = 0;
	advance_whitespace(content);
	while (**content >= 48 && **content <= 57) {
		s[c] = **content;
		(*content)++;
		c++;
	}
	s[c] = '\0';
	int* ip = malloc(sizeof(int));
	*ip = atoi(s);
	free(s);
	return init_token(INT, (void*)ip);
}