object EvaluateWhereClause(Runtime.PassiveExpression expr, Runtime.Pattern lastPattern, ScriptThread thread) { // instantiate where-clause pattern var patt = Pattern.Instantiate(thread); patt.CopyBoundVariables(lastPattern); // perform matching var result = patt.Match(expr); if (result) { // store last recognized pattern as a local variable thread.SetLastPattern(patt); // match succeeded, return result expression if (ResultExpression != null) { return(ResultExpression.Evaluate(thread)); } // match succeeded, evaluate more conditions if (MoreConditions != null) { return(MoreConditions.Evaluate(thread)); } } // matching failed, return nothing return(null); }
public PassiveExpression Add(PassiveExpression expression) { BigInteger op1, op2; GetBigIntegerArguments(expression, out op1, out op2); return PassiveExpression.Build(ConvertBigIntegerToRefalNumber(op1 + op2)); }
public override MatchResult Match(PassiveExpression expression, ref int exIndex, int patIndex) { // if it's the first occurance of the variable, it can match any value if (patIndex == FirstOccurance) return MatchAny(expression, ref exIndex); // if it's not the first, it can match only the same value as before return MatchSame(expression, ref exIndex); }
public PassiveExpression Prout(PassiveExpression expression) { if (expression == null) return null; //Console.WriteLine("{0}", expression.ToStringBuilder(0)); ScriptThread.App.WriteLine(expression.ToStringBuilder(0).ToString()); return null; }
public PassiveExpression Card(PassiveExpression expression) { throw new NotSupportedException(); /*string s = Console.ReadLine(); if (s != null) return PassiveExpression.Build(s.ToCharArray()); else return PassiveExpression.Build(0);*/ }
protected override MatchResult MatchSame(PassiveExpression expression, ref int exIndex) { if (expression == null || exIndex >= expression.Count) return MatchResult.Failure; // match the bound value if (Value.Equals(expression[exIndex++])) { return MatchResult.Success; } return MatchResult.Failure; }
public override MatchResult Match(PassiveExpression expression, ref int exIndex, int patIndex) { if (expression == null || exIndex >= expression.Count) return MatchResult.Failure; // symbol matches single symbol if ((Value == null && expression[exIndex] != null) || !Value.Equals(expression[exIndex])) { return MatchResult.Failure; } exIndex++; return MatchResult.Success; }
protected override MatchResult MatchAny(PassiveExpression expression, ref int exIndex) { if (expression == null || exIndex >= expression.Count) return MatchResult.Failure; // match anything except braces if (!(expression[exIndex] is StructureBrace)) { this.Symbol = expression[exIndex++]; return MatchResult.Success; } return MatchResult.Failure; }
protected override MatchResult MatchAny(PassiveExpression expression, ref int exIndex) { if (this.Expression == null) { // start with empty expression, don't advance exIndex this.Expression = PassiveExpression.Build(); return MatchResult.PartialSuccess; } else { if (expression == null || exIndex >= expression.Count) return MatchResult.Failure; // continue adding terms to expression object ex = expression[exIndex++]; // add single symbol if (!(ex is StructureBrace)) { this.Expression.Add(ex); return MatchResult.PartialSuccess; } else if (ex is OpeningBrace) { // add subexpression this.Expression.Add(ex); // extract subexpression within the structure braces int rank = 1; while (exIndex < expression.Count && rank > 0) { ex = expression[exIndex++]; this.Expression.Add(ex); if (ex is OpeningBrace) rank++; else if (ex is ClosingBrace) rank--; } // subexpression with surrounding braces is extracted if (rank == 0) return MatchResult.PartialSuccess; } return MatchResult.Failure; } }
public static PassiveExpression Build(params object[] objects) { PassiveExpression result = new PassiveExpression(); // flatten expressions, if needed foreach (object obj in objects) { if (obj is PassiveExpression) { foreach (object symbol in (PassiveExpression)obj) result.Add(symbol); } else result.Add(obj); } return result; }
public bool CompareToExpression(int startIndex, PassiveExpression expression) { if (expression == null || expression.IsEmpty) return true; for (int i = 0; i < expression.Count; i++) { if (startIndex + i >= this.Count) return false; object ex1 = this[startIndex + i]; object ex2 = expression[i]; if (ex1 is OpeningBrace) { if (!(ex2 is OpeningBrace)) return false; } else if (ex1 is ClosingBrace) { if (!(ex2 is ClosingBrace)) return false; } else if (!ex1.Equals(ex2)) return false; } return true; }
private static PassiveExpression _Inout(PassiveExpression expression) { Pattern pattern6 = new Pattern(0); if (pattern6.Match(expression)) { return PassiveExpression.Build(_Prout(PassiveExpression.Build("End of session".ToCharArray()))); }; Pattern pattern7 = new Pattern(new ExpressionVariable("e.X")); if (pattern7.Match(expression)) { return PassiveExpression.Build(_Out(PassiveExpression.Build(_Translate(PassiveExpression.Build(_Lex(PassiveExpression.Build(new OpeningBrace(), "$".ToCharArray(), new ClosingBrace(), pattern7.GetVariable("e.X")))))))); }; throw new RecognitionImpossibleException("Recognition impossible. Last expression: " + expression.ToString()); }
private static PassiveExpression _Loop(PassiveExpression expression) { Pattern pattern4 = new Pattern(); if (pattern4.Match(expression)) { return PassiveExpression.Build(_Nil(PassiveExpression.Build(_Dgall(PassiveExpression.Build()))), _Invitation(PassiveExpression.Build()), _Inout(PassiveExpression.Build(_Card(PassiveExpression.Build())))); }; throw new RecognitionImpossibleException("Recognition impossible. Last expression: " + expression.ToString()); }
private static PassiveExpression _Invitation(PassiveExpression expression) { Pattern pattern3 = new Pattern(); if (pattern3.Match(expression)) { return PassiveExpression.Build(_Prout(PassiveExpression.Build()), _Prout(PassiveExpression.Build()), _Prout(PassiveExpression.Build("Type in an expression (one line), or Ctrl-Z to terminate".ToCharArray()))); }; throw new RecognitionImpossibleException("Recognition impossible. Last expression: " + expression.ToString()); }
private static PassiveExpression _Translate(PassiveExpression expression) { Pattern pattern43 = new Pattern(); if (pattern43.Match(expression)) { return PassiveExpression.Build(); }; Pattern pattern44 = new Pattern(new ExpressionVariable("e.1")); if (pattern44.Match(expression)) { return PassiveExpression.Build(_Code__gen(PassiveExpression.Build(new OpeningBrace(), 1, new ClosingBrace(), _Parse(PassiveExpression.Build(pattern44.GetVariable("e.1"))), _Dg(PassiveExpression.Build("compl".ToCharArray()))))); }; throw new RecognitionImpossibleException("Recognition impossible. Last expression: " + expression.ToString()); }
public PassiveExpression Putout(PassiveExpression expression) { throw new NotImplementedException(); }
protected override MatchResult MatchAny(PassiveExpression expression, ref int exIndex) { if (expression == null || exIndex >= expression.Count) return MatchResult.Failure; // match single symbol (symbol == not a brace) if (!(expression[exIndex] is StructureBrace)) { this.Symbol = expression[exIndex++]; return MatchResult.Success; } // match subexpression else if (expression[exIndex] is OpeningBrace) { this.Expression = PassiveExpression.Build(); this.Expression.Add(expression[exIndex++]); // extract subexpression within the structure braces int rank = 1; while (exIndex < expression.Count && rank > 0) { object ex = expression[exIndex++]; this.Expression.Add(ex); if (ex is OpeningBrace) rank++; else if (ex is ClosingBrace) rank--; } // subexpression with surrounding braces is extracted if (rank == 0) return MatchResult.Success; } // unmatched braces return MatchResult.Failure; }
public PassiveExpression Implode_Ext(PassiveExpression expression) { throw new NotImplementedException(); }
public PassiveExpression Type(PassiveExpression expression) { if (expression == null || expression.IsEmpty) return PassiveExpression.Build('*', 0, expression); object o = expression[0]; if (o is OpeningBrace) return PassiveExpression.Build('B', 0, expression); if (o is char) { char c = (char)o; char subtype = Char.IsUpper(c) ? 'u' : 'l'; if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) return PassiveExpression.Build('L', subtype, expression); if (c >= '0' && c <= '9') return PassiveExpression.Build('D', 0, expression); if (Char.IsControl(c)) return PassiveExpression.Build('O', subtype, expression); // printable return PassiveExpression.Build('P', subtype, expression); } if (o is int || o is long) return PassiveExpression.Build('N', 0, expression); if (o is string) { string s = o as string; char subtype = 'i'; if (s.Length == 0 || !(Char.IsLetter(s[0])) || s.IndexOf(" ") >= 0) subtype = 'q'; return PassiveExpression.Build('W', subtype, expression); } return PassiveExpression.Build('P', 'l', expression); }
// extract arguments and convert to BigIntegers void GetBigIntegerArguments(PassiveExpression expr, out BigInteger arg1, out BigInteger arg2) { object op1, op2; GetArguments(expr, out op1, out op2); arg1 = ToBigInteger(op1); arg2 = ToBigInteger(op2);; }
// extract arguments specified as <Function t.1 e.2> void GetArguments(PassiveExpression expression, out object arg1, out object arg2) { var p = new Pattern(new TermVariable("t.1"), new ExpressionVariable("e.2")); if (p.Match(expression)) { arg1 = p.GetVariable("t.1"); arg2 = p.GetVariable("e.2"); return; } // can't find match throw new RecognitionImpossibleException(); }
public PassiveExpression Dgall(PassiveExpression expression) { List<object> result = new List<object>(); foreach (string strKey in BuriedKeys.Keys) { result.AddRange(new object[] {new OpeningBrace(), BuriedKeys[strKey], '=', BuriedValues[strKey], new ClosingBrace()}); BuriedKeys[strKey] = null; BuriedValues[strKey] = null; } return PassiveExpression.Build(result.ToArray()); }
private static PassiveExpression _Output(PassiveExpression expression) { Pattern pattern3 = new Pattern(new OpeningBrace(), new ClosingBrace(), new OpeningBrace(), new SymbolVariable("s.D"), new ClosingBrace()); if (pattern3.Match(expression)) { return(PassiveExpression.Build(_Output(PassiveExpression.Build(new OpeningBrace(), _Get(PassiveExpression.Build(pattern3.GetVariable("s.D"))), new ClosingBrace(), new OpeningBrace(), pattern3.GetVariable("s.D"), new ClosingBrace())))); } ; Pattern pattern4 = new Pattern(new OpeningBrace(), 0, new ClosingBrace(), new OpeningBrace(), new SymbolVariable("s.D"), new ClosingBrace()); if (pattern4.Match(expression)) { return(PassiveExpression.Build()); } ; Pattern pattern5 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace(), new OpeningBrace(), new SymbolVariable("s.D"), new ClosingBrace()); if (pattern5.Match(expression)) { return(PassiveExpression.Build(_Prout(PassiveExpression.Build(pattern5.GetVariable("e.1"))), _Output(PassiveExpression.Build(new OpeningBrace(), _Get(PassiveExpression.Build(pattern5.GetVariable("s.D"))), new ClosingBrace(), new OpeningBrace(), pattern5.GetVariable("s.D"), new ClosingBrace())))); } ; throw new RecognitionImpossibleException("Recognition impossible. Last expression: " + expression.ToString()); }
public static PassiveExpression _Go(PassiveExpression expression) { Pattern pattern1 = new Pattern(); if (pattern1.Match(expression)) { return(PassiveExpression.Build(_Open(PassiveExpression.Build("r".ToCharArray(), _InputFile(PassiveExpression.Build()), "test10.ref".ToCharArray())), _Output(PassiveExpression.Build(new OpeningBrace(), new ClosingBrace(), new OpeningBrace(), _InputFile(PassiveExpression.Build()), new ClosingBrace())))); } ; throw new RecognitionImpossibleException("Recognition impossible. Last expression: " + expression.ToString()); }
public static PassiveExpression _First(PassiveExpression expression) { throw new NotImplementedException(); }
// match expression at exIndex pointer, advance pointer as needed public abstract MatchResult Match(PassiveExpression expression, ref int exIndex, int patIndex);
public PassiveExpression Implode(PassiveExpression expression) { string s = expression.ToString().Trim(); int index = 0; while (index < s.Length && (char.IsLetterOrDigit(s[index]) || "-_".IndexOf(s[index]) >= 0)) { index++; } return PassiveExpression.Build(s.Substring(0, index)); }
protected override MatchResult MatchSame(PassiveExpression expression, ref int exIndex) { if (expression != null && expression.CompareToExpression(exIndex, this.Expression)) { exIndex += this.Expression.Count; return MatchResult.Success; } return MatchResult.Failure; }
public PassiveExpression Explode(PassiveExpression expression) { // convert expression to string and remove trailing space, if any var sb = expression.ToStringBuilder(0); if (sb.Length > 0 && sb[sb.Length - 1] == ' ') sb.Length -= 1; return PassiveExpression.Build(sb.ToString().ToCharArray()); }
private static PassiveExpression _Pr__lmb(PassiveExpression expression) { Pattern pattern41 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace(), new ExpressionVariable("e.2")); if (pattern41.Match(expression)) { return PassiveExpression.Build(_Pr__lmb(PassiveExpression.Build(pattern41.GetVariable("e.1"))), _Prout(PassiveExpression.Build("(".ToCharArray(), pattern41.GetVariable("e.2")))); }; Pattern pattern42 = new Pattern("$".ToCharArray(), new ExpressionVariable("e.1")); if (pattern42.Match(expression)) { return PassiveExpression.Build(); }; throw new RecognitionImpossibleException("Recognition impossible. Last expression: " + expression.ToString()); }
public PassiveExpression Numb(PassiveExpression expression) { if (expression == null || expression.IsEmpty) return PassiveExpression.Build(); return PassiveExpression.Build(ConvertBigIntegerToRefalNumber(ToBigInteger(expression.ToString()))); }
private static PassiveExpression _Parse(PassiveExpression expression) { Pattern pattern48 = new Pattern(new ExpressionVariable("e.Exp")); if (pattern48.Match(expression)) { expression = PassiveExpression.Build(_Last1(PassiveExpression.Build(new OpeningBrace(), "+-".ToCharArray(), new ClosingBrace(), pattern48.GetVariable("e.Exp"), new OpeningBrace(), new ClosingBrace()))); Pattern pattern49 = new Pattern(new ExpressionVariable("e.1"), new SymbolVariable("s.Op"), new OpeningBrace(), new ExpressionVariable("e.2"), new ClosingBrace()); pattern49.CopyBoundVariables(pattern48); if (pattern49.Match(expression)) { return PassiveExpression.Build(pattern49.GetVariable("s.Op"), new OpeningBrace(), _Parse(PassiveExpression.Build(pattern49.GetVariable("e.1"))), new ClosingBrace(), _Parse(PassiveExpression.Build(pattern49.GetVariable("e.2")))); } }; Pattern pattern50 = new Pattern(new ExpressionVariable("e.Exp")); if (pattern50.Match(expression)) { expression = PassiveExpression.Build(_Last1(PassiveExpression.Build(new OpeningBrace(), "*/".ToCharArray(), new ClosingBrace(), pattern50.GetVariable("e.Exp"), new OpeningBrace(), new ClosingBrace()))); Pattern pattern51 = new Pattern(new ExpressionVariable("e.1"), new SymbolVariable("s.Op"), new OpeningBrace(), new ExpressionVariable("e.2"), new ClosingBrace()); pattern51.CopyBoundVariables(pattern50); if (pattern51.Match(expression)) { return PassiveExpression.Build(pattern51.GetVariable("s.Op"), new OpeningBrace(), _Parse(PassiveExpression.Build(pattern51.GetVariable("e.1"))), new ClosingBrace(), _Parse(PassiveExpression.Build(pattern51.GetVariable("e.2")))); } }; Pattern pattern52 = new Pattern(new ExpressionVariable("e.1"), "^".ToCharArray(), new ExpressionVariable("e.2")); if (pattern52.Match(expression)) { return PassiveExpression.Build("^".ToCharArray(), new OpeningBrace(), _Parse(PassiveExpression.Build(pattern52.GetVariable("e.1"))), new ClosingBrace(), _Parse(PassiveExpression.Build(pattern52.GetVariable("e.2")))); }; Pattern pattern53 = new Pattern(new SymbolVariable("s.Symb")); if (pattern53.Match(expression)) { expression = PassiveExpression.Build(_Type(PassiveExpression.Build(pattern53.GetVariable("s.Symb")))); Pattern pattern54 = new Pattern("Wi".ToCharArray(), new ExpressionVariable("e.S")); pattern54.CopyBoundVariables(pattern53); if (pattern54.Match(expression)) { return PassiveExpression.Build(pattern54.GetVariable("s.Symb")); } }; Pattern pattern55 = new Pattern(new SymbolVariable("s.Symb")); if (pattern55.Match(expression)) { expression = PassiveExpression.Build(_Type(PassiveExpression.Build(pattern55.GetVariable("s.Symb")))); Pattern pattern56 = new Pattern("N".ToCharArray(), new ExpressionVariable("e.S")); pattern56.CopyBoundVariables(pattern55); if (pattern56.Match(expression)) { return PassiveExpression.Build(pattern56.GetVariable("s.Symb")); } }; Pattern pattern57 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.Exp"), new ClosingBrace()); if (pattern57.Match(expression)) { return PassiveExpression.Build(_Parse(PassiveExpression.Build(pattern57.GetVariable("e.Exp")))); }; Pattern pattern58 = new Pattern(); if (pattern58.Match(expression)) { return PassiveExpression.Build(); }; Pattern pattern59 = new Pattern(new ExpressionVariable("e.Exp")); if (pattern59.Match(expression)) { return PassiveExpression.Build(_Prout(PassiveExpression.Build("Syntax error. Cannot parse ".ToCharArray(), pattern59.GetVariable("e.Exp"))), _Br(PassiveExpression.Build("compl=fail".ToCharArray()))); }; throw new RecognitionImpossibleException("Recognition impossible. Last expression: " + expression.ToString()); }
public PassiveExpression Chr(PassiveExpression expression) { var args = new List<object>(); foreach (object o in expression) { var v = (o is int) ? (char)Convert.ToByte(o) : o; // <Chr NewLine> returns Environment.NewLine if (o != null && o.ToString().ToLower() == "newline") { v = Environment.NewLine.ToCharArray(); } args.Add(v); } return PassiveExpression.Build(args.ToArray()); }
private static PassiveExpression _Code__gen(PassiveExpression expression) { Pattern pattern60 = new Pattern(new ExpressionVariable("e.1"), "fail".ToCharArray()); if (pattern60.Match(expression)) { return PassiveExpression.Build(); }; Pattern pattern61 = new Pattern(new OpeningBrace(), new SymbolVariable("s.N"), new ClosingBrace(), "-".ToCharArray(), new OpeningBrace(), new ClosingBrace(), new ExpressionVariable("e.2")); if (pattern61.Match(expression)) { return PassiveExpression.Build(_Code__gen(PassiveExpression.Build(new OpeningBrace(), pattern61.GetVariable("s.N"), new ClosingBrace(), pattern61.GetVariable("e.2"))), "Minus ;".ToCharArray()); }; Pattern pattern62 = new Pattern(new OpeningBrace(), new SymbolVariable("s.N"), new ClosingBrace(), new SymbolVariable("s.Op"), new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace(), new SymbolVariable("s.2")); if (pattern62.Match(expression)) { return PassiveExpression.Build(_Code__gen(PassiveExpression.Build(new OpeningBrace(), pattern62.GetVariable("s.N"), new ClosingBrace(), pattern62.GetVariable("e.1"))), _Code__op(PassiveExpression.Build(pattern62.GetVariable("s.Op"))), _Outform(PassiveExpression.Build(pattern62.GetVariable("s.2"))), ";".ToCharArray()); }; Pattern pattern63 = new Pattern(new OpeningBrace(), new SymbolVariable("s.N"), new ClosingBrace(), "+".ToCharArray(), new OpeningBrace(), new SymbolVariable("s.1"), new ClosingBrace(), new ExpressionVariable("e.2")); if (pattern63.Match(expression)) { return PassiveExpression.Build(_Code__gen(PassiveExpression.Build(new OpeningBrace(), pattern63.GetVariable("s.N"), new ClosingBrace(), pattern63.GetVariable("e.2"))), _Code__op(PassiveExpression.Build("+".ToCharArray())), _Outform(PassiveExpression.Build(pattern63.GetVariable("s.1"))), ";".ToCharArray()); }; Pattern pattern64 = new Pattern(new OpeningBrace(), new SymbolVariable("s.N"), new ClosingBrace(), "*".ToCharArray(), new OpeningBrace(), new SymbolVariable("s.1"), new ClosingBrace(), new ExpressionVariable("e.2")); if (pattern64.Match(expression)) { return PassiveExpression.Build(_Code__gen(PassiveExpression.Build(new OpeningBrace(), pattern64.GetVariable("s.N"), new ClosingBrace(), pattern64.GetVariable("e.2"))), _Code__op(PassiveExpression.Build("*".ToCharArray())), _Outform(PassiveExpression.Build(pattern64.GetVariable("s.1"))), ";".ToCharArray()); }; Pattern pattern65 = new Pattern(new OpeningBrace(), new SymbolVariable("s.N"), new ClosingBrace(), new SymbolVariable("s.Op"), new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace(), new ExpressionVariable("e.2")); if (pattern65.Match(expression)) { return PassiveExpression.Build(_Code__gen(PassiveExpression.Build(new OpeningBrace(), pattern65.GetVariable("s.N"), new ClosingBrace(), pattern65.GetVariable("e.2"))), "STORE R+".ToCharArray(), _Symb(PassiveExpression.Build(pattern65.GetVariable("s.N"))), ";".ToCharArray(), _Code__gen(PassiveExpression.Build(new OpeningBrace(), _Add(PassiveExpression.Build(new OpeningBrace(), pattern65.GetVariable("s.N"), new ClosingBrace(), 1)), new ClosingBrace(), pattern65.GetVariable("e.1"))), _Code__op(PassiveExpression.Build(pattern65.GetVariable("s.Op"))), "R+".ToCharArray(), _Symb(PassiveExpression.Build(pattern65.GetVariable("s.N"))), ";".ToCharArray()); }; Pattern pattern66 = new Pattern(new OpeningBrace(), new SymbolVariable("s.N"), new ClosingBrace(), new SymbolVariable("s.Symb")); if (pattern66.Match(expression)) { return PassiveExpression.Build("LOAD ".ToCharArray(), _Outform(PassiveExpression.Build(pattern66.GetVariable("s.Symb"))), ";".ToCharArray()); }; Pattern pattern67 = new Pattern(new OpeningBrace(), new SymbolVariable("s.N"), new ClosingBrace(), new ExpressionVariable("e.X")); if (pattern67.Match(expression)) { return PassiveExpression.Build(new OpeningBrace(), "Syntax error".ToCharArray(), new ClosingBrace(), ";".ToCharArray()); }; throw new RecognitionImpossibleException("Recognition impossible. Last expression: " + expression.ToString()); }
public PassiveExpression Ord(PassiveExpression expression) { var args = new List<object>(); foreach (object o in expression) { var v = (o is char) ? Convert.ToInt32(o) : o; args.Add(v); } return PassiveExpression.Build(args.ToArray()); }
private static PassiveExpression _Write(PassiveExpression expression) { Pattern pattern77 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace(), ";".ToCharArray(), new ExpressionVariable("e.2")); if (pattern77.Match(expression)) { return PassiveExpression.Build(_Prout(PassiveExpression.Build(pattern77.GetVariable("e.1")))); }; Pattern pattern78 = new Pattern(new ExpressionVariable("e.1"), ";".ToCharArray(), new ExpressionVariable("e.2")); if (pattern78.Match(expression)) { return PassiveExpression.Build(_Prout(PassiveExpression.Build(" ".ToCharArray(), pattern78.GetVariable("e.1"))), _Write(PassiveExpression.Build(pattern78.GetVariable("e.2")))); }; Pattern pattern79 = new Pattern(); if (pattern79.Match(expression)) { return PassiveExpression.Build(); }; throw new RecognitionImpossibleException("Recognition impossible. Last expression: " + expression.ToString()); }
public PassiveExpression Divmod(PassiveExpression expression) { throw new NotImplementedException(); }
private static PassiveExpression _Out(PassiveExpression expression) { Pattern pattern8 = new Pattern(); if (pattern8.Match(expression)) { return PassiveExpression.Build(_Loop(PassiveExpression.Build())); }; Pattern pattern9 = new Pattern(new ExpressionVariable("e.1")); if (pattern9.Match(expression)) { return PassiveExpression.Build(_Prout(PassiveExpression.Build("The translation is:".ToCharArray())), _Write(PassiveExpression.Build(pattern9.GetVariable("e.1"))), _Loop(PassiveExpression.Build())); }; throw new RecognitionImpossibleException("Recognition impossible. Last expression: " + expression.ToString()); }
private static PassiveExpression _PreAlph(PassiveExpression expression) { Pattern pattern2 = new Pattern(new SymbolVariable("s.1"), new SymbolVariable("s.1")); if (pattern2.Match(expression)) { return(PassiveExpression.Build(true)); } ; Pattern pattern3 = new Pattern(new SymbolVariable("s.1"), new SymbolVariable("s.2")); if (pattern3.Match(expression)) { return(PassiveExpression.Build(_Before(PassiveExpression.Build(pattern3.GetVariable("s.1"), pattern3.GetVariable("s.2"), "In", _Alphabet(PassiveExpression.Build()))))); } ; throw new RecognitionImpossibleException("Recognition impossible. Last expression: " + expression.ToString()); }
protected abstract MatchResult MatchSame(PassiveExpression expression, ref int exIndex);
public override MatchResult Match(PassiveExpression expression, ref int exIndex, int patIndex) { if (expression == null || exIndex >= expression.Count) return MatchResult.Failure; // opening brace <=> opening brace, closing brace <=> closing brace if (expression[exIndex].GetType() == GetType()) { exIndex++; return MatchResult.Success; } return MatchResult.Failure; }
private static PassiveExpression _Output(PassiveExpression expression) { Pattern pattern2 = new Pattern(); if (pattern2.Match(expression)) { return(PassiveExpression.Build(_Output(PassiveExpression.Build(_Card(PassiveExpression.Build()))))); } ; Pattern pattern3 = new Pattern(0); if (pattern3.Match(expression)) { return(PassiveExpression.Build()); } ; Pattern pattern4 = new Pattern(new ExpressionVariable("e.1")); if (pattern4.Match(expression)) { return(PassiveExpression.Build(_Prout(PassiveExpression.Build(pattern4.GetVariable("e.1"))), _Output(PassiveExpression.Build(_Card(PassiveExpression.Build()))))); } ; throw new RecognitionImpossibleException("Recognition impossible. Last expression: " + expression.ToString()); }
protected override MatchResult MatchSame(PassiveExpression expression, ref int exIndex) { if (expression == null || exIndex >= expression.Count) return MatchResult.Failure; // match same symbol if (this.Symbol != null) { // match the bound value if (Symbol.Equals(expression[exIndex++])) return MatchResult.Success; } // match same subexpression else if (this.Expression != null) { if (expression.CompareToExpression(exIndex, this.Expression)) { exIndex += this.Expression.Count; return MatchResult.Success; } } return MatchResult.Failure; }
private static PassiveExpression _Introduction(PassiveExpression expression) { Pattern pattern2 = new Pattern(); if (pattern2.Match(expression)) { return PassiveExpression.Build(_Prout(PassiveExpression.Build("This is a program to translate an arithmetic expression".ToCharArray())), _Prout(PassiveExpression.Build("into a code for a one-address computer. Primary operands".ToCharArray())), _Prout(PassiveExpression.Build("are identifiers and whole numbers. Operations are:".ToCharArray())), _Prout(PassiveExpression.Build("+, -, *, /, ^with usual priorities. Parentheses as usual.".ToCharArray())), _Prout(PassiveExpression.Build("Example: Joe^2*5/(SUM + 318)".ToCharArray())), _Prout(PassiveExpression.Build())); }; throw new RecognitionImpossibleException("Recognition impossible. Last expression: " + expression.ToString()); }
public PassiveExpression Open(PassiveExpression expression) { // <Open s.Mode s.D e.File-name> if (expression == null || expression.Count < 1) throw new ArgumentNullException("s.Mode"); else if (expression.Count < 2) throw new ArgumentNullException("s.D"); string mode = expression[0].ToString().ToUpper(); string handle = expression[1].ToString(); string fileName = string.Format("refal{0}.dat", handle); // fileName can be omitted if (expression.Count > 2) { fileName = expression.ToStringBuilder(2).ToString(); } // R - read, W - write, A - append if (mode.StartsWith("R")) { OpenFiles[handle] = new StreamReader(File.OpenRead(fileName)); } else if (mode.StartsWith("W")) { OpenFiles[handle] = new StreamWriter(File.Create(fileName)); } else if (mode.StartsWith("A")) { OpenFiles[handle] = File.AppendText(fileName); } else { throw new NotSupportedException("Bad file open mode: " + mode + " (R, W, or A expected)"); } // AFAIK, Open don't return anything return null; }
private static PassiveExpression _Lex(PassiveExpression expression) { Pattern pattern10 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace(), "(".ToCharArray(), new ExpressionVariable("e.2")); if (pattern10.Match(expression)) { return PassiveExpression.Build(_Lex(PassiveExpression.Build(new OpeningBrace(), new OpeningBrace(), pattern10.GetVariable("e.1"), new ClosingBrace(), new ClosingBrace(), pattern10.GetVariable("e.2")))); }; Pattern pattern11 = new Pattern(new OpeningBrace(), new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace(), new ExpressionVariable("e.2"), new ClosingBrace(), ")".ToCharArray(), new ExpressionVariable("e.3")); if (pattern11.Match(expression)) { return PassiveExpression.Build(_Lex(PassiveExpression.Build(new OpeningBrace(), pattern11.GetVariable("e.1"), new OpeningBrace(), pattern11.GetVariable("e.2"), new ClosingBrace(), new ClosingBrace(), pattern11.GetVariable("e.3")))); }; Pattern pattern12 = new Pattern(new OpeningBrace(), "$".ToCharArray(), new ExpressionVariable("e.1"), new ClosingBrace(), ")".ToCharArray(), new ExpressionVariable("e.3")); if (pattern12.Match(expression)) { return PassiveExpression.Build(_Prout(PassiveExpression.Build("ERROR: Unpaired right parenthsis:".ToCharArray())), _Prout(PassiveExpression.Build(pattern12.GetVariable("e.1"), ")".ToCharArray()))); }; Pattern pattern13 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace(), " ".ToCharArray(), new ExpressionVariable("e.2")); if (pattern13.Match(expression)) { return PassiveExpression.Build(_Lex(PassiveExpression.Build(new OpeningBrace(), pattern13.GetVariable("e.1"), new ClosingBrace(), pattern13.GetVariable("e.2")))); }; Pattern pattern14 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace(), "\\t".ToCharArray(), new ExpressionVariable("e.2")); if (pattern14.Match(expression)) { return PassiveExpression.Build(_Lex(PassiveExpression.Build(new OpeningBrace(), pattern14.GetVariable("e.1"), new ClosingBrace(), pattern14.GetVariable("e.2")))); }; Pattern pattern15 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace(), "\'".ToCharArray(), new ExpressionVariable("e.2")); if (pattern15.Match(expression)) { expression = PassiveExpression.Build(_Quotes(PassiveExpression.Build("\'".ToCharArray(), new OpeningBrace(), new ClosingBrace(), pattern15.GetVariable("e.2")))); { Pattern pattern16 = new Pattern(new OpeningBrace(), new ClosingBrace(), new ExpressionVariable("e.3")); pattern16.CopyBoundVariables(pattern15); if (pattern16.Match(expression)) { return PassiveExpression.Build(_Prout(PassiveExpression.Build("ERROR: Unpaired quote \'".ToCharArray(), pattern16.GetVariable("e.2")))); }; Pattern pattern17 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.0"), new ClosingBrace(), new ExpressionVariable("e.3")); pattern17.CopyBoundVariables(pattern15); if (pattern17.Match(expression)) { return PassiveExpression.Build(_Lex(PassiveExpression.Build(new OpeningBrace(), pattern17.GetVariable("e.1"), pattern17.GetVariable("e.0"), new ClosingBrace(), pattern17.GetVariable("e.2")))); }; throw new RecognitionImpossibleException("Recognition impossible. Last expression: " + expression.ToString()); } }; Pattern pattern19 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace(), new SymbolVariable("s.A"), new ExpressionVariable("e.2")); if (pattern19.Match(expression)) { expression = PassiveExpression.Build(_Type(PassiveExpression.Build(pattern19.GetVariable("s.A")))); { Pattern pattern20 = new Pattern("L".ToCharArray(), new ExpressionVariable("e.A1")); pattern20.CopyBoundVariables(pattern19); if (pattern20.Match(expression)) { expression = PassiveExpression.Build(_Id__tail(PassiveExpression.Build(new OpeningBrace(), pattern20.GetVariable("s.A"), new ClosingBrace(), pattern20.GetVariable("e.2")))); Pattern pattern21 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.Id"), new ClosingBrace(), new ExpressionVariable("e.3")); pattern21.CopyBoundVariables(pattern20); if (pattern21.Match(expression)) { return PassiveExpression.Build(_Lex(PassiveExpression.Build(new OpeningBrace(), pattern21.GetVariable("e.1"), _Implode(PassiveExpression.Build(pattern21.GetVariable("e.Id"))), new ClosingBrace(), pattern21.GetVariable("e.3")))); } }; Pattern pattern22 = new Pattern("D".ToCharArray(), new ExpressionVariable("e.A1")); pattern22.CopyBoundVariables(pattern19); if (pattern22.Match(expression)) { expression = PassiveExpression.Build(_D__string(PassiveExpression.Build(new OpeningBrace(), pattern22.GetVariable("s.A"), new ClosingBrace(), pattern22.GetVariable("e.2")))); Pattern pattern23 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.D-Str"), new ClosingBrace(), new ExpressionVariable("e.3")); pattern23.CopyBoundVariables(pattern22); if (pattern23.Match(expression)) { return PassiveExpression.Build(_Lex(PassiveExpression.Build(new OpeningBrace(), pattern23.GetVariable("e.1"), _Numb(PassiveExpression.Build(pattern23.GetVariable("e.D-Str"))), new ClosingBrace(), pattern23.GetVariable("e.3")))); } }; Pattern pattern24 = new Pattern(new SymbolVariable("s.T"), new ExpressionVariable("e.A1")); pattern24.CopyBoundVariables(pattern19); if (pattern24.Match(expression)) { return PassiveExpression.Build(_Lex(PassiveExpression.Build(new OpeningBrace(), pattern24.GetVariable("e.1"), pattern24.GetVariable("s.A"), new ClosingBrace(), pattern24.GetVariable("e.2")))); }; throw new RecognitionImpossibleException("Recognition impossible. Last expression: " + expression.ToString()); } }; Pattern pattern26 = new Pattern(new OpeningBrace(), "$".ToCharArray(), new ExpressionVariable("e.1"), new ClosingBrace()); if (pattern26.Match(expression)) { return PassiveExpression.Build(pattern26.GetVariable("e.1")); }; Pattern pattern27 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.M"), new ClosingBrace()); if (pattern27.Match(expression)) { return PassiveExpression.Build(_Prout(PassiveExpression.Build("ERROR: Unpaired left parentheses".ToCharArray())), _Pr__lmb(PassiveExpression.Build(pattern27.GetVariable("e.M")))); }; throw new RecognitionImpossibleException("Recognition impossible. Last expression: " + expression.ToString()); }
public bool Match(PassiveExpression expression) { int exIndex = 0, patIndex = 0; while (patIndex < Count || exIndex < expression.Count) { if (patIndex >= Count) { if (!RollBackToLastPartialMatch(ref exIndex, ref patIndex)) return false; continue; } switch (this[patIndex].Match(expression, ref exIndex, patIndex++)) { case MatchResult.Success: continue; case MatchResult.PartialSuccess: SaveRollBackInfo(exIndex, patIndex - 1); continue; } if (!RollBackToLastPartialMatch(ref exIndex, ref patIndex)) return false; } return true; }
public static PassiveExpression _Go(PassiveExpression expression) { Pattern pattern1 = new Pattern(); if (pattern1.Match(expression)) { return(PassiveExpression.Build(_Prout(PassiveExpression.Build(_PreAlph(PassiveExpression.Build("a".ToCharArray(), "c".ToCharArray())))))); } ; throw new RecognitionImpossibleException("Recognition impossible. Last expression: " + expression.ToString()); }