Language engineering

BiharLang

Custom programming language

BiharLang is a dynamically typed language I wrote in Go with Bhojpuri keywords.

Role Independent developer

BiharLang FizzBuzz source alongside its terminal output

Tools and technologies

  • Go
  • Lexer
  • Pratt Parser
  • AST
  • Tree-Walking Interpreter
  • REPL

About BiharLang

BiharLang is a toy programming language I wrote from scratch in Go. It’s dynamically typed and interpreted, with keywords drawn from Bhojpuri. I built it to learn how programming languages work, and for fun. Thanks to my friends from Bihar who helped me pick the keywords.

It has a hand-written lexer, a Pratt parser, and a tree-walking evaluator. It runs .bh files and has an interactive REPL. Functions are first-class values and can be recursive or capture variables from an enclosing scope.

Project structure

Browse the Go packages
Path What it does
cmd/bihari/ Runs script files and starts the REPL
lexer/ Converts source text into tokens
token/ Defines tokens and maps keywords to token types
parser/ Assembles expressions and statements into an AST
ast/ Defines the syntax tree nodes
evaluator/ Evaluates the tree and manages lexical scope
object/ Defines runtime values
repl/ Reads and evaluates input interactively
examples/ Contains sample .bh programs

Installation and usage

These commands run from a checkout of BiharLang. Use the Go version specified in go.mod.

Build the CLI
# Windows
go build -o bh.exe ./cmd/bihari

# Linux or macOS
go build -o bh ./cmd/bihari

Add the binary’s directory to your PATH to use bh from any terminal.

Run a script or start the REPL
go run ./cmd/bihari examples/fizzBuzz.bh
go run ./cmd/bihari

With the compiled binary:

bh examples/fizzBuzz.bh
bh run examples/fizzBuzz.bh
bh repl
bh help

bh compile also runs a file, just like bh run. With no arguments, bh starts the REPL.

Run the tests
go test ./... -v

The lexer, parser, and evaluator each have their own unit tests.

Language reference

Keywords
Keyword Equivalent Purpose
rakha let / var Bind a variable
bol print Print to the console
agar if Start a conditional branch
nata else Provide an alternative branch
jabtak while Repeat while a condition holds
kaam function Define a function
lauta return Return a value
sach true Boolean true
jhoot false Boolean false
kuchnahi null Null value
Operators and delimiters
Syntax Purpose
+ Integer addition or string concatenation
-, *, / Subtraction, multiplication, and integer division
==, != Equality and inequality
<, >, <=, >= Comparisons
= Bind a value, as in rakha x = 5
( ) Group expressions and enclose parameters or arguments
{ } Enclose blocks
, Separate arguments and parameters
Runtime values

Integers, strings, booleans, null, and functions are represented as Go objects. The evaluator also uses objects to carry return values out of function bodies and to report errors. Every object implements the same interface:

type Object interface {
    Type() ObjectType
    Inspect() string
}

Examples

Open an example, edit the code, and hit Run.

Variables and printing

Show output
Ka ho Babu!
25
Conditionals

Show output
Ameer ba
Functions and recursion

Show output
120

Each recursive call binds its own n. The base case returns 1 and the pending calls multiply their results as they return.

While loop

Show output
1
2
3
4
5
Closures

Show output
10

getX resolves x in its defining scope. A function keeps that environment even when it is returned and called later.

Star pattern

Show output
*
* *
* * *
* * * *
* * * * *

Architecture

Three stages from source code to runtime values
  1. LexerSource code becomes a stream of tokens.
  2. Pratt parserTokens become an abstract syntax tree.
  3. Tree-walking evaluatorSyntax tree nodes produce runtime values.

Follow the same line through all three stages:

bol 2 + 3 * 4
1. Lexer · Read the tokens

The lexer reads characters and emits typed tokens. It recognizes identifiers, keywords, and literals, and reads both characters of operators like <= before emitting a token.

BOL("bol")  INT("2")  +  INT("3")  *  INT("4")  EOF
2. Pratt parser · See the syntax tree

The parser uses operator precedence to build the tree. Multiplication binds more tightly than addition, so 3 * 4 becomes the right operand of +.

Print
└── +
    ├── 2
    └── *
        ├── 3
        └── 4
Precedence, low to high Operators
Equality ==, !=
Comparison <, >, <=, >=
Sum +, -
Product *, /
Call ()
3. Evaluator · See the result

The evaluator walks the tree. It computes 3 * 4, adds 2, then the print statement writes 14. When it evaluates a function call, it creates a new environment that points to the scope where the function was defined. That’s how a function can read an outer variable.

Roadmap

Planned features

The roadmap lists user input, arrays, hash maps, logical operators, comments, break and continue, built-in functions, file I/O, string methods, and a Windows installer.

License and inspiration

BiharLang is licensed under the MIT License. Bhai Lang inspired me to make something fun and weird of my own. I also learned from reading The Super Tiny Compiler, a small compiler written in Go.