Skip to content

Commit f172056

Browse files
authored
Merge pull request #18 from Nenkai/adhoc-accuracy
Adhoc compilation/syntax accuracy improvements
2 parents 840b288 + 565b950 commit f172056

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+865
-358
lines changed

GTAdhocToolchain.CLI/Program.cs

Lines changed: 130 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.IO;
45

6+
using CommandLine;
7+
58
using Esprima;
9+
using Esprima.Ast;
610

7-
using CommandLine;
811
using NLog;
912

10-
using GTAdhocToolchain.Compiler;
1113
using GTAdhocToolchain.CodeGen;
12-
using GTAdhocToolchain.Project;
14+
using GTAdhocToolchain.Compiler;
15+
using GTAdhocToolchain.Core.Instructions;
1316
using GTAdhocToolchain.Disasm;
1417
using GTAdhocToolchain.Menu;
18+
using GTAdhocToolchain.Menu.Fields;
1519
using GTAdhocToolchain.Menu.Resources;
1620
using GTAdhocToolchain.Packaging;
17-
using GTAdhocToolchain.Core.Instructions;
18-
using GTAdhocToolchain.Menu.Fields;
1921
using GTAdhocToolchain.Preprocessor;
20-
using GTAdhocToolchain.Analyzer;
21-
using Esprima.Ast;
22+
using GTAdhocToolchain.Project;
2223

2324
namespace GTAdhocToolchain.CLI;
2425

@@ -58,8 +59,9 @@ public static void Main(string[] args)
5859
return;
5960
}
6061

61-
Parser.Default.ParseArguments<BuildVerbs, PackVerbs, UnpackVerbs, MProjectToBinVerbs, MProjectToTextVerbs>(args)
62+
Parser.Default.ParseArguments<BuildVerbs, DissasemblyReplVerbs, PackVerbs, UnpackVerbs, MProjectToBinVerbs, MProjectToTextVerbs>(args)
6263
.WithParsed<BuildVerbs>(Build)
64+
.WithParsed<DissasemblyReplVerbs>(DissasemblyRepl)
6365
.WithParsed<PackVerbs>(Pack)
6466
.WithParsed<UnpackVerbs>(Unpack)
6567
.WithParsed<MProjectToBinVerbs>(MProjectToBin)
@@ -408,6 +410,119 @@ private static void BuildScript(string inputPath, string output, int version = 1
408410
Environment.ExitCode = -1;
409411
}
410412

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+
411526
public static void MProjectToBin(MProjectToBinVerbs verbs)
412527
{
413528
if (verbs.Version == 0)
@@ -512,6 +627,13 @@ public class BuildVerbs
512627
public string BaseIncludeFolder { get; set; }
513628
}
514629

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+
515637
[Verb("pack", HelpText = "Pack files like gpb's, or mpackage's.")]
516638
public class PackVerbs
517639
{

0 commit comments

Comments
 (0)