static public QuotedFunction ApplyMacros(INameLookup names, QuotedFunction f) { CatExpr list = new CatExpr(f.GetSubFxns().ToArray()); MetaCat.ApplyMacros(names, list); return(new QuotedFunction(list)); }
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()); } }
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); }
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); }