fictiveworks / CalyxSharp

Generative text processing for C# and Unity applications
Other
0 stars 0 forks source link

Registry.Evaluate(string, Dictionary<string, string>) has side effects on the Registry #30

Closed bentorkington closed 1 year ago

bentorkington commented 1 year ago

This method seems like it's intended to be like a static method, but has the effect of adding the built rules to the registry. This might be surprising, given the method name suggests it wouldn't do this.

Is this (and it's caller, Grammar.Generate(string, Dictionary<string, string>) intended to be part of the API?

maetl commented 1 year ago

The side effects should be reset after each grammar run, so the rules only persist for that single evaluation pass.

From the original docs:

Dynamically Constructing Rules

Template expansions can be dynamically constructed at runtime by passing a context map of rules to the #generate method:

class AppGreeting < Calyx::Grammar
   start 'Hi {username}!', 'Welcome back {username}!', 'Hola {username}!'
end

context = {
  username: UserModel.username
}

greeting = AppGreeting.new
greeting.generate(context)