Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Closure Parameters"(?) #2

Open
dan-fritchman opened this issue Apr 14, 2022 · 1 comment
Open

"Closure Parameters"(?) #2

dan-fritchman opened this issue Apr 14, 2022 · 1 comment

Comments

@dan-fritchman
Copy link
Owner

dan-fritchman commented Apr 14, 2022

In breaking news to me, parameters of the form of closure_param below:

.param a=5 b=6
.param closure_param(par1,par2) = 'par1 > par2 ? a : b'
.param calls_that_closure = '12 * closure_param(13, 14)'
  • (a) Are valid syntactically, in each major commercial simulator I have tried
  • (b) Are actively used by major device models

I don't know if they give such a thing a name. Here I will call it a "closure parameter", since it looks very much like a function-call which also captures some of its environment.

It is not yet clear whether nesting these among "regular parameters" like so is supported:

.param a=5 b=6 closure(x,y) = 'x > y ? a : b'
@dan-fritchman
Copy link
Owner Author

dan-fritchman commented Apr 21, 2022

Commit 73b2da2 has handling for this, at least in the first case described above, in which a "closure parameter" has its own .param statement.

The .param-statement parser now does something like:

    def parse_param_statement(self) -> Union[ast.ParamDecls, ast.FunctionDef]:
        """ 
        Parse a `.param` statement, which defines one of: 

        * (a) A set of parameter-declaration `ParamDecl`s, or 
        * (b) A *single* "parameter function" `FunctionDef`. 
        
        Netlist syntax mixing the two, e.g. 
        ```
        .param a=5 b=6 func(x,y) 'x*a +y*b'
        ``` 
        is not supported. 
        """
        self.expect(Tokens.PARAM)

        # Parse the first key-name, so we can see what follows
        _ = self.parse_ident()
        if self.nxt and self.nxt.tp == Tokens.LPAREN:
            # This is a function definition. 
            returnfunc = self.parse_function_def
        else: # Otherwise, parse a set of parameter-declarations
            returnfunc = lambda: ast.ParamDecls(self.parse_param_declarations())
        
        # Either way, push the first ident back on before calling `returnfunc`
        self.rewind()
        # And call the parsing function for either the `ParamDecls` or `FunctionDef`
        return returnfunc()

As noted in that function signature, mixing "value parameters" and "closure parameters" (both made-up names) in the same .param statement, like so:

.param a=5 b=6 func(x,y) 'x*a +y*b'

is not supported.
It remains unclear (to me) whether such syntax is supported by any significant spice-class simulators.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant