-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinter.go
188 lines (155 loc) Β· 4.67 KB
/
printer.go
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
package main
import (
"fmt"
"reflect"
"github.com/DanielNos/neco/lexer"
VM "github.com/DanielNos/neco/virtualMachine"
"github.com/fatih/color"
)
func printTokens(tokens []*lexer.Token) {
fmt.Println()
for _, token := range tokens {
if token.TokenType >= lexer.TT_KW_const {
color.Set(color.FgHiCyan)
} else if token.TokenType == lexer.TT_EndOfCommand {
color.Set(color.FgHiYellow)
} else if token.TokenType == lexer.TT_Identifier {
color.Set(color.FgHiGreen)
} else if token.TokenType == lexer.TT_StartOfFile || token.TokenType == lexer.TT_EndOfFile {
color.Set(color.FgHiRed)
} else if token.TokenType.IsOperator() {
color.Set(color.FgHiMagenta)
} else if token.TokenType.IsDelimiter() {
color.Set(color.FgHiBlue)
} else {
color.Set(color.FgHiWhite)
}
fmt.Printf("%v\n", token.TableString())
}
color.Set(color.FgHiWhite)
fmt.Println()
}
func printInstructions(instructions *[]VM.Instruction, constants []any) {
lines := map[byte]int{}
currentFile := byte(0)
justChanged := true
for i := 0; i < len(*instructions); i++ {
instruction := (*instructions)[i]
// Skip removed instruction
if instruction.InstructionType == 255 {
continue
}
// Change file
if instruction.InstructionType == VM.IT_FileMarker {
fmt.Println("\nFile " + constants[instruction.InstructionValue[0]].(string))
currentFile = instruction.InstructionValue[0]
lines[instruction.InstructionValue[0]] = int((*instructions)[i+1].InstructionValue[0])
i++
justChanged = true
continue
}
// Display empty line instead of offset and record new line number
if instruction.InstructionType == VM.IT_LineOffset {
lines[currentFile] += int(instruction.InstructionValue[0])
justChanged = true
fmt.Println()
continue
}
// Print line number
if justChanged {
if lines[currentFile] < 10 {
fmt.Print(" ")
}
fmt.Printf("%d ", lines[currentFile])
justChanged = false
} else {
fmt.Print(" ")
}
// Print instruction number
if i < 10 {
fmt.Print(" ")
}
if i < 100 {
fmt.Print(" ")
}
fmt.Printf("%d ", i)
// Print instruction name
fmt.Printf("%s", VM.InstructionTypeToString[instruction.InstructionType])
j := len(VM.InstructionTypeToString[instruction.InstructionType])
for j < 16 {
fmt.Print(" ")
j++
}
// Print arguments
if len(instruction.InstructionValue) != 0 {
fmt.Printf("%d", instruction.InstructionValue[0])
// Jump back instructions
if instruction.InstructionType == VM.IT_JumpBack || instruction.InstructionType == VM.IT_JumpBackEx {
fmt.Printf(" (%d)", i-int(instruction.InstructionValue[0])+1)
// Jump forward instructions
} else if VM.IsJumpForward(instruction.InstructionType) {
fmt.Printf(" (%d)", i+int(instruction.InstructionValue[0])+1)
// Instructions loading constants
} else if instruction.InstructionType == VM.IT_PushScope || instruction.InstructionType == VM.IT_LoadConst || instruction.InstructionType == VM.IT_LoadConstToList {
if reflect.TypeOf(constants[instruction.InstructionValue[0]]).Kind() == reflect.String {
fmt.Printf(" (\"%v\")", constants[instruction.InstructionValue[0]])
} else {
fmt.Printf(" (%v)", constants[instruction.InstructionValue[0]])
}
// Instruction calling built-in functions
} else if instruction.InstructionType == VM.IT_CallBuiltInFunc {
fmt.Printf(" %v()", VM.BuiltInFuncToString[instruction.InstructionValue[0]])
}
}
fmt.Println()
}
}
func printConstants(stringsCount, intsCount, floatsCount int, constants []any) {
// Calculate segments sizes
stringsSize := 0
for i := 0; i < stringsCount; i++ {
stringsSize += len(constants[i].(string)) + 1
}
intsSize := intsCount * 8
floatsSize := floatsCount * 8
color.Yellow("Constants %d B\n", stringsSize+intsSize+floatsSize)
// Print constants
color.Set(color.FgHiWhite)
index := 0
fmt.Print("ββ ")
color.HiYellow("Strings %d B\n", stringsSize)
for index < stringsCount {
if index == stringsCount-1 {
fmt.Print("β ββ ")
} else {
fmt.Print("β ββ ")
}
fmt.Printf("[%d] \"%v\"\n", index, constants[index])
index++
}
fmt.Println("β")
fmt.Print("ββ ")
color.HiYellow("Integers %d B\n", intsSize)
endOfInts := stringsCount + intsCount
for index < endOfInts {
if index == endOfInts-1 {
fmt.Print("β ββ ")
} else {
fmt.Print("β ββ ")
}
fmt.Printf("[%d] %v\n", index, constants[index])
index++
}
fmt.Println("β")
fmt.Print("ββ ")
color.HiYellow("Floats %d B\n", floatsSize)
for index < len(constants) {
if index == len(constants)-1 {
fmt.Print(" ββ ")
} else {
fmt.Print(" ββ ")
}
fmt.Printf("[%d] %v\n", index, constants[index])
index++
}
}