O

From Esolang
Jump to: navigation, search

O, previously name Fifth, is a stack-based language based off Forth and GolfScript. It is read character by character like Fish, Befunge, and Refract, but it is only one dimensional. It was made in July, 2015, by User:Phase. You can find a more up-to-date version of the language on ReadTheDocs or try it out online.

O
Paradigm(s) stack-oriented, imperative, concatenative
Designed by User:Phase
Appeared in 2015
Dimensions one-dimensional
Computational class Turing complete?
Major implementations C, Java
Influenced by Forth, GolfScript, refract
File extension(s) .ogc


Syntax

Code is read character by character, with each one doing different things. Spaces are not needed unless you set it to a variable or codeblock.

Examples

Hello World

"Hello, World!"o

Factorial

[i#,;]*o

Gets input from stdin and outputs to stdout. Explanation:

[        Start making an array
 i#      Store the input from the user to an array
   ,;    Store list from 1 to input in the array
     ]   Push the array to the stack
      *o Output all the contents of the array multiplied together

Cat

io

Function definition

{.*}:s

This squares the top of the stack.

Repeat a string

"ha"5*4{+}o

Prints "hahahahaha"

The sum of the cubes of a list from 1 to n

[i#,]3^+o

This takes a list from 0 to n (n being the input) and squares each value, then adds them together.

The Stack

You can push & pop things from the stack, as well as manipulate the items on it,

Pushing

Strings

To push a string, you use quote marks.

"Hello, World!"
Characters

Characters are stored as strings on the stack, so you can manipulate them the same way. Here's how you push a single character:

'h o

This prints h as if it were a string (because it is).

Numbers

Numbers are written in base 36, from 0 to Z. This pushes 2, 3, and 36. They are saved in the stack as doubles.

23Z

Together, now

'H"ello, World!"2345

Stack contents:

  • 5
  • 4
  • 3
  • 2
  • ello, World!
  • H

Popping

; is used to pop the top value off the stack.

23;4

Stack contents:

  • 4
  • 2

Stack manipulation

  • . clones the top of the stack
  • \ swaps the top to values on the stack
  • @ moves the third value on the stack to the top
  • r reverses the stack
  • l pushes the length of the stack to the stack
  • o pops the top value and prints it
  • i gets input from stdin and pushes it as a string

String manipulation

+-*/% all do normal math for numbers, but they do special things for strings.

For the next examples, we'll assume a and b are the two objects on the top of the stack, b on top of a, like:

  • b
  • a

+

If a or b is a string, concatenate.

5"Test"+o

Prints 5.0Test, as numbers are stored as doubles.

-

If a or b is a string, removeAll.

"Hello&World&Hi" '& -o

Prints Hello World Hi

*

If a xor b is a string, duplicate.

"He"5*

This pushes He to the stack 5 times, so the stack is:

  • He
  • He
  • He
  • He
  • He

5"He"* does the same thing.

/

If a or b is a string, split.

"A_B_C_D_E_F" '_ /

This splits the string and pushes each substring in reverse order (or normal depending on how you're viewing it):

  • F
  • E
  • D
  • C
  • B
  • A

%

String replacement, only works if the top two elements are strings.

"This sucks" "sucks" "rocks" % o

Prints This rocks.

#

This only takes in one argument, called s. It tries to parse s as a double and pushes it to the stack. If it's not a double, it returns the hashcode of the string.

"5.01"#

Pushes 5.01 to the stack as a double.

"abc"#

Pushes the hascode of abc.

~

~ evaluates the string as code.

"55+"~

This parses each character in the string and pushes 10 to the stack.

Boolean logic

There are many logic operations that can be used on strings and numbers. If one object is a number and the other is a string, the number will be casted to a string and compared like strings.

=

Pushes 1 if the two items are equal.

22=o

Prints 1

"2" "2" =o

Prints 1

2 "2" = o

Prints 1

2 "1" =o

Prints 0

<

Numbers

Pushes 1 if the first item is less than the second item. Both item are casted to strings.

22<o

Prints 0

23<o

Prints 1

Strings

< turns into contains if one of the arguments is a string. Both items are casted to strings.

"234" '2 <o

Prints 1

"333" 3 <o

Prints 1

>

Numbers

Pushes 1 if the first item is greater than the second item.

22>o

Prints 0

21>o

Prints 1

Strings

> turns into contains if one of the arguments is a string.

'2 "234" >o

Prints 1

3 "333" >o

Prints 1

CodeBlocks

CodeBlocks are surrounded by {} and are pushed to the stack. You can then assign them to variables with :. Calling the variable that holds the CodeBlock again will run the block. A CodeBlock will stay on the stack until it is popped off or used in a function/operator/CodeBlock. CodeBlocks can be popped off like every other object (;) and variables that are assigned to them will stay assigned to them.

{.*}:s;5s

This creates a block called s that squares a number. 5 is pushed and then the square block is called.

CodeBlocks can be set to any character, including spaces and unicode characters.

{'s}: ;  oo

This prints s twice.

CodeBlocks can also overwrite operators, like variables.

{;}:.;6.o

Throws an error because . is set to pop.

+

Adding CodeBlocks will have different effects depending on the arguments.

{1} "234+++" +

This will concatenate the string into the CodeBlock's code.

{5}5+

This does the same as above, including using the second argument as a string.

"1234**o" {5o}

This also does the same as above, except the string is added to the end of the CodeBlock code, so running this would print 51234.

Variables

Variables hold one value that can be pushed to the stack. To make a variable, you use :. This will take the value on the top of the stack and set the variable's value to it.

4:C

C is now set to 4. This will override any numbers, so using C won’t push 12 anymore, it will push 4. You can use any Unicode characters, but they will add to you byte count.

To push the value of a variable, you just have to use it as an operator.

4:k;kk+

The stack now holds 8:

  • 4 is pushed
  • the value of k is set to 4
  • 4 is popped off
  • k is pushed twice, putting two 4s on the stack
  • the two 4s are added together, leaving 8

To change the value of a variable, you just have to reassign it to the top of the stack using :.

4:l;5:l;ll*

The top of the stack is now 25.

Arrays

Arrays are made with [ ].

[1 2 3]

Arrays can contain any object except other lists.

+

To add lists together, you use +.

[1 2 3] [4 5 6]+

`

You can get the nth item of a list using `

[1 2 3 4 5] 2`

Pushes 3, since it starts from zero.

Dictionaries

Dictionaries are made with &. It will pop everything off the stack and store them in a Dictionary object witch can be modified and manipulated. The stack size must be an even number for everything to be put in the stack.

'a 5 'b 6 &

This creates a dictionary with keys a and b and values 5 and 6.

`

To get a value from a dictionary, you have to supply a key and use `

'a `

Pushes 5 to the stack.

+

You can add dictionaries together using +.

'a 1 'b 2 'c 3 & 'd 4 'e 5 'f 6 & +

Control flow

If statements

If statements are made in the form if then else by using ?.

1 {"true"o} {"false"o} ?

An object will trigger the 'true' block if it is:

  • String -> not equal to ""
  • double -> not equal to zero
  • CodeBlock -> run the block, pop the top value: check if the top value is true
  • Array -> size is not equal to zero
  • Dictionary -> keyset size is not equal to zero

If the object returns false, it will run the false CodeBlock.

For loops

For Loops are renamed to "Do Loops", as in "do {block} x amount of times", because f was already taken for File I/O.

5 {'%o} d

This will run the CodeBlock 5 times and print %%%%%.

While loops

This will pop the top of the stack. If that value is true, run the CodeBlock.

A.{1-..o' o} w

This will print 9 8 7 6 5 4 3 2 1 0. There are a lot of .s because w pops the stack each time it runs.

HTTP server

To make an http server, you use h.

"C:\index.html"ZZ*h

This creates a server using the index file located in the C drive; the port is set to 1296.

File I/O

f puts the interpreter into file mode, meaning the next command does something with files.

Getting data from a file

Let's say you have a file named stuff.txt and you want to get the text from that file. You can using fi

stuff.txt contains:

I like cheese

We'll use this to push the contents of the file to the stack (assuming that stuff.txt is in the same directory):

"stuff.txt"fi

The stack now contains the string "I like cheese".

Putting data in a file

Using fo, we can overwrite whatever is in a file with what is on the stack.

"I don't like cheese""stuff.txt"fo

Anything that was in stuff.txt is now gone, and it only contains "I don't like cheese". fo will only put the second item on the stack into the file, no other elements will be affected.

External resources