public static void AnalyzeSyntax(Token root) { SyntaxAnalyzer analyzer = new SyntaxAnalyzer(); Token srcs = root.Find("Sources"); Token syntax = root.Find("Syntax"); List<Token> synflw = new List<Token>(); foreach (Token f in srcs.Follows) { if (f.Group == "SourcePath") { continue; } if (f.Group == "SourceText") { synflw.Add(analyzer.Run(f.Value, f.First.Value)); } } syntax.Follows = synflw.ToArray(); }
public static void Check(Token root) { if (root == null) { throw new ArgumentNullException("args"); } if (0 == root.Select("CompileOptions").Length) { throw new ArgumentException("No @CompileOptions Token"); } if (1 < root.Select("CompileOptions").Length) { throw new ArgumentException("Too many @CompileOptions Token"); } if (0 == root.Select("Sources").Length || 0 == root.Find("Sources").Follows.Length) { throw new ArgumentException("No source filename is specified to command line parameter"); } if (1 < root.Select("Sources").Length) { throw new ArgumentException("Too many @Sources Token"); } if (0 == root.Select("CompileOptions/@out").Length) { if (0 == root.Select("Sources").Length || 0 == root.Find("Sources").Follows.Length) { throw new ArgumentException("Cannot omit source path when out option was omitted"); } } if (1 < root.Select("CompileOptions/out").Length) { throw new ArgumentException("Too many out options are specified to command line parameter"); } if (1 == root.Select("Sources").Length) { foreach (Token p in root.Select("Sources/SourcePath")) { if (false == File.Exists(p.Value)) { if (false == File.Exists(p.Value + ".nana")) { throw new FileNotFoundException("Source file was not found", p.Value); } else { p.Value += ".nana"; } } } } }
public void Compile(Token root) { Prepare(root); Token srcs = root.Find("Sources"); // append SourceText if it's SourcePath ReadSourceFiles(root); AnalyzeSyntax(root); AfterSyntaxAnalyze(root); Env env = AnalyzeSemantic(root); AfterSemanticAnalyze(root, env); IMRGenerator imrgen = new IMRGenerator(); imrgen.GenerateIMR(env.Ap); Token code = root.Find("Code"); CodeGenerator codegen = new CodeGenerator(); code.Value = codegen.GenerateCode(env); }
public static void ReadSourceFiles(Token root) { Token srcs = root.Find("Sources"); UTF8Encoding utf8 = new UTF8Encoding(false /* no byte order mark */); List<Token> srcsflw = new List<Token>(); foreach (Token f in srcs.Follows) { srcsflw.Add(f); if (f.Group == "SourceText") { f.First = new Token(""); continue; } if (f.Group == "SourcePath") { string text = File.ReadAllText(f.Value, utf8); Token txtt = new Token(text, "SourceText"); txtt.First = f; srcsflw.Add(txtt); } } srcs.Follows = srcsflw.ToArray(); }