-
Notifications
You must be signed in to change notification settings - Fork 47
/
cxprogram.go
473 lines (394 loc) · 12.1 KB
/
cxprogram.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
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
package cxcore
import (
"errors"
"fmt"
"strings"
)
/*
* The CXProgram struct contains a full program.
*
* It is the root data structures for all code, variable and data structures
* declarations.
*/
// CXProgram is used to represent a full CX program.
//
// It is the root data structure for the declarations of all functions,
// variables and data structures.
//
type CXProgram struct {
// Metadata
Path string // Path to the CX project in the filesystem
// Contents
Packages []*CXPackage // Packages in a CX program
// Runtime information
Inputs []*CXArgument // OS input arguments
Outputs []*CXArgument // outputs to the OS
Memory []byte // Used when running the program
StackSize int // This field stores the size of a CX program's stack
HeapSize int // This field stores the size of a CX program's heap
HeapStartsAt int // Offset at which the heap starts in a CX program's memory
StackPointer int // At what byte the current stack frame is
CallStack []CXCall // Collection of function calls
CallCounter int // What function call is the currently being executed in the CallStack
HeapPointer int // At what offset a CX program can insert a new object to the heap
Terminated bool // Utility field for the runtime. Indicates if a CX program has already finished or not.
Version string // CX version used to build this CX program.
// Used by the REPL and parser
CurrentPackage *CXPackage // Represents the currently active package in the REPL or when parsing a CX file.
}
// CXCall ...
type CXCall struct {
Operator *CXFunction // What CX function will be called when running this CXCall in the runtime
Line int // What line in the CX function is currently being executed
FramePointer int // Where in the stack is this function call's local variables stored
}
// MakeProgram ...
func MakeProgram() *CXProgram {
minHeapSize := minHeapSize()
newPrgrm := &CXProgram{
Packages: make([]*CXPackage, 0),
CallStack: make([]CXCall, CALLSTACK_SIZE),
Memory: make([]byte, STACK_SIZE+minHeapSize),
StackSize: STACK_SIZE,
HeapSize: minHeapSize,
HeapPointer: NULL_HEAP_ADDRESS_OFFSET, // We can start adding objects to the heap after the NULL (nil) bytes.
}
return newPrgrm
}
// ----------------------------------------------------------------
// Getters
// GetCurrentPackage ...
func (prgrm *CXProgram) GetCurrentPackage() (*CXPackage, error) {
if prgrm.CurrentPackage != nil {
return prgrm.CurrentPackage, nil
}
return nil, errors.New("current package is nil")
}
// GetCurrentStruct ...
func (prgrm *CXProgram) GetCurrentStruct() (*CXStruct, error) {
if prgrm.CurrentPackage != nil {
if prgrm.CurrentPackage.CurrentStruct != nil {
return prgrm.CurrentPackage.CurrentStruct, nil
}
return nil, errors.New("current struct is nil")
}
return nil, errors.New("current package is nil")
}
// GetCurrentFunction ...
func (prgrm *CXProgram) GetCurrentFunction() (*CXFunction, error) {
if prgrm.CurrentPackage != nil {
if prgrm.CurrentPackage.CurrentFunction != nil {
return prgrm.CurrentPackage.CurrentFunction, nil
}
return nil, errors.New("current function is nil")
}
return nil, errors.New("current package is nil")
}
// GetCurrentExpression ...
func (prgrm *CXProgram) GetCurrentExpression() (*CXExpression, error) {
if prgrm.CurrentPackage != nil &&
prgrm.CurrentPackage.CurrentFunction != nil &&
prgrm.CurrentPackage.CurrentFunction.CurrentExpression != nil {
return prgrm.CurrentPackage.CurrentFunction.CurrentExpression, nil
}
return nil, errors.New("current package, function or expression is nil")
}
// GetGlobal ...
func (prgrm *CXProgram) GetGlobal(name string) (*CXArgument, error) {
mod, err := prgrm.GetCurrentPackage()
if err != nil {
return nil, err
}
var foundArgument *CXArgument
for _, def := range mod.Globals {
if def.Name == name {
foundArgument = def
break
}
}
for _, imp := range mod.Imports {
for _, def := range imp.Globals {
if def.Name == name {
foundArgument = def
break
}
}
}
if foundArgument == nil {
return nil, fmt.Errorf("global '%s' not found", name)
}
return foundArgument, nil
}
// GetPackage ...
func (prgrm *CXProgram) GetPackage(modName string) (*CXPackage, error) {
if prgrm.Packages != nil {
var found *CXPackage
for _, mod := range prgrm.Packages {
if modName == mod.Name {
found = mod
break
}
}
if found != nil {
return found, nil
}
return nil, fmt.Errorf("package '%s' not found", modName)
}
return nil, fmt.Errorf("package '%s' not found", modName)
}
// GetStruct ...
func (prgrm *CXProgram) GetStruct(strctName string, modName string) (*CXStruct, error) {
var foundPkg *CXPackage
for _, mod := range prgrm.Packages {
if modName == mod.Name {
foundPkg = mod
break
}
}
var foundStrct *CXStruct
if foundPkg != nil {
for _, strct := range foundPkg.Structs {
if strct.Name == strctName {
foundStrct = strct
break
}
}
}
if foundStrct == nil {
//looking in imports
typParts := strings.Split(strctName, ".")
if mod, err := prgrm.GetPackage(modName); err == nil {
for _, imp := range mod.Imports {
for _, strct := range imp.Structs {
if strct.Name == typParts[0] {
foundStrct = strct
break
}
}
}
}
}
if foundPkg != nil && foundStrct != nil {
return foundStrct, nil
}
return nil, fmt.Errorf("struct '%s' not found in package '%s'", strctName, modName)
}
// GetFunction ...
func (prgrm *CXProgram) GetFunction(fnName string, pkgName string) (*CXFunction, error) {
// I need to first look for the function in the current package
if pkg, err := prgrm.GetCurrentPackage(); err == nil {
for _, fn := range pkg.Functions {
if fn.Name == fnName {
return fn, nil
}
}
}
var foundPkg *CXPackage
for _, pkg := range prgrm.Packages {
if pkgName == pkg.Name {
foundPkg = pkg
break
}
}
var foundFn *CXFunction
if foundPkg != nil {
if foundPkg != nil {
for _, fn := range foundPkg.Functions {
if fn.Name == fnName {
foundFn = fn
break
}
}
}
} else {
return nil, fmt.Errorf("package '%s' not found", pkgName)
}
if foundPkg != nil && foundFn != nil {
return foundFn, nil
}
return nil, fmt.Errorf("function '%s' not found in package '%s'", fnName, pkgName)
}
// GetExpr returns the current CXExpression
func (prgrm *CXProgram) GetExpr() *CXExpression {
//call := prgrm.GetCall()
//return call.Operator.Expressions[call.Line]
call := &prgrm.CallStack[prgrm.CallCounter]
return call.Operator.Expressions[call.Line]
}
// GetCall returns the current CXCall
//TODO: What does this do?
func (prgrm *CXProgram) GetCall() *CXCall {
return &prgrm.CallStack[prgrm.CallCounter]
}
// GetOpCode returns the current OpCode
func (prgrm *CXProgram) GetOpCode() int {
return prgrm.GetExpr().Operator.OpCode
}
// GetFramePointer returns the current frame pointer
func (prgrm *CXProgram) GetFramePointer() int {
return prgrm.GetCall().FramePointer
}
// ----------------------------------------------------------------
// Package handling
// AddPackage ...
func (prgrm *CXProgram) AddPackage(mod *CXPackage) *CXProgram {
found := false
for _, md := range prgrm.Packages {
if md.Name == mod.Name {
prgrm.CurrentPackage = md
found = true
break
}
}
if !found {
prgrm.Packages = append(prgrm.Packages, mod)
prgrm.CurrentPackage = mod
}
return prgrm
}
// RemovePackage ...
func (prgrm *CXProgram) RemovePackage(modName string) {
lenMods := len(prgrm.Packages)
for i, mod := range prgrm.Packages {
if mod.Name == modName {
if i == lenMods-1 {
prgrm.Packages = prgrm.Packages[:len(prgrm.Packages)-1]
} else {
prgrm.Packages = append(prgrm.Packages[:i], prgrm.Packages[i+1:]...)
}
// This means that we're removing the package set to be the CurrentPackage.
// If it is removed from the program's list of packages, prgrm.CurrentPackage
// would be pointing to a package meant to be collected by the GC.
// We fix this by pointing to the last package in the program's list of packages.
if mod == prgrm.CurrentPackage {
prgrm.CurrentPackage = prgrm.Packages[len(prgrm.Packages)-1]
}
break
}
}
}
// ----------------------------------------------------------------
// Selectors
// SelectProgram sets `PROGRAM` to the the receiver `prgrm`. This is a utility function used mainly
// by CX chains. `PROGRAM` is used in multiple parts of the CX runtime as a convenience; instead of having
// to pass around a parameter of type CXProgram, the CX program currently being run is accessible through
// `PROGRAM`.
func (prgrm *CXProgram) SelectProgram() (*CXProgram, error) {
PROGRAM = prgrm
return PROGRAM, nil
}
// GetProgram returns the CX program assigned to global variable `PROGRAM`.
// This function is mainly used for CX chains.
func GetProgram() (*CXProgram, error) {
if PROGRAM == nil {
return nil, fmt.Errorf("a CX program has not been loaded")
}
return PROGRAM, nil
}
// SelectPackage ...
func (prgrm *CXProgram) SelectPackage(name string) (*CXPackage, error) {
// prgrmStep := &CXProgramStep{
// Action: func(prgrm *CXProgram) {
// prgrm.SelectPackage(name)
// },
// }
// saveProgramStep(prgrmStep, prgrm)
var found *CXPackage
for _, mod := range prgrm.Packages {
if mod.Name == name {
prgrm.CurrentPackage = mod
found = mod
}
}
if found == nil {
return nil, fmt.Errorf("Package '%s' does not exist", name)
}
return found, nil
}
// SelectFunction ...
func (prgrm *CXProgram) SelectFunction(name string) (*CXFunction, error) {
// prgrmStep := &CXProgramStep{
// Action: func(prgrm *CXProgram) {
// prgrm.SelectFunction(name)
// },
// }
// saveProgramStep(prgrmStep, prgrm)
mod, err := prgrm.GetCurrentPackage()
if err == nil {
return mod.SelectFunction(name)
}
return nil, err
}
// SelectStruct ...
func (prgrm *CXProgram) SelectStruct(name string) (*CXStruct, error) {
// prgrmStep := &CXProgramStep{
// Action: func(prgrm *CXProgram) {
// prgrm.SelectStruct(name)
// },
// }
// saveProgramStep(prgrmStep, prgrm)
mod, err := prgrm.GetCurrentPackage()
if err == nil {
return mod.SelectStruct(name)
}
return nil, err
}
// SelectExpression ...
func (prgrm *CXProgram) SelectExpression(line int) (*CXExpression, error) {
// prgrmStep := &CXProgramStep{
// Action: func(prgrm *CXProgram) {
// prgrm.SelectExpression(line)
// },
// }
// saveProgramStep(prgrmStep, prgrm)
mod, err := prgrm.GetCurrentPackage()
if err == nil {
return mod.SelectExpression(line)
}
return nil, err
}
// ----------------------------------------------------------------
// Debugging
// PrintAllObjects prints all objects in a program
//
func (prgrm *CXProgram) PrintAllObjects() {
fp := 0
for c := 0; c <= prgrm.CallCounter; c++ {
op := prgrm.CallStack[c].Operator
for _, ptr := range op.ListOfPointers {
heapOffset := Deserialize_i32(prgrm.Memory[fp+ptr.Offset : fp+ptr.Offset+TYPE_POINTER_SIZE])
var byts []byte
if ptr.CustomType != nil {
// then it's a pointer to a struct
// use CustomStruct to match the fields against the bytes
// for _, fld := range ptr.Fields {
// }
byts = prgrm.Memory[int(heapOffset)+OBJECT_HEADER_SIZE : int(heapOffset)+OBJECT_HEADER_SIZE+ptr.CustomType.Size]
}
// var currLengths []int
// var currCustom *CXStruct
// for c := len(ptr.DeclarationSpecifiers) - 1; c >= 0; c-- {
// // we need to go backwards in here
// switch ptr.DeclarationSpecifiers[c] {
// case DECL_POINTER:
// // we might not need to do anything
// case DECL_ARRAY:
// currLengths = ptr.Lengths
// case DECL_SLICE:
// case DECL_STRUCT:
// currCustom = ptr.CustomType
// case DECL_BASIC:
// }
// }
// if len(ptr.Lengths) > 0 {
// fmt.Println("ARRAY")
// }
// if ptr.CustomType != nil {
// fmt.Println("STRUCT")
// }
fmt.Println("declarat", ptr.DeclarationSpecifiers)
fmt.Println("obj", ptr.Name, ptr.CustomType, prgrm.Memory[heapOffset:int(heapOffset)+op.Size], byts)
}
fp += op.Size
}
}