Skip to content

Commit c4e95f4

Browse files
committed
Added globalvariabes support
1 parent 86c8a7f commit c4e95f4

File tree

2 files changed

+149
-18
lines changed

2 files changed

+149
-18
lines changed

VM/Program.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ internal unsafe class VirtualMachine
3939
public int TextSectionOffset { get; set; }
4040
public int CSharpTypesSectionOffset { get; set; }
4141
public int ClearSectionOffset { get; set; }
42-
//public object[] GlobalVariables = new object[0];
42+
public object[] GlobalVariables = new object[512];
4343

4444
public VirtualMachine(byte[] b)
4545
{
@@ -71,7 +71,7 @@ private void push(object val)
7171

7272
public void Run(int Offset)
7373
{
74-
//object[] LocalVariables = new object[0];
74+
object[] LocalVariables = new object[0];
7575
while (true)
7676
{
7777
byte curByte = Marshal.ReadByte(IntPtr.Add(MemoryPointer, CodeSectionOffset + Offset++));
@@ -91,13 +91,19 @@ public void Run(int Offset)
9191
}
9292
case 0x51: //LdInt
9393
{
94-
push(Marshal.ReadInt32(MemoryPointer + CodeSectionOffset + Offset));
94+
push(Marshal.ReadInt32(MemoryPointer, CodeSectionOffset + Offset));
9595
Offset += 4;
96-
break;
96+
break;
97+
}
98+
case 0x52: //LdInt
99+
{
100+
push(Marshal.ReadByte(MemoryPointer, CodeSectionOffset + Offset));
101+
Offset += 1;
102+
break;
97103
}
98104
case 0x40: //Call C# Method
99105
{
100-
Run(Marshal.ReadInt32(MemoryPointer + CodeSectionOffset + Offset));
106+
Run(Marshal.ReadInt32(MemoryPointer, CodeSectionOffset + Offset));
101107
Offset += 4;
102108
break;
103109
}
@@ -116,6 +122,20 @@ public void Run(int Offset)
116122
pop();
117123
break;
118124
}
125+
case 0x93:
126+
{
127+
int index = Marshal.ReadInt32(MemoryPointer, CodeSectionOffset + Offset);
128+
Offset += 4;
129+
GlobalVariables[index] = pop();
130+
break;
131+
}
132+
case 0x94:
133+
{
134+
int index = Marshal.ReadInt32(MemoryPointer, CodeSectionOffset + Offset);
135+
Offset += 4;
136+
push(GlobalVariables[index]);
137+
break;
138+
}
119139
case 0x22:
120140
{
121141
return;

VMCompiler/Form1.cs

Lines changed: 124 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ class StringType
2222
public int Index = 0;
2323
public int Length = 0;
2424

25-
public static explicit operator string(StringType stringType)
26-
{
27-
return stringType.String;
28-
}
29-
3025
public StringType(string str, int index, int length)
3126
{
3227
String = str;
@@ -61,13 +56,24 @@ public StringType Add(string str)
6156

6257
enum OpCode
6358
{
64-
LdStr, //50
65-
LdInt, //51
66-
Call, //40
67-
CallC, //41
68-
Br, //60
59+
// Types
60+
LdStr, //50 //Load String to Stack
61+
LdInt, //51 //Load Integer to Stack
62+
LdByte,//52 //Load Byte to Stack
63+
// Voids
64+
Call, //40 //Call Void
65+
CallC, //41 //CallCSharp Void
66+
// Jumps
67+
Br, //60 //Jump to Instruction
68+
// Stack & Variables
69+
Pop, //91 //Remove Top object in Stack
70+
Dup, //92 //Duplicate top object in Stack
71+
StGv, //93 //Push Top stack object to Global Variables
72+
LdGv, //94 //Load Global Variable to Stack
73+
StLv, //95 //Push Top stack object to local Variables
74+
LdLv, //95 //Load Local Variable to Stack
75+
// Other
6976
Nop, //90
70-
Pop, //91
7177
Ret //22
7278
}
7379

@@ -85,7 +91,6 @@ public Instruction(OpCode opcode, object oper)
8591

8692
class VoidType
8793
{
88-
// void ([a-zA-Z0-9]{1,})\n{\n(?:\n*.*?\n*){1,}}
8994
public string Name;
9095
public int Index = 0;
9196
public int Length = 0;
@@ -113,6 +118,13 @@ public VoidType(string name, string v)
113118
Length += 9;
114119
}
115120
break;
121+
case OpCode.LdByte:
122+
{
123+
Instructions[Instructions.Length - 1] =
124+
new Instruction(op, Convert.ToByte(str[i].Substring(str[i].IndexOf(" ") + 3), 16));
125+
Length += 2;
126+
}
127+
break;
116128
case OpCode.LdInt:
117129
{
118130
Instructions[Instructions.Length - 1] =
@@ -134,6 +146,22 @@ public VoidType(string name, string v)
134146
Length += 1;
135147
}
136148
break;
149+
150+
case OpCode.StGv:
151+
{
152+
Instructions[Instructions.Length - 1] =
153+
new Instruction(op, str[i].Substring(str[i].IndexOf(" ") + 1));
154+
Length += 5;
155+
}
156+
break;
157+
case OpCode.LdGv:
158+
{
159+
Instructions[Instructions.Length - 1] =
160+
new Instruction(op, str[i].Substring(str[i].IndexOf(" ") + 1));
161+
Length += 5;
162+
}
163+
break;
164+
137165
case OpCode.Ret:
138166
{
139167
Instructions[Instructions.Length - 1] =
@@ -152,9 +180,43 @@ public VoidType(string name, string v)
152180
}
153181
}
154182

183+
class VariableType
184+
{
185+
public string Name;
186+
public int Index;
187+
188+
public VariableType(string name, int index)
189+
{
190+
Name = name;
191+
Index = index;
192+
}
193+
}
194+
195+
class VariablesContainer
196+
{
197+
private VariableType[] variables = new VariableType[0];
198+
199+
public VariableType this[string name]
200+
{
201+
get { return variables.First(x => x.Name == name); }
202+
}
203+
204+
public VariableType Add(string name)
205+
{
206+
if (variables.Count(x => x.Name == name) == 0)
207+
{
208+
Array.Resize(ref variables, variables.Length + 1);
209+
variables[variables.Length - 1] = new VariableType(name, variables.Length - 1);
210+
}
211+
return variables.First(x => x.Name == name);
212+
}
213+
}
214+
155215
private void Compile()
156216
{
157217
StringsContainer stringsContainer = new StringsContainer();
218+
VariablesContainer variablesContainer = new VariablesContainer();
219+
158220
VoidType[] voids = new VoidType[0];
159221

160222
int CodeSectionStart = 0;
@@ -207,6 +269,15 @@ private void Compile()
207269
}
208270
}
209271

272+
{ //Parse Global Variables
273+
Regex globals = new Regex("var ([a-zA-Z0-9]{1,});");
274+
MatchCollection globalsm = globals.Matches(richTextBox1.Text);
275+
foreach (Match m in globalsm)
276+
{
277+
variablesContainer.Add(m.Groups[1].Value);
278+
}
279+
}
280+
210281
{
211282
//Generate
212283
foreach (var v in voids)
@@ -236,6 +307,15 @@ private void Compile()
236307
CodeSection.Length - 4, 4);
237308
}
238309
break;
310+
case OpCode.LdByte:
311+
{
312+
int oper = (byte) i.operand;
313+
Array.Resize(ref CodeSection, CodeSection.Length + 2);
314+
CodeSection[CodeSection.Length - 2] = 0x52;
315+
Array.Copy(BitConverter.GetBytes(oper), 0, CodeSection,
316+
CodeSection.Length - 1, 1);
317+
}
318+
break;
239319
case OpCode.Call:
240320
{
241321
string voidname = (string)i.operand;
@@ -253,6 +333,29 @@ private void Compile()
253333
CodeSection[CodeSection.Length - 1] = 0x91;
254334
}
255335
break;
336+
337+
case OpCode.StGv:
338+
{
339+
string varname = (string) i.operand;
340+
int oper = variablesContainer[varname].Index;
341+
i.operand = oper;
342+
Array.Resize(ref CodeSection, CodeSection.Length + 5);
343+
CodeSection[CodeSection.Length - 5] = 0x93;
344+
Array.Copy(BitConverter.GetBytes(oper), 0, CodeSection,
345+
CodeSection.Length - 4, 4);
346+
}
347+
break;
348+
case OpCode.LdGv:
349+
{
350+
string varname = (string)i.operand;
351+
int oper = variablesContainer[varname].Index;
352+
i.operand = oper;
353+
Array.Resize(ref CodeSection, CodeSection.Length + 5);
354+
CodeSection[CodeSection.Length - 5] = 0x94;
355+
Array.Copy(BitConverter.GetBytes(oper), 0, CodeSection,
356+
CodeSection.Length - 4, 4);
357+
}
358+
break;
256359
case OpCode.Ret:
257360
{
258361
Array.Resize(ref CodeSection, CodeSection.Length + 1);
@@ -322,7 +425,7 @@ private void richTextBox1_TextChanged(object sender, EventArgs e)
322425

323426
private void Highlight(string text, int offset)
324427
{
325-
Regex voids = new Regex(@"void");
428+
Regex voids = new Regex("void ([a-zA-Z0-9]{1,})\\n{\\n(?:\\n*.*?\\n*){1,}}");
326429
MatchCollection voidsm = voids.Matches(text);
327430
foreach (Match match in voidsm)
328431
{
@@ -364,6 +467,14 @@ private void Highlight(string text, int offset)
364467
richTextBox1.Select(match.Groups[1].Index + offset, match.Groups[1].Length);
365468
richTextBox1.SelectionColor = ColorTranslator.FromHtml("#3498db");
366469
}
470+
471+
Regex globalvars = new Regex(@"var ([a-zA-Z0-9]{1,});");
472+
MatchCollection globalvarsm = globalvars.Matches(text);
473+
foreach (Match match in globalvarsm)
474+
{
475+
richTextBox1.Select(match.Index + offset, 3);
476+
richTextBox1.SelectionColor = ColorTranslator.FromHtml("#3498db");
477+
}
367478
}
368479
}
369480
}

0 commit comments

Comments
 (0)