fredi-68 / OverScript

OverScript - Overwatch Workshop Script Compiler for Python 3
MIT License
6 stars 1 forks source link

Add structs #9

Open fredi-68 opened 5 years ago

fredi-68 commented 5 years ago

add structs using dictionary syntax

my_struct = {"health": 100, "armor": 50, "shields": 70} my_struct["armor"] = 20 shields = my_struct["shields"] - 10

fredi-68 commented 5 years ago

new idea: use Pythons class syntax instead:

class MyStruct():
    health = 100
    armor = None
    shields = 50

myStruct = MyStruct(100, 50, 70)
myStruct.health = 20
shields = myStruct.shields

however, there are also some issues I didn't consider until now. For one, to treat an array (which is what structs use internally to store their values) as a struct, we need to know that it was created as a struct. This means we need to track variable types, at least for variables that we create inside the program. This becomes an issue when dealing with arrays. We can either prohibit the insertion of struct instances into arrays or restrict it to only be arrays of struct instances. To accomplish this, one could introduce a semi statically typed array syntax, which allows for either only workshop types or only user defined types in an array (can be easily checked at compile time). This in turn however means that we need to simulate the workshop code during compilation to detect any problems with array manipulations. Further, we need support for array manipulation to deal with attribute assignment for structs. Another feature worth considering would be the addition of methods to structs, turning them into classes. We can use utility functions with an implicit first argument to accomplish this and if we track usage of user defined types this can be done easily.