-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.go
129 lines (104 loc) · 2.42 KB
/
object.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
// seehuhn.de/go/postscript - a rudimentary PostScript interpreter
// Copyright (C) 2023 Jochen Voss <voss@seehuhn.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package postscript
import (
"fmt"
"strings"
)
type Object interface{}
type Integer int
type Real float64
type Boolean bool
type String []byte
func (s String) String() string {
return fmt.Sprintf("%q", string(s))
}
// PS returns the string as it would be written in a PostScript file.
func (s String) PS() string {
var out []byte
l := []byte(s)
level := 0
for _, c := range l {
if c == '(' {
level++
} else if c == ')' {
level--
if level < 0 {
break
}
}
}
balanced := level == 0
out = append(out, '(')
for _, c := range l {
switch c {
case '\\':
out = append(out, '\\', '\\')
case '(':
if balanced {
out = append(out, '(')
} else {
out = append(out, '\\', '(')
}
case ')':
if balanced {
out = append(out, ')')
} else {
out = append(out, '\\', ')')
}
case '\r':
out = append(out, '\\', 'r')
default:
out = append(out, c)
}
}
out = append(out, ')')
return string(out)
}
type Name string
func (n Name) String() string {
return "/" + string(n)
}
func (n Name) PS() string {
for _, c := range []byte(n) {
if !isRegular(c) {
panic(fmt.Sprintf("invalid character in name: %q", n))
}
}
return "/" + string(n)
}
type Operator string
type Array []Object
type Procedure []Object
func (p Procedure) String() string {
var ss []string
ss = append(ss, "{")
for i, o := range p {
if i > 0 {
ss = append(ss, " ")
}
ss = append(ss, fmt.Sprint(o))
}
ss = append(ss, "}")
return strings.Join(ss, "")
}
type Dict map[Name]Object
func (d Dict) String() string {
return fmt.Sprintf("<Dict %d>", len(d))
}
type mark struct{}
var theMark Object = mark{}
type builtin func(*Interpreter) error