// TODO The following methods are based on the ones from DafnyPipeline/DafnyMain.cs. // It could be convenient to adapt them in the main-repo so location info could be extracted. public bool TryParseIncludesOfModule(ModuleDecl module, BuiltIns builtIns, ErrorReporter errorReporter) { var errors = new Errors(errorReporter); // Issue #40: // A HashSet must not be used here since equals treats A included by B not equal to A included by C. // In contrast, the compareTo-Method treats them as the same. var resolvedIncludes = new SortedSet <Include>(); var dependencyMap = new DependencyMap(); dependencyMap.AddIncludes(resolvedIncludes); bool newIncludeParsed = true; while (newIncludeParsed) { newIncludeParsed = false; // Parser.Parse appears to modify the include list; thus, we create a copy to avoid concurrent modifications. var moduleIncludes = new List <Include>(((LiteralModuleDecl)module).ModuleDef.Includes); dependencyMap.AddIncludes(moduleIncludes); foreach (var include in moduleIncludes) { bool isNewInclude = resolvedIncludes.Add(include); if (isNewInclude) { newIncludeParsed = true; if (!TryParseInclude(include, module, builtIns, errorReporter, errors)) { return(false); } } } } return(true); }
private bool TryParseInclude(Include include, ModuleDecl module, BuiltIns builtIns, ErrorReporter errorReporter, Errors errors) { try { var dafnyFile = new DafnyFile(include.includedFilename); int errorCount = Parser.Parse( useStdin: false, dafnyFile.SourceFileName, include, module, builtIns, errors, verifyThisFile: false, compileThisFile: false ); if (errorCount != 0) { errorReporter.Error(MessageSource.Parser, include.tok, $"{errorCount} parse error(s) detected in {include.includedFilename}"); return(false); } } catch (IllegalDafnyFile e) { errorReporter.Error(MessageSource.Parser, include.tok, $"Include of file {include.includedFilename} failed."); _logger.LogDebug(e, "encountered include of illegal dafny file {}", include.includedFilename); return(false); } catch (IOException e) { errorReporter.Error(MessageSource.Parser, include.tok, $"Unable to open the include {include.includedFilename}."); _logger.LogDebug(e, "could not open file {}", include.includedFilename); return(false); } return(true); }
public static string ParseIncludes(ModuleDecl module, BuiltIns builtIns, IList <string> excludeFiles, Errors errs) { SortedSet <Include> includes = new SortedSet <Include>(new IncludeComparer()); DependencyMap dmap = new DependencyMap(); foreach (string fileName in excludeFiles) { includes.Add(new Include(null, null, fileName)); } dmap.AddIncludes(includes); bool newlyIncluded; do { newlyIncluded = false; List <Include> newFilesToInclude = new List <Include>(); dmap.AddIncludes(((LiteralModuleDecl)module).ModuleDef.Includes); foreach (Include include in ((LiteralModuleDecl)module).ModuleDef.Includes) { bool isNew = includes.Add(include); if (isNew) { newlyIncluded = true; newFilesToInclude.Add(include); } } foreach (Include include in newFilesToInclude) { DafnyFile file; try { file = new DafnyFile(include.includedFilename); } catch (IllegalDafnyFile) { return(String.Format("Include of file \"{0}\" failed.", include.includedFilename)); } string ret = ParseFile(file, include, module, builtIns, errs, false); if (ret != null) { return(ret); } } } while (newlyIncluded); if (DafnyOptions.O.PrintIncludesMode != DafnyOptions.IncludesModes.None) { dmap.PrintMap(); } return(null); // Success }
private static string ParseFile(string dafnyFileName, IToken tok, ModuleDecl module, BuiltIns builtIns, Errors errs, bool verifyThisFile = true) { string fn = CommandLineOptions.Clo.UseBaseNameForFileName ? Path.GetFileName(dafnyFileName) : dafnyFileName; try { int errorCount = Microsoft.Dafny.Parser.Parse(dafnyFileName, module, builtIns, errs, verifyThisFile); if (errorCount != 0) { return($"{errorCount} parse errors detected in {fn}"); } } catch (IOException e) { errs.SemErr(tok, "Unable to open included file"); return($"Error opening file \"{fn}\": {e.Message}"); } return(null); // Success }
private static string ParseFile(string dafnyFileName, Bpl.IToken tok, ModuleDecl module, BuiltIns builtIns, Errors errs, bool verifyThisFile = true) { var fn = DafnyOptions.Clo.UseBaseNameForFileName ? Path.GetFileName(dafnyFileName) : dafnyFileName; try { int errorCount = Dafny.Parser.Parse(dafnyFileName, module, builtIns, errs, verifyThisFile); if (errorCount != 0) { return(string.Format("{0} parse errors detected in {1}", errorCount, fn)); } } catch (IOException e) { errs.SemErr(tok, "Unable to open included file"); return(string.Format("Error opening file \"{0}\": {1}", fn, e.Message)); } return(null); // Success }
private static string ParseFile(DafnyFile dafnyFile, Include include, ModuleDecl module, BuiltIns builtIns, Errors errs, bool verifyThisFile = true, bool compileThisFile = true) { var fn = DafnyOptions.Clo.UseBaseNameForFileName ? Path.GetFileName(dafnyFile.FilePath) : dafnyFile.FilePath; try { int errorCount = Dafny.Parser.Parse(dafnyFile.UseStdin, dafnyFile.SourceFileName, include, module, builtIns, errs, verifyThisFile, compileThisFile); if (errorCount != 0) { return(string.Format("{0} parse errors detected in {1}", errorCount, fn)); } } catch (IOException e) { Bpl.IToken tok = include == null ? Bpl.Token.NoToken : include.tok; errs.SemErr(tok, "Unable to open included file"); return(string.Format("Error opening file \"{0}\": {1}", fn, e.Message)); } return(null); // Success }
private bool LookupLevel(IToken levelDescriptor, string kind, out LiteralModuleDecl m) { ModuleDecl md = null; m = null; if (!bindings.TryLookup(levelDescriptor, out md)) { AH.PrintError(prog, $"Could not find {kind} level {levelDescriptor} referred to in proof module {mProof.Name}"); return(false); } if (!(md is LiteralModuleDecl)) { AH.PrintError(prog, $"Low-level {levelDescriptor} referred to in proof module {mProof.Name} isn't a code level"); return(false); } m = (LiteralModuleDecl)md; return(true); }
public static string ParseIncludes(ModuleDecl module, BuiltIns builtIns, IList <string> excludeFiles, Errors errs) { SortedSet <Include> includes = new SortedSet <Include>(new IncludeComparer()); foreach (string fileName in excludeFiles) { includes.Add(new Include(null, fileName, Path.GetFullPath(fileName))); } bool newlyIncluded; do { newlyIncluded = false; var newFilesToInclude = new List <Include>(); foreach (var include in ((LiteralModuleDecl)module).ModuleDef.Includes) { bool isNew = includes.Add(include); if (!isNew) { continue; } newlyIncluded = true; newFilesToInclude.Add(include); } foreach (var include in newFilesToInclude) { string ret = ParseFile(include.filename, include.tok, module, builtIns, errs, false); if (ret != null) { return(ret); } } } while (newlyIncluded); return(null); // Success }
///<summary> /// Parses top-level things (modules, classes, datatypes, class members) /// and appends them in appropriate form to "module". /// Returns the number of parsing errors encountered. /// Note: first initialize the Scanner with the given Errors sink. ///</summary> public static int Parse(string/*!*/ s, string/*!*/ fullFilename, string/*!*/ filename, ModuleDecl module, BuiltIns builtIns, Errors/*!*/ errors, bool verifyThisFile=true) { Contract.Requires(s != null); Contract.Requires(filename != null); Contract.Requires(module != null); Contract.Requires(errors != null); byte[]/*!*/ buffer = cce.NonNull( UTF8Encoding.Default.GetBytes(s)); MemoryStream ms = new MemoryStream(buffer,false); Scanner scanner = new Scanner(ms, errors, fullFilename, filename); Parser parser = new Parser(scanner, errors, module, builtIns, verifyThisFile); parser.Parse(); return parser.errors.ErrorCount; }
///<summary> /// Parses top-level things (modules, classes, datatypes, class members) /// and appends them in appropriate form to "module". /// Returns the number of parsing errors encountered. /// Note: first initialize the Scanner. ///</summary> public static int Parse(string/*!*/ s, string/*!*/ fullFilename, string/*!*/ filename, ModuleDecl module, BuiltIns builtIns, ErrorReporter reporter, bool verifyThisFile=true) { Contract.Requires(s != null); Contract.Requires(filename != null); Contract.Requires(module != null); Errors errors = new Errors(reporter); return Parse(s, fullFilename, filename, module, builtIns, errors, verifyThisFile); }
/* throws System.IO.IOException */ ///<summary> /// Parses top-level things (modules, classes, datatypes, class members) from "filename" /// and appends them in appropriate form to "module". /// Returns the number of parsing errors encountered. /// Note: first initialize the Scanner. ///</summary> public static int Parse(string/*!*/ filename, ModuleDecl module, BuiltIns builtIns, Errors/*!*/ errors, bool verifyThisFile=true) { Contract.Requires(filename != null); Contract.Requires(module != null); string s; if (filename == "stdin.dfy") { s = Microsoft.Boogie.ParserHelper.Fill(System.Console.In, new List<string>()); return Parse(s, filename, filename, module, builtIns, errors, verifyThisFile); } else { using (System.IO.StreamReader reader = new System.IO.StreamReader(filename)) { s = Microsoft.Boogie.ParserHelper.Fill(reader, new List<string>()); return Parse(s, filename, DafnyOptions.Clo.UseBaseNameForFileName ? Path.GetFileName(filename) : filename, module, builtIns, errors, verifyThisFile); } } }
void SubModuleDecl(DeclModifierData dmod, ModuleDefinition parent, out ModuleDecl submodule) { Attributes attrs = null; IToken/*!*/ id; List<MemberDecl/*!*/> namedModuleDefaultClassMembers = new List<MemberDecl>();; List<IToken> idPath, idExports; IToken idRefined = null; ModuleDefinition module; submodule = null; // appease compiler bool isAbstract = dmod.IsAbstract; bool isExclusively = false; bool isProtected = dmod.IsProtected; bool opened = false; CheckDeclModifiers(dmod, "Modules", AllowedDeclModifiers.Abstract | AllowedDeclModifiers.Extern | AllowedDeclModifiers.Protected); if (la.kind == 70) { Get(); while (la.kind == 50) { Attribute(ref attrs); } NoUSIdent(out id); EncodeExternAsAttribute(dmod, ref attrs, id, /* needAxiom */ false); if (la.kind == 71) { Get(); ModuleName(out idRefined); isExclusively = false; } module = new ModuleDefinition(id, id.val, isAbstract, isProtected, false, isExclusively, idRefined, parent, attrs, false); module.IsToBeVerified = theVerifyThisFile; Expect(50); module.BodyStartTok = t; while (StartOf(1)) { TopDecl(module, namedModuleDefaultClassMembers, /* isTopLevel */ false, isAbstract); } Expect(51); module.BodyEndTok = t; module.TopLevelDecls.Add(new DefaultClassDecl(module, namedModuleDefaultClassMembers)); submodule = new LiteralModuleDecl(module, parent); } else if (la.kind == 72) { Get(); if (la.kind == 73) { Get(); opened = true; } ModuleName(out id); EncodeExternAsAttribute(dmod, ref attrs, id, /* needAxiom */ false); if (StartOf(3)) { idPath = new List<IToken>(); idExports = new List<IToken>(); if (la.kind == 28 || la.kind == 29) { QualifiedModuleExportSuffix(idPath, idExports); } if (idPath.Count > 0) SemErr(idPath[0], "Qualified imports must be given a name."); idPath.Insert(0, id); submodule = new AliasModuleDecl(idPath, id, parent, opened, idExports); } else if (la.kind == 74) { Get(); QualifiedModuleExport(out idPath, out idExports); submodule = new AliasModuleDecl(idPath, id, parent, opened, idExports); } else if (la.kind == 22) { Get(); QualifiedModuleExport(out idPath, out idExports); submodule = new ModuleFacadeDecl(idPath, id, parent, opened, idExports); } else SynErr(150); if (la.kind == 30) { while (!(la.kind == 0 || la.kind == 30)) {SynErr(151); Get();} Get(); errors.Deprecated(t, "the semi-colon that used to terminate a sub-module declaration has been deprecated; in the new syntax, just leave off the semi-colon"); } } else if (la.kind == 75) { IToken exportId; List<ExportSignature> exports = new List<ExportSignature>();; List<string> extends = new List<string>(); bool provideAll = false; bool revealAll = false; bool isDefault = false; ExportSignature exsig; Get(); exportId = t; if (StartOf(4)) { ModuleExport(out exportId); } while (la.kind == 76 || la.kind == 77 || la.kind == 78) { if (la.kind == 76) { Get(); if (la.kind == 1) { ModuleExportSignature(true, out exsig); exports.Add(exsig); while (la.kind == 23) { Get(); ModuleExportSignature(true, out exsig); exports.Add(exsig); } } else if (la.kind == 61) { Get(); provideAll = true; } else SynErr(152); } else if (la.kind == 77) { Get(); if (la.kind == 1) { ModuleExportSignature(false, out exsig); exports.Add(exsig); while (la.kind == 23) { Get(); ModuleExportSignature(false, out exsig); exports.Add(exsig); } } else if (la.kind == 61) { Get(); revealAll = true; } else SynErr(153); } else { Get(); ModuleExport(out id); extends.Add(id.val); while (la.kind == 23) { Get(); ModuleExport(out id); extends.Add(id.val); } } } if (exportId.val == "export" || exportId.val == parent.Name) { isDefault = true; } submodule = new ModuleExportDecl(exportId, parent, exports, extends, provideAll, revealAll, isDefault); } else SynErr(154); }
void SubModuleDecl(ModuleDefinition parent, out ModuleDecl submodule) { ClassDecl/*!*/ c; DatatypeDecl/*!*/ dt; TopLevelDecl td; IteratorDecl iter; Attributes attrs = null; IToken/*!*/ id; TraitDecl/*!*/ trait; List<MemberDecl/*!*/> namedModuleDefaultClassMembers = new List<MemberDecl>();; List<IToken> idRefined = null, idPath = null, idAssignment = null; ModuleDefinition module; ModuleDecl sm; submodule = null; // appease compiler bool isAbstract = false; bool isExclusively = false; bool opened = false; if (la.kind == 61 || la.kind == 62) { if (la.kind == 61) { Get(); isAbstract = true; } Expect(62); while (la.kind == 46) { Attribute(ref attrs); } NoUSIdent(out id); if (la.kind == 63 || la.kind == 64) { if (la.kind == 63) { Get(); Expect(64); QualifiedModuleName(out idRefined); isExclusively = true; } else { Get(); QualifiedModuleName(out idRefined); isExclusively = false; } } module = new ModuleDefinition(id, id.val, isAbstract, false, isExclusively, idRefined == null ? null : idRefined, parent, attrs, false, this); Expect(46); module.BodyStartTok = t; while (StartOf(1)) { switch (la.kind) { case 61: case 62: case 65: { SubModuleDecl(module, out sm); module.TopLevelDecls.Add(sm); break; } case 70: { ClassDecl(module, out c); module.TopLevelDecls.Add(c); break; } case 72: { TraitDecl(module, out trait); module.TopLevelDecls.Add(trait); break; } case 76: case 77: { DatatypeDecl(module, out dt); module.TopLevelDecls.Add(dt); break; } case 79: { NewtypeDecl(module, out td); module.TopLevelDecls.Add(td); break; } case 80: { OtherTypeDecl(module, out td); module.TopLevelDecls.Add(td); break; } case 81: { IteratorDecl(module, out iter); module.TopLevelDecls.Add(iter); break; } case 38: case 39: case 40: case 41: case 42: case 73: case 74: case 75: case 78: case 84: case 85: case 86: case 87: { ClassMemberDecl(namedModuleDefaultClassMembers, false, !DafnyOptions.O.AllowGlobals, DafnyOptions.O.IronDafny && isAbstract); break; } } } Expect(47); module.BodyEndTok = t; module.TopLevelDecls.Add(new DefaultClassDecl(module, namedModuleDefaultClassMembers)); submodule = new LiteralModuleDecl(module, parent); } else if (la.kind == 65) { Get(); if (la.kind == 66) { Get(); opened = true; } NoUSIdent(out id); if (la.kind == 67 || la.kind == 68) { if (la.kind == 67) { Get(); QualifiedModuleName(out idPath); submodule = new AliasModuleDecl(idPath, id, parent, opened); } else { Get(); QualifiedModuleName(out idPath); if (la.kind == 69) { Get(); QualifiedModuleName(out idAssignment); } submodule = new ModuleFacadeDecl(idPath, id, parent, idAssignment, opened); } } if (la.kind == 28) { while (!(la.kind == 0 || la.kind == 28)) {SynErr(139); Get();} Get(); errors.Warning(t, "the semi-colon that used to terminate a sub-module declaration has been deprecated; in the new syntax, just leave off the semi-colon"); } if (submodule == null) { idPath = new List<IToken>(); idPath.Add(id); submodule = new AliasModuleDecl(idPath, id, parent, opened); } } else SynErr(140); }
public bool CheckIsRefinement(ModuleDecl derived, ModuleFacadeDecl original) { // Check explicit refinement // TODO syntactic analysis of export sets is not quite right var derivedPointer = derived.Signature.ModuleDef; while (derivedPointer != null) { if (derivedPointer == original.OriginalSignature.ModuleDef) { HashSet<string> exports; if (derived is AliasModuleDecl) { exports = new HashSet<string>(((AliasModuleDecl)derived).Exports.ConvertAll(t => t.val)); } else if (derived is ModuleFacadeDecl) { exports = new HashSet<string>(((ModuleFacadeDecl)derived).Exports.ConvertAll(t => t.val)); } else { reporter.Error(MessageSource.RefinementTransformer, derived, "a module ({0}) can only be refined by an alias module or a module facade", original.Name); return false; } var oexports = new HashSet<string>(original.Exports.ConvertAll(t => t.val)); return oexports.IsSubsetOf(exports); } derivedPointer = derivedPointer.RefinementBase; } return false; }
void SubModuleDecl(DeclModifierData dmod, ModuleDefinition parent, out ModuleDecl submodule) { Attributes attrs = null; IToken/*!*/ id; List<MemberDecl/*!*/> namedModuleDefaultClassMembers = new List<MemberDecl>();; List<IToken> idRefined = null, idPath = null, idAssignment = null; ModuleDefinition module; submodule = null; // appease compiler bool isAbstract = dmod.IsAbstract; bool isExclusively = false; bool opened = false; CheckDeclModifiers(dmod, "Modules", AllowedDeclModifiers.Abstract | AllowedDeclModifiers.Extern); if (la.kind == 69) { Get(); while (la.kind == 46) { Attribute(ref attrs); } NoUSIdent(out id); EncodeExternAsAttribute(dmod, ref attrs, id, /* needAxiom */ false); if (la.kind == 70 || la.kind == 71) { if (la.kind == 70) { Get(); Expect(71); QualifiedModuleName(out idRefined); isExclusively = true; } else { Get(); QualifiedModuleName(out idRefined); isExclusively = false; } } module = new ModuleDefinition(id, id.val, isAbstract, false, isExclusively, idRefined == null ? null : idRefined, parent, attrs, false, this); Expect(46); module.BodyStartTok = t; while (StartOf(1)) { TopDecl(module, namedModuleDefaultClassMembers, /* isTopLevel */ false, isAbstract); } Expect(47); module.BodyEndTok = t; module.TopLevelDecls.Add(new DefaultClassDecl(module, namedModuleDefaultClassMembers)); submodule = new LiteralModuleDecl(module, parent); } else if (la.kind == 72) { Get(); if (la.kind == 73) { Get(); opened = true; } NoUSIdent(out id); EncodeExternAsAttribute(dmod, ref attrs, id, /* needAxiom */ false); if (StartOf(3)) { if (la.kind == 74) { Get(); QualifiedModuleName(out idPath); submodule = new AliasModuleDecl(idPath, id, parent, opened); } else if (la.kind == 75) { Get(); QualifiedModuleName(out idPath); if (IsDefaultImport()) { Expect(76); QualifiedModuleName(out idAssignment); } submodule = new ModuleFacadeDecl(idPath, id, parent, idAssignment, opened); errors.Warning(t, "\"import A as B\" has been deprecated; in the new syntax, it is \"import A:B\""); } else if (la.kind == 21) { Get(); QualifiedModuleName(out idPath); submodule = new ModuleFacadeDecl(idPath, id, parent, idAssignment, opened); } else { Get(); QualifiedModuleName(out idPath); idPath.Insert(0, id); submodule = new AliasModuleDecl(idPath, id, parent, opened); } } if (la.kind == 28) { while (!(la.kind == 0 || la.kind == 28)) {SynErr(153); Get();} Get(); errors.Warning(t, "the semi-colon that used to terminate a sub-module declaration has been deprecated; in the new syntax, just leave off the semi-colon"); } if (submodule == null) { idPath = new List<IToken>(); idPath.Add(id); submodule = new AliasModuleDecl(idPath, id, parent, opened); } } else if (la.kind == 76 || la.kind == 77) { bool isDefault = false; bool includeBody; IToken exportId; List<ExportSignature> exports = new List<ExportSignature>();; List<string> extends = new List<string>(); if (la.kind == 76) { Get(); isDefault = true; } Expect(77); NoUSIdent(out exportId); if (la.kind == 78) { Get(); NoUSIdent(out id); extends.Add(id.val); while (la.kind == 22) { Get(); NoUSIdent(out id); extends.Add(id.val); } } Expect(46); NoUSIdent(out id); includeBody = false; if (la.kind == 79) { Get(); includeBody = true; } exports.Add(new ExportSignature(id, includeBody)); while (la.kind == 22) { Get(); NoUSIdent(out id); includeBody = false; if (la.kind == 79) { Get(); includeBody = true; } exports.Add(new ExportSignature(id, includeBody)); } Expect(47); submodule = new ModuleExportDecl(exportId, parent, isDefault, exports, extends); } else SynErr(154); }
public IEnumerable <String> Module(ModuleDecl m) { foreach (var _Line in Combine(Combine(Begin(), "class "), GetEscapedIdentifier(m.Name))) { yield return(_Line); } yield return("{"); yield return("private:"); foreach (var f in m.Functions) { foreach (var _Line in Combine(Combine(Combine(Combine(Combine(Begin(), "std::function<"), f.ReturnValue.ToString()), "(Niveum::Expression::ExpressionParameterContext &)> "), GetEscapedIdentifier(Combine(Combine(Begin(), "Func_"), f.Name))), ";")) { yield return(_Line == "" ? "" : " " + _Line); } } yield return(""); yield return("public:"); foreach (var _Line in Combine(Combine(Combine(Begin(), " "), GetEscapedIdentifier(m.Name)), "(std::shared_ptr<Niveum::ExpressionSchema::ModuleDef> md)")) { yield return(_Line); } yield return(" {"); yield return(" std::unordered_map<std::u16string, std::shared_ptr<Niveum::ExpressionSchema::Expr>> fd;"); yield return(" for (auto _f_ : md->Functions)"); yield return(" {"); yield return(" fd[_f_->Name] = _f_->Body;"); yield return(" }"); foreach (var f in m.Functions) { yield return(" " + "{"); foreach (var _Line in Combine(Combine(Combine(Begin(), " auto Body = fd["), GetEscapedStringLiteral(f.Name)), "];")) { yield return(_Line == "" ? "" : " " + _Line); } yield return(" " + ""); yield return(" " + " Niveum::Expression::ExpressionParameterTypeProvider eptp;"); foreach (var p in f.Parameters) { foreach (var _Line in Combine(Combine(Combine(Combine(Combine(Begin(), "eptp.Parameters["), GetEscapedStringLiteral(p.Name)), "] = Niveum::ExpressionSchema::PrimitiveType::"), p.Type.ToString()), ";")) { yield return(_Line == "" ? "" : " " + _Line); } } yield return(" " + " Niveum::Expression::ExpressionCalculator ec;"); foreach (var _Line in Combine(Combine(Combine(Combine(Combine(Begin(), " "), GetEscapedIdentifier(Combine(Combine(Begin(), "Func_"), f.Name))), " = ec.BuildExpression<"), f.ReturnValue.ToString()), ">(eptp, Body);")) { yield return(_Line == "" ? "" : " " + _Line); } yield return(" " + "}"); } yield return(" }"); yield return(""); foreach (var f in m.Functions) { var ParameterList = String.Join(", ", f.Parameters.Select(p => p.Type.ToString() + " " + GetEscapedIdentifier(p.Name))); foreach (var _Line in Combine(Combine(Combine(Combine(Combine(Combine(Begin(), f.ReturnValue.ToString()), " "), GetEscapedIdentifier(f.Name)), "("), ParameterList), ")")) { yield return(_Line == "" ? "" : " " + _Line); } yield return(" " + "{"); yield return(" " + " Niveum::Expression::ExpressionParameterContext epc;"); foreach (var p in f.Parameters) { foreach (var _Line in Combine(Combine(Combine(Combine(Combine(Begin(), "epc.Parameters["), GetEscapedStringLiteral(p.Name)), "] = "), GetEscapedIdentifier(p.Name)), ";")) { yield return(_Line == "" ? "" : " " + _Line); } } foreach (var _Line in Combine(Combine(Combine(Begin(), " return "), GetEscapedIdentifier(Combine(Combine(Begin(), "Func_"), f.Name))), "(epc);")) { yield return(_Line == "" ? "" : " " + _Line); } yield return(" " + "}"); } yield return("};"); }
// the real work public Parser(Scanner/*!*/ scanner, Errors/*!*/ errors, ModuleDecl module, BuiltIns builtIns, bool verifyThisFile=true) : this(scanner, errors) { // initialize readonly fields dummyExpr = new LiteralExpr(Token.NoToken); dummyRhs = new ExprRhs(dummyExpr, null); dummyFrameExpr = new FrameExpression(dummyExpr.tok, dummyExpr, null); dummyStmt = new ReturnStmt(Token.NoToken, Token.NoToken, null); theModule = module; theBuiltIns = builtIns; theVerifyThisFile = verifyThisFile; }
public IEnumerable <String> Module(ModuleDecl m) { foreach (var _Line in Combine(Combine(Begin(), "public class "), GetEscapedIdentifier(m.Name))) { yield return(_Line); } yield return("{"); foreach (var f in m.Functions) { foreach (var _Line in Combine(Combine(Begin(), "private class "), GetEscapedIdentifier(Combine(Combine(Begin(), "FuncContext_"), f.Name)))) { yield return(_Line == "" ? "" : " " + _Line); } yield return(" " + "{"); foreach (var p in f.Parameters) { foreach (var _Line in Combine(Combine(Combine(Combine(Combine(Begin(), "public "), p.Type.ToString()), " "), GetEscapedIdentifier(p.Name)), ";")) { yield return(_Line == "" ? "" : " " + _Line); } } yield return(" " + "}"); foreach (var _Line in Combine(Combine(Combine(Combine(Combine(Combine(Combine(Begin(), "private Func<"), GetEscapedIdentifier(Combine(Combine(Begin(), "FuncContext_"), f.Name))), ", "), f.ReturnValue.ToString()), "> "), GetEscapedIdentifier(Combine(Combine(Begin(), "Func_"), f.Name))), ";")) { yield return(_Line == "" ? "" : " " + _Line); } } yield return(""); foreach (var _Line in Combine(Combine(Combine(Begin(), " public "), GetEscapedIdentifier(m.Name)), "(ModuleDef md)")) { yield return(_Line); } yield return(" {"); yield return(" var fd = md.Functions.ToDictionary(_f_ => _f_.Name);"); foreach (var f in m.Functions) { yield return(" " + "{"); foreach (var _Line in Combine(Combine(Combine(Begin(), " var vc = new VariableContext<"), GetEscapedIdentifier(Combine(Combine(Begin(), "FuncContext_"), f.Name))), ">();")) { yield return(_Line == "" ? "" : " " + _Line); } foreach (var p in f.Parameters) { foreach (var _Line in Combine(Combine(Combine(Combine(Combine(Combine(Combine(Combine(Combine(Combine(Combine(Combine(Combine(Begin(), "vc.Replace("), GetEscapedStringLiteral(p.Name)), ", null, PrimitiveType."), p.Type.ToString()), ", vvc => (Func<"), GetEscapedIdentifier(Combine(Combine(Begin(), "FuncContext_"), f.Name))), ", "), p.Type.ToString()), ">)("), GetEscapedIdentifier(Combine(Combine(Begin(), "Context_"), f.Name))), " => "), GetEscapedIdentifier(Combine(Combine(Combine(Combine(Begin(), "Context_"), f.Name), "."), p.Name))), "));")) { yield return(_Line == "" ? "" : " " + _Line); } } foreach (var _Line in Combine(Combine(Combine(Combine(Combine(Begin(), " var p = new VariableProviderCombiner<"), GetEscapedIdentifier(Combine(Combine(Begin(), "FuncContext_"), f.Name))), ">(vc, new ExpressionRuntimeProvider<"), GetEscapedIdentifier(Combine(Combine(Begin(), "FuncContext_"), f.Name))), ">());")) { yield return(_Line == "" ? "" : " " + _Line); } foreach (var _Line in Combine(Combine(Combine(Combine(Combine(Combine(Combine(Begin(), " var d = ExpressionEvaluator<"), GetEscapedIdentifier(Combine(Combine(Begin(), "FuncContext_"), f.Name))), ">.Compile<"), f.ReturnValue.ToString()), ">(p, fd["), GetEscapedStringLiteral(f.Name)), "].Body);")) { yield return(_Line == "" ? "" : " " + _Line); } foreach (var _Line in Combine(Combine(Combine(Begin(), " "), GetEscapedIdentifier(Combine(Combine(Begin(), "Func_"), f.Name))), " = t =>")) { yield return(_Line == "" ? "" : " " + _Line); } yield return(" " + " {"); yield return(" " + " return d(t);"); yield return(" " + " };"); yield return(" " + "}"); } yield return(" }"); yield return(""); foreach (var f in m.Functions) { var ParameterList = String.Join(", ", f.Parameters.Select(p => p.Type.ToString() + " " + GetEscapedIdentifier(p.Name))); foreach (var _Line in Combine(Combine(Combine(Combine(Combine(Combine(Combine(Begin(), "public "), f.ReturnValue.ToString()), " "), GetEscapedIdentifier(f.Name)), "("), ParameterList), ")")) { yield return(_Line == "" ? "" : " " + _Line); } yield return(" " + "{"); foreach (var _Line in Combine(Combine(Combine(Combine(Combine(Begin(), " var "), GetEscapedIdentifier(Combine(Combine(Begin(), "Context_"), f.Name))), " = new "), GetEscapedIdentifier(Combine(Combine(Begin(), "FuncContext_"), f.Name))), "();")) { yield return(_Line == "" ? "" : " " + _Line); } foreach (var p in f.Parameters) { foreach (var _Line in Combine(Combine(Combine(Combine(Combine(Combine(Begin(), GetEscapedIdentifier(Combine(Combine(Begin(), "Context_"), f.Name))), "."), GetEscapedIdentifier(p.Name)), " = "), GetEscapedIdentifier(p.Name)), ";")) { yield return(_Line == "" ? "" : " " + _Line); } } foreach (var _Line in Combine(Combine(Combine(Combine(Combine(Begin(), " return "), GetEscapedIdentifier(Combine(Combine(Begin(), "Func_"), f.Name))), "("), GetEscapedIdentifier(Combine(Combine(Begin(), "Context_"), f.Name))), ");")) { yield return(_Line == "" ? "" : " " + _Line); } yield return(" " + "}"); } yield return("}"); }