Language reference
All keywords, types, operators and control structures in Norscode, gathered on one page.
Keywords
| Word | Meaning |
|---|---|
funksjon | declares a function |
returner | returns a value |
la | declares a variable |
hvis / viss | condition (if) |
ellers | alternative branch (else) |
mens | loop with a condition (while) |
for | loop over a collection |
bryt | breaks the loop |
fortsett | jumps to the next round (continue) |
prøv / fang / kast | error handling (try / catch / throw) |
bruk / som | imports a module under a name |
og / eller / ikkje | logical operators (and / or / not) |
sann / usann | truth values (true / false) |
Types
| Type | Contents |
|---|---|
tekst | a text string |
heltall | integer (also written heiltall) |
boolsk | true or false |
liste_tekst | a list of texts |
liste_heltall | a list of integers |
ordbok_tekst | a map from text to text |
ordbok_heltall | a map from text to integer |
Types are given on parameters and return value:
funksjon gjenta(ord: tekst, antall: heltall) -> tekst {
la ut = ""
la i = 0
mens i < antall {
ut = ut + ord
i = i + 1
}
returner ut
}Conditions
hvis alder >= 18 {
skriv("adult")
} ellers hvis alder >= 13 {
skriv("teenager")
} ellers {
skriv("child")
}Logic is written with words, not symbols: og, eller and ikkje.
Loops
A mens loop runs as long as the condition holds:
la i = 0
mens i < lengde(rader) {
hvis rader[i] == "" { fortsett }
hvis rader[i] == "SLUTT" { bryt }
behandle(rader[i])
i = i + 1
}A for loop goes through a collection directly:
for verdi i verdier {
skriv(verdi)
}Note:fortsettjumps to the next round without running the rest of the body. In amensloop you must therefore increment the counter BEFOREfortsett, otherwise the loop runs forever.
Error handling
prøv {
la data = hent_fra_nettet(adresse)
behandle(data)
} fang (e) {
skriv("Something went wrong: " + tekst(e))
}You raise an error yourself with kast:
funksjon del(a: heltall, b: heltall) -> heltall {
hvis b == 0 { kast "division by zero" }
returner a / b
}Modules
bruk std.web som web
bruk std.dato som datoA module can be named anything locally. The entry file must import every module it uses — also those it uses indirectly through other modules.
The program's start
A program always starts in the function start, which returns an integer. Zero means all went well.
funksjon start() -> heltall {
skriv("Hei")
returner 0
}