lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi!

The following "proposals" are just for fun.

-----------------------------
1) A must-have "dont" statement :-)

dont
   -- code inside dont..end block will be skipped
   -- useful for easy commenting do..end block
end

"dont" is also applicable to any block: if, while, for, multiline assignment, function definition, etc.

dont if ... then -- this condition will not be evaluated
   -- this code will be skipped
else
   -- this code will be skipped too
end

"dont" statement acts as --[[...]] comment, but only for a single statement inside
When using "dont" we do not need to search for a place where very long statement ends, so using "dont" is more easy than --[[...]]
The "dont...end" is a syntactic sugar for "dont do...end"

-----------------------------
2) Famous "comefrom" operator as the opposite to "goto"

...
::label1::
...
comefrom label1, label2, label3
...
::label2::
...
::label3::
...

is equivalent to

...
goto label0
...
::label0::
...
goto label0
...
goto label0
...

-----------------------------
3) Labels are considered to be first-class citizens

::Label1::
...
local my_label = Label1  -- assigning a value of type "label" to a variable
...
-- Now "goto" operator gets its real power!
goto my_array_of_labels[where_do_you_want_to_go_today]
...
-- And of course the same is true for "comefrom" :-)
comefrom get_some_labels()
-- it is equivalent to "comefrom label1, label2,..."
-- where label1, label2,.. are values returned by LAST invocation of get_some_labels()
-- "comefrom" without labels is valid (it just does nothing useful)
...

-----------------------------
4)  Program can modify its own body using methods of labels
A label is a "bookmark" between statements in a program (or before first statement or after last statement)
A label may be named (defined at compile time) or unnamed (created at runtime)

...
some_func(some_params)
...
-- standard library function to create unnamed label just before current statement
local here = labels.this_label()
-- create label just before some_func invocation
local previous_some_func_invocation = here:search_nearest("backward", "call", "some_func")
-- replace "some_func()" with "another_func()" in the program body
previous_some_func_invocation:replace_statement("another_func()")
...
-- go to unnamed label
goto previous_some_func_invocation
...

-----------------------------
The source of craziness:
www.modell.com/Magery/SPharmful.html

-- Egor