Files and statements
A program is one .nvs file. It starts with the tag <?nvs. The statements after the tag run from
top to bottom. Each statement ends with ;.
A program is a file
A Novis program is one file you run, and the lines written in it are the program.
There is no build step and no starting function to write. You save the file, you run it, and the statements in it happen from the top downwards, in the order you wrote them. echo writes values out, one after another, and puts nothing between them; print does the same for a single value. Classes are different: one can be written anywhere in the file, above or below the lines that use it, because declaring a class is not a step that happens at a moment.
Good to know: nothing is added to what you write out — not even a line break. A program that should end its line says so, by writing the newline itself.
The smallest program
<?nvs// The whole program. Save it as `hello.nvs` and run `nvs run hello.nvs`.// There is nothing else to write: no starting function, no build step.
echo 'Hello, World!', "\n";Hello, World!Write a line piece by piece
<?nvs// Putting a line together out of several pieces.// `echo` takes as many values as you like and writes them one after another,// with nothing in between. Spaces and line breaks are yours to write.
string $name = 'Ada';int $messages = 3;
echo 'Hi ', $name, ', you have ', $messages, ' new messages.', "\n";
// `print` writes a single value, and is handy when there is only one.print "That is all for now.\n";Hi Ada, you have 3 new messages.That is all for now.Print a short report
<?nvs// A small script of the kind you run from a terminal: it prints a short// report and stops. The lines run from the top down, and the class they lean// on is written underneath them, which is allowed.
array<int> $minutes = [35, 12, 90, 5];
echo "Today\n";echo "-----\n";
foreach ($minutes as int $spent) { echo Report::line($spent), "\n";}
echo "-----\n";echo Report::line(Core\Arr::sum($minutes) as int), " in total\n";
class Report{ public static function line(int $minutes): string { return '- ' . ($minutes as string) . ' min'; }}Today------ 35 min- 12 min- 90 min- 5 min------ 142 min in totalHTML around the code
A .nvs file can be a page and a program at once. Text outside the tags is written to the output unchanged, and code between the tags runs.
A file starts in text mode, where every byte is written to the output. <?nvs starts code mode and ?> ends it. Most files start with <?nvs and never close it, so the whole file is code.
<?= $name ?> writes one value at the place where the tag is. A template uses it for most values in a page. You can change mode as often as you like. A { … } block can start in one code section and end in a later one, so a loop or an if can repeat or skip plain HTML.
Good to know: a newline directly after ?> is not written to the output, so a line that ends with a closing tag leaves no empty line. <?php does not compile, and the error message names <?nvs. There is no short <? tag, so a <?xml line in a page stays text.
Put one value into a page
<html><body><?nvs// Everything outside the tags is written out exactly as it stands. This part// is code, and it runs.string $who = 'world';?><p>Hello, <?= $who ?>!</p></body></html><html><body><p>Hello, world!</p></body></html>Wrap plain html in a loop
<?nvs// Shows a loop that starts and ends in code, with plain HTML in between.// The HTML is not written as strings inside the program.array<string> $products = ['pen', 'ink', 'paper'];?><ul><?nvs foreach ($products as string $name) { ?> <li><?= $name ?></li><?nvs } ?></ul><ul> <li>pen</li> <li>ink</li> <li>paper</li></ul>An order confirmation page
<?nvs// A real page: a heading, one row per item, a total, and a different message// when there is nothing to show. The page text stays page text throughout.array<string> $items = ['Fountain pen', 'Ink, blue', 'Paper, 100 sheets'];array<int> $cents = [4900, 1250, 899];
int $total = 0;foreach ($cents as int $price) { $total = $total + $price;}?><h1>Thank you for your order</h1><?nvs if (Core\Arr::count($items) == 0) { ?><p>Your basket was empty.</p><?nvs } else { ?><table><?nvs int $row = 0; foreach ($items as string $name) { ?> <tr><td><?= $name ?></td><td><?= $cents[$row] ?></td></tr><?nvs $row = $row + 1; } ?></table><p>Total: <?= $total ?> cents</p><?nvs } ?><h1>Thank you for your order</h1><table> <tr><td>Fountain pen</td><td>4900</td></tr> <tr><td>Ink, blue</td><td>1250</td></tr> <tr><td>Paper, 100 sheets</td><td>899</td></tr></table><p>Total: 7049 cents</p>Details
<?= $value ?> is short for <?nvs echo $value; ?>. It writes one expression at the place where the tag is. Inside <?= ?>, a ; before ?> is allowed and not needed. Inside <?nvs ?>, each statement needs its ;, also the last one before ?>.
A file whose first line starts with #! is in code mode from its second line. The first line is not written to the output. A <?nvs tag before the first ?> in such a file does not compile (E0009).
In an HTTP request, the output escapes every string you write, so <?= $title ?> cannot add a tag to the page. A Core\Html\Markup value is written as it is. You build one with an html template, and <?= $value ?> inside that template escapes the value in the same way.
How names are written
Novis checks the spelling of every name you write, so the spelling alone tells a reader what kind of thing the name is.
Classes, interfaces, enums, enum cases, type aliases and the parts of a namespace start with a capital letter. Methods, properties, parameters and variables start with a small one. A class constant is written in capitals with an underscore between the words. In every other name, the compiler checks the first letter and that there is no underscore. An abbreviation inside a name keeps its capitals, so HTTPClient and HttpClient are equally fine.
A name spelled the wrong way is an error, not a warning, and there is no setting that turns it off. The message names the spelling to use instead.
Good to know: no name may start with _, and a constructor is spelled constructor. Names are case-sensitive everywhere, so Core\Str and Core\STR are two different names. The last example shows a class name in snake case, which does not compile.
One spelling for each kind of name
<?nvs// One small class using every kind of name there is, each spelled the way// its kind is spelled.
class OrderLine{ public const int MAX_QUANTITY = 10;
public int $quantity = 0;
public function constructor(int $quantity) { $this->quantity = $quantity; }
public function isTooLarge(): bool { return $this->quantity > OrderLine::MAX_QUANTITY; }}
OrderLine $line = new OrderLine(12);
echo 'quantity: ', $line->quantity, "\n";if ($line->isTooLarge()) { echo 'over the limit of ', OrderLine::MAX_QUANTITY, "\n";}quantity: 12over the limit of 10An abbreviation keeps its capitals
<?nvs// The compiler checks the first letter of a name and that it has no underscore.// An abbreviation can keep the capitals people write it with.
class HTTPClient{ public static function parseXMLPayload(string $body): uint { return Core\Str::length($body); }}
class HttpHeader{ public static function forName(string $name): string { return Core\Str::upper($name); }}
// Both spellings of the same abbreviation are accepted, in a class name and// in the middle of a variable name alike.uint $payloadID = HTTPClient::parseXMLPayload("<order id='7'/>");
echo $payloadID, "\n";echo HttpHeader::forName('content-type'), "\n";15CONTENT-TYPEA shipping quote in its own namespace
<?nvs// Shows a typical file that follows the naming rules. The namespace, the class,// the two constants and the variables each use the casing for their kind.// A reader can see what each name is from the way it is written.
namespace App\Billing;
class ShippingQuote{ public const int FREE_OVER_CENTS = 5000; public const int FLAT_RATE_CENTS = 499;
public static function forOrder(int $totalCents): int { if ($totalCents >= ShippingQuote::FREE_OVER_CENTS) { return 0; } return ShippingQuote::FLAT_RATE_CENTS; }}
array<int> $baskets = [1200, 5000, 9900];
foreach ($baskets as int $totalCents) { int $shippingCents = ShippingQuote::forOrder($totalCents); echo 'basket ', $totalCents, ' ships for ', $shippingCents, "\n";}basket 1200 ships for 499basket 5000 ships for 0basket 9900 ships for 0A class name in snake case
<?nvs// Shows that a class name must start with a capital letter, so this program// does not compile. The message gives the name to write instead.// proof: compile-error
class order_line{ public int $quantity = 1;}
order_line $line = new order_line();echo $line->quantity, "\n";error[E0110]: `order_line` must be PascalCase, e.g. `OrderLine` --> 04-a-class-name-in-snake-case.nvs:6:7 |6 | class order_line | ^^^^^^^^^^ class name must be PascalCase = suggestion: rename to `OrderLine`: `OrderLine`
error: aborting due to 1 errorTo read what a code means and how to fix it, run `nvs agent show <code>`.Details
Each kind of name has its own error code. E0110 is for a class, an interface, an enum, an enum case, a type alias or a part of a namespace. Each of these must start with a capital letter. A method name is E0111. A property, a parameter or a variable is E0112. A class constant that is not all capitals and underscores, such as MaxLines or MAX_lines, is E0113. The message shows the name written the right way.
A class constant may contain an underscore. No other name may: $line_count is E0112, and an enum case GOLD_RANK is E0110. An enum case GOLD compiles, because it starts with a capital letter and has no underscore.
A constructor written __construct is E0114.
Every property, method and class constant must say public, protected or private. Without one, the program does not compile (E0122).
Do and don’t
Section titled “Do and don’t”- Do leave the
<?nvstag open in a file that has only code. - Do use
<?= ?>to put a value into a page. It escapes the value for you. - Don’t write
<?php. It does not compile. - Don’t start a name with
_.
