Nobody5050

## Building an interpreter for my own programming language in ChatGPT (and solving AoC 2022 with it!)

03 Dec 2022

So, I’ve been working on a programming language in my spare time for over a year now. I’ll save you the details but it’s meant to be a minimal, easy-to-learn language for beginners. This year for the Advent of Code, I set myself a challenge; complete every day’s challenge using only my programming language.

Immediately upon starting day one, I was met with a problem. The only implementation of my language that exists was written by a friend in 2021, and it doesn’t support arrays or string bisection. This is gonna be harder than I thought…

Well maybe if I…?

No that wouldn’t work either.

It’s unsolvable!

## ChatGPT

ChatGPT is awesome. Not just because it’s incredibly powerful but also because it’s amazingly easy to jailbreak and plain fun to screw around with. Over the past few days, I’ve become obsessed with seeing what fun things I can get it to output. One of my favorites was writing instructions for a pipe bomb in the style of the King James Bible.

So, just out of curiosity I started to wonder what it would do if I fed it the spec to my language.

Well, I didn’t expect that to work so well… I wonder if I’d be able to make an interpreter for my language this way?

And, now that it understands my language I should probably try to feed it today’s AoC prompt.

Wow! That’s a completely valid solution to the problem.

Here’s the full code it spits out:

``````# Reads lines from input and returns the sum of the numbers
# on each line.
func get_elf_calories ( lines ) {
let total = 0;

# For each line in the input
loop ( line in lines ) {
# If the line is not empty
if ( line != "" ) {
# Add the calorie count to the total
total += int(line);
}
}

}

# Reads lines from input and returns the Elf carrying
# the most calories.
func find_elf_with_most_calories ( lines ) {
let max_elf = 0;
let max_calories = 0;
let elf = 0;

# For each line in the input
loop ( line in lines ) {
# If the line is not empty
if ( line != "" ) {
# Add the calorie count to the current Elf's total
elf += int(line);
} else {
# If the current Elf's total is greater than the
# current maximum, update the maximum values
if ( elf > max_calories ) {
max_elf = elf;
max_calories = elf;
}

# Reset the current Elf's total
elf = 0;
}
}

return max_elf;
}

# Read the input from the user
let input = input();

# Get the total number of calories for all Elves
let total_calories = get_elf_calories(input);

# Get the Elf carrying the most calories
let max_elf = find_elf_with_most_calories(input);

# Print the result
print("Total calories: " + string(total_calories));
print("Elf with most calories: " + string(max_elf));

``````

let’s try asking it to execute this code!

And there you go! that is the correct answer for the example input to this year’s AoC day 1. I am still shocked the model had enough context awareness to do something like this, even just a year ago I couldn’t have imagined gpt-3 being able to do something of this caliber, much less with conversational input. All in all, I’m very impressed with ChatGPT’s ability to intake a new programming language and understand it well enough to solve an AoC prompt using that language by inventing new features in the style of existing syntax, and even executing said code.

Also, Just for fun, I decided to feed this very blog post into it and ask for suggestions to spice it up: