|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
| 1 | +using System.Collections.Generic; |
3 | 2 | using SourcepawnCondenser.SourcemodDefinition; |
4 | 3 | using SourcepawnCondenser.Tokenizer; |
5 | 4 |
|
6 | 5 | namespace SourcepawnCondenser |
7 | 6 | { |
8 | 7 | public partial class Condenser |
9 | 8 | { |
10 | | - private static string[] baseTypes = {"bool", "char", "float", "int", "any", "Handle"}; |
11 | | - |
12 | 9 | private int ConsumeSMVariable() |
13 | 10 | { |
14 | 11 | if (position + 3 < length) |
15 | 12 | { |
16 | 13 | var startIndex = t[position].Index; |
17 | 14 |
|
18 | 15 | 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; |
24 | 20 |
|
| 21 | + // Dynamic array: "char[] x = new char[64];" |
25 | 22 |
|
26 | | - // Simple var match: "int x;" |
27 | | - if (t[position + 2].Kind == TokenKind.Semicolon) |
| 23 | + if (t[position + 1].Kind == TokenKind.Character) |
28 | 24 | { |
29 | | - def.Variables.Add(new SMVariable |
| 25 | + for (var i = 1; i < length; i += 2) |
30 | 26 | { |
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; |
36 | 30 |
|
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) |
40 | 47 | { |
| 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): |
41 | 93 | def.Variables.Add(new SMVariable |
42 | 94 | { |
43 | 95 | Index = startIndex, Length = t[position + 4].Index - startIndex, File = FileName, |
44 | 96 | Name = varName, Type = varType, Value = t[position + 3].Value |
45 | 97 | }); |
46 | 98 | return position + 4; |
47 | | - } |
| 99 | + } |
48 | 100 |
|
49 | 101 |
|
50 | 102 | // Array declaration match: "int x[3][3]...;" |
51 | | - |
52 | | - var size = new List<string>(); |
53 | | - var dimensions = 0; |
54 | | - var index = -1; |
55 | 103 | var requireAssign = false; |
56 | 104 |
|
57 | 105 | for (var i = 2; i < length;) |
|
0 commit comments