Ejemplo n.º 1
0
        /* DEBUG: 
        private static string VarsToString(List<string> vars)
        {
            string ret = "";
            foreach (string s in vars)
                ret += s + " ";
            return ret;
        }
         */

        /// <summary>
        /// Converts a list of terms to point-free form. 
        /// </summary>
        public static void ConvertTerms( List<string> vars, List<CatAstNode> terms ) {
            for (int i = 0; i < terms.Count; ++i) {
                CatAstNode exp = terms[ i ];
                if (exp is AstLambda)
                    Convert( exp as AstLambda );
            }

            if (vars.Count == 0)
                return;

            for (int i = 0; i < vars.Count; ++i) {
                string var = vars[ i ];

                if (CountInstancesOf( var, terms ) == 0) {
                    // Remove unused arguments
                    terms.Insert( 0, new AstName( "pop" ) );
                } else {
                    int nDupCount = 0;
                    while (CountInstancesOf( var, terms ) > 1) {
                        // Create a new name for the used argument
                        string sNewVar = GenerateUniqueName( var );
                        RenameFirstInstance( var, sNewVar, terms );
                        RemoveTerm( sNewVar, terms );
                        nDupCount++;
                    }

                    RemoveTerm( var, terms );

                    for (int j = 0; j < nDupCount; ++j)
                        terms.Insert( 0, new AstName( "dup" ) );
                }
            }
        }
Ejemplo n.º 2
0
        public AstLambda(PegAstNode node)
            : base(node)
        {
            CheckLabel(AstLabel.Lambda);
            CheckChildCount(node, 2);

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

            mIdentifiers.Add(name.ToString());
            CatAstNode tmp = Create(node.GetChild(1));

            // lambda nodes either contain quotes or other lambda nodes
            if (!(tmp is AstQuote))
            {
                if (!(tmp is AstLambda))
                {
                    throw new Exception("expected lambda expression or quotation");
                }
                AstLambda lambda = tmp as AstLambda;
                mIdentifiers.AddRange(lambda.mIdentifiers);

                // Take ownership of the terms from the child lambda expression
                mTerms = lambda.mTerms;
            }
            else
            {
                AstQuote q = tmp as AstQuote;

                // Take ownership of the terms from the quote
                mTerms = q.mTerms;
            }
        }
Ejemplo n.º 3
0
 public static bool TermContains(CatAstNode term, string var)
 {
     if (term is AstQuote)
     {
         AstQuote q = term as AstQuote;
         foreach (CatAstNode child in q.mTerms)
         {
             if (TermContainsOrEquals(child, var))
             {
                 return(true);
             }
         }
         return(false);
     }
     else if (term is AstLambda)
     {
         AstLambda l = term as AstLambda;
         foreach (CatAstNode child in l.mTerms)
         {
             if (TermContainsOrEquals(child, var))
             {
                 return(true);
             }
         }
         return(false);
     }
     else
     {
         return(false);
     }
 }
Ejemplo n.º 4
0
 public static void ConvertLocalCalls(List <CatAstNode> terms, List <AstDef> locals)
 {
     foreach (AstDef def in locals)
     {
         for (int i = terms.Count - 1; i >= 0; --i)
         {
             CatAstNode node = terms[i];
             if (node is AstLambda)
             {
                 ConvertLocalCalls((node as AstLambda).GetTerms(), locals);
             }
             else if (node is AstQuote)
             {
                 ConvertLocalCalls((node as AstQuote).GetTerms(), locals);
             }
             else if (node is AstName)
             {
                 if ((node as AstName).ToString().Equals(def.mName))
                 {
                     terms.Insert(i + 1, new AstName("apply"));
                 }
             }
         }
     }
 }
Ejemplo n.º 5
0
 public static bool TermContains(CatAstNode term, string var)
 {
     if (term is AstQuote)
     {
         AstQuote q = term as AstQuote;
         foreach (CatAstNode child in q.mTerms)
         {
             if (TermContainsOrEquals(child, var))
                 return true;
         }
         return false;
     }
     else if (term is AstLambda)
     {
         AstLambda l = term as AstLambda;
         foreach (CatAstNode child in l.mTerms)
         {
             if (TermContainsOrEquals(child, var))
                 return true;
         }
         return false;
     }
     else
     {
         return false;
     }
 }
Ejemplo n.º 6
0
 public AstProgram(Peg.PegAstNode node)
     : base(node)
 {
     foreach (Peg.PegAstNode child in node.GetChildren())
     {
         CatAstNode statement = CatAstNode.Create(child);
         mStatements.Add(statement);
     }
 }
Ejemplo n.º 7
0
 public AstMacroPattern(PegAstNode node)
     : base(node)
 {
     CheckLabel(AstLabel.MacroPattern);
     foreach (PegAstNode child in node.GetChildren())
     {
         AstMacroTerm tmp = CatAstNode.Create(child) as AstMacroTerm;
         if (tmp == null)
         {
             throw new Exception("invalid grammar: only macro terms can be children of an ast macro mPattern");
         }
         mPattern.Add(tmp);
     }
 }
Ejemplo n.º 8
0
 public AstStack(PegAstNode node)
     : base(node)
 {
     CheckLabel(AstLabel.Stack);
     foreach (PegAstNode child in node.GetChildren())
     {
         CatAstNode tmp = Create(child);
         if (!(tmp is AstType))
         {
             throw new Exception("stack AST node should only have type AST nodes as children");
         }
         mTypes.Add(tmp as AstType);
     }
 }
Ejemplo n.º 9
0
 public AstQuote(PegAstNode node)
     : base(node)
 {
     CheckLabel(AstLabel.Quote);
     foreach (PegAstNode child in node.GetChildren())
     {
         CatAstNode tmp = CatAstNode.Create(child);
         if (!(tmp is AstExpr))
         {
             throw new Exception("invalid child node " + child.ToString() + ", expected an expression node");
         }
         mTerms.Add(tmp as AstExpr);
     }
 }
Ejemplo n.º 10
0
        public CatExpr NodesToFxns(string name, List <CatAstNode> nodes)
        {
            CatExpr result = new CatExpr();

            for (int i = 0; i < nodes.Count; ++i)
            {
                CatAstNode node = nodes[i];
                if (node.GetLabel().Equals(AstLabel.Name))
                {
                    string s = node.ToString();
                    if (s.Equals(name))
                    {
                        result.Add(new SelfFunction(name));
                    }
                    else
                    {
                        result.Add(ThrowingLookup(s));
                    }
                }
                else if (node is AstLiteral)
                {
                    result.Add(LiteralToFunction(name, node as AstLiteral));
                }
                else if (node is AstDef)
                {
                    MakeFunction(node as AstDef);
                }
                else if (node is AstMacro)
                {
                    MetaCat.AddMacro(node as AstMacro);
                }
                else
                {
                    throw new Exception("unable to convert node to function: " + node.ToString());
                }
            }
            return(result);
        }
Ejemplo n.º 11
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++;
            }
        }
Ejemplo n.º 12
0
 public static bool TermEquals(CatAstNode term, string var)
 {
     return(term.ToString().Equals(var));
 }
Ejemplo n.º 13
0
 public static bool TermContainsOrEquals(CatAstNode term, string var)
 {
     return(TermEquals(term, var) || TermContains(term, var));
 }
Ejemplo n.º 14
0
 public static bool TermEquals(CatAstNode term, string var)
 {
     return term.ToString().Equals(var);
 }
Ejemplo n.º 15
0
 public static bool TermContainsOrEquals(CatAstNode term, string var)
 {
     return TermEquals(term, var) || TermContains(term, var);
 }