interpret.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package tool
  11. import (
  12. "encoding/json"
  13. "flag"
  14. "fmt"
  15. "io"
  16. "io/ioutil"
  17. "os"
  18. "path/filepath"
  19. "strings"
  20. "devt.de/krotik/common/fileutil"
  21. "devt.de/krotik/common/stringutil"
  22. "devt.de/krotik/common/termutil"
  23. "devt.de/krotik/ecal/config"
  24. "devt.de/krotik/ecal/interpreter"
  25. "devt.de/krotik/ecal/parser"
  26. "devt.de/krotik/ecal/scope"
  27. "devt.de/krotik/ecal/stdlib"
  28. "devt.de/krotik/ecal/util"
  29. )
  30. /*
  31. CLICustomHandler is a handler for custom operations.
  32. */
  33. type CLICustomHandler interface {
  34. CLIInputHandler
  35. /*
  36. LoadInitialFile clears the global scope and reloads the initial file.
  37. */
  38. LoadInitialFile(tid uint64) error
  39. }
  40. /*
  41. CLIInterpreter is a commandline interpreter for ECAL.
  42. */
  43. type CLIInterpreter struct {
  44. GlobalVS parser.Scope // Global variable scope
  45. RuntimeProvider *interpreter.ECALRuntimeProvider // Runtime provider of the interpreter
  46. // Customizations of output and input handling
  47. CustomHandler CLICustomHandler
  48. CustomWelcomeMessage string
  49. CustomHelpString string
  50. EntryFile string // Entry file for the program
  51. LoadPlugins bool // Flag if stdlib plugins should be loaded
  52. // Parameter these can either be set programmatically or via CLI args
  53. Dir *string // Root dir for interpreter
  54. LogFile *string // Logfile (blank for stdout)
  55. LogLevel *string // Log level string (Debug, Info, Error)
  56. // User terminal
  57. Term termutil.ConsoleLineTerminal
  58. // Log output
  59. LogOut io.Writer
  60. }
  61. /*
  62. NewCLIInterpreter creates a new commandline interpreter for ECAL.
  63. */
  64. func NewCLIInterpreter() *CLIInterpreter {
  65. return &CLIInterpreter{scope.NewScope(scope.GlobalScope), nil, nil, "", "", "",
  66. true, nil, nil, nil, nil, os.Stdout}
  67. }
  68. /*
  69. ParseArgs parses the command line arguments. Call this after adding custon flags.
  70. Returns true if the program should exit.
  71. */
  72. func (i *CLIInterpreter) ParseArgs() bool {
  73. if i.Dir != nil && i.LogFile != nil && i.LogLevel != nil {
  74. return false
  75. }
  76. wd, _ := os.Getwd()
  77. i.Dir = flag.String("dir", wd, "Root directory for ECAL interpreter")
  78. i.LogFile = flag.String("logfile", "", "Log to a file")
  79. i.LogLevel = flag.String("loglevel", "Info", "Logging level (Debug, Info, Error)")
  80. showHelp := flag.Bool("help", false, "Show this help message")
  81. flag.Usage = func() {
  82. fmt.Fprintln(flag.CommandLine.Output())
  83. fmt.Fprintln(flag.CommandLine.Output(), fmt.Sprintf("Usage of %s run [options] [file]", osArgs[0]))
  84. fmt.Fprintln(flag.CommandLine.Output())
  85. flag.PrintDefaults()
  86. fmt.Fprintln(flag.CommandLine.Output())
  87. }
  88. if len(osArgs) >= 2 {
  89. flag.CommandLine.Parse(osArgs[2:])
  90. if cargs := flag.Args(); len(cargs) > 0 {
  91. i.EntryFile = flag.Arg(0)
  92. }
  93. if *showHelp {
  94. flag.Usage()
  95. }
  96. }
  97. return *showHelp
  98. }
  99. /*
  100. CreateRuntimeProvider creates the runtime provider of this interpreter. This function expects Dir,
  101. LogFile and LogLevel to be set.
  102. */
  103. func (i *CLIInterpreter) CreateRuntimeProvider(name string) error {
  104. var logger util.Logger
  105. var err error
  106. if i.RuntimeProvider != nil {
  107. return nil
  108. }
  109. // Check if we should log to a file
  110. if i.LogFile != nil && *i.LogFile != "" {
  111. var logWriter io.Writer
  112. logFileRollover := fileutil.SizeBasedRolloverCondition(1000000) // Each file can be up to a megabyte
  113. logWriter, err = fileutil.NewMultiFileBuffer(*i.LogFile, fileutil.ConsecutiveNumberIterator(10), logFileRollover)
  114. logger = util.NewBufferLogger(logWriter)
  115. } else {
  116. // Log to the console by default
  117. logger = util.NewStdOutLogger()
  118. }
  119. // Set the log level
  120. if err == nil {
  121. if i.LogLevel != nil && *i.LogLevel != "" {
  122. logger, err = util.NewLogLevelLogger(logger, *i.LogLevel)
  123. }
  124. if err == nil {
  125. // Get the import locator
  126. importLocator := &util.FileImportLocator{Root: *i.Dir}
  127. // Create interpreter
  128. i.RuntimeProvider = interpreter.NewECALRuntimeProvider(name, importLocator, logger)
  129. }
  130. }
  131. return err
  132. }
  133. /*
  134. LoadInitialFile clears the global scope and reloads the initial file.
  135. */
  136. func (i *CLIInterpreter) LoadInitialFile(tid uint64) error {
  137. var err error
  138. i.RuntimeProvider.Processor.Finish()
  139. i.RuntimeProvider.Processor.Reset()
  140. if i.CustomHandler != nil {
  141. i.CustomHandler.LoadInitialFile(tid)
  142. }
  143. i.GlobalVS.Clear()
  144. if i.EntryFile != "" {
  145. var ast *parser.ASTNode
  146. var initFile []byte
  147. initFile, err = ioutil.ReadFile(i.EntryFile)
  148. if err == nil {
  149. if ast, err = parser.ParseWithRuntime(i.EntryFile, string(initFile), i.RuntimeProvider); err == nil {
  150. if err = ast.Runtime.Validate(); err == nil {
  151. _, err = ast.Runtime.Eval(i.GlobalVS, make(map[string]interface{}), tid)
  152. }
  153. defer func() {
  154. if i.RuntimeProvider.Debugger != nil {
  155. i.RuntimeProvider.Debugger.RecordThreadFinished(tid)
  156. }
  157. }()
  158. }
  159. }
  160. }
  161. i.RuntimeProvider.Processor.Start()
  162. return err
  163. }
  164. /*
  165. CreateTerm creates a new console terminal for stdout.
  166. */
  167. func (i *CLIInterpreter) CreateTerm() error {
  168. var err error
  169. if i.Term == nil {
  170. i.Term, err = termutil.NewConsoleLineTerminal(os.Stdout)
  171. }
  172. return err
  173. }
  174. /*
  175. Interpret starts the ECAL code interpreter. Starts an interactive console in
  176. the current tty if the interactive flag is set.
  177. */
  178. func (i *CLIInterpreter) Interpret(interactive bool) error {
  179. if i.ParseArgs() {
  180. return nil
  181. }
  182. err := i.LoadStdlibPlugins(interactive)
  183. if err == nil {
  184. err = i.CreateTerm()
  185. if interactive {
  186. fmt.Fprintln(i.LogOut, fmt.Sprintf("ECAL %v", config.ProductVersion))
  187. }
  188. // Create Runtime Provider
  189. if err == nil {
  190. if err = i.CreateRuntimeProvider("console"); err == nil {
  191. tid := i.RuntimeProvider.NewThreadID()
  192. if interactive {
  193. if lll, ok := i.RuntimeProvider.Logger.(*util.LogLevelLogger); ok {
  194. fmt.Fprint(i.LogOut, fmt.Sprintf("Log level: %v - ", lll.Level()))
  195. }
  196. fmt.Fprintln(i.LogOut, fmt.Sprintf("Root directory: %v", *i.Dir))
  197. if i.CustomWelcomeMessage != "" {
  198. fmt.Fprintln(i.LogOut, fmt.Sprintf(i.CustomWelcomeMessage))
  199. }
  200. }
  201. // Execute file if given
  202. if err = i.LoadInitialFile(tid); err == nil {
  203. // Drop into interactive shell
  204. if interactive {
  205. // Add history functionality without file persistence
  206. i.Term, err = termutil.AddHistoryMixin(i.Term, "",
  207. func(s string) bool {
  208. return i.isExitLine(s)
  209. })
  210. if err == nil {
  211. if err = i.Term.StartTerm(); err == nil {
  212. var line string
  213. defer i.Term.StopTerm()
  214. fmt.Fprintln(i.LogOut, "Type 'q' or 'quit' to exit the shell and '?' to get help")
  215. line, err = i.Term.NextLine()
  216. for err == nil && !i.isExitLine(line) {
  217. trimmedLine := strings.TrimSpace(line)
  218. i.HandleInput(i.Term, trimmedLine, tid)
  219. line, err = i.Term.NextLine()
  220. }
  221. }
  222. }
  223. }
  224. }
  225. }
  226. }
  227. }
  228. return err
  229. }
  230. /*
  231. LoadStdlibPlugins load plugins from .ecal.json.
  232. */
  233. func (i *CLIInterpreter) LoadStdlibPlugins(interactive bool) error {
  234. var err error
  235. if i.LoadPlugins {
  236. confFile := filepath.Join(*i.Dir, ".ecal.json")
  237. if ok, _ := fileutil.PathExists(confFile); ok {
  238. if interactive {
  239. fmt.Fprintln(i.LogOut, fmt.Sprintf("Loading stdlib plugins from %v", confFile))
  240. }
  241. var content []byte
  242. if content, err = ioutil.ReadFile(confFile); err == nil {
  243. var conf map[string]interface{}
  244. if err = json.Unmarshal(content, &conf); err == nil {
  245. if stdlibPlugins, ok := conf["stdlibPlugins"]; ok {
  246. err = fmt.Errorf("Config stdlibPlugins should be a list")
  247. if plugins, ok := stdlibPlugins.([]interface{}); ok {
  248. err = nil
  249. if errs := stdlib.LoadStdlibPlugins(plugins); len(errs) > 0 {
  250. for _, e := range errs {
  251. fmt.Fprintln(i.LogOut, fmt.Sprintf("Error loading plugins: %v", e))
  252. }
  253. err = fmt.Errorf("Could not load plugins defined in .ecal.json")
  254. }
  255. }
  256. }
  257. }
  258. }
  259. }
  260. }
  261. return err
  262. }
  263. /*
  264. isExitLine returns if a given input line should exit the interpreter.
  265. */
  266. func (i *CLIInterpreter) isExitLine(s string) bool {
  267. return s == "exit" || s == "q" || s == "quit" || s == "bye" || s == "\x04"
  268. }
  269. /*
  270. HandleInput handles input to this interpreter. It parses a given input line
  271. and outputs on the given output terminal. Requires a thread ID of the executing
  272. thread - use the RuntimeProvider to generate a unique one.
  273. */
  274. func (i *CLIInterpreter) HandleInput(ot OutputTerminal, line string, tid uint64) {
  275. // Process the entered line
  276. if line == "?" {
  277. // Show help
  278. ot.WriteString(fmt.Sprintf("ECAL %v\n", config.ProductVersion))
  279. ot.WriteString(fmt.Sprint("\n"))
  280. ot.WriteString(fmt.Sprint("Console supports all normal ECAL statements and the following special commands:\n"))
  281. ot.WriteString(fmt.Sprint("\n"))
  282. ot.WriteString(fmt.Sprint(" @reload - Clear the interpreter and reload the initial file if it was given.\n"))
  283. ot.WriteString(fmt.Sprint(" @sym [glob] - List all available inbuild functions and available stdlib packages of ECAL.\n"))
  284. ot.WriteString(fmt.Sprint(" @std <package> [glob] - List all available constants and functions of a stdlib package.\n"))
  285. if i.CustomHelpString != "" {
  286. ot.WriteString(i.CustomHelpString)
  287. }
  288. ot.WriteString(fmt.Sprint("\n"))
  289. ot.WriteString(fmt.Sprint("Add an argument after a list command to do a full text search. The search string should be in glob format.\n"))
  290. } else if strings.HasPrefix(line, "@reload") {
  291. // Reload happens in a separate thread as it may be suspended on start
  292. go i.LoadInitialFile(i.RuntimeProvider.NewThreadID())
  293. ot.WriteString(fmt.Sprintln(fmt.Sprintln("Reloading interpreter state")))
  294. } else if strings.HasPrefix(line, "@sym") {
  295. i.displaySymbols(ot, strings.Split(line, " ")[1:])
  296. } else if strings.HasPrefix(line, "@std") {
  297. i.displayPackage(ot, strings.Split(line, " ")[1:])
  298. } else if i.CustomHandler != nil && i.CustomHandler.CanHandle(line) {
  299. i.CustomHandler.Handle(ot, line)
  300. } else {
  301. var ierr error
  302. var ast *parser.ASTNode
  303. var res interface{}
  304. if line != "" {
  305. if ast, ierr = parser.ParseWithRuntime("console input", line, i.RuntimeProvider); ierr == nil {
  306. if ierr = ast.Runtime.Validate(); ierr == nil {
  307. if res, ierr = ast.Runtime.Eval(i.GlobalVS, make(map[string]interface{}), tid); ierr == nil && res != nil {
  308. ot.WriteString(fmt.Sprintln(stringutil.ConvertToString(res)))
  309. }
  310. defer func() {
  311. if i.RuntimeProvider.Debugger != nil {
  312. i.RuntimeProvider.Debugger.RecordThreadFinished(tid)
  313. }
  314. }()
  315. }
  316. }
  317. if ierr != nil {
  318. ot.WriteString(fmt.Sprintln(ierr.Error()))
  319. }
  320. }
  321. }
  322. }
  323. /*
  324. displaySymbols lists all available inbuild functions and available stdlib packages of ECAL.
  325. */
  326. func (i *CLIInterpreter) displaySymbols(ot OutputTerminal, args []string) {
  327. tabData := []string{"Inbuild function", "Description"}
  328. for name, f := range interpreter.InbuildFuncMap {
  329. ds, _ := f.DocString()
  330. if len(args) > 0 && !matchesFulltextSearch(ot, fmt.Sprintf("%v %v", name, ds), args[0]) {
  331. continue
  332. }
  333. tabData = fillTableRow(tabData, name, ds)
  334. }
  335. if len(tabData) > 2 {
  336. ot.WriteString(stringutil.PrintGraphicStringTable(tabData, 2, 1,
  337. stringutil.SingleDoubleLineTable))
  338. }
  339. packageNames, _, _ := stdlib.GetStdlibSymbols()
  340. tabData = []string{"Package name", "Description"}
  341. for _, p := range packageNames {
  342. ps, _ := stdlib.GetPkgDocString(p)
  343. if len(args) > 0 && !matchesFulltextSearch(ot, fmt.Sprintf("%v %v", p, ps), args[0]) {
  344. continue
  345. }
  346. tabData = fillTableRow(tabData, p, ps)
  347. }
  348. if len(tabData) > 2 {
  349. ot.WriteString(stringutil.PrintGraphicStringTable(tabData, 2, 1,
  350. stringutil.SingleDoubleLineTable))
  351. }
  352. }
  353. /*
  354. displayPackage list all available constants and functions of a stdlib package.
  355. */
  356. func (i *CLIInterpreter) displayPackage(ot OutputTerminal, args []string) {
  357. _, constSymbols, funcSymbols := stdlib.GetStdlibSymbols()
  358. tabData := []string{"Constant", "Value"}
  359. for _, s := range constSymbols {
  360. if len(args) > 0 && !strings.HasPrefix(s, args[0]) {
  361. continue
  362. }
  363. val, _ := stdlib.GetStdlibConst(s)
  364. tabData = fillTableRow(tabData, s, fmt.Sprint(val))
  365. }
  366. if len(tabData) > 2 {
  367. ot.WriteString(stringutil.PrintGraphicStringTable(tabData, 2, 1,
  368. stringutil.SingleDoubleLineTable))
  369. }
  370. tabData = []string{"Function", "Description"}
  371. for _, f := range funcSymbols {
  372. if len(args) > 0 && !strings.HasPrefix(f, args[0]) {
  373. continue
  374. }
  375. fObj, _ := stdlib.GetStdlibFunc(f)
  376. fDoc, _ := fObj.DocString()
  377. fDoc = strings.Replace(fDoc, "\n", " ", -1)
  378. fDoc = strings.Replace(fDoc, "\t", " ", -1)
  379. if len(args) > 1 && !matchesFulltextSearch(ot, fmt.Sprintf("%v %v", f, fDoc), args[1]) {
  380. continue
  381. }
  382. tabData = fillTableRow(tabData, f, fDoc)
  383. }
  384. if len(tabData) > 2 {
  385. ot.WriteString(stringutil.PrintGraphicStringTable(tabData, 2, 1,
  386. stringutil.SingleDoubleLineTable))
  387. }
  388. }