create alias instruction

This commit is contained in:
xdrm-brackets 2018-11-13 00:29:57 +01:00
parent ff458d83da
commit 492835a67a
2 changed files with 41 additions and 0 deletions

View File

@ -0,0 +1,37 @@
package instruction
import (
"strings"
)
type alias struct {
raw string
// Name of the alias
Name string
// Value to resolve to
Value string
}
func (d *alias) Raw() string { return strings.Join([]string{"alias", d.raw}, " ") }
func (d *alias) Build(_args string) error {
// 1. extract action fields ()
split := strings.SplitN(_args, " ", 2)
// 2. check syntax
if len(split) != 2 {
return ErrInvalidSyntax
}
d.Name = split[0]
d.Value = split[1]
d.raw = _args
return nil
}
func (d alias) Exec(ctx ExecutionContext) ([]byte, error) {
ctx.Alias[d.Name] = d.Value
return nil, nil
}

View File

@ -40,6 +40,10 @@ func Parse(raw string) (T, error) {
i := &set{}
err := i.Build(split[1])
return i, err
case "alias":
i := &alias{}
err := i.Build(split[1])
return i, err
default:
return nil, ErrUnknownInstruction
}