예제 #1
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();
            }
        }
예제 #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());
        }
예제 #3
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();
        }