8 using System.Text.RegularExpressions;
14 private const int HEADER_MAX = 100;
15 private const int SYNTAX_MAX = 100;
16 private string[,] SyntaxTable;
17 private string CSNameSpace =
"";
31 this.CSNameSpace = CSNameSpace;
34 SyntaxTable[0, 0] =
"generic";
35 SyntaxTable[0, 1] =
"main";
36 SyntaxTable[0, 2] =
"#include";
37 SyntaxTable[0, 3] =
"int";
38 SyntaxTable[0, 4] =
"float";
39 SyntaxTable[0, 5] =
"double";
40 SyntaxTable[0, 6] =
"char";
41 SyntaxTable[0, 7] =
"void";
42 SyntaxTable[0, 8] =
"NULL";
61 while (SyntaxTable[i, 0] != null)
72 while (SyntaxTable[IndexB, i] != null)
99 SyntaxTable[fileIndex, 0] =
"generic";
100 SyntaxTable[fileIndex, 1] =
"main";
101 SyntaxTable[fileIndex, 2] =
"#include";
102 SyntaxTable[fileIndex, 3] =
"int";
103 SyntaxTable[fileIndex, 4] =
"float";
104 SyntaxTable[fileIndex, 5] =
"double";
105 SyntaxTable[fileIndex, 6] =
"char";
106 SyntaxTable[fileIndex, 7] =
"void";
107 SyntaxTable[fileIndex, 8] =
"NULL";
116 switch (SyntaxMatchNumber)
123 string SyntaxPattern =
@"(^|[^\w])([a-zA-Z]+)(\s+)(main)(.*)";
124 string Syntax_Main =
"";
125 if (Regex.IsMatch(codeLine, SyntaxPattern))
127 Syntax_Main = Regex.Replace(codeLine, SyntaxPattern, main =>
"\t\tstatic " + main.Groups[1].Value +
128 main.Groups[2].Value +
" Main(string[] args)");
131 codeLine =
"namespace " + CSNameSpace +
System.Environment.NewLine;
132 codeLine +=
"{" +
System.Environment.NewLine;
133 codeLine +=
"\tclass Program" +
System.Environment.NewLine;
134 codeLine +=
"\t{" +
System.Environment.NewLine;
135 codeLine += Syntax_Main;
144 Match match = Regex.Match(codeLine,
@"(^|[^\w])<stdio.h>([^\w]|$)");
149 codeLine =
@"using System;" +
System.Environment.NewLine;
150 codeLine +=
@"using System.Collections.Generic;" +
System.Environment.NewLine;
151 codeLine +=
@"using System.Text;";
158 match = Regex.Match(codeLine,
@"(^|[^\w])<stdlib.h>([^\w]|$)");
169 match = Regex.Match(codeLine,
@"(^|[^\w])<string.h>([^\w]|$)");
183 string patternCharArray =
@"(^|[^\w])(char)(\s+)(\w+)(\[\d+\])";
184 if (Regex.IsMatch(codeLine, patternCharArray))
186 codeLine = Regex.Replace(codeLine, patternCharArray, main => main.Groups[1].Value +
"string" + main.Groups[3].Value + main.Groups[4].Value);
195 string patternNull =
@"(^|[^\w])(NULL)([^\w]|$)";
196 if (Regex.IsMatch(codeLine, patternNull))
198 codeLine = Regex.Replace(codeLine, patternNull, main => main.Groups[1].Value +
"null" + main.Groups[3].Value);
224 this.SyntaxTable[fileIndex, 0] =
"stdio.h";
225 this.SyntaxTable[fileIndex, 1] =
"printf";
226 this.SyntaxTable[fileIndex, 2] =
"gets";
227 this.SyntaxTable[fileIndex, 3] =
"FILE";
228 this.SyntaxTable[fileIndex, 4] =
"fopen";
229 this.SyntaxTable[fileIndex, 5] =
"fgets";
230 this.SyntaxTable[fileIndex, 6] =
"fclose";
231 this.SyntaxTable[fileIndex, 7] =
"fprintf";
239 switch (SyntaxMatchNumber)
246 string patternPrintf =
@"(^|[^\w])(printf)([^\w])";
247 Regex rNewLine =
new Regex(
@"(\\n)(\s*"")");
252 if (rNewLine.IsMatch(codeLine))
254 codeLine = Regex.Replace(codeLine, patternPrintf, m => m.Groups[1].Value +
@"System.Console.WriteLine" + m.Groups[3].Value);
255 codeLine = rNewLine.Replace(codeLine, m =>
"" + m.Groups[2].Value);
262 codeLine = Regex.Replace(codeLine, patternPrintf, m => m.Groups[1].Value +
@"System.Console.Write" + m.Groups[3].Value);
265 string patternFormat =
@"(""[^""^%]*)(%(\d)*([dDfFsSuUcC]))";
266 Match matchFormat = Regex.Match(codeLine, patternFormat);
269 while (matchFormat.Success)
271 if (matchFormat.Groups[3].Success)
272 codeLine = Regex.Replace(codeLine, patternFormat, m => m.Groups[1].Value +
"{" + i++ +
273 "," + matchFormat.Groups[3].Value +
":" + matchFormat.Groups[4].Value +
"}");
275 codeLine = Regex.Replace(codeLine, patternFormat, m => m.Groups[1].Value +
"{" + i++ +
276 ":" + matchFormat.Groups[4].Value +
"}");
277 matchFormat = Regex.Match(codeLine, patternFormat);
287 string patternGets =
@"(^|[^\w])(gets)(\s*)(\()(\w+)(\))";
288 if (Regex.IsMatch(codeLine, patternGets))
290 codeLine = Regex.Replace(codeLine, patternGets, m => m.Groups[1].Value + m.Groups[5].Value +
" = " +
291 @"System.Console.ReadLine()");
301 string patternFopen =
@"(^|[^\w])(fopen)([^\w])";
302 if (Regex.IsMatch(codeLine, patternFopen))
304 string patternFile =
@"(^|[^\w])(FILE)([^\w])";
305 codeLine = Regex.Replace(codeLine, patternFile, m =>
"");
319 string patternFopen1 =
@"(^|[^\w])(\w+)(\s*=\s*)(fopen)(\s*\([^,]*)(,\s*"")([a-zA-Z]+)(""\s*\))";
320 Match match = Regex.Match(codeLine, patternFopen1);
326 if (match.Groups[7].Value ==
"w")
328 codeLine = Regex.Replace(codeLine, patternFopen1, m => m.Groups[1].Value +
"System.IO.StreamWriter " + m.Groups[2].Value +
329 m.Groups[3].Value +
"new System.IO.StreamWriter" + m.Groups[5].Value +
")");
334 else if (match.Groups[7].Value ==
"r")
336 codeLine = Regex.Replace(codeLine, patternFopen1, m => m.Groups[1].Value +
"System.IO.StreamReader " + m.Groups[2].Value +
337 m.Groups[3].Value +
"new System.IO.StreamReader" + m.Groups[5].Value +
")");
348 string patternFgets =
@"(^|[^\w])(fgets)(\s*\()(\w+)(\s*,\s*[^,]+,\s*)(\w+)(\s*\)\s*)((!=)?(==)?)";
349 Match matchFgets = Regex.Match(codeLine, patternFgets);
350 if (matchFgets.Success)
352 if (matchFgets.Groups[8].Success)
354 codeLine = Regex.Replace(codeLine, patternFgets, m =>
"(" + m.Groups[1].Value + m.Groups[4].Value +
355 " = " + m.Groups[6].Value +
".ReadLine()" +
") " + m.Groups[8].Value);
359 codeLine = Regex.Replace(codeLine, patternFgets, m => m.Groups[1].Value + m.Groups[4].Value +
360 " = " + m.Groups[6].Value +
".ReadLine()");
371 string patternFclose =
@"(^|[^\w])(fclose)(\s*\()(\w+)(\s*\))";
372 if (Regex.IsMatch(codeLine, patternFclose))
374 codeLine = Regex.Replace(codeLine, patternFclose, m => m.Groups[1].Value + m.Groups[4].Value +
385 string patternFprintf =
@"(^|[^\w])(fprintf)(\s*\()(\w+)(,\s*)([^\)]*)";
386 Regex rNewLine1 =
new Regex(
@"(\\n)(\s*"")");
388 if (rNewLine1.IsMatch(codeLine))
390 codeLine = Regex.Replace(codeLine, patternFprintf, m => m.Groups[1].Value + m.Groups[4].Value +
391 ".WriteLine(" + m.Groups[6].Value);
392 codeLine = rNewLine1.Replace(codeLine, m =>
"" + m.Groups[2].Value);
396 codeLine = Regex.Replace(codeLine, patternFprintf, m => m.Groups[1].Value + m.Groups[4].Value +
397 ".Write(" + m.Groups[6].Value +
")");
424 this.SyntaxTable[fileIndex, 0] =
"stdlib.h";
425 this.SyntaxTable[fileIndex, 1] =
"atoi";
433 switch (SyntaxMatchNumber)
440 string patternAtoi =
@"(^|[^\w])(atoi)([^\w])";
442 if (Regex.IsMatch(codeLine, patternAtoi))
444 codeLine = Regex.Replace(codeLine, patternAtoi, m => m.Groups[1].Value +
"Int32.Parse" + m.Groups[3].Value);
471 SyntaxTable[fileIndex, 0] =
"string.h";
472 SyntaxTable[fileIndex, 1] =
"strlen";
480 switch (SyntaxMatchNumber)
487 string SyntaxPattern =
@"(^|[^\w])(strlen\s*\(\s*)([^)|^\s]+)(\s*\))";
489 if (Regex.IsMatch(codeLine, SyntaxPattern))
491 codeLine = Regex.Replace(codeLine, SyntaxPattern, m => m.Groups[1].Value + m.Groups[3].Value +
".Length");
513 bool SyntaxMainScan =
false;
519 for (
int IndexHeader = 0; IndexHeader < IndexMaxA; ++IndexHeader)
525 for (
int IndexSyntax = 1; IndexSyntax < IndexMaxB; ++IndexSyntax)
530 Match SYNTAX = Regex.Match(codeLine,
@"(^|[^\w])(" + SyntaxTable[IndexHeader, IndexSyntax] +
@")([^\w]|$)");
537 if (SyntaxTable[IndexHeader, IndexSyntax] ==
"main") { SyntaxMainScan =
true; }
542 return SyntaxMainScan;
Transposer(string CSNameSpace)
This method manages the name space for the cs file
void Transpose_Stdlib_Syntax(char Mode, ref string codeLine, int SyntaxMatchNumber)
This method manages the stdlib.h library transpose
void Transpose_Studio_Syntax(char Mode, ref string codeLine, int SyntaxMatchNumber)
This method manages the stdio.h library transpose
bool SyntaxScan(ref string codeLine)
This method scan each line of object c code and transpose it to c-sharp
void Transpose_Generic_Syntax(char Mode, ref string codeLine, int SyntaxMatchNumber)
This method Manages the generic syntax transpose
int SyntaxTableLastIndex(int IndexA, int IndexB)
This method manages the syntax table indexs
void Transpose_String_Syntax(char Mode, ref string codeLine, int SyntaxMatchNumber)
This method manages the String.h libraty transpose