123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Globalization;
- 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(RealExpression().ToString());
- }
- [KeywordMethod(Token.PrintHtml)]
- private void PrintHtml()
- {
- console.PrintHtml(RealExpression().ToString().Trim().Trim('"'), true);
- }
- [KeywordMethod(Token.PrintImg)]
- private void PrintImg()
- {
- console.PrintImg(RealExpression().ToString().Trim().Trim('"'));
- }
- [KeywordMethod(Token.PrintButton)]
- private void PrintButton()
- {
- console.PrintButton(RealExpression().ToString(), 0);
- }
- private static readonly Regex FormRegex = new Regex("{(.*?)}");
- [KeywordMethod(Token.PrintFormL)]
- private void PrintFormL()
- {
- string rawString = RealExpression().ToString();
- var evaluator = new MatchEvaluator(match => Variables[match.Groups[1].Value].ToString());
- console.PrintSingleLine(FormRegex.Replace(rawString, evaluator));
- }
- [KeywordMethod(Token.DrawLine)]
- private void DrawLine()
- {
- console.PrintBar();
- }
- [KeywordMethod(Token.DrawLineForm)]
- private void DrawLineForm()
- {
- 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))
- {
- console.SetStringStyle(Color.FromName(Lexer.Value));
- }
- else
- {
- 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));
- }
- else
- {
- int hexResult = int.Parse(Lexer.Identifer.Substring(1), NumberStyles.HexNumber);
- int argb = (int)(hexResult | 0xFF000000);
- Color c = Color.FromArgb(argb);
- console.SetStringStyle(c);
- }
- }
- GetNextToken();
- }
- #endregion
- #region Control
- [KeywordMethod(Token.If)]
- private void If()
- {
- bool result = RealExpression().Real == 1;
- AssertToken(Token.Then, false);
- GetNextToken();
- if (result)
- {
- int i = ifcounter;
- while (true)
- {
- if (lastToken == Token.If)
- {
- i++;
- }
- else if (lastToken == Token.Else)
- {
- if (i == ifcounter)
- {
- GetNextToken();
- return;
- }
- }
- else if (lastToken == Token.EndIf)
- {
- if (i == ifcounter)
- {
- GetNextToken();
- return;
- }
- i--;
- }
- GetNextToken();
- }
- }
- }
- [KeywordMethod(Token.Else)]
- private void Else()
- {
- int i = ifcounter;
- while (true)
- {
- if (lastToken == Token.If)
- {
- i++;
- }
- else if (lastToken == Token.EndIf)
- {
- if (i == ifcounter)
- {
- GetNextToken();
- return;
- }
- i--;
- }
- GetNextToken();
- }
- }
- [KeywordMethod(Token.EndIf)]
- private void EndIf()
- {
- }
- [KeywordMethod(Token.End)]
- private void End()
- {
- exit = true;
- }
- [KeywordMethod(Token.Let)]
- private void Let()
- {
- if (lastToken != Token.Equal)
- {
- AssertToken(Token.Identifer, false);
- AssertToken(Token.Equal);
- }
- string id = Lexer.Identifer;
- var typeHint = Variables[id].Type;
- if (typeHint == ValueType.Real)
- {
- GetNextToken();
- Variables[id] = RealExpression();
- }
- else
- Variables[id] = StringExpression();
- }
- [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 && prevToken == 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));
- lastToken = Token.NewLine;
- }
- [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
- }
- }
|