public bool Encloses(CiSymbol symbol) { for (CiScope scope = symbol.Parent; scope != null; scope = scope.Parent) { if (scope == this) { return(true); } } return(false); }
static CiProgram ParseAndResolve(CiParser parser, CiScope parent, List <string> files, List <string> searchDirs) { parser.Program = new CiProgram { Parent = parent }; foreach (string file in files) { parser.Parse(file, File.OpenText(file)); } new CiResolver(parser.Program, searchDirs); return(parser.Program); }
public virtual CiSymbol TryLookup(string name) { for (CiScope scope = this; scope != null; scope = scope.Parent) { object result = scope.Dict[name]; if (result != null) { return((CiSymbol)result); } } return(null); }
public void Add(CiSymbol symbol) { string name = symbol.Name; for (CiScope scope = this; scope != null; scope = scope.Parent) { if (scope.Dict[name] is CiSymbol duplicate && (scope == this || (!IsPrivate(duplicate as CiMember) && !IsOverrideOf(symbol as CiMethod, duplicate as CiMethod)))) { CiScope symbolScope = symbol as CiScope ?? this; CiScope duplicateScope = duplicate as CiScope ?? scope; throw new CiException(symbolScope.Filename, symbol.Line, string.Format("Duplicate symbol {0}, already defined in {1} line {2}", name, duplicateScope.Filename, duplicate.Line)); } } symbol.Parent = this; this.Dict.Add(name, symbol); }
public CiException(CiScope scope, string format, params object[] args) : this(scope, string.Format(format, args)) { }
public CiException(CiScope scope, string message) : this(scope.Container.Filename, scope.Line, message) { }
public static int Main(string[] args) { CiParser parser = new CiParser(); List <string> inputFiles = new List <string>(); List <string> referencedFiles = new List <string>(); List <string> searchDirs = new List <string>(); string lang = null; string outputFile = null; string namespace_ = null; for (int i = 0; i < args.Length; i++) { string arg = args[i]; if (arg[0] == '-') { switch (arg) { case "--help": Usage(); return(0); case "--version": Console.WriteLine("cito 1.0.0"); return(0); case "-l": lang = args[++i]; break; case "-o": outputFile = args[++i]; break; case "-n": namespace_ = args[++i]; break; case "-D": string symbol = args[++i]; if (symbol == "true" || symbol == "false") { throw new ArgumentException(symbol + " is reserved"); } parser.PreSymbols.Add(symbol); break; case "-r": referencedFiles.Add(args[++i]); break; case "-I": searchDirs.Add(args[++i]); break; default: throw new ArgumentException("Unknown option: " + arg); } } else { inputFiles.Add(arg); } } if (lang == null && outputFile != null) { string ext = Path.GetExtension(outputFile); if (ext.Length >= 2) { lang = ext.Substring(1); } } if (lang == null || outputFile == null || inputFiles.Count == 0) { Usage(); return(1); } GenBase gen; switch (lang) { case "c": gen = new GenC(); break; case "cpp": gen = new GenCpp(); break; case "cs": gen = new GenCs(); break; case "java": gen = new GenJava(); break; case "js": gen = new GenJs(); break; case "py": gen = new GenPy(); break; case "swift": gen = new GenSwift(); break; case "cl": gen = new GenCl(); break; default: throw new ArgumentException("Unknown language: " + lang); } gen.Namespace = namespace_; gen.OutputFile = outputFile; CiProgram program; try { CiScope parent = CiSystem.Value; if (referencedFiles.Count > 0) { parent = ParseAndResolve(parser, parent, referencedFiles, searchDirs); } program = ParseAndResolve(parser, parent, inputFiles, searchDirs); } catch (CiException ex) { Console.Error.WriteLine("{0}({1}): ERROR: {2}", ex.Filename, ex.Line, ex.Message); return(1); // throw; } gen.Write(program); return(0); }