aboutsummaryrefslogtreecommitdiff
path: root/nomsu.moon
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2018-01-24 13:13:03 -0800
committerBruce Hill <bitbucket@bruce-hill.com>2018-01-24 13:13:26 -0800
commitf769351556cceed58ab6bf844c671114ec1862c2 (patch)
treedd7dcf811082cf4e4c4bb0b6b3cd3811cf4385e1 /nomsu.moon
parentd173e9ae88380bb1217b3f775e6c3cec71a6606c (diff)
Updated and improve syntax for some stuff. Also added string indexing
like in python, including slices.
Diffstat (limited to 'nomsu.moon')
-rwxr-xr-xnomsu.moon13
1 files changed, 11 insertions, 2 deletions
diff --git a/nomsu.moon b/nomsu.moon
index 0f13a3e..222f129 100755
--- a/nomsu.moon
+++ b/nomsu.moon
@@ -20,10 +20,16 @@ colored = setmetatable({}, {__index:(_,color)-> ((msg)-> colors[color]..(msg or
{:insert, :remove, :concat} = table
-- Use + operator for string coercive concatenation (note: "asdf" + 3 == "asdf3")
+-- Use [] for accessing string characters, or s[{3,4}] for s:sub(3,4)
-- Note: This globally affects all strings in this instance of Lua!
do
STRING_METATABLE = getmetatable("")
STRING_METATABLE.__add = (other)=> @ .. stringify(other)
+ STRING_METATABLE.__index = (i)=>
+ if type(i) == 'number' then return string.sub(@, i, i)
+ elseif type(i) == 'table' then return string.sub(@, i[1], i[2])
+ else return string[i]
+ STRING_METATABLE.__mul = (other)=> string.rep(@, other)
-- TODO:
-- consider non-linear codegen, rather than doing thunks for things like comprehensions
@@ -921,8 +927,11 @@ class NomsuCompiler
return statements:lua_code, locals:lua.locals
@define_compile_action "lua> %code", "nomsu.moon", (_code)->
- lua = nomsu_string_as_lua(_code)
- return statements:lua
+ if _code.type == "Text"
+ lua = nomsu_string_as_lua(_code)
+ return statements:lua
+ else
+ return statements:"nomsu:run_lua(#{nomsu\tree_to_lua(_code).expr});"
@define_compile_action "=lua %code", "nomsu.moon", (_code)->
lua = nomsu_string_as_lua(_code)