pcl/bin/main.go

93 lines
1.4 KiB
Go
Raw Permalink Normal View History

2023-07-21 09:32:51 +00:00
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
2023-07-21 09:32:51 +00:00
)
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
}
2023-08-13 16:08:37 +00:00
if v.Kind != pcl.VALUE_STR {
panic("return value must not be string")
2023-07-21 09:32:51 +00:00
}
2023-08-13 16:08:37 +00:00
fmt.Printf("RETURN VALUE = [%s]\n", *(*string)(v.V))
2023-07-21 09:32:51 +00:00
interp.Close()
f.Close()
os.Exit(0)
oops:
if interp != nil {
interp.Close()
}
if f != nil {
f.Close()
}
os.Exit(1)
}