We are currently working on new rules for what content should and shouldn't be allowed on this website, and are looking for feedback! See Esolang:2026 topicality proposal to view and give feedback on the current draft.

Niflheim

From Esolang
Jump to navigation Jump to search
The topic of this article isn't a place called Niflheim in Norse mythology.

Niflheim is a programming language designed by PSTF.

Niflheim is an imperative language with integers, strings, mutable wells, and functions. It is Turing-complete. It is also, by construction, miserable to program in — not because the syntax is arbitrary, but because five semantic rules interlock so that every natural way of writing a program is subtly wrong.

DE FEM HAARDE PRINCIPPER LOEGMAAL

  1. No boolean values, no logical operations. Odd numbers (like 1, 3, 5, 7) are true, even numbers (like 2, 4, 6, 8, and 0 is also even) are false. Comparison operators return 0 and 1, which is the convention, and under this rule, it works too.
  2. All binary operators are equal and right-associative. Only parentheses can change the priority of operators — what’s inside the parentheses gets calculated first. Example: 2 * 3 + 4 returns the result of adding 3 and 4 first and then multiplying by 2, which is 14, not multiplying 2 and 3 first and then adding 4, which would be 10.
  3. Bindings have frost-value. Every read decrements it. At zero the binding freezes: further reads are free, further writes are silently ignored.
  4. Scoping is mist-scoped: the outermost binding wins. That is to say, when variables with the same name are defined in different scopes, each time you access them, you're actually getting the variable from the outermost scope (usually the global one).
  5. The warmth value represents whether the program keeps running. Each statement will lower the program's warmth by 1. When the program cools down to 0, it will pause immediately and exit with code 0 (you can't distinguish frozen-death and successfully ran simply from exit code). All buffered outputs (like C++'s cout) will be discarded then.

NOEGLEORD

Here are some key keywords.

saga <name> varmi <n>;

This statement is used to start a program. When saved, the program will automatically be saved under the name it specifies. The number after the keyword 'varmi' is the program's warmth. If everything after this keyword is omitted, the default warmth is 1145141919810—because I don't want to turn this introduction into Niflheim Programming Language: From Beginner to Giving Up.

sogur <name>(<params>) { … }

This defines a function. It costs no warmth.

kveda <name> frost <n> = <expr>;

This statement will bind a name-value pair, or in other words, define a well. The number after the keyword frost is the well's initial frost value. From then on, each read will decrease its frost value by 1. When the frost value reaches zero, the well will be read-only.

endurfaeda <name> frost <n> = <expr>;

Same as above, but this is a rebind, and it only works when the well corresponding to the identifier can't be read or written. Otherwise, it will interpret 滚木, in other words, be considered as no-op. No-op doesn't consume any warmth value.

thurka <name>;

This command will completely and permanently unbind the outermost layer. This action cannot be undone.

<name> = <expr>;

Assignment, which costs 1 warmth.

opinn <expr>;

Print in base-12. This is not really encouraged in practical-programming.

telja <expr>;

Print in base-10.

lina;

Print a newline.

kalla <expr>;

Evaluate and discard.

brenna <expr>;

This command can warm up the program. If expr's a string, the program's warmth will increase by its length. If expr's a number, it will increase by the result of the absolute value modulo 24. For example, if you burn "qwertyuiop", the program's warmth will be increased by 10.

skila <expr>;

Return from a function.

bradna;

Flush the output and halt.

ef <expr> { … } annars { … }

If-else.

medan <expr> { … }

While.

proev...fang...ellers...endelig

Try-except-else-finally.

{ … }

A scope.

$$ blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah

This is a single-line comment — the language doesn't have comment blocks. Comments are free — don't say I didn't show mercy to programmers.

"blahblahblah"

Strings are delimited by " and have no escape sequences — a literal newline inside a string is a newline. lina; exists mostly so you don't have to do that.

Each statement executed (including { } blocks and bradna) costs 1.
Each loop iteration (in addition to the statements inside) costs 1.
Each function call except burn costs 3.
Reading a frozen well costs nothing.
If the program errors out and it hasn't been caught yet, the warmth value gets set to zero.

If warmth would drop below zero, the interpreter raises the Freeze condition: execution halts immediately, the output buffer is discarded, and the process exits 0 — the same exit code as success. There is no error message.

Net-positive burns require care, and growing a string in a loop to burn it is the idiomatic way to pay for a long computation. That is the entire economy.

DATALAGRING

3 types only: numbers (unlimited), lists and strings (dword sequences). Index the strings or lists with @, measure them with #.

  • Lists are represented by brackets. Use x[n] to return the n-th element of it -- The list is 1-indexed.
  • Lists are shared by reference, never copied.
  • Indexing uses two different operators for two different types.
  • == on lists is identity, not structure.
  • Indexed assignment does not burn frost — but it respects it.
  • Writing an index through a function parameter Freezes.

A well is a binding with three fields: value, frost counter, frozen flag.

  • kveda x frost 5 = 1; creates a well with 5 frost.
  • Every read decrements frost. Reaching zero sets the frozen flag.
  • A frozen well still reads (for free) — it will never change again.
  • Write on a frozen well is a silent no-op. There is no warning.
  • Command thurka deletes the binding entirely, from whichever scope holds it. This is permanent: leaving the scope does not restore it.

When there are variables with the same name in multiple scopes, the variables in the outer scope are accessed first. This is my habit—I really hate giving variables in different scopes the same name, unless those scopes are completely separated. Consequences: parameters are shadowed by globals of the same name; a kveda inside a block may be completely unreachable; and the only way to reach a more specific binding is to destroy a more general one. The naming discipline this forces ("never reuse a name, ever") is the point.

VIRKA

sogur twice(n) {
    skila n + n;
}

Arguments are passed by name (call-by-name): the argument expression is re-evaluated, in the caller's environment, every single time the parameter is mentioned. If the argument reads wells, each mention burns frost.

kveda a frost 10 = 4;
opinn twice(a);      // the expression `a` is evaluated twice; 2 frost burned

Functions must be defined before they are called — definitions execute as ordinary statements. No closures, no overloading, no defaults. Return without skila yields 0.

SKILGREINING II UUTFAERDHRI BACKUS-NAUR MYND

program   = "saga" IDENT "varmi" NUM ";" stmt* EOF ;

stmt      = "kveda"      IDENT "frost" NUM "=" expr ";"
          | "endurfaeda" IDENT "frost" NUM "=" expr ";"
          | "thurka" IDENT ";"
          | "opinn" expr ";" | "telja" expr ";" | "lina" ";"
          | "kalla" expr ";" | "brenna" expr ";"
          | "skila" expr ";" | "bradna" ";"
          | "ef"     expr block [ "annars" block ]
          | "medan"  expr block
          | "sogur"  IDENT "(" [ IDENT { "," IDENT } ] ")" block
          | block
          | lvalue "=" expr ";" ;

lvalue    = IDENT { "[" expr "]" } ;

block     = "{" stmt* "}" ;

expr      = primary [ binop expr ] ;          (* right-associative *)
primary   = NUM | STR | IDENT | IDENT "(" [ expr { "," expr } ] ")"
          | "#" primary | "-" primary | "(" expr ")"
          | "[" [ expr { "," expr } ] "]"     (* list literal — new *)
          | primary "[" expr "]"              (* element index — new *)
          ;

binop     = "+" | "-" | "*" | "/" | "%" | "<" | ">" | "==" | "@" ;

NUM       = DIGIT { DIGIT } ;                 (* arbitrary precision *)

EIGINLEIKASAFN

  1. Operator precedence is a lie—all symbols are evaluated from right to left. For example, (i mod 15) == 0 is a valid statement, which checks whether i has a remainder of 0 when divided by 15. i mod 15 == 0 is an invalid statement, which tries to first check if 15 equals 0, then uses the result (which is actually false, represented as 0) as the divisor with i as the dividend.
  2. Odd numbers are the truths.
  3. x + x costs 2 frost. Freeze costs 1.
  4. Writes to frozen wells vanish. A loop counter that freezes mid-loop becomes a constant and the loop runs forever — or, rather, until warmth runs out and you get no output at all.
  5. Inner bindings are invisible. A kveda inside a block does nothing useful if any outer scope binds that name.
  6. opinn 10 prints A. Output is duodecimal. Use telja for decimal.
  7. Arguments are re-evaluated. f(x) where f mentions x three times reads x three times.
  8. Freeze is silent. A frozen program produces no output, no error, and exit code 0. The only debugging aid is the optional --thaw flag in the reference interpreter, which is a concession, not part of the language.
  9. Direct assign does not make the new list.
  10. == is identity. telja [1,2] == [1,2]; prints 0. Two literal lists are always different objects.
  11. List argument re-evaluates. f([1,2]) — every mention of the parameter builds a fresh [1,2]. Mutations are lost.
  12. Cannot write through parameters. sogur push(xs) { xs[0] = 1; } Freezes on the assignment — xs is a thunk. Works only because mist scoping makes globals shadow parameters; name a parameter xs while a global xs exists and you mutate the global while silently ignoring the argument.
  13. The frost cascade. This is the canonical Niflheim list bug.
  14. opinn [12] prints [10] (base 12 element formatting). telja [12] prints [12].
  15. Strings and lists don't share an indexer. "abc"[0] and [1,2] @ 0 both Freeze.
  16. brenna xs for warmth is len(xs) mod 16. Concatenating the list to itself in a loop is the standard way to pay for computation — but each + copies, so the warmth gain is linear in work done, not exponential.

DAEMI FORRIT

Hello, World!

saga hello varmi 50;
opinn "Heill, Niflheim!";
lina;
bradna;

Countdown

saga count varmi 2147483648;
kveda i frost 1145141919810 = 5;
medan i > 0 {
  telja i;
  opinn " ";
  i = i - 1;
}
bradna;
// => 5 4 3 2 1

Please pay attention to the explicit comparison in the program. Without this comparison, the countdown will stop at the first even number—after all, in Niflheim, true and false are odd and even, not non-zero and zero.

FizzBuzz

saga fizzbuzz varmi 40000000000000000000;
kveda i frost 40000000000 = 1;
medan i < 100 {
  ef (i % 15) == 0 {
    opinn "FizzBuzz";
  } annars {
    ef (i % 3) == 0 {
      opinn "Fizz";
    } annars {
      ef (i % 5) == 0 {
        opinn "Buzz";
      } annars {
        telja i;
      }
    }
  }
  lina;
  i = i + 1;
}
bradna;

Interpreter

Niflheim/Python Interpreter

Categories