Browse Source

fix: Allow adding fixed custom rules for CLI interpreter

Matthias Ladkau 3 years ago
parent
commit
ea62e5aaa9
2 changed files with 19 additions and 2 deletions
  1. 11 2
      cli/tool/interpret.go
  2. 8 0
      cli/tool/interpret_test.go

+ 11 - 2
cli/tool/interpret.go

@@ -20,10 +20,12 @@ import (
 	"path/filepath"
 	"strings"
 
+	"devt.de/krotik/common/errorutil"
 	"devt.de/krotik/common/fileutil"
 	"devt.de/krotik/common/stringutil"
 	"devt.de/krotik/common/termutil"
 	"devt.de/krotik/ecal/config"
+	"devt.de/krotik/ecal/engine"
 	"devt.de/krotik/ecal/interpreter"
 	"devt.de/krotik/ecal/parser"
 	"devt.de/krotik/ecal/scope"
@@ -55,6 +57,7 @@ type CLIInterpreter struct {
 	CustomHandler        CLICustomHandler
 	CustomWelcomeMessage string
 	CustomHelpString     string
+	CustomRules          []*engine.Rule
 
 	EntryFile   string // Entry file for the program
 	LoadPlugins bool   // Flag if stdlib plugins should be loaded
@@ -78,8 +81,8 @@ type CLIInterpreter struct {
 NewCLIInterpreter creates a new commandline interpreter for ECAL.
 */
 func NewCLIInterpreter() *CLIInterpreter {
-	return &CLIInterpreter{scope.NewScope(scope.GlobalScope), nil, nil, "", "", "",
-		true, nil, nil, nil, nil, os.Stdout}
+	return &CLIInterpreter{scope.NewScope(scope.GlobalScope), nil, nil, "", "",
+		[]*engine.Rule{}, "", true, nil, nil, nil, nil, os.Stdout}
 }
 
 /*
@@ -180,6 +183,12 @@ func (i *CLIInterpreter) LoadInitialFile(tid uint64) error {
 	i.RuntimeProvider.Processor.Finish()
 	i.RuntimeProvider.Processor.Reset()
 
+	// Add custom rules
+
+	for _, r := range i.CustomRules {
+		errorutil.AssertOk(i.RuntimeProvider.Processor.AddRule(r))
+	}
+
 	if i.CustomHandler != nil {
 		i.CustomHandler.LoadInitialFile(tid)
 	}

+ 8 - 0
cli/tool/interpret_test.go

@@ -25,6 +25,7 @@ import (
 	"devt.de/krotik/common/errorutil"
 	"devt.de/krotik/common/fileutil"
 	"devt.de/krotik/ecal/config"
+	"devt.de/krotik/ecal/engine"
 	"devt.de/krotik/ecal/interpreter"
 	"devt.de/krotik/ecal/stdlib"
 	"devt.de/krotik/ecal/util"
@@ -66,6 +67,13 @@ func newTestInterpreterWithConfig() *CLIInterpreter {
 	tin.Dir = &l
 
 	tin.CustomWelcomeMessage = "123"
+	tin.CustomRules = []*engine.Rule{
+		{
+			Name:       "myrule",
+			ScopeMatch: []string{},
+			KindMatch:  []string{"my.custom.rule"},
+		},
+	}
 
 	return tin
 }