loadstring("\27\76\117\97\81\0\1\4\4\4\8\0\56\0\0\0\64\67\58\47\85\115\101\114\115\47\74\97\115\111\110\47\68\111\99\117\109\101\110\116\115\47\80\114\111\103\114\97\109\109\105\110\103\47\76\117\97\47\77\79\68\83\47\116\101\115\116\46\108\117\97\0\13\0\0\0\13\0\0\0\0\0\0\2\1\0\0\0\30\0\128\0\0\0\0\0\0\0\0\0\1\0\0\0\13\0\0\0\0\0\0\0\0\0\0\0")();
local mods = require("MODS.Current.main");
local bytecode = mods.Assemble(
[[
; Hello_World.lasm
.options 0, 0, 0, 2 ; a directive, which sets some properties, the '2' meaning MaxStackSize
getglobal 0, <'print'> ; <> denotes a constant, i've yet to bother with a directive for doing constants
loadk 1, <'Hello World!'> ; The LuaVM is register based, we put the function in one register, then arguments above it
call 0, 2, 1 ; now we call register 0, with (2-1) parameters, accepting (1-1) values back
return 0, 0 ; and now we return
]]);
loadstring(bytecode)() -- turns the bytecode into a function, then we call that function
>Hello World!
Hmm Interesting. I unfortunately no longer am into programming but I'M glad to see more programming tools coming out. For which platforms will this be?I've yet to run the assembler/linker on calc (the code is horendous (no planning), and it would be really slow for large files.
Looks real nice. So that means another programming language for the NSpire? (And maybe anything that runs Lua?) Cool.I'm not sure I would call it an entire new language, since the code it produces is ran normally in Lua. It's just a more minimalistic version of Lua.
.options 0, 0, 0, 200 ; yay for large stacks!
.macro print(s, msg){
getglobal s, <'print'>
loadk s + 1, msg
call s, 2, 1
}
print 0, <"Omnimaga :)"> ; this line will be replaced with everything in the print macro (this is good for inlining functions and stuff)
return 0, 0
>Omnimaga :)
How exactly would we include this stuff in the actual script, though?
local mods = require("current");
local code = mods.Assemble([[
; put cool Lua ASM code here
]]);
print(code:gsub(".", function(a) return "\\" .. a:byte() end)) -- turn the code into an escape sequence
local code = "" -- copy and paste the output of the previous script here
local func = loadstring(code);
func()
Really nice indeed.
Looking forward to be able to test that project in more complex situations, once I understand how to program in this asm-looking language :P
require("MODS.Current.main");
Which file/folder would we make the example script you showed above in?You could run it in the \src\ folder I believe.
Then, once we do that, which script do we run to convert to bytecode?
-- let's assume this script is in the \src\ folder
local mods = require("current"); -- this runs current.lua, which returns a table of the major API functions
local bytecode = mods.Assemble([[
; put cool bytecode here
]]); -- this passes the multi-line string argument to Assemble(), which turns it into bytecode
-- The easiest way to utilize bytecode in a script is in an escape sequence like so: \104\101\108\108\111 (this is the ASCII sequence "hello")
print(bytecode:gsub(".", function(a) return "\\" .. a:byte() end)) -- this is the bytecode in the above format, which you can pass as a string argument to loadstring()
And, finally, how do we run Lua scripts like that on the computer?You would need the Lua executables:
Any chance you could make some sort of an API/less confusing and nspire-oriented tutorial?
>lua current.lua input.lasm -o output.lua
Which will create a .lua file that will loadstring() an ASCII sequence like I demonstrated above :)The fun part on loadstring is that it return a function, which you will be able to use during the whole script. So if you do everything in the beginning (when you open the script), it should be fine :)