DEV Community

Unicorn Developer
Unicorn Developer

Posted on

Let’s make a programming language. Variables — Key points

We continue the development of our toy programming language that has evolved from handling single expressions to supporting full programs composed of declaration lists. In this episode, we'll explore variable implementation, declarations, and type handling in a simple programming language interpreter/compiler.


About the speaker

Yuri Minaev is an experienced C++ developer, architect at PVS Studio, and a recognized voice in the C++ community who has spoken at CppCast, C++ on Sea, and CppCon. Over the course of ten sessions, he’ll guide you through each stage of building your own programming language.

Parsing and name resolution

Introducing variable names requires more than parsing. The semantic analyzer, or SEMA, manages the symbol table, scopes, and types. The symbol table associates names with variable declarators, allowing the language to resolve references and detect undeclared identifiers and duplicate definitions. Declarations are recorded in the current scope.

Types and implicit conversions

SEMA extracts types from expressions using a visitor. The language has error, void, int, float, boolean, and function types. Functions and void values cannot initialize variables. When an operation combines different types, the language calculates a common type. For example, int and float produce float, so int is implicitly cast. Logical and relational expressions produce boolean results, while arithmetic expressions use their operands' common type.

Building the syntax tree

Implicit casts are added to the syntax tree during construction, so the evaluator will not need to calculate types itself. It can walk the tree and execute the represented operations and conversions. Errors propagate through the tree, and invalid programs are never evaluated.

Want more?

If you want to watch other talks, follow this link.

You can also sign up for our upcoming webinars: Let's make a programming language. Functions. We hope to see you there!

If you'd like to learn more about PVS-Studio analyzer, check out our website.

See ya!

Top comments (0)