forked from feyeleanor/GoLightly
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmemory.go
More file actions
81 lines (65 loc) · 1.34 KB
/
memory.go
File metadata and controls
81 lines (65 loc) · 1.34 KB
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
package govirtual
import (
"fmt"
"strings"
)
//A Value is an allocation of memory of undefined type
type Value interface {
Get() interface{}
Set(interface{})
}
//An Address is something that can be used to address a Value in Memory
type Address interface{}
//Memory maps Addresses to Values
type Memory map[Address]Value
// A Variable is a named Value
type Variable struct {
Value
Name string
}
func (this *Variable) Get() interface{} {
return this.Value.Get()
}
func (this *Variable) Set(value interface{}) {
this.Value.Set(value)
}
func (this *Variable) String() string {
return this.Name
}
//A Literal Value
type Literal struct {
Value interface{}
}
func (this *Literal) Get() interface{} {
return this.Value
}
func (this *Literal) Set(value interface{}) {
this.Value = value
}
func (this *Literal) String() string {
switch x := this.Value.(type) {
case string:
if strings.HasPrefix(x, ":") {
return x
} else {
return fmt.Sprintf("\"%v\"", x)
}
default:
return fmt.Sprintf("%v", x)
}
}
//A Reference points at another value
type Reference struct {
Value *Value
}
func (this *Reference) Get() interface{} {
val := *(this.Value)
return val.Get()
}
func (this *Reference) Set(value interface{}) {
val := *(this.Value)
val.Set(value)
}
func (this *Reference) String() string {
return fmt.Sprintf("&%v", this.Value)
}