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/png"
"draw2d.googlecode.com/svn/trunk/draw2d/src/pkg/draw2d"
//"postscript"
"postscript-go.googlecode.com/svn/trunk/postscript-go/src/pkg/postscript"
"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
import (
@ -215,6 +218,12 @@ func (interpreter *Interpreter) OperandSize() int {
return len(interpreter.valueStack)
}
func (interpreter *Interpreter) ClearOperands() {
interpreter.valueStack = interpreter.valueStack[0:0]
}
// misc pop
func (interpreter *Interpreter) PopFloat() float {
operand := interpreter.Pop()
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
type OperatorFunc func(interpreter *Interpreter)
@ -22,7 +25,6 @@ func restore(interpreter *Interpreter) {
}
func initSystemOperators(interpreter *Interpreter) {
interpreter.SystemDefine("save", NewOperator(save))
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
@ -33,9 +36,8 @@ func foroperator(interpreter *Interpreter) {
}
func initControlOperators(interpreter *Interpreter) {
interpreter.SystemDefine("if", NewOperator(ifoperator))
interpreter.SystemDefine("ifelse", NewOperator(ifelse))
interpreter.SystemDefine("for", NewOperator(foroperator))
}
}

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
@ -87,4 +90,4 @@ func initDictionaryOperators(interpreter *Interpreter) {
interpreter.SystemDefine("end", NewOperator(end))
interpreter.SystemDefine("dict", NewOperator(dict))
interpreter.SystemDefine("where", NewOperator(where))
}
}

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
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
import (
"math"
"rand"
)
// begin Primitive Operator implementation
//num1 num2 add sum -> Return num1 plus num2
func add(interpreter *Interpreter) {
f2 := interpreter.PopFloat()
f1 := interpreter.PopFloat()
interpreter.Push(f1 + f2)
num2 := interpreter.PopFloat()
num1 := interpreter.PopFloat()
interpreter.Push(num1 + num2)
}
func sub(interpreter *Interpreter) {
f2 := interpreter.PopFloat()
f1 := interpreter.PopFloat()
interpreter.Push(f1 - f2)
}
//num1 num2 div quotient -> Return num1 divided by num2
func div(interpreter *Interpreter) {
f2 := interpreter.PopFloat()
f1 := interpreter.PopFloat()
interpreter.Push(f1 / f2)
}
func mul(interpreter *Interpreter) {
f2 := interpreter.PopFloat()
f1 := interpreter.PopFloat()
interpreter.Push(f1 * f2)
num2 := interpreter.PopFloat()
num1 := interpreter.PopFloat()
interpreter.Push(num1 / num2)
}
//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) {
f := interpreter.PopFloat()
interpreter.Push(float(math.Sqrt(float64(f))))
}
//num den atan angle -> Return arctangent of num/den in degrees
func atan(interpreter *Interpreter) {
den := interpreter.PopFloat()
num := interpreter.PopFloat()
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) {
a := interpreter.PopFloat()
interpreter.Push(float(math.Cos(float64(a))) * (180.0 / math.Pi))
}
//angle sin real -> Return sine of angle degrees
func sin(interpreter *Interpreter) {
a := interpreter.PopFloat()
interpreter.Push(float(math.Sin(float64(a))) * (180.0 / math.Pi))
}
func neg(interpreter *Interpreter) {
f := interpreter.PopFloat()
interpreter.Push(-f)
//base exponent exp real -> Raise base to exponent power
func exp(interpreter *Interpreter) {
exponent := interpreter.PopFloat()
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) {
f := interpreter.PopFloat()
interpreter.Push(float(int(f + 0.5)))
var randGenerator *rand.Rand
//int srand -> Set random number seed
func srand(interpreter *Interpreter) {
randGenerator = rand.New(rand.NewSource(int64(interpreter.PopInt())))
}
func abs(interpreter *Interpreter) {
f := interpreter.PopFloat()
interpreter.Push(float(math.Fabs(float64(f))))
// rrand int -> Return random number seed
func rrand(interpreter *Interpreter) {
interpreter.Push(float(randGenerator.Int()))
}
func initMathOperators(interpreter *Interpreter) {
interpreter.SystemDefine("add", NewOperator(add))
interpreter.SystemDefine("sub", NewOperator(sub))
interpreter.SystemDefine("mul", NewOperator(mul))
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("atan", NewOperator(atan))
interpreter.SystemDefine("cos", NewOperator(cos))
interpreter.SystemDefine("sin", NewOperator(sin))
interpreter.SystemDefine("atan", NewOperator(atan))
interpreter.SystemDefine("neg", NewOperator(neg))
interpreter.SystemDefine("round", NewOperator(round))
interpreter.SystemDefine("abs", NewOperator(abs))
}
interpreter.SystemDefine("exp", NewOperator(exp))
interpreter.SystemDefine("ln", NewOperator(ln))
interpreter.SystemDefine("log", NewOperator(log10))
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
@ -16,4 +19,4 @@ func lt(interpreter *Interpreter) {
func initRelationalOperators(interpreter *Interpreter) {
interpreter.SystemDefine("eq", NewOperator(eq))
interpreter.SystemDefine("lt", NewOperator(lt))
}
}

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
//any pop -> Discard top element
func pop(interpreter *Interpreter) {
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) {
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) {
f := interpreter.PopInt()
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) {
j := interpreter.PopInt()
n := interpreter.PopInt()
@ -22,27 +42,47 @@ func roll(interpreter *Interpreter) {
interpreter.Push(values[(n+i-j)%n])
}
}
func copystack(interpreter *Interpreter) {
n := interpreter.PopInt()
values := interpreter.GetValues(n)
for _, value := range values {
interpreter.Push(value)
//any1 … anyn clear -> Discard all elements
func clear(interpreter *Interpreter) {
interpreter.ClearOperands()
}
//any1 … anyn count any1 … anyn n -> Count elements on stack
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()
}
}
func exch(interpreter *Interpreter) {
value1 := interpreter.Pop()
value2 := interpreter.Pop()
interpreter.Push(value1)
interpreter.Push(value2)
//mark obj 1 … obj n counttomark mark obj 1 … obj n n -> Count elements down to mark
func counttomark(interpreter *Interpreter) {
i := 0
value := interpreter.Get(i)
for _, ok := value.(Mark); !ok; i++ {
value = interpreter.Get(i)
}
interpreter.Push(float(i))
}
func initStackOperator(interpreter * Interpreter) {
func initStackOperator(interpreter *Interpreter) {
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("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
type ProcedureDefinition struct {