/// <summary> /// Gets a compiled module for an abstract module /// </summary> /// <param name="module"></param> /// <returns></returns> public Module GetModule(Grammar.Module module) { var tu = units.FirstOrDefault(u => u.Source == module); if (tu == null) { throw new ArgumentOutOfRangeException(nameof(module)); } if (tu.IsComplete == false) { throw new InvalidOperationException("Cannot get an incomplete module"); } return(tu.Module); }
public TranslationUnit(Grammar.Module input, Module output) { this.Source = input ?? throw new ArgumentNullException(nameof(input)); this.Module = output ?? throw new ArgumentNullException(nameof(output)); }
private Module AddModule(Grammar.Module module, TranslationUnit parent) { if (module == null) { throw new ArgumentNullException(nameof(module)); } if (units.Any(u => u.Source == module)) { throw new InvalidOperationException("Cannot add the same module twice!"); } var name = (parent != null ? parent.Module.Name + "." : "") + module.Name; Module target = null; { var parts = name.Split('.'); for (int i = 0; i < parts.Length; i++) { var mname = string.Join(".", parts, 0, i + 1); if (modules.ContainsKey(mname)) { target = modules[mname]; } else { var child = new Module(target, mname); modules.Add(mname, child); if (target != null) { target.Symbols.Add(new Symbol(Type.ModuleType, child.LocalName) { Initializer = new ModuleLiteral(child), IsConst = true, IsExported = true }); } target = child; } } } if (target?.Name != name) { throw new InvalidOperationException("An error happened in module name resolvation!"); } var tu = new TranslationUnit(module, target); // Initialize the base scope of the translation unit if (parent == null) { tu.Scope.Push(this.GlobalScope); } else { tu.Scope.Push(parent.Scope); } tu.Scope.Push(tu.Imports); tu.Scope.Push(tu.Module.Symbols); this.units.Add(tu); // Now create each lexical sub-module (not logical submodule!) foreach (var sm in module.Submodules) { this.AddModule(sm, tu); } return(target); }
public void AddModule(Grammar.Module module) => this.AddModule(module, null);