-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.c
73 lines (66 loc) · 1.45 KB
/
scanner.c
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
#include<string.h>
#include<stdbool.h>
#include<stdio.h>
#include "scanner.h"
typedef struct {
char* start;
int current;
} Scanner;
Scanner scanner;
void init_scanner(char* source) {
scanner.start = source;
scanner.current = 0;
}
TokenType scan_token() {
while(true){
char ret = scanner.start[scanner.current];
// printf("Scanner got %c at index %d\n", ret, scanner.current);
scanner.current++;
if(ret == 0x00){
return Token_EOF;
} else if(ret == '>') {
return Token_R;
} else if(ret == '<') {
return Token_L;
} else if(ret == '+') {
return Token_Inc;
} else if(ret == '-') {
return Token_Dec;
} else if(ret == '.') {
return Token_Period;
} else if(ret == ',') {
return Token_Comma;
} else if(ret == '[') {
return Token_LB;
} else if(ret == ']') {
return Token_RB;
}
}
}
void jump_forward(){
int open = 1;
while(open > 0) {
TokenType next = scan_token();
if(next == Token_LB) {
open++;
} else if(next == Token_RB) {
open--;
}
}
// printf("Scanner jumped forward to %d", scanner.current);
}
void jump_back(){
int closed = 1;
scanner.current--; // Jump back one extra so we don't start looking on the ']' that caused this call in the first place
while(closed > 0) {
scanner.current--;
char prev = scanner.start[scanner.current];
if(prev == '[') {
closed--;
} else if(prev == ']') {
closed++;
}
}
scanner.current++;
// printf("Scanner jumped back to %d\n", scanner.current);
}