document operators

This commit is contained in:
legoff.laurent 2010-12-13 12:01:24 +00:00
parent 5cf7561847
commit be8e2922d3
10 changed files with 210 additions and 70 deletions

View file

@ -9,8 +9,8 @@ import (
"image" "image"
"image/png" "image/png"
"draw2d.googlecode.com/svn/trunk/draw2d/src/pkg/draw2d" "draw2d.googlecode.com/svn/trunk/draw2d/src/pkg/draw2d"
//"postscript" "postscript"
"postscript-go.googlecode.com/svn/trunk/postscript-go/src/pkg/postscript" //"postscript-go.googlecode.com/svn/trunk/postscript-go/src/pkg/postscript"
) )

View file

@ -1,3 +1,6 @@
// Copyright 2010 The postscript-go Authors. All rights reserved.
// created: 13/12/2010 by Laurent Le Goff
package postscript package postscript
import ( import (
@ -215,6 +218,12 @@ func (interpreter *Interpreter) OperandSize() int {
return len(interpreter.valueStack) return len(interpreter.valueStack)
} }
func (interpreter *Interpreter) ClearOperands() {
interpreter.valueStack = interpreter.valueStack[0:0]
}
// misc pop
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 {

View file

@ -1,3 +1,6 @@
// Copyright 2010 The postscript-go Authors. All rights reserved.
// created: 13/12/2010 by Laurent Le Goff
package postscript package postscript
type OperatorFunc func(interpreter *Interpreter) type OperatorFunc func(interpreter *Interpreter)
@ -22,7 +25,6 @@ func restore(interpreter *Interpreter) {
} }
func initSystemOperators(interpreter *Interpreter) { func initSystemOperators(interpreter *Interpreter) {
interpreter.SystemDefine("save", NewOperator(save)) interpreter.SystemDefine("save", NewOperator(save))
interpreter.SystemDefine("restore", NewOperator(restore)) interpreter.SystemDefine("restore", NewOperator(restore))

View file

@ -1,3 +1,6 @@
// Copyright 2010 The postscript-go Authors. All rights reserved.
// created: 13/12/2010 by Laurent Le Goff
package postscript package postscript
@ -33,7 +36,6 @@ func foroperator(interpreter *Interpreter) {
} }
func initControlOperators(interpreter *Interpreter) { func initControlOperators(interpreter *Interpreter) {
interpreter.SystemDefine("if", NewOperator(ifoperator)) interpreter.SystemDefine("if", NewOperator(ifoperator))
interpreter.SystemDefine("ifelse", NewOperator(ifelse)) interpreter.SystemDefine("ifelse", NewOperator(ifelse))

View file

@ -1,3 +1,6 @@
// Copyright 2010 The postscript-go Authors. All rights reserved.
// created: 13/12/2010 by Laurent Le Goff
package postscript package postscript

View file

@ -1,3 +1,6 @@
// Copyright 2010 The postscript-go Authors. All rights reserved.
// created: 13/12/2010 by Laurent Le Goff
package postscript package postscript
import ( import (

View file

@ -1,82 +1,157 @@
// Copyright 2010 The postscript-go Authors. All rights reserved.
// created: 13/12/2010 by Laurent Le Goff
//Arithmetic and Math Operators
package postscript package postscript
import ( import (
"math" "math"
"rand"
) )
// begin Primitive Operator implementation // begin Primitive Operator implementation
//num1 num2 add sum -> Return num1 plus num2
func add(interpreter *Interpreter) { func add(interpreter *Interpreter) {
f2 := interpreter.PopFloat() num2 := interpreter.PopFloat()
f1 := interpreter.PopFloat() num1 := interpreter.PopFloat()
interpreter.Push(f1 + f2) interpreter.Push(num1 + num2)
} }
//num1 num2 div quotient -> Return num1 divided by num2
func sub(interpreter *Interpreter) {
f2 := interpreter.PopFloat()
f1 := interpreter.PopFloat()
interpreter.Push(f1 - f2)
}
func div(interpreter *Interpreter) { func div(interpreter *Interpreter) {
f2 := interpreter.PopFloat() num2 := interpreter.PopFloat()
f1 := interpreter.PopFloat() num1 := interpreter.PopFloat()
interpreter.Push(f1 / f2) interpreter.Push(num1 / num2)
}
func mul(interpreter *Interpreter) {
f2 := interpreter.PopFloat()
f1 := interpreter.PopFloat()
interpreter.Push(f1 * f2)
} }
//int1 int2 idiv quotient -> Return int1 divided by int2
func idiv(interpreter *Interpreter) {
int2 := interpreter.PopInt()
int1 := interpreter.PopInt()
interpreter.Push(float(int1 / int2))
}
//int int mod remainder -> Return remainder after dividing int by int
func mod(interpreter *Interpreter) {
int2 := interpreter.PopInt()
int1 := interpreter.PopInt()
interpreter.Push(float(int1 % int2))
}
//num1 num2 mul product -> Return num1 times num2
func mul(interpreter *Interpreter) {
num2 := interpreter.PopFloat()
num1 := interpreter.PopFloat()
interpreter.Push(num1 * num2)
}
//num1 num2 sub difference -> Return num1 minus num2
func sub(interpreter *Interpreter) {
num2 := interpreter.PopFloat()
num1 := interpreter.PopFloat()
interpreter.Push(num1 - num2)
}
//num1 abs num2 -> Return absolute value of num1
func abs(interpreter *Interpreter) {
f := interpreter.PopFloat()
interpreter.Push(float(math.Fabs(float64(f))))
}
//num1 neg num2 -> Return negative of num1
func neg(interpreter *Interpreter) {
f := interpreter.PopFloat()
interpreter.Push(-f)
}
//num1 ceiling num2 -> Return ceiling of num1
func ceiling(interpreter *Interpreter) {
f := interpreter.PopFloat()
interpreter.Push(float(int(f + 1)))
}
//num1 floor num2 -> Return floor of num1
func floor(interpreter *Interpreter) {
f := interpreter.PopFloat()
interpreter.Push(float(math.Floor(float64(f))))
}
//num1 round num2 -> Round num1 to nearest integer
func round(interpreter *Interpreter) {
f := interpreter.PopFloat()
interpreter.Push(float(int(f + 0.5)))
}
//num1 truncate num2 -> Remove fractional part of num1
func truncate(interpreter *Interpreter) {
f := interpreter.PopFloat()
interpreter.Push(float(int(f)))
}
//num sqrt real -> Return square root of num
func sqrt(interpreter *Interpreter) { func sqrt(interpreter *Interpreter) {
f := interpreter.PopFloat() f := interpreter.PopFloat()
interpreter.Push(float(math.Sqrt(float64(f)))) interpreter.Push(float(math.Sqrt(float64(f))))
} }
//num den atan angle -> Return arctangent of num/den in degrees
func atan(interpreter *Interpreter) { func atan(interpreter *Interpreter) {
den := interpreter.PopFloat() den := interpreter.PopFloat()
num := interpreter.PopFloat() num := interpreter.PopFloat()
interpreter.Push(float(math.Atan2(float64(num), float64(den))) * (180.0 / math.Pi)) interpreter.Push(float(math.Atan2(float64(num), float64(den))) * (180.0 / math.Pi))
} }
//angle cos real -> Return cosine of angle degrees
func cos(interpreter *Interpreter) { func cos(interpreter *Interpreter) {
a := interpreter.PopFloat() a := interpreter.PopFloat()
interpreter.Push(float(math.Cos(float64(a))) * (180.0 / math.Pi)) interpreter.Push(float(math.Cos(float64(a))) * (180.0 / math.Pi))
} }
//angle sin real -> Return sine of angle degrees
func sin(interpreter *Interpreter) { func sin(interpreter *Interpreter) {
a := interpreter.PopFloat() a := interpreter.PopFloat()
interpreter.Push(float(math.Sin(float64(a))) * (180.0 / math.Pi)) interpreter.Push(float(math.Sin(float64(a))) * (180.0 / math.Pi))
} }
//base exponent exp real -> Raise base to exponent power
func neg(interpreter *Interpreter) { func exp(interpreter *Interpreter) {
f := interpreter.PopFloat() exponent := interpreter.PopFloat()
interpreter.Push(-f) base := interpreter.PopFloat()
interpreter.Push(float(math.Pow(float64(base), float64(exponent))))
}
//num ln real -> Return natural logarithm (base e)
func ln(interpreter *Interpreter) {
num := interpreter.PopFloat()
interpreter.Push(float(math.Log(float64(num))))
}
//num log real -> Return common logarithm (base 10)
func log10(interpreter *Interpreter) {
num := interpreter.PopFloat()
interpreter.Push(float(math.Log10(float64(num))))
}
// rand int Generate pseudo-random integer
func randInt(interpreter *Interpreter) {
interpreter.Push(rand.Int())
} }
func round(interpreter *Interpreter) { var randGenerator *rand.Rand
f := interpreter.PopFloat() //int srand -> Set random number seed
interpreter.Push(float(int(f + 0.5))) func srand(interpreter *Interpreter) {
randGenerator = rand.New(rand.NewSource(int64(interpreter.PopInt())))
} }
// rrand int -> Return random number seed
func abs(interpreter *Interpreter) { func rrand(interpreter *Interpreter) {
f := interpreter.PopFloat() interpreter.Push(float(randGenerator.Int()))
interpreter.Push(float(math.Fabs(float64(f))))
} }
func initMathOperators(interpreter *Interpreter) { func initMathOperators(interpreter *Interpreter) {
interpreter.SystemDefine("add", NewOperator(add)) interpreter.SystemDefine("add", NewOperator(add))
interpreter.SystemDefine("sub", NewOperator(sub))
interpreter.SystemDefine("mul", NewOperator(mul))
interpreter.SystemDefine("div", NewOperator(div)) interpreter.SystemDefine("div", NewOperator(div))
interpreter.SystemDefine("idiv", NewOperator(idiv))
interpreter.SystemDefine("mod", NewOperator(mod))
interpreter.SystemDefine("mul", NewOperator(mul))
interpreter.SystemDefine("sub", NewOperator(sub))
interpreter.SystemDefine("abs", NewOperator(abs))
interpreter.SystemDefine("neg", NewOperator(neg))
interpreter.SystemDefine("ceiling", NewOperator(ceiling))
interpreter.SystemDefine("floor", NewOperator(floor))
interpreter.SystemDefine("round", NewOperator(round))
interpreter.SystemDefine("truncate", NewOperator(truncate))
interpreter.SystemDefine("sqrt", NewOperator(sqrt)) interpreter.SystemDefine("sqrt", NewOperator(sqrt))
interpreter.SystemDefine("atan", NewOperator(atan))
interpreter.SystemDefine("cos", NewOperator(cos)) interpreter.SystemDefine("cos", NewOperator(cos))
interpreter.SystemDefine("sin", NewOperator(sin)) interpreter.SystemDefine("sin", NewOperator(sin))
interpreter.SystemDefine("atan", NewOperator(atan)) interpreter.SystemDefine("exp", NewOperator(exp))
interpreter.SystemDefine("neg", NewOperator(neg)) interpreter.SystemDefine("ln", NewOperator(ln))
interpreter.SystemDefine("round", NewOperator(round)) interpreter.SystemDefine("log", NewOperator(log10))
interpreter.SystemDefine("abs", NewOperator(abs)) interpreter.SystemDefine("rand", NewOperator(randInt))
interpreter.SystemDefine("srand", NewOperator(srand))
interpreter.SystemDefine("rrand", NewOperator(rrand))
} }

View file

@ -1,3 +1,6 @@
// Copyright 2010 The postscript-go Authors. All rights reserved.
// created: 13/12/2010 by Laurent Le Goff
package postscript package postscript

View file

@ -1,18 +1,38 @@
// Copyright 2010 The postscript-go Authors. All rights reserved.
// created: 13/12/2010 by Laurent Le Goff
//Operand Stack Manipulation Operators
package postscript package postscript
//any pop -> Discard top element
func pop(interpreter *Interpreter) { func pop(interpreter *Interpreter) {
interpreter.Pop() interpreter.Pop()
} }
//any1 any2 exch any2 any1 -> Exchange top two elements
func exch(interpreter *Interpreter) {
value1 := interpreter.Pop()
value2 := interpreter.Pop()
interpreter.Push(value1)
interpreter.Push(value2)
}
//any dup any any -> Duplicate top element
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
func copystack(interpreter *Interpreter) {
n := interpreter.PopInt()
values := interpreter.GetValues(n)
for _, value := range values {
interpreter.Push(value)
}
}
//anyn … any0 n index anyn … any0 anyn -> Duplicate arbitrary element
func index(interpreter *Interpreter) { func index(interpreter *Interpreter) {
f := interpreter.PopInt() f := interpreter.PopInt()
interpreter.Push(interpreter.Get(int(f))) interpreter.Push(interpreter.Get(int(f)))
} }
//anyn1 … any0 n j roll any(j1) mod n … any0 anyn1 … anyj mod n -> Roll n elements up j times
func roll(interpreter *Interpreter) { func roll(interpreter *Interpreter) {
j := interpreter.PopInt() j := interpreter.PopInt()
n := interpreter.PopInt() n := interpreter.PopInt()
@ -22,27 +42,47 @@ func roll(interpreter *Interpreter) {
interpreter.Push(values[(n+i-j)%n]) interpreter.Push(values[(n+i-j)%n])
} }
} }
//any1 … anyn clear -> Discard all elements
func copystack(interpreter *Interpreter) { func clear(interpreter *Interpreter) {
n := interpreter.PopInt() interpreter.ClearOperands()
values := interpreter.GetValues(n) }
for _, value := range values { //any1 … anyn count any1 … anyn n -> Count elements on stack
interpreter.Push(value) func count(interpreter *Interpreter) {
interpreter.Push(interpreter.OperandSize())
}
//Mark
type Mark struct{}
// mark mark -> Push mark on stack
func mark(interpreter *Interpreter) {
interpreter.Push(Mark{})
}
//mark obj 1 … obj n cleartomark -> Discard elements down through mark
func cleartomark(interpreter *Interpreter) {
value := interpreter.Pop()
for _, ok := value.(Mark); !ok; {
value = interpreter.Pop()
} }
} }
//mark obj 1 … obj n counttomark mark obj 1 … obj n n -> Count elements down to mark
func exch(interpreter *Interpreter) { func counttomark(interpreter *Interpreter) {
value1 := interpreter.Pop() i := 0
value2 := interpreter.Pop() value := interpreter.Get(i)
interpreter.Push(value1) for _, ok := value.(Mark); !ok; i++ {
interpreter.Push(value2) value = interpreter.Get(i)
}
interpreter.Push(float(i))
} }
func initStackOperator(interpreter * Interpreter) { func initStackOperator(interpreter *Interpreter) {
interpreter.SystemDefine("pop", NewOperator(pop)) interpreter.SystemDefine("pop", NewOperator(pop))
interpreter.SystemDefine("dup", NewOperator(dup))
interpreter.SystemDefine("index", NewOperator(index))
interpreter.SystemDefine("copy", NewOperator(copystack))
interpreter.SystemDefine("roll", NewOperator(roll))
interpreter.SystemDefine("exch", NewOperator(exch)) interpreter.SystemDefine("exch", NewOperator(exch))
interpreter.SystemDefine("dup", NewOperator(dup))
interpreter.SystemDefine("copy", NewOperator(copystack))
interpreter.SystemDefine("index", NewOperator(index))
interpreter.SystemDefine("roll", NewOperator(roll))
interpreter.SystemDefine("clear", NewOperator(clear))
interpreter.SystemDefine("count", NewOperator(count))
interpreter.SystemDefine("mark", NewOperator(mark))
interpreter.SystemDefine("cleartomark", NewOperator(mark))
interpreter.SystemDefine("counttomark", NewOperator(mark))
} }

View file

@ -1,3 +1,6 @@
// Copyright 2010 The postscript-go Authors. All rights reserved.
// created: 13/12/2010 by Laurent Le Goff
package postscript package postscript
type ProcedureDefinition struct { type ProcedureDefinition struct {