Пример #1
0
        public void TestUnknownNative()
        {
            var nativeDB = NativeDB.FromJson(@"
            {
                ""TranslationTable"": [[1234605617868164317, 1234605617868164317, 1234605617868164317, 1234605617868164317, 1234605617868164317, 1234605617868164317, 1234605617868164317, 1234605617868164317, 1234605617868164317, 1234605617868164317, 1234605617868164317, 1234605617868164317, 1234605617868164317, 1234605617868164317, 1234605617868164317, 1234605617868164317, 1234605617868164317, 1234605617868164317, 1234605617868164317, 1234605617868164317, 1234605617868164317, 1234605617868164317, 1234605617868164317, 1234605617868164317]],
                ""HashToRows"": [{ ""Hash"": 1234605617868164317, ""Rows"": [ 0 ] }],
                ""Commands"": [{
                      ""Hash"": 1234605617868164317,
                      ""Name"": ""TEST"",
                      ""Build"": 323,
                      ""Parameters"": [{ ""Type"": ""int"", ""Name"": ""a""}, { ""Type"": ""int"", ""Name"": ""b""}],
                      ""ReturnType"": ""void""
                    }
                ]
            }");

            var d = ParseAndIdentify($@"
                NATIVE PROC UNKNOWN_TEST(INT a, INT b)

                SCRIPT test_script
                ENDSCRIPT
            ", nativeDB);

            Assert.True(d.HasErrors);
            Assert.Single(d.Errors);
        }
Пример #2
0
        public static void DoTest()
        {
            //NativeDB.Fetch(new Uri("https://raw.githubusercontent.com/alloc8or/gta5-nativedb-data/master/natives.json"), "ScriptHookV_1.0.2060.1.zip")
            //    .ContinueWith(t => File.WriteAllText("nativedb.json", t.Result.ToJson()))
            //    .Wait();

            var nativeDB = NativeDB.FromJson(File.ReadAllText("nativedb.json"));

            using var reader = new StringReader(Code);
            var comp = new Compilation {
                NativeDB = nativeDB
            };

            comp.SetMainModule(reader);
            comp.Compile();
            File.WriteAllText("test_script.ast.txt", comp.MainModule.GetAstDotGraph());

            var d       = comp.GetAllDiagnostics();
            var symbols = comp.MainModule.SymbolTable;

            Console.WriteLine($"Errors:   {d.HasErrors} ({d.Errors.Count()})");
            Console.WriteLine($"Warnings: {d.HasWarnings} ({d.Warnings.Count()})");
            foreach (var diagnostic in d.AllDiagnostics)
            {
                diagnostic.Print(Console.Out);
            }

            foreach (var s in symbols.Symbols)
            {
                if (s is TypeSymbol t && t.Type is StructType struc)
                {
                    Console.WriteLine($"  > '{t.Name}' Size = {struc.SizeOf}");
                }
            }

            Console.WriteLine();
            new Dumper(comp.CompiledScript).Dump(Console.Out, true, true, true, true, true);

            YscFile ysc = new YscFile
            {
                Script = comp.CompiledScript
            };

            string outputPath = "test_script.ysc";

            byte[] data = ysc.Save(Path.GetFileName(outputPath));
            File.WriteAllBytes(outputPath, data);

            outputPath = Path.ChangeExtension(outputPath, "unencrypted.ysc");
            data       = ysc.Save();
            File.WriteAllBytes(outputPath, data);
            ;
        }
Пример #3
0
 public CodeGenerator(TextWriter sink, Program program, GlobalSymbolTable symbols, DiagnosticsReport diagnostics, NativeDB nativeDB)
 {
     Sink             = sink;
     Program          = program;
     Symbols          = symbols;
     Diagnostics      = diagnostics;
     NativeDB         = nativeDB;
     Strings          = new();
     stmtEmitter      = new(this);
     valueEmitter     = new(this);
     addressEmitter   = new(this);
     optimizer        = new();
     funcInstructions = new();
 }
Пример #4
0
        public static void DoTest()
        {
            //NativeDB.Fetch(new Uri("https://raw.githubusercontent.com/alloc8or/gta5-nativedb-data/master/natives.json"), "ScriptHookV_1.0.2060.1.zip")
            //    .ContinueWith(t => File.WriteAllText("nativedb.json", t.Result.ToJson()))
            //    .Wait();

            var nativeDB = NativeDB.FromJson(System.IO.File.ReadAllText("nativesdb.json"));

            const string BaseDir = "D:\\sources\\gtav-sc-tools\\examples\\language_sample\\";

            Parse(BaseDir + "language_sample_main.sc", nativeDB);
            Parse(BaseDir + "language_sample_child.sc", nativeDB);
            //Parse(BaseDir + "language_sample_shared.sch");

            ;
        }
Пример #5
0
 public static void Generate(TextWriter w, NativeDB db)
 {
     foreach (var cmd in db.Commands)
     {
         w.Write("NATIVE ");
         var returnTy = TypeToScriptType(cmd.ReturnType, true);
         if (returnTy == null)
         {
             w.Write("PROC ");
         }
         else
         {
             w.Write("FUNC ");
             w.Write(returnTy);
             w.Write(' ');
         }
         w.Write(cmd.Name);
         w.Write('(');
         w.Write(string.Join(", ", cmd.Parameters.Select(p => $"{TypeToScriptType(p.Type, false)} {SafeSymbol(p.Name)}")));
         w.Write(')');
         w.WriteLine();
     }
 }
Пример #6
0
        private static void Parse(string filePath, NativeDB nativeDB)
        {
            using var r = new StreamReader(filePath);
            var d  = new DiagnosticsReport();
            var sw = Stopwatch.StartNew();
            var p  = new Parser(r, filePath)
            {
                UsingResolver = new FileUsingResolver()
            };

            p.Parse(d);
            sw.Stop();
            Console.WriteLine(sw.Elapsed);
            //d.PrintAll(Console.Out);

            var globalSymbols = GlobalSymbolTableBuilder.Build(p.OutputAst, d);

            IdentificationVisitor.Visit(p.OutputAst, d, globalSymbols, nativeDB);
            TypeChecker.Check(p.OutputAst, d, globalSymbols);
            //d.PrintAll(Console.Out);
            ;

            if (!d.HasErrors)
            {
                Console.WriteLine("CodeGen...");
                using var sink = new StringWriter();
                new CodeGenerator(sink, p.OutputAst, globalSymbols, d, nativeDB).Generate();
                var s = sink.ToString();
                ;
                using var reader = new StringReader(s);
                var assembler = Assembler.Assemble(reader, Path.ChangeExtension(filePath, "scasm"), nativeDB, options: new() { IncludeFunctionNames = true });
                assembler.Diagnostics.PrintAll(Console.Out);
                ;
            }
            d.PrintAll(Console.Out);
            ;
        }