Jump to content




[Language] Luva v1.12 - Compiles to Lua


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

#21 Creator

    Mad Dash Victor

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

Posted 30 December 2015 - 12:21 AM

Classes:

class Example {
private:
foo
foo2
public:
bar
bar2
constructor:
Example (a,b,c) {
foo = a
foo2 = b
bar(c)
}
}

would compile to:

function Example(a,b,c)
  --Private
  local foo
  local foo2
  --Public
  local self = {}
  self.bar = 0 --It exists
  self.bar2 = function(wow)
    --actions
  end
  --Constructor
  foo = a
  foo2 = b
  self.bar(c)
  return self
end


#22 Antelux

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

Posted 30 December 2015 - 01:24 AM

Oh, only public and private variables? I thought you had something more specific in mind. I plan to do those, as well as a few other kinds. As well as a constructor too, of course.

#23 Creator

    Mad Dash Victor

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

Posted 30 December 2015 - 01:33 AM

Only a basic example.

#24 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 30 December 2015 - 01:37 PM

I would say, don't change the the regular Lua much for faster learning purposes :)

Edited by LeDark Lua, 30 December 2015 - 01:38 PM.


#25 Antelux

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

Posted 30 December 2015 - 07:20 PM

Anything in particular that you want to stay from Lua?

#26 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 30 December 2015 - 07:56 PM

Nope, Lua is wonderful the way it is but your introduction to new function styles and stuff is amazing!

EDIT:
One thing:
function test
   (x) {
	  
   }
   (x, y) {
	  
   }
   (x, y, z) {
	  
   }
end
"end" at the end of the function

EDIT2:
Ok now I understood your question.... silly me, I'm half asleep sorry :D

Edited by LeDark Lua, 30 December 2015 - 08:00 PM.


#27 Antelux

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

Posted 30 December 2015 - 08:06 PM

I'm not sure if 'end' really 'belongs' for that overloaded function. If I were to implement it, it would be the only one to use end as of now.

#28 Antelux

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

Posted 30 December 2015 - 08:53 PM

As of now, I'm still working on classes. Here's what the current code looks like:

local class Test {
	 // We can define private and public variables like so:
	 private a, b, c = 1, 2, 3
	 public d, e, f = 4, 5, 6

	 // There are also public static and public shared variables.
	 // Static variables are variables that cannot be changed (I know in other languages static means something different, but I feel as though it makes more sense for it to be this way).
	 public static g, h, i = 7, 8, 9
	 // Shared variables are variables that if one object changes it, all other objects have the same changed value. Therefore, the variable is 'shared' between objects.
	 public shared j, k, l = 10, 11, 12

	 // We can also define variables like this. It's a preference thing, really.
	 private
		  one = 1
		  two = 2
		  three = 3

	 public
		  four = 4
		  five = 5
		  six = 6

	 // Common function declaration
	 private function ABC() {
		  // When we refer to private variables, we don't use 'self.' Instead, we write them as they are.
		  print(a, b, c)
	 }

	 public function DEF() {
		  // When we refer to public variables, we do use 'self.' as to refer to the individual object.
		  print(self.d, self.e, self.f)
	 }
}

This is some example code for classes once they are fully complete:
local class Animal {
	 public name = "Unknown"
	 public species = "Unknown"
	 public sound = "Silence"
	 private age = 0

	 private shared totalAnimals = 0
	 public static kingdom = "Animalia"

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

	 public static function getOlder(amount: number) {
		  age += amount
	 }

	 public static function getAge() {
		  return age
	 }

	 public static function getTotal() {
		  return totalAnimals
	 }

	 private function __init() { // Ran every time a new object is created.
		  totalAnimals++ // All objects from this class now have this updated variable.
	 }
}

local Dog = new Animal(species: "Dog", sound: "Bark")
Dog.name = "Stray Dog"
Dog.makeSound() // Prints "Bark"

// We can put the type of the object in () during a class declaration.
local class Cat (feline) extends Animal {
	 public static override species = "Cat"
	 public static override sound = "Meow"

	 private function __init() {
		  super.makeSound() // super refers to the class it extends
	 }
}

local cat1 = new Cat() // Prints "Meow"
if type(cat1) == "feline" { // True, as we assigned the object's type as 'feline'
	 print("I think it's a cat.")
} else {
	 print("Dunno what it is.")
}
print("Number of animals: " ..cat1.getTotal()) // prints "Number of animals: 2"

Also, as a side note, I added a new operator. It's for strings, really.
local str = "test"
str.="ing"
print(str) // Prints "testing"

Edited by Detective_Smith, 30 December 2015 - 09:10 PM.


#29 Creator

    Mad Dash Victor

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

Posted 30 December 2015 - 09:08 PM

I would change
private
		  one = 1
		  two = 2
		  three = 3

to

private:
		  one = 1
		  two = 2
		  three = 3

because it can be interpreted as

private one = 1
		  two = 2 --undefined
		  three = 3 --undefined

And I would use the self table to refer to public objects, not from public methods.

Static would use metatables, making it VERY slow.

Also, shared variables can be in a shared table, declared outside the function generating the object.

#30 Antelux

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

Posted 30 December 2015 - 09:20 PM

View PostCreator, on 30 December 2015 - 09:08 PM, said:

I would change
private
		  one = 1
		  two = 2
		  three = 3

to

private:
		  one = 1
		  two = 2
		  three = 3

because it can be interpreted as

private one = 1
		  two = 2 --undefined
		  three = 3 --undefined

I've made sure it wouldn't interpret it as such.

View PostCreator, on 30 December 2015 - 09:08 PM, said:

And I would use the self table to refer to public objects, not from public methods.

The self table is used in order to tell what function you're calling exactly.
Consider the following example:
local function test() {
// Does stuff
}

local class A {
	 public function test() {
	 // Does stuff
	 }

	 public function abc() {
		  test() // Can't tell without 'self' if calling outside function or not.
	 }
}

View PostCreator, on 30 December 2015 - 09:08 PM, said:

Static would use metatables, making it VERY slow.

These classes are designed with power over performance in mind.
However, there have been optimizations made to make sure that if you don't use static or shared, it won't use metatables.

View PostCreator, on 30 December 2015 - 09:08 PM, said:

Also, shared variables can be in a shared table, declared outside the function generating the object.

I know. That was already being done. Thanks for the reminder, though.

Edited by Detective_Smith, 30 December 2015 - 09:23 PM.


#31 Creator

    Mad Dash Victor

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

Posted 30 December 2015 - 09:30 PM

View PostDetective_Smith, on 30 December 2015 - 09:20 PM, said:


The self table is used in order to tell what function you're calling exactly.
Consider the following example:
local function test() {
// Does stuff
}

local class A {
	 public function test() {
	 // Does stuff
	 }

	 public function abc() {
		  test() // Can't tell without 'self' if calling outside function or not.
	 }
}



Local scope gets priority over global scope, so the test function inside the class definition would be called. Also, there is no reason to use the self table when using public functions, since public and private functions both get the same environment.

That makes sense?

#32 Antelux

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

Posted 30 December 2015 - 09:32 PM

View PostCreator, on 30 December 2015 - 09:30 PM, said:

Local scope gets priority over global scope, so the test function inside the class definition would be called. Also, there is no reason to use the self table when using public functions, since public and private functions both get the same environment.

That makes sense?

Assuming I didn't use self, how would I call test() from the global scope, then?

#33 Creator

    Mad Dash Victor

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

Posted 30 December 2015 - 09:40 PM

View PostDetective_Smith, on 30 December 2015 - 09:32 PM, said:


Assuming I didn't use self, how would I call test() from the global scope, then?

Since test is defined as public, to call the test inside the class, you use self.test, and because there is no private self, test would simply be equal to the outside one.

#34 Antelux

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

Posted 30 December 2015 - 09:53 PM

View PostCreator, on 30 December 2015 - 09:40 PM, said:

Since test is defined as public, to call the test inside the class, you use self.test, and because there is no private self, test would simply be equal to the outside one.

To me, it just makes more sense to use self.test when referring to the object itself, rather than when referring to an outside source.
Though, with the current set up, if you really wanted to call a function without self, you can just make a private function.

local class Test {
     private function doSomething() {
          // Does stuff
     }

     public function ABC() {
          doSomething()
     }
}

The way classes are created doesn't really allow for what you're speaking about: referring to an outside source using self.

#35 Creator

    Mad Dash Victor

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

Posted 30 December 2015 - 10:11 PM

I wasn't suggesting referring to an outside source using self. I thought you wanted to refer to the object itself when using public function using self.

TL;DR: Use self to refer to public attributes/methods and use "only variable name" when referring to local stuff.

#36 Antelux

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

Posted 30 December 2015 - 10:13 PM

Oh, my bad then. Well, that's how it's done at the moment.
local class Test {
	 public function ABC() { }
	 public function test() {
		  self.ABC()
	 }
}
Again, it's because of the way classes are structured at the moment.

Edited by Detective_Smith, 30 December 2015 - 10:13 PM.


#37 Creator

    Mad Dash Victor

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

Posted 30 December 2015 - 10:21 PM

Exactly! That it how it should be done.

#38 Antelux

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

Posted 30 December 2015 - 10:38 PM

And now I'm laughing because I most likely misinterpreted you earlier. Looks like we don't have any disagreements, then. :)

#39 Antelux

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

Posted 01 January 2016 - 03:15 AM

After taking a little break from classes, I've implemented try, with, and catch statements. Check the bottom of the main post for more info and their usage.

#40 Quartz101

  • Members
  • 141 posts
  • Location/dev/nvme0n1

Posted 01 January 2016 - 04:57 AM

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

Edited by Quartz101, 01 January 2016 - 04:57 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users