This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Kingdaro's profile picture

Moonscript - An alternative way to writing in lua.

Started by Kingdaro, 11 October 2012 - 09:10 PM
Kingdaro #1
Posted 11 October 2012 - 11:10 PM
A long time ago, I found this neat little thing called moonscript. It's another language, described as programmer-friendly, that compiles into Lua.

I'm obviously not going to list all of the features here, since it adds and changes a lot, but I'll name some notable ones that I've enjoyed using.

Assignment operators:

foo = 5
foo += 3
foo -= 4
foo /= 2
foo *= 3
foo ..= 'bar'

No need for ends, thens or dos (with some exceptions):

if Moonscript
  print 'Awesome!'

No need for parentheses (with some exceptions):

term.setCursorPos 1,1

Switch statement:

switch 'toast'
  when 'bagel'
	print "That's cool."
  when 'eggs'
	print "Meh."
  when 'butter'
	print "wtf is your problem"
  else
	print "...What are you even eating?"

Alternate function calling (without arguments):

afunction!

Alternate function declaration (without arguments):

afunction = ->
  somecode!

Automatic localization ("export" is used to make global):

declare_var = ->
  x = 5

declare_var!

if x
  print "x is global!"
else
  print "I can't find x!" --should print this

There's a lot more to know about moonscript than what I've shown here, if you're interested. There's some helpful documentation on the moonscript website. I've written some things in CC using moonscript, and once you understand what it actually is, it becomes really easy to understand.

http://moonscript.org/
Kingdaro #2
Posted 11 October 2012 - 11:23 PM
Looks as though it was inspired by a lot of other languages.

For me, python comes to mind.
jag #3
Posted 12 October 2012 - 12:13 AM
Really cool! How many lines of code is it? :P/>/>
Kingdaro #4
Posted 12 October 2012 - 01:09 AM
Really cool! How many lines of code is it? :P/>/>
Looking through the source…

a lot.
toxicwolf #5
Posted 12 October 2012 - 02:55 PM
This is really interesting! Could you tell me how you'd go about installing Moonscript for CC?
Kingdaro #6
Posted 12 October 2012 - 08:20 PM
This is really interesting! Could you tell me how you'd go about installing Moonscript for CC?
You don't need to do anything specifically for CC. Because of how moonscript complies to lua, you can just use CC APIs in it.

Simple password program in moonscript:

pass = "a password"

while true
  term.clear!
  term.setCursorPos 1,1
  print "Please enter the password."
  input = read!
  if input == pass
	print "Access granted."
	rs.setOutput 'right', true
	sleep 3
	rs.setOutput 'right', false
  else
	print "Access denied."
	sleep 2
No doubt I made a mistake somewhere in there though, haha.
GopherAtl #7
Posted 12 October 2012 - 08:30 PM
until/unless someone ports it, you can't use it in cc. You can only use it outside cc to compile moonscript programs into lua programs for use in CC.

:edit: or, it might be possible to use this web demo with the http api http://moonscript.org/compiler/ but maybe not, without going through the html source I dunno how that demo works, or if it uses technology that can be used from cc's http api
1v2 #8
Posted 12 October 2012 - 08:36 PM
Interesting.
Kingdaro #9
Posted 12 October 2012 - 09:11 PM
until/unless someone ports it, you can't use it in cc. You can only use it outside cc to compile moonscript programs into lua programs for use in CC.

:edit: or, it might be possible to use this web demo with the http api http://moonscript.org/compiler/ but maybe not, without going through the html source I dunno how that demo works, or if it uses technology that can be used from cc's http api
I've actually been meaning to make a moonscript compiler for CC

Not really a full compiler, just to steal some of it's features - I didn't intend on adding the ability of being able to omit ends, thens and dos, but I wanted to add simple stuff, like ! function calling and assignment operators.
ETHANATOR360 #10
Posted 12 October 2012 - 09:32 PM
cool
toxicwolf #11
Posted 13 October 2012 - 10:56 AM
Yeah, when you said that you had written stuff in CC using moonscript, I assumed that you meant that there was some level of moonscript compiler that could be installed into CC. It would be brilliant if someone could make one (I obviously don't have the skill/knowledge or I would try to myself), since it seems a really nice combination of programming languages.
toxicwolf #12
Posted 15 October 2012 - 10:22 PM
Looking at the source code, could you not use the moonscript module by loading it into one big API? For example the source code references things such as 'moonscript.init', so int would be a subtable loaded into the moonscript API and loaded from the init.lua file in the sourcecode. I'm sure there must be a function that uses the rest of the module to directly parse/compile moonscript strings to lua ones.
Kingdaro #13
Posted 18 October 2012 - 10:19 PM
Looking at the source code, could you not use the moonscript module by loading it into one big API? For example the source code references things such as 'moonscript.init', so int would be a subtable loaded into the moonscript API and loaded from the init.lua file in the sourcecode. I'm sure there must be a function that uses the rest of the module to directly parse/compile moonscript strings to lua ones.
That is a very good point, I never thought about that.

I'll be right back.

EDIT: Well, I attempted to write a compiler, but the "require" function doesn't even exist in CC apparently, which basically breaks the entire thing.

Time to keep waiting until someone remakes a compiler XD