Recursion in pocket-lang

Troy O'Neal
Sep 2, 2018 · 2 min read

I’ve been working on a new programming language project for about the last month. Rather than tell you all about why I’m creating a new language, let me just share a program in my new language, that I just got to compile:

main func
print(factorial 4)
factorial func(x int) int
if x=1
return 1
return x * factorial(x-1)

And the output?

24

As I’ll touch on in future blog posts, pocket works by transpiling to go-lang. The generated go code for the above program is:

package mainimport "fmt"func main() {
fmt.Println(factorial(4))
}
func factorial(x int)int {
if (x==1){
return (1)
}
return ((x*factorial((x-1))))
}

For the morbidly curious, the XASG (transformed abstract syntax graph) for this program is:

TOPLEVEL
[0]->FUNCDEF
NAME->IDENTIFIER: "main"
BODY->IMPERATIVE
[0]->31
ARG->CALL
64->FUNCDEF
IN->PARAM
TYPE->TYPE: int
VARNAME->IDENTIFIER: "x"
55->TYPE: int
29->VARDEF
VARNAME->IDENTIFIER: "x"
SCOPE->VARSCOPE: 2
TYPE->TYPE: int
OUT->TYPE: int
NAME->IDENTIFIER: "factorial"
BODY->IMPERATIVE
[0]->IF
IFTRUE->IMPERATIVE
[0]->74
75->INT: 1
TYPE->TYPE: int
COND->109
TYPE->TYPE: bool
118->VARGET
251->IDENTIFIER: "x"
29->VARDEF (SEEN)
TYPE->TYPE: int
119->INT: 1
TYPE->TYPE: int
[1]->74
75->102
118->VARGET
251->IDENTIFIER: "x"
29->VARDEF (SEEN)
TYPE->TYPE: int
119->CALL
NAME->IDENTIFIER: "factorial"
ARG->101
118->VARGET
251->IDENTIFIER: "x"
29->VARDEF (SEEN)
TYPE->TYPE: int
119->INT: 1
TYPE->TYPE: int
TYPE->TYPE: int
64->FUNCDEF (SEEN)
TYPE->TYPE: int
TYPE->TYPE: int
51->VARTABLE
[0]->VARDEF (SEEN)
TYPE->TYPE: int
NAME->IDENTIFIER: "factorial"
ARG->INT: 4
TYPE->TYPE: int
NAME->IDENTIFIER: "print"
[1]->FUNCDEF (SEEN)
65->66
[0]->FUNCDEF (SEEN)
[1]->FUNCDEF (SEEN)
200000->IDENTIFIER: "fmt"

Although it’s nothing groundbreaking, I thought it was pretty cool to see recursion working in my new language.

Stay tuned for more updates in this series, as I’ll be showing off sneak peeks of what pocket can do.

Here’s a link to the language project:

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade