dan-fritchman / Netlist

Parsing and generating popular formats of circuit netlist
BSD 3-Clause "New" or "Revised" License
28 stars 1 forks source link

"Closure Parameters"(?) #2

Open dan-fritchman opened 2 years ago

dan-fritchman commented 2 years ago

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)'

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 commented 2 years ago

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) 'xa +yb'



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