aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/llgcode/ps/operators_dictionary.go
blob: 75c757104cfad4c5b7e4c092aeb91bf23678bf87 (plain)
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
// Copyright 2010 The postscript-go Authors. All rights reserved.
// created: 13/12/2010 by Laurent Le Goff

package ps

import (
	"log"
)

//int dict dict -> Create dictionary with capacity for int elements
func dict(interpreter *Interpreter) {
	interpreter.Push(NewDictionary(interpreter.PopInt()))
}

//dict length int -> Return number of entries in dict
func lengthdict(interpreter *Interpreter) {
	dictionary := interpreter.Pop().(Dictionary)
	interpreter.Push(float64(len(dictionary)))
}

//dict maxlength int -> Return current capacity of dict
func maxlength(interpreter *Interpreter) {
	interpreter.Pop()
	interpreter.Push(float64(999999999)) // push arbitrary value
}

//dict begin – -> Push dict on dictionary stack
func begin(interpreter *Interpreter) {
	interpreter.PushDictionary(interpreter.Pop().(Dictionary))
}

//– end – -> Pop current dictionary off dictionary stack
func end(interpreter *Interpreter) {
	interpreter.PopDictionary()
}

//key  value def – -> Associate key and value in current dictionary
func def(interpreter *Interpreter) {
	value := interpreter.Pop()
	name := interpreter.PopName()
	if p, ok := value.(*ProcedureDefinition); ok {
		value = NewProcedure(p)
	}
	interpreter.Define(name, value)
}

//key load value -> Search dictionary stack for key and return associated value
func load(interpreter *Interpreter) {
	name := interpreter.PopName()
	value, _ := interpreter.FindValueInDictionaries(name)
	if value == nil {
		log.Printf("Can't find value %s\n", name)
	}
	interpreter.Push(value)
}

//key  value store – -> Replace topmost definition of key
func store(interpreter *Interpreter) {
	value := interpreter.Pop()
	key := interpreter.PopName()
	_, dictionary := interpreter.FindValueInDictionaries(key)
	if dictionary != nil {
		dictionary[key] = value
	}
}

//dict  key get any -> Return value associated with key in dict
func getdict(interpreter *Interpreter) {
	key := interpreter.PopName()
	dictionary := interpreter.Pop().(Dictionary)
	interpreter.Push(dictionary[key])
}

//dict  key  value put – -> Associate key with value in dict
func putdict(interpreter *Interpreter) {
	value := interpreter.Pop()
	key := interpreter.PopName()
	dictionary := interpreter.Pop().(Dictionary)
	dictionary[key] = value
}

//dict  key undef – Remove key and its value from dict
func undef(interpreter *Interpreter) {
	key := interpreter.PopName()
	dictionary := interpreter.Pop().(Dictionary)
	dictionary[key] = nil
}

//dict  key known bool -> Test whether key is in dict
func known(interpreter *Interpreter) {
	key := interpreter.PopName()
	dictionary := interpreter.Pop().(Dictionary)
	interpreter.Push(dictionary[key] != nil)
}

//key where (dict  true) or false -> Find dictionary in which key is defined
func where(interpreter *Interpreter) {
	key := interpreter.PopName()
	_, dictionary := interpreter.FindValueInDictionaries(key)
	if dictionary == nil {
		interpreter.Push(false)
	} else {
		interpreter.Push(dictionary)
		interpreter.Push(true)
	}
}

// dict1  dict2 copy dict2 -> Copy contents of dict1 to dict2
func copydict(interpreter *Interpreter) {
	dict2 := interpreter.Pop().(Dictionary)
	dict1 := interpreter.Pop().(Dictionary)
	for key, value := range dict1 {
		dict2[key] = value
	}
	interpreter.Push(dict2)
}

//dict  proc forall – -> Execute proc for each entry in dict
func foralldict(interpreter *Interpreter) {
	proc := NewProcedure(interpreter.PopProcedureDefinition())
	dict := interpreter.Pop().(Dictionary)
	for key, value := range dict {
		interpreter.Push(key)
		interpreter.Push(value)
		proc.Execute(interpreter)
	}
}

//– currentdict dict -> Return current dictionary
func currentdict(interpreter *Interpreter) {
	interpreter.Push(interpreter.PeekDictionary())
}

//– systemdict dict -> Return system dictionary
func systemdict(interpreter *Interpreter) {
	interpreter.Push(interpreter.SystemDictionary())
}

//– userdict dict -> Return writeable dictionary in local VM
func userdict(interpreter *Interpreter) {
	interpreter.Push(interpreter.UserDictionary())
}

//– globaldict dict -> Return writeable dictionary in global VM
func globaldict(interpreter *Interpreter) {
	interpreter.Push(interpreter.UserDictionary())
}

//– statusdict dict -> Return product-dependent dictionary
func statusdict(interpreter *Interpreter) {
	interpreter.Push(interpreter.UserDictionary())
}

//– countdictstack int -> Count elements on dictionary stack
func countdictstack(interpreter *Interpreter) {
	interpreter.Push(float64(interpreter.DictionaryStackSize()))
}

//array dictstack subarray -> Copy dictionary stack into array
func dictstack(interpreter *Interpreter) {
	panic("No yet implemenented")
}

//– cleardictstack – -> Pop all nonpermanent dictionaries off dictionary stack
func cleardictstack(interpreter *Interpreter) {
	interpreter.ClearDictionaries()
}

func initDictionaryOperators(interpreter *Interpreter) {
	interpreter.SystemDefine("dict", NewOperator(dict))
	//interpreter.SystemDefine("length", NewOperator(length)) // already define in operators_conflict.go
	interpreter.SystemDefine("maxlength", NewOperator(maxlength))
	interpreter.SystemDefine("begin", NewOperator(begin))
	interpreter.SystemDefine("end", NewOperator(end))
	interpreter.SystemDefine("def", NewOperator(def))
	interpreter.SystemDefine("load", NewOperator(load))
	interpreter.SystemDefine("store", NewOperator(store))
	//interpreter.SystemDefine("get", NewOperator(get)) // already define in operators_conflict.go
	//interpreter.SystemDefine("put", NewOperator(put)) // already define in operators_conflict.go
	interpreter.SystemDefine("undef", NewOperator(undef))
	interpreter.SystemDefine("known", NewOperator(known))
	interpreter.SystemDefine("where", NewOperator(where))
	//interpreter.SystemDefine("copydict", NewOperator(copydict)) // already define in operators_conflict.go
	//interpreter.SystemDefine("foralldict", NewOperator(foralldict)) // already define in operators_conflict.go
	interpreter.SystemDefine("currentdict", NewOperator(currentdict))
	interpreter.SystemDefine("systemdict", NewOperator(systemdict))
	interpreter.SystemDefine("userdict", NewOperator(userdict))
	interpreter.SystemDefine("globaldict", NewOperator(globaldict))
	interpreter.SystemDefine("statusdict", NewOperator(statusdict))
	interpreter.SystemDefine("countdictstack", NewOperator(countdictstack))
	interpreter.SystemDefine("dictstack", NewOperator(dictstack))
	interpreter.SystemDefine("cleardictstack", NewOperator(cleardictstack))
}