From be8e2922d393bf26bfc4c4769fd17eb3a3672519 Mon Sep 17 00:00:00 2001 From: "legoff.laurent" Date: Mon, 13 Dec 2010 12:01:24 +0000 Subject: [PATCH] document operators --- postscript-go/src/cmd/testpostscript.go | 4 +- .../src/pkg/postscript/interpreter.go | 9 + postscript-go/src/pkg/postscript/operators.go | 4 +- .../src/pkg/postscript/operators_control.go | 6 +- .../pkg/postscript/operators_dictionary.go | 5 +- .../src/pkg/postscript/operators_graphics.go | 3 + .../src/pkg/postscript/operators_math.go | 159 +++++++++++++----- .../pkg/postscript/operators_relational.go | 5 +- .../src/pkg/postscript/operators_stack.go | 82 ++++++--- postscript-go/src/pkg/postscript/procedure.go | 3 + 10 files changed, 210 insertions(+), 70 deletions(-) diff --git a/postscript-go/src/cmd/testpostscript.go b/postscript-go/src/cmd/testpostscript.go index 9835f6e..680805e 100644 --- a/postscript-go/src/cmd/testpostscript.go +++ b/postscript-go/src/cmd/testpostscript.go @@ -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" ) diff --git a/postscript-go/src/pkg/postscript/interpreter.go b/postscript-go/src/pkg/postscript/interpreter.go index 7c48489..1ddbc26 100644 --- a/postscript-go/src/pkg/postscript/interpreter.go +++ b/postscript-go/src/pkg/postscript/interpreter.go @@ -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 { diff --git a/postscript-go/src/pkg/postscript/operators.go b/postscript-go/src/pkg/postscript/operators.go index 0b52b0e..2325ed2 100644 --- a/postscript-go/src/pkg/postscript/operators.go +++ b/postscript-go/src/pkg/postscript/operators.go @@ -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)) diff --git a/postscript-go/src/pkg/postscript/operators_control.go b/postscript-go/src/pkg/postscript/operators_control.go index 21e7dfb..0b87b7e 100644 --- a/postscript-go/src/pkg/postscript/operators_control.go +++ b/postscript-go/src/pkg/postscript/operators_control.go @@ -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)) -} \ No newline at end of file +} diff --git a/postscript-go/src/pkg/postscript/operators_dictionary.go b/postscript-go/src/pkg/postscript/operators_dictionary.go index 5346939..8cd6ecf 100644 --- a/postscript-go/src/pkg/postscript/operators_dictionary.go +++ b/postscript-go/src/pkg/postscript/operators_dictionary.go @@ -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)) -} \ No newline at end of file +} diff --git a/postscript-go/src/pkg/postscript/operators_graphics.go b/postscript-go/src/pkg/postscript/operators_graphics.go index fd977cc..f135ed5 100644 --- a/postscript-go/src/pkg/postscript/operators_graphics.go +++ b/postscript-go/src/pkg/postscript/operators_graphics.go @@ -1,3 +1,6 @@ +// Copyright 2010 The postscript-go Authors. All rights reserved. +// created: 13/12/2010 by Laurent Le Goff + package postscript import ( diff --git a/postscript-go/src/pkg/postscript/operators_math.go b/postscript-go/src/pkg/postscript/operators_math.go index 961daed..5267ed2 100644 --- a/postscript-go/src/pkg/postscript/operators_math.go +++ b/postscript-go/src/pkg/postscript/operators_math.go @@ -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)) -} \ No newline at end of file + 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)) + +} diff --git a/postscript-go/src/pkg/postscript/operators_relational.go b/postscript-go/src/pkg/postscript/operators_relational.go index 7488b53..546a90f 100644 --- a/postscript-go/src/pkg/postscript/operators_relational.go +++ b/postscript-go/src/pkg/postscript/operators_relational.go @@ -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)) -} \ No newline at end of file +} diff --git a/postscript-go/src/pkg/postscript/operators_stack.go b/postscript-go/src/pkg/postscript/operators_stack.go index f7096b8..e55a4ea 100644 --- a/postscript-go/src/pkg/postscript/operators_stack.go +++ b/postscript-go/src/pkg/postscript/operators_stack.go @@ -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))) } - +//anyn−1 … any0 n j roll any(j−1) mod n … any0 anyn−1 … 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)) -} \ No newline at end of file + 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)) +} diff --git a/postscript-go/src/pkg/postscript/procedure.go b/postscript-go/src/pkg/postscript/procedure.go index c17fdee..f14c721 100644 --- a/postscript-go/src/pkg/postscript/procedure.go +++ b/postscript-go/src/pkg/postscript/procedure.go @@ -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 {