Jump to content




LASM - ComputerCraft's First Alternate Programming Language


35 replies to this topic

#1 ElvishJerricco

  • Members
  • 803 posts

Posted 15 June 2013 - 08:33 PM

LASM - An Assembly Language for the Lua Virtual Machine


I'm sure many of us know that lua runs in a virtual machine via bytecode. You can get bytecode for a function by using string.dump(func). This is nice because you can compile Lua code into bytecode and save the bytecode instead of the source.

LASM is an assembly language for this bytecode. It's entire purpose is to allow the direct programming of Lua bytecode. On the surface, this isn't so useful. But theoretically, this could be useful in creating any new languages. It's much easier to target an assembly language in a compiler than it is to target Lua itself.

I'm not the first to create a LASM assembler for Lua, but I am the first to make one that's compatible with LuaJ and CC. And I think mine's pretty nice.

Here is a large writeup on the Lua bytecode specification as it stands today. The syntax used in their examples isn't exactly like my LASM implementation's syntax, but it's close.

Writing Your First LASM Program

Spoiler

Details of This LASM Language

Spoiler

Download: pastebin get ZghTBkmh lasm

Usage:

lasm [in file] [out file]

There is one little requirement though. LASM was designed with my Project NewLife in mind (which has been updated and now includes LASM), so it doesn't have any way of automatically making CraftOS able to run Lua bytecode files. So either at startup or at least before you try to run the output file, run the following code somehow.

function _G.loadfile(inFile)
    local data = {}
    local file = assert(fs.open(inFile,"rb"))
    for i = 1,fs.getSize(inFile)do
            data[i] = string.char(file.read())
    end
    file.close()
    return loadstring(table.concat(data), fs.getName(inFile))
end


#2 Yevano

  • Members
  • 376 posts
  • LocationUSA

Posted 15 June 2013 - 09:47 PM

Just tried the example out. This is really awesome what you've made here. I believe I shall embark on a new coding project. :D Perhaps a Lisp compiler. :P

#3 ElvishJerricco

  • Members
  • 803 posts

Posted 15 June 2013 - 10:19 PM

 Yevano, on 15 June 2013 - 09:47 PM, said:

Just tried the example out. This is really awesome what you've made here. I believe I shall embark on a new coding project. :D/> Perhaps a Lisp compiler. :P/>

I'm considering making a backend for LLVM that compiles to this assembly language. Then we'd be able to run C and C++ code in CC!

#4 superaxander

  • Members
  • 609 posts
  • LocationHolland

Posted 16 June 2013 - 05:31 AM

Nicely made! But there is no real point of this. *Is gonna try make a language too*

#5 ElvishJerricco

  • Members
  • 803 posts

Posted 16 June 2013 - 08:35 AM

 superaxander, on 16 June 2013 - 05:31 AM, said:

Nicely made! But there is no real point of this. *Is gonna try make a language too*

As I said above, the best reason for it is that it's easier to target an assembly language when building a compiler for a language than bytecode, so if you make a language, you'll have a better time compiling down to LASM than to bytecode or Lua.

#6 svdragster

  • Members
  • 222 posts
  • LocationGermany

Posted 16 June 2013 - 09:50 AM

Cool, nice job!

#7 M4sh3dP0t4t03

  • Members
  • 255 posts
  • LocationGermany

Posted 16 June 2013 - 10:28 AM

Nice job with this language. Maybe I will try to make a language with a compiler that compiles to this.

#8 ElvishJerricco

  • Members
  • 803 posts

Posted 16 June 2013 - 10:35 AM

 KingOfNoobs, on 16 June 2013 - 10:28 AM, said:

Nice job with this language. Maybe I will try to make a language with a compiler that compiles to this.

Thanks. I enjoyed learning compiler design to make it =P

#9 SiKeDDeMoNMC

  • Members
  • 4 posts

Posted 17 June 2013 - 09:10 AM

AMAZING!!!!!!

#10 Nvirjskly

  • New Members
  • 1 posts

Posted 18 June 2013 - 05:59 AM

This is great. I'm going to use this as a backend 100%.

#11 ElvishJerricco

  • Members
  • 803 posts

Posted 18 June 2013 - 07:46 PM

I think I'm going to make Objective Lua. It'll be to Lua what Objective C is to C. I'll probably target LASM instead of Lua so that I can have some more freedom in the compiler, but valid Lua code will be valid Objective Lua code. This could take a while to build...

#12 Dave-ee Jones

  • Members
  • 456 posts
  • LocationVan Diemen's Land

Posted 19 June 2013 - 03:59 AM

Wow. You made a programming language INSIDE a programming language. Nice work.

#13 M4sh3dP0t4t03

  • Members
  • 255 posts
  • LocationGermany

Posted 19 June 2013 - 10:47 AM

 AutoLocK, on 19 June 2013 - 03:59 AM, said:

Wow. You made a programming language INSIDE a programming language. Nice work.
That isn't something special. Lua, python and lots of other programming languages are written in other programming languages.

#14 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 19 June 2013 - 10:50 AM

 KingOfNoobs, on 19 June 2013 - 10:47 AM, said:

 AutoLocK, on 19 June 2013 - 03:59 AM, said:

Wow. You made a programming language INSIDE a programming language. Nice work.
That isn't something special. Lua, python and lots of other programming languages are written in other programming languages.
And as a matter of fact Lua for ComputerCraft is written in Java.

#15 ElvishJerricco

  • Members
  • 803 posts

Posted 19 June 2013 - 10:03 PM

 theoriginalbit, on 19 June 2013 - 10:50 AM, said:

 KingOfNoobs, on 19 June 2013 - 10:47 AM, said:

 AutoLocK, on 19 June 2013 - 03:59 AM, said:

Wow. You made a programming language INSIDE a programming language. Nice work.
That isn't something special. Lua, python and lots of other programming languages are written in other programming languages.
And as a matter of fact Lua for ComputerCraft is written in Java.

Realistically, it's impossible to create a new programming language without first creating it in another language. But once you've got it working you can use it to compile a compiler written in the new language so it needs no other language. Point is though, no language exists without some other language with the exception of machine language.

#16 NeptunasLT

  • Banned
  • 147 posts
  • LocationVilnius,Lithuania

Posted 20 June 2013 - 12:33 AM

Lua + Assembly = Bad.

#17 jesusthekiller

  • Banned
  • 562 posts
  • LocationWrocław, Poland

Posted 20 June 2013 - 01:00 AM

I used to say so, but it's actually powerful :)
Offtopic: You sure you use Unix? FYI Linux is not Unix. Last Unix release was in 1986....




 ElvishJerricco, on 19 June 2013 - 10:03 PM, said:

Realistically, it's impossible to create a new programming language without first creating it in another language. But once you've got it working you can use it to compile a compiler written in the new language so it needs no other language. Point is though, no language exists without some other language with the exception of machine language.

It is, but not in CC :)

#18 M4sh3dP0t4t03

  • Members
  • 255 posts
  • LocationGermany

Posted 20 June 2013 - 05:53 AM

Actually, every programming language except assembly is made within another language.

#19 jesusthekiller

  • Banned
  • 562 posts
  • LocationWrocław, Poland

Posted 20 June 2013 - 06:45 AM

Only thing that is not created in other language are logic gates.

#20 lieudusty

  • Members
  • 419 posts

Posted 20 June 2013 - 10:03 AM

Looks amazing! :D





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users