public ComplexTerm(DecimalTerm d) { this.re = d.ValueD; this.im = 0; functor = string.Format("{0}+{1}i", re, im); termType = TermType.Number; }
public ComplexTerm Divide(DecimalTerm d) { if (d.Value == 0) { IO.Error("Division by zero not allowed"); } return(new ComplexTerm(re / d.ValueD, im / d.ValueD)); }
public bool GetNextValue(out DecimalTerm dt) { dt = null; if (!iEnum.MoveNext()) { return(false); } dt = (DecimalTerm)iEnum.Current; return(true); }
void JsonLiteral(TerminalSet _TS, out BaseTerm t) { #if LL1_tracing ReportParserProcEntry("JsonLiteral"); #endif GetSymbol(new TerminalSet(terminalCount, IntLiteral, RealLiteral, StringLiteral, TrueSym, FalseSym, NullSym), false, true); if (symbol.IsMemberOf(IntLiteral, RealLiteral)) { GetSymbol(new TerminalSet(terminalCount, IntLiteral, RealLiteral), false, true); if (symbol.Terminal == IntLiteral) { symbol.SetProcessed(); } else { symbol.SetProcessed(); } t = new DecimalTerm(symbol.ToDecimal()); } else if (symbol.Terminal == StringLiteral) { symbol.SetProcessed(); t = new StringTerm(symbol.ToString().Dequoted()); } else if (symbol.Terminal == TrueSym) { symbol.SetProcessed(); t = new AtomTerm("true"); } else if (symbol.Terminal == FalseSym) { symbol.SetProcessed(); t = new AtomTerm("false"); } else { symbol.SetProcessed(); t = new AtomTerm("null"); } #if LL1_tracing ReportParserProcExit("JsonLiteral"); #endif }
public static BaseTerm MakeMatchTerm(Match m, bool asAtom) { BaseTerm[] args = new BaseTerm[4]; if (asAtom) { args[0] = new AtomTerm(m.Value.ToAtom()); } else { args[0] = new StringTerm(m.Value.ToString()); } args[1] = new DecimalTerm(m.Index); args[2] = new DecimalTerm(m.Length); args[3] = new AtomTerm("m.Groups"); return(new CompoundTerm("match", args)); }
public ComplexTerm Power(DecimalTerm d) // z^n = r^n * (cos (n*phi) + i*sin(n*phi)) { if (d.IsInteger) { int n = d.To <int> (); if (re == 0 && im == 0) // try Google 'what is 0 to the power of 0', etc. { return(new ComplexTerm(n == 0 ? 1 : 0, 0f)); } double rn = Math.Pow(magnitude, n); double nArg = n * arg; return(new ComplexTerm(rn * Math.Cos(nArg), rn * Math.Sin(nArg))); } else { return((Log().Multiply(d)).Exp()); // t^d = exp(log(t^d)) = exp(d*log(t)) } }
private void JsonLiteral(TerminalSet _TS, out BaseTerm t) { GetSymbol(new TerminalSet(terminalCount, IntLiteral, RealLiteral, StringLiteral, TrueSym, FalseSym, NullSym), false, true); if (symbol.IsMemberOf(IntLiteral, RealLiteral)) { GetSymbol(new TerminalSet(terminalCount, IntLiteral, RealLiteral), false, true); if (symbol.TerminalId == IntLiteral) { symbol.SetProcessed(); } else { symbol.SetProcessed(); } t = new DecimalTerm(symbol.ToDecimal()); } else if (symbol.TerminalId == StringLiteral) { symbol.SetProcessed(); t = new StringTerm(symbol.ToString().Dequoted()); } else if (symbol.TerminalId == TrueSym) { symbol.SetProcessed(); t = new AtomTerm("true"); } else if (symbol.TerminalId == FalseSym) { symbol.SetProcessed(); t = new AtomTerm("false"); } else { symbol.SetProcessed(); t = new AtomTerm("null"); } }
static BaseTerm ToTermEx(Node root) { BaseTerm [] args = new BaseTerm [3]; string tagName = root.TagName.ToAtom(); args [1] = ListTerm.EMPTYLIST; Decimal d; foreach (KeyValuePair <string, string> kv in root.Attributes) // XML Declaration { BaseTerm pair; if (Decimal.TryParse(kv.Value, styleAllowDecPnt, CIC, out d)) { pair = new OperatorTerm(EqualOpDescr, new AtomTerm(kv.Key), new DecimalTerm(d)); } else { pair = new OperatorTerm(EqualOpDescr, new AtomTerm(kv.Key), new StringTerm(kv.Value)); } args [1] = new ListTerm(pair, args [1]); } args [2] = ListTerm.EMPTYLIST; if (root.ChildNodes.Count > 0) { foreach (Node n in root.ChildNodes) { BaseTerm e; e = null; switch (n.type) { case XmlNodeType.Element: e = ToTermEx(n); break; case XmlNodeType.Comment: e = new CompoundTerm(COMMENT, new StringTerm(n.text.Trim().EscapeDoubleQuotes())); break; case XmlNodeType.Text: if (Decimal.TryParse(n.text, styleAllowDecPnt, CIC, out d)) { e = new DecimalTerm(d); } else if (n.text.HasAtomFormat()) { e = new AtomTerm(n.text); } else { e = new StringTerm(n.text.EscapeDoubleQuotes()); } break; case XmlNodeType.SignificantWhitespace: case XmlNodeType.Whitespace: e = new StringTerm(n.text); break; case XmlNodeType.CDATA: e = new CompoundTerm(CDATA, new StringTerm(n.text.Trim().EscapeDoubleQuotes())); break; case XmlNodeType.ProcessingInstruction: e = new CompoundTerm("processing_instruction", new AtomTerm(n.name.ToAtom()), new StringTerm(n.text.Trim().EscapeDoubleQuotes())); break; default: break; } if (e != null) { args [2] = new ListTerm(e, args [2]); } } } if (args [1].IsEmptyList) { return(new CompoundTerm(tagName, args [2])); } else { return(new CompoundTerm(tagName, args [1], args [2])); } }
public ComplexTerm Multiply(DecimalTerm d) { return(new ComplexTerm(re * d.ValueD, im * d.ValueD)); }
public ComplexTerm Subtract(DecimalTerm d) { return(new ComplexTerm(re - d.ValueD, im)); }
public ComplexTerm Add(DecimalTerm d) { return(new ComplexTerm(d.ValueD + re, im)); }
static BaseTerm ToTermEx(Node root) { BaseTerm [] args = new BaseTerm [3]; #if fullyTagged args [0] = new AtomTerm(root.TagName.ToAtom()); #else string tagName = root.TagName.ToAtom(); #endif args [1] = ListTerm.EMPTYLIST; Decimal d; foreach (KeyValuePair <string, string> kv in root.Attributes) // XML Declaration { BaseTerm pair; if (Decimal.TryParse(kv.Value, styleAllowDecPnt, Utils.CIC, out d)) { pair = new OperatorTerm(EqualOpDescr, new AtomTerm(kv.Key), new DecimalTerm(d)); } else { pair = new OperatorTerm(EqualOpDescr, new AtomTerm(kv.Key), new StringTerm(kv.Value)); } args [1] = new ListTerm(pair, args [1]); } args [2] = ListTerm.EMPTYLIST; if (root.ChildNodes.Count > 0) { foreach (Node n in root.ChildNodes) { BaseTerm e; e = null; switch (n.type) { case XmlNodeType.Element: e = ToTermEx(n); break; case XmlNodeType.Comment: e = new CompoundTerm(COMMENT, new StringTerm(n.text.Trim().EscapeDoubleQuotes())); break; case XmlNodeType.Text: if (Decimal.TryParse(n.text, styleAllowDecPnt, Utils.CIC, out d)) #if fullyTagged { e = new CompoundTerm(TEXT, new DecimalTerm(d)); } else { e = new CompoundTerm(TEXT, new StringTerm(n.text.Trim().EscapeDoubleQuotes())); } #else { e = new DecimalTerm(d); } else { e = new StringTerm(n.text.Trim().EscapeDoubleQuotes()); } #endif break;
static BaseTerm ToTermEx(Node root) { BaseTerm [] args = new BaseTerm [3]; #if fullyTagged args [0] = new AtomTerm (root.TagName.ToAtom ()); #else string tagName = root.TagName.ToAtom (); #endif args [1] = ListTerm.EMPTYLIST; Decimal d; foreach (KeyValuePair<string, string> kv in root.Attributes) // XML Declaration { BaseTerm pair; if (Decimal.TryParse (kv.Value, styleAllowDecPnt, Utils.CIC, out d)) pair = new OperatorTerm (EqualOpDescr, new AtomTerm (kv.Key), new DecimalTerm (d)); else pair = new OperatorTerm (EqualOpDescr, new AtomTerm (kv.Key), new StringTerm (kv.Value)); args [1] = new ListTerm (pair, args [1]); } args [2] = ListTerm.EMPTYLIST; if (root.ChildNodes.Count > 0) { foreach (Node n in root.ChildNodes) { BaseTerm e; e = null; switch (n.type) { case XmlNodeType.Element: e = ToTermEx (n); break; case XmlNodeType.Comment: e = new CompoundTerm (COMMENT, new StringTerm (n.text.Trim ().EscapeDoubleQuotes ())); break; case XmlNodeType.Text: if (Decimal.TryParse (n.text, styleAllowDecPnt, Utils.CIC, out d)) #if fullyTagged e = new CompoundTerm (TEXT, new DecimalTerm (d)); else e = new CompoundTerm (TEXT, new StringTerm (n.text.Trim ().EscapeDoubleQuotes ())); #else e = new DecimalTerm (d); else e = new StringTerm (n.text.Trim ().EscapeDoubleQuotes ()); #endif break; case XmlNodeType.CDATA: e = new CompoundTerm (CDATA, new StringTerm (n.text.Trim ().EscapeDoubleQuotes ())); break; case XmlNodeType.ProcessingInstruction: e = new CompoundTerm ("processing_instruction", new AtomTerm (n.name.ToAtom ()), new StringTerm (n.text.Trim ().EscapeDoubleQuotes ())); break; case XmlNodeType.SignificantWhitespace: case XmlNodeType.Whitespace: break;
public static BaseTerm FindRegexMatches( OperatorTable opTable, string source, string matchPattern, string[] options) { Regex re = null; RegexOptions reOptions = RegexOptions.None; if (options != null) { foreach (string o in options) { switch (o.ToLower()) { case "ignorecase": reOptions |= RegexOptions.IgnoreCase; break; case "multiline": reOptions |= RegexOptions.Multiline; break; case "singleline": reOptions |= RegexOptions.Singleline; break; case "explicitcapture": reOptions |= RegexOptions.ExplicitCapture; break; case "cultureinvariant": reOptions |= RegexOptions.CultureInvariant; break; default: IO.Error("match_regex -- unsupported option '{0}'", o); break; } } ; } try { re = new Regex(matchPattern, reOptions); //, RegexOptions.Multiline); } catch (Exception x) { IO.Error("Error in regular expression '{0}'\r\nMessage: {1}", matchPattern, x.Message); } Match m = re.Match(source); if (!m.Success) { return(null); } int[] gnums = re.GetGroupNumbers(); BaseTerm groups = new ListTerm(); while (m.Success) { for (int i = 1; i < gnums.Length; i++) // start at group 1 (0 is the fully captured match string) { Group g = m.Groups[gnums[i]]; BaseTerm captures = new ListTerm(); string groupId = re.GetGroupNames()[i]; int groupNo; BaseTerm groupIdTerm; foreach (Capture c in g.Captures) { captures = ((ListTerm)captures).AppendElement(new StringTerm(c.ToString())); } if (int.TryParse(groupId, out groupNo)) { groupIdTerm = new DecimalTerm(groupNo); } else { groupIdTerm = new StringTerm(re.GetGroupNames()[i]); } groups = ((ListTerm)groups).AppendElement( new OperatorTerm(opTable, ":", groupIdTerm, captures)); } m = m.NextMatch(); } if (((ListTerm)groups).ProperLength == 1) { groups = groups.Arg(0); // groups is <groupname>:[<captures>] if (groups.Arg(0) is DecimalTerm) { groups = groups.Arg(1); // anonymous group, just return the list of captures } } return(groups); }