Movatterモバイル変換


[0]ホーム

URL:


leafo.net

My projects

View more →

Recent guides

View all →

Recent posts

MoonScript v0.2.4

PostedJuly 01, 2013 by leafo (@moonscript)
Tweet

I'm happy to announceMoonScript version 0.2.4, the CoffeeScript inspiredlanguage that compiles to Lua. It’s been about 5 months since the last release.

MoonScript 0.2.4 — moonscript.org

As always, if you've got any questions or want to tell me about how you areusing MoonScript you canemail me or contact me ontwitter.

Changes

  • The way the subtraction operator works has changed. There was always a littleconfusion as to the rules regarding whitespace around it and it wasrecommended to always add whitespace around the operator when doing subtraction. Not anymore. Hopefully it nowworks how you wouldexpect. (a-b compiles toa - b and nota(-b) anymore).
  • Themoon library is no longer sets a global variable and instead returnsthe module. Your code should now be:
moon=require"moon"
  • Generated code will reuse local variables when appropriate. Local variablesare guaranteed to not have side effects when being accessed as opposed toexpressions and global variables. MoonScript will now take advantage of thisand reuse those variable without creating and copying to a temporary name.See theexamples below.
  • Reduced the creation of anonymous functions that are called immediately.MoonScript uses this technique to convert a series of statements into asingle expression. It’s inefficient because it allocates a new functionobject and has to do a function call. It also obfuscates stack traces.MoonScript will flatten these functions into the current scope in a lot ofsituations now. See theexamples below.
  • Reduced the amount of code generated for classes. Parent class code it leftout if there is no parent. See theexamples below.

New Things

  • You can now put line breaks inside of string literals. It will be replacedwith\n in the generated code.
x="helloworld"
  • Addedmoonscript.base module. It’s a way of including themoonscriptmodule without automatically installing the moonloader.
  • You are free to use any whitespace around the name list in an importstatement. It has the same rules as an array table, meaning you can delimitnames with line breaks.
importa,bc,dfromz
  • Added significantly better tests. Previously the testing suite would onlyverify that code compiled to an expected string. Now there are unit tests thatexecute the code as well. This will make it easier to change the generatedoutput while still guaranteeing the semantics are the same.

Bug Fixes

  • b is not longer treated as self assign in{ a : b }
  • load functions will returnnil instead of throwing error, as described indocumentation
  • fixed an issue withmoon.mixin where it did not work as described

Code Generation Changes

As mentioned above a lot has changed about the generated code. Here’s a quickoverview with examples. All of these address special cases, if it’s notpossible to write the optimized form the original form will be generated.

Reusing locals:

input={}{:a,:b,d:{one,two,}}=input
Hover For Old
localinput={}locala,b,one,twodolocal_obj_0=inputa,b,one,two=_obj_0.a,_obj_0.b,_obj_0.d[1],_obj_0.d[2]end
localinput={}locala,b,one,twoa,b,one,two=input.a,input.b,input.d[1],input.d[2]

No unnecessarywith variable:

withthing="hello!"print\upper!
Hover For Old
dolocal_with_0="hello!"localthing=_with_0print(_with_0:upper())end
dolocalthing="hello!"print(thing:upper())end

Remove unnecessary inheritance code with simple classes:

classHellonew:=>print"hello"
Hover For Old
localHellodolocal_parent_0=nillocal_base_0={}_base_0.__index=_base_0if_parent_0thensetmetatable(_base_0,_parent_0.__base)endlocal_class_0=setmetatable({__init=function(self)returnprint("hello")end,__base=_base_0,__name="Hello",__parent=_parent_0},{__index=function(cls,name)localval=rawget(_base_0,name)ifval==niland_parent_0thenreturn_parent_0[name]elsereturnvalendend,__call=function(cls,...)local_self_0=setmetatable({},_base_0)cls.__init(_self_0,...)return_self_0end})_base_0.__class=_class_0if_parent_0and_parent_0.__inheritedthen_parent_0.__inherited(_parent_0,_class_0)endHello=_class_0end
localHellodolocal_base_0={}_base_0.__index=_base_0local_class_0=setmetatable({__init=function(self)returnprint("hello")end,__base=_base_0,__name="Hello"},{__index=_base_0,__call=function(cls,...)local_self_0=setmetatable({},_base_0)cls.__init(_self_0,...)return_self_0end})_base_0.__class=_class_0Hello=_class_0end

Comprehensions reuse local, don’t make temporary function:

things={}x=[aforain*things]
Hover For Old
localthings={}localx=(function()local_accum_0={}local_len_0=1local_list_0=thingsfor_index_0=1,#_list_0doa=_list_0[_index_0]_accum_0[_len_0]=a_len_0=_len_0+1endreturn_accum_0end)()
localthings={}localxdolocal_accum_0={}local_len_0=1for_index_0=1,#thingsdoa=things[_index_0]_accum_0[_len_0]=a_len_0=_len_0+1endx=_accum_0end

Accumulated loops also don’t use temporary function:

y=whilecheck_something!math.random!
Hover For Old
localy=(function()local_accum_0={}local_len_0=1whilecheck_something()do_accum_0[_len_0]=math.random()_len_0=_len_0+1endreturn_accum_0end)()
localydolocal_accum_0={}local_len_0=1whilecheck_something()do_accum_0[_len_0]=math.random()_len_0=_len_0+1endy=_accum_0end

Other Stuff

Libraries

Some updates for libraries written in MoonScript:

  • Lapis, the MoonScript powered web framework has come out with version0.0.2.
  • magick, LuaJIT FFI bindings to ImageMagick
  • web_sanitize, HTML sanitization

Games

Ludum Dare happened again, and I wrote another game in MoonScript:

Thanks

Thanks to everyone who provided feedback for this release. See you next time.

MoonScript 0.2.4 — moonscript.org

leafo.net · Generated Sun Oct 8 13:02:35 2023 bySitegenmastodon.social/@leafo


[8]ページ先頭

©2009-2025 Movatter.jp