Initial version of prototype cmdlang
This commit is contained in:
commit
781a761ead
10 changed files with 224 additions and 0 deletions
26
cmdlang/ast.go
Normal file
26
cmdlang/ast.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package cmdlang
|
||||
|
||||
import (
|
||||
"github.com/alecthomas/participle/v2"
|
||||
"io"
|
||||
)
|
||||
|
||||
type astLiteral struct {
|
||||
Str *string `parser:"@String"`
|
||||
Ident *string `parser:" | @Ident"`
|
||||
}
|
||||
|
||||
type astCmdArg struct {
|
||||
Literal astLiteral `parser:"@@"`
|
||||
}
|
||||
|
||||
type astCmd struct {
|
||||
Name string `parser:"@Ident"`
|
||||
Args []astCmdArg `parser:"@@*"`
|
||||
}
|
||||
|
||||
var parser = participle.MustBuild[astCmd]()
|
||||
|
||||
func parse(r io.Reader) (*astCmd, error) {
|
||||
return parser.Parse("test", r)
|
||||
}
|
||||
21
cmdlang/builtins.go
Normal file
21
cmdlang/builtins.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package cmdlang
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func echoBuiltin(ctx context.Context, args invocationArgs) error {
|
||||
if len(args.args) == 0 {
|
||||
log.Print()
|
||||
return nil
|
||||
}
|
||||
|
||||
var line strings.Builder
|
||||
for _, arg := range args.args {
|
||||
line.WriteString(arg)
|
||||
}
|
||||
log.Print(line.String())
|
||||
return nil
|
||||
}
|
||||
32
cmdlang/env.go
Normal file
32
cmdlang/env.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package cmdlang
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
type evalCtx struct {
|
||||
parent *evalCtx
|
||||
commands map[string]invokable
|
||||
}
|
||||
|
||||
func (ec *evalCtx) addCmd(name string, inv invokable) {
|
||||
if ec.commands == nil {
|
||||
ec.commands = make(map[string]invokable)
|
||||
}
|
||||
|
||||
ec.commands[name] = inv
|
||||
}
|
||||
|
||||
func (ec *evalCtx) lookupCmd(name string) (invokable, error) {
|
||||
for e := ec; e != nil; e = e.parent {
|
||||
if ec.commands == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if cmd, ok := ec.commands[name]; ok {
|
||||
return cmd, nil
|
||||
}
|
||||
|
||||
}
|
||||
return nil, errors.New("name " + name + " not found")
|
||||
}
|
||||
47
cmdlang/eval.go
Normal file
47
cmdlang/eval.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package cmdlang
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/lmika/gopkgs/fp/slices"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type evaluator struct {
|
||||
}
|
||||
|
||||
func (e evaluator) evaluate(ctx context.Context, ec *evalCtx, ast *astCmd) error {
|
||||
cmd, err := ec.lookupCmd(ast.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
args, err := slices.MapWithError(ast.Args, func(a astCmdArg) (string, error) {
|
||||
return e.evalArg(ctx, ec, a)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return cmd.invoke(ctx, invocationArgs{
|
||||
args: args,
|
||||
})
|
||||
}
|
||||
|
||||
func (e evaluator) evalArg(ctx context.Context, ec *evalCtx, n astCmdArg) (string, error) {
|
||||
return e.evalLiteral(ctx, ec, n.Literal)
|
||||
}
|
||||
|
||||
func (e evaluator) evalLiteral(ctx context.Context, ec *evalCtx, n astLiteral) (string, error) {
|
||||
switch {
|
||||
case n.Str != nil:
|
||||
uq, err := strconv.Unquote(*n.Str)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return uq, nil
|
||||
case n.Ident != nil:
|
||||
return *n.Ident, nil
|
||||
}
|
||||
return "", errors.New("unhandled literal type")
|
||||
}
|
||||
30
cmdlang/inst.go
Normal file
30
cmdlang/inst.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package cmdlang
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Inst struct {
|
||||
rootEC *evalCtx
|
||||
}
|
||||
|
||||
func New() *Inst {
|
||||
rootEC := evalCtx{}
|
||||
rootEC.addCmd("echo", invokableFunc(echoBuiltin))
|
||||
|
||||
return &Inst{
|
||||
rootEC: &rootEC,
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: return value?
|
||||
func (inst *Inst) Eval(ctx context.Context, expr string) error {
|
||||
ast, err := parse(strings.NewReader(expr))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
eval := evaluator{}
|
||||
return eval.evaluate(ctx, inst.rootEC, ast)
|
||||
}
|
||||
17
cmdlang/objs.go
Normal file
17
cmdlang/objs.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package cmdlang
|
||||
|
||||
import "context"
|
||||
|
||||
type invocationArgs struct {
|
||||
args []string
|
||||
}
|
||||
|
||||
type invokable interface {
|
||||
invoke(ctx context.Context, args invocationArgs) error
|
||||
}
|
||||
|
||||
type invokableFunc func(ctx context.Context, args invocationArgs) error
|
||||
|
||||
func (i invokableFunc) invoke(ctx context.Context, args invocationArgs) error {
|
||||
return i(ctx, args)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue