Difficult

From Esolang
Jump to: navigation, search

Difficult is an esoteric programming language that tries to add every characteristic possible into the concept, making it harder to learn. It is created by User:A.

Documentation

Hello World!

Starting with "Hello world". This is very simple.

thing.console.print("Hello, world")?

Here, "console" is a class (that is not needed yet), and "print" is a method. The method "print" prints a string to the console. Note that commands end in question marks. "thing" is also a class that contains everything.

This is the way to input:

thing.console.Input("This is the input prompt. ")?

Note that "Input" is capitalized, to make it harder to program. :-)

Note that only the "print" and "Input" functions have parentheses-enclosed parameters. Other function have brace-enclosed parameters.

Every escape character in C is supported.

Types

Difficult only has one type. That is the "everything" type. This type will auto-detect the value.

Setting variables

You can directly use a variable. It is set to 0.

Use the "=" operator. This sets a variable to a value.

`This is a multi-line
comment`
#This is a comment
#You can set a variable into a string
a="string"?
#Or a number
b=2?
c=3.1?
#The print function supports variable printing.
thing.console.print("*(a) *(b) *(c)")?

Stdout:

string 2 3.1

The *() operator in the string prints a variable instead of a string literal.

If you want to print "*()", then use single quote marks. Single quote marks directly uses the string literal.

console.print('*()')?

You can also set a table.

Numeric table(index starts from 0):

a={1,2,3}?

String-indexed table

a={"one":1,"two":2,"three":3}?

To index:

thing.table.index{a,0}?

To define 2-dimensional tables:

a={{0}}?
thing.table.index{a,0,0}?

Etc. for dimensional arrays more than 2 dimensions.

There is also the len operator, which returns the length of a table.

Other operators

len:table length
+:mathematical plus and string connection operator
-:mathematical minus
*:mathematical multiplication
/:mathematical division
;:mathematical floor division
%:mathematical mod
^:mathematical power
sqrt:mathematical square root
sin:mathematical sine
cos:mathematical cosine
tan:mathematical tangent
abs:mathematical absolute value
max:maximum value
min:minimum value
acos:mathematical acos
asin:mathematical asin
atan:mathematical atan
ceil:mathematical ceil
floor:mathematical floor
log:mathematical log
pi:mathematical pi
random{a,b}:produce a random integer between a and b
exit:quit program
run{a}:run the string a in the standard command-line interpreter.
assert{a}:assert a is true
error{a}:output a in stderr
and:logical and
or:logical or
not:logical not
xor:logical exclusive or
band:bitwise and
bor:bitwise or
bnot:bitwise not
bxor:bitwise exclusive or
nand:logical nand
nor:logical nor
xnor:logical xnor
eq:equal
noteq:not equal
gt:greater than
notgt:less than or equal
ls:less than
notls:greater than or equal
lsh:left shift
rsh:right shift
THESE ARE SET OPERATORS
+= -= *= /= ^= %= ;= max= min= and= or= not= xor= nand= nor= xnor= eq= noteq= ls= notls= gt= notgt= lsh= rsh= ++ --

Control structures

If-else if-else statement

thing.keyword.if condition[
    do_something?
]thing.keyword.elseif condition[
    do_someOtherThing?
]thing.keyword.else[
    else_statement?
]
Shortcut
condition thing.keyword.if do_something thing.keyword.else do_someOtherThing?

This c if a else b statement returns a if c is true. Otherwise, it returns b.

Switch-case statement

thing.keyword.switch a
[
    thing.keyword.wheneq 2
    [
        thing.console.print("a is 2")?
    ]
    thing.keyword.autoMatic
    [
        thing.console.print("a is not 2")?
    ]
]

The switch statement above will not fallthrough. To let it fallthrough, change the "switch" to "fswitch".

Goto statement

Just used for writing Spaghetti code.

Begin:
thing.keyword.goto("Begin")?

Also, it supports BASIC-like gotos:

12 thing.keywords.gol(12)?

Loop constructs

Infinite loop
thing.keywords.infiniteLoop
[
    thing.console.print("This will output forever")?
]
while
condition=true?
thing.keywords.while condition
[
    thing.console.print("this will output forever")?
]
for loop

C-like for loop

thing.keywords.for thing.keywords.start1 condition=true thing.keywords.end1 thing.keywords.start2 condition thing.keywords.end2 thing.keywords.start3 thing.keywords.end3
[
    #Try to avoid using it. This for loop is very wordy.
    thing.console.print("This will also output forever.")?
]

range-based for loops like C++11 are also supported.

thing.keywords.for start thing.keywords.in {1,2,3}
[
    thing.console.print("*(start)")?
]

Stdout:

1
2
3

Also, count controlled loops are supported!

thing.keywords.for i=1 thing.keywords.top 3 #thing.keywords.step 2 : this is optional
[
    thing.console.print("*(i)")?
]

Stdout:

1
2
3
do..while and do..until loop

These are similar to the while loops.

Do while loop:

thing.keyword.do[
    thing.console.print("this will print")?
] thing.keyword.while condition?

The do-until loop is similar, just substitute while as until.

Early exit from loops

Break statement:thing.keyword.toend Continue statement:thing.keyword.tobeg

exception handling

thing.keyword.check[
    #try
    a=0/0?
]thing.keyword.findOut divError[
    #except
    thing.console.print("There is a division error")?
]thing.keyword.last[
    #finally
    print("This must be executed")?
]

Functions

procedures

thing.keywords.function a{}
[
    thing.console.print("Did this")?
]
a{}?

Stdout:

Did this

functions

#a terrible fib function
thing.keywords.function fib{i}
[
    thing.keywords.if (i eq 1) or (i eq 0)
    [
        thing.keywords.return 1?
    ]
    thing.keywords.return fib{i-1}+fib{i-2}?
]

closure example

things.keyword.function add{a}
[
    things.keyword.function clo{b}
    [
        thing.keywords.return a+b?
    ]
    thing.keywords.return clo?
]
thing.console.print("*(add{1}{2})")?

Stdout:

3

Same example using anonymous functions

thing.keyword.function add{a}
[
    thing.keyword.return(
        thing.keyword.function{b}
        [
            return a+b?
        ]
    )?
]
thing.console.print("*(add{1}{2})")?

Objects

thing.keyword.object oriented
[
    thing.keyword.function method{a,b}
    [
        thing.console.print("*(a*b)")?
    ]
]
oriented.method{1,2}?

Stdout:

2

You can even define anonymous objects!

thing.keyword.object class
[
    thing.keyword.object
    [
        thing.keyword.function method{a}
        [
            thing.console.print("*(a)")?
        ]
    ]
]
class..method{1}?

Stdout:

1

Sub-classes:

thing.keyword.class classsub
[
    thing.keyword.function method{a}
    [
        thing.console.print("*(a)")?
    ]
]thing.keyword.from class?

Macros

thing.macro.replace a thing.console.print("A")?
a
A

Dynamic macros

thing.macro.dynamicReplace a
a

stdin:

thing.console.print("hello, world")?

stdout:

hello, world

File inclusion

thing.include.include a.df
#executes a.df before all the code

SKI combinators

I:

thing.ski.directReturn{x}

That is equivalent to x.

K:

thing.ski.ignoreNext{x,y}

That is equivalent to x.

I:

thing.ski.substitute{x,y,z}

that is equivalent to xy(xz).

That is probably enough.

The "thing" library

thing.console.*

print output a string with a newline character
printn output a string without a newline character
printd example: printd{a} is equal to print("*(a)").
printnd example: printnd{a} is equal to printn("*(a)").
Input Input from console with a prompt
fprint print the contents of a file
fout output contents into a file, replacing the original information.
foutn output contents into a file, not replacing the original information.
fInput Input from file
setstdin set the standard input as stdin or a file
setstdout set the standard output as stdout or a file

thing.*

Everything that is in this documentation.thing.ownSource is also one that represents the current source.

thing.time.*

time return an array with numbers representing the year, month, day, hour, minute, and second.
time1990 return a number: the number of seconds since 1990.

Self-modification

thing.mod.modxin(a,"b")?

Change the source code starting from character a (this starts from 0) while printing prompt "b".

Instruction encipherment/decipherment

Add this. This might produce error messages like "command not found".

#using rot13 enscryption

Substitute "rot13" as any encryption algorithm.

Local variables

A local variable can only be used in a specific part of code.

thing.keyword.local a?
[
    #you can only use "a" here
    a=1?
    thing.console.print("*(a)")?
]
thing.keyword.consume
[
    thing.console.print("*(a)")?
]
thing.keyword.findOut undefinedError
[
    thing.console.print("variable a is undefined")?
]

Stdout:

1
variable a is undefined

Stacks

thing.stack.stack a?#define a stack called a
thing.stack.push(a,"e")?#push the string "e" into stack a
thing.console.print("*(thing.stack.top(a)?)")?#prints the top value of the stack
thing.stack.pop(a)?#pops the top value of the stack
#thing.stack.size(a) returns the size of stack a

Queues

thing.queue.queue a?#define a queue called a
thing.queue.push(a,"e")?#push the string "e" into queue a
thing.console.print("*(thing.stack.front(a)?)")?#prints the front value of the stack
thing.stack.pop(a)?#pops the front value of the queue
#thing.queue.size(a) returns the size of queue a

Deques

thing.deque.deque a?#define a deque called a
thing.deque.push(a,"e")?#push the string "e" into deque a in the back
thing.deque.pushf(a,"l")?#push the string "l" into deque a in the front
thing.console.print("*(thing.deque.back(a)?)")?#prints the back value of the stack
thing.console.print("*(thing.deque.front(a)?)")?#prints the front value of the stack
thing.deque.pop(a)?#pops the back value of the deque
thing.deque.popf(a)?#pops the front value of the deque
#thing.deque.size(a) returns the size of deque a

Pointers

#define a variable
v=1?
#define a pointer a
thing.pointer.pointer a?
#Set the pointer v as a's place in memory
a=thing.pointer.place(v)?
#output the place where the variable a is saved in
thing.console.output("*(a)")?
#output the value of a in the memory
thing.console.output("*(thing.pointer.savedValue(a))")?
#output the place where the pointer a is saved
thing.console.output("*(thing.pointer.place(a))")?

User-defined types

#to make a type
thing.types.makeType a[
    #there are two variables in this type
    a,b
    #of course you can modify to make it support arrays and stacks and etc.
]?#<-- there is a question make here
#accessing the variables
thing.types.access(a,a)=12?
thing.types.access(a,b)=13?
thing.console.print("*(thing.types.access(a,a))")#--> 12
thing.console.print("*(thing.types.access(a,b))")#--> 13

Computational class

This is Turing complete, because it is an extension of 3-celled Brainfuck with no-I/O, which was already proved to be Turing complete in the Collatz function page.

The variable a can be any of the three of a, b, and c, depending on the current operating variable.

Conversion
+       a++?
-       a--?
< and > these do not produce code. Instead, they change the currently operating variable.
[       thing.keyword.while a[
]       ]

Unfinished list

  • Graph re-writing
  • Automata rule specification
  • typeclasses
  • stochastic grammars (context-free at the least)
  • qubits