Skip to content

Commit

Permalink
feat(tools): s2scompiler
Browse files Browse the repository at this point in the history
This was a side product of my "wasp-launcher" project. It was growing too large to be easy to maintain and I needed to split it into several files to keep working on it.
At the same time, I need the end result to be a single script.
So I've made this tool which is basically a "Simba to Simba compiler" that compiles multiple scripts into a single one.
I thought more people might find it useful.
No instructions given, should be easy to use, figure it out.
It's also pretty basic and simple, won't understand compiler if directives, keep that in mind.
  • Loading branch information
Torwent committed Feb 22, 2024
1 parent 87e068f commit 45094c4
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions tools/s2scompiler.simba
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
(*
# S2SCompiler
A "Simba to Simba compiler" that compiles multiple scripts into a single one.
This is a very basic script and won't understand things like "IF" compiler directives.
*)
{$I SRL-T/osr.simba}

const
FILE_NAME: String = 'launcher.simba';
PATH: String = {$macro CURRENT_DIRECTORY}; //Change to project path or drop this in your project folder.
ERASE_DIRECTIVE: String = '{$IFNDEF WS_CORE}{$I core.simba}{$ENDIF}';

const INCLUDE_DIRECTIVES: TStringArray = ['{$I ', '{$INCLUDE', '{$i ', '$include'];

function GetDirectiveIndex(content: String): Int32;
begin
for Result := 0 to High(INCLUDE_DIRECTIVES) do
if content.Contains(INCLUDE_DIRECTIVES[Result]) then
Exit;
Result := -1;
end;

function GetIncludeContent(content: String; directiveIndex: Int32): String;
var
filePath: String;
begin
filePath := content.After(INCLUDE_DIRECTIVES[directiveIndex]).Before('}');
Result := ReadFileContents(PATH + filePath);
Result := Result.Replace(ERASE_DIRECTIVE, '');
Result := LineEnding + Result + LineEnding;
end;

function ReplaceIncludeDirective(content, sub: String; directiveIndex: Int32): String;
var
directive: String;
begin
directive := content.After(INCLUDE_DIRECTIVES[directiveIndex]).Before('}');
directive := INCLUDE_DIRECTIVES[directiveIndex] + directive + '}';
Result := content.Replace(directive, sub);
end;

var
content, sub: String;
index: Int32;
begin
content := ReadFileContents(PATH + FILE_NAME);

index := GetDirectiveIndex(content);
while index > -1 do
begin
sub := GetIncludeContent(content, index);
content := ReplaceIncludeDirective(content, sub, index);
index := GetDirectiveIndex(content);
end;

WriteFileContents(PATH + FILE_NAME.Replace('.simba', '-compiled.simba'), content, False);
end;

0 comments on commit 45094c4

Please sign in to comment.