This repository has been archived by the owner on Mar 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Form1.cs
1603 lines (1279 loc) · 60.5 KB
/
Form1.cs
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
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Schema;
using System.Reflection;
using System.Security.Cryptography;
using System.Security;
namespace UnifIL
{
public partial class Form1 : Form
{
static string DefaultOutputFolder="\\Translated";
public bool HasSelectedOutput = false;
public LinkedList<XmlDocument> Dictionaries = new LinkedList<XmlDocument>();
public LinkedList<string> local_stacks = new LinkedList<string>();
public class_types.IL_data IL_file=new class_types.IL_data();
public LinkedList<string> InCode = new LinkedList<string>();
int DictionariesIndex = -1;
public Form1()
{
InitializeComponent();
textBoxInput.Text = Directory.GetCurrentDirectory();
textBoxOutput.Text = Directory.GetCurrentDirectory() + DefaultOutputFolder;
IL_file.codelines = new LinkedList<class_types.IL_CodeLine>();
IL_file.variables = new LinkedList<class_types.IL_variable>();
LoadImages();
LoadTranslationLibraries();
//TODO: comment these, here for faster debugging
if (comboBox1.Items.Count > 0)
comboBox1.SelectedIndex = 0;
else
{
MessageBox.Show("Dictionaries not found.", "Error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
label2.Text = "Error occurred: No dictionaries found.";
}
}
string MD5SUM(byte[] FileOrText) //Output: String<-> Input: Byte[] //
{
return BitConverter.ToString(new
MD5CryptoServiceProvider().ComputeHash(FileOrText)).Replace("-", "").ToLower();
}
public static byte[] ReadFully(Stream stream)
{
byte[] buffer = new byte[32768];
using (MemoryStream ms = new MemoryStream())
{
while (true)
{
int read = stream.Read(buffer, 0, buffer.Length);
if (read <= 0)
return ms.ToArray();
ms.Write(buffer, 0, read);
}
}
}
public void LoadImages()
{
byte[] bytearray;
Assembly myAssembly = Assembly.GetExecutingAssembly();
string[] names = myAssembly.GetManifestResourceNames();
Stream myStream = myAssembly.GetManifestResourceStream("Unif_IL.Resources.logofct.png");
Bitmap logofct = new Bitmap(myStream);
myStream = myAssembly.GetManifestResourceStream("Unif_IL.Resources.logofct.png");
bytearray = ReadFully(myStream);
myStream.Close();
string md5_fct = MD5SUM(bytearray);
myStream = myAssembly.GetManifestResourceStream("Unif_IL.Resources.logounl.png");
Bitmap logounl = new Bitmap(myStream);
myStream = myAssembly.GetManifestResourceStream("Unif_IL.Resources.logounl.png");
bytearray = ReadFully(myStream);
myStream.Close();
string md5_unl = MD5SUM(bytearray);
myStream = myAssembly.GetManifestResourceStream("Unif_IL.Resources.UnifIL.ico");
bytearray = ReadFully(myStream);
myStream.Close();
string md5_ico = MD5SUM(bytearray);
pictureBox2.Image = logofct;
pictureBox1.Image = logounl;
if (md5_unl != "15e6fb149464c324946e39a35ef1e37b" || md5_fct != "c545c0f7add43132c6815c43f0e12d11" || md5_ico != "7df551011f96107ef1040f375f63d221")
{
MessageBox.Show("Program corrupted, exiting.", "Error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(0);
}
}
public static void MyValidationEventHandler(object sender, ValidationEventArgs args) {}
private void LoadTranslationLibraries()
{
DirectoryInfo RootFolder = new DirectoryInfo(Directory.GetCurrentDirectory() + "\\Dictionaries\\");
if (Directory.Exists(RootFolder.ToString()))
{
FileInfo[] LibsList = RootFolder.GetFiles();
for (int i = 0; i < LibsList.Count(); i++)
{
FileInfo Lib = LibsList.ElementAt(i);
progressBar1.Value = ((i + 1) / LibsList.Count()) * 100;
if (Path.GetExtension(Lib.Name) == ".xml")
{
bool Validated=LoadLibrary(Lib.Name);
if (Validated)
{
XmlNode title = Dictionaries.First().SelectSingleNode("//conversion_library//title");
if (title.InnerText != "")
comboBox1.Items.Insert(0, title.InnerText);
else
{
MessageBox.Show("Invalid dictionary: " + LibsList.ElementAt(i), "Error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
label2.Text = "Error occurred: Invalid dictionary.";
}
}
else
{
MessageBox.Show("Invalid dictionary: " + LibsList.ElementAt(i), "Error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
label2.Text = "Error occurred: Invalid dictionary.";
}
}
}
label2.Text = "Libraries loaded successfully";
}
else
{
MessageBox.Show("Dictionaries folder not found." + textBoxInput.Text, "Error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
label2.Text = "Error occurred: Dictionaries folder not found.";
return;
}
}
private void BadSchemaHandler(object sender, ValidationEventArgs e)
{}
public bool LoadLibrary(string filename)
{
LinkedList<string> LibText = new LinkedList<string>();
XmlSchemaSet validation_schema = new XmlSchemaSet();
bool result=false;
Assembly myAssembly;
Stream Schema_file;
XmlDocument XML = new XmlDocument();
myAssembly= Assembly.GetExecutingAssembly();
byte[] bytearray;
Stream myStream = myAssembly.GetManifestResourceStream("Unif_IL.Resources.Libraries.xsd");
bytearray = ReadFully(myStream);
myStream.Close();
string md5_schema = MD5SUM(bytearray);
if (md5_schema != "7c4e07e4277079b669473b9798bc7c34")
{
MessageBox.Show("Program corrupted, exiting.", "Error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(0);
}
try//Evaluate and load library
{
XML.Load(Directory.GetCurrentDirectory() + "\\Dictionaries\\" + filename);
result = true;
Schema_file = myAssembly.GetManifestResourceStream("Unif_IL.Resources.Libraries.xsd");
XmlSchema mySchema = XmlSchema.Read(Schema_file, new ValidationEventHandler(BadSchemaHandler));
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add(mySchema);
settings.ValidationType = ValidationType.Schema;
XmlReader rdr = XmlReader.Create(new StringReader(XML.InnerXml), settings);
}
catch
{
MessageBox.Show("Invalid dictionary.", "Error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
label2.Text = "Error occurred: Invalid dictionary.";
result= false;
}
if (result)
Dictionaries.AddFirst(XML);
return result;
}
private void UpdateInputDirectory(string InputDirectory)
{
textBoxInput.Text = InputDirectory;
}
private void UpdateOutputDirectory(string OutputDirectory)
{
//root clause: avoids something like c:\\DefaultOutputFolder
string outputfolder;
//char slash='\';
if (textBoxInput.Text.Substring(textBoxInput.Text.Length - 1, 1) == '\\'.ToString())
outputfolder = DefaultOutputFolder.Substring(1, DefaultOutputFolder.Length - 1);
else
outputfolder = DefaultOutputFolder;
if (!HasSelectedOutput)
{
if (OutputDirectory.Length == 0)
textBoxOutput.Text = textBoxInput.Text + outputfolder;
else
textBoxOutput.Text = OutputDirectory;
}
else
{
if (OutputDirectory.Length != 0)
textBoxOutput.Text = OutputDirectory;
}
}
public void button1_Click(object sender, EventArgs e)
{
LinkedList<string> TempList = new LinkedList<string>();
InCode = ReadFromText(textBoxInput.Text);
int CurrentLine=0;
bool EOF = false;
if (textBoxInput.Text == "")
{
MessageBox.Show("No file selected." + textBoxInput.Text, "Error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
label2.Text = "Error occurred: no file selected.";
return;
}
if (IsFileEmpty(textBoxInput.Text))
{
MessageBox.Show("File not found:\n" + textBoxInput.Text, "Error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
label2.Text = "Error occurred: file not found.";
return;
}
if (comboBox1.SelectedItem==null)
{
MessageBox.Show("No conversion dictionary selected.", "Error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
label2.Text = "Error occurred: no conversion dictionary selected.";
return;
}
else //if file exists and dictionary is selected, go ahead
{
try
{
IL_file.codelines.Clear();
IL_file.variables.Clear();
//Gather program title (filename)
string filename = textBoxInput.Text;
filename = ReverseString(filename).Substring(0, ReverseString(filename).IndexOf("\\"));
IL_file.title.comment = "Original filename: " + ReverseString(filename);
filename = ReverseString(filename.Substring((filename.IndexOf('.') + 1), filename.Length - filename.IndexOf('.') - 1));
filename = ReplaceIllegalChars(filename);
//program title = filename
IL_file.type = "PROGRAM";
IL_file.title.code = filename;
//check for general rules, if found, apply outcome
ApplyGeneralRules(Dictionaries.ElementAt(DictionariesIndex));
while (!EOF && CurrentLine < InCode.Count) //while not end of file, run all lines
{
if (InCode.ElementAt(CurrentLine).Length > 0)
{
//check for instruction, if found translate
//also declare variables then append code, if required by dictionary
if (CheckForInstructions(CurrentLine, Dictionaries.ElementAt(DictionariesIndex)) == false)
{
MessageBox.Show("Error occurred reading code on line " + (CurrentLine + 1) + " .", "Error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
label2.Text = "Error occurred reading code on line " + (CurrentLine + 1) + " .";
return;
}
}
CurrentLine++;
progressBar1.Value = ((CurrentLine + 1) / InCode.Count) * 100;
}
if (writeToFile(textBoxOutput.Text).success)
{
MessageBox.Show("File successfully translated to IL to \n" + textBoxOutput.Text, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
label2.Text = "File successfully translated to IL.";
}
else
{
MessageBox.Show("Error occurred while writing file to \n" + textBoxOutput.Text, "Error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
label2.Text = "Error occurred while writing file.";
}
return;
}
catch
{
MessageBox.Show("Error occurred while translating file to \n" + textBoxOutput.Text, "Error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
label2.Text = "Error occurred while translating file at line " + CurrentLine + ".";
}
}
}
private void ApplyGeneralRules(XmlDocument Dictionary)
{
class_types.IL_CodeLine codeline = new class_types.IL_CodeLine();
XmlNodeList Rules = Dictionary.SelectNodes("//conversion_library//conversion_rules//general_rules//general_rule//outcome");
string outcome_instruction = "";
if (Rules != null)
{
foreach (XmlNode node in Rules)
{
//outcome_instruction = GetXmlNodeValueByTag(node, "outcome");
outcome_instruction = node.InnerText;
//literal
if (System.Text.Encoding.UTF8.GetBytes(outcome_instruction)[0] == 34 && System.Text.Encoding.UTF8.GetBytes(outcome_instruction)[System.Text.Encoding.UTF8.GetBytes(outcome_instruction).Length - 1] == 34)
{
outcome_instruction = outcome_instruction.Substring(1);
outcome_instruction = outcome_instruction.Substring(0, outcome_instruction.Length - 1);
//-----------Remove comments--------------------------------
string comment_marker_start = Dictionary.SelectSingleNode("//conversion_library//conversion_rules//comment_markers//start").InnerText;
string comment_marker_end = Dictionary.SelectSingleNode("//conversion_library//conversion_rules//comment_markers//end").InnerText;
class_types.CodeResult comments_operation = RemoveComment(outcome_instruction, comment_marker_start, comment_marker_end);
codeline.comment = comments_operation.comment;
outcome_instruction = comments_operation.code;
//-------------End remove comments-------------------------------
IL_file.codelines.AddLast(codeline);
}
}
}
}
private LinkedList<class_types.IL_CodeLine> RemoveLinkedListCodeline(LinkedList<class_types.IL_CodeLine> InCode, int index)
{
LinkedList<class_types.IL_CodeLine> result=new LinkedList<class_types.IL_CodeLine>();
for (int i = 0; i < index; i++)
result.AddLast(InCode.ElementAt(i));
for (int i = index+1; i < InCode.Count; i++)
result.AddLast(InCode.ElementAt(i));
return result;
}
private LinkedList<class_types.IL_CodeLine> InsertCodeAtIndexOfList(LinkedList<class_types.IL_CodeLine> InCode, class_types.IL_CodeLine to_add, int index)
{
LinkedList<class_types.IL_CodeLine> result = new LinkedList<class_types.IL_CodeLine>();
for (int i = 0; i < index; i++)
result.AddLast(InCode.ElementAt(i));
result.AddLast(to_add);
for (int i = index; i < InCode.Count; i++)
result.AddLast(InCode.ElementAt(i));
return result;
}
private void CheckForRules(int CurrentLine, XmlDocument Dictionary, XmlNodeList current_instruction, class_types.Instruction_and_Argument old_code, class_types.Instruction_and_Argument new_code)
{
class_types.IL_CodeLine codeline = new class_types.IL_CodeLine();
string location = "";
string reference = "";
string which = "";
int index = 0;
while (new_code.instruction != "")
{
switch (new_code.instruction)
{
//--------------------No spaces---------------------------
case "delete": //delete what where
new_code = SeparateInstFromArg(new_code.argument);
bool special = false;
string to_delete = "";
//literal
if (System.Text.Encoding.UTF8.GetBytes(new_code.instruction)[0] == 34 && System.Text.Encoding.UTF8.GetBytes(new_code.instruction)[System.Text.Encoding.UTF8.GetBytes(new_code.instruction).Length - 1] == 34)
{
new_code.instruction = new_code.instruction.Substring(1);
new_code.instruction = new_code.instruction.Substring(0, new_code.instruction.Length - 1);
to_delete = new_code.instruction;
}
else
{
switch (new_code.instruction)
{
case "space":
to_delete = " ";
break;
case "line":
special = true;
to_delete = "line";
break;
}
}
if (!special)
{
//delete a string element from the place
//TODO: cleanup
new_code = SeparateInstFromArg(new_code.argument);
switch (new_code.instruction)
{
case "outcome":
codeline = IL_file.codelines.Last();
IL_file.codelines.RemoveLast();
codeline.code = RemoveString(codeline.code, to_delete);
IL_file.codelines.AddLast(codeline);
break;
case "argument":
break;
}
}
else
{//case of special
new_code = SeparateInstFromArg(new_code.argument);
//find index
location = "";
reference = "";
which = "";
location = new_code.instruction;//location now contains "before", "after" or "at"
new_code = SeparateInstFromArg(new_code.argument);
which = new_code.instruction;//which now contains "first","last", "line" TODO: add numbered option?
new_code = SeparateInstFromArg(new_code.argument);//TODO: check for literals
reference = new_code.instruction;//reference now contains "new_stack", "..." TODO: complete
//where?
//case of literal, accepts number of line
if (System.Text.Encoding.UTF8.GetBytes(reference)[0] == 34 && System.Text.Encoding.UTF8.GetBytes(reference)[System.Text.Encoding.UTF8.GetBytes(reference).Length - 1] == 34)
{
reference = reference.Substring(1);
reference = reference.Substring(0, reference.Length - 1);
try
{
IL_file.codelines = RemoveLinkedListCodeline(IL_file.codelines, int.Parse(reference));
}
catch
{
}
}
else
{
index = GetIndexFromOrders(Dictionary, location, which, reference);
if (index < 0 && index > IL_file.codelines.Count)
break;
IL_file.codelines= RemoveLinkedListCodeline(IL_file.codelines, index);
}
}
break;
//----------------------------------------------------
//-------------------Declare---------------------------
case "declare":
class_types.IL_variable declare_line = new class_types.IL_variable();
declare_line.type = null;
bool exists = false;
new_code = SeparateInstFromArg(new_code.argument);
for (int j = 0; j < IL_file.variables.Count; j++)
{
if (IL_file.variables.ElementAt(j).name == ReplaceIllegalChars(old_code.argument))
{
exists = true;
break;
}
}
if (!exists && new_code.instruction == "argument")
{
XmlNodeList var_types = Dictionary.SelectNodes("//conversion_library//variable_types//variable_type");
XmlNodeList var_type_result = CompareVarType(var_types, old_code.argument);//run through variable types
class_types.Instruction_and_Argument type_node = SeparateInstFromArg(GetXmlNodeValueByTag(var_type_result, "new"));//TODO: update according to dictionary
string argument = SeparateInstFromArg(type_node.argument).instruction;
string remainder =
declare_line.name = ReplaceIllegalChars(old_code.argument);
declare_line.IO_type = type_node.instruction;
if (argument != "")
{
string type_real = "";
if (argument == "binary")
type_real = "BOOL";
if (argument == "digital")
type_real = "BOOL";
declare_line.type = type_real;
}
if (SeparateInstFromArg(type_node.argument).argument == "" && declare_line.type != null)
{
IL_file.variables.AddLast(declare_line);
new_code.argument = "";
new_code.instruction = "";
}
else
break;
}
else
{
new_code.argument = "";
new_code.instruction = "";
}
break;
//-------------------End declare---------------------------
//-------------------Add---------------------------
case "add":
string simbolic_codeline = "";
codeline.code = "";
codeline.comment = "";
while (true)
{
new_code = SeparateInstFromArg(new_code.argument);
if (new_code.instruction == "" || new_code.instruction == "at" || new_code.instruction == "before" || new_code.instruction == "after")
break;
if (simbolic_codeline == "")
simbolic_codeline += new_code.instruction;
else
simbolic_codeline += " " + new_code.instruction;
}
//here, the codeline is simbolic //TODO: accept literals
//find index
location = "";
reference = "";
which = "";
location = new_code.instruction;//location now contains "before", "after" or "at"
new_code = SeparateInstFromArg(new_code.argument);
which = new_code.instruction;//which now contains "first" or "last" TODO: add numbered option?
new_code = SeparateInstFromArg(new_code.argument);//TODO: check for literals
reference = new_code.instruction;//reference now contains "new_stack", "..." TODO: complete
//TODO: check for end if literal is found
index = GetIndexFromOrders(Dictionary, location, which, reference);
if (index < 0 && index > IL_file.codelines.Count)
break;
while (simbolic_codeline != "")//knowing the index and what to write, generate the actual codeline from the simbolic one
{
if (codeline.code != "")
codeline.code += " ";
switch (SeparateInstFromArg(simbolic_codeline).instruction)
{
case "new_instruction":
codeline.code += GetXmlNodeValueByTag(current_instruction, "new_instruction");
break;
case "argument":
codeline.code += SeparateInstFromArg(IL_file.codelines.ElementAt(index).code).argument;
break;
}
simbolic_codeline = SeparateInstFromArg(simbolic_codeline).argument;
}
IL_file.codelines = InsertCodeAtIndexOfList(IL_file.codelines, codeline, index);
break;
//-------------------End add-----------------------------
default:
new_code.instruction = "";
break;
}
}
//**********************************************************END RULES*****************************************************
}
private string GetXmlNodeValueByTag(XmlNodeList node_list, string tag)
{
string result = "";
if (node_list!= null)
{
foreach (XmlNode node in node_list)
{
string name = node.Name;
if (node.HasChildNodes)
if (name == tag)
{
result = node.InnerText;
//get value
break;
}
}
}
return result;
}
private LinkedList<string> GetXmlNodeValuesByTag(XmlNodeList node_list, string tag)
{
LinkedList<string> result = new LinkedList<string>();
if (node_list != null)
{
foreach (XmlNode node in node_list)
{
if (node.Name == tag)
{
result.AddLast(node.InnerText);//get value
}
}
}
return result;
}
bool CompareWithIgnores(string Lib, string Target)
{
bool result=true;;
bool in_ignore=false;
char break_char='*';
if (!Lib.Contains(break_char))
{
if (Lib == Target)
result = true;
else
result = false;
}
else
{
for (int i = 0; i < Target.Length; i++)
{
if (!in_ignore)
{
if (Lib[i] == '*')
{
in_ignore = true;
if (Lib.Length > i + 1)
break_char = Lib[i + 1];
}
else
{
if (Target[i] != Lib[i])
{
result = false;
break;
}
//else, keep going
}
}
else
{
if (Target[i] == break_char)
in_ignore = false;
}
}
}
return result;
}
XmlNodeList CompareVarType(XmlNodeList var_types, string argument)
{
XmlNodeList result=null;
XmlNodeList current_var = null;
for (int i = 0; i < var_types.Count; i++)
{
current_var = var_types.Item(i).ChildNodes;
string test = GetXmlNodeValueByTag(current_var,"old");
if (CompareWithIgnores(GetXmlNodeValueByTag(current_var,"old"),argument))
{
result = var_types.Item(i).ChildNodes ;
break;
}
}
return result;
}
string ReplaceIllegalChars(string in_string)
{
StringBuilder no_line_illegals = new StringBuilder(in_string);
//remove end_spaces
while (true)
{
if (in_string.LastIndexOf(" ") == in_string.Length-1)
{
in_string = in_string.Substring(0, in_string.Length - 1);
}
else
break;
}
char[] work_string = in_string.ToCharArray();
for (int i = 0; i < in_string.Length; i++)
{
if (work_string[i] == '.' || work_string[i] == ',' || work_string[i] == ' ' || work_string[i] == '%') //illegal (not accepted by matlab) chars in names
no_line_illegals[i] = '_';
}
string result = no_line_illegals.ToString();
return result;
}
public bool Compare2(string Lib, string Target)
{
bool result=true;;
bool in_ignore=false;
char break_char='*';
for (int i = 0; i < Target.Length; i++)
{
if (!in_ignore)
{
if (Lib[i] == '*')
{
in_ignore = true;
if (Lib.Length>i+1)
break_char = Lib[i + 1];
}
else
{
if (Target[i] != Lib[i])
{
result = false;
break;
}
//else, keep going
}
}
else
{
if (Target[i] == break_char)
in_ignore = false;
}
}
return result;
}
public bool CompareWithAsterisc(string inst_code, string inst_dic)
{
if (inst_dic.Contains('*'))
{
return true;
}
else
{
if (inst_code.ToUpper() == inst_dic.ToUpper())
return true;
else
return false;
}
}
private bool CheckForInstructions( int CurrentLine, XmlDocument Dictionary)
{
XmlNodeList instructions = Dictionary.SelectNodes("//conversion_library//conversion_rules//instructions//instruction//old_instruction");
class_types.Instruction_and_Argument old_code,new_code;
class_types.IL_CodeLine codeline = new class_types.IL_CodeLine();
class_types.CodeResult comments_operation = new class_types.CodeResult();
string comment_marker_start =Dictionary.SelectSingleNode("//conversion_library//conversion_rules//comment_markers//start").InnerText;
string comment_marker_end = Dictionary.SelectSingleNode("//conversion_library//conversion_rules//comment_markers//end").InnerText;
bool success=true;
new_code.argument = "";
new_code.instruction = "";
comments_operation = RemoveComment(InCode.ElementAt(CurrentLine), comment_marker_start, comment_marker_end);
//check for comments only line
if (comments_operation.code == "")
{
codeline.code = "";
codeline.comment = comments_operation.comment;
IL_file.codelines.AddLast(codeline);
}
else //line with code
{
for (int i = 0; i < instructions.Count; i++)
{
//-----------Remove comments--------------------------------
comments_operation = RemoveComment(InCode.ElementAt(CurrentLine), comment_marker_start, comment_marker_end);
if (comments_operation.success == false)
{
success = false;
break;
}
old_code = SeparateInstFromArg(comments_operation.code);
XmlNodeList current_instruction = instructions.Item(i).ParentNode.ChildNodes;
//-------------End remove comments-------------------------------
//old now has the old line, separated by current "instruction" and the tail
if (CompareWithIgnores(instructions.Item(i).InnerText,old_code.instruction))
{
codeline.code = "";
codeline.comment = comments_operation.comment;//new line has comments only
//************************************************OUTCOME WRITER*******************************************
new_code = SeparateInstFromArg(GetXmlNodeValueByTag(current_instruction, "outcome"));
while (new_code.instruction != "")
{
//check for literal i.e. "
if (System.Text.Encoding.UTF8.GetBytes(new_code.instruction)[0] == 34 && System.Text.Encoding.UTF8.GetBytes(new_code.instruction)[System.Text.Encoding.UTF8.GetBytes(new_code.instruction).Length - 1] == 34)
{
new_code.instruction = new_code.instruction.Substring(1);
new_code.instruction = new_code.instruction.Substring(0, new_code.instruction.Length - 1);
codeline.code += new_code.instruction;
new_code = SeparateInstFromArg(new_code.argument);
}
else
{
switch (new_code.instruction)
{
case "instruction":
codeline.code += " " + GetXmlNodeValueByTag(current_instruction, "new_instruction");
new_code = SeparateInstFromArg(new_code.argument);
break;
case "argument":
codeline.code += " " + ReplaceIllegalChars(old_code.argument);
new_code = SeparateInstFromArg(new_code.argument);
break;
case "old_instruction":
codeline.code += " "+ old_code.instruction;
new_code = SeparateInstFromArg(new_code.argument);
break;
}
}
}
//add written line
if ((codeline.code != "" || codeline.comment != "") && new_code.instruction == "")
{
if (codeline.code.Length > 0 && codeline.code.Substring(0, 1) == " ")//clear first space, if there is one (typical)
codeline.code = codeline.code.Substring(1);
IL_file.codelines.AddLast(codeline);
}
//*******************************************************************************************************
//***************************************************RULES***********************************************************
LinkedList<string> rules = new LinkedList<string>();
rules = GetXmlNodeValuesByTag(current_instruction, "rules");
for (int j = 0; j < rules.Count; j++)
{
new_code.argument = "";
new_code.instruction = "";
new_code = SeparateInstFromArg(rules.ElementAt(j));
CheckForRules(CurrentLine, Dictionary, current_instruction, old_code, new_code);
}
}
}
}
return success;
}
private int GetIndexFromOrders(XmlDocument Dictionary, string location, string which, string reference)
{
int index = -1;
int j=0;
int last_j=0;
switch (reference)
{
case "new_stack":
//find all new_stack manipulators
XmlNodeList stack_manipulators = Dictionary.SelectNodes("//conversion_library//stack_manipulators//new_stack");
if (which=="last")
{
j = IL_file.codelines.Count - 1;
last_j=0;
}
if (which=="first")
{
j = 0;
last_j=IL_file.codelines.Count - 1;
}
while (j!=last_j)