KSP-KOS / KOS

Fully programmable autopilot mod for KSP. Originally By Nivekk
Other
699 stars 230 forks source link

[Enhancement] List and lexicon literals #2042

Open zegkljan opened 7 years ago

zegkljan commented 7 years ago

It would be great if lists and lexicons could be created as literals, i.e. without using functions. The idea is that it should be similar to JSON or Python.

Lists example (based on kOS documentation example):

// Make an empty list with zero items in it:
set mylist to list(). // old way
set mylist to []. // new way
// Make a list with 3 strings in it:
set mylist to list("10","20","30"). // old way
set mylist to ["10", "20", "30"]. // new way
// Make a two dimensional 2x3 list with heterogenious contents
// mixing strings and numbers:
set mylist to list( list("a","b","c"), list(1,2,3) ). // old way
set mylist to [["a", "b", "c"], [1, 2, 3]]. //new way

For lists, it is not as sexy since the standard constructor is already pretty ok but it would still save some typing, especially for nested lists.

Lexicons example (based on kOS documentation example):

// old way
set arr to lexicon().
arr:add( "ABC", 1234.1 ).
arr:add( "Carmine", 4.1 ).
// new way
set arr to { "ABC": 1234.1, "Carmine": 4.1 }.

Complex example:

// old way
set arr to lexicon().
arr:add( "a", list(1, 2, 3, 4) ).
arr:add( "b", lexicon() ).
arr:add( "c", "x" ).
arr["b"]:add("d", 1).
arr["b"]:add("e", 2).
// new way
set arr to { "a": [1, 2, 3, 4], "b": { "d": 1, "e": 2 }, "c": "x" }.

Case-sensitive lexicons Lexicons, by default, are case insensitive but can be set to be case sensitive. The literals should support creating case-sensitive lexicons right away, without the extra step of setting the case sensitivity. A random solution to this could be adding some special character right after the opening brace:

// case insensitive
set lex to { "A": 1, "b": 2 }.
// case sensitive
set lex to {@ "A": 1, "b": 2 }.
brantwedel commented 6 years ago

Just a note: the the "old way" examples can also be a single line, as the lexicon constructor function takes alternating pairs for key values:

set arr to lexicon("a", list(1, 2, 3, 4), "b", lexicon("d", 1, "e", 2), "c", "x").

https://ksp-kos.github.io/KOS/structures/collections/lexicon.html?highlight=alternating#constructing-a-lexicon

Dunbaratu commented 5 years ago

(Sorry, reading through old issues.) The reason this is a PITA to implement is because the braces are already the syntax for anonymous delegates, like so:

set x to { return 0. }.

So if this was also valid syntax:

set x to { "a": 0, "b": 1 }.

The parser would have no way of realizing which rule it was in when it hit the first open-brace token. It would have to wait until it got quite a ways into the syntax before it realized "well, let's see, string literal, colon, expression, comma.... Okay this isn't valid statement syntax so I guess this can't be an anonymous delegate so I guess it's a list of keys and values instead."

The Parser we use is only lookahead-1 so it can't really do that.

meltingSnowdrift commented 5 years ago

Maybe do not try to determine what curly brace contents mean at all. For lists, we could have something like: L{123, 567, "abcd"} and for lexicons something like: X{"ab":10, "de":30}

This is similar to how some languages resolve potential type ambiguities in literals by, for example, allowing a double to be specified by 1234D and a long to be specified by 1234L.