LMPTHW ex34 no built-in print function in punypy parser [solved]

I’m working on the analyzer for the puny python interpreter ex34. It’s making sense to me except for one thing right now (setting aside recursion and indent levels).

Maybe I’m implementing functions totally wrong, but my function definition productions have a visitor method which ‘declares’ the function in the world.functions dict. Similarly, The formal arguments for those function definitions are declaring themselves in the world.variables dict.

Unfortunately, the demo code calls a print() function which hasn’t been defined. This is breaking my analyzer. Am I supposed to write a definition for this print function, or should there be another production type for handling the language’s built-in functions?

Here is the demo code I’m analyzing…

script = [
        FuncDef("hello",
                ArgsFormal([
                        ArgF('x'), ArgF('y')
                        ]),
                Body([
                    FuncCall("print",
                            Params([
                                ExprPlus(ExprVariable('x'), ExprVariable('y'))
                                ])
                            )
                    ])),
        FuncCall("hello",
                Params([ExprNumber(10), ExprNumber(20)])
                )
            ]

I figure it’d make most sense to write a definition for it, but that wasn’t in the exercise/demo so I’m asking here.

Actually… maybe this ‘undefined function’ problem is exactly what the analyzer is supposed to catch. So the demo code is not necessarily breaking the analyzer, but the analyzer doesn’t yet have error-handling for undeclared functions.

Ok… trying to rewrite the demo script with a print() funcdef. Realizing now that I my funcdef formal arguments only support a specified number of arguments. But for print() to work with commas, I have to a create formalargs production which can hold an arbitrary number of arguments.

Argh. Solving one problem opens a whole new level of complexity. I get the sense the goalposts are receding into the distance!

Here’s an update to the analyzer test script in regards to the print() function definition:

script = [
        FuncDef("print",
                ArgsFormal([
                        ArgF('printarg'),
                        ]),
                Body([
                   ])
                ),
        FuncDef("hello",
                ArgsFormal([
                        ArgF('x'), ArgF('y')
                        ]),
                Body([
                    FuncCall("print",
                            Params([
                                ExprPlus(ExprVariable('x'), ExprVariable('y'))
                                ])
                            )
                    ])),
        FuncCall("hello",
                Params([ExprNumber(10), ExprNumber(20)])
                )
            ]

This addresses the problem I was having, however now I don’t know how to write the print function so that it will actually work as advertised once I run my punypy interpreter. I guess I’ll cross that bridge in ex35.

If you look in my sample code all I do is pre-load the World with a PrintFunction that implements a FuncDef but just calls Python’s print on its args. You could probably just grep for print in the code and find it, but bug me if you can’t and I’ll post a link to it.