From u1129777 at utah.edu Tue Apr 14 13:41:34 2020 From: u1129777 at utah.edu (TUO ZHAO) Date: Tue, 14 Apr 2020 19:41:34 +0000 Subject: [xsmith-dev] Random Function Name Generate Question Message-ID: Hi all, I'm trying to use XSmith to generate functions with different function names. >From `3.6 Another Small Example With Variables` in the XSmith Doc, I think I need to use: (add-to-grammar .... [Decl #f (name type)] ) But I still don't understand how to `access` this `name` What I want to do, is that I want to generate a program like: fn main() { random_program_name() ; } fn random_program_name() -> givenType { return 73 * 7 ; } Basically it has a function declaration (with a random name and given type) in main and then I could create a function with that random name and return type. Now I just hard code it. I don't actually know how to describe my problem well, so I attached my program and add a `;;TODO` comment next to the lines I'm confusing, hope you could give me some suggestions on how to fix it. Please let me know what can I do Best, Jack -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: rustlang.rkt Type: application/octet-stream Size: 4078 bytes Desc: rustlang.rkt URL: From william at hatch.uno Tue Apr 14 20:39:20 2020 From: william at hatch.uno (William G Hatch) Date: Tue, 14 Apr 2020 20:39:20 -0600 Subject: [xsmith-dev] Random Function Name Generate Question In-Reply-To: References: Message-ID: <20200415023920.GA24424@conspirator> On Tue, Apr 14, 2020 at 07:41:34PM +0000, TUO ZHAO wrote: >Hi all, > >I'm trying to use XSmith to generate functions with different function names. > >From `3.6 Another Small Example With Variables` in the XSmith Doc, I think I need to use: > >(add-to-grammar > .... > [Decl #f (name type)] >) First impression, your `Decl` node should have some kind of right-hand side - eg. an expression or something. There are a few ways you could encode this situation. The basic approach I would recommend is to say the RHS of a Decl is an Expression, and then add some kind of Lambda/Function expression to your language. If your language does not actually have a lambda form, just restrict it so that it can only be generated as the RHS of a Decl. Even if your language has special syntax for defining a function vs some other kind of value (which they generally do), you could just have your printer check whether the RHS of the definition is a function or something else, and print accordingly. >But I still don't understand how to `access` this `name` To be able to get the value of the name field of a node `n`, you can use `(ast-child 'name n)`. To set a value when the node is being generated, you could put something in `fresh`, probably using `fresh-var-name`. But I suggest you simply allow the automatic lifting mechanism to generate definitions when needed. In other words, if you give `Decl` the `binder-info` property from the example you reference, and then give a `Reference` node the `reference-info` property, then when a reference is generated, it can automatically create a valid definition if there isn't already one there for it to use. Also, to have a place where these auto-generated definitions can be put, you'll need to give your program node a field for a list of Decls, eg. ``` [Program Node ([decls : Decl *] ...)] ... ``` An example that would show this more clearly might be Schemely in the xsmith-examples directory. Look at how Definition, Application, and Lambda are defined. Or better yet, you might use the brand-new `add-basic-expressions` and `add-basic-statements` forms that were just added to the `xsmith/canned-components` module to get a quick-start for your language. They are a little under-documented, but they are certainly the quickest way to get a fuzzer going quickly. Does that make sense, and is that helpful? Feel free to follow up.