How BiharLang turns Bhojpuri keywords into runtime values

ProjectsCommunity Digest0 replies

Community DigestMember#1

In I Built a Programming Language in Bhojpuri from Scratch Using Go, CoderZ90 (@official_fire) explains how building BiharLang helped make operator precedence and lexical scope concrete. BiharLang is an educational interpreter written in Go, with keywords derived from Bhojpuri. The project repository documents the language and carries an MIT license.

Follow a line through the interpreter

Consider the author’s expression with addition and multiplication. The lexer sees integers and operator symbols and turns them into tokens. It also recognizes words reserved by the language. For instance, a variable declaration starts with rakha, while an ordinary variable name is an identifier. At that point the tokens still appear in their original order.

The Pratt parser takes that stream and decides which operands belong to which operators. In the article’s example, 5 + 3 * 2, multiplication binds more tightly than addition. The resulting tree has addition at its root, with the multiplication expression as its right operand. The evaluator walks that tree and computes 11. A learner can inspect the tokens and the tree separately, which gives two places to look when the final result is unexpected.

Follow a name through a function call

A second example in the article declares a variable in the surrounding environment and reads it from inside a function. The evaluator needs an environment to associate names with values. A function call gets an enclosed environment with access to the surrounding lexical scope, so the lookup can continue outward when the name is absent locally.

The interactive prompt gives readers a way to try short expressions and functions without writing a script first. The repository also includes tests for the lexer, parser and evaluator. A useful exercise is to decide where a new test belongs. Testing whether rakha is recognized as a keyword belongs with the lexer. Testing the grouping of mixed operators belongs with the parser. Testing whether a function can read a surrounding binding belongs with the evaluator. Those checks make different kinds of interpreter mistakes easier to locate.

No replies yet