-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexer.cs
More file actions
111 lines (85 loc) · 4.05 KB
/
Copy pathLexer.cs
File metadata and controls
111 lines (85 loc) · 4.05 KB
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace JenPile {
public class Lexer {
private const string separatorPattern = @"[=*+/\-;\s:()\[\]{},]+";
// currently working for single letters
private const string identifierPattern = @"[A-Z]";
private const string floatPattern = @"^-?\d+\.\d+$";
private const string integerPattern = @"^-?\d+$";
// Trying this
private const string operatorPattern = @"[=+_*/]";
private readonly Regex separatorRgx = new Regex(separatorPattern, RegexOptions.IgnoreCase);
private readonly Regex identifierRgx = new Regex(identifierPattern, RegexOptions.IgnoreCase);
private readonly Regex floatRgx = new Regex(floatPattern);
private readonly Regex integerRgx = new Regex(integerPattern);
// Trying this
private readonly Regex operatorRgx = new Regex(operatorPattern);
// moved to Parser
// private SymbolTable symbolTbl = new SymbolTable();
#region Properties
public List<Token> Tokens { get; private set; } = new List<Token>();
#endregion Properties
#region Constructor
public Lexer() {
}
#endregion Constructor
#region Class Methods
public List<Token> LexInput(List<string> input) {
List<Token> tokens = new List<Token>();
StringBuilder evalLine = new StringBuilder();
// SymbolTable symbolTable = new SymbolTable();
bool evalOff = false;
foreach (string line in input) {
foreach (char c in line) {
// Turning evaluation off during comments
if (c == '!') {
evalOff = !evalOff;
continue;
}
if (evalOff) { continue; }
Match separatorCheck = separatorRgx.Match(c.ToString());
if (separatorCheck.Success) {
// We've hit a separator, evaluate the line, & add it as a token with the separator
if (evalLine.Length > 0) {
string tokenToEval = evalLine.ToString();
TokenType type;
if (TokenDictionary.Tokens.TryGetValue(tokenToEval, out type)) {
} else if (floatRgx.IsMatch(tokenToEval)) {
type = TokenType.FLOAT;
} else if (integerRgx.IsMatch(tokenToEval)) {
type = TokenType.INTEGER;
} else if (identifierRgx.IsMatch(tokenToEval)) {
type = TokenType.IDENTIFIER;
} else {
// Should throw an error if this hits
type = TokenType.UNDEFINED;
}
tokens.Add(new Token(type, tokenToEval));
}
if (operatorRgx.IsMatch(c.ToString())) {
tokens.Add(new Token(TokenType.OPERATOR, c.ToString()));
} else {
tokens.Add(new Token(TokenType.SEPARATOR, c.ToString()));
}
evalLine.Clear();
} else {
evalLine.Append(c);
}
}
}
return tokens;
}
//TODO: Remove. For Testing Purposes
public void PrintTokens(List<Token> tokens) {
foreach (Token token in tokens) {
Console.WriteLine($"{token.Type} = {token.Value}");
// System.IO.File.AppendAllText(@"CompilerOutput.jen", $"{token.Type} = {token.Value} \r");
}
// symbolTbl.PrintSymbolTable();
}
#endregion Class Methods
}
}