DEV Community

Cover image for I Built a Programming Language in Bhojpuri from Scratch Using Go
CoderZ90
CoderZ90

Posted on Edited on

I Built a Programming Language in Bhojpuri from Scratch Using Go

Lexer challenges with transliterated scripts

I Built a Programming Language in Bhojpuri from Scratch Using Go

Nobody asked for a Bhojpuri programming language, so I built one.

Meet BiharLang, a dynamically typed, interpreted toy programming language written entirely in Go, with keywords derived from Bhojpuri.

Why I built it

I've always been curious about how programming languages work beneath the syntax we use every day.

I wanted to see how a language parses expressions, applies operator precedence, stores variables and lets functions remember their surrounding scope. Reading about those ideas only got me so far. So I implemented them myself.

Why Bhojpuri? It made the project more fun.

A huge shoutout to the developers behind BhaiLang for the inspiration and to my friends from Bihar who helped me figure out the keywords.

Project structure:

biharlang/
├── cmd/
│   └── bihari/
│       └── main.go                 # CLI entry point (REPL and file runner)
├── lexer/
│   ├── lexer.go                    # Tokenizer implementation
│   └── lexer_test.go               # Lexer unit tests
├── parser/
│   ├── parser.go                   # Pratt parser implementation
│   └── parser_test.go              # Parser unit tests
├── evaluator/
│   ├── evaluator.go                # Tree-walking evaluator
│   ├── evaluator_test.go           # Evaluator unit tests
│   └── environment.go              # Scoped variable environment (closures)
├── ast/
│   └── ast.go                      # Abstract Syntax Tree node definitions
├── object/
│   └── object.go                   # Runtime value types
├── token/
│   └── token.go                    # Token type definitions and keyword lookup
├── repl/
│   └── repl.go                     # Interactive REPL implementation
├── go.mod                          # Go module definition
├── main.bh                         # Example BiharLang script
├── README.md
└── LICENSE
Enter fullscreen mode Exit fullscreen mode

What BiharLang looks like

Here's a simple program:

rakha naam = "Babu"
rakha umar = 25

bol "Ka ho " + naam + "!"
bol umar
Enter fullscreen mode Exit fullscreen mode

Output:

Ka ho Babu!
25
Enter fullscreen mode Exit fullscreen mode

Some of the keywords currently supported are:

BiharLang English equivalent
rakha let / var
bol print
agar if
nata else
jabtak while
kaam function
lauta return
sach true
jhoot false
kuchnahi null

BiharLang also supports functions, recursion and loops. Here's a factorial implementation:

kaam factorial(n) {
    agar (n <= 1) {
        lauta 1
    }

    lauta n * factorial(n - 1)
}

bol factorial(5)
Enter fullscreen mode Exit fullscreen mode

Output:

120
Enter fullscreen mode Exit fullscreen mode

How BiharLang works internally

BiharLang uses a fairly simple interpreter architecture with three major components: a lexer, a parser and an evaluator.

Source Code (.bh)
        |
        v
      Lexer
        |
        v
      Tokens
        |
        v
      Parser
        |
        v
 Abstract Syntax Tree
        |
        v
     Evaluator
        |
        v
   Runtime Values
Enter fullscreen mode Exit fullscreen mode

1. The lexer

The lexer converts raw source code into tokens the parser can understand. Given this BiharLang statement:

rakha x = 10 + 5
Enter fullscreen mode Exit fullscreen mode

It produces tokens that conceptually look like this:

RAKHA
IDENTIFIER(x)
ASSIGN
INTEGER(10)
PLUS
INTEGER(5)
EOF
Enter fullscreen mode Exit fullscreen mode
  • Nothing runs yet. At this stage, the interpreter doesn't evaluate anything
  • The lexer only identifies what each part of the source represents.
  • I implemented it manually in Go, with support for identifiers, keywords, integers, strings and both single-character and multi-character operators.
  • This is also where the Bhojpuri keywords are recognized.

2. The parser

  • Once I had a stream of tokens, I needed to determine how they relate to one another.
  • Why a Pratt parser?
  • It parses expressions according to operator precedence.

Consider this expression:

5 + 3 * 2
Enter fullscreen mode Exit fullscreen mode

Multiplication must happen before addition, so the parser represents the expression as an Abstract Syntax Tree (AST):

       +
      / \
     5   *
        / \
       3   2
Enter fullscreen mode Exit fullscreen mode

The evaluator traverses this tree. The result is 11.

BiharLang currently implements the following precedence levels, from lowest to highest:

  • LOWEST
  • EQUALS
  • LESSGREATER
  • SUM
  • PRODUCT
  • CALL

The Pratt parser also makes it easier to add more operators later.

3. The evaluator

After the parser constructs the AST, the evaluator walks through it recursively. This is where the interpreter runs the program.

For this code:

rakha x = 10 + 5
bol x
Enter fullscreen mode Exit fullscreen mode

The evaluator computes the expression, stores the result in the environment and retrieves it when evaluating the print statement.

Every runtime value in BiharLang implements a common Go interface:

type Object interface {
    Type() ObjectType
    Inspect() string
}
Enter fullscreen mode Exit fullscreen mode

The runtime currently includes integers, strings, booleans, null values, functions, return values and errors.

The language is dynamically typed, so variable types are determined at runtime rather than explicitly declared.

4. Environments and closures

An environment stores variable names and their corresponding runtime values.

Functions create enclosed environments that reference their parent environment, allowing them to access variables from the surrounding lexical scope.

For example:

rakha x = 10

kaam getX() {
    lauta x
}

bol getX()
Enter fullscreen mode Exit fullscreen mode

Output:

10
Enter fullscreen mode Exit fullscreen mode

The function can access x even though it wasn't declared inside the function itself.

Implementing environments gave me a much clearer understanding of lexical scoping and closures than simply using them in other programming languages.

5. The interactive REPL

I also built a REPL (Read-Eval-Print Loop) so users can run BiharLang interactively without creating a script file:

bh repl
Enter fullscreen mode Exit fullscreen mode

To run a .bh file directly:

bh main.bh
Enter fullscreen mode Exit fullscreen mode

The project includes unit tests for the lexer, parser and evaluator. Run them with:

go test ./... -v
Enter fullscreen mode Exit fullscreen mode

What's next?

BiharLang is still a toy language, and plenty is missing. I'd like to add:

  • Arrays
  • Hash maps
  • Logical operators
  • Break and continue statements
  • Built-in functions
  • User input
  • File I/O

For now, it can execute functions, recursion, loops, expressions and conditionals—which is exactly what I set out to build.

Final thoughts

It isn't meant to replace an existing language. It's a learning project I built for fun, and it's open-source under the MIT license.

GitHub: https://github.com/CoderZ90/BiharLang
Try it live: https://helloadi.vercel.app/projects/biharlang
Linkedin: https://lnkd.in/p/gg6JMejZ

#golang #programminglanguages #webassembly #opensource #buildinpublic | Aditya Singh

Nobody asked for a Bhojpuri programming language, so I built one. Meet BiharLang which is a dynamically typed, interpreted toy programming language built from scratch in Go, with keywords derived from Bhojpuri. I've always been curious about what actually happens behind the scenes when we write and execute code. So I decided to build my own programming language while having a little fun with it. Instead of the usual programming keywords, BiharLang uses Bhojpuri: • rakha → let / var • bol → print • kaam → function • jabtak → while • lauta → return • sach / jhoot → true / false And more. Under the hood, I've implemented: • A handwritten lexer • A Pratt parser with operator precedence • Abstract Syntax Trees (AST) • A tree-walking evaluator • Lexical scoping, closures and recursion • An interactive REPL and .bh file execution • Custom error messages with a bit of personality It also runs directly in your browser! - I compiled my existing Go interpreter to WebAssembly, so the browser runs the exact same lexer, parser and evaluator as the CLI. I'll post about how I got it working on the web in my next post :D - Building this helped me understand a lot of things about programming languages that I previously took for granted. - A huge shoutout to the developers behind BhaiLang for the inspiration, and to my friends from Bihar who helped me figure out the Bhojpuri keywords. There's still a lot I'd like to implement, including arrays, hash maps and more built-in functions. The project is open-source. Feel free to try it out, break it, or contribute! Suggestions and feedback are always welcome. I'd love to hear what you think and what features you'd like to see next! Check it out here: Try it live: https://lnkd.in/gwy-f-nK GitHub repo: https://lnkd.in/gCGeVkhc Made with ❤️ for fun & learning. #Golang #ProgrammingLanguages #WebAssembly #OpenSource #BuildInPublic

favicon linkedin.com

Try it, break it or contribute. Suggestions, feedback and bug reports are always welcome! :D

Made with ❤️ for fun & learning.

Resources that helped me

  • BhaiLang — the inspiration behind the fun part of BiharLang.
  • The Super Tiny Compiler — helped me understand some of the concepts behind language implementation.

Top comments (7)

Collapse
 
maxbuildsdev profile image
Max Kaya •

The Pratt setup is the right call, but the lexer is where a localized language usually breaks first, and it's easy to miss while you test with ASCII keyword tables.

rakha, bol, jabtak are Latin transliterations, so a reader who types Devanagari (रखा) gets an "unexpected character" from the identifier check — unicode.IsLetter(r) handles that if you read runes, but Go's utf8.RuneCountInString vs len() also matters for your error columns: len("रखा") is 9, so line:col in errors points at the wrong place for exactly the users this language is for. Same thing applies the day someone writes a string literal with Devanagari in it and gets a column that's three times too far right.

The other one is closures. If environment.Get walks the outer chain but Set always writes into the innermost environment, a recursive factorial that reassigns an outer variable silently creates a fresh inner binding instead of updating it — the Monkey-style tutorial has this bug and returns right answers on most examples. A resolver pass that records the scope depth per identifier at parse time fixes it before it reaches users. Did you hit any of this when you were getting kaam/lauta working, or did the keyword table stay in ASCII the whole way?

Collapse
 
official_fire profile image
CoderZ90 •

Thanks a lot for such detailed feedback! The keyword table stayed ASCII throughout the implementation, as my initial focus was on understanding how interpreters work rather than supporting Devanagari directly.

I hadn't explored Unicode handling or the distinction between declaration and reassignment across nested environments in much depth either. BiharLang started as a fun learning project, but you've given me some really interesting things to explore in the next iteration.

I especially appreciate the explanation about runes and closure scoping. Thanks for taking the time to look into the project!

Collapse
 
nullandvoid_ profile image
Jyanthi •

What a splendid undertaking! Bringing the richness of one's mother tongue into the realm of programming is both ingenious and commendable. It is heartening to see technology embrace linguistic diversity rather than remain confined to convention. A truly admirable work of ingenuity and cultural pride.

Collapse
 
official_fire profile image
CoderZ90 •

Haha, thanks a lot! I had a ton of fun building this project and learned so much along the way. And yes, I totally agree with you! I've always wanted to build my own programming language, or at least a toy one, so I finally decided to give it a shot. It was a really rewarding experience!

Collapse
 
syedfahads profile image
Syed Fahad •

kuchnahi for null is perfect lol. when break lands from your next list, bas feels like the obvious keyword

Collapse
 
robertpaschal profile image
Odinaka Robert Nnamani •

Bhojpuri keywords, same parser problems. the language on the surface changes, the hard part underneath doesn't.

Collapse
 
official_fire profile image
CoderZ90 •

Exactly! The Bhojpuri keywords were the fun part, but implementing the lexer, Pratt parser and evaluator was where the real learning happened. That's precisely what made building BiharLang so interesting for me.