def evaluate(self, expr): if isinstance(expr, exprs.Identifier): return self.prop_access(self.context.model, expr.text) elif isinstance(expr, exprs.PropertyAccessExpression): obj_value = self.evaluate(expr.object) return self.prop_access(obj_value, expr.property_name) elif isinstance(expr, exprs.UnresolvedCallExpression): func = self.evaluate(expr.func) args = list(map(lambda x: self.evaluate(x), expr.args)) result = func.call(args) return result elif isinstance(expr, exprs.StringLiteral): return vals.StringValue(expr.string_value) elif isinstance(expr, exprs.NumericLiteral): return vals.NumericValue(int(expr.value_as_text)) elif isinstance(expr, exprs.ConditionalExpression): cond_result = self.evaluate(expr.condition) result = self.evaluate(expr.when_true if ( cond_result).value else expr.when_false) return result elif isinstance(expr, exprs.TemplateString): result = "" for part in expr.parts: if part.is_literal: result += part.literal_text else: value = self.evaluate(part.expression) result += value.value if isinstance( value, vals.StringValue ) else self.context.hooks.stringify_value(value) return vals.StringValue(result) elif isinstance(expr, exprs.BinaryExpression): left = self.evaluate(expr.left) right = self.evaluate(expr.right) if expr.operator == "==" or expr.operator == "===": return vals.BooleanValue(left.equals(right)) elif expr.operator == "!=" or expr.operator == "!==": return vals.BooleanValue(not left.equals(right)) else: raise Error( f'''Unsupported binary operator: {expr.operator}''') else: raise Error("Unsupported expression!")
def add_plugin(self, plugin): self.plugins.append(plugin) # TODO: hack? if isinstance(plugin, templFileGenPlug.TemplateFileGeneratorPlugin): plugin.model_globals["toStream"] = templFileGenPlug.LambdaValue( lambda args: vals.StringValue( self.array_stream((args[0]).value))) plugin.model_globals["isArray"] = templFileGenPlug.LambdaValue( lambda args: vals.BooleanValue(self.is_array((args[0]).value))) plugin.model_globals["toArray"] = templFileGenPlug.LambdaValue( lambda args: vals.StringValue(self.to_array((args[0]).type))) plugin.model_globals["escape"] = templFileGenPlug.LambdaValue( lambda args: vals.StringValue(self.escape(args[0]))) plugin.model_globals["escapeRepl"] = templFileGenPlug.LambdaValue( lambda args: vals.StringValue(self.escape_repl(args[0])))