W (Viktor T. Toth)

From Esolang
Jump to navigation Jump to search

W is a compiled low-level programming language created by Viktor T. Toth in or before 2001. The goal of the language is to have a compiler that can be run on DOS 3 computers with a small amount of memory. The compiler host and target platform are both DOS x86 real mode with only a single segment used, so address space is limited to 64 kilobytes. The compiler is self-hosting implemented in W, though there is a (likely unpublished) C version as well.

The language is low-level, somewhat like ancient and very simplified C, but it only uses one type, unsigned 16-bit integers that are also used as pointers. You can build arithmetic expressions from this using the binary operators + - << >> & * / % | + - < <= == > >= != && || [] () = as well as unary - ~ ! # @ and parenthesis. The operator # takes address like the & operator in C, while @ dereferences a pointer to its pointed 16-bit integer. The operators && and || are not short-circuiting; they and comparison operators don't always output 0 or 1.

The control structures of the W language have somewhat unusual syntax. Statements are written one after another with no separator punctuation, just like Lua allows, but there is not even an optional separator that you can use: in fact ; starts a comment. As a result, statements can't start with an opening parenthesis for that would be ambiguous with a function call. A braced blocks of statements { } can be used as an expression, with the last statement giving the return value. Conditional expressions x ? y , z or x ? y are available. Loops are written using the $ pseudo-variable which represents the instruction pointer: you save it to a temporary variable at the start of a loop and assign to it with the = operator when you want to jump back.

Global and local variables, declared with :=, can be scalars as well as arrays. Such arrays are initialized with a comma-separated list of 16-bit integer constants, and this can be used not only to store data but also machine code functions that would be hard to express directly in W. You can also declare functions with argument lists and call them with the () operator.

The compiler generates straightforward code with not many optimizations, but surprisingly does constant folding. Expressions are evaluated with a stack stored on the x86 stack, the top element in AX instead. Interestingly, reading the $ pseudo-variable is compiled to using the CALL instruction to get the instruction pointer, rather than using the known address as a constant.