fc349e2cf1/vm/golang/gonga.go
Commiter: Charles Childers
Author: Charles Childers
Revision: fc349e2cf1
File Size: 2.05 KB
(March 11, 2010 01:12 UTC) About 2 years ago
also remove embedded retroImage
// Original Ngaro, RetroForth, Uki, ...
// Copyright (C) 2008, 2009, Charles Childers
// Go port
// Copyright 2009 JGL
// Public Domain or LICENSE file
/*
Gonga launchs a new ngaro image (retroForth by default). The
vm exposes the function Channel(int id) to get a direct channel:
any int can be used as channel id, but 0 is stdin and 1 is stdout.
Gonga uses these functions in goroutinesthat read from stdin
and write to stdout.
When launching gonga it is posible to specify the image to
load. Multiple images can be given (they will be concatenated)
the special filename "-" means to load the default image.
Usage:
gonga [options] [image file(s)]
Options:
-s=50000: image size
-d="": image dump file
-v=false: verbose output
*/
package main
import (
"./ngaro"
"bufio"
"flag"
"fmt"
"io"
"os"
)
var size = flag.Int("s", 50000, "image size")
var dump = flag.String("d", "retro.img", "image dump file")
var verbose = flag.Bool("v", false, "verbose output")
var retroImage = [...]int{}
func perror(s ...interface{}) {
if *verbose {
fmt.Fprint(os.Stderr, s)
}
}
func readIn(vm *ngaro.NgaroVM) {
r := bufio.NewReader(os.Stdin)
b := make([]byte, 1)
for {
_, err := io.ReadAtLeast(r, b, 1)
if err != nil {
break
}
vm.Write(b)
}
vm.EOI <- true
}
func printOut(vm *ngaro.NgaroVM) {
b := make([]byte, 1)
for {
_, err := vm.Read(b)
if err != nil {
vm.Off <- true
}
fmt.Print(string(b))
vm.OutputDone <- true
}
}
func main() {
flag.Parse()
ngaro.Verbose = *verbose
ngaro.ClearScreen = func() {
fmt.Printf("\033[2J\033[1;1H") // Clear screen
}
var img = make([]int, *size)
perror("Gonga: loading ")
if flag.NArg() == 0 {
img = &retroImage
perror("default (retroForth) ")
}
for i, n := 0, 0; i < flag.NArg(); i++ {
if flag.Arg(i) == "-" {
perror("default (retroForth-10.3) ")
img = &retroImage
img = img[0:cap(img)]
n += len(retroImage)
} else {
perror(flag.Arg(i), " ")
n += ngaro.LoadDump(flag.Arg(i), img, n)
}
}
vm := ngaro.NewVM(img, *size, *dump)
go readIn(vm)
go printOut(vm)
<-vm.Off
} |