Exemplo n.º 1
0
 static void Main(string[] args)
 {
     if (args.Length != 1)
         Console.WriteLine("usage: RACLCompiler programfile");
     else
     {
         using (FileStream fs = new FileStream(args[0], FileMode.Open))
         {
             string data = new StreamReader(fs).ReadToEnd();
             var compiler = new Compiler.Compiler();
             var compiledFunctions = compiler.Compile(data);
             foreach (var cfunc in compiledFunctions)
             {
                 var fileName = cfunc.Name + ".dnl";
                 if (File.Exists(fileName))
                     File.Delete(fileName);
                 using(var fs2 = new FileStream(fileName,FileMode.Create))
                 {
                     var writter = new StreamWriter(fs2);
                     writter.Write(cfunc.Body);
                     writter.Flush();
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
        public void SimpleExpression1_Test()
        {
            GrammarTree GT = LoadGrammar(TESTFILESPATH + @"simple expression1.tpg");
            Grammar G = (Grammar) GT.Eval();

            G.Directives["TinyPG"]["TemplatePath"] = TEMPLATEPATH;
            G.Directives["TinyPG"]["OutputPath"] = OUTPUTPATH;

            // basic checks
            string temp = G.PrintFirsts();
            Assert.IsTrue(!String.IsNullOrEmpty(temp));
            temp = G.GetOutputPath();
            Assert.IsTrue(!String.IsNullOrEmpty(temp));
            temp = G.PrintGrammar();
            Assert.IsTrue(!String.IsNullOrEmpty(temp));

            Compiler.Compiler compiler = new Compiler.Compiler();

            compiler.Compile(G);

            Assert.IsTrue(compiler.Errors.Count == 0, "compilation contains errors");

            CompilerResult result = compiler.Run("5+7/3*2+(4*2)");

            Assert.IsTrue(result.Output.StartsWith("Parse was successful."));
        }
        internal Tuple<string, ICompilation, IMetadataImporter, MockErrorReporter> Compile(string source, bool includeLinq = false, bool expectErrors = false)
        {
            var sourceFile = new MockSourceFile("file.cs", source);
            var md = new MetadataImporter.ScriptSharpMetadataImporter(false);
            var n = new DefaultNamer();
            var er = new MockErrorReporter(!expectErrors);
            PreparedCompilation compilation = null;
            var rtl = new ScriptSharpRuntimeLibrary(md, er, n.GetTypeParameterName, tr => { var t = tr.Resolve(compilation.Compilation).GetDefinition(); return new JsTypeReferenceExpression(t.ParentAssembly, md.GetTypeSemantics(t).Name); });
            var compiler = new Compiler.Compiler(md, n, rtl, er);

            var references = includeLinq ? new[] { Common.Mscorlib, Common.Linq } : new[] { Common.Mscorlib };
            compilation = compiler.CreateCompilation(new[] { sourceFile }, references, null);
            var compiledTypes = compiler.Compile(compilation);

            if (expectErrors) {
                Assert.That(er.AllMessages, Is.Not.Empty, "Compile should have generated errors");
                return Tuple.Create((string)null, compilation.Compilation, (IMetadataImporter)md, er);
            }

            er.AllMessagesText.Should().BeEmpty("Compile should not generate errors");

            var js = new OOPEmulator.ScriptSharpOOPEmulator(md, rtl, er).Rewrite(compiledTypes, compilation.Compilation);
            js = new GlobalNamespaceReferenceImporter().ImportReferences(js);

            string script = string.Join("", js.Select(s => OutputFormatter.Format(s, allowIntermediates: false)));

            if (Output == OutputType.GeneratedScript)
                Console.WriteLine(script);
            return Tuple.Create(script, compilation.Compilation, (IMetadataImporter)md, er);
        }
Exemplo n.º 4
0
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            compiler = new Compiler.Compiler(new PsiNodeParser());

            compiler.Compile(psimulex.Text, "s", false, ProgramPart.Statement);

            ProgramString();
        }
Exemplo n.º 5
0
 // Convenience function that takes the code field and compiles to the program field
 public void compile()
 {
     MemoryStream output = new MemoryStream();
     Compiler.Compiler cc = new Compiler.Compiler(
         new StringReader(code), output);
     cc.compile();
     program = output.ToArray();
 }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            FileReader f = new FileReader("thescript");
            Tokenizr.Tokenizr t = new Tokenizr.Tokenizr(f.GetSplittedLines());

            Console.WriteLine("\n===============Compiler=================== ");
            Compiler.Compiler c = new Compiler.Compiler(t.Tokens);

            Console.WriteLine("\n===============VirtualMachine============= ");
            VirtualMachine.VirtualMachine vm = new VirtualMachine.VirtualMachine();

            vm.Execute(c.Actions);
            //vm.Print();
            Console.ReadLine();
        }
Exemplo n.º 7
0
 private void button1_Click(object sender, EventArgs e)
 {
     try
     {
         var compiler = new Compiler.Compiler();
         var compiledFunctions = compiler.Compile(richTextBox1.Text);
         listBox1.Items.Clear();
         listBox1.Items.AddRange(compiledFunctions);
     }
     catch (Exception es)
     {
         MessageBox.Show("Compilation Failed\n" + es.Message);
         listBox1.Items.Clear();
         richTextBox2.Text = "";
     }
 }
Exemplo n.º 8
0
 protected void Page_Load(object sender, EventArgs e)
 {
     var c_code = Request.Params["CCodeTB"];
     if (c_code != null)
     {
         CCodeTB.Text = c_code;
         try
         {
             var compiler = new Compiler.Compiler();
             var code_list = compiler.Compile(c_code);
             var cd = String.Concat(from x in code_list select String.Format("// program: {0}\n\n{1}\n//end program: {0}\n\n\n", x.Name, x.Body));
             ACLCodeTB.Text = cd;
         }
         catch (Exception es)
         {
             ACLCodeTB.Text = String.Format("Compilation Error: {0}", es.Message);
         }
     }
 }
            public bool Compile(CompilerOptions options, ErrorReporterWrapper er)
            {
                string intermediateAssemblyFile = Path.GetTempFileName(), intermediateDocFile = Path.GetTempFileName();
                try {
                    // Compile the assembly
                    var settings = MapSettings(options, intermediateAssemblyFile, intermediateDocFile, er);
                    if (er.HasErrors)
                        return false;

                    if (!options.AlreadyCompiled) {
                        // Compile the assembly
                        var ctx = new CompilerContext(settings, new ConvertingReportPrinter(er));
                        var d = new Mono.CSharp.Driver(ctx);
                        d.Compile();
                        if (er.HasErrors)
                            return false;
                    }

                    // Compile the script
                    var md = new MetadataImporter.ScriptSharpMetadataImporter(options.MinimizeScript);
                    var n = new DefaultNamer();
                    PreparedCompilation compilation = null;
                    var rtl = new ScriptSharpRuntimeLibrary(md, er, n.GetTypeParameterName, tr => { var t = tr.Resolve(compilation.Compilation).GetDefinition(); return new JsTypeReferenceExpression(t.ParentAssembly, md.GetTypeSemantics(t).Name); });
                    var compiler = new Compiler.Compiler(md, n, rtl, er, allowUserDefinedStructs: options.References.Count == 0 /* We allow user-defined structs in mscorlib only, which can be identified by the fact that it has no references*/);

                    var references = LoadReferences(settings.AssemblyReferences, er);
                    if (references == null)
                        return false;

                    compilation = compiler.CreateCompilation(options.SourceFiles.Select(f => new SimpleSourceFile(f, settings.Encoding)), references, options.DefineConstants);
                    var compiledTypes = compiler.Compile(compilation);

                    var js = new ScriptSharpOOPEmulator(compilation.Compilation, md, rtl, er).Rewrite(compiledTypes, compilation.Compilation);
                    js = new GlobalNamespaceReferenceImporter().ImportReferences(js);

                    if (er.HasErrors)
                        return false;

                    string outputAssemblyPath = !string.IsNullOrEmpty(options.OutputAssemblyPath) ? options.OutputAssemblyPath : Path.ChangeExtension(options.SourceFiles[0], ".dll");
                    string outputScriptPath   = !string.IsNullOrEmpty(options.OutputScriptPath)   ? options.OutputScriptPath   : Path.ChangeExtension(options.SourceFiles[0], ".js");

                    if (!options.AlreadyCompiled) {
                        try {
                            File.Copy(intermediateAssemblyFile, outputAssemblyPath, true);
                        }
                        catch (IOException ex) {
                            er.Region = DomRegion.Empty;
                            er.Message(7950, ex.Message);
                            return false;
                        }
                        if (!string.IsNullOrEmpty(options.DocumentationFile)) {
                            try {
                                File.Copy(intermediateDocFile, options.DocumentationFile, true);
                            }
                            catch (IOException ex) {
                                er.Region = DomRegion.Empty;
                                er.Message(7952, ex.Message);
                                return false;
                            }
                        }
                    }

                    string script = string.Join("", js.Select(s => options.MinimizeScript ? OutputFormatter.FormatMinified(Minifier.Process(s)) : OutputFormatter.Format(s)));
                    try {
                        File.WriteAllText(outputScriptPath, script, settings.Encoding);
                    }
                    catch (IOException ex) {
                        er.Region = DomRegion.Empty;
                        er.Message(7951, ex.Message);
                        return false;
                    }
                    return true;
                }
                catch (Exception ex) {
                    er.Region = DomRegion.Empty;
                    er.InternalError(ex.ToString());
                    return false;
                }
                finally {
                    if (!options.AlreadyCompiled) {
                        try { File.Delete(intermediateAssemblyFile); } catch {}
                        try { File.Delete(intermediateDocFile); } catch {}
                    }
                }
            }
		private IList<JsType> Compile(string program) {
			var compilation = PreparedCompilation.CreateCompilation("X", new[] { new MockSourceFile("file.cs", program) }, new[] { Common.Mscorlib }, new string[0]);
			var compiler = new Compiler.Compiler(new MockMetadataImporter(), new MockNamer(), new MockRuntimeLibrary(), new MockErrorReporter());
			return compiler.Compile(compilation).ToList();
		}
Exemplo n.º 11
0
        public void SimpleExpression1_VB_Test()
        {
            GrammarTree GT = LoadGrammar(TESTFILESPATH + @"simple expression1_vb.tpg");
            Grammar G = (Grammar)GT.Eval();
            G.Directives["TinyPG"]["TemplatePath"] = TEMPLATEPATH_VB;

            Compiler.Compiler compiler = new Compiler.Compiler();

            compiler.Compile(G);
            Assert.IsTrue(compiler.Errors.Count == 0, "compilation contains errors");

            CompilerResult result = compiler.Run("5+7/3*2+(4*2)");

            Assert.IsTrue(result.Output.StartsWith("Parse was successful."));
        }
Exemplo n.º 12
0
        public void SimpleExpression4_VB_Test()
        {
            GrammarTree GT = LoadGrammar(TESTFILESPATH + @"GrammarHighlighter_vb.tpg");
            Grammar G = (Grammar)GT.Eval();
            G.Directives.Add(new Directive("TinyPG"));
            G.Directives["TinyPG"]["TemplatePath"] = TEMPLATEPATH_VB;

            Compiler.Compiler compiler = new Compiler.Compiler();

            compiler.Compile(G);
            Assert.IsTrue(compiler.Errors.Count == 0, "compilation contains errors");

            CompilerResult result = compiler.Run("using System.IO;\r\n");

            Assert.IsTrue(result.Output.StartsWith("Parse was successful."));
        }
Exemplo n.º 13
0
        public void SimpleExpression2_VB_Test()
        {
            GrammarTree GT = LoadGrammar(TESTFILESPATH + @"simple expression2_vb.tpg");
            Grammar G = (Grammar)GT.Eval();
            G.Directives.Add(new Directive("TinyPG"));
            G.Directives["TinyPG"]["TemplatePath"] = TEMPLATEPATH_VB;

            Compiler.Compiler compiler = new Compiler.Compiler();

            compiler.Compile(G);
            Assert.IsTrue(compiler.Errors.Count == 0, "compilation contains errors");

            CompilerResult result = compiler.Run("5+8/4*2+(4*2)");

            Assert.IsTrue(Convert.ToInt32(result.Value) == 17);
        }