LoginLogin
Might make SBS readonly: thread

Lua Code Compiler

Root / Talk About Programs / [.]

Forsaken_AstralDev1Created:
Hello! I want to know how to make a lua code compiler. How would you make a compiler in smilebasic?

Not trying to be condescending, but if you need to ask then it's probably beyond your skill level.

Ok,but I still kind of want do it,not trying to be annoying.

Ok,but I still kind of want do it,not trying to be annoying.
It's impossible. The Lua interpreter is written in C, and in order to interpret Lua code you'd need to compile it. There's no C compiler that targets SmileBASIC, and there never will be because it's not possible. So you're out of luck.

It's not impossible. The original poster never meant "running the original Lua VM in SmileBASIC", but interpreting Lua code instead. The first thing you'd need is to have a tokenizer. There are tools that might help you making one, like actorbug's Lex. After you have a proper tokenizer you can just convert a string into an array of tokens and start reading them and doing stuff to "execute" your program.

Ok,thanks everyone I wanted to do this for a while,also how can I make a theme and a console that runs the lua code? Would you set a chain of input commandes?

Ok,but I still kind of want do it,not trying to be annoying.
It's impossible. The Lua interpreter is written in C, and in order to interpret Lua code you'd need to compile it. There's no C compiler that targets SmileBASIC, and there never will be because it's not possible. So you're out of luck.
Actually, pretty sure someone made a c compiler in SB.

Ok,but I still kind of want do it,not trying to be annoying.
It's impossible. The Lua interpreter is written in C, and in order to interpret Lua code you'd need to compile it. There's no C compiler that targets SmileBASIC, and there never will be because it's not possible. So you're out of luck.
Actually, pretty sure someone made a c compiler in SB.
I've heard of a C struct interpreter and C compiler for a CP/M emulator, but no C-to-SB compiler. Doing so wouldn't be technically possible.

Ok,but I still kind of want do it,not trying to be annoying.
It's impossible. The Lua interpreter is written in C, and in order to interpret Lua code you'd need to compile it. There's no C compiler that targets SmileBASIC, and there never will be because it's not possible. So you're out of luck.
Actually, pretty sure someone made a c compiler in SB.
I've heard of a C struct interpreter and C compiler for a CP/M emulator, but no C-to-SB compiler. Doing so wouldn't be technically possible.

Lua is often embedded into game engines because it small flexible and very easy to embed. The fact that it is written in plain C makes it fairly easy to port. Small however, is relative, and this would be quite big for a SmileBasic program. 837KB of code across 63 files (half are headers so you may not need much from those). I would guess that the hardest thing to port would be structs and pointers, if you can figure out how to handle that, it should be possible to port (I am thinking you may need your own memory allocation system). You also may need to worry about running past the number of lines of code allowed in a file by SmileBasic. Possible but I didn't say fast, or easy. I also wouldn't attempt it if you aren't good at C. If the below looks scary or mystifying, you may want to work on something else or go learn C first.
LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
                       Dyndata *dyd, const char *name, int firstchar) {
  LexState lexstate;
  FuncState funcstate;
  LClosure *cl = luaF_newLclosure(L, 1);  /* create main closure */
  setclLvalue2s(L, L->top, cl);  /* anchor it (to avoid being collected) */
  luaD_inctop(L);
  lexstate.h = luaH_new(L);  /* create table for scanner */
  sethvalue2s(L, L->top, lexstate.h);  /* anchor it */
  luaD_inctop(L);
  funcstate.f = cl->p = luaF_newproto(L);
  luaC_objbarrier(L, cl, cl->p);
  funcstate.f->source = luaS_new(L, name);  /* create and anchor TString */
  luaC_objbarrier(L, funcstate.f, funcstate.f->source);
  lexstate.buff = buff;
  lexstate.dyd = dyd;
  dyd->actvar.n = dyd->gt.n = dyd->label.n = 0;
  luaX_setinput(L, &lexstate, z, funcstate.f->source, firstchar);
  mainfunc(&lexstate, &funcstate);
  lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs);
  /* all scopes should be correctly finished */
  lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0);
  L->top--;  /* remove scanner's table */
  return cl;  /* closure is on the stack, too */
}

Of course this is only going to get you the Lua interpreter, which would be running in another interpreter, so it is going to be slow. If you want to make games, you would also need to port over something like LOVE as well. I assume LOVE would be just as large a project. By the way if you want to look at the code it is at: https://www.lua.org/ftp/. You may need something like 7-zip to open up the tar.gz file. Edit: Sorry may have misread the original question, I thought you wanted to run Lua code on SmileBasic. It sounds like you want to convert LUA code to SmileBasic which is a different problem altogether. In that case, I would worry about how to manage tables the most. Good luck either way.

Hey,I do want run lua code on smilebasic,but I cant read the example you gave me.

" I also wouldn't attempt it if you aren't good at C. If the below looks scary or mystifying, you may want to work on something else or go learn C first". I think he's trying to say that you shouldn't attempt this because you don't understand Lua's complexity.

oh,I'll work on something else,thanks.