This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Varscott11's profile picture

Create a lua interpreter

Started by Varscott11, 22 June 2016 - 05:17 PM
Varscott11 #1
Posted 22 June 2016 - 07:17 PM
I'm currently working on a program called UNET, which is a very complex internet service. When it comes to the servers, I want to make it so that the web designer, can create his/her website using a lua interpreter. This will ease the pain of using lua to create things like the gui, by simply converting the users simplified code to it's lua equivalent. Problem is, I have no idea how to go about doing this. Anyone have any ideas? :)/>
KingofGamesYami #2
Posted 22 June 2016 - 08:06 PM
You want to make a lua IDE? I'm not exactly certain what you're asking for.
Exerro #3
Posted 22 June 2016 - 09:45 PM
Are you aiming to make something like HTML? A language that lets a person describe a GUI, that you then want to convert into Lua code so it draws and updates and stuff?
Emma #4
Posted 22 June 2016 - 10:36 PM
I think what they want to do is to have a Lua interpreter interpret Lua code and generate html&css from that.
Varscott11 #5
Posted 24 June 2016 - 05:22 PM
I want to do exactly what Exerro said. Use it to convert mainly simplified gui code into lua form.
Any help is appreciated :)/>
H4X0RZ #6
Posted 24 June 2016 - 06:36 PM
I want to do exactly what Exerro said. Use it to convert mainly simplified gui code into lua form.
Any help is appreciated :)/>

Instead of writing your own language, you could use the flexibility of Lua to make it work the way you want. I'll give you an example in a minute.
Exerro #7
Posted 25 June 2016 - 01:47 PM
Well, as H4X0RZ said, making some fancy Lua interface is probably better. Lua lets you do some nice syntactical things:


new "button" at (5, 5) {
      text = "Hello!";
}
style "dark" {
      ["background-colour"] = colours.grey;
}

However, if you're set on writing a custom language, you'll probably want to follow these steps:

Lexing - Breaking some input text into a series of 'tokens'. A token is some object in the text, like a word or a number. If you get the lexing right, you won't have to worry about whitespace, and you'll easily be able to parse (the next step) the input.

Parsing - Converting the tokens into an AST. An AST (abstract syntax tree) is a format that represents your language. For HTML, you might want something like:


{ type = "h1", attributes = {}, body = {
       { type = "span", body = "Hello world!" };
       { type = "a", attributes = { href = "some-url" }, body = "a link ooooh" };
} }

Once you've parsed it, it'll be much easier to deal with. You can then get around to code generation. If I were you, I'd make some form of GUI API or use an existing one. Then your code generation can hook into that, for example if the user wants a button at (5, 5), you could just generate the code:


GUIAPI.addButton( 5, 5 )

There are a few GUI APIs these lying around on the forums, which you'd be able to use.
Varscott11 #8
Posted 26 June 2016 - 02:34 AM
Thanks for the reply Exerro! This helped alot. I have begun making some basic commands for the user to use while keeping in mind the parsing method.
Wilma456 #9
Posted 26 June 2016 - 11:39 AM
If you want to make something like HTML, look at this