mirror of
https://github.com/hymkor/lispect.git
synced 2026-09-06 09:07:20 -04:00
Add New function to construct and initialize Env instances
The `New` function creates a new `Env` that embeds a `gmnlisp.World` and sets up the necessary pseudoterminal and inter-goroutine communication channels. This allows users to execute Lisp code with flexible argument control and custom Go function integration, offering more control compared to `RunString` or `RunFile`.
This commit is contained in:
@@ -22,6 +22,7 @@ var ErrCtrlC = errors.New("^C")
|
|||||||
type Env struct {
|
type Env struct {
|
||||||
w *Watcher
|
w *Watcher
|
||||||
term *Term
|
term *Term
|
||||||
|
*gmnlisp.World
|
||||||
closer []func()
|
closer []func()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,6 +31,10 @@ func (env *Env) Close() {
|
|||||||
env.closer[i]()
|
env.closer[i]()
|
||||||
}
|
}
|
||||||
env.closer = nil
|
env.closer = nil
|
||||||
|
if env.term != nil {
|
||||||
|
env.term.Close()
|
||||||
|
env.term = nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (env *Env) send(ctx context.Context, w *gmnlisp.World, args []gmnlisp.Node) (gmnlisp.Node, error) {
|
func (env *Env) send(ctx context.Context, w *gmnlisp.World, args []gmnlisp.Node) (gmnlisp.Node, error) {
|
||||||
|
|||||||
@@ -21,18 +21,14 @@ func RunFile(fname string, args []string) error {
|
|||||||
return RunString(string(script), args)
|
return RunString(string(script), args)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunString executes a Lisp script given directly as a string.
|
// New creates a new Env instance that embeds a gmnlisp.World and
|
||||||
//
|
// sets up a pseudoterminal session and related communication structures.
|
||||||
// - script: the Lisp source code to be executed (equivalent to the contents of a file).
|
// This allows users to customize and control the Lisp environment flexibly.
|
||||||
// - args: typically corresponds to os.Args[1:].
|
func New() (*Env, error) {
|
||||||
// - args[0]: the script file name (not used directly).
|
|
||||||
// - args[1:]: arguments passed to the script.
|
|
||||||
func RunString(script string, args []string) error {
|
|
||||||
term, err := NewTerm()
|
term, err := NewTerm()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer term.Close()
|
|
||||||
|
|
||||||
watcher := NewWatcher(term)
|
watcher := NewWatcher(term)
|
||||||
|
|
||||||
@@ -40,13 +36,12 @@ func RunString(script string, args []string) error {
|
|||||||
term: term,
|
term: term,
|
||||||
w: watcher,
|
w: watcher,
|
||||||
}
|
}
|
||||||
defer env.Close()
|
|
||||||
|
|
||||||
gmnlisp.NewLineOnFormat = []byte{'\r', '\n'}
|
gmnlisp.NewLineOnFormat = []byte{'\r', '\n'}
|
||||||
|
|
||||||
lisp := gmnlisp.New()
|
lisp := gmnlisp.New()
|
||||||
|
|
||||||
lisp = lisp.Flet(
|
env.World = lisp.Flet(
|
||||||
gmnlisp.Functions{
|
gmnlisp.Functions{
|
||||||
gmnlisp.NewSymbol("send"): &gmnlisp.Function{Min: 1, F: env.send},
|
gmnlisp.NewSymbol("send"): &gmnlisp.Function{Min: 1, F: env.send},
|
||||||
gmnlisp.NewSymbol("sendln"): &gmnlisp.Function{Min: 1, F: env.sendln},
|
gmnlisp.NewSymbol("sendln"): &gmnlisp.Function{Min: 1, F: env.sendln},
|
||||||
@@ -57,6 +52,23 @@ func RunString(script string, args []string) error {
|
|||||||
gmnlisp.NewSymbol("setenv"): gmnlisp.Function2(env.setenv),
|
gmnlisp.NewSymbol("setenv"): gmnlisp.Function2(env.setenv),
|
||||||
gmnlisp.NewSymbol("wait"): gmnlisp.Function1(env.wait),
|
gmnlisp.NewSymbol("wait"): gmnlisp.Function1(env.wait),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return env, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunString executes a Lisp script given directly as a string.
|
||||||
|
//
|
||||||
|
// - script: the Lisp source code to be executed (equivalent to the contents of a file).
|
||||||
|
// - args: typically corresponds to os.Args[1:].
|
||||||
|
// - args[0]: the script file name (not used directly).
|
||||||
|
// - args[1:]: arguments passed to the script.
|
||||||
|
func RunString(script string, args []string) error {
|
||||||
|
env, err := New()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer env.Close()
|
||||||
|
|
||||||
posixArgv := []gmnlisp.Node{}
|
posixArgv := []gmnlisp.Node{}
|
||||||
for _, s := range args[1:] {
|
for _, s := range args[1:] {
|
||||||
posixArgv = append(posixArgv, gmnlisp.String(s))
|
posixArgv = append(posixArgv, gmnlisp.String(s))
|
||||||
@@ -65,7 +77,7 @@ func RunString(script string, args []string) error {
|
|||||||
if value, err := os.Executable(); err == nil {
|
if value, err := os.Executable(); err == nil {
|
||||||
executable = value
|
executable = value
|
||||||
}
|
}
|
||||||
lisp = lisp.Let(gmnlisp.Variables{
|
lisp := env.Let(gmnlisp.Variables{
|
||||||
gmnlisp.NewSymbol("ARGV"): gmnlisp.List(posixArgv...),
|
gmnlisp.NewSymbol("ARGV"): gmnlisp.List(posixArgv...),
|
||||||
gmnlisp.NewSymbol("*argv*"): gmnlisp.List(posixArgv...),
|
gmnlisp.NewSymbol("*argv*"): gmnlisp.List(posixArgv...),
|
||||||
gmnlisp.NewSymbol("PROGRAM-NAME"): gmnlisp.String(args[0]),
|
gmnlisp.NewSymbol("PROGRAM-NAME"): gmnlisp.String(args[0]),
|
||||||
|
|||||||
Reference in New Issue
Block a user