|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
3 | 4 | using System.IO; |
4 | 5 |
|
| 6 | +using CommandLine; |
| 7 | + |
5 | 8 | using Esprima; |
| 9 | +using Esprima.Ast; |
6 | 10 |
|
7 | | -using CommandLine; |
8 | 11 | using NLog; |
9 | 12 |
|
10 | | -using GTAdhocToolchain.Compiler; |
11 | 13 | using GTAdhocToolchain.CodeGen; |
12 | | -using GTAdhocToolchain.Project; |
| 14 | +using GTAdhocToolchain.Compiler; |
| 15 | +using GTAdhocToolchain.Core.Instructions; |
13 | 16 | using GTAdhocToolchain.Disasm; |
14 | 17 | using GTAdhocToolchain.Menu; |
| 18 | +using GTAdhocToolchain.Menu.Fields; |
15 | 19 | using GTAdhocToolchain.Menu.Resources; |
16 | 20 | using GTAdhocToolchain.Packaging; |
17 | | -using GTAdhocToolchain.Core.Instructions; |
18 | | -using GTAdhocToolchain.Menu.Fields; |
19 | 21 | using GTAdhocToolchain.Preprocessor; |
20 | | -using GTAdhocToolchain.Analyzer; |
21 | | -using Esprima.Ast; |
| 22 | +using GTAdhocToolchain.Project; |
22 | 23 |
|
23 | 24 | namespace GTAdhocToolchain.CLI; |
24 | 25 |
|
@@ -58,8 +59,9 @@ public static void Main(string[] args) |
58 | 59 | return; |
59 | 60 | } |
60 | 61 |
|
61 | | - Parser.Default.ParseArguments<BuildVerbs, PackVerbs, UnpackVerbs, MProjectToBinVerbs, MProjectToTextVerbs>(args) |
| 62 | + Parser.Default.ParseArguments<BuildVerbs, DissasemblyReplVerbs, PackVerbs, UnpackVerbs, MProjectToBinVerbs, MProjectToTextVerbs>(args) |
62 | 63 | .WithParsed<BuildVerbs>(Build) |
| 64 | + .WithParsed<DissasemblyReplVerbs>(DissasemblyRepl) |
63 | 65 | .WithParsed<PackVerbs>(Pack) |
64 | 66 | .WithParsed<UnpackVerbs>(Unpack) |
65 | 67 | .WithParsed<MProjectToBinVerbs>(MProjectToBin) |
@@ -408,6 +410,119 @@ private static void BuildScript(string inputPath, string output, int version = 1 |
408 | 410 | Environment.ExitCode = -1; |
409 | 411 | } |
410 | 412 |
|
| 413 | + private static void DissasemblyRepl(DissasemblyReplVerbs replVerbs) |
| 414 | + { |
| 415 | + Console.Clear(); |
| 416 | + Console.WriteLine("REPL mode. Start typing adhoc code to dissasemble it. Enter /? for more commands."); |
| 417 | + Console.WriteLine($"Adhoc Version: {replVerbs.Version}"); |
| 418 | + |
| 419 | + while (true) |
| 420 | + { |
| 421 | + Console.Write(">"); |
| 422 | + var line = Console.ReadLine(); |
| 423 | + |
| 424 | + if (line.StartsWith("/?")) |
| 425 | + { |
| 426 | + Console.WriteLine("/clear - Clears the console"); |
| 427 | + Console.WriteLine("/version - Sets adhoc version"); |
| 428 | + continue; |
| 429 | + } |
| 430 | + else if (line.StartsWith("/clear")) |
| 431 | + { |
| 432 | + Console.Clear(); |
| 433 | + continue; |
| 434 | + } |
| 435 | + else if (line.StartsWith("/version")) |
| 436 | + { |
| 437 | + ReadOnlySpan<char> range = line.AsSpan().Slice("/version".Length).Trim(); |
| 438 | + if (range.IsEmpty) |
| 439 | + { |
| 440 | + Console.WriteLine($"Current adhoc version is set to {replVerbs.Version}."); |
| 441 | + } |
| 442 | + else if (int.TryParse(range, out int version)) |
| 443 | + { |
| 444 | + Console.WriteLine($"Now compiling for adhoc version {version}"); |
| 445 | + replVerbs.Version = version; |
| 446 | + } |
| 447 | + else |
| 448 | + { |
| 449 | + Console.WriteLine("Invalid version. Usage: /version <adhoc version>"); |
| 450 | + } |
| 451 | + |
| 452 | + continue; |
| 453 | + } |
| 454 | + |
| 455 | + |
| 456 | + var preprocessor = new AdhocScriptPreprocessor(); |
| 457 | + preprocessor.SetCurrentFileName("temp.ad"); |
| 458 | + |
| 459 | + string preprocessed = preprocessor.Preprocess(line); |
| 460 | + |
| 461 | + var errorHandler = new AdhocErrorHandler(); |
| 462 | + var parser = new AdhocAbstractSyntaxTree(preprocessed, new ParserOptions() |
| 463 | + { |
| 464 | + ErrorHandler = errorHandler |
| 465 | + }); |
| 466 | + parser.SetFileName("temp.ad"); |
| 467 | + |
| 468 | + var program = parser.ParseScript(); |
| 469 | + if (errorHandler.HasErrors()) |
| 470 | + { |
| 471 | + foreach (ParseError error in errorHandler.Errors) |
| 472 | + Logger.Error($"Syntax error: {error.Description} at {error.Source}:{error.LineNumber}"); |
| 473 | + } |
| 474 | + else |
| 475 | + { |
| 476 | + try |
| 477 | + { |
| 478 | + var compiler = new AdhocScriptCompiler(); |
| 479 | + compiler.SetSourcePath("test.ad"); |
| 480 | + compiler.Setup(replVerbs.Version); |
| 481 | + compiler.CompileScript(program); |
| 482 | + |
| 483 | + AdhocCodeGen codeGen = new AdhocCodeGen(compiler.MainFrame, compiler.SymbolMap); |
| 484 | + codeGen.Generate(); |
| 485 | + |
| 486 | + for (int i = 0; i < codeGen.Frame.Instructions.Count; i++) |
| 487 | + { |
| 488 | + InstructionBase inst = codeGen.Frame.Instructions[i]; |
| 489 | + Dissasemble(inst, i, 0); |
| 490 | + } |
| 491 | + |
| 492 | + void Dissasemble(InstructionBase inst, int instNumber, int depth) |
| 493 | + { |
| 494 | + Console.WriteLine($"{new string(' ', depth * 2)} {instNumber, 3} | {inst}"); |
| 495 | + if (inst.IsFunctionOrMethod()) |
| 496 | + { |
| 497 | + SubroutineBase subroutine = inst as SubroutineBase; |
| 498 | + for (int i = 0; i < subroutine.CodeFrame.Instructions.Count; i++) |
| 499 | + { |
| 500 | + InstructionBase subInst = subroutine.CodeFrame.Instructions[i]; |
| 501 | + Dissasemble(subInst, i, depth + 1); |
| 502 | + } |
| 503 | + } |
| 504 | + } |
| 505 | + } |
| 506 | + catch (PreprocessorException preprocessException) |
| 507 | + { |
| 508 | + Logger.Error($"{preprocessException.FileName}:{preprocessException.Token.Location.Start.Line}: preprocess error: {preprocessException.Message}"); |
| 509 | + } |
| 510 | + catch (ParserException parseException) |
| 511 | + { |
| 512 | + Logger.Error($"Syntax error: {parseException.Description} at {parseException.SourceText}:{parseException.LineNumber}"); |
| 513 | + } |
| 514 | + catch (AdhocCompilationException compileException) |
| 515 | + { |
| 516 | + Logger.Error($"Compilation error: {compileException.Message}"); |
| 517 | + } |
| 518 | + catch (Exception e) |
| 519 | + { |
| 520 | + Logger.Fatal(e, "Internal error in compilation"); |
| 521 | + } |
| 522 | + } |
| 523 | + } |
| 524 | + } |
| 525 | + |
411 | 526 | public static void MProjectToBin(MProjectToBinVerbs verbs) |
412 | 527 | { |
413 | 528 | if (verbs.Version == 0) |
@@ -512,6 +627,13 @@ public class BuildVerbs |
512 | 627 | public string BaseIncludeFolder { get; set; } |
513 | 628 | } |
514 | 629 |
|
| 630 | +[Verb("dissasembly-repl", HelpText = "Starts a dissasembler repl for quickly disassembling input adhoc source code.")] |
| 631 | +public class DissasemblyReplVerbs |
| 632 | +{ |
| 633 | + [Option('v', "version", HelpText = "Adhoc version.")] |
| 634 | + public int Version { get; set; } |
| 635 | +} |
| 636 | + |
515 | 637 | [Verb("pack", HelpText = "Pack files like gpb's, or mpackage's.")] |
516 | 638 | public class PackVerbs |
517 | 639 | { |
|
0 commit comments