Urgent.News

What's breaking now, across thousands of outlets.

Tech

Let's make a programming language. Functions—Key points

We continue the development of our toy programming language. In this episode, our speaker Yuri demonstrates how to implement functions and explains how functions, scopes, and symbol lookup are built in code using C++. 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…

In our latest episode, Yuri Minaev, a seasoned C++ developer and architect at PVS-Studio, guides us through the implementation of functions in our toy programming language. Using C++, Yuri explains how functions establish their own scope, distinct from the global scope where variables reside. Inside a function, names are not visible outside, while names from parent scopes remain accessible.

Yuri also introduces compound statements, which can create nested scopes. To manage the symbol table and resolve names, the semantic analyzer employs a symbol table that links names with declarators and their respective scopes. Two lookup methods are utilized: an unscoped lookup searches the current scope and ascends through parent scopes until it finds the name, while a scoped lookup checks only the specified scope, preventing duplicate names within the same scope.

The parsing of functions involves the fn keyword, a name, parameters, an optional return type, and a compound statement constituting the body. Parameters consist of a type and a unique name. Parsing occurs in two stages: the function's declaration is parsed and registered in the symbol table before processing its body. This enables functions to reference themselves recursively.

The semantic analyzer ascertains a function's return type from its return statements when no type is specified. It also verifies that return expressions are compatible with the function's return type and inserts implicit casts when necessary. A function devoid of return statements receives the void type. Incompatible return types render the function invalid.

The episode concludes with Yuri acknowledging the completion of functions and introducing the evaluator as the subsequent topic. He invites viewers to follow for more talks and sign up for upcoming webinars, such as "Let's make a programming language. Evaluator." He concludes by expressing hope to see his audience at the webinars and encouraging those interested in learning more about the PVS-Studio analyzer to visit their website.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

While Microsoft Is Busy Learning Rust, I'm Rebuilding .NET for WASI

Apparently Stephen Toub is busy with Rust now, so I've decided to cover his .NET shift. Okay, that's a tongue in cheek obviously.

  • Stephen Toub migrated GitHub Copilot's agent runtime from TypeScript to Rust
  • Author developing independent .NET compiler, CoreLib, and runtime for WASI
  • NetWasm provides native executables without conventional .NET runtime

More from Wednesday 23 September →