123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Reflection;
- using System.Text.RegularExpressions;
- using NTERA.Core.Interop;
- namespace NTERA.Interpreter
- {
- public partial class Interpreter
- {
- private void GenerateKeywordDictionary()
- {
- KeywordMethods = new Dictionary<Token, Action>();
- foreach (var method in typeof(Interpreter).GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
- {
- var attribute = method.GetCustomAttributes(typeof(KeywordMethodAttribute), true).FirstOrDefault() as KeywordMethodAttribute;
- if (attribute == null)
- continue;
- KeywordMethods[attribute.Token] = () => method.Invoke(this, null);
- }
- }
- #region Printing
- [KeywordMethod(Token.Print)]
- private void Print()
- {
- Console.Write(RealExpression().ToString());
- }
- [KeywordMethod(Token.PrintL)]
- private void PrintL()
- {
- Console.PrintSingleLine(ParseFormat(new Lexer(Lexer.Value, LexerType.String).Expression(this)));
- GetNextToken();
- }
-
- [KeywordMethod(Token.PrintHtml)]
- private void PrintHtml()
- {
- AssertToken(Token.Value, false);
- Console.PrintHtml(ParseFormat(new Lexer(Lexer.Value, LexerType.String).Expression(this)), true);
- GetNextToken();
- }
-
- [KeywordMethod(Token.PrintImg)]
- private void PrintImg()
- {
- Console.PrintImg(RealExpression().ToString().Trim().Trim('"'));
- }
-
- [KeywordMethod(Token.PrintButton)]
- private void PrintButton()
- {
- string text = Lexer.Value;
-
- AssertToken(Token.Comma);
- GetNextToken();
- var value = RealExpression();
- Console.PrintButton(text, (long)value.Real);
- }
- private static readonly Regex RealFormatRegex = new Regex("{(.*?)}");
- private static readonly Regex StringFormatRegex = new Regex("%(.*?)%");
- private string ParseFormat(string rawInput)
- {
- var realEvaluator = new MatchEvaluator(match => new Lexer(match.Groups[1].Value).Expression(this));
- string reals = RealFormatRegex.Replace(rawInput, realEvaluator);
- return StringFormatRegex.Replace(reals, realEvaluator);
- }
- [KeywordMethod(Token.PrintForm)]
- private void PrintForm()
- {
- AssertToken(Token.Value, false);
- Console.Write(ParseFormat(Lexer.Value));
- GetNextToken();
- }
- [KeywordMethod(Token.PrintFormL)]
- private void PrintFormL()
- {
- AssertToken(Token.Value, false);
- Console.PrintSingleLine(ParseFormat(Lexer.Value));
- GetNextToken();
- }
- [KeywordMethod(Token.DrawLine)]
- private void DrawLine()
- {
- Console.PrintBar();
- }
- [KeywordMethod(Token.DrawLineForm)]
- private void DrawLineForm()
- {
- Console.printCustomBar(RealExpression().ToString().Trim());
- }
- [KeywordMethod(Token.CustomDrawLine)]
- private void CustomDrawLine()
- {
- Console.printCustomBar(RealExpression().ToString().Trim());
- }
- [KeywordMethod(Token.Alignment)]
- private void Alignment()
- {
- AssertToken(Token.Value, false);
- Console.Alignment = (DisplayLineAlignment)Enum.Parse(typeof(DisplayLineAlignment), Lexer.Value);
- GetNextToken();
- }
- [KeywordMethod(Token.SetColor)]
- private void SetColor()
- {
- if (TryAssertToken(Token.Identifer, false))
- {
- int argb = (int)((int)RealExpression().Real | 0xFF000000);
- Color c = Color.FromArgb(argb);
- Console.SetStringStyle(c);
- return;
- }
- AssertToken(Token.Value, false);
- if (TryAssertToken(Token.Comma))
- {
- int r = (int)Lexer.Value.Real;
- AssertToken(Token.Value);
- int g = (int)Lexer.Value;
- AssertToken(Token.Comma);
- AssertToken(Token.Value);
- int b = (int)Lexer.Value;
- Console.SetStringStyle(Color.FromArgb(r, g, b));
- GetNextToken();
- }
- else
- {
- int argb = (int)((int)Lexer.Value.Real | 0xFF000000);
- Color c = Color.FromArgb(argb);
- Console.SetStringStyle(c);
- }
- }
- [KeywordMethod(Token.ResetColor)]
- private void ResetColor()
- {
- Console.ResetStyle();
- GetNextToken();
- }
- #endregion
- #region Control
- [KeywordMethod(Token.If)]
- private void If()
- {
- bool result = RealExpression();
- //needs to be redone
- }
- [KeywordMethod(Token.Else)]
- private void Else()
- {
- //needs to be redone
- }
- [KeywordMethod(Token.EndIf)]
- private void EndIf()
- {
- }
- [KeywordMethod(Token.End)]
- private void End()
- {
- exit = true;
- }
- [KeywordMethod(Token.Let)]
- private void Let()
- {
- int arrayIndex = 0;
- bool appending = false;
- if (CurrentToken == Token.Colon)
- {
- GetNextToken();
- if (CurrentToken == Token.Value)
- arrayIndex = (int)Lexer.Value.Real;
- else
- {
- arrayIndex = (int)RealExpression(Lexer.Identifer).Real;
- }
- GetNextToken();
- }
- if (CurrentToken == Token.Append)
- {
- appending = true;
- }
- else
- {
- AssertToken(Token.Equal, false);
- }
- string id = Lexer.Identifer;
- var typeHint = Variables[id].Type;
- Value value;
- if (typeHint == ValueType.Real)
- {
- GetNextToken();
- value = RealExpression();
- }
- else
- value = ParseFormat(StringExpression());
- if (appending)
- Variables[id, arrayIndex] += value;
- else
- Variables[id, arrayIndex] = value;
- }
- [KeywordMethod(Token.For)]
- private void For()
- {
- AssertToken(Token.Identifer, false);
- string var = Lexer.Identifer;
- AssertToken(Token.Equal);
- GetNextToken();
- Value v = RealExpression();
- if (Loops.ContainsKey(var))
- {
- Loops[var] = LineMarker;
- }
- else
- {
- Variables[var] = v;
- Loops.Add(var, LineMarker);
- }
- AssertToken(Token.To, false);
- GetNextToken();
- v = RealExpression();
- if (Variables[var].Operate(v, Token.More).Real == 1)
- {
- while (true)
- {
- while (!(GetNextToken() == Token.Identifer && previousToken == Token.Next)) { }
- if (Lexer.Identifer == var)
- {
- Loops.Remove(var);
- AssertToken(Token.NewLine);
- break;
- }
- }
- }
- }
- [KeywordMethod(Token.Next)]
- private void Next()
- {
- AssertToken(Token.Identifer, false);
- string var = Lexer.Identifer;
- Variables[var] = Variables[var].Operate(new Value(1), Token.Plus);
- Lexer.GoTo(new Marker(Loops[var].Pointer - 1, Loops[var].Line, Loops[var].Column - 1));
- }
- [KeywordMethod(Token.Times)]
- private void Times()
- {
- AssertToken(Token.Identifer, false);
- string var = Lexer.Identifer;
- AssertToken(Token.Comma);
- GetNextToken();
- var arg2 = RealExpression();
- Variables[var] = Variables[var].Operate(arg2, Token.Asterisk);
- }
- #endregion
- #region Global
- [KeywordMethod(Token.Sharp)]
- void Global()
- {
- if (TryAssertToken(Token.Dim, false))
- {
- if (TryAssertToken(Token.Const))
- {
- GetNextToken();
- }
- AssertToken(Token.Identifer, false);
- string identifier = Lexer.Identifer;
- AssertToken(Token.Equal);
- GetNextToken();
- Variables[identifier] = Lexer.Value;
- GetNextToken();
- return;
- }
- throw new Exception("Unknown global evalution");
- }
- #endregion
- }
- }
|