DEV Community

Cover image for Validating Function Parameters (Pogo Pt:12)
Chig Beef
Chig Beef

Posted on • Updated on

Validating Function Parameters (Pogo Pt:12)

Intro

In this series I am creating a transpiler from Python to Golang called Pogo. In the last post we made the errors so much better, adding line numbers and the function line. In this post we will be fixing functions so that the compiler understands that parameter variables count as declarations.

Current Errors

Here is some sample code.

from GoType import *

def test(x: int):
    print(x)
Enter fullscreen mode Exit fullscreen mode

Compiling this gives us this compiler error.

[Analyzer (analyze-call)] An uninitialized variable was used in a function call
Enter fullscreen mode Exit fullscreen mode

Firstly, lets fix this error a bit.

analyze.go -> analyze:ST_CALL -> line: 4
An uninitialized variable was used in a function call
Enter fullscreen mode Exit fullscreen mode

There we go, that's better. So now that we have an uninitialized variable in a function call, we should initialize it.
This is actually pretty easy, as we have looked at initializing variables in declarations, so a lot of the code can be copy pasted.

else if s.code == structureCode["ST_FUNCTION"] {
    i := 3
    for s.children[i].code == structureCode["IDENTIFIER"] {
        n := s.children[i]   // IDENTIFIER - name
        t := s.children[i+2] // IDENTIFIER - type
        v := Variable{n.text, t.text}
        vars = append(vars, v)

        i += 4 // Next variable, otherwise this will end up on an anti-colon
    }
}
Enter fullscreen mode Exit fullscreen mode

We use i to keep track of the index of the identifier. If i points to an identifier then we can add it to the slice of variables. By incrementing by 4 we end up on the next identifier to repeat the process. If we're one the last parameter then we will end up overshooting, and we land on the anti-colon. This fixes the errors!

Next

I know that we didn't do too much here, I've been a bit busy, but in the next post we will work on variables when calling a function, because at the moment when we try to put anything in a function call it gives us an error.

Top comments (0)