Ejemplo n.º 1
0
 private static void Emit(SourceFileBuilder source, IReadOnlyList <Statement> statements)
 {
     foreach (var statement in statements)
     {
         Emit(source, statement);
     }
 }
Ejemplo n.º 2
0
        public string Emit()
        {
            var source = new SourceFileBuilder();

            source.WriteIndentedLine("#pragma once");
            source.WriteLine();

            source.WriteIndentedLine("// Dependencies");
            source.WriteIndentedLine("#include <cstdint>");
            source.WriteIndentedLine($"#include \"{CppRuntime.FileName}\"");
            foreach (var dependency in package.Dependencies)
            {
                source.WriteIndentedLine($"#include \"{dependency.Package.Name}.cpp\"");
            }

            source.WriteLine();

            source.WriteIndentedLine("namespace");
            source.BeginBlock();
            Emit(source, package.GlobalNamespace.GetMembers());
            source.EndBlock();

            EmitEntryPoint(source);

            return(source.ToString());
        }
Ejemplo n.º 3
0
        private static void Emit(SourceFileBuilder source, IEnumerable <Declaration> declarations)
        {
            foreach (var declaration in declarations)
            {
                declaration.Match()
                .With <Namespace>(ns =>
                {
                    source.WriteIndentedLine($"namespace {ns.Name}");
                    source.BeginBlock();
                    Emit(source, ns.GetMembers());
                    source.EndBlock();
                })
                .With <Function>(function =>
                {
                    // TODO write correct return type
                    // TODO write correct parameter types

                    source.WriteIndentedLine($"{TypeOf(function.ReturnType)} {function.Name}()");
                    source.BeginBlock();
                    Emit(source, function.Body);
                    // TODO write body
                    source.EndBlock();
                })
                .Exhaustive();
            }
        }
Ejemplo n.º 4
0
        private void EmitEntryPoint(SourceFileBuilder source)
        {
            var entryPoint = package.EntryPoints.SingleOrDefault();

            if (entryPoint == null)
            {
                return;
            }

            source.WriteLine();
            source.WriteIndentedLine("int main(int argc, char *argv[])");
            source.BeginBlock();
            var entryPointName = CodeFor(entryPoint.QualifiedName());

            if (entryPoint.ReturnType.Type is VoidType)
            {
                source.WriteIndentedLine($"{entryPointName}();");
                source.WriteIndentedLine("return 0;");
            }
            else
            {
                source.WriteIndentedLine($"auto exitCodePtr = {entryPointName}();");
                source.WriteIndentedLine("auto exitCode = *exitCodePtr;");
                source.WriteIndentedLine("delete exitCodePtr;");
                source.WriteIndentedLine("return exitCode;");
            }
            source.EndBlock();
        }
Ejemplo n.º 5
0
 private static void Emit(SourceFileBuilder source, Statement statement)
 {
     statement.Match()
     .With <Return>(@return =>
     {
         var code = @return.Expression != null ? $"return {CodeFor(@return.Expression)};" : "return;";
         source.WriteIndentedLine(code);
     })
     .Exhaustive();
 }