WTFCode

From Esolang
Jump to navigation Jump to search

WTFCode is an esoteric programming language created by User:Asicosilomu on the 3rd of Feburary 2023, with the goal of over-complicating the simplest of tasks.

The language only has three symbols: brackets, square brackets and double quotes. It uses instructions for everything, math, comparison, and logic operators are all instructions. This doesn't seem that bad at first, but the messed up syntax will quickly confuse you.

Syntax

  • Each instruction must be on a separate line.
  • When passing arguments to an instruction, you must specify the datatype and, after a space, the value.
  • Arguments are separated by spaces.
  • Numbers are not wrapped in any way.
  • Strings must be enclosed in double quotes, which cannot be escaped.
  • Code for returnvalues must be enclosed in brackets (not square brackets), and any brackets inside the code must be properly opened and closed.
  • if, while and function statements have their conditions/arguments enclosed in square brackets.
  • Instructions and datatypes are case insensitive.

Datatypes

WTFCode has three datatypes: number, string and returnvalue. Syntax for the first two is in the previous section.

returnvalue is a datatype that denotes a value that is the return value of a specific instruction. The value of a returnvalue is enclosed within brackets (see Syntax), and inside it is a single instruction. If the instruction returns a value, that becomes the returnvalue, otherwise, it becomes undefined.

Instructions

JSEVAL

Evaluates a line of JavaScript (that must be able to be put in a return statement) and returns its result.

Syntax:

JSEVAL [CODE]

Returns: any

RETURN

Returns a value. If used in the root of the program, it exits the interpreter and the value is returned outside. If used in a function, the function exits, and the value is returned to the caller.

Syntax:

RETURN [DATATYPE] [VALUE]

Returns: none

VARIABLE

Instruction for managing variables. Has two modes: GET and SET.

VARIABLE GET

Gets the value of the variable [NAME].

Syntax:

VARIABLE GET [NAME]

Returns: any

VARIABLE SET

Sets variable [NAME]'s value to [VALUE], of type [DATATYPE].

Syntax:

VARIABLE SET [DATATYPE] [NAME] [VALUE]

Returns: none

EXECCHILDFUNCTIONJS

Allows you to execute a JavaScript function that you have access to as an object. Complete with the ability to pass parameters. Has a way more humane shorthand of "ecfjs". First argument (/w datatype) is the function object, and, after that, parameters.

Syntax:

ECFJS [DATATYPE] [FUNCTION] [DATATYPE] [VALUE] [DATATYPE] [VALUE] ...

Returns: any

Instruction groups

Instruction groups are groups of instructions that are meant to accomplish the same category of tasks.

Comparison instructions

Instructions which allow you to compare two values.

Equality:

EQ [DATATYPE] [VALUE] [DATATYPE] [VALUE]

if, while and function

IF

Checks if the condition is true and, if so, executes code up until the ESCAPE instruction. ESCAPE is used to close the statement. Condition is wrapped in square brackets.

Syntax:

IF [(CONDITION)]
    # your code here...
ESCAPE

WHILE

Just like the if, except that it will repeatedly execute the code until the condition becomes false. If the condition never becomes false, an infinite loop is formed and freezes your program.

Syntax:

WHILE [(CONDITION)]
    # your code here...
ESCAPE

FUNCTION

To declare a Function, open a new function statement by using the FUNCTION instruction. After a space, type the name of your function (case-insensitive) and, after another space, the arguments between square brackets, under the form [DATATYPE] [NAME] [DATATYPE] [NAME] .... You'll be able to access them in your function as regular variables, by their [NAME]. The statement is closed with the ESCAPE instruction.

Syntax:

FUNCTION MYFUNC [[DATATYPE] [NAME] [DATATYPE] [NAME] ...]
    # your code here...
ESCAPE

You can also not specify arguments, by just typing [], or omitting the brackets altogether. Afterwards, you can call the function exactly like a built-in instruction. As mentioned before, function names are case-insensitive, which means myfunc, mYfUnC and MYFUNC refer to the same function. Declaring myfunc, then MYFUNC, because of this property, MYFUNC will overwrite myfunc.

Syntax:

MYFUNC [DATATYPE] [VALUE] [DATATYPE] [VALUE] ...

Here is an example of a function which takes a number and returns its value times 10:

FUNCTION MULT10 [NUMBER NR]
    RETURN RETURNVALUE (MULT RETURNVALUE (VAR GET NR) NUMBER 10)
ESCAPE

And to call it:

MULT10 NUMBER 69

That returns 690.

Special Datatypes

There are two datatypes exclusive to the Function: any and mode. They may be used when specifying function arguments.

ANY

any indicates that a given argument accepts a value of any type.

MODE

mode allows you to create functions like VAR, that have multiple modes. A mode argument cannot have a datatype in function calls.

The following function uses mode (it needs ostream):

FUNCTION MYFUNC [MODE WAYOFLIFE STRING TEST]
    SHOW INFO RETURNVALUE (VAR GET WAYOFLIFE)
    SHOW INFO RETURNVALUE (VAR GET TEST)
ESCAPE

Calling it:

MYFUNC SHOVE STRING "in ur mom"

That would print "SHOVE", then, on another line, "in ur mom".

Tip! To make your modes case-insensitive, pass them through LOWER before checking them against anything. Don't forget to make the check value all lowercase. Or uppercase, if you use UPPER.

"arguments" array

"arguments" is a special variable that is set by the interpreter at the time your function is called. This array contains all the arguments that your function was called with, but didn't ask for. The name IS case-sensitive.

If you have said function:

FUNCTION MYFUNC [STRING ONE STRING TWO]
 INCLUDE LOCAL/STRING
 RETURN RETURNVALUE (CONCAT RETURNVALUE (VAR GET ONE) STRING " " RETURNVALUE (VAR GET TWO))
ESCAPE

And you call it like this:

MYFUNC STRING "Hello" STRING "World" STRING "Asicosilomu" STRING "is" STRING "the" STRING "best!"

The arguments object would look like:

Array (4) [ "Asicosilomu", "is", "the", "best!" ]

Imports

Modules are snippets of code that you can import into your program to provide extra functionality. Just like #include -ing something in your C++ program. You can import modules from local/, a special directory that contains all of the interpreter's built-in modules, or load them from a remote location!

Syntax:

INCLUDE [PATH]

or

IMPORT [PATH]

Example:

INCLUDE LOCAL/DIALOGS
INCLUDE https://raw.githubusercontent.com/Asicosilomu/files/main/sample.wtf

or

IMPORT LOCAL/DIALOGS
IMPORT https://raw.githubusercontent.com/Asicosilomu/files/main/sample.wtf

Loading remote modules

You can load a remote module by simply not importing from local/. Entering a path that is not part of local/ will cause the interpreter to load the module remotely. If you specify a relative path, it will be loaded correctly, so a script running on https://asicosilomu.com loading modules/sample.wtf would lead to the importing of https://asicosilomu.com/modules/sample.wtf.

For example, if you have a file at https://asicosilomu.com/sample.wtf which looks like this:

# Sample WTFCode module

function greet [string t]
  include local/ostream
  include local/string
  show log returnvalue (concat string "Hello, " returnvalue (var get t) string "!")
escape

And you load it like this...

include https://asicosilomu.com/sample.wtf

Then call the function...

greet string "Asicosilomu"

Your script would show this:

Hello, Asicosilomu!

Warning! When including a remote module, your program halts until the server responds or the request times out.

Built-in modules

Here are the modules that come with the latest version of the interpreter. Keep in mind, modules are written 100% in WTFCode!

DIALOGS

The dialogs module can be used to show pop-ups, using the alert, confirm and prompt JavaScript functions. Those are nicely integrated as WTFCode instructions when you import dialogs. Paths are case-insensitive, like everything else.

Importing:

INCLUDE LOCAL/DIALOGS

or

IMPORT LOCAL/DIALOGS

Example:

INCLUDE LOCAL/DIALOGS
VAR SET RETURNVALUE NAME (PROMPT STRING "What's your name?" STRING "Asicosilomu")
ALERT RETURNVALUE (CONCAT STRING "OK. Your name is " RETURNVALUE (VAR GET NAME) STRING ".")

This example will ask you for your name, then show it back to you.

COMPARISON

Since the migration of most instructions from native JS to WTFCode modules, comparison provides comparison instructions.

Importing:

INCLUDE LOCAL/COMPARISON

or

IMPORT LOCAL/COMPARISON
Instructions

Greater than:

GREAT [DATATYPE] [VALUE] [DATATYPE] [VALUE]

Less than:

LESS [DATATYPE] [VALUE] [DATATYPE] [VALUE]

Greater than or equal to:

GREATEQ [DATATYPE] [VALUE] [DATATYPE] [VALUE]

Less than or equal to:

LESSEQ [DATATYPE] [VALUE] [DATATYPE] [VALUE]

LOGIC

Since the migration of most instructions from native JS to WTFCode modules, logic provides logic operators.

Importing:

INCLUDE LOCAL/LOGIC

or

IMPORT LOCAL/LOGIC
Instructions

Invert the value of a boolean:

NOT [DATATYPE] [VALUE] [DATATYPE] [VALUE]

Check if two or more values are true:

AND [DATATYPE] [VALUE] [DATATYPE] [VALUE] ...

Check if one of two or more values is true:

OR [DATATYPE] [VALUE] [DATATYPE] [VALUE] ...

STRING

Since the migration of most instructions from native JS to WTFCode modules, string provides string manipulation functions.

Importing:

INCLUDE LOCAL/STRING

or

IMPORT LOCAL/STRING
Instructions
STRING

Converts a value to a string.

Syntax:

STRING [DATATYPE] [VALUE]

Returns: string

UPPER

Changes a string to all uppercase.

Syntax:

UPPER [DATATYPE] [VALUE]

Returns: string

LOWER

Changes a string to all lowercase.

Syntax:

LOWER [DATATYPE] [VALUE]

Returns: string

CONCAT

Concatenates two one or more values as a string.

Syntax:

CONCAT [DATATYPE] [VALUE] [DATATYPE] [VALUE] ...

Returns: string

MATH

Since the migration of most instructions from native JS to WTFCode modules, math provides mathematical operators.

Importing:

INCLUDE LOCAL/MATH

or

IMPORT LOCAL/MATH
Instructions
FLOOR

Shaves the decimal points off a number.

Syntax:

FLOOR [DATATYPE] [VALUE]

Returns: number

Chain addition:

CHAINADD [DATATYPE] [VALUE] [DATATYPE] [VALUE] ...

Chain subtraction:

CHAINSUB [DATATYPE] [VALUE] [DATATYPE] [VALUE] ...

Chain multiplication:

CHAINMULT [DATATYPE] [VALUE] [DATATYPE] [VALUE] ...

Addition:

ADD [DATATYPE] [VALUE] [DATATYPE] [VALUE]

Subtraction:

SUB [DATATYPE] [VALUE] [DATATYPE] [VALUE]

Multiplication:

MULT [DATATYPE] [VALUE] [DATATYPE] [VALUE]

Division:

DIV [DATATYPE] [VALUE] [DATATYPE] [VALUE]

Modulus:

MOD [DATATYPE] [VALUE] [DATATYPE] [VALUE]

RANDOM

Since the migration of most instructions from native JS to WTFCode modules, random provides random numbers.

Importing:

INCLUDE LOCAL/RANDOM

or

IMPORT LOCAL/RANDOM
Instructions
RANDOM

Returns a random number in a specified range. The first argument is the minimum number, and the second is the maximum.

Syntax:

RANDOM [DATATYPE] [VALUE] [DATATYPE] [VALUE]

Returns: number

DOM

Since the migration of most instructions from native JS to WTFCode modules, dom provides DOM management functions.

Importing:

INCLUDE LOCAL/DOM

or

IMPORT LOCAL/DOM
Instructions
DOM CREATE

Returns a new HTML element with tagname [NAME]. [NAME] is a value with a datatype, which must be specified.

Syntax:

DOM CREATE [DATATYPE] [NAME]

Returns: object

DOM QUERY

Returns the first child of [ELEMENT] that matches the selector [SELECTOR].

Syntax:

DOM QUERY [DATATYPE] [ELEMENT] [DATATYPE] [SELECTOR]

Returns: object

DOM QUERYALL

Returns a NodeList that contains all of [ELEMENT]'s children that match the selector [SELECTOR].

Syntax:

DOM QUERYALL [DATATYPE] [ELEMENT] [DATATYPE] [SELECTOR]

Returns: NodeList

DOM APPEND

Append [ELEMENT] as a child of [PARENT].

Syntax:

DOM APPEND [DATATYPE] [PARENT] [DATATYPE] [ELEMENT]

Returns: none

DOM CLONE

Create a clone of [ELEMENT].

Syntax:

DOM CLONE [DATATYPE] [ELEMENT]

Returns: object

PROPERTY

Since the migration of most instructions from native JS to WTFCode modules, property provides property management functions.

Importing:

INCLUDE LOCAL/PROPERTY

or

IMPORT LOCAL/PROPERTY
Instructions
PROPERTY GET

Returns the value of [OBJECT]'s [NAME] property.

Syntax:

PROPERTY GET [DATATYPE] [OBJECT] [DATATYPE] [NAME]

Returns: none

PROPERTY SET

Sets the value of [OBJECT]'s [NAME] property to [VALUE]. Equivalent of [OBJECT].[NAME] = [VALUE].

Syntax:

PROPERTY SET [DATATYPE] [OBJECT] [DATATYPE] [NAME] [DATATYPE] [VALUE]

Returns: any

ARRAY

Since the migration of most instructions from native JS to WTFCode modules, array provides array management functions.

Importing:

INCLUDE LOCAL/ARRAY

or

IMPORT LOCAL/ARRAY
Instructions
ARRAY GET

Returns the value of [ARRAY]'s index [INDEX] entry.

Syntax:

ARRAY GET [DATATYPE] [ARRAY] [DATATYPE] [INDEX]

Returns: any

ARRAY SET

Sets the value of [ARRAY]'s index [INDEX] entry to [VALUE].

Syntax:

ARRAY GET [DATATYPE] [ARRAY] [DATATYPE] [INDEX] [DATATYPE] [VALUE]

Returns: any

ARRAY CREATE

Creates a new, empty array.

Syntax:

ARRAY CREATE

Returns: array

ARRAY LENGTH

Returns the length of [ARRAY].

Syntax:

ARRAY LENGTH [DATATYPE] [ARRAY]

Returns: number

OSTREAM

Since the migration of most instructions from native JS to WTFCode modules, ostream provides string output capabilities.

Importing:

INCLUDE LOCAL/OSTREAM

or

IMPORT LOCAL/OSTREAM
Instructions
SHOW

Prints the [STRING](s) to the console, with log level [LOGLEVEL].

Syntax:

SHOW [LOGLEVEL] [DATATYPE] [STRING] [DATATYPE] [STRING] ...

Returns: none

EXCEPTION

Since the migration of most instructions from native JS to WTFCode modules, exception provides an error gun.

Importing:

INCLUDE LOCAL/EXCEPTION

or

IMPORT LOCAL/EXCEPTION
Instructions
THROW

Throws an exception, with text [ERROR].

Syntax:

THROW [DATATYPE] [ERROR]

Returns: none

NOP

Since the migration of most instructions from native JS to WTFCode modules, nop provides nothing.

Importing:

INCLUDE LOCAL/NOP

or

IMPORT LOCAL/NOP
Instructions
NOP

Does nothing.

Syntax:

NOP

Returns: none

Examples

Hello World

include local/ostream
show log string "Hello, World!"

99 Bottles of Beer

include local/comparison
include local/math
include local/string
include local/ostream

var set number beer 99
while [great returnvalue (var get beer) number 0]
    if [great returnvalue (var get beer) number 2]
        var set returnvalue prefix (concat returnvalue (var get beer) string " bottles of beer on the wall, " returnvalue (var get beer) string " bottles of beer.")
        var set returnvalue suffix (concat returnvalue (sub returnvalue (var get beer) number 1) string " bottles of beer on the wall.")
    escape
    if [eq returnvalue (var get beer) number 2]
        var set string prefix "2 bottles of beer on the wall, 2 bottles of beer."
        var set string suffix "1 bottle of beer on the wall."
    escape
    if [eq returnvalue (var get beer) number 1]
        var set string prefix "1 bottle of beer on the wall, 1 bottle of beer."
        var set string suffix "no more beer on the wall!"
    escape
    show log returnvalue (concat returnvalue (var get prefix) string " Take one down, pass it around, " returnvalue (var get suffix))
    var set returnvalue beer (sub returnvalue (var get beer) number 1)
escape

Username Generator

include local/random
include local/comparison
include local/string

var set returnvalue iadjective (random number 1 number 5)
var set returnvalue inoun (random number 1 number 5)
if [eq returnvalue (var get iadjective) number 1]
  var set string adjective "Optimistic"
escape
if [eq returnvalue (var get iadjective) number 2]
  var set string adjective "Amazing"
escape
if [eq returnvalue (var get iadjective) number 3]
  var set string adjective "Tactical"
escape
if [eq returnvalue (var get iadjective) number 4]
  var set string adjective "Generous"
escape
if [eq returnvalue (var get iadjective) number 5]
  var set string adjective "Interesting"
escape
if [eq returnvalue (var get inoun) number 1]
  var set string noun "Potato"
escape
if [eq returnvalue (var get inoun) number 2]
  var set string noun "Fisher"
escape
if [eq returnvalue (var get inoun) number 3]
  var set string noun "Kitten"
escape
if [eq returnvalue (var get inoun) number 4]
  var set string noun "Debugger"
escape
if [eq returnvalue (var get inoun) number 5]
  var set string noun "Player"
escape
return returnvalue (concat returnvalue (var get adjective) returnvalue (var get noun))

Polish Cheetos

include local/dialogs
include local/math
include local/comparison
include local/string
include local/exception

alert string "You ran out of cheetos! Let's buy some!"
var set returnvalue price (prompt string "How much is a bag of cheetos (polish złoty)? the price probably rised since last time" string "22")
var set returnvalue budget (prompt string "How much polish złoty do you have?" string "66")
var set returnvalue cheetos (prompt string "how much cheeto u want?" string "3")
var set returnvalue dif (sub returnvalue (var get budget) returnvalue (mult returnvalue (var get cheetos) returnvalue (var get price)))

if [greateq returnvalue (var get dif) number 0]
	alert returnvalue (concat string "you bought " returnvalue (var get cheetos) string " cheetos. yay! You have " returnvalue (var get dif) string " polish złoty left.")
	return string "what a success"
escape

alert string "you dont have enough polish złoty go work you poor idiot"
throw string "User too poor."

Quine

include local/string
var set returnvalue input (jseval `return returnvalue (concat returnvalue (jseval "include local/string" + String.fromCharCode(10) + "var set returnvalue input ") returnvalue (jseval String.fromCharCode(40)) string "jseval " returnvalue (jseval String.fromCharCode(96)) returnvalue (var get input) returnvalue (jseval String.fromCharCode(96)) returnvalue (jseval String.fromCharCode(41)) returnvalue (jseval String.fromCharCode(10)) returnvalue (var get input))`)
return returnvalue (concat returnvalue (jseval "include local/string" + String.fromCharCode(10) + "var set returnvalue input ") returnvalue (jseval String.fromCharCode(40)) string "jseval " returnvalue (jseval String.fromCharCode(96)) returnvalue (var get input) returnvalue (jseval String.fromCharCode(96)) returnvalue (jseval String.fromCharCode(41)) returnvalue (jseval String.fromCharCode(10)) returnvalue (var get input))

Interpreter

The WTFCode interpreter allows you to use WTFCode anywhere in your HTML document, if you wanted to, for some reason...

Simply follow the instructions on the GitHub page for loading it, then you can put WTFCode in script tags like any other code. Like this:

<script type="text/wtfcode">
  include local/ostream
  show log string "Hello, World!"
</script>