[go: up one dir, main page]

Menu

[904618]: / src / t_vm.c  Maximize  Restore  History

Download this file

1186 lines (987 with data), 27.0 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
// -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-
// ----------------------------------------------------------------------------
// :oCCCCOCoc.
// .cCO8OOOOOOOOO8Oo:
// .oOO8OOOOOOOOOOOOOOOCc
// cO8888: .:oOOOOC. TM
// :888888: :CCCc .oOOOOC. ### ### #########
// C888888: .ooo: .C######## ##### ##### ###### ###### ##########
// O888888: .oO### ### ##### ##### ######## ######## #### ###
// C888888: :8O. .C########## ### #### ### ## ## ## ## #### ###
// :8@@@@8: :888c o### ### #### ### ######## ######## ##########
// :8@@@@C C@@@@ oo######## ### ## ### ###### ###### #########
// cO@@@@@@@@@@@@@@@@@Oc0
// :oO8@@@@@@@@@@Oo.
// .oCOOOOOCc. http://remood.org/
// ----------------------------------------------------------------------------
// Copyright (C) 2012-2013 GhostlyDeath <ghostlydeath@remood.org>
// <ghostlydeath@gmail.com>
// ----------------------------------------------------------------------------
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// ----------------------------------------------------------------------------
// DESCRIPTION: Virtual Machine
/***************
*** INCLUDES ***
***************/
#include "t_vm.h"
#include "dstrings.h"
#include "console.h"
#include "z_zone.h"
/****************
*** CONSTANTS ***
****************/
#define TVMPP_COMMENT UINT16_C(0x0001) // In Comment
#define TVMPP_ESCAPE UINT16_C(0x0002) // Escaped Sequence
#define TVMPP_QUOTE UINT16_C(0x0004) // Quoted String
/* TVM_TokenType_t -- Type of token thing is */
typedef enum TVM_TokenType_e
{
TVMTT_INVALID, // Invalid
TVMTT_SYMBOL, // Symbol
TVMTT_IDENTIFIER, // Identifier
TVMTT_STRING, // String
TVMTT_INTEGER, // Integer
TVMTT_FIXED, // Fixed
TVMTT_DECLARE, // Declare Word
NUMTVMTOKENTYPES
} TVM_TokenType_t;
/* TVM_ScopeFlags_t -- Flags for scopes */
typedef enum TVM_ScopeFlags_e
{
TVMSF_EXTERN = UINT32_C(0x00000001), // External Visible Scope
} TVM_ScopeFlags_t;
/*****************
*** STRUCTURES ***
*****************/
typedef bool_t (*TVM_ParseMode_t)(struct TVM_State_s* const a_State, char* const a_Buf, const size_t a_Len);
/* TVM_TokenChain_t -- Chains of tokens */
typedef struct TVM_TokenChain_s
{
TVM_TokenType_t Type; // Type of token
char* Text; // Token text
struct TVM_TokenChain_s* Prev; // Previous Link
struct TVM_TokenChain_s* Next; // Next Link
struct TVM_TokenChain_s* First; // First link
} TVM_TokenChain_t;
/* TVM_State_t -- State */
typedef struct TVM_State_s
{
TVM_Namespace_t NameSpace; // Namespace
char* ErrorStr; // Error string
int32_t ScopeDepth; // Depth of Scope
TVM_ParseMode_t ParseMode; // Current Parse Mode
TVM_ParseMode_t Confused; // Confused mode
int8_t ReadInclude; // Include read count
int32_t ReadCond; // Conditionals Read
int32_t CondStack; // Conditional parenthesis stack
const WL_WADEntry_t* IncludeThis; // Includes this file
const WL_WADEntry_t** Incs; // Stack of includes
size_t NumIncs; // Number of them
TVM_TokenChain_t* TokChain; // Token Chain
char CondType; // Type of condition
uint32_t ScopeIdent; // Scope Identity
int32_t ReadScript; // Script Reading
uint8_t ScriptID; // ID of script
} TVM_State_t;
/****************
*** CONSTANTS ***
****************/
static const struct
{
const char* Token; // Token for operator
bool_t Backwards; // Backwards expression
} c_VMOps[] =
{
{"=", true},
{"||", false},
{"&&", false},
{"|", false},
{"&", false},
{"==", false},
{"!=", false},
{"<", false},
{">", false},
{"<=", false},
{">=", false},
{"+", false},
{"-", false},
{"*", false},
{"/", false},
{"%", false},
{"!", false},
{"++", false},
{"--", false},
{".", false},
{NULL}
};
/*************
*** LOCALS ***
*************/
//static TVM_State_t l_VMState[NUMTVMNAMESPACES]; // Current Map State
/****************
*** FUNCTIONS ***
****************/
/* TVMS_IsSpace() -- Returns true if character is space */
static bool_t TVMS_IsSpace(const char a_Char)
{
switch (a_Char)
{
case ' ':
case '\r':
case '\t':
return true;
default:
return false;
}
}
/* TVMS_GetTokenType() -- Returns the type of the token passed */
static TVM_TokenType_t TVMS_GetTokenType(const char* const a_Buf)
{
char* p;
int32_t i;
/* Setup */
p = a_Buf;
/* End? */
if (!*p)
return TVMTT_INVALID;
/* Perform checks */
// Special
if (!strcmp(a_Buf, "hub") || !strcmp(a_Buf, "const") ||
!strcmp(a_Buf, "string") || !strcmp(a_Buf, "int") ||
!strcmp(a_Buf, "mobj") || !strcmp(a_Buf, "float") ||
!strcmp(a_Buf, "fixed"))
return TVMTT_DECLARE;
// Some kind of integer
else if (*p >= '0' && *p <= '9')
{
// Count number of decimal points
for (i = 0, p += 1; *p; p++)
if (*p == '.')
i++;
// Contains non-numbers
else if (*p < '0' || *p > '9')
return TVMTT_INVALID;
// If 1, return fixed
if (i == 1)
return TVMTT_FIXED;
// More than 1, is illegal
else if (i > 1)
return TVMTT_INVALID;
// Otherwise, is an integer
return TVMTT_INTEGER;
}
// Identifier
else if (*p == '_' || (*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z'))
{
// Make sure the rest is valid
for (p += 1; *p; p++)
if (!(*p == '_' || (*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || (*p >= '0' && *p <= '9')))
return TVMTT_INVALID;
// Return as identifier
return TVMTT_IDENTIFIER;
}
// String
else if (*p == '\"')
return TVMTT_STRING;
// Symbol
else
return TVMTT_SYMBOL;
/* Failure */
return TVMTT_INVALID;
}
/* TVMS_AppendToken() -- Appends token to chain */
static TVM_TokenChain_t* TVMS_AppendToken(TVM_TokenChain_t* const a_Chain, const char* const a_Token)
{
TVM_TokenChain_t* New, *Last;
/* No chain */
if (!a_Chain)
Last = NULL;
else
// Go to last bit
for (Last = a_Chain; Last->Next; Last = Last->Next)
;
/* Allocate New */
New = Z_Malloc(sizeof(*New), PU_STATIC, NULL);
/* Setup based on last */
// It is around
if (Last)
{
Last->Next = New;
New->Prev = Last;
New->First = Last->First;
}
// Not around
else
{
New->First = New;
}
/* Setup Word */
New->Text = Z_StrDup(a_Token, PU_STATIC, NULL);
New->Type = TVMS_GetTokenType(New->Text);
/* Always return the first chain */
return New->First;
}
/* TVMS_ClearChain() -- Clears token chain */
static void TVMS_ClearChain(TVM_TokenChain_t* const a_Chain)
{
TVM_TokenChain_t* Rover, *Next;
/* Check */
if (!a_Chain)
return;
/* Clear everything */
Next = NULL;
for (Rover = a_Chain->First; Rover; Rover = Next)
{
// Remember Next
Next = Rover->Next;
// Delete
Z_Free(Rover->Text);
Z_Free(Rover);
}
}
/* TVMS_PutBuffer() -- Puts text into buffer */
static void TVMS_PutBuffer(char** const a_BufRef, size_t* const a_SizeRef, size_t* const a_MaxRef, const char a_Char)
{
#define GROWSIZE 32
/* No buffer? */
if (!*a_BufRef)
{
*a_BufRef = Z_Malloc(sizeof(**a_BufRef) * GROWSIZE, PU_STATIC, NULL);
*a_MaxRef = GROWSIZE;
}
/* Too small? */
else if (*a_SizeRef >= *a_MaxRef - 2)
{
Z_ResizeArray((void**)a_BufRef, sizeof(**a_BufRef),
*a_MaxRef, *a_MaxRef + GROWSIZE);
*a_MaxRef += GROWSIZE;
}
/* Place */
(*a_BufRef)[(*a_SizeRef)++] = a_Char;
#undef GROWSIZE
}
/* TVMS_PushScope() -- Pushes new scope to the VM */
static bool_t TVMS_PushScope(struct TVM_State_s* const a_State, const char* const a_Name, const uint32_t a_Flags)
{
CONL_PrintF(">> PUSH %s (%08x)\n", a_Name, a_Flags);
a_State->ScopeDepth++;
return true;
}
/* TVMS_PopScope() -- Pops the last scope from the VM */
static bool_t TVMS_PopScope(struct TVM_State_s* const a_State, char* const a_Dest, const size_t a_Len)
{
CONL_PrintF("<< POP\n");
a_State->ScopeDepth--;
return true;
}
/* TVMS_CreateVar() -- Creates a variable */
static bool_t TVMS_CreateVar(struct TVM_State_s* const a_State, char* const a_Type, const char* const a_Name, TVM_CCVarInfo_t** const a_Info)
{
/* Check */
if (!a_Info)
{
a_State->ErrorStr = "No info passed";
return false;
}
CONL_PrintF("?? Declare `%s` as type `%s`\n", a_Name, a_Type);
return true;
}
/* TVMS_LSEval() -- Evaluation of LegacyScript expression */
static bool_t TVMS_LSEval(struct TVM_State_s* const a_State, TVM_TokenChain_t** a_List, int32_t a_Count, int32_t a_Start, int32_t a_End, TVM_CCVarInfo_t* const a_PlaceHere)
{
int32_t i;
/* Debug */
for (i = a_Start; i <= a_End; i++)
CONL_PrintF("~%s ", a_List[i]->Text);
CONL_PrintF("\n");
/* Possible Parenthesis that are not needed */
// TODO FIXME
return false;
}
/* TVMS_SolveExpr() -- Solves an expression */
// This is a mostly direct port of t_parse.c evaluate_expression() and friends.
// This is just for script compatibility (since we do not want all the Legacy mods
// to break into pieces).
static bool_t TVMS_SolveExpr(struct TVM_State_s* const a_State, TVM_TokenChain_t* const a_Chain)
{
TVM_TokenChain_t* Rover;
TVM_TokenChain_t** List;
TVM_TokenChain_t* DeclareAs;
int32_t Count;
bool_t RetVal;
TVM_CCVarInfo_t* Var;
/* Convert expression to list */
// Init
List = NULL;
Count = 0;
DeclareAs = NULL;
Var = NULL;
// Rove and append to list
for (Rover = a_Chain; Rover; Rover = Rover->Next)
{
// Current Token is a reserved variable word
if (Rover->Type == TVMTT_DECLARE)
{
// Doubly declared?
if (DeclareAs)
{
if (List)
Z_Free(List);
a_State->ErrorStr = "It is illegal to declare a variable named after a reserved word.";
return false;
}
DeclareAs = Rover;
}
// Grab declare
else
{
// Declaring?
if (DeclareAs)
{
// Not an indentifier
if (Rover->Type != TVMTT_IDENTIFIER)
{
if (List)
Z_Free(List);
a_State->ErrorStr = "Variable name is not a valid identifier.";
return false;
}
// Create new variable
if (!TVMS_CreateVar(a_State, DeclareAs->Text, Rover->Text, &Var))
{
if (List)
Z_Free(List);
return false;
}
// Not declaring anymore
DeclareAs = NULL;
}
// Reallocate List
Z_ResizeArray((void**)&List, sizeof(*List), Count, Count + 1);
// Set current
List[Count++] = Rover;
}
}
/* Call sub handler */
RetVal = TVMS_LSEval(a_State, List, Count, 0, Count - 1, Var);
/* Cleanup */
if (List)
Z_Free(List);
return RetVal;
}
bool_t TVMS_CompileWLESIntExt(TVM_State_t* const a_State, WL_ES_t* const a_Stream, const uint32_t a_End);
/* TVMS_PM_Include() -- Include directive */
static bool_t TVMS_PM_Include(struct TVM_State_s* const a_State, char* const a_Buf, const size_t a_Len)
{
WL_ES_t* IncStr;
bool_t RetVal;
size_t i;
/* Based on inclusion state... */
switch (a_State->ReadInclude)
{
// Expect '('
case 0:
if (strcmp(a_Buf, "("))
{
a_State->ErrorStr = "Expected \'(\' after include.";
return false;
}
a_State->ReadInclude++;
return true;
// Expect string, containing said include
case 1:
// Check for ", which deems an include
if (a_Buf[0] != '\"')
{
a_State->ErrorStr = "Expected quoted string in include.";
return false;
}
// Cheat Buffer (remove quotes)
memmove(&a_Buf[0], &a_Buf[1], a_Len);
a_Buf[a_Len - 2] = 0;
// Locate Include
a_State->IncludeThis = WL_FindEntry(NULL, 0, a_Buf);
// Check if it exists
if (!a_State->IncludeThis)
{
a_State->ErrorStr = "Include lump not found.";
return false;
}
// Move on
a_State->ReadInclude++;
return true;
// Expect ')'
case 2:
if (strcmp(a_Buf, ")"))
{
a_State->ErrorStr = "Expected \')\' after include.";
return false;
}
a_State->ReadInclude++;
return true;
// Expect ';'
case 3:
if (strcmp(a_Buf, ";"))
{
a_State->ErrorStr = "Expected \';\' at end of include statement.";
return false;
}
// Make sure it is not on the stack
for (i = 0; i < a_State->NumIncs; i++)
if (a_State->Incs[i] == a_State->IncludeThis)
{
a_State->ErrorStr = "Recursive include detected.";
return false;
}
// Push to stack
Z_ResizeArray((void**)&a_State->Incs, sizeof(*a_State->Incs),
a_State->NumIncs, a_State->NumIncs + 1);
a_State->Incs[a_State->NumIncs++] = a_State->IncludeThis;
// Return state to normal
a_State->ParseMode = a_State->Confused;
// Parse said include now, using whatever it returns
IncStr = WL_StreamOpen(a_State->IncludeThis);
RetVal = TVMS_CompileWLESIntExt(a_State, IncStr, a_State->IncludeThis->Size);
WL_StreamClose(IncStr);
// Remove from stack
a_State->Incs[a_State->NumIncs - 1] = NULL;
Z_ResizeArray((void**)&a_State->Incs, sizeof(*a_State->Incs),
a_State->NumIncs, a_State->NumIncs - 1);
a_State->NumIncs--;
// Change message?
if (!RetVal)
a_State->ErrorStr = "Error in include file.";
return RetVal;
}
/* Oops? */
return false;
}
/* TVMS_PM_Cond() -- Conditional */
static bool_t TVMS_PM_Cond(struct TVM_State_s* const a_State, char* const a_Buf, const size_t a_Len)
{
int32_t OldStack;
/* First read */
if (a_State->ReadCond++ == 0)
{
// Not a '('?
if (strcmp(a_Buf, "("))
{
a_State->ErrorStr = "Expected \'(\' after conditional statement.";
return false;
}
// Increase parenthesis stack
a_State->CondStack++;
return true;
}
/* Possibly modify stack count */
OldStack = a_State->CondStack;
// Increase in stack
if (!strcmp(a_Buf, "("))
a_State->CondStack++;
// Decrease in stack
else if (!strcmp(a_Buf, ")"))
{
a_State->CondStack--;
// End of if statement?
if (a_State->CondStack <= 0)
{
// TODO FIXME
a_State->ErrorStr = "End-cond TODO FIXME";
return false;
}
}
// TODO FIXME
a_State->ErrorStr = "Conditional TODO FIXME";
return false;
}
/* TVMS_PM_Script() -- Script */
static bool_t TVMS_PM_Script(struct TVM_State_s* const a_State, char* const a_Buf, const size_t a_Len)
{
#define BUFSIZE 32
char Buf[BUFSIZE];
TVM_TokenType_t TokType;
int32_t IntVal;
/* Reading first, integer */
if (a_State->ReadScript == 0)
{
// Obtain type of token
TokType = TVMS_GetTokenType(a_Buf);
// Not an integer?
if (TokType != TVMTT_INTEGER)
{
a_State->ErrorStr = "Script IDs are integers only.";
return false;
}
// Obtain integer value
IntVal = C_strtoi32(a_Buf, NULL, 10);
// Out of range?
if (IntVal < 0 || IntVal >= 255)
{
a_State->ErrorStr = "Script IDs may only be within 0 - 255 inclusive.";
return false;
}
// Set ID, and wait for next token
a_State->ScriptID = IntVal;
a_State->ReadScript++;
return true;
}
/* Read curly brace */
else if (a_State->ReadScript == 1)
{
// Expect '{'
if (strcmp("{", a_Buf))
{
a_State->ErrorStr = "Expected '{' after script ID.";
return false;
}
// Emit warnings in global namespace
if (a_State->NameSpace == TVMNS_GLOBAL)
CONL_OutputUT(CT_SCRIPTING, DSTR_TVMC_GLOBALSCRIPTNUM, "%u\n", (uint8_t)a_State->ScriptID);
// Setup script name
memset(Buf, 0, sizeof(Buf));
snprintf(Buf, BUFSIZE, "@___script_%02x", (uint8_t)a_State->ScriptID);
// Push it
TVMS_PushScope(a_State, Buf, TVMSF_EXTERN);
// Return to confused state
a_State->ParseMode = a_State->Confused;
// Success!
return true;
}
/* Failure */
return false;
#undef BUFSIZE
}
/* TVMS_PM_Confused() -- Confused state (default) */
static bool_t TVMS_PM_Confused(struct TVM_State_s* const a_State, char* const a_Buf, const size_t a_Len)
{
#define BUFSIZE 32
char Buf[BUFSIZE];
bool_t SolveOK;
/* Special keywords */
// Include
if (!strcmp("include", a_Buf))
{
// Start parsing include
a_State->ReadInclude = 0;
a_State->ParseMode = TVMS_PM_Include;
return true;
}
// Script
else if (!strcmp("script", a_Buf))
{
// Make sure no tokens were read
if (a_State->TokChain)
{
a_State->ErrorStr = "Missing \';\' before \'}\'.";
return false;
}
// Disallow scripts inside of a scope
if (a_State->ScopeDepth > 0)
{
a_State->ErrorStr = "Scripts may only exist at the top level block.";
return false;
}
// Start parsing script
a_State->ReadScript = 0;
a_State->ParseMode = TVMS_PM_Script;
// Define a new code point
return true;
}
// if/while/for/else
else if (!strcmp("if", a_Buf) || !strcmp("while", a_Buf) || !strcmp("for", a_Buf) || !strcmp("else", a_Buf))
{
// Make sure no tokens were read
if (a_State->TokChain)
{
a_State->ErrorStr = "Missing \';\' before \'}\'.";
return false;
}
// Switch to conditional
a_State->CondType = a_Buf[0];
a_State->ReadCond = 0;
a_State->CondStack = 0;
a_State->ParseMode = TVMS_PM_Cond;
// Continue
return true;
}
// Start of scope
else if (!strcmp("{", a_Buf))
{
// Make sure no tokens were read
if (a_State->TokChain)
{
a_State->ErrorStr = "Missing \';\' before \'}\'.";
return false;
}
// Setup scope name
memset(Buf, 0, sizeof(Buf));
snprintf(Buf, BUFSIZE, "@___scope_%08x", (uint32_t)(++a_State->ScopeIdent));
// Push it -- but it is NOT external (not jumpable from remote)
TVMS_PushScope(a_State, Buf, 0);
}
// End of scope
else if (!strcmp("}", a_Buf))
{
// Make sure no tokens were read
if (a_State->TokChain)
{
a_State->ErrorStr = "Missing \';\' before \'}\'.";
return false;
}
// Already at zero scope?
if (a_State->ScopeDepth == 0)
{
a_State->ErrorStr = "Extra \'}\', causes negative scope depth.";
return false;
}
// Fall out of scope
TVMS_PopScope(a_State, NULL, 0);
}
/* Parse statements into code */
// If ; was read, this is an end of a statement
else if (!strcmp(";", a_Buf))
{
// Solve expression if tokens waiting
if (a_State->TokChain)
{
// Attempt it
a_State->ErrorStr = NULL;
SolveOK = TVMS_SolveExpr(a_State, a_State->TokChain);
// Clear chain
TVMS_ClearChain(a_State->TokChain);
a_State->TokChain = NULL;
// Failed?
if (!SolveOK)
{
// Change string if not any
if (!a_State->ErrorStr)
a_State->ErrorStr = "Invalid expression.";
return false;
}
}
}
// Otherwise, append to the chain
else
{
// Append
a_State->TokChain = TVMS_AppendToken(a_State->TokChain, a_Buf);
}
/* Success! */
return true;
#undef BUFSIZE
}
/* TVMS_ParseToken() -- Parses token into the state */
static bool_t TVMS_ParseToken(TVM_State_t* const a_State, char* const a_Buf, const size_t a_Len)
{
//CONL_PrintF("(%i)`%s`, ", (int)a_Len, a_Buf);
return a_State->ParseMode(a_State, a_Buf, a_Len);
}
/* TVMS_CompileLine() -- Compiles a line (tokenizes it) */
// In Legacy Script, tokens are either identifiers, strings, numbers, or symbols.
static bool_t TVMS_CompileLine(TVM_State_t* const a_State, char* const a_Buf, const size_t a_Len)
{
char* Token, *p, *ep;
size_t TokAt, TokSz;
int8_t Type;
bool_t SendAway, Escape;
bool_t RetVal;
/* Init */
Token = NULL;
TokAt = TokSz = 0;
Type = -1;
SendAway = false;
Escape = false;
RetVal = true;
/* Read characters */
for (p = a_Buf, ep = a_Buf + a_Len; p < ep;)
{
// Initialize Token State
if (!Token)
{
// Ignore whitespace
if (TVMS_IsSpace(*p))
{
p++;
continue;
}
// Number
if ((*p >= '0' && *p <= '9') || *p == '.')
Type = 1;
// Identifier
else if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || *p == '_')
Type = 2;
// String
else if (*p == '\"')
Type = 3;
// Symbol
else
Type = 4;
// Add to token
TVMS_PutBuffer(&Token, &TokAt, &TokSz, *(p++));
continue;
}
// Determne if token ends
// Number
if (Type == 1)
{
if (!((*p >= '0' && *p <= '9') || *p == '.'))
SendAway = true;
}
// Identifier
else if (Type == 2)
{
if (!((*p >= 'a' && *p <= 'z') ||
(*p >= 'A' && *p <= 'Z') ||
(*p >= '0' && *p <= '9') || *p == '_'))
SendAway = true;
}
// String
else if (Type == 3)
{
// Not escaped and quoted?
if (!Escape && *p == '\"')
{
TVMS_PutBuffer(&Token, &TokAt, &TokSz, *(p++));
SendAway = true;
}
// Escape?
Escape = false;
if (*p == '\\')
Escape = true;
}
// Symbol
else
{
// Double symbol?
if (*p != '(' && *p != ')')
if (*p == '=' || *p == Token[TokAt - 1])
TVMS_PutBuffer(&Token, &TokAt, &TokSz, *(p++));
// Always send away
SendAway = true;
}
// Send away?
if (SendAway)
{
// Call token handler thing
if (!TVMS_ParseToken(a_State, Token, TokAt))
RetVal = false;
// Re-initialize token bits
Z_Free(Token);
Token = NULL;
TokAt = TokSz = 0;
Type = -1;
SendAway = false;
// Failed?
if (!RetVal)
break;
}
// Otherwise add to token
else
TVMS_PutBuffer(&Token, &TokAt, &TokSz, *(p++));
}
/* A token remains */
if (Token)
{
if (!TVMS_ParseToken(a_State, Token, TokAt))
RetVal = false;
// Free it
Z_Free(Token);
}
/* It worked? */
return RetVal;
}
/* TVMS_CompileWLESInt() -- Compiles stream segment */
// This is based off my C99 preprocessor, at least the tokenization part.
// However compared to C, this is alot smaller and alot faster too.
// It already is fast, but since Legacy Script lacks so many things there's no
// code to execute.
// So compared to C, there is no...
// * Trigraphs
// * Multi-line Comments
// * Escaped Newlines
static bool_t TVMS_CompileWLESInt(TVM_State_t* const a_State, WL_ES_t* const a_Stream, const uint32_t a_End)
{
#define EATCHAR {ReadLeft--; memmove(&ReadBuf[0], &ReadBuf[1], sizeof(*ReadBuf) * (READSIZE - 1));}
#define READSIZE 32
char ReadBuf[READSIZE];
uint32_t ReadLeft;
uint16_t Mode;
uint32_t LineNum;
char* Token;
size_t TokAt, TokSz;
/* Check */
if (!a_Stream || !a_End)
return false;
/* Message */
CONL_OutputUT(CT_SCRIPTING, DSTR_TVMC_COMPILING, "%s\n", WL_StreamGetEntry(a_Stream)->Name);
/* Initialize */
ReadLeft = Mode = TokAt = 0;
memset(ReadBuf, 0, sizeof(ReadBuf));
Token = NULL;
LineNum = 1;
/* Tokenization Loop */
while ((!WL_StreamEOF(a_Stream) && WL_StreamTell(a_Stream) < a_End) || ReadLeft > 0)
{
// Read Into Buffer
while (ReadLeft < READSIZE - 1)
if (!WL_StreamEOF(a_Stream) && WL_StreamTell(a_Stream) < a_End)
ReadBuf[ReadLeft++] = WL_Src(a_Stream);
else
{
ReadBuf[ReadLeft] = 0;
break;
}
// Nothing read?
if (ReadLeft == 0)
break;
// If not in quotes, transform whitespace to space
if ((Mode & TVMPP_QUOTE) == 0)
if (TVMS_IsSpace(ReadBuf[0]))
ReadBuf[0] = ' ';
// If not in quotes and multiple spaces...
if ((Mode & TVMPP_QUOTE) == 0)
while (ReadBuf[0] == ' ' && ReadBuf[1] == ' ')
EATCHAR;
// Detect Comment
if ((Mode & TVMPP_QUOTE) == 0)
if (ReadBuf[0] == '/' && ReadBuf[1] == '/')
{
// Set as comment mode
Mode |= TVMPP_COMMENT;
// Eat two characters and move on
EATCHAR;
EATCHAR;
continue;
}
// Detect Quotes
if ((Mode & TVMPP_COMMENT) == 0)
if ((Mode & TVMPP_ESCAPE) == 0)
if (ReadBuf[0] == '\"')
Mode ^= TVMPP_QUOTE;
// Handle Newline
if (ReadBuf[0] == '\n')
{
// Eat newline
EATCHAR;
// Increase Line Number
LineNum++;
// Clear comment and escapedness, if any
Mode &= ~(TVMPP_COMMENT | TVMPP_ESCAPE);
// Send to tokenizer
if (Token && TokAt > 0)
if (!TVMS_CompileLine(a_State, Token, TokAt))
{
CONL_OutputUT(CT_SCRIPTING, DSTR_TVMC_ERROR, "%u%s\n", LineNum, a_State->ErrorStr);
// Free token before death
if (Token)
Z_Free(Token);
return false;
}
// Clear Token
if (Token)
Z_Free(Token);
Token = NULL;
TokAt = TokSz = 0;
// Continue on
continue;
}
// Escape sequence
Mode &= ~TVMPP_ESCAPE;
if (ReadBuf[0] == '\\')
Mode |= TVMPP_ESCAPE;
// Single-line comments, ignore the remaining
if (Mode & TVMPP_COMMENT)
{
EATCHAR;
continue;
}
// Otherwise append to token buffer
TVMS_PutBuffer(&Token, &TokAt, &TokSz, ReadBuf[0]);
// Eat character
EATCHAR;
}
/* Clear Token */
if (Token)
Z_Free(Token);
#undef READSIZE
#undef EATCHAR
/* Success! */
return true;
}
/* TVMS_CompileWLESIntExt() -- Externalized internal call */
bool_t TVMS_CompileWLESIntExt(TVM_State_t* const a_State, WL_ES_t* const a_Stream, const uint32_t a_End)
{
return TVMS_CompileWLESInt(a_State, a_Stream, a_End);
}
/* TVMS_Cleanup() -- Cleans up */
static void TVMS_Cleanup(TVM_State_t* const a_State)
{
// Left over includes?
if (a_State->Incs)
{
Z_Free(a_State->Incs);
a_State->Incs = NULL;
a_State->NumIncs = 0;
}
// Left over tokens?
if (a_State->TokChain)
TVMS_ClearChain(a_State->TokChain);
}
/* TVM_CompileWLES() -- Wraps internal */
void TVM_CompileWLES(const TVM_Namespace_t a_NameSpace, WL_ES_t* const a_Stream, const uint32_t a_End)
{
TVM_State_t State;
/* Check valid namespace */
if (a_NameSpace < 0 || a_NameSpace >= NUMTVMNAMESPACES)
return;
/* Clear */
memset(&State, 0, sizeof(State));
/* Init for new compile stage */
State.NameSpace = a_NameSpace;
State.ParseMode = TVMS_PM_Confused;
State.Confused = TVMS_PM_Confused;
State.ScopeDepth = 0;
/* Start compilation */
if (TVMS_CompileWLESInt(&State, a_Stream, a_End))
CONL_OutputUT(CT_SCRIPTING, DSTR_TVMC_SUCCESS, "%s\n", WL_StreamGetEntry(a_Stream)->Name);
/* Cleanup */
TVMS_Cleanup(&State);
}
/* TVM_Clear() -- Clears the VM */
void TVM_Clear(const TVM_Namespace_t a_NameSpace)
{
#if 0
/* Check namespace */
if (a_NameSpace < 0 || a_NameSpace >= NUMTVMNAMESPACES)
return;
/* Reset scope identity */
l_VMState[a_NameSpace].ScopeIdent = 1;
/* Cleanup */
TVMS_Cleanup(&l_VMState[a_NameSpace]);
#endif
}
/* TVM_CompileEntry() -- Compiles entry */
void TVM_CompileEntry(const TVM_Namespace_t a_NameSpace, const WL_WADEntry_t* const a_Entry)
{
WL_ES_t* ScriptStream;
/* Open stream */
ScriptStream = WL_StreamOpen(a_Entry);
/* If it was created */
if (ScriptStream)
{
// Check Unicode viability
WL_StreamCheckUnicode(ScriptStream);
// Compile it
TVM_CompileWLES(a_NameSpace, ScriptStream, a_Entry->Size);
// Close it
WL_StreamClose(ScriptStream);
}
}