137 lines
2.8 KiB
Go
137 lines
2.8 KiB
Go
package interpreter
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
type Interpreter struct {
|
|
RegexMap RegexMap
|
|
Funcs Funcs
|
|
// Env map[string]string
|
|
// Funcs map[string]func(args ...string) string
|
|
// Vars map[string]string
|
|
Statments Statments
|
|
LogicSplitters []string
|
|
}
|
|
type Funcs map[string]*Func
|
|
type Func struct {
|
|
Name string
|
|
Function Function
|
|
}
|
|
type Function func(args ...any) string
|
|
type Statments map[string]*Statment
|
|
type Statment struct {
|
|
Name string
|
|
Value string
|
|
RegexSplit []string
|
|
}
|
|
|
|
type RegexMap map[string]*Regex
|
|
type Regex struct {
|
|
Name string
|
|
Value string
|
|
}
|
|
|
|
func New() *Interpreter {
|
|
i := &Interpreter{}
|
|
return i
|
|
}
|
|
|
|
type InterpreterInterface interface {
|
|
}
|
|
|
|
func (i *Interpreter) Regex(name string, regx string) *Regex {
|
|
i.RegexMap = make(RegexMap)
|
|
i.RegexMap[name] = &Regex{
|
|
Name: name,
|
|
Value: regx,
|
|
}
|
|
return i.RegexMap[name]
|
|
}
|
|
|
|
func (i *Interpreter) AddStatment(name string, stmnt string) *Statment {
|
|
if _, ok := i.Statments[name]; !ok {
|
|
i.Statments = make(Statments)
|
|
}
|
|
i.Statments[name] = &Statment{
|
|
Value: stmnt,
|
|
Name: name,
|
|
}
|
|
return i.Statments[name]
|
|
}
|
|
func (fncs Funcs) GetFunc(funcName string) *Func {
|
|
fn := fncs[funcName]
|
|
return fn
|
|
}
|
|
func (i *Interpreter) Func(funcName string, fn func(args ...any) string) *Func {
|
|
if i.Funcs == nil {
|
|
i.Funcs = make(Funcs)
|
|
}
|
|
newFunc := &Func{
|
|
Name: funcName,
|
|
Function: fn,
|
|
}
|
|
i.Funcs[funcName] = newFunc
|
|
return i.Funcs[funcName]
|
|
}
|
|
func (i *Interpreter) GetRegex(name string) *Regex {
|
|
return i.RegexMap[name]
|
|
}
|
|
|
|
func (i *Interpreter) CutPrefix(slc []string, prefix string) []string {
|
|
//TODO: get the slc from the struct... need to implement the setter before
|
|
for i, v := range slc {
|
|
clean, _ := strings.CutPrefix(v, prefix)
|
|
slc[i] = clean
|
|
}
|
|
|
|
return slc
|
|
}
|
|
|
|
func (s *Statment) SplitStatementByRegex(regx *Regex) *Statment {
|
|
|
|
input := s.Value
|
|
re := regexp.MustCompile(string(regx.Value))
|
|
matches := re.FindAllStringSubmatch(input, -1)
|
|
if len(matches) == 0 {
|
|
return s // Return original if no backticks found
|
|
}
|
|
|
|
result := []string{}
|
|
lastIndex := 0
|
|
|
|
for _, match := range matches {
|
|
|
|
startIndex := strings.Index(input, match[0])
|
|
|
|
if startIndex > lastIndex {
|
|
result = append(result, input[lastIndex:startIndex])
|
|
}
|
|
|
|
result = append(result, match[1]) // Append the content within backticks
|
|
lastIndex = startIndex + len(match[0])
|
|
}
|
|
|
|
if lastIndex < len(input) {
|
|
result = append(result, input[lastIndex:])
|
|
}
|
|
s.RegexSplit = result
|
|
|
|
return s
|
|
}
|
|
func (s Statments) ForEach(fn func(stmnt *Statment)) {
|
|
for _, stmntData := range s {
|
|
fn(stmntData)
|
|
}
|
|
}
|
|
|
|
// func (i *Interpreter) AddLogicSplitters(splitter ...string) {
|
|
// for _, s := range splitter {
|
|
// i.addLogicSplitter(s)
|
|
// }
|
|
// }
|
|
|
|
// func (i *Interpreter) addLogicSplitter(splitter string) {
|
|
// i.LogicSplitters = append(i.LogicSplitters, splitter)
|
|
// }
|