-1

I'm writing a script interpreter in Python using Sly. While figuring out how to best write assignment interpretation, I found myself unable to quite understand how to handle the left-hand side being different sorts of values. The scripting language I'm using, the left-hand side could be a variable or a field on an object (possibly a few layers deep). So the following assignments in the scripting language would all be correct:

bob = 4.5
this.speed = 5.3
eachthing.child[prevRef+2].name = "William"

Currently, I have the regular variables stored in a dictionary with type and value. The other sort of "variable" involves object fields which are sometimes a simple reference to the current object that the script is attached to and sometimes a reference to a global object, and it's possible to navigate through a hierarchy of objects (for example, going to the hero, finding his second sword, referencing its magical effect, and getting its name). Thus, it's not a simple lookup in some cases.

Initially writing this for variables, it made sense to look up the variable, verify its existence (and fail with an exception if it doesn't exist), evaluate the right-hand side, check their types against each other, and then assign the value. When thinking through the fields, there are multiple steps to determine where the field exists (FWIW, no operations are allowed in the LHS that would change state) and its details, and it seems slightly wasteful to go through all of those steps to determine the existence of the field, and then do all of the RHS evaluation, and then have to go through the LHS steps to assign a new value.

Is there a way to establish a "reference" like a pointer where I can then use it to assign the new value?

After reading through this article about how variables work in Python, I was pretty certain that it's method of pointing one variable at the other wouldn't allow me to make the assignment, and unfortunately, I was right. I poked around for various "by reference" pages on the web, but they were all discussing passing by ref into functions, not getting a reference to a variable/field.

Alternately, I'm probably looking at creating my own index where the values of the fields are stored in a dictionary such that I can directly access them with a reference value (maybe making that second sword's name accessible with an index of (hero, weapon7, effect, name) although that feels clunky).

Sean Duggan
  • 1,105
  • 2
  • 18
  • 48
  • At least in Python, only the first is a "real" assignment, giving a value to the name `bob`. The other two are syntactic sugar for method calls, namely `this.__setattr__('speed', 5.3)` and `thing.child[prevRef + 2].__setattr__('name', "William")`. – chepner Nov 27 '22 at 23:44
  • Sorry, I'll try to clarify, but those are assignments within the scripting language. – Sean Duggan Nov 29 '22 at 13:51
  • In your own scripting language, you can do anything you like. I think this is too broad, though, unless you have a specific implementation that needs to be debugged. – chepner Nov 29 '22 at 15:07

1 Answers1

1

All of your assignment operations can be reduced to "set selector S of container C to value V". While Python doesn't let you create a reference value C[S], it certainly lets you pass around the tuple (C, S); that works because Python containers dictionaries, lists, etc.) are effectively reference values.

(In the case of bob = 4.5, the container is whatever you use to hold global variables and the selector might be the global's index or it might be the name, depending on how you handle globals. But it will certainly be some kind of value.)

You could use a triple instead, where the first value is a function to call with C and S as arguments. That might turn out to be easier. Or not. You haven't revealed much of your approach so it's difficult to provide an answer which isn't just generalities.

rici
  • 234,347
  • 28
  • 237
  • 341
  • Despite my lack of information, I think I kind of see where my approach needs to lie. Both the "variables" in the scripting language, and the data values are stored internally by me in a way such there should be a non-ambiguous way to reference each item in a way such that I can build a key since ultimately, it will be a value of a single object, just maybe with additional steps to figure out which one. – Sean Duggan Nov 29 '22 at 13:54