@@ -11,11 +11,13 @@ import (
11
11
type InstructionMemory struct {
12
12
PC int64
13
13
Instructions []string
14
+ Labels map [string ]int64
14
15
}
15
16
16
17
var InstructionMem = InstructionMemory {
17
18
PC : 0 ,
18
19
Instructions : []string {},
20
+ Labels : make (map [string ]int64 ),
19
21
}
20
22
21
23
var dataMemory = DataMemory {
@@ -46,7 +48,7 @@ func (instructionMemory *InstructionMemory) isValidPC() bool {
46
48
}
47
49
48
50
/*
49
- * Function : validateAndExecuteInstruction
51
+ * Function : ValidateAndExecuteInstruction
50
52
* Details : checks instruction type, performs syntax analysis, parses the statement and executes it
51
53
*/
52
54
@@ -57,6 +59,17 @@ func (instructionMemory *InstructionMemory) ValidateAndExecuteInstruction() erro
57
59
58
60
var err error
59
61
62
+ // Check for labels
63
+ labelRegex , _ := regexp .Compile ("^([a-zA-Z][[:alnum:]]*)[[:space:]]*:" )
64
+ if labelRegex .MatchString (currentInstruction ) {
65
+
66
+ indexColon := strings .Index (currentInstruction , ":" )
67
+ labelName := strings .TrimSpace (currentInstruction [:indexColon ])
68
+ currentInstruction = strings .TrimSpace (currentInstruction [indexColon + 1 :])
69
+ instructionMemory .Labels [labelName ] = instructionMemory .PC
70
+
71
+ }
72
+
60
73
if strings .HasPrefix (currentInstruction , "ADD " ) {
61
74
62
75
currentInstructionObject := AddInstruction {inst : currentInstruction }
0 commit comments