diff options
-rw-r--r-- | OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/LSL/LSL2CSConverter.cs | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/LSL/LSL2CSConverter.cs b/OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/LSL/LSL2CSConverter.cs new file mode 100644 index 0000000..b302274 --- /dev/null +++ b/OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/LSL/LSL2CSConverter.cs | |||
@@ -0,0 +1,156 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Text; | ||
4 | using System.Text.RegularExpressions; | ||
5 | |||
6 | namespace LSL2CS.Converter | ||
7 | { | ||
8 | public class LSL2CSConverter | ||
9 | { | ||
10 | //private Regex rnw = new Regex(@"[a-zA-Z0-9_\-]", RegexOptions.Compiled); | ||
11 | private Dictionary<string, string> DataTypes = new Dictionary<string, string>(); | ||
12 | private Dictionary<string, string> QUOTES = new Dictionary<string, string>(); | ||
13 | |||
14 | public LSL2CSConverter() | ||
15 | { | ||
16 | DataTypes.Add("void", "void"); | ||
17 | DataTypes.Add("integer", "int"); | ||
18 | DataTypes.Add("float", "float"); | ||
19 | DataTypes.Add("string", "string"); | ||
20 | DataTypes.Add("key", "string"); | ||
21 | DataTypes.Add("vector", "vector"); | ||
22 | DataTypes.Add("rotation", "rotation"); | ||
23 | DataTypes.Add("list", "list"); | ||
24 | DataTypes.Add("null", "null"); | ||
25 | } | ||
26 | |||
27 | |||
28 | |||
29 | public string Convert(string Script) | ||
30 | { | ||
31 | string Return = ""; | ||
32 | |||
33 | // | ||
34 | // Prepare script for processing | ||
35 | // | ||
36 | |||
37 | // Here we will remove those linebreaks that are allowed in so many languages. | ||
38 | // One hard-ass regex should do the job. | ||
39 | |||
40 | // Then we will add some linebreaks. | ||
41 | Script = Regex.Replace(Script, @"\r\n", "\n"); | ||
42 | Script = Regex.Replace(Script, @"\n", "\r\n"); | ||
43 | //Script = Regex.Replace(Script, @"\n\s*{", @"{"); | ||
44 | //Script = Regex.Replace(Script, @"[\r\n]", @""); | ||
45 | |||
46 | // Then we remove unwanted linebreaks | ||
47 | |||
48 | |||
49 | //// | ||
50 | //// Split up script to process line by line | ||
51 | //// | ||
52 | //int count = 0; | ||
53 | //string line; | ||
54 | //Regex r = new Regex("\r?\n", RegexOptions.Multiline | RegexOptions.Compiled); | ||
55 | //string[] lines = r.Split(Script); | ||
56 | |||
57 | //for (int i = 0; i < lines.Length; i++) | ||
58 | //{ | ||
59 | // // Remove space in front and back | ||
60 | // line = lines[i].Trim(); | ||
61 | // count++; | ||
62 | // Return += count + ". " + translate(line) + "\r\n"; | ||
63 | //} | ||
64 | |||
65 | int level = 0; | ||
66 | bool Level0Set = false; | ||
67 | bool Level1Set = false; | ||
68 | |||
69 | |||
70 | // QUOTE REPLACEMENT | ||
71 | //char[] SA = Script.ToCharArray(); | ||
72 | string _Script = ""; | ||
73 | string C; | ||
74 | bool in_quote = false; | ||
75 | bool quote_replaced = false; | ||
76 | string quote_replacement_string = "Q_U_O_T_E_REPLACEMENT_"; | ||
77 | string quote = ""; | ||
78 | int quote_replaced_count = 0; | ||
79 | for (int p = 0; p < Script.Length; p++) | ||
80 | { | ||
81 | |||
82 | while (true) { | ||
83 | C = Script.Substring(p,1); | ||
84 | if (C == "\"") { | ||
85 | // Toggle inside/outside quote | ||
86 | in_quote = ! in_quote; | ||
87 | if(in_quote) { | ||
88 | quote_replaced_count++; | ||
89 | } else { | ||
90 | // We just left a quote | ||
91 | QUOTES.Add(quote_replacement_string + quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]), quote); | ||
92 | quote = ""; | ||
93 | } | ||
94 | break; | ||
95 | } | ||
96 | if (!in_quote) { | ||
97 | // We are not inside a quote | ||
98 | quote_replaced = false; | ||
99 | |||
100 | } else { | ||
101 | // We are inside a quote | ||
102 | if (!quote_replaced) { | ||
103 | // Replace quote | ||
104 | _Script += quote_replacement_string + quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]); | ||
105 | quote_replaced = true; | ||
106 | } | ||
107 | quote += C; | ||
108 | break; | ||
109 | } | ||
110 | _Script += C; | ||
111 | break; | ||
112 | } | ||
113 | } | ||
114 | // | ||
115 | // END OF QUOTE REPLACEMENT | ||
116 | // | ||
117 | |||
118 | // Replace CAST - (integer) with (int) | ||
119 | Script = Regex.Replace(_Script, @"\(integer\)", @"(int)", RegexOptions.Compiled | RegexOptions.Multiline); | ||
120 | // Replace return types and function variables - integer a() and f(integer a, integer a) | ||
121 | _Script = Regex.Replace(Script, @"(^|[\(,])(\s*int)eger(\s*)", @"$1$2$3", RegexOptions.Compiled | RegexOptions.Multiline); | ||
122 | |||
123 | // Add void to functions not having that | ||
124 | Script = Regex.Replace(_Script, @"^(\s*)((?!if|switch|for|foreach)[a-zA-Z0-9_]*\s*\([^\)]*\)[^;]*\{)", @"$1void $2", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline); | ||
125 | |||
126 | |||
127 | // Replace int (TEMPORARY QUICKFIX) | ||
128 | //_Script = Regex.Replace(Script, "integer", "int", RegexOptions.Compiled); | ||
129 | |||
130 | foreach (string key in QUOTES.Keys) | ||
131 | { | ||
132 | string val; | ||
133 | QUOTES.TryGetValue(key, out val); | ||
134 | _Script = Script.Replace(key, "\"" + val + "\""); | ||
135 | Script = _Script; | ||
136 | } | ||
137 | |||
138 | |||
139 | Return = Script; | ||
140 | |||
141 | return Return; | ||
142 | } | ||
143 | //private string GetWord(char[] SA, int p) | ||
144 | //{ | ||
145 | // string ret = ""; | ||
146 | // for (int i = p; p < SA.Length; i++) | ||
147 | // { | ||
148 | // if (!rnw.IsMatch(SA[i].ToString())) | ||
149 | // break; | ||
150 | // ret += SA[i]; | ||
151 | // } | ||
152 | // return ret; | ||
153 | //} | ||
154 | |||
155 | } | ||
156 | } | ||