Exemple #1
0
 // translates the Deslang code into the equivalent C# code by visiting all AST root nodes
 // assumes that the ASTs have all been properly type checked prior to translating the code
 public string Generate(List <ProgramNode> ASTRoots)
 {
     _CSharpCode.AppendLine("using System;");
     _CSharpCode.AppendLine("using Deslang.CodeGeneration;"); // reference to the namespace containing stdlib
     _CSharpCode.AppendLine("namespace Deslang");             // build the C# code in the 'Deslang' namespace so the runtime can correctly identify it
     _CSharpCode.AppendLine("{");
     IncreaseIndent();
     foreach (ProgramNode AST in ASTRoots)
     {
         AST.Accept(this, null);
     }
     DecreaseIndent();
     _CSharpCode.AppendLine("}");
     return(_CSharpCode.ToString());
 }
Exemple #2
0
    public static void Main(string[] args)
    {
        string filename   = null;
        bool   printAST   = false;
        bool   printASTtc = false;

        foreach (string arg in args)
        {
            if (arg.StartsWith("-"))
            {
                switch (arg)
                {
                case "-ast":
                    printAST = true;  break;

                case "-tc":
                    printASTtc = true;  break;

                default:
                    Console.WriteLine("Unknown option {0}, ignored", arg);
                    break;
                }
            }
            else
            {
                if (filename != null)
                {
                    Usage();
                }
                filename = arg;
            }
        }
        // require exactly one input file to be provided
        if (filename == null)
        {
            Usage();
        }
        // require a filename suffix of .cb or .cs
        if (!filename.EndsWith(".cb") && !filename.EndsWith(".cs"))
        {
            Usage();
        }

        AST tree = DoParse(filename);

        if (tree == null)
        {
            Console.WriteLine("\n-- no tree constructed");
            return;
        }

        if (printAST)
        {
            PrVisitor printVisitor = new PrVisitor();
            tree.Accept(printVisitor);
        }

        int numErrors = 0;

        // perform typechecking ...
        TcVisitor typeCheckVisitor = new TcVisitor();

        tree.Accept(typeCheckVisitor);
        numErrors = typeCheckVisitor.NumErrors;

        if (printASTtc)
        {
            PrVisitor printVisitor = new PrVisitor();
            tree.Accept(printVisitor);    // print AST with datatype annotations
        }

        string asmFileName = filename.Substring(0, filename.Length - 2) + "s";

        BackEnd.GenCode cgen = new BackEnd.GenCode();
        cgen.GenProgram(tree);

        if (numErrors > 0)
        {
            Console.WriteLine("\n{0} errors reported, compilation halted", numErrors);
            return;
        }

        if (cgen.Asm != null)
        {
            cgen.Asm.WriteCode(asmFileName);
            Console.WriteLine("Assembler code written to {0}", asmFileName);
        }
    }
Exemple #3
0
    public static void Main(string[] args)
    {
        string filename   = null;
        bool   printAST   = false;
        bool   printASTtc = false;

        foreach (string arg in args)
        {
            if (arg.StartsWith("-"))
            {
                switch (arg)
                {
                case "-ast":
                    printAST = true;  break;

                case "-tc":
                    printASTtc = true;  break;

                default:
                    Console.WriteLine("Unknown option {0}, ignored", arg);
                    break;
                }
            }
            else
            {
                if (filename != null)
                {
                    Usage();
                }
                filename = arg;
            }
        }
        // require exactly one input file to be provided
        if (filename == null)
        {
            Usage();
        }
        // require a filename suffix of .cb or .cs
        if (!filename.EndsWith(".cb") && !filename.EndsWith(".cs"))
        {
            Usage();
        }

        AST tree = DoParse(filename);

        if (tree == null)
        {
            Console.WriteLine("\n-- no tree constructed");
            return;
        }

        if (printAST)
        {
            PrVisitor printVisitor = new PrVisitor();
            tree.Accept(printVisitor);
        }

/*
 *      int numErrors;
 *
 *      // perform typechecking ...
 *
 *      if (printASTtc) {
 *              PrVisitor printVisitor = new PrVisitor();
 *          tree.Accept(printVisitor);    // print AST with datatype annotations
 *      }
 *
 *              // generate intermediate code
 *
 *      if (numErrors > 0) {
 *          Console.WriteLine("\n{0} errors reported, compilation halted", numErrors);
 *          return;
 *      }
 */
    }