Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81498 / Project / Wizard / Homeworks
diff options
context:
space:
mode:
Commit68964c78790c0eaf847fa0491f85d73566e4985d (patch)
Author
 Yasen Bonev <yabonev@gmail.com>
2018-10-25 12:52:15 +0300
Committer
 dtrendafilov <trendafilov.dn@gmail.com>
2018-10-25 12:52:15 +0300
Files6410e06a3f99cbe76626c75f843c636fcff3a7ce /homework/81498/Project/Wizard/Homeworks
IPL-68964c78790c0eaf847fa0491f85d73566e4985d.tar
IPL-68964c78790c0eaf847fa0491f85d73566e4985d.tar.gz
IPL-68964c78790c0eaf847fa0491f85d73566e4985d.zip
Parent087fc8804bc0d26f19cdcb2111877f2da90bf471 (diff)
Homework 1 (#21)
* Commiting .gitignore before real solution * C# project implementing very basic functionality of the lexer, needs refactoring. * Added most javascript reserved keywords * Added logic for most js operators in Tokenizer.cs * added lexemes for operator tokens also created the PrettyPrint() function * added a coloring logic to the parsed text * Refactored Lexer class, added TokenHandler class PrettyPrint colors require tweaking, Lexer needs more refactoring * created TokenHandler to pretty print text to html * Deleted project files * Updated Program.cs
diff --git a/homework/81498/Project/Wizard/Homeworks/WeekOne/BooleanOperatorsTokenTypes.cs b/homework/81498/Project/Wizard/Homeworks/WeekOne/BooleanOperatorsTokenTypes.cs
new file mode 100644
index 0000000..34eda75
--- /dev/null
+++ b/homework/81498/Project/Wizard/Homeworks/WeekOne/BooleanOperatorsTokenTypes.cs
@@ -0,0 +1,29 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Wizard.Homeworks.WeekOne
+{
+ public static class BooleanOperatorsTokenTypes
+ {
+ public static List<TokenType> AsList()
+ {
+ return new List<TokenType>()
+ {
+ TokenType.Bang,
+ TokenType.BangEqual,
+ TokenType.LogicalOr,
+ TokenType.LogicalAnd,
+ TokenType.EqualEqual,
+ TokenType.StrictEqual,
+ TokenType.StrictNotEqual,
+ TokenType.Greater,
+ TokenType.GreaterEqual,
+ TokenType.Less,
+ TokenType.LessEqual
+ };
+ }
+ }
+}
diff --git a/homework/81498/Project/Wizard/Homeworks/WeekOne/Colour.cs b/homework/81498/Project/Wizard/Homeworks/WeekOne/Colour.cs
new file mode 100644
index 0000000..06d4670
--- /dev/null
+++ b/homework/81498/Project/Wizard/Homeworks/WeekOne/Colour.cs
@@ -0,0 +1,19 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Wizard.Homeworks.WeekOne
+{
+ public static class Color
+ {
+ public const string ForBoolean = "#FF0000";
+ public const string ForBooleanOperator = "000000";
+ public const string ForKeyword = "#0B0AF6";
+ public const string ForString = "#F609F5";
+ public const string ForNumber = "#F66509";
+ public const string ForIdentifier = "222222";
+ public const string ForDefault = "000000";
+ }
+}
diff --git a/homework/81498/Project/Wizard/Homeworks/WeekOne/TokenHandler.cs b/homework/81498/Project/Wizard/Homeworks/WeekOne/TokenHandler.cs
new file mode 100644
index 0000000..9e25c6c
--- /dev/null
+++ b/homework/81498/Project/Wizard/Homeworks/WeekOne/TokenHandler.cs
@@ -0,0 +1,93 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Wizard.Homeworks.WeekOne;
+using Wizard.Misc;
+
+namespace Wizard.Homeworks
+{ // private or property?
+ public class TokenHandler
+ {
+ private List<Token> _tokens;
+ private List<TokenType> _operatorsAsTokenList;
+ private List<TokenType> _keywordsAsTokenTypes;
+
+ public string FileDestination { get; set; }
+
+ public TokenHandler(List<Token> tokens, string fileDestionation)
+ {
+ _tokens = tokens;
+ FileDestination = fileDestionation;
+
+ _operatorsAsTokenList = OperatorsToTokens.AsTokenList();
+ _keywordsAsTokenTypes = KeywordsToTokens.AsTokenList();
+ }
+
+ public void PrintTokens()
+ {
+ foreach (var token in _tokens)
+ {
+ Console.WriteLine(token.ToString());
+ }
+ }
+
+ public void PrettyPrint(string fileDestination)
+ {
+ using (var writer = new StreamWriter(fileDestination))
+ {
+ foreach (var token in _tokens)
+ {
+ string coloredLexeme = "";
+ if (token.Type is TokenType.True ||
+ token.Type is TokenType.False)
+ {
+ coloredLexeme = "<span><font color=\"" + Color.ForBoolean + "\">" + token.Lexeme + "</font></span>";
+ writer.Write(coloredLexeme);
+ continue;
+ }
+
+ if (_operatorsAsTokenList.Contains(token.Type))
+ {
+ coloredLexeme = "<span><font color=\"" + Color.ForBooleanOperator + "\"><b>" + token.Lexeme + "</b></font></span>";
+ writer.Write(coloredLexeme); // more
+ continue;
+ }
+
+ if (_keywordsAsTokenTypes.Contains(token.Type))
+ {
+ coloredLexeme = "<span><font color=\"" + Color.ForKeyword + "\">" + token.Lexeme + "</font></span>";
+ writer.Write(coloredLexeme); // more
+ continue;
+ }
+
+ switch (token.Type)
+ {
+ case TokenType.Whitespace:
+ writer.Write("&nbsp;");
+ break;
+ case TokenType.Newline: writer.Write("<br>"); break;
+ case TokenType.String: // more
+ coloredLexeme = "<span><font color=\"" + Color.ForString + "\">\"" + token.Lexeme + "\"</font></span>";
+ writer.Write(coloredLexeme);
+ break;
+ case TokenType.Number:
+ coloredLexeme = "<span><font color=\"" + Color.ForNumber + "\">" + token.Lexeme + "</font></span>";
+ writer.Write(coloredLexeme);
+ break;
+ case TokenType.Identifier:
+ coloredLexeme = "<span><font color=\"" + Color.ForIdentifier + "\">" + token.Lexeme + "</font></span>";
+ writer.Write(coloredLexeme);
+ break;
+ default:
+ coloredLexeme = "<span><font color=\"" + Color.ForDefault + "\">" + token.Lexeme + "</font></span>";
+ writer.Write(coloredLexeme);
+ break;
+ }
+ }
+ }
+ }
+ }
+}