private static State plist(char[] fmt, ref int pos, ref System.Collections.Generic.List <Erlang.Object> items, ref int argc, params object[] args) { State rc = State.ERL_FMT_ERR; if (pos < fmt.Length) { pos = skip_null_chars(fmt, pos); switch (fmt[pos++]) { case ']': rc = State.ERL_OK; break; case ',': rc = plist(fmt, ref pos, ref items, ref argc, args); break; case '|': pos = skip_null_chars(fmt, pos); if (char.IsUpper(fmt[pos]) || fmt[pos] == '_') { TermType t; string s = pvariable(fmt, ref pos, out t); items.Add(new Erlang.Var(s, t)); pos = skip_null_chars(fmt, pos); if (fmt[pos] == ']') { rc = State.ERL_OK; } break; } break; default: { --pos; Erlang.Object obj = create(fmt, ref pos, ref argc, args); items.Add(obj); rc = plist(fmt, ref pos, ref items, ref argc, args); break; } } } return(rc); }
public override bool match(Erlang.Object pattern, Erlang.VarBind binding) { if (binding == null) { return(false); } Erlang.Object value = binding.find(m_var); if (value != null) { return(checkType(value) ? value.match(pattern, binding) : false); } else if (!checkType(pattern)) { return(false); } Erlang.Object term = null; binding[m_var] = pattern.subst(ref term, binding) ? term : pattern; return(true); }
public override bool subst(ref Erlang.Object obj, Erlang.VarBind binding) { if (isAny() || binding == null || binding.Empty) { throw new UnboundVarException(); } Erlang.Object term = binding[m_var]; if (term == null) { throw new UnboundVarException("Variable " + m_var + " not bound!"); } if (!checkType(term)) { throw new InvalidValueType( string.Format("Invalid variable {0} value type (got={1}, expected={2})", m_var, obj.Type, m_termType)); } obj = term; return(true); }
public override bool match(Erlang.Object pattern, VarBind binding) { if (pattern is Erlang.Var) { pattern.match(this, binding); } else if (!(pattern is Erlang.Tuple)) { return(false); } Erlang.Tuple tup = pattern as Erlang.Tuple; if (arity() != tup.arity()) { return(false); } for (int i = 0; i < arity(); ++i) { if (!elems[i].match(tup[i], binding)) { return(false); } } return(true); }
public virtual bool subst(ref Erlang.Object term, Erlang.VarBind binding) { return(false); }
public virtual bool match(Erlang.Object pattern, Erlang.VarBind binding) { return((pattern is Erlang.Var) ? pattern.match(this, binding) : this.Equals(pattern)); }
public void bind(string name, Erlang.Object obj) { m_dict[name] = obj; }
public Pattern(int id, PatternMatchAction b, string pattern, params object[] args) { ID = id; Action = b; Term = Object.Format(pattern, args); }
public Pattern(int id, PatternMatchAction b, Erlang.Object p) { ID = id; Action = b; Term = p; }
internal static Erlang.Object create( char[] fmt, ref int pos, ref int argc, params object[] args) { var items = new System.Collections.Generic.List <Erlang.Object>(); Erlang.Object result = null; pos = skip_null_chars(fmt, pos); switch (fmt[pos++]) { case '{': if (State.ERL_OK != ptuple(fmt, ref pos, ref items, ref argc, args)) { throw new FormatException( string.Format("Error parsing tuple at pos {0}", pos)); } result = new Erlang.Tuple(items.ToArray()); break; case '[': if (fmt[pos] == ']') { result = new Erlang.List(); pos++; break; } else if (State.ERL_OK == plist(fmt, ref pos, ref items, ref argc, args)) { result = new Erlang.List(items.ToArray()); break; } throw new FormatException( string.Format("Error parsing list at pos {0}", pos)); case '$': /* char-value? */ result = new Erlang.Char(fmt[pos++]); break; case '~': if (State.ERL_OK != pformat(fmt, ref pos, ref items, ref argc, args)) { throw new FormatException( string.Format("Error parsing term at pos {0}", pos)); } result = items[0]; break; default: char c = fmt[--pos]; if (char.IsLower(c)) /* atom ? */ { string s = patom(fmt, ref pos); result = createAtom(s); } else if (char.IsUpper(c) || c == '_') { TermType t; string s = pvariable(fmt, ref pos, out t); result = new Erlang.Var(s, t); } else if (char.IsDigit(c) || c == '-') /* integer/float ? */ { string s = pdigit(fmt, ref pos); if (s.IndexOf('.') < 0) { result = new Erlang.Long(long.Parse(s, CultureInfo.InvariantCulture)); } else { result = new Erlang.Double(double.Parse(s, CultureInfo.InvariantCulture)); } } else if (c == '"') /* string ? */ { string s = pstring(fmt, ref pos); result = new Erlang.String(s); } else if (c == '\'') /* quoted atom ? */ { string s = pquotedAtom(fmt, ref pos); result = createAtom(s); } break; } return(result); }
private bool checkType(Erlang.Object value) { var vt = value.TermType; return(m_termType == TermType.Object || vt == m_termType); }