-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.h
87 lines (83 loc) · 2.4 KB
/
lexer.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
84
85
86
87
#ifndef MICROL_LEXER_H
#define MICROL_LEXER_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "token.h"
#define dlex_printf(...) //printf(__VA_ARGS__)
token_list_t lex(FILE *fptr)
{
token_list_t toks = create_token_list();
// test_token_list();
char c;
while((c = getc(fptr)) != EOF)
{
dlex_printf("Read character: '%c'\n", c);
// Skip whitespace.
while(isspace(c)) c = getc(fptr);
while(c == '#')
{
while(c != '\n' && c != EOF)
c = getc(fptr);
if(c == '\n') c = getc(fptr);
}
while(isspace(c)) c = getc(fptr);
if(c == EOF) break;
dlex_printf("Not space\n");
// Tokenize.
if(c == '_' || c == '@' || isalpha(c))
{
long size = 0;
do { c = (++size, getc(fptr)); }
while(c == '_' || c == '@' || isalnum(c));
fseek(fptr, -size - 1, SEEK_CUR);
char *str = malloc(size + 1);
str[size] = 0;
fread(str, 1, size, fptr);
add_token(&toks, (token_t){ str, tt_var });
}
else if(isdigit(c))
{
//printf("Digit first@%ld: '%c'\n", ftell(fptr), c);
long size = 0;
do { c = (++size, getc(fptr)); }
while(c == '_' || isdigit(c));
fseek(fptr, -size - 1, SEEK_CUR);
char *str = malloc(size + 1);
str[size] = 0;
//printf("Size@%ld: %ld\n", ftell(fptr), size);
fread(str, 1, size, fptr);
//printf("fread %ld bytes -> \"%s\"\n", size, str);
add_token(&toks, (token_t){ str, tt_num });
}
else if(c == '(') add_token(&toks, (token_t){ NULL, tt_open_paren });
else if(c == ')') add_token(&toks, (token_t){ NULL, tt_close_paren });
else if(c == '+') add_token(&toks, (token_t){ NULL, tt_add });
else if(c == '-') add_token(&toks, (token_t){ NULL, tt_sub });
else if(c == '*') add_token(&toks, (token_t){ NULL, tt_mul });
else if(c == '/') add_token(&toks, (token_t){ NULL, tt_div });
else if(c == ',') add_token(&toks, (token_t){ NULL, tt_cma });
else if(c == '<') add_token(&toks, (token_t){ NULL, tt_lst });
else if(c == '>') add_token(&toks, (token_t){ NULL, tt_grt });
else if(c == '.') add_token(&toks, (token_t){ NULL, tt_dot });
else if(c == '=')
{
char d = getc(fptr);
if(d == '=')
add_token(&toks, (token_t){ NULL, tt_ieq });
else
{
ungetc(d, fptr);
add_token(&toks, (token_t){ NULL, tt_eql });
}
}
else
{
fprintf(stderr, "error: lexer -> unknown character: '%c'\n", c);
}
// TODO: Other symbols like ; : and so on. maybe?
}
return toks;
}
#endif//MICROL_LEXER_H