-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tokenizer.cs
150 lines (136 loc) · 4.55 KB
/
Tokenizer.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JFICompiler
{
/// <summary>
/// Generates a list of tokens out of the original input string.
/// </summary>
class Tokenizer
{
private string Input;
private StringBuilder SB;
private List<Token> Tokens;
private bool ParsingConstant;
private int CurrentLine;
public Tokenizer(string _Input)
{
Input = _Input;
SB = new StringBuilder();
Tokens = new List<Token>();
ParsingConstant = false;
CurrentLine = 0;
}
public Token[] Run()
{
try
{
for (int I = 0; I < Input.Length; I++)
{
InterpretBuffer(I);
}
}
catch (Exception E)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Syntax Error in Line " + CurrentLine);
Console.WriteLine(E.Message);
Console.ForegroundColor = ConsoleColor.White;
return new Token[0];
}
return Tokens.ToArray();
}
private void InterpretBuffer(int Index)
{
char NewChar = Input[Index];
string Buffer = SB.ToString();
if(Buffer.Length > 0 && ((NewChar == '=' && Buffer[0] != '=' && Buffer[0] != '<' && Buffer[0] != '>' && Buffer[0] != '+' && Buffer[0] != '-') || NewChar == ')' || NewChar == ';' || NewChar == '>' || NewChar == '<' || NewChar == '+' || NewChar == '-'))
{
if (ParsingConstant || Buffer == "true" || Buffer == "false")
{
Tokens.Add(new Token(TokenType.CONSTANT, Buffer));
}
else
{
Tokens.Add(new Token(TokenType.IDENTIFIER, Buffer));
}
ParsingConstant = false;
SB.Clear();
}
else if (NewChar == '\n')
{
CurrentLine++;
return;
}
else if (Char.IsWhiteSpace(NewChar) || NewChar == '\r')
{
return;
}
else if (Char.IsDigit(NewChar) && !ParsingConstant && Buffer.Length < 1)
{
ParsingConstant = true;
SB.Append(NewChar);
return;
}
SB.Append(NewChar);
Buffer = SB.ToString();
if (Buffer == "if" || Buffer == "while" || Buffer == "print" || Buffer == "read" || Buffer == "return")
{
Tokens.Add(new Token(TokenType.KEYWORD, Buffer));
}
else if (Buffer == "int" || Buffer == "bool")
{
Tokens.Add(new Token(TokenType.TYPE, Buffer));
}
else if (Buffer == "(" || Buffer == ")" || Buffer == "{" || Buffer == "}" || Buffer == ";")
{
Tokens.Add(new Token(TokenType.SYMBOL, Buffer));
}
else if (Buffer == "=" || Buffer == "<" || Buffer == ">" || Buffer == "+" || Buffer == "-")
{
char NextChar = Input[Index + 1];
if (NextChar == '=' || NextChar == '<' || NextChar == '>')
{
return;
}
else
{
Tokens.Add(new Token(TokenType.OPERATOR, Buffer));
}
}
else if (Buffer == "+" || Buffer == "-" || Buffer == "*" || Buffer == "/" || Buffer == "^" || Buffer == "==" || Buffer == "+=" || Buffer == "-=" || Buffer == "&&" || Buffer == "||" || Buffer == "<=" || Buffer == ">=")
{
Tokens.Add(new Token(TokenType.OPERATOR, Buffer));
}
else
{
if (ParsingConstant)
{
Tokens.Add(new Token(TokenType.CONSTANT, Buffer));
ParsingConstant = false;
}
else
{
return;
}
}
SB.Clear();
}
}
public enum TokenType
{
KEYWORD, TYPE, CONSTANT, IDENTIFIER, OPERATOR, SYMBOL
}
public struct Token
{
public TokenType Type;
public string Body;
public Token(TokenType _Type, string _Body)
{
Type = _Type;
Body = _Body;
}
}
}