Fundamentals 8 min read

Exploring Virgil: A Lightweight High‑Performance Systems Language with Cross‑Compilation

This article introduces the Virgil programming language, highlighting its goal as a lightweight high‑performance system language with powerful cross‑compilation, comparing it to Zig and Rust, and provides step‑by‑step installation, code examples, algebraic data types, and pointer usage for developers.

21CTO
21CTO
21CTO
Exploring Virgil: A Lightweight High‑Performance Systems Language with Cross‑Compilation

Virgil is a new systems programming language aimed at lightweight high‑performance applications, featuring a strong cross‑compiler. Its creator Ben Titzer, also a co‑creator of WebAssembly, says he wants Virgil to be an excellent system language that removes legacy garbage while enabling robust code for virtual machines, compilers, kernels, network stacks, and more.

The language offers classes, functions, and type parameters, giving it a modern feel. Compared to Zig and Rust, Virgil follows the trend of targeting lightweight high‑performance systems.

Getting Started with Virgil

Clone the repository using the provided Git command and add the virgil/bin directory to your PATH:

export PATH=$PATH:~/Projects/TheNewStack/virgil/virgil/bin

Run a simple "Hello World" program:

def main() {</code><code>    System.puts("Hello World!
");</code><code>}

Execute it with the built‑in interpreter:

To compile, Virgil can detect the host and target architecture, producing a binary or a JAR for cross‑compilation.

Example of cross‑compiling to a JAR:

Algebraic Data Types in Virgil

Virgil supports algebraic data types (ADTs), combining polymorphism, switches, and enums to create complex structures without objects.

type Travel {</code><code>    case Walk {</code><code>        def distance(hoursTravelled: int) -> int {</code><code>            return hoursTravelled * 3;</code><code>        }</code><code>    }</code><code>    case Cycle {</code><code>        def distance(hoursTravelled: int) -> int {</code><code>            return hoursTravelled * 16;</code><code>        }</code><code>    }</code><code>    case Drive (speed: int) {</code><code>        def distance(hoursTravelled: int) -> int {</code><code>            return speed * hoursTravelled;</code><code>        }</code><code>    }</code><code>    def distance(hoursTravelled: int) -> int; // top‑level method</code><code>}</code><code>def main() {</code><code>    var walking = Travel.Walk;</code><code>    var busTrip = Travel.Drive(50);</code><code>    var cycling = Travel.Cycle;</code><code>    var totalDistance = walking.distance(1) + busTrip.distance(2) + cycling.distance(1);</code><code>    System.puts("Today, We covered a distance of ");</code><code>    System.puti(totalDistance);</code><code>    System.puts(" miles in 4 hours.n");</code><code>}

ADTs are immutable structures with top‑level methods and support deep equality comparison.

Pointers in Virgil

Virgil provides raw, untyped pointers for low‑level control, similar to C pointers. They can be loaded and stored unsafely:

var p: Pointer;</code><code>var x: int = p.load<int>(); // load an int</code><code>p.store<int>(33); // store an int</code><code>var y: string = p.load<string>(); // unchecked, dangerous

Using pointers gives developers direct memory access, which can lead to hard‑to‑track errors.

Conclusion

Although Virgil is still young, it already demonstrates strong practicality. It supports both object‑oriented and functional styles, making it a multi‑paradigm language suitable for low‑level development while remaining lightweight.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

pointersSystems Programmingcross-compilationalgebraic data typeslanguage tutorialVirgil
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.