-
Notifications
You must be signed in to change notification settings - Fork 2
/
Parser.cs
83 lines (76 loc) · 4.26 KB
/
Parser.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MipSim.Instructions;
using System.Text.RegularExpressions;
namespace MipSim
{
public class Parser
{
public static Instruction ParseInstruction(string instruction, int instructionNumber)
{
//Patterns for all possible instructions
string[] patterns = {
//Using @ before the string disables string character escaping preventing the need to use double \ to escape regex characters
//Rd is limited 1-15 since we shouldn't be able to write to $0
@"^(add|xor|slt) \$([1-9]|1[0-5]),\s?\$([0-9]|1[0-5]),\s?\$([0-9]|1[0-5])$",
@"^addi \$([1-9]|1[0-5]),\s?\$([0-9]|1[0-5]),\s?(-?[0-9]+)$",
@"^(sw|lw) \$([0-9]|1[0-5]),\s?(-?[0-9]+)\(\$([0-9]|1[0-5])\)$",
"^(j|jal|jp) ([0-9]+)$",
@"^jr \$([0-9]|1[0-5])$",
@"^ble (\$([0-9]|1[0-5])),\s?\$([0-9]|1[0-5]),\s?(-?[0-9]+)$",
"^rp$",
"^nop$"
};
instruction = instruction.ToLower().Trim();
//Syntax check
for (int i = 0; i < patterns.Length; i++)
{
var reg = new Regex(patterns[i]);
var match = reg.Match(instruction);
if(match.Success)
{
switch (i)
{
case 0:
if(match.Groups[1].Value == "add")
return new Add(instruction, instructionNumber, int.Parse(match.Groups[2].Value), int.Parse(match.Groups[3].Value), int.Parse(match.Groups[4].Value));
if (match.Groups[1].Value == "xor")
return new Xor(instruction, instructionNumber, int.Parse(match.Groups[2].Value), int.Parse(match.Groups[3].Value), int.Parse(match.Groups[4].Value));
if (match.Groups[1].Value == "slt")
return new Slt(instruction, instructionNumber, int.Parse(match.Groups[2].Value), int.Parse(match.Groups[3].Value), int.Parse(match.Groups[4].Value));
break;
case 1:
return new Addi(instruction, instructionNumber, int.Parse(match.Groups[1].Value), int.Parse(match.Groups[2].Value), int.Parse(match.Groups[3].Value));
case 2:
if (match.Groups[1].Value == "sw")
return new SW(instruction, instructionNumber, int.Parse(match.Groups[2].Value), int.Parse(match.Groups[3].Value), int.Parse(match.Groups[4].Value));
if (match.Groups[1].Value == "lw")
return new LW(instruction, instructionNumber, int.Parse(match.Groups[2].Value), int.Parse(match.Groups[3].Value), int.Parse(match.Groups[4].Value));
break;
case 3:
if (match.Groups[1].Value == "j")
return new J(instruction, instructionNumber, int.Parse(match.Groups[2].Value));
if (match.Groups[1].Value == "jal")
return new Jal(instruction, instructionNumber, int.Parse(match.Groups[2].Value));
if (match.Groups[1].Value == "jp")
return new JumpProcedure(instruction, instructionNumber, int.Parse(match.Groups[2].Value));
break;
case 4:
return new JR(instruction, instructionNumber, int.Parse(match.Groups[1].Value));
case 5:
return new Ble(instruction, instructionNumber, int.Parse(match.Groups[2].Value), int.Parse(match.Groups[3].Value), int.Parse(match.Groups[4].Value));
case 6:
return new ReturnProcedure(instruction, instructionNumber);
case 7:
return new Nop(instruction, instructionNumber);
}
break;
}
}
throw new ParserException("Invalid instruction");
}
}
}