diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2018-01-18 01:49:13 -0800 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2018-01-18 01:49:27 -0800 |
| commit | f91d06d9fa567b26a098ff26a0339afdd5daa778 (patch) | |
| tree | 6899c345433629b05c5b51f6545ae7ba02a692e7 /lib/operators.nom | |
| parent | 12cc294c7ac31e3d08d6b8924dfc6cc427a1f712 (diff) | |
Initial commit of object oriented classes.
Diffstat (limited to 'lib/operators.nom')
| -rw-r--r-- | lib/operators.nom | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/lib/operators.nom b/lib/operators.nom index b697aad..2bbeadf 100644 --- a/lib/operators.nom +++ b/lib/operators.nom @@ -5,7 +5,27 @@ use "lib/metaprogramming.nom" # Indexing: immediately: - compile [%obj'%key, %obj's %key, %obj -> %key] to: "(\(%obj as lua))[\(%key as lua)]" + #.. NOTE!!! It's critical that there are spaces around %key if it's a string, + otherwise, Lua will get confused and interpret %obj[[[foo]]] as %obj("[foo]") + instead of %obj[ "foo" ]. + It's also critical to have parens around %obj, otherwise Lua is too dumb to + realize that {x=1}["x"] is the same as ({x=1})["x"] or that + {x=1}.x is the same as ({x=1}).x + compile [%obj'%key, %obj's %key, %obj -> %key] to: + lua> ".." + local obj_lua = \(%obj as lua); + if not obj_lua:sub(-1,-1):match("[a-zA-Z)]") then + obj_lua = "("..obj_lua..")"; + end + local key_lua = \(%key as lua); + local key_attr = (key_lua:match("'([a-zA-Z][a-zA-Z0-9]*)'") + or key_lua:match('"([a-zA-Z][a-zA-Z0-9]*)"')); + if key_attr then + return obj_lua.."."..key_attr; + elseif key_lua:sub(1,1) == "[" then + key_lua = " "..key_lua.." "; + end + return obj_lua.."["..key_lua.."]"; # Comparison Operators immediately: |
