示例#1
0
            public OpDescrTriplet Add(int prec, string sassoc, string name, bool user)
            {
                AssocType      assoc = AssocType.None;
                OpDescrTriplet triplet;

                try
                {
                    assoc = (AssocType)Enum.Parse(typeof(AssocType), sassoc);
                }
                catch
                {
                    IO.Error("Illegal operator associativity '{0}'", sassoc);
                }

                if (prec < 0 || prec > 1200)
                {
                    IO.Error("Illegal precedence value {0} for operator '{1}'", prec, name);
                }

                if (TryGetValue(name, out triplet)) // operator exists -- modify its properties
                {
                    triplet.Assign(name, prec, assoc, user);
                }
                else
                {
                    this[name] = triplet = new OpDescrTriplet(name, prec, assoc, user);
                }

                return(triplet);
            }
示例#2
0
            public void Assign(string name, int prec, AssocType assoc, bool user)
            {
                AssocGroup fixType = GetFixType(assoc);

                od           = triplet[(int)fixType];
                od.Name      = (name == "','") ? "," : name;
                od.Prec      = prec;
                od.Assoc     = assoc;
                od.User      = user;
                od.IsInfix   = (fixType == AssocGroup.Infix);
                od.IsPrefix  = (fixType == AssocGroup.Prefix);
                od.IsPostfix = (fixType == AssocGroup.Postfix);
                od.LeftRelOp = (assoc == AssocType.yfx || assoc == AssocType.fy || assoc == AssocType.yf)
                             ? RelOp.LE
                             : RelOp.LT;
                od.RightRelOp = (assoc == AssocType.xfy || assoc == AssocType.fy || assoc == AssocType.yf)
                             ? RelOp.LE
                             : RelOp.LT;

                // An operator can be either prefix or postfix, but not both. In addition, it can be infix.
                if (fixType == AssocGroup.Prefix)
                {
                    triplet[(int)AssocGroup.Postfix].Undefine();
                }
                else if (fixType == AssocGroup.Postfix)
                {
                    triplet[(int)AssocGroup.Prefix].Undefine();
                }
            }
示例#3
0
 public void CopyValuesFrom(BaseTerm t)
 {
     functor    = t.functor;
     args       = t.args;
     termType   = t.termType;
     assocType  = t.assocType;
     precedence = t.precedence;
 }
示例#4
0
 public void Undefine()
 {
     name      = null;
     prec      = -1;
     assoc     = AssocType.None;
     user      = false;
     leftRelOp = rightRelOp = RelOp.None;
     isInfix   = false;
     isPrefix  = false;
     isPostfix = false;
 }
示例#5
0
 internal Oper(AssocType assoc, TokenType type, int level, ParseDelegate parseFunc = null, CombineDelegate combineFunc = null) : this(assoc, type, level)
 {
     if (parseFunc != null)
     {
         Parse = parseFunc;
     }
     if (combineFunc != null)
     {
         Combine = combineFunc;
     }
 }
示例#6
0
            public OpDescrTriplet(string name, int prec, AssocType assoc, bool user)
            {
                this.name = name;
                triplet   = new OperatorDescr[3];

                for (int i = 0; i < 3; i++)
                {
                    triplet[i] = new OperatorDescr();
                }

                Assign(name, prec, assoc, user);
            }
示例#7
0
            public void Unassign(string name, AssocType assoc)
            {
                AssocGroup fixType = GetFixType(assoc);

                od = triplet[(int)fixType];

                if (od == null || od.Assoc == AssocType.None)
                {
                    IO.Error("Operator '{0}' does not have an association type '{1}'", name, assoc);
                }

                triplet[(int)fixType].Undefine();
            }
示例#8
0
            public OperatorDescr AddPrologOperator(int prec, string type, string name, bool user)
            {
                AssocType assoc = AssocType.None;

                try
                {
                    assoc = (AssocType)Enum.Parse(typeof(AssocType), type);
                }
                catch
                {
                    IO.Error("Illegal operator type '{0}'", type);
                }

                if (prec < 0 || prec > 1200)
                {
                    IO.Error("Illegal precedence value {0} for operator '{1}'", prec, name);
                }

                TerminalDescr  td;
                OpDescrTriplet triplet;

                if (terminalTable.Find(name, out td))

                { // some operator symbols (:-, -->, +, ...) already exist prior to their op/3 definition
                    if (td.Payload == null)
                    {
                        td.Payload = opTable.Add(prec, type, name, user);
                        td.IVal    = Operator;
                    }
                    else // no need to add it -- just change its properties
                    {
                        ((OpDescrTriplet)td.Payload).Assign(name, prec, assoc, user);
                        td.IVal = Operator;
                    }

                    triplet = (OpDescrTriplet)td.Payload;
                }
                else // new operator
                {
                    triplet = opTable.Add(prec, type, name, user);
                    terminalTable.Add(name, Operator, triplet);
                }

                return(triplet [assoc]);
            }
示例#9
0
            static AssocGroup GetFixType(AssocType assoc)
            {
                switch (assoc)
                {
                case AssocType.xfx:
                case AssocType.xfy:
                case AssocType.yfx:
                    return(AssocGroup.Infix);

                case AssocType.fx:
                case AssocType.fy:
                    return(AssocGroup.Prefix);

                case AssocType.xf:
                case AssocType.yf:
                    return(AssocGroup.Postfix);
                }

                throw new ApplicationException("Illegal call to GetFixType");
            }
示例#10
0
            public void RemovePrologOperator(string type, string name, bool user)
            {
                AssocType assoc = AssocType.None;

                try
                {
                    assoc = (AssocType)Enum.Parse(typeof(AssocType), type);
                }
                catch
                {
                    IO.Error("Illegal operator type '{0}'", type);
                }

                TerminalDescr td;

                if (terminalTable.Find(name, out td) && td.Payload != null)
                {
                    if (!user)
                    {
                        IO.Error("Undefine of operator ({0}, {1}) not allowed", type, name);
                    }
                    else
                    {
                        ((OpDescrTriplet)td.Payload).Unassign(name, assoc);

                        foreach (OperatorDescr od in ((OpDescrTriplet)td.Payload))
                        {
                            if (od.IsDefined)
                            {
                                return;
                            }
                        }

                        terminalTable.Remove(name); // name no longer used
                    }
                }
                else // unknown operator
                {
                    IO.Error("Operator not found: ({0}, {1})", type, name);
                }
            }
示例#11
0
 public OperatorDescr this[AssocType assoc] {
     get { return(triplet[(int)GetFixType(assoc)]); }
 }
示例#12
0
            public TermNode ToGoalList(int stackSize, int level) // called during execution (when there is a stack)
            {
                TermNode result = null;
                BaseTerm t0, t1;

                TermType  tt = this.TermType;
                AssocType at = this.AssocType;
                int       pr = this.Precedence;

                if (this is Cut)
                {
                    if (stackSize == 0)
                    {
                        return(new TermNode(this, null, level));
                    }
                    else
                    {
                        return(new TermNode(new Cut(stackSize), null, level));
                    }
                }

                switch (this.Functor as string)
                {
                case PrologParser.IMPLIES:
                    t0 = Arg(0);
                    if (!t0.IsCallable)
                    {
                        IO.Error("Illegal predicate head: {0}", t0);
                    }
                    t1     = Arg(1);
                    result = new TermNode(t0, t1.ToGoalList(stackSize, level));
                    break;

                case PrologParser.DCGIMPL:
                    t0 = Arg(0);
                    if (!t0.IsCallable)
                    {
                        IO.Error("Illegal DCG head: {0}", t0);
                    }
                    t1     = Arg(1);
                    result = new TermNode(t0, t1.ToGoalList(stackSize, level));
                    break;

                case PrologParser.COMMA:
                    t0     = Arg(0);
                    t1     = Arg(1);
                    result = t0.ToGoalList(stackSize, level);
                    result.Append(t1.ToGoalList(stackSize, level));
                    break;

                case PrologParser.DOT:
                    t0     = Arg(0);
                    t1     = Arg(1);
                    result = (new CompoundTerm("consult", new ListTerm(t0, t1))).ToGoalList(stackSize, level);
                    break;

                case PrologParser.CURL:
                    t0     = Arg(0);
                    result = t0.ToGoalList(stackSize, level);
                    break;

                default:
                    if (this.IsVar)
                    {
                        result = new TermNode(new CompoundTerm("meta$call", this), null, level);
                    }
                    else if (this.IsCallable)
                    {
                        result = new TermNode(this, null, level);
                    }
                    else
                    {
                        IO.Error("Illegal term {0} in goal list", this);
                    }
                    break;
                }

                return(result);
            }
示例#13
0
 protected BaseTerm() // base() constructor
 {
     assocType  = AssocType.None;
     precedence = 0;
 }
示例#14
0
 public BaseToken()
 {
     this.group = AssocType.None;
 }
示例#15
0
            internal Oper(AssocType assoc, TokenType type, int level)
            {
                this.assoc = assoc;
                this.type  = type;
                this.level = level;
                switch (assoc)
                {
                case AssocType.BinaryLeft:
                case AssocType.BinaryRight:
                    switch (type)
                    {
                    case TokenType.GT:
                        Parse = _parse_gt;
                        break;

                    case TokenType.LT:
                        Parse = _parse_lt;
                        break;

                    default:
                        Parse = _parse;
                        break;
                    }
                    Combine = _combine_binary;
                    break;

                case AssocType.PostfixDot:
                    this.assoc = AssocType.Postfix;
                    Parse      = _parse_postfix_dot;
                    Combine    = _combine_postfix_dot;
                    break;

                case AssocType.PostfixColon:
                    this.assoc = AssocType.Postfix;
                    Parse      = _parse_postfix_colon;
                    Combine    = _combine_postfix_colon;
                    break;

                case AssocType.BinaryDot:
                    this.assoc = AssocType.BinaryLeft;
                    Parse      = _parse;
                    Combine    = _combine_binary_dot;
                    break;

                case AssocType.BinaryColon:
                    this.assoc = AssocType.BinaryLeft;
                    Parse      = _parse;
                    Combine    = _combine_binary_colon;
                    break;

                case AssocType.PostfixCall:
                    this.assoc = AssocType.Postfix;
                    Parse      = _parse_postfix_call;
                    Combine    = _combine_postfix_call;
                    break;

                case AssocType.PostfixIndex:
                    this.assoc = AssocType.Postfix;
                    Parse      = _parse_postfix_index;
                    Combine    = _combine_postfix_index;
                    break;

                case AssocType.BinaryAssign:
                    this.assoc = AssocType.BinaryRight;
                    Parse      = _parse;
                    Combine    = _combine_binary_assign;
                    break;

                case AssocType.BinaryAssignOp:
                    this.assoc = AssocType.BinaryRight;
                    Parse      = _parse;
                    Combine    = _combine_binary_assign_op;
                    break;

                case AssocType.BinaryLogic:
                    this.assoc = AssocType.BinaryLeft;
                    Parse      = _parse;
                    Combine    = _combine_binary_logic;
                    break;

                case AssocType.Postfix:
                    Parse   = _parse;
                    Combine = _combine_postfix;
                    break;

                case AssocType.Prefix:
                    Parse   = _parse;
                    Combine = _combine_prefix;
                    break;

                case AssocType.PostfixAssign:
                    this.assoc = AssocType.Postfix;
                    Parse      = _parse;
                    Combine    = _combine_postfix_assign;
                    break;

                case AssocType.PrefixAssign:
                    this.assoc = AssocType.Prefix;
                    Parse      = _parse;
                    Combine    = _combine_prefix_assign;
                    break;

                case AssocType.PostfixIs:
                    this.assoc = AssocType.Postfix;
                    Parse      = _parse_is_as;
                    Combine    = _combine_postfix_is;
                    break;

                case AssocType.PostfixAsType:
                    this.assoc = AssocType.Postfix;
                    Parse      = _parse_is_as;
                    Combine    = _combine_postfix_as_type;
                    break;

                case AssocType.PrefixCast:
                    this.assoc = AssocType.Prefix;
                    Parse      = _parse;
                    Combine    = _combine_prefix_cast;
                    break;

                case AssocType.PrefixRuntimeId:
                    this.assoc = AssocType.Prefix;
                    Parse      = _parse;
                    Combine    = _combine_prefix_runtime_id;
                    break;

                case AssocType.BinaryAlias:
                    this.assoc = AssocType.BinaryLeft;
                    Parse      = _parse_alias;
                    Combine    = _combine_binary_alias;
                    break;

                case AssocType.BinarySubstr:
                    this.assoc = AssocType.BinaryLeft;
                    Parse      = _parse;
                    Combine    = _combine_binary_substr;
                    break;

                case AssocType.None:
                    Parse   = _parse_empty;
                    Combine = null;
                    break;
                }
            }