- Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathoperators_array.go
More file actions
Latest commit
99 lines (85 loc) · 2.98 KB
/
Copy pathoperators_array.go
File metadata and controls
99 lines (85 loc) · 2.98 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2010 The postscript-go Authors. All rights reserved.
// created: 13/12/2010 by Laurent Le Goff
package ps
//int array array -> Create array of length int
funcarray(interpreter*Interpreter) {
interpreter.Push(make([]Value, interpreter.PopInt()))
}
//array length int -> Return number of elements in array
funclengtharray(interpreter*Interpreter) {
interpreter.Push(float64(len(interpreter.Pop().([]Value))))
}
//array index get any -> Return array element indexed by index
funcgetarray(interpreter*Interpreter) {
index:=interpreter.PopInt()
array:=interpreter.Pop().([]Value)
interpreter.Push(array[index])
}
//array index any put – -> Put any into array at index
funcputarray(interpreter*Interpreter) {
value:=interpreter.Pop()
index:=interpreter.PopInt()
array:=interpreter.Pop().([]Value)
array[index] =value
}
//array index count getinterval subarray -> Return subarray of array starting at index for count elements
funcgetinterval(interpreter*Interpreter) {
count:=interpreter.PopInt()
index:=interpreter.PopInt()
array:=interpreter.Pop().([]Value)
subarray:=make([]Value, count)
copy(subarray, array[index:index+count])
interpreter.Push(subarray)
}
//array1 index array2 putinterval – Replace subarray of array1 starting at index by array2|packedarray2
funcputinterval(interpreter*Interpreter) {
array2:=interpreter.Pop().([]Value)
index:=interpreter.PopInt()
array1:=interpreter.Pop().([]Value)
fori, v:=rangearray2 {
array1[i+index] =v
}
}
// any0 … anyn−1 array astore array
// stores the objects any0 to anyn−1 from the operand stack into array, where n is the length of array
funcastore(interpreter*Interpreter) {
array:=interpreter.Pop().([]Value)
n:=len(array)
fori:=0; i<n; i++ {
array[i] =interpreter.Pop()
}
}
//array aload any0 … any-1 array
//Push all elements of array on stack
funcaload(interpreter*Interpreter) {
array:=interpreter.Pop().([]Value)
for_, v:=rangearray {
interpreter.Push(v)
}
interpreter.Push(array)
}
//array proc forall – Execute proc for each element of array
funcforallarray(interpreter*Interpreter) {
proc:=NewProcedure(interpreter.PopProcedureDefinition())
array:=interpreter.Pop().([]Value)
for_, v:=rangearray {
interpreter.Push(v)
proc.Execute(interpreter)
}
}
varpackingbool=false
funccurrentpacking(interpreter*Interpreter) {
interpreter.Push(packing)
}
funcsetpacking(interpreter*Interpreter) {
packing=interpreter.PopBoolean()
}
funcinitArrayOperators(interpreter*Interpreter) {
interpreter.SystemDefine("array", NewOperator(array))
interpreter.SystemDefine("getinterval", NewOperator(getinterval))
interpreter.SystemDefine("putinterval", NewOperator(putinterval))
interpreter.SystemDefine("astore", NewOperator(astore))
interpreter.SystemDefine("aload", NewOperator(aload))
interpreter.SystemDefine("currentpacking", NewOperator(currentpacking))
interpreter.SystemDefine("setpacking", NewOperator(setpacking))
}