python – Evaluating functions within arithmetic grammar of a DSL

python – Evaluating functions within arithmetic grammar of a DSL

I sorted this out and wanted to provide an answer. My function evaluation class looks like this:

# Executes functions contained in expressions
class EvalFunction(object):
  pop_ = {}
  def __init__(self, tokens):
    self.func_ = tokens.funcname
    self.field_ = tokens.arg
  def eval(self):
      # Get the name of the requested field and source db
      # Functions can only be called on dbRef so this always done
      v = self.field_.value
      fieldRef = v.split(:)
      source = fieldRef[0]
      field = fieldRef[1]

      # Evaluate the dbRef (get the value from the db)
      val = self.field_.eval()

      if self.func_ == Avg:
        rec = db[Stats].find_one({_id : field})   
        return rec[value][avg]
      elif self.func_ == Root:
          return math.sqrt(val)

my function grammar is:

functionCall = funcNames(funcname) + [ + dbRef(arg) + ]
functionCall.setParseAction(EvalFunction)

python – Evaluating functions within arithmetic grammar of a DSL

Leave a Reply

Your email address will not be published.