Back to top

expreva-logo

Expreva is a programming language based on arithmetic and algebra expressions.

The word arithmetic in Greek means the art of numbers.
Algebra comes from the Arabic word for reunion of parts.

The heart of the language is a mathematical expression evaluator. It runs as a virtual machine that interprets a handful of instructions.

Its purpose is mainly educational, and aims to stay small and easy to learn. This website is an interactive reference and sandbox for building the language.


Table of Contents


Getting Started

Click on any expression in blue text, and see its result on the left side of the page.

Example: 1 + 2 + 3

Type some numbers in the expression box to see the result change.

This process of transformation is called evaluating an expression to get its value.

In other words, you run an expression and it returns a value.


Here is a more advanced example: (add(x, y, z) = x + y + z)(4, 5, 6)

  • The first part creates a kind of expression called a function.
  • The second part runs it, giving some values as input.
  • The function calculates and returns a value as output.

We'll see what that means, and what we can do with it, as we explore the language.


Symbol

The basic building block of an expression is a symbol.

A symbol is made of one or more of the following characters.

0123456789
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
.=+-*/\%^!'"<>()[]{};

A symbol can be one of the following kinds:


Number

Example: 42, 2.5


Boolean

There are two kinds of boolean symbols: true and false.


Operator

An operator works with one or more symbols to produce another symbol.
Operator Description Example
+ Add 1 + 2
- Subtract 3 - 4
* Multiply 5 * 6
/ Divide 7 / 8
^ Exponentiation 9 ^ 10
! Factorial 11!
= Assignment x = 12

Comparison

These operators are used to compare symbols on its left and right sides.

Operator Left to right Example
== Equal 1 + 2 == 3
!= Not equal 1 + 2 != 3
> Greater than 3 * 3 > 3 + 3
< Less than 1 + 1 < 2
>= Greater than or equal 2 * 2 >= 2 + 2
<= Less than or equal 2 + 3 <= 3 + 3

Punctuation

A punctuation is a kind of operator that groups symbols together.

Open and close parentheses () control the order of operations in an expression.

See the difference between (1 + 2) * 3 and 1 + (2 * 3).


Statement

The punctuation called semicolon ; is used to separate expressions into statements.

Example: x = 2; y = x * 3; z = x * y

The result of multiple statements is the what the last statement returns.

Conditional

Use the operators ? and : to create a conditional expression.

A conditional expression has 3 parts.

  • Condition: an expression that evaluates to true or false
  • True branch: an expression to evaluate when the condition is true
  • False branch: an expression to evaluate when the condition is false

Example: 4 * 8 == 32 ? 'Yes' : 'No'


Comment

A comment is any text you want to write that is not evaluated.

It is surrounded by /* and */.

Example: /* Here is some comment */

Comments are useful for including extra information, for example, to explain how an expression works.


String

A string is some text created with the punctuation ' called a single quote.

Example: 'hi'

When you click on the example, you see that the result does not include the punctuations.

' is an operator that returns what's between them, called the value of the string.

Escape character

To include ' itself in the string, use another punctuation \ before it.

Example: 'human says \'hi!\''

This is called a backslash. It is used to escape the character after it, making it a string instead of a punctuation.

Emoji

The escape character can be used to create an emoji.

Example: '\U+1F600'

The part starting with U+ is called a unicode. See Unicode: Emoji List for reference.


Variable

A variable is a name that points to another symbol.

The name has one or more characters, alphabets and numbers only. It must start with an alphabet.

Example: cat

When you click on the above example, you see the result is empty. This is because the variable cat doesn't point to anything yet.

To make a variable point to a symbol, use the = operator. This is called assigning a value.

Example: cat = '\U+1F63A'

The result of variable assignment is the value itself.

Now, when you evaluate the variable cat, it will return its value.


Function

A function takes some input, evaluates an expression, and returns an output.

This concept is the basis of all programs and algorithms.

Define

To create a function, you need four parts.

  • Name of variable to assign the function

    Usually a function is named with a verb, because it performs an action.

    For functions used only once, the common name to use is f.

  • List of variable names, called arguments, between ()

    Each input passed to the function will be assigned to a variable with that name, in the order given in the arguments.

    Note that these variables exit only in the function, and cannot be used outside it.

    If the function doesn't need any input, the argument list can be empty.

  • = (variable assignment operator)
  • Expression that returns a result

This is called defining a function.

love(item1, item2) = 'I \U+2764 ' + item1 + ' and ' + item2 + '!'

See if you can identify the four parts of creating a function.

Call

To use a function, call it with a list of arguments.

  • Name of variable that points to the function
  • List of values between ()

Example: love('apples', 'bananas')

Make sure you clicked on the earlier example so the function exists.

The following happens when you call a function:

  • The input values are assigned to variables.

    In this example, item1 = 'apples' and item2 = 'bananas'.

  • The function evaluates an expression with those variables, and returns the result.

    In this example, it joins them to create a new string.

Apply

Another way to call a function is to apply an expression to it.

Use the operator -> after an expression to call a function with it as the first argument.

square(x) = x * x;
(1 + 2)->square()


List

To create a list of items, use the open and close square brackets [].

Use a comma , to separate each item.

Example: [ 1, 2, 3 ]

A list can have any kind of item, including a function, the result of an expression, or another list.

[ true, 2 * 3 + 1, f(x) = x * x, f(12), [ 'apples', 'oranges' ] ]

List Item

To get an item from a list, use a period . after it, and the index of the item.

An index is a number for the position of an item, counting from zero.

First, let's create a list and put it in a variable.

colors = [ 'red', 'green', 'blue' ]

Then get items by their index.

  • colors.0
  • colors.1
  • colors.2
  • colors.(1 + 1)

The last example shows the index can be given as an expression.

You can get an item from any expression that returns a list.

letters() = ['a', 'b', 'c'];
letters().0

List Functions

There are a number of built-in functions for working with a list.

Use the operator -> after a list to call these functions.

Size

size returns the number of items in a list.

Example: ['red', 'green', 'blue']->size()

Search

search looks for an item in a list, and returns its position index.

  • ['red', 'green', 'blue']->search('blue')
  • ['red', 'green', 'blue']->search('purple')

If it can't find the item, it will return -1.

Join

join puts two lists together and returns a new list.

Example: ['red', 'green', 'blue']->join(['white', 'black'])

If given a string, it's used as a separator to combine all items into a string.

Example: ['red', 'green', 'blue']->join(' and ')


Push

push adds one or more items to the end of a list, and returns the list itself.

Example: ['white']->push('purple', 'pink')

Pop

pop removes an item from the end of a list, and returns the item.

Example: [1, 2, 3]->pop()

Insert

insert adds one or more items to a list at a specified index, and returns the list.

Example: [0, 1, 2, 3]->insert(2, 'Here is index 2')

Slice

slice gets a section of a list and returns a new list with those items.

It takes two arguments: start index and end index.

Example: [1, 2, 3, 4 ,5]->slice(2, 4)

If you don't pass an end index, it will get from start index to the end of the list.


Map

map calls a given function with each item in the list, and returns a new list with what the function returned.

colors = ['red', 'green', 'blue'];

colors->map(f(color, index) = (index + 1)+'. '+color)

It passes two arguments to the function, the item and its index.

Filter

filter calls a given function with each item in the list, and if the function returns true, it keeps the item. It returns a new list with those items.

numbers = [1, 2, 3, 4, 5];

numbers->filter(f(number, index) = number >= 3)

It passes two arguments to the function, the item and its index.

Reduce

reduce is a way to take a list and add up a total of some kind, such as a number, string, or list.

numbers = [1, 2, 3, 4, 5];

numbers->reduce(f(total, item) = (total = total + item), 0)

reduce takes two arguments: a function and an initial value to use, called an accumulator. In the above example, the accumulator starts with the value of zero.

For each item in the list, the given function is called with the accumulator and the item. It's expected to return a new value for the accumulator. At the end, you get the final value.


Object

An object is a kind of list where the items are in pairs, called a key and a value. It is sometimes called a dictionary, because you "look up" a value by its key.

To create an object, use the open and close curly brackets {}.

Example: {}

Use a colon : between key and value.

Example: { key: 'value' }

Use a comma , to separate key-value pairs.

Example: { x: 1, y: 2, z: 3 }

To get a value, use the puctuation . and the key after an object.

Example: { x: 1, y: 2, z: 3 }.x

Object Functions

There are a number of built-in functions for working with an object.

Use the operator -> after an object to call these functions.

Size

size returns the number of keys in an object.

Example: { x: 1, y: 2, z: 3 }->size()

Keys

keys returns the keys in an object as a list.

Example: { x: 1, y: 2, z: 3 }->keys()


Set

set assigns a key and its value to an object, and returns the object.

Example: { x: 1, y: 2, z: 3 }->set('a', 0)

If you pass an object instead of a key, it will set all keys and their values.

Example: { x: 1, y: 2, z: 3 }->set({ a: -1, b: -2 })

Unset

unset removes a key and its value from an object, and returns the object.

Example: { x: 1, y: 2, z: 3 }->unset('x')->unset('y')


Future Plan

  • Libraries
    • Math functions
    • Canvas for drawing
    • Music functions and data structures
    • Database in browser local storage
  • Implementation in TypeScript
  • Integrated development environment
    • Editor
    • Syntax highlighting
    • Import and export programs

Maybe

  • Operators: +=, -=, *=, /=, ++, --
  • Libraries to go beyond JavaScript's limitations
    • Floating-point arithmetic: .1 + .2 should equal .3
    • Big numbers: 9999999999999999
  • Implementation in WebAssembly