KSP-KOS / KOS

Fully programmable autopilot mod for KSP. Originally By Nivekk
Other
691 stars 229 forks source link

Allow kerboscript to do late binding by either hashmaps or a syntax rule. #489

Closed Dunbaratu closed 9 years ago

Dunbaratu commented 9 years ago

I just got done answering this question of someone in the reddit forum:

http://www.reddit.com/r/Kos/comments/2rebou/unique_variablenames_in_loops/

What they were trying to do would have required late-binding - that you can store a value into a variable, the name of which is unknown until runtime. For example, this:

    // make 4 variables called hello1, hello2, hello3, and hello4, holding the values 10,20,30,40 respecitvely:
    set count to 1.
    until count > 4 {
        set ("hello"+count) TO count*10.  // this is trying to store into a variable like 'hello1' or 'hello2' ,etc.
        SET count TO count +1.
    }.

As you know, there's no way the above could ever work in kOS right now. you have to choose the variable names up front with early binding rules.

It occurs to me that there's no real reason kOS couldn't allow this. In reality, kOS could allow late-binding in which it adds values to the variable dictionary as needed, but right now it's not made to do that, and making it allow it would be an ugly syntax overhaul of the var_identifier rule in the tinyPG file.

But it does occur to me that we could allow two late-binding-ish features two different ways:

The question is... do we want this, and do we want way 1, way 2, or both?

teleksterling commented 9 years ago

I like the appeal of a dictionary/hashmap - and as you say have it feel similar to the dynamic suffixes that have been discussed in the past.

My main reasoning is quite personal, and I just don't like the look of the main code example you provide in your post.

abenkovskii commented 9 years ago

Hashmap: #963. Isn't it?

Dunbaratu commented 9 years ago

I suppose it's a little different to do hashmaps than having an actual eval, but if I want eval later, it can be a new issue then.