provider.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 interpreter
  11. import (
  12. "fmt"
  13. "os"
  14. "path/filepath"
  15. "sync"
  16. "devt.de/krotik/common/timeutil"
  17. "devt.de/krotik/ecal/config"
  18. "devt.de/krotik/ecal/engine"
  19. "devt.de/krotik/ecal/parser"
  20. "devt.de/krotik/ecal/util"
  21. )
  22. /*
  23. ecalRuntimeNew is used to instantiate ECAL runtime components.
  24. */
  25. type ecalRuntimeNew func(*ECALRuntimeProvider, *parser.ASTNode) parser.Runtime
  26. /*
  27. providerMap contains the mapping of AST nodes to runtime components for ECAL ASTs.
  28. */
  29. var providerMap = map[string]ecalRuntimeNew{
  30. parser.NodeEOF: invalidRuntimeInst,
  31. parser.NodeSTRING: stringValueRuntimeInst, // String constant
  32. parser.NodeNUMBER: numberValueRuntimeInst, // Number constant
  33. parser.NodeIDENTIFIER: identifierRuntimeInst, // Idendifier
  34. // Constructed tokens
  35. parser.NodeSTATEMENTS: statementsRuntimeInst, // List of statements
  36. parser.NodeFUNCCALL: voidRuntimeInst, // Function call
  37. parser.NodeCOMPACCESS: voidRuntimeInst, // Composition structure access
  38. parser.NodeLIST: listValueRuntimeInst, // List value
  39. parser.NodeMAP: mapValueRuntimeInst, // Map value
  40. parser.NodePARAMS: voidRuntimeInst, // Function parameters
  41. parser.NodeGUARD: guardRuntimeInst, // Guard expressions for conditional statements
  42. // Condition operators
  43. parser.NodeGEQ: greaterequalOpRuntimeInst,
  44. parser.NodeLEQ: lessequalOpRuntimeInst,
  45. parser.NodeNEQ: notequalOpRuntimeInst,
  46. parser.NodeEQ: equalOpRuntimeInst,
  47. parser.NodeGT: greaterOpRuntimeInst,
  48. parser.NodeLT: lessOpRuntimeInst,
  49. // Separators
  50. parser.NodeKVP: voidRuntimeInst, // Key-value pair
  51. parser.NodePRESET: voidRuntimeInst, // Preset value
  52. // Arithmetic operators
  53. parser.NodePLUS: plusOpRuntimeInst,
  54. parser.NodeMINUS: minusOpRuntimeInst,
  55. parser.NodeTIMES: timesOpRuntimeInst,
  56. parser.NodeDIV: divOpRuntimeInst,
  57. parser.NodeMODINT: modintOpRuntimeInst,
  58. parser.NodeDIVINT: divintOpRuntimeInst,
  59. // Assignment statement
  60. parser.NodeASSIGN: assignmentRuntimeInst,
  61. parser.NodeLET: letRuntimeInst,
  62. // Import statement
  63. parser.NodeIMPORT: importRuntimeInst,
  64. parser.NodeAS: voidRuntimeInst,
  65. // Sink definition
  66. parser.NodeSINK: sinkRuntimeInst,
  67. parser.NodeKINDMATCH: kindMatchRuntimeInst,
  68. parser.NodeSCOPEMATCH: scopeMatchRuntimeInst,
  69. parser.NodeSTATEMATCH: stateMatchRuntimeInst,
  70. parser.NodePRIORITY: priorityRuntimeInst,
  71. parser.NodeSUPPRESSES: suppressesRuntimeInst,
  72. // Function definition
  73. parser.NodeFUNC: funcRuntimeInst,
  74. parser.NodeRETURN: returnRuntimeInst,
  75. // Boolean operators
  76. parser.NodeOR: orOpRuntimeInst,
  77. parser.NodeAND: andOpRuntimeInst,
  78. parser.NodeNOT: notOpRuntimeInst,
  79. // Condition operators
  80. parser.NodeLIKE: likeOpRuntimeInst,
  81. parser.NodeIN: inOpRuntimeInst,
  82. parser.NodeHASPREFIX: beginswithOpRuntimeInst,
  83. parser.NodeHASSUFFIX: endswithOpRuntimeInst,
  84. parser.NodeNOTIN: notinOpRuntimeInst,
  85. // Constant terminals
  86. parser.NodeFALSE: falseRuntimeInst,
  87. parser.NodeTRUE: trueRuntimeInst,
  88. parser.NodeNULL: nullRuntimeInst,
  89. // Conditional statements
  90. parser.NodeIF: ifRuntimeInst,
  91. // Loop statements
  92. parser.NodeLOOP: loopRuntimeInst,
  93. parser.NodeBREAK: breakRuntimeInst,
  94. parser.NodeCONTINUE: continueRuntimeInst,
  95. // Try statement
  96. parser.NodeTRY: tryRuntimeInst,
  97. parser.NodeEXCEPT: voidRuntimeInst,
  98. parser.NodeOTHERWISE: voidRuntimeInst,
  99. parser.NodeFINALLY: voidRuntimeInst,
  100. // Mutex block
  101. parser.NodeMUTEX: mutexRuntimeInst,
  102. }
  103. /*
  104. ECALRuntimeProvider is the factory object producing runtime objects for ECAL ASTs.
  105. */
  106. type ECALRuntimeProvider struct {
  107. Name string // Name to identify the input
  108. ImportLocator util.ECALImportLocator // Locator object for imports
  109. Logger util.Logger // Logger object for log messages
  110. Processor engine.Processor // Processor of the ECA engine
  111. Mutexes map[string]*sync.Mutex // Map of named mutexes
  112. Cron *timeutil.Cron // Cron object for scheduled execution
  113. Debugger util.ECALDebugger // Optional: ECAL Debugger object
  114. }
  115. /*
  116. NewECALRuntimeProvider returns a new instance of a ECAL runtime provider.
  117. */
  118. func NewECALRuntimeProvider(name string, importLocator util.ECALImportLocator, logger util.Logger) *ECALRuntimeProvider {
  119. if importLocator == nil {
  120. // By default imports are located in the current directory
  121. importLocator = &util.FileImportLocator{Root: filepath.Dir(os.Args[0])}
  122. }
  123. if logger == nil {
  124. // By default we just have a memory logger
  125. logger = util.NewMemoryLogger(100)
  126. }
  127. proc := engine.NewProcessor(config.Int(config.WorkerCount))
  128. // By default ECAL should stop the triggering sequence of sinks after the
  129. // first sink that returns a sinkerror.
  130. proc.SetFailOnFirstErrorInTriggerSequence(true)
  131. cron := timeutil.NewCron()
  132. cron.Start()
  133. return &ECALRuntimeProvider{name, importLocator, logger, proc,
  134. make(map[string]*sync.Mutex), cron, nil}
  135. }
  136. /*
  137. Runtime returns a runtime component for a given ASTNode.
  138. */
  139. func (erp *ECALRuntimeProvider) Runtime(node *parser.ASTNode) parser.Runtime {
  140. if instFunc, ok := providerMap[node.Name]; ok {
  141. return instFunc(erp, node)
  142. }
  143. return invalidRuntimeInst(erp, node)
  144. }
  145. /*
  146. NewRuntimeError creates a new RuntimeError object.
  147. */
  148. func (erp *ECALRuntimeProvider) NewRuntimeError(t error, d string, node *parser.ASTNode) error {
  149. source := erp.Name
  150. if node.Token != nil {
  151. source = fmt.Sprintf("%v (%v)", source, node.Token.Lsource)
  152. }
  153. return util.NewRuntimeError(source, t, d, node)
  154. }
  155. /*
  156. NewThreadID creates a new thread ID unique to this runtime provider instance.
  157. This ID can be safely used for the thread ID when calling Eval on a
  158. parser.Runtime instance.
  159. */
  160. func (erp *ECALRuntimeProvider) NewThreadID() uint64 {
  161. return erp.Processor.ThreadPool().NewThreadID()
  162. }