about summary refs log tree commit diff
path: root/README.md
blob: 0669111c21503dd1fea2a70cfbb79bda3474ebc4 (plain)
1
2
3
4
# specimen

this is a practical coding test!
hopefully you will find it to your liking :3
* Literal.String.Doc */ .highlight .s2 { color: #87CEEB } /* Literal.String.Double */ .highlight .se { color: #87CEEB } /* Literal.String.Escape */ .highlight .sh { color: #87CEEB } /* Literal.String.Heredoc */ .highlight .si { color: #87CEEB } /* Literal.String.Interpol */ .highlight .sx { color: #87CEEB } /* Literal.String.Other */ .highlight .sr { color: #87CEEB } /* Literal.String.Regex */ .highlight .s1 { color: #87CEEB } /* Literal.String.Single */ .highlight .ss { color: #87CEEB } /* Literal.String.Symbol */ .highlight .bp { color: #DDD } /* Name.Builtin.Pseudo */ .highlight .fm { color: #FF0 } /* Name.Function.Magic */ .highlight .vc { color: #EEDD82 } /* Name.Variable.Class */ .highlight .vg { color: #EEDD82 } /* Name.Variable.Global */ .highlight .vi { color: #EEDD82 } /* Name.Variable.Instance */ .highlight .vm { color: #EEDD82 } /* Name.Variable.Magic */ .highlight .il { color: #F0F } /* Literal.Number.Integer.Long */
package bot

import (
	"jinx/pkg/discord/events"
	"jinx/pkg/lang/vm"
	"jinx/pkg/lang/vm/text"
	"strings"
)

type Cmd func(b *Bot, content string, msg events.Message) error

var (
	nameToCmd = map[string]Cmd{
		"ping": pingCmd,
		"vm":   vmCmd,
	}
)

func pingCmd(b *Bot, content string, msg events.Message) error {
	return b.client.SendMessage(msg.ChannelID, "pong")
}

func vmCmd(b *Bot, content string, msg events.Message) error {
	src := strings.Split(content, "```")[1]

	comp := text.NewCompiler(strings.NewReader(src))
	bc, err := comp.Compile()
	if err != nil {
		b.logger.Error().Err(err).Msg("error compiling code")
		return err
	}

	vm := vm.New(&bc)

	if err := vm.Run(); err != nil {
		return err
	}

	res, err := vm.GetResult()
	if err != nil {
		return err
	}

	b.logger.Debug().Msg("executed code")

	return b.client.SendMessage(msg.ChannelID, res)
}