|
| 08 Apr 2015 03:29 PM |
you can init the super class by calling super(...)
I also made it so instead of using CLASS:METHOD(...) you do CLASS.METHOD(...) because I think it looks better (self is automatically passed as the first arguments thx string manip)
Here's some sample code (if you have no idea what im talking about check here: http://www.roblox.com/Forum/ShowPost.aspx?PostID=159660913)
class Animal init(name) self.name = name end method sayName() print("Hello! I am an animal and my name is " .. self.name) end @end
class Dog extends Animal init(name, breed) super(name) self.breed = breed end method bark() print(self.name .. " barked!") end @end
local pedro = new Dog("Pedro", "Chocolate Lab")
pedro.sayName() // OUTPUT: Hello! I am an animal and my name is Pedro! pedro.bark() // OUTPUT: Pedro barked! |
|
|
| Report Abuse |
|
|
|
| 08 Apr 2015 03:31 PM |
This is what the code is turned into, (it kinda does what MoonScript does) (note im too lazy to format it, I just printed it to the output)
Animal = setmetatable({}, { __call = function(self, ...) local inherit_list = {self} local instance; self.__index = function(_, key) for i, v in ipairs(inherit_list) do if v[key] then if type(v[key]) == "function" then return function(...) return v[key](instance, ...) end end return v[key] end end end self.__super = inherit_list[2] or {} instance = setmetatable({}, self) for i, v in ipairs(inherit_list) do if v.init then v.init(instance, ...) end end return instance end }) function Animal.init(self, name) self.name = name end function Animal.sayName(self) print("Hello! I am an animal and my name is " .. self.name) end
Dog = setmetatable({}, { __call = function(self, ...) local inherit_list = {self, Animal} local instance; self.__index = function(_, key) for i, v in ipairs(inherit_list) do if v[key] then if type(v[key]) == "function" then return function(...) return v[key](instance, ...) end end return v[key] end end end self.__super = inherit_list[2] or {} instance = setmetatable({}, self) for i, v in ipairs(inherit_list) do if v.init then v.init(instance, ...) end end return instance end }) function Dog.init(self, name, breed) if self.__super.init then self.__super.init (self, name) end self.breed = breed end function Dog.bark(self) print(self.name .. " barked!") end
local pedro = Dog ("Pedro", "Chocolate Lab")
pedro.sayName() pedro.bark() Hello! I am an animal and my name is Pedro Pedro barked! |
|
|
| Report Abuse |
|
|
ked2000
|
  |
| Joined: 10 Jul 2011 |
| Total Posts: 1059 |
|
|
| 08 Apr 2015 03:32 PM |
| Looks more like Lua + Java(Lava) than Lua + C++(L++). |
|
|
| Report Abuse |
|
|
|
| 08 Apr 2015 03:34 PM |
| it's not supposed to be Lua + C++ I just call it L++ for Lua plus more |
|
|
| Report Abuse |
|
|
instawin
|
  |
| Joined: 04 Jun 2013 |
| Total Posts: 8777 |
|
| |
|
ked2000
|
  |
| Joined: 10 Jul 2011 |
| Total Posts: 1059 |
|
|
| 08 Apr 2015 03:36 PM |
| I know, it's just a name after all. Anyways, this seems pretty cool, how long does it take to run a file and does L++ support cross-file inheritance? |
|
|
| Report Abuse |
|
|
|
| 08 Apr 2015 03:37 PM |
@ked
yeah I have build in Array and String libraries that are automatically loaded in
As for how long it takes, im not really sure, I gotta to some testing |
|
|
| Report Abuse |
|
|
| |
|
ked2000
|
  |
| Joined: 10 Jul 2011 |
| Total Posts: 1059 |
|
|
| 08 Apr 2015 03:43 PM |
Here's a suggestion, add member accessors(public, private, protected).
Uppercase members are public, lowercase members or private, and members that start with a _ or some special character are protected. |
|
|
| Report Abuse |
|
|
|
| 08 Apr 2015 03:44 PM |
alright, I'll see what I can do
So much work D:
|
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 08 Apr 2015 05:33 PM |
| Well maybe it's time you rewrite your lexer c: |
|
|
| Report Abuse |
|
|
ked2000
|
  |
| Joined: 10 Jul 2011 |
| Total Posts: 1059 |
|
| |
|
|
| 08 Apr 2015 05:37 PM |
well i implemented some new stuff not how i feel about it
but for loops are like
for i in 1, 10 print i end
and pair/ipairs are like:
for i, v in table
end
forn i, v in table (n for "number", this is ipairs)
end
you can also optionally do:
for v in table end forn v in table end |
|
|
| Report Abuse |
|
|
|
| 08 Apr 2015 05:38 PM |
| not sure how I feel about it* |
|
|
| Report Abuse |
|
|
|
| 08 Apr 2015 05:43 PM |
It doesn't really have a lexer..... Whenever I need something new I create a function to do it. eg)
local function parse_super_func_call() local base = [[ if self.__super.init then self.__super.init %s end ]] local f_call = "(self, " local p_count = 1 while space() and getchar() ~= "(" do up() end up() while space() and p_count > 0 do local char = getchar() f_call = f_call .. char if char == ")" then p_count = p_count - 1 elseif char == "(" then p_count = p_count + 1 end up() end r |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 08 Apr 2015 05:44 PM |
| You should make a lexer and parser, maybe even make your code compile into Lua bytecode instead of Lua :) that would be neat. |
|
|
| Report Abuse |
|
|
|
| 08 Apr 2015 05:45 PM |
| I would have zero idea how to turn it into Lua bytecode lol |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 08 Apr 2015 05:47 PM |
| Well it might not really be a good idea anyways considering that Lua bytecode is volatile and Lua itself isn't really going to ever change. |
|
|
| Report Abuse |
|
|
Labstein
|
  |
| Joined: 02 Feb 2015 |
| Total Posts: 970 |
|
|
| 08 Apr 2015 05:47 PM |
| Oh my god it's so precious and beautiful. |
|
|
| Report Abuse |
|
|
|
| 08 Apr 2015 05:48 PM |
| what do you mean by "volatile"? |
|
|
| Report Abuse |
|
|
Lecturous
|
  |
| Joined: 17 Aug 2013 |
| Total Posts: 1096 |
|
|
| 08 Apr 2015 05:49 PM |
how did u manage to do this q-q
~ Post by Lecturous ~ |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 08 Apr 2015 05:50 PM |
| I mean it is probably going to change every Lua update, making your code less portable unless you have a separate 'lua bytecode generator' for each Lua version. |
|
|
| Report Abuse |
|
|
|
| 08 Apr 2015 05:53 PM |
@cnt
now I have the sudden urge to convert it to bytecode help
@lect
1) read all core library files and load them into a string 2) do tons of string manipulation 3) loadstring |
|
|
| Report Abuse |
|
|
Lecturous
|
  |
| Joined: 17 Aug 2013 |
| Total Posts: 1096 |
|
| |
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 08 Apr 2015 05:58 PM |
| Well I mean there is a "cheating" way to convert it into bytecode, but it ruins the whole purpose and makes doing it at all a waste: string.dump the loadstring that you use to run your code stuff at the end. Although that's stupid because there would be no point in doing that since you are still having to generate Lua code first. The cool thing about going straight to bytecode is that since it is your language, you know what works and what won't making it much easier to optimize the bytecode better than the Lua compiler anyways since it is not really an optimizing compiler at all. |
|
|
| Report Abuse |
|
|