Author Topic: [LuaZM] Back2BASIC: a library  (Read 8519 times)

0 Members and 1 Guest are viewing this topic.

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
[LuaZM] Back2BASIC: a library
« on: March 06, 2013, 12:59:46 pm »
What it is
Back2BASIC is a library that allows you to have all the functions you loved in BASIC. This will help BASIC programmers switch to lua, as well as making it more straightforward to port BASIC programs to lua.

Already implemented
- b2b.menu (a prefabricated menu function)
- b2b.printText (like "HELLO" in BASIC, but with LESS-style page breaks)

To be implemented
- a graphing function
- Locate

Usage
Make a folder called "lib" in your root directory. Put b2b.lua in this folder.
In the program you wish to use the library, put run([[lib\b2b.lua]]) at the top.

Function syntax
NOTE: all width, height, x, and y values are not pixels, but characters.
b2b.menu(x, y, width, height, title, array, color1, color2)
b2b.printText(string, colorfg, colorbg)

Screenshots
See "Known Bugs"

Code

lib\b2b.lua
Code: [Select]
b2b={}
b2b.version="0.1beta"

b2b.menu = function (x, y, width, height, title, array, color1, color2)
local x=x*18-18
local y=y*18-18
local continue=0
local selected=1
local max=height
local j=1
while continue==0 do
--handle keys
if key==28 then
if selected>1 then
selected=selected-1
if selected<max-height+1 then max=selected+height-1 end
else
selected=#array
max=#array
end
end

if key==37 then
if selected<#array then
selected=selected+1
if selected>height then max=selected end
else
selected=1
max=height
end
end

if key==31 then
continue=1
end

--draw menu
zmg.drawRectFill(x+1, y+18, width*12, height*18, color2)
zmg.drawText(x+1, y+1, title, color2, color1)

j=1
for i=max-height+1, max, 1 do
if selected==i then
zmg.drawText(x+1, y+(j*18), array[i], color2, color1)
else
zmg.drawText(x+1, y+(j*18), array[i], color1, color2)
end
j=j+1
end


--refresh screen
zmg.fastCopy()
--keyMenu
if continue~=1 then key=zmg.keyMenu() end
end
return selected
end

b2b.printText = function (string, colorfg, colorbg)
local substring={}
local k=1
--if string extends past screen end (x)
if #string>31 then
--split string into screen width sized portions
for i=1, math.floor(#string/31)+1, 1 do
substring[i] = string.sub(string, i*31-30, i*31)
end
end
--display
for j=1, math.floor(#substring/11)+1, 1 do
k=1
--clear screen
zmg.drawRectFill(0, 0, 384, 216, colorbg)
for i=j*11-10, j*11, 1 do
if substring[i] then zmg.drawText(1, k*18-18, substring[i], colorfg, colorbg) end
k=k+1
end
zmg.drawText(1, 198, "Press a key (Page " .. j .. "/" .. math.floor(#substring/11)+1 .. ")", colorbg, colorfg)
--refresh
zmg.fastCopy()
--wait
zmg.keyMenu()
end
end

demo.lua
Code: [Select]
run([[lib\b2b.lua]])
zmg.clear()

test = b2b.menu(1, 1, 15, 5, "Demo", {"display text","entry2","entry3","entry4","entry5","entry","entry","entry","entry"}, zmg.makeColor("blue"), zmg.makeColor("black"))
print(test)

teststring="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed porttitor, sapien quis sagittis sodales, metus felis faucibus sem, eget mollis erat dolor non elit. Cras id nibh vel massa auctor euismod. Fusce semper rutrum neque, ut faucibus lacus egestas at. Donec velit augue, pulvinar sit amet vulputate et, consectetur iaculis neque. Quisque eu enim eu est condimentum feugiat. Duis gravida ultrices elit ac malesuada. Sed dui sapien, hendrerit nec mattis in, viverra sit amet nibh. Donec vel sodales risus. Duis facilisis cursus placerat. Donec sed ligula sed odio mollis posuere. Integer ante sapien, cursus eu eleifend in, tristique vehicula arcu. Fusce vel erat nibh, non placerat quam. Vivamus quis nibh ut est rhoncus faucibus et vestibulum elit. Pellentesque vel dui eget leo varius faucibus a ac mi. Nulla a nulla non enim euismod ornare a non mauris. Proin ut sagittis turpis. Maecenas in sem tellus, sit amet placerat ligula. Praesent non augue tellus, a convallis lacus. Curabitur suscipit consectetur aliquet. Nunc vehicula lorem in odio accumsan a placerat neque aliquam. Etiam ultricies orci eu justo dapibus vel elementum libero fermentum. Donec et risus nisi, non pulvinar nunc. Duis quis sem neque. Ut eu dignissim nisl. Maecenas a nisl risus, sed consequat ante. Morbi vitae imperdiet erat. Suspendisse potenti. In urna est, viverra id hendrerit vel, pulvinar eget felis. Donec suscipit, dui ac molestie molestie, mi orci scelerisque mi, quis cursus mi metus eget nibh. Aliquam commodo mi at eros sagittis dictum. Nunc ultrices turpis eu urna luctus dictum. Cras sollicitudin ante quis metus aliquet rutrum. Nam vestibulum velit commodo risus ornare eu varius sem placerat. Maecenas egestas odio eu mi tincidunt in porttitor lorem consequat. Proin libero risus, venenatis ut suscipit et, varius non sapien. Suspendisse eget elementum dolor."

if test==1 then b2b.printText(teststring, zmg.makeColor("blue"), zmg.makeColor("black")) end

Known Bugs
- All the functions use zmg.keyMenu(), so it should be possible to use Screen Record to record the program. However, if you run them while in ScreenRecord mode, your calc will crash with the "SYSTEM ERROR" message.

- running demo.lua twice in a row gives an error starting with "demo.lua:1:syntax error near <eof>"



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: [LuaZM] Back2BASIC: a library
« Reply #1 on: March 06, 2013, 01:28:16 pm »
This is interesting. I think that if the syntax is very similar to TI-BASIC or Casio BASIC, it might be popular eventually, because some people tend to dislike having to learn a totally new language. This would also be a great alternative to the insanely slow PRIZM drawing commands.

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
Re: [LuaZM] Back2BASIC: a library
« Reply #2 on: March 06, 2013, 01:38:26 pm »
This is interesting. I think that if the syntax is very similar to TI-BASIC or Casio BASIC, it might be popular eventually, because some people tend to dislike having to learn a totally new language. This would also be a great alternative to the insanely slow PRIZM drawing commands.

Well, I suppose I could make the syntax the same, but I sort of like the customizability I have right now.

like in BASIC:
Code: [Select]
menu "TITLE","ENTRY1",1,"ENTRY2",2

with b2b:
Code: [Select]
var = menu(1, 1, 10, 5, "TITLE", {"Entry1", "Entry2"}, zmg.makeColor("blue"), zmg.makeColor("white"))



I guess I could change the syntax to this if there was a demand for it:
Code: [Select]
var = menu("TITLE",{"Entry1", "Entry2"})
« Last Edit: March 06, 2013, 01:39:21 pm by flyingfisch »



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: [LuaZM] Back2BASIC: a library
« Reply #3 on: March 06, 2013, 01:43:05 pm »
Ah ok, well I meant not changing it too drastically to the point where it's just like a computer language that isn't any easier than LuaZM itself. More customizability would be nice, though, as above. What I like in some TI-BASIC commands is how new color arguments are optional, so porting programs is easy.

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
Re: [LuaZM] Back2BASIC: a library
« Reply #4 on: March 06, 2013, 02:04:51 pm »
I am trying to decide whether or not to try to add goto... what do you think?



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: [LuaZM] Back2BASIC: a library
« Reply #5 on: March 06, 2013, 02:35:12 pm »
It might be a good idea for compatibility and for certain programs, but some people tend to consider using Goto/Lbl as bad practice. It could be useful in some cases like sub-routines I guess.

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
Re: [LuaZM] Back2BASIC: a library
« Reply #6 on: March 06, 2013, 02:41:26 pm »
... but some people tend to consider using Goto/Lbl as bad practice.

I know... I don't really like giving people that functionality, but I'll wait and see. If people want it i will implement it.



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
Re: [LuaZM] Back2BASIC: a library
« Reply #7 on: March 06, 2013, 08:00:33 pm »
OK, I made a git repo for this, and added a couple functions (read the readme).
« Last Edit: March 08, 2013, 10:41:05 am by flyingfisch »



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: [LuaZM] Back2BASIC: a library
« Reply #8 on: March 07, 2013, 03:45:31 am »
Cool to hear. :) And yes it's always good to back up often :P (even if not programming on-calc, people lose stuff due to hard drive failures x.x)

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
Re: [LuaZM] Back2BASIC: a library
« Reply #9 on: March 07, 2013, 09:25:46 am »
(even if not programming on-calc, people lose stuff due to hard drive failures x.x)

That happened to a friend of mine and I don't really want to learn from experience, so.

Also, about goto, i found that it was already implemented in lua 5, so we're good.

So far, here are all the functions:

- b2b.menu (a prefabricated menu function)
- b2b.printText (like "HELLO" in BASIC, but with LESS-style page breaks)
- b2b.locate (like locate in BASIC)
- b2b.ygraph (graphs a y= graph)


And I plan to make a string input function today, so stay tuned! :)



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: [LuaZM] Back2BASIC: a library
« Reply #10 on: March 07, 2013, 02:40:32 pm »
Is printText like Disp on TI calcs (displaying text then moving to next line, scrolling if needed)?

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
Re: [LuaZM] Back2BASIC: a library
« Reply #11 on: March 07, 2013, 07:37:23 pm »
Is printText like Disp on TI calcs (displaying text then moving to next line, scrolling if needed)?


Nope. It displays a page, and waits for a key until the next page is shown.

also...

UPDATE:
Added b2b.inputString()




Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
Re: [LuaZM] Back2BASIC: a library
« Reply #12 on: March 08, 2013, 12:13:20 pm »
OK, people, I have run out of ideas temporarily!

Any features that are missing in lua that you had in BASIC? Please ideas in this thread.  :)



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: [LuaZM] Back2BASIC: a library
« Reply #13 on: March 08, 2013, 11:31:58 pm »
I sadly have no clue about Lua since I never coded it, but does it feature floating points, the ability to copy a flash program to RAM and delete RAM programs?

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
Re: [LuaZM] Back2BASIC: a library
« Reply #14 on: March 09, 2013, 02:24:04 pm »
I sadly have no clue about Lua since I never coded it, but does it feature floating points, the ability to copy a flash program to RAM and delete RAM programs?

Hmm... I think you are thinking about TI calculators.

LuaZM programs are run from the 16MB storage memory.

And yes, lua does support floating points.



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0