This commit is contained in:
legoff.laurent 2010-12-13 19:13:10 +00:00
parent dae8bc663d
commit efd332158c
12 changed files with 1867 additions and 68 deletions

View file

@ -77,7 +77,16 @@ func (interpreter *Interpreter) computeReference(ref string) {
} }
func (interpreter *Interpreter) scan(scanner *Scanner, token int) { func (interpreter *Interpreter) scan(scanner *Scanner, token int) {
if token == Ident { if token == Ident {
switch scanner.TokenText() {
case "true":
interpreter.Push(true)
case "false":
interpreter.Push(false)
case "null":
interpreter.Push(nil)
default:
interpreter.computeReference(scanner.TokenText()) interpreter.computeReference(scanner.TokenText())
}
} else if token == '/' { } else if token == '/' {
scanner.Scan() scanner.Scan()
interpreter.Push("/" + scanner.TokenText()) interpreter.Push("/" + scanner.TokenText())
@ -104,7 +113,16 @@ func (interpreter *Interpreter) scanArray(scanner *Scanner) []Value {
token := scanner.Scan() token := scanner.Scan()
for token != EOF && token != ']' { for token != EOF && token != ']' {
if token == Ident { if token == Ident {
array = append(array, scanner.TokenText()) var v Value = scanner.TokenText()
switch scanner.TokenText() {
case "true":
v = true
case "false":
v = false
case "null":
v = nil
}
array = append(array, v)
} else { } else {
interpreter.scan(scanner, token) interpreter.scan(scanner, token)
array = append(array, interpreter.Pop()) array = append(array, interpreter.Pop())
@ -119,7 +137,16 @@ func (interpreter *Interpreter) scanProcedure(scanner *Scanner) *ProcedureDefini
token := scanner.Scan() token := scanner.Scan()
for token != EOF && token != '}' { for token != EOF && token != '}' {
if token == Ident { if token == Ident {
proceduredef.Add(scanner.TokenText()) var v Value = scanner.TokenText()
switch scanner.TokenText() {
case "true":
v = true
case "false":
v = false
case "null":
v = nil
}
proceduredef.Add(v)
} else { } else {
interpreter.scan(scanner, token) interpreter.scan(scanner, token)
proceduredef.Add(interpreter.Pop()) proceduredef.Add(interpreter.Pop())
@ -146,6 +173,9 @@ func (interpreter *Interpreter) PeekDictionary() Dictionary {
stackPointer := len(interpreter.dictionaryStack) - 1 stackPointer := len(interpreter.dictionaryStack) - 1
return interpreter.dictionaryStack[stackPointer] return interpreter.dictionaryStack[stackPointer]
} }
func (interpreter *Interpreter) ClearDictionaries() {
interpreter.dictionaryStack = interpreter.dictionaryStack[:2]
}
func (interpreter *Interpreter) DictionaryStackSize() int { func (interpreter *Interpreter) DictionaryStackSize() int {
return len(interpreter.dictionaryStack) return len(interpreter.dictionaryStack)
@ -165,6 +195,14 @@ func (interpreter *Interpreter) FindValueInDictionaries(name string) (Value, Dic
return nil, nil return nil, nil
} }
func (interpreter *Interpreter) UserDictionary() Dictionary {
return interpreter.dictionaryStack[0]
}
func (interpreter *Interpreter) SystemDictionary() Dictionary {
return interpreter.dictionaryStack[0]
}
func (interpreter *Interpreter) Define(name string, value Value) { func (interpreter *Interpreter) Define(name string, value Value) {
interpreter.PeekDictionary()[name] = value interpreter.PeekDictionary()[name] = value
} }
@ -227,7 +265,7 @@ func (interpreter *Interpreter) ClearOperands() {
func (interpreter *Interpreter) PopFloat() float { func (interpreter *Interpreter) PopFloat() float {
operand := interpreter.Pop() operand := interpreter.Pop()
if s, ok := operand.(string); ok { if s, ok := operand.(string); ok {
log.Printf("Erro this is a string %s\n", s) log.Printf("String not float: %s", s)
} }
return operand.(float) return operand.(float)
} }

View file

@ -19,9 +19,11 @@ func (o *PrimitiveOperator) Execute(interpreter *Interpreter) {
func save(interpreter *Interpreter) { func save(interpreter *Interpreter) {
interpreter.Push("VM Snapshot")
} }
func restore(interpreter *Interpreter) { func restore(interpreter *Interpreter) {
interpreter.Pop()
} }
@ -30,8 +32,12 @@ func initSystemOperators(interpreter *Interpreter) {
interpreter.SystemDefine("restore", NewOperator(restore)) interpreter.SystemDefine("restore", NewOperator(restore))
initStackOperator(interpreter) initStackOperator(interpreter)
initMathOperators(interpreter) initMathOperators(interpreter)
initArrayOperators(interpreter)
initDictionaryOperators(interpreter) initDictionaryOperators(interpreter)
initRelationalOperators(interpreter) initRelationalOperators(interpreter)
initControlOperators(interpreter) initControlOperators(interpreter)
initMiscellaneousOperators(interpreter)
initDrawingOperators(interpreter) initDrawingOperators(interpreter)
initConflictingOperators(interpreter)
} }

View file

@ -0,0 +1,93 @@
// Copyright 2010 The postscript-go Authors. All rights reserved.
// created: 13/12/2010 by Laurent Le Goff
package postscript
import ()
//int array array -> Create array of length int
func array(interpreter *Interpreter) {
interpreter.Push(make([]Value, interpreter.PopInt()))
}
//array length int -> Return number of elements in array
func lengtharray(interpreter *Interpreter) {
interpreter.Push(float(len(interpreter.Pop().([]Value))))
}
//array index get any -> Return array element indexed by index
func getarray(interpreter *Interpreter) {
index := interpreter.PopInt()
array := interpreter.Pop().([]Value)
interpreter.Push(array[index])
}
//array index any put -> Put any into array at index
func putarray(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
func getinterval(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
func putinterval(interpreter *Interpreter) {
array2 := interpreter.Pop().([]Value)
index := interpreter.PopInt()
array1 := interpreter.Pop().([]Value)
for i, v := range array2 {
array1[i+index] = v
}
}
// any0 … anyn1 array astore array
// stores the objects any0 to anyn1 from the operand stack into array, where n is the length of array
func astore(interpreter *Interpreter) {
array := interpreter.Pop().([]Value)
n := len(array)
for i := 0; i < n; i++ {
array[i] = interpreter.Pop()
}
}
//array aload any0 … any-1 array
//Push all elements of array on stack
func aload(interpreter *Interpreter) {
array := interpreter.Pop().([]Value)
for _, v := range array {
interpreter.Push(v)
}
interpreter.Push(array)
}
//array proc forall Execute proc for each element of array
func forallarray(interpreter *Interpreter) {
proc := NewProcedure(interpreter.PopProcedureDefinition())
array := interpreter.Pop().([]Value)
for _, v := range array {
interpreter.Push(v)
proc.Execute(interpreter)
}
}
var packing bool = false
func currentpacking(interpreter *Interpreter) {
interpreter.Push(packing)
}
func setpacking(interpreter *Interpreter) {
packing = interpreter.PopBoolean()
}
func initArrayOperators(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))
}

View file

@ -0,0 +1,96 @@
package postscript
import (
"fmt"
"log"
)
// dictionary copy conflict with stack copy
// type dicriminant
func commonCopy(interpreter *Interpreter) {
switch v := interpreter.Peek().(type) {
case float:
copystack(interpreter)
case Dictionary:
copydict(interpreter)
default:
panic(fmt.Sprintf("Not yet implemented: %v copy", v))
}
}
func commonforall(interpreter *Interpreter) {
switch v := interpreter.Get(1).(type) {
case Dictionary:
foralldict(interpreter)
case []Value:
forallarray(interpreter)
case string:
panic("Not yet implemented: string proc forall")
default:
panic(fmt.Sprintf("Not yet implemented: %v proc forall", v))
}
}
func length(interpreter *Interpreter) {
switch v := interpreter.Peek().(type) {
case Dictionary:
lengthdict(interpreter)
case []Value:
lengtharray(interpreter)
case string:
panic("Not yet implemented: string proc forall")
default:
panic(fmt.Sprintf("Not yet implemented: %v length", v))
}
}
func get(interpreter *Interpreter) {
switch v := interpreter.Get(1).(type) {
case Dictionary:
getdict(interpreter)
case []Value:
getarray(interpreter)
case string:
panic("Not yet implemented: string proc forall")
default:
panic(fmt.Sprintf("Not yet implemented: %v index get", v))
}
}
func put(interpreter *Interpreter) {
switch v := interpreter.Get(2).(type) {
case Dictionary:
putdict(interpreter)
case []Value:
putarray(interpreter)
case string:
panic("Not yet implemented: string proc forall")
default:
panic(fmt.Sprintf("Not yet implemented: %v index any put", v))
}
}
func readonly(interpreter *Interpreter) {
log.Println("readonly, not yet implemented")
}
func cvlit(interpreter *Interpreter) {
log.Println("cvlit, not yet implemented")
}
func xcheck(interpreter *Interpreter) {
value := interpreter.Pop()
if _, ok := value.(*ProcedureDefinition); ok {
interpreter.Push(true)
} else {
interpreter.Push(false)
}
}
func initConflictingOperators(interpreter *Interpreter) {
interpreter.SystemDefine("copy", NewOperator(commonCopy))
interpreter.SystemDefine("forall", NewOperator(commonforall))
interpreter.SystemDefine("length", NewOperator(length))
interpreter.SystemDefine("get", NewOperator(get))
interpreter.SystemDefine("put", NewOperator(put))
interpreter.SystemDefine("readonly", NewOperator(readonly))
interpreter.SystemDefine("cvlit", NewOperator(cvlit))
interpreter.SystemDefine("xcheck", NewOperator(xcheck))
}

View file

@ -3,6 +3,21 @@
package postscript package postscript
import (
"log"
)
// any exec Execute arbitrary object
func exec(interpreter *Interpreter) {
value := interpreter.Pop()
if pdef, ok := value.(*ProcedureDefinition); ok {
NewProcedure(pdef).Execute(interpreter)
} else if procedure, ok := value.(*Procedure); ok {
procedure.Execute(interpreter)
} else {
log.Printf("Push value: %v\n", value)
interpreter.Push(value)
}
}
func ifoperator(interpreter *Interpreter) { func ifoperator(interpreter *Interpreter) {
operator := NewProcedure(interpreter.PopProcedureDefinition()) operator := NewProcedure(interpreter.PopProcedureDefinition())
@ -35,9 +50,22 @@ func foroperator(interpreter *Interpreter) {
} }
} }
// any stopped bool -> Establish context for catching stop
func stopped(interpreter *Interpreter) {
value := interpreter.Pop()
if pdef, ok := value.(*ProcedureDefinition); ok {
NewProcedure(pdef).Execute(interpreter)
} else {
interpreter.Push(value)
}
interpreter.Push(false)
}
func initControlOperators(interpreter *Interpreter) { func initControlOperators(interpreter *Interpreter) {
interpreter.SystemDefine("exec", NewOperator(exec))
interpreter.SystemDefine("if", NewOperator(ifoperator)) interpreter.SystemDefine("if", NewOperator(ifoperator))
interpreter.SystemDefine("ifelse", NewOperator(ifelse)) interpreter.SystemDefine("ifelse", NewOperator(ifelse))
interpreter.SystemDefine("for", NewOperator(foroperator)) interpreter.SystemDefine("for", NewOperator(foroperator))
interpreter.SystemDefine("stopped", NewOperator(stopped))
} }

View file

@ -7,14 +7,29 @@ package postscript
import ( import (
"log" "log"
) )
//int dict dict -> Create dictionary with capacity for int elements
func dict(interpreter *Interpreter) {
func get(interpreter *Interpreter) { interpreter.Push(NewDictionary(interpreter.PopInt()))
name := interpreter.PopName()
dictionary := interpreter.Pop().(Dictionary)
interpreter.Push(dictionary[name])
} }
//dict length int -> Return number of entries in dict
func lengthdict(interpreter *Interpreter) {
dictionary := interpreter.Pop().(Dictionary)
interpreter.Push(float(len(dictionary)))
}
//dict maxlength int -> Return current capacity of dict
func maxlength(interpreter *Interpreter) {
interpreter.Pop()
interpreter.Push(float(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) { func def(interpreter *Interpreter) {
value := interpreter.Pop() value := interpreter.Pop()
name := interpreter.PopName() name := interpreter.PopName()
@ -23,32 +38,7 @@ func def(interpreter *Interpreter) {
} }
interpreter.Define(name, value) interpreter.Define(name, value)
} }
//key load value -> Search dictionary stack for key and return associated value
func bind(interpreter *Interpreter) {
pdef := interpreter.PopProcedureDefinition()
values := make([]Value, len(pdef.Values))
for i, value := range pdef.Values {
if s, ok := value.(string); ok {
firstChar := s[0]
if firstChar != '(' && firstChar != '/' {
v, _ := interpreter.FindValueInDictionaries(s)
operator, isOperator := v.(Operator)
if isOperator {
values[i] = operator
} else {
values[i] = value
}
} else {
values[i] = value
}
} else {
values[i] = value
}
}
pdef.Values = values
interpreter.Push(pdef)
}
func load(interpreter *Interpreter) { func load(interpreter *Interpreter) {
name := interpreter.PopName() name := interpreter.PopName()
value, _ := interpreter.FindValueInDictionaries(name) value, _ := interpreter.FindValueInDictionaries(name)
@ -57,19 +47,41 @@ func load(interpreter *Interpreter) {
} }
interpreter.Push(value) interpreter.Push(value)
} }
//key value store -> Replace topmost definition of key
func begin(interpreter *Interpreter) { func store(interpreter *Interpreter) {
interpreter.PushDictionary(interpreter.Pop().(Dictionary)) 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 end(interpreter *Interpreter) { func getdict(interpreter *Interpreter) {
interpreter.PopDictionary() key := interpreter.PopName()
dictionary := interpreter.Pop().(Dictionary)
interpreter.Push(dictionary[key])
} }
//dict key value put -> Associate key with value in dict
func dict(interpreter *Interpreter) { func putdict(interpreter *Interpreter) {
interpreter.Push(NewDictionary(interpreter.PopInt())) 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) { func where(interpreter *Interpreter) {
key := interpreter.PopName() key := interpreter.PopName()
_, dictionary := interpreter.FindValueInDictionaries(key) _, dictionary := interpreter.FindValueInDictionaries(key)
@ -80,14 +92,80 @@ func where(interpreter *Interpreter) {
interpreter.Push(true) 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(float(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) { func initDictionaryOperators(interpreter *Interpreter) {
interpreter.SystemDefine("get", NewOperator(get)) interpreter.SystemDefine("dict", NewOperator(dict))
interpreter.SystemDefine("def", NewOperator(def)) //interpreter.SystemDefine("length", NewOperator(length)) // already define in operators_conflict.go
interpreter.SystemDefine("load", NewOperator(load)) interpreter.SystemDefine("maxlength", NewOperator(maxlength))
interpreter.SystemDefine("bind", NewOperator(bind))
interpreter.SystemDefine("begin", NewOperator(begin)) interpreter.SystemDefine("begin", NewOperator(begin))
interpreter.SystemDefine("end", NewOperator(end)) interpreter.SystemDefine("end", NewOperator(end))
interpreter.SystemDefine("dict", NewOperator(dict)) 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("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))
} }

View file

@ -1,19 +1,17 @@
// Copyright 2010 The postscript-go Authors. All rights reserved. // Copyright 2010 The postscript-go Authors. All rights reserved.
// created: 13/12/2010 by Laurent Le Goff // created: 13/12/2010 by Laurent Le Goff
// Graphics operators
package postscript package postscript
import ( import (
"log"
"image" "image"
"draw2d.googlecode.com/svn/trunk/draw2d/src/pkg/draw2d" "draw2d.googlecode.com/svn/trunk/draw2d/src/pkg/draw2d"
"math" "math"
"log"
) )
// begin Primitive Operator implementation
//Path Construction Operators //Path Construction Operators
func newpath(interpreter *Interpreter) { func newpath(interpreter *Interpreter) {
interpreter.GetGraphicContext().BeginPath() interpreter.GetGraphicContext().BeginPath()
@ -74,7 +72,7 @@ func rcurveto(interpreter *Interpreter) {
} }
func clippath(interpreter *Interpreter) { func clippath(interpreter *Interpreter) {
log.Printf("clippath not yet implemented")
} }
func stroke(interpreter *Interpreter) { func stroke(interpreter *Interpreter) {
@ -194,7 +192,7 @@ func setcmybcolor(interpreter *Interpreter) {
func setdash(interpreter *Interpreter) { func setdash(interpreter *Interpreter) {
offset := interpreter.PopInt() offset := interpreter.PopInt()
dash := interpreter.PopArray() dash := interpreter.PopArray()
log.Printf("dash: %v, offset: %d \n", dash, offset) log.Printf("setdash not yet implemented dash: %v, offset: %d \n", dash, offset)
} }
func setlinejoin(interpreter *Interpreter) { func setlinejoin(interpreter *Interpreter) {
@ -223,6 +221,7 @@ func setlinecap(interpreter *Interpreter) {
func setmiterlimit(interpreter *Interpreter) { func setmiterlimit(interpreter *Interpreter) {
interpreter.PopInt() interpreter.PopInt()
log.Printf("setmiterlimit not yet implemented")
} }
func setlinewidth(interpreter *Interpreter) { func setlinewidth(interpreter *Interpreter) {
@ -230,37 +229,49 @@ func setlinewidth(interpreter *Interpreter) {
} }
func showpage(interpreter *Interpreter) { func showpage(interpreter *Interpreter) {
log.Printf("showpage may be an implementation specific, override show page to generate multi page images")
} }
func show(interpreter *Interpreter) { func show(interpreter *Interpreter) {
s := interpreter.PopString() s := interpreter.PopString()
interpreter.GetGraphicContext().FillString(s) interpreter.GetGraphicContext().FillString(s)
log.Printf("show not really implemented")
}
//ax ay string ashow -> Add (ax , ay) to width of each glyph while showing string
func ashow(interpreter *Interpreter) {
log.Printf("ashow not really implemented")
s := interpreter.PopString()
interpreter.PopFloat()
interpreter.PopFloat()
interpreter.GetGraphicContext().FillString(s)
} }
func findfont(interpreter *Interpreter) { func findfont(interpreter *Interpreter) {
log.Printf("findfont not yet implemented")
} }
func scalefont(interpreter *Interpreter) { func scalefont(interpreter *Interpreter) {
log.Printf("scalefont not yet implemented")
} }
func setfont(interpreter *Interpreter) { func setfont(interpreter *Interpreter) {
log.Printf("setfont not yet implemented")
} }
func stringwidth(interpreter *Interpreter) { func stringwidth(interpreter *Interpreter) {
interpreter.Push(10.0) interpreter.Push(10.0)
interpreter.Push(10.0) interpreter.Push(10.0)
log.Printf("stringwidth not yet implemented")
} }
func setflat(interpreter *Interpreter) { func setflat(interpreter *Interpreter) {
interpreter.Pop() interpreter.Pop()
log.Printf("setflat not yet implemented")
} }
func currentflat(interpreter *Interpreter) { func currentflat(interpreter *Interpreter) {
interpreter.Push(1.0) interpreter.Push(1.0)
log.Printf("currentflat not yet implemented")
} }
@ -308,6 +319,7 @@ func initDrawingOperators(interpreter *Interpreter) {
interpreter.SystemDefine("stroke", NewOperator(stroke)) interpreter.SystemDefine("stroke", NewOperator(stroke))
interpreter.SystemDefine("fill", NewOperator(fill)) interpreter.SystemDefine("fill", NewOperator(fill))
interpreter.SystemDefine("show", NewOperator(show)) interpreter.SystemDefine("show", NewOperator(show))
interpreter.SystemDefine("ashow", NewOperator(ashow))
interpreter.SystemDefine("showpage", NewOperator(showpage)) interpreter.SystemDefine("showpage", NewOperator(showpage))
interpreter.SystemDefine("findfont", NewOperator(findfont)) interpreter.SystemDefine("findfont", NewOperator(findfont))
@ -333,7 +345,7 @@ func initDrawingOperators(interpreter *Interpreter) {
interpreter.SystemDefine("currentflat", NewOperator(currentflat)) interpreter.SystemDefine("currentflat", NewOperator(currentflat))
// Coordinate System and Matrix operators // Coordinate System and Matrix operators
interpreter.SystemDefine("matrix", NewOperator(transform)) interpreter.SystemDefine("matrix", NewOperator(matrix))
interpreter.SystemDefine("transform", NewOperator(transform)) interpreter.SystemDefine("transform", NewOperator(transform))
interpreter.SystemDefine("itransform", NewOperator(itransform)) interpreter.SystemDefine("itransform", NewOperator(itransform))
interpreter.SystemDefine("translate", NewOperator(translate)) interpreter.SystemDefine("translate", NewOperator(translate))

View file

@ -0,0 +1,42 @@
// Copyright 2010 The postscript-go Authors. All rights reserved.
// created: 13/12/2010 by Laurent Le Goff
// Miscellaneous Operators
package postscript
import (
"log"
)
//proc bind proc Replace operator names in proc with operators; perform idiom recognition
func bind(interpreter *Interpreter) {
pdef := interpreter.PopProcedureDefinition()
values := make([]Value, len(pdef.Values))
for i, value := range pdef.Values {
if s, ok := value.(string); ok {
firstChar := s[0]
if firstChar != '(' && firstChar != '/' {
v, _ := interpreter.FindValueInDictionaries(s)
operator, isOperator := v.(Operator)
if v == nil {
log.Printf("Can't find def: %s\n", s)
}
if isOperator {
values[i] = operator
} else {
values[i] = value
}
} else {
values[i] = value
}
} else {
values[i] = value
}
}
pdef.Values = values
interpreter.Push(pdef)
}
func initMiscellaneousOperators(interpreter *Interpreter) {
interpreter.SystemDefine("bind", NewOperator(bind))
}

View file

@ -10,6 +10,12 @@ func eq(interpreter *Interpreter) {
interpreter.Push(value1 == value2) interpreter.Push(value1 == value2)
} }
func ne(interpreter *Interpreter) {
value1 := interpreter.Pop()
value2 := interpreter.Pop()
interpreter.Push(value1 != value2)
}
func lt(interpreter *Interpreter) { func lt(interpreter *Interpreter) {
f2 := interpreter.PopFloat() f2 := interpreter.PopFloat()
f1 := interpreter.PopFloat() f1 := interpreter.PopFloat()
@ -18,5 +24,6 @@ func lt(interpreter *Interpreter) {
func initRelationalOperators(interpreter *Interpreter) { func initRelationalOperators(interpreter *Interpreter) {
interpreter.SystemDefine("eq", NewOperator(eq)) interpreter.SystemDefine("eq", NewOperator(eq))
interpreter.SystemDefine("ne", NewOperator(ne))
interpreter.SystemDefine("lt", NewOperator(lt)) interpreter.SystemDefine("lt", NewOperator(lt))
} }

View file

@ -19,6 +19,7 @@ func exch(interpreter *Interpreter) {
func dup(interpreter *Interpreter) { func dup(interpreter *Interpreter) {
interpreter.Push(interpreter.Peek()) interpreter.Push(interpreter.Peek())
} }
//any1 … anyn n copy any1 … anyn any1 … anyn -> Duplicate top n elements //any1 … anyn n copy any1 … anyn any1 … anyn -> Duplicate top n elements
func copystack(interpreter *Interpreter) { func copystack(interpreter *Interpreter) {
n := interpreter.PopInt() n := interpreter.PopInt()
@ -77,7 +78,6 @@ func initStackOperator(interpreter *Interpreter) {
interpreter.SystemDefine("pop", NewOperator(pop)) interpreter.SystemDefine("pop", NewOperator(pop))
interpreter.SystemDefine("exch", NewOperator(exch)) interpreter.SystemDefine("exch", NewOperator(exch))
interpreter.SystemDefine("dup", NewOperator(dup)) interpreter.SystemDefine("dup", NewOperator(dup))
interpreter.SystemDefine("copy", NewOperator(copystack))
interpreter.SystemDefine("index", NewOperator(index)) interpreter.SystemDefine("index", NewOperator(index))
interpreter.SystemDefine("roll", NewOperator(roll)) interpreter.SystemDefine("roll", NewOperator(roll))
interpreter.SystemDefine("clear", NewOperator(clear)) interpreter.SystemDefine("clear", NewOperator(clear))

View file

@ -284,7 +284,7 @@ func (s *Scanner) error(msg string) {
func (s *Scanner) scanIdentifier() int { func (s *Scanner) scanIdentifier() int {
ch := s.next() // read character after first '_' or letter ch := s.next() // read character after first '_' or letter
for ch == '_' || unicode.IsLetter(ch) || unicode.IsDigit(ch) || ch == '.' { for ch == '_' || unicode.IsLetter(ch) || unicode.IsDigit(ch) || ch == '.' || ch == '-' || ch == '`' {
ch = s.next() ch = s.next()
} }
return ch return ch
@ -489,7 +489,7 @@ redo:
// determine token value // determine token value
tok := ch tok := ch
switch { switch {
case unicode.IsLetter(ch) || ch == '_': case unicode.IsLetter(ch) || ch == '_' || ch == '`':
if s.Mode&ScanIdents != 0 { if s.Mode&ScanIdents != 0 {
tok = Ident tok = Ident
ch = s.scanIdentifier() ch = s.scanIdentifier()
@ -499,7 +499,8 @@ redo:
case ch == '-': // minus case ch == '-': // minus
ch = s.next() ch = s.next()
if !isDecimal(ch) { if !isDecimal(ch) {
fmt.Printf("Error may be a digi: %c\n") s.error(fmt.Sprintf("Error may be a digi: %c\n", ch))
} else { } else {
if s.Mode&(ScanInts|ScanFloats) != 0 { if s.Mode&(ScanInts|ScanFloats) != 0 {
tok, ch = s.scanNumber(ch) tok, ch = s.scanNumber(ch)

File diff suppressed because it is too large Load diff