Exemplo n.º 1
0
        public InteractiveCompiler(string filename)
        {
            PrologCodeProvider provider = new PrologCodeProvider();
            IPrologCompiler compiler = provider.CreateCompiler();
            PrologCompilerParameters parameters = new PrologCompilerParameters();
            PrologCompilerResults results = compiler.CompileAbstractCodeFromFile(parameters, "boot.pro");

            /* Run */
            AbstractMachineState runtime = new AbstractMachineState(new AMFactory());
            //runtime.Init(results.AbstractInstructions, results.ForeignMethods, results.Namespaces, results.AssemblyFiles);
            runtime.Initialize(results.AbstractInstructions);

            runtime.Transition();
        }
Exemplo n.º 2
0
        PrologCompilerResults IPrologCompiler.CompileAbstractCodeFromUnit(PrologCompilerParameters p, PrologCodeUnit unit)
        {
            PrologCompilerResults results = new PrologCompilerResults();
            results.AbstractInstructions = new ArrayList();
            GenerateCodeFromUnit(unit, results.AbstractInstructions);

            /* patch predicates */
            //PatchPredicates(results.AbstractInstructions, GetPredicateAddresses(results.AbstractInstructions));

            /* save foreign methods */
            //results.ForeignMethods = GetForeignMethods(unit.Methods);

            /* save namespaces */
            results.Namespaces = unit.Namespaces;

            /* save assembly files */
            results.AssemblyFiles = unit.AssemblyFiles;

            /* return results */
            return results;
        }
Exemplo n.º 3
0
        PrologCompilerResults IPrologCompiler.CompileAbstractCodeFromFile(PrologCompilerParameters p, string fileName)
        {
            PrologCompilerResults results = new PrologCompilerResults();
            PrologCodeParser parser = new PrologCodeParser();
            PrologCodeUnit unit = new PrologCodeUnit();
            try
            {
                StreamReader reader = new StreamReader(fileName);
                unit = parser.Parse(reader);

                /* Get errors after parsing */
                results.Errors = parser.Errors;
            }
            catch (FileNotFoundException)
            {
                results.Errors.Add(new PrologCompilerError("P0008", "Input file not found.", fileName, false, 0, 0));
                return results;
            }
            results.AbstractInstructions = new ArrayList();
            GenerateCodeFromUnit(unit, results.AbstractInstructions);

            /* patch predicates */
            //PatchPredicates(results.AbstractInstructions, GetPredicateAddresses(results.AbstractInstructions));

            /* Save foreign methods */
            //results.ForeignMethods = GetForeignMethods(unit.Methods);

            /* save namespaces */
            results.Namespaces = unit.Namespaces;

            /* save assembly files */
            results.AssemblyFiles = unit.AssemblyFiles;

            /* return results */
            return results;
        }
Exemplo n.º 4
0
 PrologCompilerResults IPrologCompiler.CompileAssemblyFromUnit(PrologCompilerParameters options, PrologCodeUnit e)
 {
     return(FromUnit(options, e));
 }
Exemplo n.º 5
0
        private Assembly GenerateExecutableAssembly(PrologCompilerParameters compilerParameters, ArrayList instructions)
        {
            CodeCompileUnit compileUnit = new CodeCompileUnit();

            // Declare namespace, default is Prolog.Assembly
            CodeNamespace plNamespace = new CodeNamespace("Prolog.Assembly");

            plNamespace.Imports.Add(new CodeNamespaceImport("System"));
            plNamespace.Imports.Add(new CodeNamespaceImport("System.Collections"));
            plNamespace.Imports.Add(new CodeNamespaceImport("Axiom.Runtime"));

            compileUnit.Namespaces.Add(plNamespace);

            // Declare class type
            CodeTypeDeclaration classType = new CodeTypeDeclaration("PrologApp");

            plNamespace.Types.Add(classType);
            classType.TypeAttributes = TypeAttributes.Public;

            // Declare private members
            CodeMemberField machineField = new CodeMemberField(new CodeTypeReference("AbstractMachineState"), "machine");

            CodeMemberField moreField = new CodeMemberField(new CodeTypeReference("System.Boolean"), "_more");

            classType.Members.Add(machineField);
            classType.Members.Add(moreField);

            // Generate constructor method
            CodeConstructor cons = new CodeConstructor();

            cons.Attributes = MemberAttributes.Public;
            cons.Statements.Add(new CodeSnippetExpression("Init()"));
            classType.Members.Add(cons);


            // Generate Init() method
            GenerateInitMethod(classType, instructions);

            // Generate main method
            CodeEntryPointMethod mainMethod = new CodeEntryPointMethod();

            mainMethod.Name       = "Main";
            mainMethod.Attributes = MemberAttributes.Static | MemberAttributes.Public;
            mainMethod.Statements.Add(new CodeSnippetStatement("PrologApp app = new PrologApp();"));
            mainMethod.Statements.Add(new CodeSnippetStatement("app.Run();"));
            classType.Members.Add(mainMethod);


            CodeMemberMethod runMethod = new CodeMemberMethod();

            runMethod.Name       = "Run";
            runMethod.Attributes = MemberAttributes.Public;
            runMethod.Statements.Add(new CodeSnippetStatement("machine.Call(\"main\");"));
            classType.Members.Add(runMethod);


            // Compile the file into a DLL
            CompilerParameters compparams = new CompilerParameters(new string[] { "mscorlib.dll", "Axiom.Runtime.dll" });

            compparams.GenerateInMemory    = false;
            compparams.GenerateExecutable  = true;
            compparams.OutputAssembly      = compilerParameters.OutputAssembly;
            compparams.TempFiles.KeepFiles = true;

            Microsoft.CSharp.CSharpCodeProvider csharp = new Microsoft.CSharp.CSharpCodeProvider();
            ICodeCompiler cscompiler = csharp.CreateCompiler();

            CompilerResults compresult = cscompiler.CompileAssemblyFromDom(compparams, compileUnit);

            if (compresult.Errors.Count > 0)
            {
                foreach (CompilerError err in compresult.Errors)
                {
                    Console.WriteLine(err);
                }
                return(null);
            }

            return(compresult.CompiledAssembly);
        }
Exemplo n.º 6
0
        private Assembly GenerateDllAssembly(PrologCompilerParameters compilerParameters, ArrayList instructions, PrologCodeUnit unit)
        {
            CodeCompileUnit compileUnit = new CodeCompileUnit();

            // Declare namespace, default is Prolog.Assembly
            CodeNamespace plNamespace = new CodeNamespace("Prolog.Assembly");

            plNamespace.Imports.Add(new CodeNamespaceImport("System"));
            plNamespace.Imports.Add(new CodeNamespaceImport("System.Collections"));
            plNamespace.Imports.Add(new CodeNamespaceImport("Axiom.Runtime"));

            compileUnit.Namespaces.Add(plNamespace);

            // Declare class type
            CodeTypeDeclaration classType = new CodeTypeDeclaration(unit.Class);

            plNamespace.Types.Add(classType);
            classType.TypeAttributes = TypeAttributes.Public;

            // Declare private members
            CodeMemberField machineField = new CodeMemberField(new CodeTypeReference("AbstractMachineState"), "machine");
            CodeMemberField moreField    = new CodeMemberField(new CodeTypeReference("System.Boolean"), "_more");

            classType.Members.Add(machineField);
            classType.Members.Add(moreField);

            // Generate constructor method
            CodeConstructor cons = new CodeConstructor();

            cons.Attributes = MemberAttributes.Public;
            cons.Statements.Add(new CodeSnippetExpression("Init()"));
            classType.Members.Add(cons);

            // Generate the 'More' property
            CodeMemberProperty moreProperty = new CodeMemberProperty();

            moreProperty.Attributes = MemberAttributes.Public;
            moreProperty.Name       = "More";
            moreProperty.HasGet     = true;
            moreProperty.HasSet     = false;
            moreProperty.Type       = new CodeTypeReference("System.Boolean");
            string getStmt1 = "if (machine.Program.CurrentInstruction() == null || machine.Program.CurrentInstruction().Name().Equals(\"stop\")) {  _more = false; } ";
            string getStmt2 = "return !(machine.Program.CurrentInstruction() == null || machine.Program.CurrentInstruction().Name().Equals(\"stop\"));";

            moreProperty.GetStatements.Add(new CodeSnippetStatement(getStmt1));
            moreProperty.GetStatements.Add(new CodeSnippetStatement(getStmt2));
            classType.Members.Add(moreProperty);

            // Generate Redo() method
            CodeMemberMethod redoMethod = new CodeMemberMethod();

            redoMethod.Name = "Redo";
            redoMethod.Statements.Add(new CodeSnippetStatement("machine.Backtrack();"));
            redoMethod.Statements.Add(new CodeSnippetStatement("_more = true;"));
            redoMethod.Attributes = MemberAttributes.Public;
            classType.Members.Add(redoMethod);

            // Generate Init() method
            GenerateInitMethod(classType, instructions);

            // Generate method signatures
            GenerateMethodSignatures(classType, instructions);

            // Compile the file into a DLL
            CompilerParameters compparams = new CompilerParameters(new string[] { "mscorlib.dll", "Axiom.Runtime.dll" });

            compparams.GenerateInMemory    = false;
            compparams.OutputAssembly      = compilerParameters.OutputAssembly;
            compparams.TempFiles.KeepFiles = true;


            Microsoft.CSharp.CSharpCodeProvider csharp = new Microsoft.CSharp.CSharpCodeProvider();
            ICodeCompiler cscompiler = csharp.CreateCompiler();

            CompilerResults compresult = cscompiler.CompileAssemblyFromDom(compparams, compileUnit);

            if (compresult.Errors.Count > 0)
            {
                foreach (CompilerError err in compresult.Errors)
                {
                    Console.WriteLine(err);
                }
                return(null);
            }

            return(compresult.CompiledAssembly);
        }
Exemplo n.º 7
0
        /* Run the Compiler Tool application */
        public void Run()
        {
            if(cmdOptions.NoArgs())
            {
                PrintLogo();
                this.compiler = new InteractiveCompiler();
                return;
            }
            if(cmdOptions.NoOptions())
            {
                PrintLogo();
                this.compiler = new InteractiveCompiler(cmdOptions["source"]);
                return;
            }
            this.compiler = new CLICompiler();
            /* Check command line options */
            if(cmdOptions["help"] != null)
            {
                Help();
                return;
            }
            if(cmdOptions["version"] != null)
            {
                PrintLogo();
                return;
            }
            if(cmdOptions["source"] == null)
            {
                Error("CT1008", null);
            }

            if (cmdOptions["static"] != null)
            {
                staticAssembly = true;
            }

            if(cmdOptions["listing"] != null)
            {
                /* set the listing generator */
                string outputFile = cmdOptions["source"] + ".txt";
                if(cmdOptions["output"] != null)
                {
                    outputFile = cmdOptions["output"];
                }
                //compiler.SetGenerator(new ListingGenerator(outputFile));
            }
            if(cmdOptions["target"] != null)
            {
                switch(cmdOptions["target"])
                {
                    case "exe":

                        string outputFile = cmdOptions["source"];
                        if(cmdOptions["output"] != null)
                        {
                            outputFile = cmdOptions["output"];
                        }
                        PrologCodeProvider provider = new PrologCodeProvider();
                        IPrologCompiler compiler = provider.CreateCompiler();
                        PrintLogo();
                        PrologCompilerParameters parameters = new PrologCompilerParameters();
                        parameters.GenerateExecutable = true;
                        parameters.OutputAssembly = outputFile.Replace(".pro", ".exe");
                        PrologCompilerResults cr = compiler.CompileAssemblyFromFile(parameters, outputFile);

                        if (cr.Errors.Count > 0)
                        {
                            foreach (PrologCompilerError e in cr.Errors)
                            {
                                Console.WriteLine(e);
                            }
                        }
                        else
                        {
                            Console.WriteLine("Successfully compiled " + outputFile);
                        }
                        // check if you need to compile into static EXE
                        if (staticAssembly)
                        {
                            ArrayList inputAssemblies = new ArrayList();
                            // Add Axiom.Runtime.dll
                            inputAssemblies.Add("Axiom.Runtime.dll");

                            // Add remaining library assemblies
                            foreach (string file in Directory.GetFiles("..\\library"))
                            {
                                inputAssemblies.Add(file);
                            }

                            StaticAssemblyCompiler assc = new StaticAssemblyCompiler(parameters.OutputAssembly, parameters.OutputAssembly, inputAssemblies);
                            assc.LinkExecutable();
                        }
                        return;
                    case "dll":
                        string output = cmdOptions["source"];
                        if(cmdOptions["output"] != null)
                        {
                            output = cmdOptions["output"];
                        }
                        //PrologAssemblyCompiler asmc = new PrologAssemblyCompiler();
                        PrologCodeProvider prov = new PrologCodeProvider();
                        IPrologCompiler comp = prov.CreateCompiler();
                        PrintLogo();
                        PrologCompilerParameters par = new PrologCompilerParameters();
                        par.GenerateExecutable = false;
                        par.OutputAssembly = output.Replace(".pro", ".dll");
                        PrologCompilerResults r = comp.CompileAssemblyFromFile(par, output);
                        if (r.Errors.Count > 0)
                        {
                            foreach (PrologCompilerError e in r.Errors)
                            {
                                Console.WriteLine(e);
                            }
                        }
                        else
                        {
                            Console.WriteLine("Successfully compiled " + output);
                        }
                        // check if you need to compile into static DLL
                        if (staticAssembly)
                        {
                            ArrayList inputAssemblies = new ArrayList();

                            // Add Axiom.Runtime.dll
                            inputAssemblies.Add("Axiom.Runtime.dll");

                            // Add remaining library assemblies
                            foreach (string file in Directory.GetFiles("..\\library"))
                            {
                                inputAssemblies.Add(file);
                            }

                            StaticAssemblyCompiler assc = new StaticAssemblyCompiler(par.OutputAssembly, par.OutputAssembly, inputAssemblies);
                            assc.LinkLibrary();
                        }
                        return;
                    case "xml":
                        string outputXml = cmdOptions["source"];
                        if (cmdOptions["output"] != null)
                        {
                            outputXml = cmdOptions["output"];
                        }
                        PrologCodeProvider providerXml = new PrologCodeProvider();
                        IPrologCompiler compilerXml = providerXml.CreateCompiler();
                        PrologCompilerParameters parametersXml = new PrologCompilerParameters();
                        PrologCompilerResults results = compilerXml.CompileAbstractCodeFromFile(parametersXml, outputXml);
                        if (results.Errors.Count > 0)
                        {
                            foreach (PrologCompilerError err in results.Errors)
                            {
                                Console.WriteLine(err);
                            }
                            return;
                        }

                        string plFilename = outputXml;
                        string xmlFilename = plFilename.Replace(".pro", ".xml");

                        GenerateXmlFile(xmlFilename, results.AssemblyFiles, results.AbstractInstructions);
                        return;
                    default:
                        Error("CT1011", null);
                        break;
                }
            }
            /* debug information */
            // deferred to a future release.

            /* treat warnings as errors */
            // deferred to a future release.

            /* silent compilation */
            // deferred to a future release.

            if(cmdOptions["nologo"] == null)
            {
                PrintLogo();
            }

            if(cliErrors.Count != 0)
            {
                Errors();
                return;
            }

            /* Compile */
            //this.compiler.Compile(File.OpenText(cmdOptions["source"]));
        }
Exemplo n.º 8
0
 PrologCompilerResults IPrologCompiler.CompileAssemblyFromFile(PrologCompilerParameters options, string fileName)
 {
     return FromFile(options, fileName);
 }
Exemplo n.º 9
0
        private Assembly GenerateExecutableAssembly(PrologCompilerParameters compilerParameters, ArrayList instructions)
        {
            CodeCompileUnit compileUnit = new CodeCompileUnit();

            // Declare namespace, default is Prolog.Assembly
            CodeNamespace plNamespace = new CodeNamespace("Prolog.Assembly");

            plNamespace.Imports.Add(new CodeNamespaceImport("System"));
            plNamespace.Imports.Add(new CodeNamespaceImport("System.Collections"));
            plNamespace.Imports.Add(new CodeNamespaceImport("Axiom.Runtime"));

            compileUnit.Namespaces.Add(plNamespace);

            // Declare class type
            CodeTypeDeclaration classType = new CodeTypeDeclaration("PrologApp");
            plNamespace.Types.Add(classType);
            classType.TypeAttributes = TypeAttributes.Public;

            // Declare private members
            CodeMemberField machineField = new CodeMemberField(new CodeTypeReference("AbstractMachineState"), "machine");

            CodeMemberField moreField = new CodeMemberField(new CodeTypeReference("System.Boolean"), "_more");

            classType.Members.Add(machineField);
            classType.Members.Add(moreField);

            // Generate constructor method
            CodeConstructor cons = new CodeConstructor();
            cons.Attributes = MemberAttributes.Public;
            cons.Statements.Add(new CodeSnippetExpression("Init()"));
            classType.Members.Add(cons);

            // Generate Init() method
            GenerateInitMethod(classType, instructions);

            // Generate main method
            CodeEntryPointMethod mainMethod = new CodeEntryPointMethod();
            mainMethod.Name = "Main";
            mainMethod.Attributes = MemberAttributes.Static | MemberAttributes.Public;
            mainMethod.Statements.Add(new CodeSnippetStatement("PrologApp app = new PrologApp();"));
            mainMethod.Statements.Add(new CodeSnippetStatement("app.Run();"));
            classType.Members.Add(mainMethod);

            CodeMemberMethod runMethod = new CodeMemberMethod();
            runMethod.Name = "Run";
            runMethod.Attributes = MemberAttributes.Public;
            runMethod.Statements.Add(new CodeSnippetStatement("machine.Call(\"main\");"));
            classType.Members.Add(runMethod);

            // Compile the file into a DLL
            CompilerParameters compparams = new CompilerParameters(new string[] { "mscorlib.dll", "Axiom.Runtime.dll" });

            compparams.GenerateInMemory = false;
            compparams.GenerateExecutable = true;
            compparams.OutputAssembly = compilerParameters.OutputAssembly;
            compparams.TempFiles.KeepFiles = true;

            Microsoft.CSharp.CSharpCodeProvider csharp = new Microsoft.CSharp.CSharpCodeProvider();
            ICodeCompiler cscompiler = csharp.CreateCompiler();

            CompilerResults compresult = cscompiler.CompileAssemblyFromDom(compparams, compileUnit);

            if (compresult.Errors.Count > 0)
            {
                foreach (CompilerError err in compresult.Errors)
                {
                    Console.WriteLine(err);
                }
                return null;
            }

            return compresult.CompiledAssembly;
        }
Exemplo n.º 10
0
        private Assembly GenerateDllAssembly(PrologCompilerParameters compilerParameters, ArrayList instructions, PrologCodeUnit unit)
        {
            CodeCompileUnit compileUnit = new CodeCompileUnit();

            // Declare namespace, default is Prolog.Assembly
            CodeNamespace plNamespace = new CodeNamespace("Prolog.Assembly");

            plNamespace.Imports.Add(new CodeNamespaceImport("System"));
            plNamespace.Imports.Add(new CodeNamespaceImport("System.Collections"));
            plNamespace.Imports.Add(new CodeNamespaceImport("Axiom.Runtime"));

            compileUnit.Namespaces.Add(plNamespace);

            // Declare class type
            CodeTypeDeclaration classType = new CodeTypeDeclaration(unit.Class);
            plNamespace.Types.Add(classType);
            classType.TypeAttributes = TypeAttributes.Public;

            // Declare private members
            CodeMemberField machineField = new CodeMemberField(new CodeTypeReference("AbstractMachineState"), "machine");
            CodeMemberField moreField = new CodeMemberField(new CodeTypeReference("System.Boolean"), "_more");

            classType.Members.Add(machineField);
            classType.Members.Add(moreField);

            // Generate constructor method
            CodeConstructor cons = new CodeConstructor();
            cons.Attributes = MemberAttributes.Public;
            cons.Statements.Add(new CodeSnippetExpression("Init()"));
            classType.Members.Add(cons);

            // Generate the 'More' property
            CodeMemberProperty moreProperty = new CodeMemberProperty();
            moreProperty.Attributes = MemberAttributes.Public;
            moreProperty.Name = "More";
            moreProperty.HasGet = true;
            moreProperty.HasSet = false;
            moreProperty.Type = new CodeTypeReference("System.Boolean");
            string getStmt1 = "if (machine.Program.CurrentInstruction() == null || machine.Program.CurrentInstruction().Name().Equals(\"stop\")) {  _more = false; } ";
            string getStmt2 = "return !(machine.Program.CurrentInstruction() == null || machine.Program.CurrentInstruction().Name().Equals(\"stop\"));";
            moreProperty.GetStatements.Add(new CodeSnippetStatement(getStmt1));
            moreProperty.GetStatements.Add(new CodeSnippetStatement(getStmt2));
            classType.Members.Add(moreProperty);

            // Generate Redo() method
            CodeMemberMethod redoMethod = new CodeMemberMethod();
            redoMethod.Name = "Redo";
            redoMethod.Statements.Add(new CodeSnippetStatement("machine.Backtrack();"));
            redoMethod.Statements.Add(new CodeSnippetStatement("_more = true;"));
            redoMethod.Attributes = MemberAttributes.Public;
            classType.Members.Add(redoMethod);

            // Generate Init() method
            GenerateInitMethod(classType, instructions);

            // Generate method signatures
            GenerateMethodSignatures(classType, instructions);

            // Compile the file into a DLL
            CompilerParameters compparams = new CompilerParameters(new string[] { "mscorlib.dll", "Axiom.Runtime.dll" });

            compparams.GenerateInMemory = false;
            compparams.OutputAssembly = compilerParameters.OutputAssembly;
            compparams.TempFiles.KeepFiles = true;

            Microsoft.CSharp.CSharpCodeProvider csharp = new Microsoft.CSharp.CSharpCodeProvider();
            ICodeCompiler cscompiler = csharp.CreateCompiler();

            CompilerResults compresult = cscompiler.CompileAssemblyFromDom(compparams, compileUnit);

            if (compresult.Errors.Count > 0)
            {
                foreach(CompilerError err in compresult.Errors)
                {
                    Console.WriteLine(err);
                }
                return null;
            }

            return compresult.CompiledAssembly;
        }
Exemplo n.º 11
0
        protected virtual PrologCompilerResults FromUnit(PrologCompilerParameters options, PrologCodeUnit e)
        {
            ArrayList instructions = new ArrayList();
            PrologCompilerResults results = new PrologCompilerResults();

            /* Generate abstract machine instructions */
            GenerateCodeFromUnit(e, instructions);

            /* Determine assembly name and type to generate */
            if (options.GenerateExecutable)
            {
                results.CompiledAssembly = GenerateExecutableAssembly(options, instructions);

            }
            else
            {
               results.CompiledAssembly = GenerateDllAssembly(options, instructions, e);
            }
            return results;
        }
Exemplo n.º 12
0
 protected virtual PrologCompilerResults FromFile(PrologCompilerParameters options, string fileName)
 {
     PrologCompilerResults results = new PrologCompilerResults();
     PrologCodeParser parser = new PrologCodeParser();
     PrologCodeUnit unit = new PrologCodeUnit();
     try
     {
         StreamReader reader = new StreamReader(fileName);
         unit = parser.Parse(reader);
     }
     catch (FileNotFoundException)
     {
         results.Errors.Add(new PrologCompilerError("P0008", "Input file not found.", fileName, false, 0, 0));
         return results;
     }
     return FromUnit(options, unit);
 }
Exemplo n.º 13
0
 PrologCompilerResults IPrologCompiler.CompileAssemblyFromUnit(PrologCompilerParameters options, PrologCodeUnit e)
 {
     return FromUnit(options, e);
 }