diff --git a/postscript-go/TestPostscript.png b/postscript-go/TestPostscript.png index 3039bda..0906fcd 100644 Binary files a/postscript-go/TestPostscript.png and b/postscript-go/TestPostscript.png differ diff --git a/postscript-go/src/cmd/testpostscript.go b/postscript-go/src/cmd/testpostscript.go index ef6091f..80a780f 100644 --- a/postscript-go/src/cmd/testpostscript.go +++ b/postscript-go/src/cmd/testpostscript.go @@ -6,7 +6,9 @@ import ( "time" "log" "os" + "io/ioutil" "bufio" + "strings" "image" "image/png" "draw2d.googlecode.com/svn/trunk/draw2d/src/pkg/draw2d" @@ -43,8 +45,15 @@ func main() { gc.Scale(1, -1) gc.Translate(0, -380) lastTime := time.Nanoseconds() + src, err := os.Open("../../test_files/tiger.ps", 0, 0) + if err != nil { + return + } + defer src.Close() + bytes, err := ioutil.ReadAll(src) + reader := strings.NewReader(string(bytes)) interpreter := postscript.NewInterpreter(gc) - interpreter.ExecuteFile("../../test_files/tiger.ps") + interpreter.Execute(reader) dt := time.Nanoseconds() - lastTime fmt.Printf("Draw image: %f ms\n", float64(dt)*1e-6) saveToPngFile("../../TestPostscript.png", i) diff --git a/postscript-go/src/pkg/postscript/Makefile b/postscript-go/src/pkg/postscript/Makefile index d4e0cce..1739d3b 100644 --- a/postscript-go/src/pkg/postscript/Makefile +++ b/postscript-go/src/pkg/postscript/Makefile @@ -1,16 +1,18 @@ include $(GOROOT)/src/Make.inc TARG=postscript-go.googlecode.com/svn/trunk/postscript-go/src/pkg/postscript -GOFILES=\ +GOFILES=operators_array.go\ + operators_dictionary.go\ + operators_misc.go\ + procedure.go\ interpreter.go\ + operators_conflict.go\ + operators_graphics.go\ + operators_relational.go\ + scanner.go\ operators.go\ operators_control.go\ - operators_dictionary.go\ - operators_graphics.go\ operators_math.go\ - operators_relational.go\ operators_stack.go\ - procedure.go\ - scanner.go\ include $(GOROOT)/src/Make.pkg diff --git a/postscript-go/src/pkg/postscript/interpreter.go b/postscript-go/src/pkg/postscript/interpreter.go index 73df4a2..9987b2f 100644 --- a/postscript-go/src/pkg/postscript/interpreter.go +++ b/postscript-go/src/pkg/postscript/interpreter.go @@ -7,6 +7,7 @@ import ( "os" "log" "strconv" + "io" "draw2d.googlecode.com/svn/trunk/draw2d/src/pkg/draw2d" ) @@ -47,21 +48,26 @@ func NewDictionary(prealloc int) Dictionary { func (interpreter *Interpreter) GetGraphicContext() *draw2d.GraphicContext { return interpreter.gc } - -func (interpreter *Interpreter) ExecuteFile(filePath string) { - src, err := os.Open(filePath, 0, 0) - if src == nil { - log.Printf("can't open file; err=%s\n", err.String()) - return - } +func (interpreter *Interpreter) Execute(reader io.Reader) { var scanner Scanner - scanner.Init(src) + scanner.Init(reader) token := scanner.Scan() for token != EOF { interpreter.scan(&scanner, token) token = scanner.Scan() } } + +func (interpreter *Interpreter) ExecuteFile(filePath string) os.Error { + src, err := os.Open(filePath, 0, 0) + if src == nil { + log.Printf("can't open file; err=%s\n", err.String()) + return err + } + defer src.Close() + interpreter.Execute(src) + return nil +} func (interpreter *Interpreter) computeReference(ref string) { value, _ := interpreter.FindValueInDictionaries(ref) if value == nil {