Skip to content

Commit

Permalink
add implementation of Lab7
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey-Sagaydak committed Mar 18, 2024
1 parent 69013b8 commit 1bef877
Show file tree
Hide file tree
Showing 8 changed files with 543 additions and 1 deletion.
40 changes: 40 additions & 0 deletions Compiler/Models/Lab7/Token.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Compiler;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab7;

public enum TokenType
{
While,
Or,
And,
Rel,
Do,
Var,
Const,
Assignment,
End,
Semicolon,
ArithOp,
Error
}

public class Token
{
public TokenType Type { get; set; }
public string Value { get; set; }
public int StartIndex { get; set; }
public int EndIndex { get; set; }

public Token(TokenType type, string value, int startIndex, int endIndex)
{
Type = type;
Value = value;
StartIndex = startIndex;
EndIndex = endIndex;
}
}
140 changes: 140 additions & 0 deletions Compiler/Models/Lab7/WhileLexer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using Compiler;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab7;

public class WhileLexer
{
private List<Token> Tokens;

public List<Token> Analyze(string input)
{
int i;
string value;

Tokens.Clear();

for (i = 0; i < input.Length; i++)
{
value = string.Empty + input[i];

if (char.IsLetter(input[i]))
{
int startIndex = i;

while ((i + 1) < input.Length && char.IsLetter(input[i + 1]))
{
i++;
value += input[i];
}

switch (value)
{
case "while":
Tokens.Add(new Token(TokenType.While, value, startIndex + 1, i + 1));
break;
case "do":
Tokens.Add(new Token(TokenType.Do, value, startIndex + 1, i + 1));
break;
case "end":
Tokens.Add(new Token(TokenType.End, value, startIndex + 1, i + 1));
break;
case "and":
Tokens.Add(new Token(TokenType.And, value, startIndex + 1, i + 1));
break;
case "or":
Tokens.Add(new Token(TokenType.Or, value, startIndex + 1, i + 1));
break;
default:
Tokens.Add(new Token(TokenType.Var, value, startIndex + 1, i + 1));
break;
}
}
else
{
if (char.IsDigit(input[i]))
{
int startIndex = i;

while ((i + 1) < input.Length && char.IsDigit(input[i + 1]))
{
i++;
value += input[i];
}

Tokens.Add(new Token(TokenType.Const, value, startIndex + 1, i + 1));
}
else
{
switch (input[i])
{
case '\t':
case ' ':
break;
case (char)13:
if ((i + 1) < input.Length && input[i + 1] == (char)10)
{
i++;
value = "\\n";
}
break;
case '>':
case '<':
case '!':
if ((i + 1) < input.Length && input[i + 1] == '=')
{
i++;
value += input[i];
Tokens.Add(new Token(TokenType.Rel, value, i, i + 1));
}
else
{
if (input[i] == '!')
{
Tokens.Add(new Token(TokenType.Error, value, i + 1, i + 1));
}
else
{
Tokens.Add(new Token(TokenType.Rel, value, i + 1, i + 1));
}
}
break;
case '=':
if ((i + 1) < input.Length && input[i + 1] == '=')
{
i++;
value += input[i];
Tokens.Add(new Token(TokenType.Rel, value, i, i + 1));
}
else
{
Tokens.Add(new Token(TokenType.Assignment, value, i, i + 1));
}
break;
case '+':
case '-':
Tokens.Add(new Token(TokenType.ArithOp, value, i + 1, i + 1));
break;
case ';':
Tokens.Add(new Token(TokenType.Semicolon, value, i + 1, i + 1));
break;
default:
Tokens.Add(new Token(TokenType.Error, value, i + 1, i + 1));
break;
}
}
}
}

return Tokens;
}

public WhileLexer()
{
Tokens = new List<Token>();
}
}
Loading

0 comments on commit 1bef877

Please sign in to comment.