rhai icon indicating copy to clipboard operation
rhai copied to clipboard

code generation

Open cn-kali-team opened this issue 4 years ago • 10 comments

Is there a code generation tool? Do I want to batch convert from yaml files to rhai scripts, or can I use AST to code?

cn-kali-team avatar Dec 29 '21 06:12 cn-kali-team

What do you mean by code generation tool?

Usually you just output Rhai script text. There is no need to work with the AST.

If you want to convert YAML to Rhai, you can consider using a handlebars engine to generate scripts based on a template.

schungx avatar Dec 29 '21 08:12 schungx

  • I used yaml as a plug-in before, but it is not flexible, so I want to convert to rhai. I need to convert from yaml to rhai. Thank you for your suggestion. I am trying to use 'handlebars' to write a template to convert yaml to rhai. I hope it will work.
  • like this
requests:
  - method: GET
    path:
      - `${URL}/`

    matchers:
      - opt: contains
        words:
          - "some string"
        part: body
  • convert
fn main(){
    let session = http::session();
    let rep = session.requests("GET",`${URL}/`);
    if rep.body.contains("some string"){
        print("yes");
    }
}
  • I think we need a simple code to generate expressions and functions, which makes it easier to write rhai code.

cn-kali-team avatar Dec 29 '21 09:12 cn-kali-team

https://rhai.rs/playground/stable/ We can convert code to AST, why can't we convert AST to code.

fn main(){
    let session = http::session();
    let rep = session.requests("GET",`${URL}/`);
    if rep.body.contains("some string"){
        print("yes");
    }
}
  • convert AST
//This is the Debug representation of the AST.

// Statements:
[]

// Modules (script-defined functions):
(
    Internal,
    Public,
    "main",
    0,
    ScriptFnDef {
        body: [
            Let(
                FnCall {
                    namespace: http,
                    name: "session",
                    hash: (18378892110909728069==18378892110909728069),
                    args: [],
                } @ 2:25,
                "session" @ 2:9,
                false,
                2:5,
            ),
            Let(
                Dot {
                    lhs: Variable(session, 1) @ 3:15,
                    rhs: FnCall {
                        name: "requests",
                        hash: (7668326916806819885, 25807173721817111),
                        args: [
                            "GET" @ 3:32,
                            InterpolatedString[
                                "" @ 3:38,
                                Stmt[
                                    Expr(
                                        Variable(URL) @ 3:41,
                                    ),
                                ] @ 3:40,
                                "/" @ 3:44,
                            ],
                        ],
                    } @ 3:23,
                } @ 3:22,
                "rep" @ 3:9,
                false,
                3:5,
            ),
            If(
                Dot {
                    lhs: Variable(rep, 1) @ 4:8,
                    rhs: Dot {
                        lhs: Property(body) @ 4:12,
                        rhs: FnCall {
                            name: "contains",
                            hash: (12355910114618825986, 15624829150756018649),
                            args: [
                                "some string" @ 4:26,
                            ],
                        } @ 4:17,
                    } @ 4:16,
                } @ 4:11,
                (
                    [
                        Expr(
                            FnCall {
                                name: "print",
                                hash: (8519014621316573023==8519014621316573023),
                                args: [
                                    "yes" @ 5:15,
                                ],
                            } @ 5:9,
                        ),
                    ] @ 4:40,
                    [],
                ),
                4:5,
            ),
        ] @ 1:10,
        lib: None,
        mods: Imports{},
        name: "main",
        access: Public,
        params: [],
        externals: {
            "URL",
        },
    },
)

cn-kali-team avatar Dec 29 '21 09:12 cn-kali-team

We can convert code to AST, why can't we convert AST to code.

Yes, of course it is possible to convert an AST to code. This is called a code formatter.

A Rhai formatter is a TODO item... so one doesn't exist as yet.

However, what I mean is, you most probably don't need to. I'm quite sure you can find a way to avoid having to build complete Rhai AST's from scratch. It is MUCH easier to generate Rhai script text than to build AST's!

schungx avatar Dec 29 '21 09:12 schungx

Actually, I think handlebars templates are just about perfect for your purposes. They can generate the Rhai scripts from your YAML config easily, provided you have a way to grab the correct template to use based on the YAML settings.

schungx avatar Dec 29 '21 09:12 schungx

Yes, I have already used handlebars to convert yaml to rhai. The conversion of simple yaml works well. Thank you again for your suggestions

cn-kali-team avatar Dec 29 '21 09:12 cn-kali-team

The conversion of simple yaml works well.

In that case I cannot see why you'd need to construct an AST in order to format into Rhai script code.

Can you post a snippet on a really complex/difficult yaml? I may be able to let you know how to get around your difficulties.

schungx avatar Dec 29 '21 10:12 schungx

I converted the complex yaml manually. This complex yaml only accounts for a small part of the total

cn-kali-team avatar Dec 29 '21 10:12 cn-kali-team

https://lupyuen.github.io/articles/rhai

cn-kali-team avatar Dec 30 '21 02:12 cn-kali-team

https://lupyuen.github.io/articles/rhai

Yes, this is a nice project that translates Rhai script code into a runtime on an embedded device. Beware that the AST had some minor changes since then.

schungx avatar Jan 01 '22 09:01 schungx

Closing this for now. Feel free to reopen if you have any issues.

schungx avatar Nov 04 '22 14:11 schungx