Example #1
0
 public static void RenameFirstInstance(string sOld, string sNew, List <CatAstNode> terms)
 {
     for (int i = 0; i < terms.Count; ++i)
     {
         if (TermContains(terms[i], sOld))
         {
             if (terms[i] is AstLambda)
             {
                 AstLambda l = terms[i] as AstLambda;
                 RenameFirstInstance(sOld, sNew, l.mTerms);
                 return;
             }
             else if (terms[i] is AstQuote)
             {
                 AstQuote q = terms[i] as AstQuote;
                 RenameFirstInstance(sOld, sNew, q.mTerms);
                 return;
             }
             else
             {
                 // Should never happen.
                 throw new Exception("expected either a lamda node or quote node");
             }
         }
         else if (TermEquals(terms[i], sOld))
         {
             terms[i] = new AstName(sNew);
             return;
         }
     }
     throw new Exception(sOld + " was not found in the list of terms");
 }
Example #2
0
 public static void RenameFirstInstance(string sOld, string sNew, List<CatAstNode> terms)
 {
     for (int i = 0; i < terms.Count; ++i)
     {
         if (TermContains(terms[i], sOld))
         {
             if (terms[i] is AstLambda)
             {
                 AstLambda l = terms[i] as AstLambda;
                 RenameFirstInstance(sOld, sNew, l.mTerms);
                 return;
             }
             else if (terms[i] is AstQuote)
             {
                 AstQuote q = terms[i] as AstQuote;
                 RenameFirstInstance(sOld, sNew, q.mTerms);
                 return;
             }
             else 
             {
                 // Should never happen.
                 throw new Exception("expected either a lamda node or quote node");
             }
         }
         else if (TermEquals(terms[i], sOld))
         {
             terms[i] = new AstName(sNew);
             return;
         }
     }
     throw new Exception(sOld + " was not found in the list of terms");
 }
Example #3
0
        public static void RemoveTerm( string var, List<CatAstNode> terms ) {
            // Find the first term that either contains, or is equal to the 
            // free variable
            int i = 0;
            while (i < terms.Count) {
                if (TermContainsOrEquals( terms[ i ], var ))
                    break;
                ++i;
            }

            if (i == terms.Count)
                throw new Exception( "error in abstraction elimination algorithm" );

            if (i > 0) {
                AstQuote q = new AstQuote( terms.GetRange( 0, i ) );
                terms.RemoveRange( 0, i );
                terms.Insert( 0, q );
                terms.Insert( 1, new AstName( "dip" ) );
                i = 2;
            } else {
                i = 0;
            }

            if (TermEquals( terms[ i ], var )) {
                terms[ i ] = new AstName( "id" );
                return;
            } else if (TermContains( terms[ i ], var )) {
                if (terms[ i ] is AstQuote) {
                    AstQuote subExpr = terms[ i ] as AstQuote;
                    RemoveTerm( var, subExpr.mTerms );
                } else if (TermContains( terms[ i ], var )) {
                    AstLambda subExpr = terms[ i ] as AstLambda;
                    RemoveTerm( var, subExpr.mTerms );
                } else {
                    throw new Exception( "internal error: expected either a quotation or lambda term" );
                }
                terms.Insert( i + 1, new AstName( "papply" ) );
                return;
            } else {
                throw new Exception( "error in abstraction elimination algorithm" );
            }
        }
Example #4
0
        public AstDef(PegAstNode node)
            : base(node)
        {
            CheckLabel(AstLabel.Def);

            if (node.GetNumChildren() == 0)
            {
                throw new Exception("invalid function definition node");
            }

            AstName name = new AstName(node.GetChild(0));

            mName = name.ToString();

            int n = 1;

            // Look to see if a type is defined
            if ((node.GetNumChildren() >= 2) && (node.GetChild(1).GetLabel().Equals(AstLabel.FxnType)))
            {
                mType = new AstFxnType(node.GetChild(1));
                ++n;
            }

            while (n < node.GetNumChildren())
            {
                PegAstNode child = node.GetChild(n);

                if (!child.GetLabel().Equals(AstLabel.Param))
                {
                    break;
                }

                mParams.Add(new AstParam(child));
                n++;
            }

            while (n < node.GetNumChildren())
            {
                PegAstNode child = node.GetChild(n);

                if (!child.GetLabel().Equals(AstLabel.Param))
                {
                    break;
                }

                mParams.Add(new AstParam(child));
                n++;
            }

            while (n < node.GetNumChildren())
            {
                PegAstNode child = node.GetChild(n);

                if (!child.GetLabel().Equals(AstLabel.MetaDataBlock))
                {
                    break;
                }

                mpMetaData = new AstMetaDataBlock(child);
                n++;
            }

            while (n < node.GetNumChildren())
            {
                PegAstNode child = node.GetChild(n);

                if (!child.GetLabel().Equals(AstLabel.Def))
                {
                    break;
                }

                mLocals.Add(new AstDef(child));
                n++;
            }

            while (n < node.GetNumChildren())
            {
                PegAstNode child = node.GetChild(n);
                CatAstNode expr  = Create(child);

                if (!(expr is AstExpr))
                {
                    throw new Exception("expected expression node");
                }

                mTerms.Add(expr as AstExpr);
                n++;
            }
        }
Example #5
0
        public static void RemoveTerm(string var, List<CatAstNode> terms)
        {
            // Find the first term that either contains, or is equal to the 
            // free variable
            int i = 0;
            while (i < terms.Count)
            {
                if (TermContainsOrEquals(terms[i], var))
                    break;
                ++i;
            }

            if (i == terms.Count)
                throw new Exception("error in abstraction elimination algorithm");

            if (i > 0)
            {
                AstQuote q = new AstQuote(terms.GetRange(0, i));
                terms.RemoveRange(0, i);
                terms.Insert(0, q);
                terms.Insert(1, new AstName("dip"));
                i = 2;
            }
            else
            {
                i = 0;
            }

            if (TermEquals(terms[i], var))
            {
                terms[i] = new AstName("id");
                return;
            }
            else if (TermContains(terms[i], var))
            {
                if (terms[i] is AstQuote)
                {
                    AstQuote subExpr = terms[i] as AstQuote;
                    RemoveTerm(var, subExpr.mTerms);
                }
                else if (TermContains(terms[i], var))
                {
                    AstLambda subExpr = terms[i] as AstLambda;
                    RemoveTerm(var, subExpr.mTerms);
                }
                else
                {
                    throw new Exception("internal error: expected either a quotation or lambda term");
                }
                terms.Insert(i + 1, new AstName("papply"));
                return;
            }
            else
            {
                throw new Exception("error in abstraction elimination algorithm");
            }
        }
Example #6
0
        public AstDef(PegAstNode node)
            : base(node)
        {
            CheckLabel(AstLabel.Def);

            if (node.GetNumChildren() == 0)
                throw new Exception("invalid function definition node");

            AstName name = new AstName(node.GetChild(0));
            mName = name.ToString();

            int n = 1;

            // Look to see if a type is defined
            if ((node.GetNumChildren() >= 2) && (node.GetChild(1).GetLabel().Equals(AstLabel.FxnType)))
            {
                mType = new AstFxnType(node.GetChild(1));
                ++n;
            }

            while (n < node.GetNumChildren())
            {
                PegAstNode child = node.GetChild(n);

                if (!child.GetLabel().Equals(AstLabel.Param))
                    break;

                mParams.Add(new AstParam(child));
                n++;
            }

            while (n < node.GetNumChildren())
            {
                PegAstNode child = node.GetChild(n);

                if (!child.GetLabel().Equals(AstLabel.Param))
                    break;

                mParams.Add(new AstParam(child));
                n++;
            }

            while (n < node.GetNumChildren())
            {
                PegAstNode child = node.GetChild(n);

                if (!child.GetLabel().Equals(AstLabel.MetaDataBlock))
                    break;

                mpMetaData = new AstMetaDataBlock(child);
                n++;
            }

            while (n < node.GetNumChildren())
            {
                PegAstNode child = node.GetChild(n);

                if (!child.GetLabel().Equals(AstLabel.Def))
                    break;

                mLocals.Add(new AstDef(child));
                n++;
            }

            while (n < node.GetNumChildren())
            {
                PegAstNode child = node.GetChild(n);
                CatAstNode expr = Create(child);

                if (!(expr is AstExpr))
                    throw new Exception("expected expression node");

                mTerms.Add(expr as AstExpr);
                n++;
            }
        }