provider.go 5.8 KB

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