public static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("wtf dude give some files: <output> <file.lah...>"); } var outname = args[0]; var sources = new string[] { "stdlib/mem.lah", "stdlib/collections.lah", "stdlib/math.lah" }.Concat(args.Skip(1)); foreach (var file in args.Skip(1)) { Console.WriteLine($"Compiling {file}"); } var multiSource = new MultiCodeSource(sources.Select(x => CodeSource.FromFile(x)).ToArray()); Console.WriteLine(multiSource.Content); var parser = new LahdaParser(new LahdaLexer(new CompilationConfiguration(multiSource))); var output = new StringBuilderOutput(); var codeGen = new CodeGenerator(output, parser.Root()); codeGen.Build(); if (File.Exists(outname)) { File.Delete(outname); } File.WriteAllText(outname, output.ToString()); }
public sealed override string ToString() { var strOutput = new StringBuilderOutput(); var printer = new ScriptPrinter(strOutput, new ScriptPrinterOptions() { Mode = ScriptMode.ScriptOnly }); printer.Write(this); var result = strOutput.ToString(); strOutput.Builder.Length = 0; return(result); }
public void CodeGenerator_should_generate_godlike_code(string content, int i) { var codeSource = CodeSource.FromMemory(content); var parser = new LahdaParser(new LahdaLexer(new CompilationConfiguration(codeSource))); var output = new StringBuilderOutput(); var codeGen = new CodeGenerator(output, parser.Root()); codeGen.Build(); var path = $"test_{i}.s"; if (File.Exists(path)) { File.Delete(path); } File.WriteAllText(path, output.ToString()); }
/// <summary> /// The evaluates a string as a scriban template or evaluate the passed function or return the passed value. /// </summary> /// <param name="context">The template context</param> /// <param name="span">The source span</param> /// <param name="value">The input value, either a scriban template in a string, or an alias function or directly a value.</param> /// <returns>The evaluation of the input value.</returns> /// <remarks> /// ```scriban-html /// {{ "This is a template text {{ 1 + 2 }}" | object.eval_template }} /// ``` /// ```html /// This is a template text 3 /// ``` /// </remarks> public static object EvalTemplate(TemplateContext context, SourceSpan span, object value) { if (value == null) { return(null); } if (value is string templateStr) { try { var template = Template.Parse(templateStr, lexerOptions: new LexerOptions() { Lang = context.Language, Mode = ScriptMode.Default }); var output = new StringBuilderOutput(); context.PushOutput(output); try { context.Evaluate(template.Page); } finally { context.PopOutput(); } return(output.ToString()); } catch (Exception ex) { throw new ArgumentException(ex.Message, nameof(value)); } } if (value is IScriptCustomFunction function) { return(ScriptFunctionCall.Call(context, context.CurrentNode, function, false, null)); } return(value); }