provider.go 5.6 KB

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