Пример #1
0
        public Function LiteralToFunction(string name, AstLiteral literal)
        {
            switch (literal.GetLabel())
            {
            case AstLabel.Int: {
                AstInt tmp = literal as AstInt;
                return(new PushInt(tmp.GetValue()));
            }

            case AstLabel.Bin: {
                AstBin tmp = literal as AstBin;
                return(new PushInt(tmp.GetValue()));
            }

            case AstLabel.Char: {
                AstChar tmp = literal as AstChar;
                return(new PushValue <char>(tmp.GetValue()));
            }

            case AstLabel.String: {
                AstString tmp = literal as AstString;
                return(new PushValue <string>(tmp.GetValue()));
            }

            case AstLabel.Float: {
                AstFloat tmp = literal as AstFloat;
                return(new PushValue <double>(tmp.GetValue()));
            }

            case AstLabel.Hex: {
                AstHex tmp = literal as AstHex;
                return(new PushInt(tmp.GetValue()));
            }

            case AstLabel.Quote: {
                AstQuote tmp  = literal as AstQuote;
                CatExpr  fxns = NodesToFxns(name, tmp.GetTerms());
                if (Config.gbOptimizeQuotations)
                {
                    MetaCat.ApplyMacros(this, fxns);
                }
                return(new PushFunction(fxns));
            }

            case AstLabel.Lambda: {
                AstLambda tmp = literal as AstLambda;
                CatLambdaConverter.Convert(tmp);
                CatExpr fxns = NodesToFxns(name, tmp.GetTerms());
                if (Config.gbOptimizeLambdas)
                {
                    MetaCat.ApplyMacros(this, fxns);
                }
                return(new PushFunction(fxns));
            }

            default:
                throw new Exception("unhandled literal " + literal.ToString());
            }
        }
Пример #2
0
        public Function MakeFunction(AstDef def)
        {
            bool bLambda = def.mParams.Count > 0 || def.mLocals.Count > 0;

            if (bLambda)
            {
                CatLambdaConverter.Convert(def);
            }
            CatExpr  fxns = NodesToFxns(def.mName, def.mTerms);
            Function ret  = new DefinedFunction(def.mName, fxns);

            if (def.mpMetaData != null)
            {
                ret.SetMetaData(new CatMetaDataBlock(def.mpMetaData));
            }
            if (bLambda && Config.gbOptimizeLambdas)
            {
                MetaCat.ApplyMacros(this, fxns);
            }

            AddFunction(ret);
            return(ret);
        }