provider.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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.NodeFINALLY: voidRuntimeInst,
  98. // Mutex block
  99. parser.NodeMUTEX: mutexRuntimeInst,
  100. }
  101. /*
  102. ECALRuntimeProvider is the factory object producing runtime objects for ECAL ASTs.
  103. */
  104. type ECALRuntimeProvider struct {
  105. Name string // Name to identify the input
  106. ImportLocator util.ECALImportLocator // Locator object for imports
  107. Logger util.Logger // Logger object for log messages
  108. Processor engine.Processor // Processor of the ECA engine
  109. Mutexes map[string]*sync.Mutex // Map of named mutexes
  110. Cron *timeutil.Cron // Cron object for scheduled execution
  111. Debugger util.ECALDebugger // Optional: ECAL Debugger object
  112. }
  113. /*
  114. NewECALRuntimeProvider returns a new instance of a ECAL runtime provider.
  115. */
  116. func NewECALRuntimeProvider(name string, importLocator util.ECALImportLocator, logger util.Logger) *ECALRuntimeProvider {
  117. if importLocator == nil {
  118. // By default imports are located in the current directory
  119. importLocator = &util.FileImportLocator{Root: filepath.Dir(os.Args[0])}
  120. }
  121. if logger == nil {
  122. // By default we just have a memory logger
  123. logger = util.NewMemoryLogger(100)
  124. }
  125. proc := engine.NewProcessor(config.Int(config.WorkerCount))
  126. // By default ECAL should stop the triggering sequence of sinks after the
  127. // first sink that returns a sinkerror.
  128. proc.SetFailOnFirstErrorInTriggerSequence(true)
  129. cron := timeutil.NewCron()
  130. cron.Start()
  131. return &ECALRuntimeProvider{name, importLocator, logger, proc,
  132. make(map[string]*sync.Mutex), cron, nil}
  133. }
  134. /*
  135. Runtime returns a runtime component for a given ASTNode.
  136. */
  137. func (erp *ECALRuntimeProvider) Runtime(node *parser.ASTNode) parser.Runtime {
  138. if instFunc, ok := providerMap[node.Name]; ok {
  139. return instFunc(erp, node)
  140. }
  141. return invalidRuntimeInst(erp, node)
  142. }
  143. /*
  144. NewRuntimeError creates a new RuntimeError object.
  145. */
  146. func (erp *ECALRuntimeProvider) NewRuntimeError(t error, d string, node *parser.ASTNode) error {
  147. return util.NewRuntimeError(erp.Name, t, d, node)
  148. }
  149. /*
  150. NewThreadID creates a new thread ID unique to this runtime provider instance.
  151. This ID can be safely used for the thread ID when calling Eval on a
  152. parser.Runtime instance.
  153. */
  154. func (erp *ECALRuntimeProvider) NewThreadID() uint64 {
  155. return erp.Processor.ThreadPool().NewThreadID()
  156. }