Skip to content

first_project

BlidDev edited this page Jan 22, 2024 · 3 revisions

Overview:

Niv supports both single-script projects and multi-scipts ones. This chapter will walk you through how to create and manage both.


Single script projects

Say we have this script we want to run:

// hello_world.niv
#MAIN |
    print "Hello, Niv!\n"
#MAIN

The simplest way to run it would be:

cargo run -- -f path/to/hello_world.niv
or
./niv -f path/to/hello_world.niv

The -f parameter is short for --file-list, which will be explained later.

This would run our script and print Hello, Niv! to the screen.


Multi script projects

There are two ways to structure a mutlifile porject in Niv, the first being a simple file-list and the second being a project-file.

For explaining it better, we will use two demo files:

// helper.niv

#helper_func | 
    print "Hello from helper_func!\n"
#helper_func
// main.niv

#MAIN | 
    helper_func
#MAIN

File list

As shown before, the simplest way to run a multi-script project is using a file-list: the files get loaded into the buffer according to the order the list was entered. So, the line

cargo run -- -f path/to/helper.niv path/to/main.niv
or
./niv -f path/to/helper.niv path/to/main.niv

would run the program as expected.


Project file

While a file-list is the most straightforward way to acheive this goal, it can get tidious to enter the entire list every time you want to run the project.

The solution to that would be a project-file like so:

# project.prj
helper.niv
main.niv
# or even another project file:
# ../libs/mathlib/mathlib.prj

that would be called like so:

cargo run -- -p path/to/project.prj
./niv -p path/to/project.prj

A project file does exactly what a file-list does, only it's in a single neat file instead of having to expand on the "compiliation" line every time a new file is added.

Clone this wiki locally