/// <summary> /// Computes the graphical circuit. /// </summary> public async Task Compute() { // Load the input file if (string.IsNullOrWhiteSpace(Filename)) { _logger?.Post(new DiagnosticMessage(SeverityLevel.Error, "SC001", "No filename specified")); return; } if (!File.Exists(Filename)) { _logger?.Post(new DiagnosticMessage(SeverityLevel.Error, "SC001", $"Could not find file")); return; } string simpleCircuitScript = File.ReadAllText(Filename); // Load the CSS file if any _cssScript = null; if (!string.IsNullOrWhiteSpace(CssFilename)) { if (!File.Exists(CssFilename)) { _logger?.Post(new DiagnosticMessage(SeverityLevel.Error, "SC001", $"Could not find file '{CssFilename}'")); return; } _cssScript = File.ReadAllText(CssFilename); } else { // Include a CSS file if there is one with the same name as the input filename string tryCssFilename = Path.GetFileNameWithoutExtension(Filename) + ".css"; if (File.Exists(tryCssFilename)) { _cssScript = File.ReadAllText(tryCssFilename); } } if (_cssScript != null) { // Add the default script unless there is a comment of the form "/* exclude default */" if (!Regex.IsMatch(_cssScript, @"^/\*\s*exclude\s+default\s*\*/$")) { _cssScript = GraphicalCircuit.DefaultStyle + Environment.NewLine + _cssScript; } } // Now we can start parsing the input file _circuit = await Task.Run(() => { var lexer = SimpleCircuitLexer.FromString(simpleCircuitScript, Path.GetFileName(Filename)); var context = new ParsingContext() { Diagnostics = _logger }; Parser.Parser.Parse(lexer, context); // Solve it already // context.Circuit.Solve(_logger); return(context.Circuit); }); }
static void Main(string[] args) { var script = @"// Horizontal chains T <r> Xia <r> NAND1 <r> Xoa <r> T T <r> Xib <r> NAND2 <r> Xob <r> T // The cross-coupled wires NAND1[b] <l d a -20 d> Xob Xoa <d a -160 d r> [b]NAND2 (X NAND1[o] <0> [o]NAND2) // Finally flip the bottom one - NAND2.Flipped = true "; var logger = new Logger(); var lexer = SimpleCircuitLexer.FromString(script); var context = new ParsingContext { Diagnostics = logger }; Parser.Parse(lexer, context); context.Circuit.Metadata.Add("script", script); // Draw the component if (context.Circuit.Count > 0 && logger.ErrorCount == 0) { var doc = context.Circuit.Render(logger); using var sw = new StringWriter(); using (var xml = XmlWriter.Create(sw, new XmlWriterSettings { OmitXmlDeclaration = true })) doc.WriteTo(xml); if (File.Exists("tmp.html")) { File.Delete("tmp.html"); } using (var fw = new StreamWriter(File.OpenWrite("tmp.html"))) { fw.WriteLine("<html>"); fw.WriteLine("<head>"); fw.WriteLine("</head>"); fw.WriteLine("<body>"); fw.WriteLine(sw.ToString()); fw.WriteLine("</body>"); fw.WriteLine("</html>"); } Process.Start(@"""C:\Program Files\Google\Chrome\Application\chrome.exe""", "\"" + Path.Combine(Directory.GetCurrentDirectory(), "tmp.html") + "\""); } }