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

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 (0)