The Morpho language, part 1: Fundamentals

Once you’ve got Morpho running for the very first time, and perhaps tried out some of the shape minimization examples, you may care to understand the language in more detail. This tutorial will explain the basic features of the language, which as discussed earlier should be familiar to many people with prior program experience.

Expressions

Morpho programs are built up from sequences of expressions to be evaluated. Each program line may contain one or more expressions; if more than one is used they should be separated by semicolons ;. Expressions can be split across lines, in which case you need to indicate to Morpho that more input is forthcoming. For example, if you typed

[code lang=”js” gutter=”false”]2+3+[/code]

at the command prompt, Morpho recognizes that an operand is missing. It therefore displays the

[code lang=”js” gutter=”false”]?[/code]

prompt and wait for you to supply an additional expression. If, however, you were to simply type

[code lang=”js” gutter=”false”]2+3[/code]

then Morpho thinks this is a complete expression and so interprets it directly.

Types

Unlike languages such as C, Java and C++, Morpho is dynamically typed and type conversions are handled internally where meaningful. Hence an expression such as,

[code lang=”js” gutter=”false”]1 + 2e-3[/code]

that mixes an integer with a floating point number, yields

[code lang=”js” gutter=”false”]1.002[/code]

Morpho supports a number of basic types, including integers, floating point numbers, complex numbers, strings, functions, object references, etc.

Operators

Morpho accepts the following operators, listed in descending order of precedence.

Operators Description
**, ^ Power
+, – Unary plus, minus
! NOT
*, / Multiplication, division
+, – Addition, subtraction
<, <=, ==, !=, >=, > Comparison
&&, || AND, OR
= Assignment

Parentheses ( and ) may be used to control evaluation order.

Lists

Expressions may be collected together using lists,

[code lang=”js” gutter=”false”]{0,x,2*x,3}[/code]

which can also be nested arbitrarily,

[code lang=”js” gutter=”false”]list={{1,0},{0,1}}[/code]

It’s possible to retrieve and set elements of lists using index notation:

[code lang=”js” gutter=”false”]list[1,1][/code]

A variety of functions to generate and manipulate lists are available, e.g. length() and join().

Strings

Things like filenames etc. may be specified as strings

[code lang=”js” gutter=”false”]"Hello world!"[/code]

Comments

The characters // are used to introduce a comment, as in C++. All text after these characters is ignored by Morpho, e.g.

[code gutter=”false”]// This is a comment[/code]

Multiline comments can be surrounded by /* and */

Unlike in the C language, these comments can be nested, e.g.

[code gutter=”false”]/* This is a multi-line
* comment /* with nesting */
*/[/code]

Constants

Morpho recognizes a few built in constants:-

pi Ratio of a circle’s perimeter to its diameter.
e Base of natural logarithms.
I The square root of -1.
true Boolean logic value.
false Boolean logic value.
red, green, blue, white and black colors.

Functions

There are two categories of function, although in practice they are used identically: intrinsic functions provide Morpho’s functionality and user functions may be defined during execution of the script. A complete listing of the available intrinsic functions is available in section [sec:List-of-Intrinsic-1].

User function definitions use a compact form if they only one expression is needed,

[code lang=”js” gutter=”false”]x(u,v) = cos(u)*sin(v)[/code]

or can be enclosed in a list if multiple expressions are required,

[code lang=”js” gutter=”false”]x(u,v) = {
a=cos(u);
b=sin(v);
a*b
}
[/code]

Such functions always return the result of the last nonempty expression unless return is used.

All variables defined within a function remain local to that function, so that executing the following program

[code lang=”js” gutter=”false”]f(u) = { a=cos(u); return(a) }[/code]

[code lang=”js” gutter=”false”]f(0.2)[/code]

[code lang=”js” gutter=”false”]a[/code]

will result in an error

[code lang=”js” gutter=”false”]Unrecognized symbol ‘a'[/code]

on the last line because the variable

[code lang=”js” gutter=”false”]a[/code]

is defined only inside the function

[code lang=”js” gutter=”false”]f(u)[/code]

Morpho has many built in functions; to see all of them

[code lang=”js” gutter=”false”]intrinsics()[/code]

which returns a list. All of these will be introduced in future tutorials.

Objects

Morpho collects certain collections of data and associated functions into objects which come in various types of class. An object is typically created by a constructor function, e.g.

[code lang=”js” gutter=”false”]g=graphics()[/code]

which, once executed, creates an object and assigns a reference to it to the variable g. Objects can also be created by the intrinsic function new.

Note that if a further assignment is made

[code lang=”js” gutter=”false”]a=g[/code]

the object is not duplicated, just the reference. Morpho keeps track of how many things currently refer to a particular object, so if these variables are removed either because they’re local to a function or using

[code lang=”js” gutter=”false”]remove(a,g)[/code]

the associated object is automatically deleted.

Objects can be made to do things by invoking their selectors

[code lang=”js” gutter=”false”]g.point({0,0})[/code]

which adds the point

[code lang=”js” gutter=”false”]{0,0}[/code]

to the graphics object.

Morpho has many built in types of object. To see a list, type

[code lang=”js” gutter=”false”]classes()[/code]

To find out what selectors an object obeys, call the special selector

[code lang=”js” gutter=”false”]selectors()[/code]

e.g.

[code lang=”js” gutter=”false”]g.selectors()[/code]

which returns a list.

This tutorial has summarized the main language features of Morpho. In further language tutorials, we’ll look at some of the built in functions and classes, as well as how to create your own classes. 

Leave a Reply