Forobj

From Esolang
Jump to navigation Jump to search
Forobj
Paradigm(s) imperative, self-modifying
Designed by User:Fergusq
Appeared in 2012 (designed), 2014 (published)
Memory system stack-based, variable-based
Dimensions one-dimensional
Computational class Turing complete
Reference implementation Unimplemented
Influenced by dc
Influenced Fortob
File extension(s) .forobj

Forobj is an object oriented programming language created by User:Fergusq. It is designed to be easily extendable and almost impossible to parse without interpreting.

Overview

Forobj is a stack-based language. A program is a list of commands and values. Values are pushed to the stack as commands are executed. Techically, values are commands.

"This is a string value and is pushed to the stack."
d
"'d' is a command which pops and prints a string."
d

The interpreter parses and executes at the same time. Command syntaxes can be edited runtime. Control flow is done by editing input queue.

Forobj is statically typed, which means that a variable can have only one type. Whitespace is not allowed between command arguments.

Input queue

Forobj code is translated into an input queue at the begin of interpretation. The input queue contains characters. The interpreter reads those characters and creates tokens from them following command syntax. A string, for example, is a token.

When a function is called, interpreter creates a new input queue from function's code and executes it.

Editing input queue

There are two commands used to modify the input queue. \ appends code to it, and ; removes it.

A loop can be done using these commands.

[, code condition;\],._%

First, a function (a string) is pushed to the stack and duplicated. Then it is called and intepreteted in the same stack (_). In the function, the function itself is first duplicated (,). At the end of the function, an input queue is conditionally cleared if a condition is true (clearing input queue exits from the function). If condition is false, the code of the function is appended to the input queue (\).

Control Flow

Basic if:

[
	then
][
	else
]condition?

While loop, code must not leave elements to the stack.

[,
	code
condition;\],._%

Functions

Defining a function:

[>argI >argII >argIII
	code
]>name

For example:

[>name
	"Hello, "d $name d "!\n"d
]>sayHello

Calling a function:

$name.argument_count

Example:

"John" $sayHello.1

Classes and objects

Creating a class:

~car<$speed>
[>self "Driving... Speed: "d $self:$speed d "\n"d ] ~car<drive:0>

Fields do not need to be declared, but they should, as if not, they are initiated with an undefined value and throw an error if are being accessed.

Using class:

^car >mycar
$mycar:drive

Fields can be used to create a hash map:

[ ""@+ ]#s_
[>self>value>name $value\ [$self:>a]\ $name s\] ~map<set:2>
[>self>name [$self:$a]\ $name s\] ~map<get:1>

^map >m
"value" "key" $m:set
"key" $m:get d

Commands

Characters c (character), i (identifier) and n (number) represent command arguments. Commands can have many syntaxes.

Values

Command Character Command syntax Stack After Name Description
" "c*" s String Creates a new string and pushes it to the stack.
[ [([?c*[?)*] s Nested String Used for function body declarations
' 'n n Number Pushes a number to the stack
^ ^i obj Object Creates a new object. Argument is the class name.

Commands and classes

Command Character Command syntax Stack Before Stack After Name Description
@ @c ? ? Original Command Executes the original command. Usable if a command is overwritten.
# #cn
#c_
string Command Declaration Defines a new command. Argument is the parameter count. Semantics similar to the method declaration and function calling.
< <[cnis]
<ac
token_value Read Token Reads a token from the parent input queue. Used for reading command arguments. c is a character, n is a number, i is an identifier and s is a whitespace. This can also accept a character, throwing syntax error if it doesn't match.
~ ~i<i:n>
~i<i:_>
string Method Declaration Defines a new method. Class name is the first argument. Between < and > are the method name and the argument count. Semantics similar to the command declaration.
~ ~i<$i> Field Declaration Defines a new field. Class name is the first argument.

Functions and object access

Command Character Command syntax Stack Before Stack After Name Description
. .n
._
args function return_value Function call Calls a function with n arguments. Function is a string. If argument number is not specified (_), uses the main stack. Otherwise creates a new stack and pushes arguments into it. Return value is popped from functions stack (if not empy).
? ? then else condition Conditional Function Call Calls then function if condition is not 0, otherwise the else function. No new stacks are created.
: :i args object return_value Object Method Call Calls a method. Semantics similar to the function call, except that the object is pushed to function's new stack.
: :$i object field_value Object Field Load Pushes the value of a field to the stack.
: :>i value object Object Field Store Stores a value to the field.

Variables

Variables are similar to fields. Variables belong to a stack. If a new stack is created (when function is called), it contains it's own variable scope.

Command Character Command syntax Stack Before Stack After Name Description
$ $i var_value Variable Load Pushes the value of a variable to the stack.
> >i value Variable Store Stores a value to a variable.

Input Queue

Every string has it's own input queue. When the program starts, the whole program loaded into an input queue. When a string is executed as a function, it is loaded into a new input queue.

Command Character Command syntax Stack Before Name Description
\ \ string Input Queue Add Adds a string to the end of the input queue.
; ; condition Input Queue Delete Deletes the input queue if the condition is not 0, exits from the current input queue.

Stack

Command Character Command syntax Stack Before Stack After Name Description
, , a a a Stack Duplicate Duplicates the top element of the stack.
½ ½ a b b a Stack Swap Exchanges the top elements of the stack.
% % a Stack Pop Removes the top element of the stack.

Math

Command Character Command syntax Stack Before Stack After Name Description
+ + a b a+b Add Adds two numbers or concatenates two strings.
- - a b a-b Subtract Subtracts a number from another.
* * a b a*b Multiply Multiplies two numbers.
/ / a b a/b Division Divides a number by another.
_ _ a -a Negation Negates a number.

Logic

Command Character Command syntax Stack Before Stack After Name Description
= = a b a=b Equals 1 if equal, otherwise 0.
& & a b a&b And 1 if neither a or b is 0, otherwise 0.
¦ ¦ a b a¦b Or 1 if a or b is 1, otherwise 0.
! ! a !a Not 1 if a is 0, otherwise 0.

I/O

Command Character Command syntax Stack Before Stack After Name Description
d d msg Default Out Print's a string to the default output stream.
n n char Default In Next Character Reads a character from the default input stream.

Examples

Hello world

"Hello World\n"d

Fibonacci sequence

["\n"d]#l_

'0 >a
'1 >b
'0 >c
'10 >i

[,
	$a $b +	>c
	$b >a
	$c >b
	
	'11$i-d	":"d $a dl
	
	$i '1 - >i
	$i!; \
],._%

Short version:

',>a>c'1>b'10>i[,$b,$a+,>c$b>a>b$a d"\n"d$i'1-,>i!; \],._%

Truth-machine

n>n["0"d][[,"1"d\],[]$n"1"=?]$n"0"=?

Command example

["\n"d]#l_
[
	<n>id
	<s
	<a-
	<s
	<i>name
	
	"<b>§"d $id d " - <i>"d $name d "</i>"dl
]#§0

§1 - Introduction
§2 - Body
§3 - FinalWords

Outputs:

<b>§1 - <i>Introduction
<b>§2 - <i>Body
<b>§3 - <i>FinalWords

Deadfish interpreter

">> "d'>i[,n>c[$i'1+>i][[$i'1->i][[$i,*>i][[$i d"\n"d][">> "d]$c"o"=?]$c"s"=?]$c"d"=?]$c"i"=?['>i][]$i'256=$i'=|?\],._

Brainfuck interpreter

Brainfuck program is appended to the end of the interpreter. Output command is p, input is not supported. Loop is %bf[ code ]o.

["\n"d]#l_

" type conversion commands "%
[ ""@+ ]#s_
[ "'"\\ ]#i_

"Class tape"%

	[@>self@>value@>name $value\ [$self:>a]\ $name s\] ~tape<set:2>
	[@>self@>name [$self:$a]\ $name s\] ~tape<get:1>

"Class bf"%

	~bf<$i>
	~bf<$tape>

^bf >bf
^tape , $bf:>tape >tape

" tape initialization "%
'1000 $bf:>i
[,
	'0 $bf:$i $tape:set
	$bf:$i '1 -$bf:>i
	$bf:$i!;
\],._%

'1 $bf:>i

[$bf:$i $bf:$tape:get '1 @+ $bf:$i $bf:$tape:set]#+_
[$bf:$i $bf:$tape:get '1 @- $bf:$i $bf:$tape:set]#-_
[$bf:$i '1 @- $bf:>i ]#<_
[$bf:$i '1 @+ $bf:>i ]#>_
[@>bf @>loop [, $bf:$i $bf:$tape:get !; $loop._ \],._% ]#o2
[$bf:$i $bf:$tape:get dl]#p_

" bf hello world "% 
++++++++$bf[>++++$bf[>++>+++>+++>+<<<<-]o>+>+>->>+$bf[<]o<-]o>>p>---p+++++++pp+++p>>p<-p<p+++p------p--------p>>+p>++p

See also

  • Fortob – The second language of the For series