Skip to content
This repository was archived by the owner on Jun 27, 2025. It is now read-only.

Commit 8d5b42f

Browse files
committed
Added indexing
1 parent d8c414e commit 8d5b42f

File tree

1 file changed

+234
-0
lines changed

1 file changed

+234
-0
lines changed

script.cs

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ function EventScript_fromScript(%script, %error)
2626

2727
%len = strlen(%script);
2828

29+
%indexTableCount = 0;
30+
2931
%state = 0;
3032
%line = 1;
3133

34+
// PASS 1
35+
// Read through the string and add everything to the list
36+
3237
for (%i = 0; %i < %len; %i++)
3338
{
3439
%char = getSubStr(%script, %i, 1);
@@ -566,6 +571,18 @@ function EventScript_fromScript(%script, %error)
566571
%list.count++;
567572
}
568573

574+
// Indexing
575+
case "[":
576+
577+
// Set default values for table element
578+
%indexTableLine[%indexTableCount] = %line;
579+
%indexTableIndex[%indexTableCount] = %list.count;
580+
%indexTableParam[%indexTableCount] = getFieldCount(%list.value[%list.count, "param"]);
581+
%indexTableType[%indexTableCount] = -1;
582+
%indexTableListCount[%indexTableCount] = 0;
583+
584+
%state = 4;
585+
569586
// Number / Boolean / Naked string
570587
default:
571588

@@ -638,6 +655,168 @@ function EventScript_fromScript(%script, %error)
638655
%list.count++;
639656
}
640657
}
658+
659+
// Indexing
660+
case 4:
661+
662+
%readChars = false;
663+
%lastSpace = false;
664+
665+
// Verify characters
666+
for (%n = %i; %n < %len; %n++)
667+
{
668+
%c = getSubStr(%script, %n, 1);
669+
670+
if (strpos(" \t\n", %c) >= 0)
671+
{
672+
if (%readChars)
673+
%lastSpace = true;
674+
if (%c $= "\n")
675+
%line++;
676+
}
677+
else if (%c $= "]" || %c $= ":" || %c $= ",")
678+
{
679+
break;
680+
}
681+
else if (strpos("1234567890", strlwr(%c)) < 0)
682+
{
683+
%n = -1;
684+
break;
685+
}
686+
else if (%readChars && %lastSpace)
687+
{
688+
%n = -2;
689+
break;
690+
}
691+
else
692+
{
693+
%readChars = true;
694+
}
695+
}
696+
697+
// Found errors
698+
if (%n < 0)
699+
{
700+
if (%n == -1)
701+
call(%error, "Parse Error: Found illegal character " @ %c @ " on line " @ %line);
702+
else if (%n == -2)
703+
call(%error, "Parse Error: Index cannot contain whitespace on line " @ %line);
704+
%list.error = true;
705+
return %list;
706+
}
707+
else if (%n >= %len)
708+
{
709+
call(%error, "Parse Error: Missing index ending delimiter ] on line " @ %line);
710+
%list.error = true;
711+
return %list;
712+
}
713+
714+
%data = trim(getSubStr(%script, %i, %n - %i));
715+
716+
%i = %n;
717+
718+
if (%c $= ",")
719+
{
720+
if (%indexTableType[%indexTableCount] == 1)
721+
{
722+
%n = -1;
723+
}
724+
else if (%data $= "")
725+
{
726+
%n = -2;
727+
}
728+
else
729+
{
730+
%indexTableType[%indexTableCount] = 0;
731+
%indexTableList[%indexTableCount, %indexTableListCount[%indexTableCount]] = %data;
732+
%indexTableListCount[%indexTableCount]++;
733+
}
734+
735+
}
736+
else if (%c $= ":")
737+
{
738+
if (%indexTableType[%indexTableCount] == 0)
739+
{
740+
%n = -1;
741+
}
742+
else
743+
{
744+
%indexTableType[%indexTableCount] = 1;
745+
%indexTableStart[%indexTableCount] = %data;
746+
}
747+
}
748+
else if (%c $= "]")
749+
{
750+
if (%indexTableType[%indexTableCount] == -1 || (%indexTableType[%indexTableCount] != 1 && %data $= ""))
751+
{
752+
%n = -2;
753+
}
754+
else if (%indexTableType[%indexTableCount] == 0)
755+
{
756+
%indexTableList[%indexTableCount, %indexTableListCount[%indexTableCount]] = %data;
757+
%indexTableListCount[%indexTableCount]++;
758+
}
759+
else if (%indexTableType[%indexTableCount] == 1)
760+
{
761+
%indexTableEnd[%indexTableCount] = %data;
762+
}
763+
764+
if (%n >= 0)
765+
{
766+
// Finished, so lets find next and get out of here
767+
for (%n = %i + 1; %n < %len; %n++)
768+
{
769+
%c = getSubStr(%script, %n, 1);
770+
771+
if (strpos(" \t\n", %c) >= 0)
772+
{
773+
if (%c $= "\n")
774+
%line++;
775+
}
776+
else if (%c $= "," || %c $= ")")
777+
{
778+
break;
779+
}
780+
else
781+
{
782+
%n = -1;
783+
break;
784+
}
785+
}
786+
}
787+
788+
if (%c $= ",")
789+
{
790+
%state = 3;
791+
}
792+
else if (%c $= ")")
793+
{
794+
%state = 0;
795+
// Finished events
796+
%list.count++;
797+
}
798+
799+
%indexTableCount++;
800+
}
801+
802+
// Found errors
803+
if (%n < 0)
804+
{
805+
if (%n == -1)
806+
call(%error, "Parse Error: Found illegal character " @ %c @ " on line " @ %line);
807+
else if (%n == -2)
808+
call(%error, "Parse Error: Empty index found on line " @ %line);
809+
%list.error = true;
810+
return %list;
811+
}
812+
else if (%n >= %len)
813+
{
814+
call(%error, "Parse Error: Missing index ending delimiter ] on line " @ %line);
815+
%list.error = true;
816+
return %list;
817+
}
818+
819+
%i = %n;
641820
}
642821
}
643822

@@ -650,6 +829,61 @@ function EventScript_fromScript(%script, %error)
650829
%list.error = true;
651830
}
652831

832+
// PASS 2
833+
// Parse parameters individually
834+
835+
for (%i = 0; %i < %indexTableCount; %i++)
836+
{
837+
%line = %indexTableLine[%i];
838+
%index = %indexTableIndex[%i];
839+
%param = %indexTableParam[%i];
840+
841+
switch (%indexTableType[%i])
842+
{
843+
// Indexing
844+
case 0:
845+
846+
%params = "";
847+
for (%n = 0; %n < %indexTableListCount[%i]; %n++)
848+
{
849+
%var = setWord(%params, %n, %indexTableList[%i, %n]);
850+
%params = mClamp(, 0, %list.count - 1);
851+
}
852+
853+
%list.value[%index, "params"] = setField(%list.value[%index, "params"], %param, %params);
854+
855+
// Range
856+
case 1:
857+
858+
%start = %indexTableStart[%i];
859+
%end = %indexTableEnd[%i];
860+
861+
// Default values
862+
if (%start $= "")
863+
%start = 0;
864+
if (%end $= "")
865+
%end = %list.count - 1;
866+
867+
%start = mClamp(%start, 0, %list.count - 1);
868+
%end = mClamp(%end, 0, %list.count - 1);
869+
870+
// Incorrect indexing order
871+
if (%start > %end)
872+
{
873+
call(%error, "Logic Error: Invalid indexing values [" @ %start @ ":" @ %end @ "] on line " @ %line);
874+
%list.error = true;
875+
return %list;
876+
}
877+
878+
%params = "";
879+
%n = -1;
880+
for (%m = %start; %m <= %end; %m++)
881+
%params = setWord(%params, %n++, %m);
882+
883+
%list.value[%index, "params"] = setField(%list.value[%index, "params"], %param, %params);
884+
}
885+
}
886+
653887
return %list;
654888
}
655889

0 commit comments

Comments
 (0)