A Quick Introduction to Virgil - Expressions

This is part of a basic introduction to programming in Virgil and gives an overview of the language, its syntax, and its structures. This page focuses on the syntax and nesting rules for expressions.

Terms - Literals and Variables

0 -1 17 2048 -9988  // decimal integers
true false          // booleans 
'a' '0' '\n' '\098' // characters
"An example string" // a string

The simplest expressions in Virgil are called terms, which are expressions that have no nested subexpressions. These are literals, which are used to represent values of primitive types and variables. The code on the right contains examples of integer literals, boolean literals, characters, and a string.

MyVar _foo _bar0  // legal identifiers
0aa $foo @bar     // illegal

In Virgil, variables, such as local variables, parameters and fields are simply referred to by their name. All legal variable names must begin with an underscore or an alphabetic character, followed by zero or more alpha-numeric characters (letters and numbers) or underscores. All variable names are case-sensitive in Virgil; thus MyVar and myvar are distinct variables.

Operators

expr + expr
expr - expr
expr * expr
-expr
(expr)

Virgil expressions can consist of the familiar infix binary operators (+ - / % * & | ^ etc) and prefix unary operators (! ~ -), which are each defined on the appropriate types of data. Each operator has a pre-defined binding precedence; for correct associativity, expressions can be nested inside parentheses ( ... ) in the familiar way. For more detail on the supported operators, see the operator reference.

Conditional Expression

expr ? expr : expr

Virgil supports the ? : conditional operator that should be familiar to C and Java programmers. The conditional operator takes three expressions: the first is the condition to evaluate, which must be of type boolean; the second is the expression to evaluate if the result is true, and the third is the expression to evaluate if the result of the condition is false.

Method Invocation

expr(expr, ...)

This operator is used to call methods in a Virgil program. This operator is covered in more detail in the methods tutorial.

Array and Raw Subscript Operator

expr[expr]

This operator can be used to access individual bits of a raw type or to access the elements of an array. This operator is covered in more detail in the raw types tutorial and arrays tutorial.

New Array Operator

new type[expr]

This operator allocates a new array of a specified type with a specified dimension. This operator is covered in more detail in the arrays tutorial.

New Object Operator

new type(expr, ...)

This operator allocates a new object of a specified type with the specified arguments to the constructor. This operator is covered in more detail in the classes tutorial.

Member Access Operator

expr.member

This operator accesses the member of an object by member name. This operator is covered in more detail in the classes and delegate tutorial pages.

Type Query Operator

expr <: type

This operator dynamically tests whether an object is of a specified type. This operator is covered in more detail in the classes tutorial page.

Type Conversion / Cast Operator

expr :: type

This operator can be used to convert values of primitive types to other primitive types and can be used to dynamically typecast an expression of an object type to another type. This operator is covered in more detail in the classes and primitives tutorial pages.

Go back to the tutorial.