93 lines
1.4 KiB
Go
93 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path"
|
|
pcl "pcl/interp"
|
|
)
|
|
|
|
func main() {
|
|
var (
|
|
interp *pcl.Interp
|
|
f *os.File
|
|
r *bufio.Reader
|
|
c rune
|
|
node *pcl.Cnode_t
|
|
v pcl.Value_t
|
|
|
|
err error
|
|
)
|
|
|
|
if len(os.Args) != 2 {
|
|
fmt.Printf("Usage: %s filename\n", path.Base(os.Args[0]))
|
|
goto oops
|
|
}
|
|
|
|
interp, err = pcl.NewInterp(1000, true)
|
|
if err != nil {
|
|
fmt.Printf("Error %s\n", err.Error())
|
|
goto oops
|
|
}
|
|
|
|
f, err = os.Open(os.Args[1])
|
|
if err != nil {
|
|
fmt.Printf("Error %s\n", err.Error())
|
|
goto oops
|
|
}
|
|
|
|
interp.BeginFeed()
|
|
|
|
r = bufio.NewReader(f)
|
|
for {
|
|
c, _, err = r.ReadRune()
|
|
if err != nil {
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
fmt.Printf("Error in ReadRune - %s\n", err.Error())
|
|
goto oops
|
|
}
|
|
|
|
err = interp.FeedRune(c)
|
|
if err != nil {
|
|
fmt.Printf("Error in FeedRune - %s\n", err.Error())
|
|
goto oops
|
|
}
|
|
}
|
|
node, err = interp.EndFeed()
|
|
if err != nil {
|
|
fmt.Printf("Error in EndFeed %s\n", err.Error())
|
|
goto oops
|
|
}
|
|
|
|
interp.Dump(node)
|
|
fmt.Printf("------------- Executing ----------------\n")
|
|
v, err = interp.Execute(node)
|
|
if err != nil {
|
|
fmt.Printf("Error in Execute - %s\n", err.Error())
|
|
goto oops
|
|
}
|
|
|
|
if v.Kind != pcl.VALUE_STR {
|
|
panic("return value must not be string")
|
|
}
|
|
fmt.Printf("RETURN VALUE = [%s]\n", *(*string)(v.V))
|
|
|
|
interp.Close()
|
|
f.Close()
|
|
os.Exit(0)
|
|
|
|
oops:
|
|
if interp != nil {
|
|
interp.Close()
|
|
|
|
}
|
|
if f != nil {
|
|
f.Close()
|
|
}
|
|
os.Exit(1)
|
|
}
|