Jump to content




[Language] Luva v1.12 - Compiles to Lua


  • You cannot reply to this topic
62 replies to this topic

#41 Lemmmy

  • Members
  • 218 posts

Posted 01 January 2016 - 06:51 AM

View PostQuartz101, on 01 January 2016 - 04:57 AM, said:

I haz an idea:
use fs.open("/hello", "w") {
write("Hello!")
close()
}

Using `use` could also make the resource self-closing (close when the scope is closed).

#42 FUNCTION MAN!

  • Members
  • 292 posts

Posted 01 January 2016 - 02:26 PM

I'd say something more like python's with-as:

with fs.open("/test", "r+") as file {
  file.write("hello")
}

Which would compile into something like:

do
 local _rsx0_file = fs.open("/test", "r+")
 _rsx0_file.write("hello")

 if _rsx0_file.close then
  _rsx0_file.close()
 end
end

Edited by DemHydraz, 01 January 2016 - 02:26 PM.


#43 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 01 January 2016 - 03:04 PM

View PostQuartz101, on 01 January 2016 - 04:57 AM, said:

I haz an idea:
use fs.open("/hello", "w") {
write("Hello!")
close()
}
The trouble with this is that scope management is a pain: you don't know if the variable you are trying to access is in the table or in the current scope. Whilst you could solve this with metatables, this introduces more cognitive load for the programmer: what does this produce:?
use aMethod() {
write(foobar)
}
foobar (and write) could either be a local variable or a property on the table.

View PostDemHydraz, on 01 January 2016 - 02:26 PM, said:

I'd say something more like python's with-as:

with fs.open("/test", "r+") as file {
  file.write("hello")
}
This is already implemented as:
with file = fs.open("test", "w") {
		 file.write("testing!")
		 file.close()
}

Edited by SquidDev, 01 January 2016 - 03:04 PM.


#44 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 06 January 2016 - 12:49 PM

Just a quick question:
I have a table with __call metamethod. I want to call it with one argument (table) and I hate nested brackets, I can call it with tbl{}. Question: is tbl[] it translated to tbl{}?

Edited by Konlab, 06 January 2016 - 12:52 PM.


#45 Antelux

  • Members
  • 295 posts
  • LocationSomewhere in the middle of nowhere.

Posted 06 January 2016 - 09:11 PM

At the moment, you can't call functions without using (). However, doing tbl([]) would translate to tbl({})

EDIT: An early release of the compiler has been made. Check the main post for details.

Edited by Detective_Smith, 06 January 2016 - 09:31 PM.


#46 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 06 January 2016 - 09:55 PM

Would it be possible to have a GitHub repo where we have issues and everybody can help & contribute?

#47 Antelux

  • Members
  • 295 posts
  • LocationSomewhere in the middle of nowhere.

Posted 06 January 2016 - 09:56 PM

Yeah. I was going to put it there as well. I put the pastebin link for now so others can get to using it quicker. I just have to go soon, so I don't have time to put it on Github yet.

Edited by Detective_Smith, 06 January 2016 - 09:59 PM.


#48 Antelux

  • Members
  • 295 posts
  • LocationSomewhere in the middle of nowhere.

Posted 07 January 2016 - 01:19 AM

Added a Github for the program, which can be found here for those who wish to contribute or report bugs. :)
In the future, the wiki will host the tutorials for using the language.

Edited by Detective_Smith, 07 January 2016 - 01:19 AM.


#49 Antelux

  • Members
  • 295 posts
  • LocationSomewhere in the middle of nowhere.

Posted 18 January 2016 - 04:29 PM

Sorry that the next update it taking so long. The language isn't dead, It's just that finals are coming up, I need to study, etc. Though, that isn't to say I haven't been making progress. I've mostly been working on the class implementation, so here's another example of what you can do with it.

local class Vector3 (vec3) {
	 public x, y, z = 0, 0, 0
	 private shared: // Faster than regular private as its only declared once.
	       sqrt = math.sqrt
	       floor = math.floor

	 public:
		  function dot(v: vec3) { return self.x * v.x + self.y * v.y + self.z * v.z }
		  function cross(v: vec3) { return new Vector3(x: self.y*v.z-self.z*v.y, y: self.z*v.x-self.x*v.z, z: self.x*v.y-self.y*v.x ) }
		  function length() { return sqrt(self.x*self.x + self.y*self.y + self.z*self.z) }
		  function normalize() { return self * (1 / self.length()) }
		  function round() { return new Vector3(x: floor(self.x + 0.5), y: floor(self.y + 0.5), z: floor(self.z + 0.5)) }

	 metamethod: // Metamethods automatically have '__' added before each one, so metamethod add would be accessed as object.__add
		  function add(v: vec3) { return new Vector3(x: self.x += v.x, y: self.y += v.y, z: self.z += v.z) }
		  function sub(v: vec3) { return new Vector3(x: self.x -= v.x, y: self.y -= v.y, z: self.z -= v.z) }
		  function div(m: number) { return new Vector3(x: self.x *= m, y: self.y *= m, z: self.z *= m) }
		  function mul(m: number) { return new Vector3(x: self.x /= m, y: self.y /= m, z: self.z /= m) }
		  function unm(m: number) { return new Vector3(x: -self.x, y: -self.y, z: -self.z) }
		  function tostring() { return self.x..","..self.y..","..self.z }
}

You might notice I've added a "metamethod" keyword.
This example may not be the final product, however. I've sort of stuck on how I should implement things. It's not that I don't know how to code it, it's just that I'm not sure if I should do features over performance, or vice-versa.
Anyway, I should probably get back to studying. I'll keep updates about the progress of the language.

Edited by Detective_Smith, 18 January 2016 - 04:31 PM.


#50 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 19 January 2016 - 06:32 PM

View PostDetective_Smith, on 26 December 2015 - 06:59 PM, said:

Tables use '[]' instead of '{}' because of how they would often interfere with statements.
Consider the following below:

if x == 1 { print("yay!") }

The compiler would often try to make the line '{ print("yay!") }' into a table, which of course would error.

I dont know if anyone posted this but you can make like:
if ifFound then
   foundIF = true
end

if found "}" or "{" then
   do stuff...
   foundIF = false
end


#51 Antelux

  • Members
  • 295 posts
  • LocationSomewhere in the middle of nowhere.

Posted 19 January 2016 - 09:20 PM

View PostLeDark Lua, on 19 January 2016 - 06:32 PM, said:

I dont know if anyone posted this but you can make like:
if ifFound then
   foundIF = true
end

if found "}" or "{" then
   do stuff...
   foundIF = false
end

That wouldn't work unfortunately, as it isn't recursive.
I also feel as thought using {} for statements and not for tables flows a bit better. Though it may just be me.

#52 Antelux

  • Members
  • 295 posts
  • LocationSomewhere in the middle of nowhere.

Posted 21 January 2016 - 01:32 AM

So, I've found a sort of compromise between speed and power for classes.
After running a few benchmarks, it turns out that classes are just about as fast, if not as fast, as tables, when it comes to reading, writing, and creation time.
Though, I used a skeleton class for the example, which contained no private variables. I doubt it would slow it down much.
However, when writing to a public shared variable, it's about 3x as slow, so you should probably create a private shared variable and use a function to write to it instead if you need to rely on it's write speed.

Also, objects will have to use object:method() now in order to supply the method with the 'self' table.

One last thing that should be noted is that with this new implementation I have, all static and shared vars, regardless of it being private or public, cannot access regular private variables. Only public and private vars can. I thought this was worth the speed gain, but if anyone thinks otherwise, do lemme know.

Edited by Detective_Smith, 21 January 2016 - 01:34 AM.


#53 Antelux

  • Members
  • 295 posts
  • LocationSomewhere in the middle of nowhere.

Posted 01 February 2016 - 08:10 PM

I'm very close to releasing the next version, just have to sort out a few bugs here and there.
The metamethod keyword has been changed. It must be used after public, but can now be combined with shared or static.

I've also changed up classes a bit to allow for arguments to be given in a similar fashion to how a function gets them.
Therefore, I've also changed how you declare the custom type of the class's objects.

Here's an example below showing off these changes.
local class Vector3(x: number, y: number, z: number) @vec3 {
     public x, y, z = x or 0, y or 0, z or 0
     private shared floor, sqrt = math.floor, math.sqrt

     // Static and shared functions and variables allow for faster object creation time.
     // Also, you can have a ':' following the declared variables and functions. Though,
     // It should be noted that this is as optional as ';' is for statements.
     public static:
          function dot(v: vec3) { return self.x * v.x + self.y * v.y + self.z * v.z }
          function cross(v: vec3) { return Vector3(self.y*v.z-self.z*v.y, self.z*v.x-self.x*v.z, self.x*v.y-self.y*v.x ) }
          function length() { return sqrt(self.x*self.x + self.y*self.y + self.z*self.z) }
          function normalize() { return self * (1 / self.length()) }
          function round() { return Vector3(floor(self.x + 0.5), floor(self.y + 0.5), floor(self.z + 0.5)) }

     // Of course all these metamethods are accessed with a '__' prefix. For example, function add is accessed
     // as object.__add. However, as said before, we can use 'static' allowing for greater control over
     // metamethods. Naturally, the faster creation time benefit that comes with static follows here as well.
     public static metamethod:
          function add(v1: vec3, v2: vec3) { return Vector3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z) }
          function sub(v1: vec3, v2: vec3) { return Vector3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z) }
          function mul(v: vec3, m: number) { return Vector3(v.x * m, v.y * m, v.z * m) }
          function div(v: vec3, m: number) { return Vector3(v.x / m, v.y / m, v.z / m) }
          function unm(v: vec3, m: number) { return Vector3(-v.x, -v.y, -v.z) }
          function tostring(v) { return v.x..","..v.y..","..v.z }
}

Many class optimizations have also been put in, depending on what kind of declarations you use.
When comparing classes in Luva to their Lua output, they're about 2-3x shorter, which means you can program classes faster in Luva.

Anyway, if anyone has any questions, comments or criticism about the above example, do let me know.

Edited by Detective_Smith, 02 February 2016 - 09:52 PM.


#54 Antelux

  • Members
  • 295 posts
  • LocationSomewhere in the middle of nowhere.

Posted 05 February 2016 - 09:24 PM

I've been thinking about the ability to add default values for classes and functions.
Consider the following code:
local class Object(color: number, size: number, shape: string) {
     public:
          color = color
          size = size
          shape = shape
}

Using the current implementation, I cannot have both optional values and type checking.
If any one of those arguments aren't given, then it will error saying it's not a number.

To solve this, like I said before, I'd like to add the ability to use default values.
Though, the thing is, I'm not sure what I should have for the syntax.

I'm currently considering this:
local class Object(color: number => 1, size: number => 1, shape: string => "square") {
     public:
          color = color
          size = size
          shape = shape
}

Here, color and size default to 1 if not given an argument, and shape defaults to "square" if not given.
Of course, if they are given, the arguments must conform to the type specification.

I'd just like some thoughts on the syntax, as I'm not too sure about it.

#55 Antelux

  • Members
  • 295 posts
  • LocationSomewhere in the middle of nowhere.

Posted 13 February 2016 - 08:24 PM

After quite some time, I've updated Luva, with its biggest feature being Classes.
For now, you can refer to its Github wiki for tutorials, or you can look at the below example to see what you can do with classes.

local class Entity(name: string => "Unknown", king: string => "Unknown", spec: string => "Unknown", sound: string => "...", test => "boolean") @entity {
     public name, kingdom, species, sound = name, king, spec, sound
     private shared totalEntities = 0

     public static:
          function sayName() {
               print("My name is " ..self.name)
          }

          function makeSound() {
               print(self.sound)
          }

          function getTotal() {
               print("There are " ..totalEntities.. " entities.")
          }

     private function init() {
          totalEntities++
     }
}

local class Animal(name: string, spec: string, sound: string) @animal extends Entity {
     @super(name, "Animalia", spec, sound)

     private shared totalAnimals = 0
     public sound = sound

     public static function getTotal() {
          print("There are " ..totalAnimals.. " animals.")
     }

     private function init() {
          totalAnimals++
     }
}

local class Human(name: string, age: number) @human extends Animal {
     @super(name, "Homo sapien", "Hi there!")

     private shared totalHumans = 0
     private age = age

     public function getAge() {
          print("I'm " ..age.. " years old.")
     }

     public static function getTotal() {
          print("There are " ..totalHumans.. " humans.")
     }

     private function init() {
          totalHumans++
     }
}

local e1 = Entity()
local a1 = Animal("Leo", "Lion", "RAWR!")
a1:makeSound()

local h1 = Human("Bob", 24)
h1:getAge()

e1:getTotal()
a1:getTotal()
h1:getTotal()

/* The output of the program:
RAWR!
I'm 24 years old.
There are 3 entities.
There are 2 animals.
There are 1 humans.
*/

Edited by Detective_Smith, 13 February 2016 - 08:27 PM.


#56 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 13 February 2016 - 08:42 PM

Cool update, gonna try it out tomorrow cuz I'm on the phone.

EDIT: Tried it, loved it. I'm starting to use this language as a Main one.

Edited by LeDark Lua, 14 February 2016 - 03:10 PM.


#57 Antelux

  • Members
  • 295 posts
  • LocationSomewhere in the middle of nowhere.

Posted 14 February 2016 - 03:17 PM

Glad to hear it. Was hoping at least one person would use the language. :P
Anyway, do let me know if you run into any bugs. I don't think there are any, but then again, I usually don't know about any anyway.
They're always hiding until they're found. :P

#58 Antelux

  • Members
  • 295 posts
  • LocationSomewhere in the middle of nowhere.

Posted 04 March 2016 - 11:16 PM

So, here's a progress report:

I've been working on re-writing the entire program.
Right now, the size is a lot smaller, and I've added quite a few things.
Here's what you can expect in the next version:
  • Generally cleaned up, rewritten code.
  • for-in loops are now possible.
  • functions can now use default values.
  • Using () with function creation is now optional.
  • catch now works with non-string values.
  • Re-added switch statements.
  • Compiled code is now indented.
  • Plenty of minor bug-fixes.
And here's what I'd like to add to this version, but I'm still working on:
  • Making two kinds of switch statements. The first one, 'switch', would act as a table. The second one, 'switch!', would simply be a short-hand version of writing if-then statements.
  • Add the 'extension' keyword for extending classes.
  • Making overloaded functions more flexible (Such as multiple functions that have the same amount of arguments, but different types. Also working on this at the moment.)
  • A type of operator chaining. For example, 1 < 2 < 3 == 3, 4 > 2 < 6 ~= 5, etc. I'm just not sure about what syntax I should use for it.
  • More code optimizations in general.
It'll be quite the update, I suppose. If anyone else has any ideas they'd like to chip in for said upcoming update, do say them.

Edited by Detective_Smith, 20 March 2016 - 02:26 PM.


#59 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 20 March 2016 - 09:17 AM

Question: Is this pure Lua? (Is it possible to run it outside CC?)
Idea: Custom compiler parameters that can add custom keywords/etc. Or an API that can do this.
I really like this project. I will definitely use it when I return to lua side of computercraft.

#60 Antelux

  • Members
  • 295 posts
  • LocationSomewhere in the middle of nowhere.

Posted 20 March 2016 - 02:18 PM

Well, the compiled output is pure Lua, so it is possible to use it in places outside of ComputerCraft, such as for LÖVE.
Also, can you give an example of the custom keyword thing you're thinking about?

EDIT: Also, I've made arithmetic and binary assignment operators a bit more useful:
local x, y, = 10, 20
x, y += 30
// The above is translated into x, y = x + 30, y + 30
// Can be used with any amount of vars, and with things such as *=, >>=, &=, .=, etc

Edited by Detective_Smith, 20 March 2016 - 02:22 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users