Skip to content
NorscodeNorscode

Language reference

All keywords, types, operators and control structures in Norscode, gathered on one page.

Keywords

WordMeaning
funksjondeclares a function
returnerreturns a value
ladeclares a variable
hvis / visscondition (if)
ellersalternative branch (else)
mensloop with a condition (while)
forloop over a collection
brytbreaks the loop
fortsettjumps to the next round (continue)
prøv / fang / kasterror handling (try / catch / throw)
bruk / somimports a module under a name
og / eller / ikkjelogical operators (and / or / not)
sann / usanntruth values (true / false)

Types

TypeContents
teksta text string
heltallinteger (also written heiltall)
boolsktrue or false
liste_teksta list of texts
liste_heltalla list of integers
ordbok_teksta map from text to text
ordbok_heltalla 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: fortsett jumps to the next round without running the rest of the body. In a mens loop you must therefore increment the counter BEFORE fortsett, 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 dato

A 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
}

See examples