/// <summary> /// Generates C# code from a Fast program /// </summary> /// <param name="inputPath">the Fast file from which it generates the CSharp code</param> /// <param name="outputPath">the C# file into which the CSharp code is generated</param> /// <param name="ns">the package the generated file will belong to</param> /// <param name="flag">true to generate the transducer's optimized version</param> public static bool GenerateCSharp(String inputPath, String outputPath, String ns, bool flag) { var pgm = Parser.ParseFromFile(inputPath); FastTransducerInstance fti = FastTransducerInstance.MkFastTransducerInstance(pgm); StringBuilder sb = new StringBuilder(); fti.ToFast(sb); pgm = Parser.ParseFromString(sb.ToString()); return(CsharpGenerator.GenerateCode(pgm, outputPath, ns)); }
static void Main(string[] args) { var input = args[0]; var output = args[1]; Console.WriteLine("CS: {0} -> {1}", input, output); var prog = Parser.ParseFromFile(input); CsharpGenerator.GenerateCode(prog, output); }
public static void Main(string[] args) { var iViewModel = new Interface("IViewModel") .WithInterface <INotifyPropertyChanged>() .WithAttribute(new Type(new Module("Test"), "Example")) .WithAttribute(new Type(new Module("Other"), "Sample"), "arg1", "arg2") .WithProperty <string>("Test", x => x.WithDocumentation("A test property.")) .WithProperty <string>("Test2", x => x.WithDocumentation("A test property.")) .WithEvent("Updated", initializer: x => x.WithDocumentation("When updated")) .WithEvent("Updated2") .WithMethod <Task>("UpdateAsync", x => { return(x.WithParameter <DateTime>("date", p => p.WithDocumentation("The last updated date") .WithAttribute(new Type(new Module("Serializers"), "Body"))) .WithParameter <CancellationToken>("token") .WithAttribute(new Type(new Module("Test"), "Example")) .WithDocumentation("Updates the current state of the view model.")); }); var sampleViewModel = new Class("SampleViewModel") .WithConstructor(x => x.WithBaseInitializers("test", "0").WithParameter <string>("test").WithBody(new Statement("this.test = test;"))) .WithInterface(iViewModel) .WithField <bool>("test2") .WithField <string>("test") .WithEvent <PropertyChangedEventHandler>("PropertyChanged") .WithEvent("Updated", initializer: x => x.WithDocumentation("When updated")) .WithAutoProperty <int>("AutoProperty") .WithProperty <string>("Test", x => { return(x.WithFieldGetter("test") .WithSetter(new Block(new Statement("this.test = value;"))) .WithDocumentation("A test property.")); }) .WithAsyncMethod <Task>("UpdateAsync", x => { return(x.WithParameter <DateTime>("date") .WithParameter <CancellationToken>("token") .WithDocumentation("Updates the current state of the view model.") .WithBody(new Block(new Statement("await Task.Delay(3);"), new Statement("this.Updated?.Invoke(this, System.EventArgs.Empty);")))); }); var module = new Module("CodeBuilder.Sample").WithTypes(iViewModel, sampleViewModel) .WithImport(Modules.ThreadingTasks) .WithImport("System.ComponentModel"); var generator = new CsharpGenerator(); Console.WriteLine(generator.Generate(module)); }
public override bool Execute() { this.Log.LogMessage("Source: {0}", string.Join(",", Source.Select(x => x.ItemSpec))); this.Log.LogMessage("AssemblyName: {0}", AssemblyName); this.Log.LogMessage("OutputFile {0}", string.Join(",", OutputFile)); try { for (int i = 0; i < Source.Length; i++) { var source = this.Source[i]; var output = this.OutputFile[i]; // Parse skml this.Log.LogMessage("Loading file: {0}", source.ItemSpec); var parser = new SkmlParser(); var xml = XDocument.Load(source.ItemSpec); // Parse referenced skss var layout = new Layout() { Path = source.ItemSpec, Content = xml, }; this.Log.LogMessage("Parsing file: {0}", source.ItemSpec); var flexTask = parser.ParseAsync(layout); flexTask.Wait(); var flex = flexTask.Result; // Generate C# this.Log.LogMessage("Generating C#: {0}, {1}", output, flex.Class); var generator = new CsharpGenerator(); if (File.Exists(output)) { File.Delete(output); } var folder = Path.GetDirectoryName(output); if (!Directory.Exists(folder)) { this.Log.LogMessage("Creating directory: {0}", folder); Directory.CreateDirectory(folder); } using (var stream = File.Create(output)) { generator.Generate(flex, stream); } } return(true); } catch (Exception e) { this.Log.LogMessage("Error: {0}", e.Message); this.Log.LogMessage("StackTrace: {0}", e.StackTrace); this.Log.LogError(null, null, null, Source, 0, 0, 0, 0, $"{e.Message}"); return(false); } }