Skip to content

Instantly share code, notes, and snippets.

@radgeRayden
Last active January 22, 2021 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radgeRayden/87187f725868018f669ff4011462fb54 to your computer and use it in GitHub Desktop.
Save radgeRayden/87187f725868018f669ff4011462fb54 to your computer and use it in GitHub Desktop.
using import Map
using import Array
using import struct
inline table-lookup (t k)
let kT = (typeof k)
let a m =
Struct.__getattr t 'a
Struct.__getattr t 'm
static-if (kT < integer)
if (k < 0)
'__getattr t (hash k)
elseif ((countof a) < k)
'__getattr t (hash k)
else
k as:= usize
'resize a (max k ((countof a) + 1))
a @ k
elseif (kT == string)
'__getattr t (hash k)
struct Table
m : (Map hash i32)
a : (Array i32)
inline __@ (self _key)
table-lookup self _key
inline __getattr (self _key)
let m = (Struct.__getattr self 'm)
let _key = (bitcast _key hash)
try
'get m _key
else
'set m _key (2 ** 31)
try ('get m _key)
else (unreachable)
sugar table (...)
let args = (uncomma (... as list))
vvv bind init
fold (field-inits = '()) for el in (args as list)
sugar-match (el as list)
case (('square-list index) '= value)
cons
qq
temp-table @ [index] = [value]
field-inits
case (field '= value)
cons
qq
(getattr temp-table (sugar-quote [field])) = [value]
field-inits
case (field '= expr...)
cons
qq
(getattr temp-table (sugar-quote [field])) = [expr...]
field-inits
default
error "incorrect syntax"
qq
do
local temp-table : Table
unquote-splice init
deref temp-table
let $ = table-lookup
define-infix> 800 $
let curly-list = table
unlet table table-lookup
run-stage;
local tbl = {a = 12, b = 144, c = 1 + 2, [0] = 999}
print tbl.a tbl.b tbl.c
print (tbl $ 0)
tbl.abc = 25
print tbl.abc
tbl $ 10 = 35
print (tbl $ 10)
print (tbl $ "a")
let k = "c"
print (tbl $ k)
none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment