2018-05-15 18:55:55 -07:00
|
|
|
#
|
2018-01-11 18:51:21 -08:00
|
|
|
This file contains code that supports manipulating and using collections like lists
|
|
|
|
and dictionaries.
|
|
|
|
|
2018-02-02 15:48:28 -08:00
|
|
|
use "core/metaprogramming.nom"
|
|
|
|
use "core/control_flow.nom"
|
|
|
|
use "core/operators.nom"
|
2017-09-21 00:10:26 -07:00
|
|
|
|
|
|
|
# List/dict functions:
|
|
|
|
|
|
|
|
# Indexing
|
2018-06-18 15:44:29 -07:00
|
|
|
compile [..]
|
|
|
|
%index st to last in %list, %index nd to last in %list, %index rd to last in %list
|
|
|
|
%index th to last in %list
|
|
|
|
..to: Lua value "utils.nth_to_last(\(%list as lua expr), \(%index as lua expr))"
|
2017-09-21 00:10:26 -07:00
|
|
|
|
2018-06-18 15:44:29 -07:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
parse [last in %list] as: 1st to last in %list
|
|
|
|
parse [first in %list] as: %list.1
|
2017-09-21 00:10:26 -07:00
|
|
|
|
|
|
|
# Membership testing
|
2018-06-18 15:44:29 -07:00
|
|
|
action [%item is in %list, %list contains %item, %list has %item]
|
|
|
|
for %key = %value in %list
|
|
|
|
if (%key is %item): return (yes)
|
|
|
|
return (no)
|
|
|
|
|
|
|
|
action [..]
|
|
|
|
%item isn't in %list, %item is not in %list
|
|
|
|
%list doesn't contain %item, %list does not contain %item
|
|
|
|
%list doesn't have %item, %list does not have %item
|
|
|
|
..
|
|
|
|
for %key = %value in %list
|
|
|
|
if (%key is %item): return (no)
|
|
|
|
return (yes)
|
|
|
|
|
|
|
|
parse [%list has key %index, %list has index %index] as
|
|
|
|
%list.%index != (nil)
|
|
|
|
|
|
|
|
parse [..]
|
|
|
|
%list doesn't have key %index, %list does not have key %index
|
|
|
|
%list doesn't have index %index, %list does not have index %index
|
|
|
|
..as
|
|
|
|
%list.%index = (nil)
|
|
|
|
|
|
|
|
compile [number of keys in %list] to
|
|
|
|
Lua value "utils.size(\(%list as lua expr))"
|
|
|
|
|
|
|
|
compile [append %item to %list, add %item to %list, to %list add %item, to %list append %item] to
|
|
|
|
Lua "table.insert(\(%list as lua expr), \(%item as lua expr))"
|
|
|
|
|
|
|
|
compile [pop from %list, remove last from %list] to
|
2018-06-20 15:22:03 -07:00
|
|
|
Lua value "table.remove(\(%list as lua expr))"
|
2018-06-18 15:44:29 -07:00
|
|
|
|
|
|
|
compile [remove index %index from %list] to
|
2018-06-20 15:22:03 -07:00
|
|
|
Lua value "table.remove(\(%list as lua expr), \(%index as lua expr))"
|
2017-09-21 21:11:13 -07:00
|
|
|
|
2018-06-18 18:10:59 -07:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2017-09-21 00:10:26 -07:00
|
|
|
# List Comprehension
|
2018-06-18 15:44:29 -07:00
|
|
|
parse [%expression for %item in %iterable] as
|
|
|
|
result of
|
|
|
|
%comprehension <- []
|
|
|
|
for %item in %iterable
|
|
|
|
add %expression to %comprehension
|
|
|
|
return %comprehension
|
|
|
|
|
|
|
|
parse [..]
|
|
|
|
%expression for %index in %start to %stop via %step
|
|
|
|
%expression for %index in %start to %stop by %step
|
|
|
|
..as
|
|
|
|
result of
|
|
|
|
%comprehension <- []
|
|
|
|
for %index in %start to %stop via %step
|
|
|
|
add %expression to %comprehension
|
|
|
|
return %comprehension
|
|
|
|
|
2018-06-18 18:10:59 -07:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2018-06-18 15:44:29 -07:00
|
|
|
parse [%expression for %var in %start to %stop] as
|
|
|
|
%expression for %var in %start to %stop via 1
|
|
|
|
|
|
|
|
parse [..]
|
|
|
|
%expression for %key = %value in %iterable
|
|
|
|
%expression for (%key,%value) in %iterable
|
|
|
|
..as
|
|
|
|
result of
|
|
|
|
%comprehension <- []
|
|
|
|
for %key = %value in %iterable
|
|
|
|
add %expression to %comprehension
|
|
|
|
return %comprehension
|
|
|
|
|
|
|
|
# Dict comprehensions
|
|
|
|
parse [..]
|
|
|
|
%key = %value for %item in %iterable
|
|
|
|
(%key,%value) for %item in %iterable
|
|
|
|
..as
|
|
|
|
result of
|
|
|
|
%comprehension <- {}
|
|
|
|
for %item in %iterable
|
|
|
|
%comprehension.%key <- %value
|
|
|
|
return %comprehension
|
|
|
|
|
|
|
|
parse [..]
|
|
|
|
%key = %value for %src_key = %src_value in %iterable
|
|
|
|
(%key,%value) for (%src_key,%src_value) in %iterable
|
|
|
|
..as
|
|
|
|
result of
|
|
|
|
%comprehension <- {}
|
|
|
|
for %src_key = %src_value in %iterable
|
|
|
|
%comprehension.%key <- %value
|
|
|
|
return %comprehension
|
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
action [%lists flattened]
|
|
|
|
%flat <- []
|
|
|
|
for %list in %lists
|
|
|
|
for %item in %list
|
|
|
|
add %item to %flat
|
|
|
|
return %flat
|
|
|
|
|
|
|
|
parse [entries in %dict] as: {key:%k, value:%v} for %k = %v in %dict
|
|
|
|
parse [keys in %dict, keys of %dict] as: %k for %k = %v in %dict
|
|
|
|
parse [values in %dict, values of %dict] as: %v for %k = %v in %dict
|
2018-01-26 20:20:12 -08:00
|
|
|
|
2018-05-27 18:28:23 -07:00
|
|
|
# Metatable stuff
|
2018-06-18 15:44:29 -07:00
|
|
|
compile [set %dict's metatable to %metatable] to
|
|
|
|
Lua "setmetatable(\(%dict as lua expr), \(%metatable as lua expr));"
|
|
|
|
|
|
|
|
compile [%dict with fallback %key -> %value] to
|
|
|
|
Lua value ".."
|
|
|
|
setmetatable(\(%dict as lua expr), {__index=function(self, \(%key as lua expr))
|
|
|
|
local value = \(%value as lua expr)
|
|
|
|
self[\(%key as lua expr)] = value
|
|
|
|
return value
|
|
|
|
end})
|
2018-05-27 18:28:23 -07:00
|
|
|
|
2018-04-25 16:30:49 -07:00
|
|
|
# Sorting
|
2018-06-18 15:44:29 -07:00
|
|
|
compile [sort %items] to: Lua "table.sort(\(%items as lua expr));"
|
|
|
|
parse [..]
|
|
|
|
sort %items by %item = %key_expr
|
|
|
|
sort %items by %item -> %key_expr
|
|
|
|
..as
|
|
|
|
do
|
|
|
|
%keys <- ({} with fallback %item -> %key_expr)
|
|
|
|
lua> "table.sort(\%items, function(x,y) return \%keys[x] < \%keys[y] end)"
|
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
action [%items sorted, sorted %items]
|
|
|
|
%copy <- (% for % in %items)
|
|
|
|
sort %copy
|
|
|
|
return %copy
|
2018-06-23 00:57:31 -07:00
|
|
|
parse [..]
|
2018-06-18 15:44:29 -07:00
|
|
|
%items sorted by %item = %key
|
|
|
|
%items sorted by %item -> %key
|
2018-06-23 00:57:31 -07:00
|
|
|
..as
|
|
|
|
result of
|
|
|
|
%copy <- (% for % in %items)
|
|
|
|
sort %copy by %item = %key
|
|
|
|
return %copy
|
2018-06-18 15:44:29 -07:00
|
|
|
|
|
|
|
action [unique %items]
|
|
|
|
%unique <- []
|
|
|
|
%seen <- {}
|
|
|
|
for % in %items
|
|
|
|
unless: %seen.%
|
|
|
|
add % to %unique
|
|
|
|
%seen.% <- (yes)
|
|
|
|
return %unique
|
2018-01-26 20:20:12 -08:00
|
|
|
|