Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit 86ef00a

Browse files
committed
Implement dynamic size variable match
1 parent 5bf42fa commit 86ef00a

File tree

1 file changed

+73
-25
lines changed

1 file changed

+73
-25
lines changed

SourcepawnCondenser/SourcepawnCondenser/CondenserFunctions/SMVariableConsumer.cs

Lines changed: 73 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,105 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using SourcepawnCondenser.SourcemodDefinition;
43
using SourcepawnCondenser.Tokenizer;
54

65
namespace SourcepawnCondenser
76
{
87
public partial class Condenser
98
{
10-
private static string[] baseTypes = {"bool", "char", "float", "int", "any", "Handle"};
11-
129
private int ConsumeSMVariable()
1310
{
1411
if (position + 3 < length)
1512
{
1613
var startIndex = t[position].Index;
1714

1815
var varType = t[position].Value;
19-
var varName = t[position + 1].Value;
20-
var varValue = string.Empty;
21-
22-
if (t[position + 1].Kind != TokenKind.Identifier) //TODO: Add dynamic array match
23-
return -1;
16+
string varName;
17+
var size = new List<string>();
18+
var dimensions = 0;
19+
var index = -1;
2420

21+
// Dynamic array: "char[] x = new char[64];"
2522

26-
// Simple var match: "int x;"
27-
if (t[position + 2].Kind == TokenKind.Semicolon)
23+
if (t[position + 1].Kind == TokenKind.Character)
2824
{
29-
def.Variables.Add(new SMVariable
25+
for (var i = 1; i < length; i += 2)
3026
{
31-
Index = startIndex, Length = t[position + 2].Index - startIndex, File = FileName,
32-
Name = varName, Type = varType
33-
});
34-
return position + 2;
35-
}
27+
index = i;
28+
if (t[position + i].Kind == TokenKind.Identifier)
29+
break;
3630

37-
// Assign var match: "int x = 5"
38-
if (t[position + 2].Kind == TokenKind.Assignment && t[position + 4].Kind == TokenKind.Semicolon)
39-
if (t[position + 3].Kind == TokenKind.Number || t[position + 3].Kind == TokenKind.Quote)
31+
if (t[position + i].Value == "[" && t[position + i + 1].Value == "]")
32+
{
33+
dimensions++;
34+
continue;
35+
}
36+
37+
return -1;
38+
}
39+
40+
varName = t[position + index].Value;
41+
42+
if (t[position + index + 1].Kind != TokenKind.Assignment ||
43+
t[position + index + 2].Kind != TokenKind.New || t[position + index + 3].Value != varType)
44+
return -1;
45+
46+
for (var i = index + 4; i < length - 2; i += 3)
4047
{
48+
if (t[position + i].Kind == TokenKind.Semicolon)
49+
{
50+
def.Variables.Add(new SMVariable
51+
{
52+
Index = startIndex, Length = t[position + i].Index - startIndex,
53+
File = FileName,
54+
Name = varName, Type = varType, Dimensions = dimensions, Size = size,
55+
});
56+
return position + i;
57+
}
58+
59+
if (t[position + i].Value == "[" &&
60+
(t[position + i + 1].Kind == TokenKind.Number ||
61+
t[position + i + 1].Kind == TokenKind.Identifier) &&
62+
t[position + i + 2].Value == "]")
63+
{
64+
size.Add(t[position + i + 1].Value);
65+
continue;
66+
}
67+
68+
return -1;
69+
}
70+
71+
return -1;
72+
}
73+
74+
if (t[position + 1].Kind != TokenKind.Identifier)
75+
return -1;
76+
77+
varName = t[position + 1].Value;
78+
79+
switch (t[position + 2].Kind)
80+
{
81+
// Simple var match: "int x;"
82+
case TokenKind.Semicolon:
83+
def.Variables.Add(new SMVariable
84+
{
85+
Index = startIndex, Length = t[position + 2].Index - startIndex, File = FileName,
86+
Name = varName, Type = varType
87+
});
88+
return position + 2;
89+
// Assign var match: "int x = 5"
90+
case TokenKind.Assignment when t[position + 4].Kind == TokenKind.Semicolon &&
91+
(t[position + 3].Kind == TokenKind.Number ||
92+
t[position + 3].Kind == TokenKind.Quote):
4193
def.Variables.Add(new SMVariable
4294
{
4395
Index = startIndex, Length = t[position + 4].Index - startIndex, File = FileName,
4496
Name = varName, Type = varType, Value = t[position + 3].Value
4597
});
4698
return position + 4;
47-
}
99+
}
48100

49101

50102
// Array declaration match: "int x[3][3]...;"
51-
52-
var size = new List<string>();
53-
var dimensions = 0;
54-
var index = -1;
55103
var requireAssign = false;
56104

57105
for (var i = 2; i < length;)

0 commit comments

Comments
 (0)