Magik (programming language)

Last updated

Magik is an object-oriented programming language that supports multiple inheritance and polymorphism, and it is dynamically typed. It was designed and implemented in 1989 by Arthur Chance of Smallworld Systems Ltd. as part of Smallworld Geographical Information System (GIS). Following Smallworld's acquisition in 2000, Magik is now provided by GE Energy, still as part of its Smallworld technology platform.

Contents

Magik (Inspirational Magik) was originally introduced in 1990 and has been improved and updated over the years. Its current version is 5.2.

In July 2012, Magik developers announced that they were in the process of porting Magik language on the Java virtual machine. The successful porting was confirmed by Oracle Corporation in November of the same year. [1]

Similarities with Smalltalk

Magik itself shares some similarities with Smalltalk in terms of its language features and its architecture: the Magik language is compiled into byte codes interpreted by the Magik virtual machine. The Magik virtual machine is available on several platforms including Microsoft Windows, various flavours of Unix and Linux.

Magik is console based and code can be modified on the fly even when an application is running. The console can also be used to execute Magik code and to see the results.

Compiled code is stored in a single file called an image file. Each image file holds the compiled byte-codes and the state of the session (for example variable values) when the image was last saved.

Language features

Comments

Magik uses the # token to mark sections of code as comments:

 # This is a comment.

Assignments

Magik uses the << operator to make assignments:

  a << 1.234   b << b + a   c << "foo" + "bar" # Concat strings 

For clarity, this notation is read as "a becomes 1.234" or "b becomes b plus a". This terminology separates assignment from comparison.

Magik also supports a compressed variation of this operator that works in a similar way to those found in C:

  b +<< a # Equivalent to b << b + a 

To print a variable you can use the following command

 a << "hello"  write(a)

Symbols

As well as conventional data types such as integers, floats and strings Magik also implements symbols. Symbols are a special token data type that are used extensively throughout Magik to uniquely identify objects. They are represented by a colon followed by a string of characters. Symbols can be escaped using the vertical bar character. For example:

  a << :hello  # whenever :hello is encountered, it is the same instance   b << :|hello world| 

Dynamic typing

Magik variables are not typed as they are in say C# and can reference different objects at runtime. Everything in Magik is an object (there is no distinction between objects and primitive types such as integers):

  a << 1.2     # a floating point number is assigned to variable 'a'   a << "1.2"   # later, a string is assigned to variable 'a' 
Objects

Objects are implemented in Magik using exemplars. Exemplars have similarities to classes in other programming languages such as Java, but with important differences. Magik supports multiple inheritance, and mixins (which implement functionality with no data). New instances are made by cloning an existing instance (which will typically be the exemplar but does not have to be).

New exemplars are created using the statement def_slotted_exemplar(), for example:

  def_slotted_exemplar(:my_object,   {     {:slot_a, 34},     {:slot_b, "hello"}   }, {:parent_object_a, :parent_object_b}) 

This code fragment will define a new exemplar called my_object that has two slots (or fields) called slot_a (pre-initialised to 34) and slot_b (pre-initialised to "hello") that inherits from two existing exemplars called parent_object_a and parent_object_b.

Comparison

Magik implements all usual logical operators (=, <, <=, >, >=, ~=/<>) for comparison, as well as a few unusual ones. The _is and _isnt operators are used for comparing specific instances of objects, or object references rather than values.

For example:

  a << "hello"   b << "hello"    a = b # returns True (_true) because the values of a and b are equal   a _is b # returns False (_false) because a is not the same instance as b    a << "hello"   b << a   a = b # returns True (_true) because the values of a and b are equal   a _is b # returns True (_true) because b was assigned the specific instance of the same object as a, rather than the value of a. 

Methods

Methods are defined on exemplars using the statements _method and _endmethod:

  _method my_object.my_method(a, b)     _return a + b   _endmethod 

It is convention to supply two methods new() (to create a new instance) and init() (to initialise an instance).

  # New method   _method person.new(name, age)     _return _clone.init(name, age)   _endmethod    # Initialise method.   _private _method person.init(name, age)      # Call the parent implementation.      _super.init(name, age)      # Initialise the slots.      .name << name      .age << age     _return _self   _endmethod 

The _clone creates a physical copy of the person object. The _super statement allows objects to invoke an implementation of a method on the parent exemplar. Objects can reference themselves using the _self statement. An object's slots are accessed and assigned using a dot notation.

Methods that are not part of the public interface of the object can be marked private using the _private statement. Private methods can only be called by _self, _super and _clone.

Optional arguments can be declared using the _optional statement. Optional arguments that are not passed are assigned by Magik to the special object _unset (the equivalent of null). The _gather statement can be used to declare a list of optional arguments.

  _method my_object.my_method(_gather values)        _endmethod 

Iteration

In Magik the _while, _for, _over, _loop and _endloop statements allow iteration.

_block  _local s << 0   _local i << 0  _while i <= 100  _loop    s +<< i    i +<< 1   _endloop >> s _endblock 

Here, the _while is combined with _loop and _endloop.

  _method my_object.my_method(_gather values)     total << 0.0     _for a _over values.elements()     _loop        total +<< a     _endloop     _return total   _endmethod    m << my_object.new()   x << m.my_method(1.0, 2, 3.0, 4) # x = 10.0 

Here values.elements() is an iterator which helps to iterate the values.

In Magik generator methods are called iterator methods. New iterator methods can be defined using the _iter and _loopbody statements:

  _iter _method my_object.even_elements()     _for a _over _self.elements()     _loop       _if a.even? _is _true       _then          _loopbody(a)              _endif     _endloop   _endmethod 

Procedures

Magik also supports functions called procedures. Procedures are also objects and are declared using the _proc and _endproc statements. Procedures are assigned to variables which may then be invoked:

  my_procedure << _proc @my_procedure(a, b, c)     _return a + b + c   _endproc    x << my_procedure(1, 2, 3) # x = 6 

Regular expression

Magik supports // regular expression syntax:

_if /Hello\,\s(\w)+!/.matches?("Hello, Magik!") _then     write("Got a match!") _endif  

and to capture groups in Regex:

/sw([0-9]+)-([0-9]+).*/.replace_all("sw65456-324sss", "$1") # "65456" /sw([0-9]+)-([0-9]+).*/.replace_all("sw65456-324sss", "$2") # "324" 

HTTP library

Magik supports making HTTP or HTTPS requests via http library, see below examples:

magikhttp << http.new() magikhttp.url("https://www.google.com").get() magikhttp.url("https://www.google.com").post({"User-agent", "Bot"}, "some data") 

Language quirks

Because Magik was originally developed in England, methods in the core smallworld libraries are spelled using British English. For example:

  Use "initialise", not "initialize".

Collections

Like other programming language Magik too has collections. They include the following:

Hello World example

The following is an example of the Hello world program written in Magik:

 write("Hello World!")

Related Research Articles

<span class="mw-page-title-main">Smalltalk</span> Object-oriented programming language released first in 1972

Smalltalk is a purely object oriented programming language (OOP) that was originally created in the 1970s for educational use, specifically for constructionist learning, but later found use in business. It was created at Xerox PARC by Learning Research Group (LRG) scientists, including Alan Kay, Dan Ingalls, Adele Goldberg, Ted Kaehler, Diana Merry, and Scott Wallace.

Sather is an object-oriented programming language. It originated circa 1990 at the International Computer Science Institute (ICSI) at the University of California, Berkeley, developed by an international team led by Steve Omohundro. It supports garbage collection and generics by subtypes.

A visitor pattern is a software design pattern that separates the algorithm from the object structure. Because of this separation new operations can be added to existing object structures without modifying the structures. It is one way to follow the open/closed principle in object-oriented programming and software engineering.

In computer programming, an iterator is an object that enables a programmer to traverse a container, particularly lists. Various types of iterators are often provided via a container's interface. Though the interface and semantics of a given iterator are fixed, iterators are often implemented in terms of the structures underlying a container implementation and are often tightly coupled to the container to enable the operational semantics of the iterator. An iterator performs traversal and also gives access to data elements in a container, but does not itself perform iteration.

<span class="mw-page-title-main">For loop</span> Control flow statement for repeated execution

In computer science a for-loop or for loop is a control flow statement for specifying iteration. Specifically, a for loop functions by running a section of code repeatedly until a certain condition has been satisfied.

<span class="mw-page-title-main">ActionScript</span> Object-oriented programming language created for the Flash multimedia platform

ActionScript is an object-oriented programming language originally developed by Macromedia Inc.. It is influenced by HyperTalk, the scripting language for HyperCard. It is now an implementation of ECMAScript, though it originally arose as a sibling, both being influenced by HyperTalk. ActionScript code is usually converted to byte-code format by a compiler.

In class-based, object-oriented programming, a constructor is a special type of function called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.

<span class="mw-page-title-main">Java syntax</span> Set of rules defining correctly structured program

The syntax of Java is the set of rules defining how a Java program is written and interpreted.

In mathematics and in computer programming, a variadic function is a function of indefinite arity, i.e., one which accepts a variable number of arguments. Support for variadic functions differs widely among programming languages.

xHarbour is a free multi-platform extended Clipper compiler, offering multiple graphic terminals (GTs), including console drivers, GUIs, and hybrid console/GUIs. xHarbour is backward-compatible with Clipper and supports many language syntax extensions, greatly extended run-time libraries, and extensive third party support.

Harbour is a computer programming language, primarily used to create database/business programs. It is a modernised, open source and cross-platform version of the older Clipper system, which in turn developed from the dBase database market of the 1980s and 1990s.

<span class="mw-page-title-main">JavaScript syntax</span> Set of rules defining correctly structured programs

The syntax of JavaScript is the set of rules that define a correctly structured JavaScript program.

<span class="mw-page-title-main">Python syntax and semantics</span> Set of rules defining correctly structured programs

The syntax of the Python programming language is the set of rules that defines how a Python program will be written and interpreted. The Python language has many similarities to Perl, C, and Java. However, there are some definite differences between the languages. It supports multiple programming paradigms, including structured, object-oriented programming, and functional programming, and boasts a dynamic type system and automatic memory management.

C# and Visual Basic .NET are the two primary languages used to program on the .NET Framework.

PROMELA is a verification modeling language introduced by Gerard J. Holzmann. The language allows for the dynamic creation of concurrent processes to model, for example, distributed systems. In PROMELA models, communication via message channels can be defined to be synchronous, or asynchronous. PROMELA models can be analyzed with the SPIN model checker, to verify that the modeled system produces the desired behavior. An implementation verified with Isabelle/HOL is also available, as part of the Computer Aided Verification of Automata (CAVA) project. Files written in Promela traditionally have a .pml file extension.

This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.

The syntax and semantics of PHP, a programming language, form a set of rules that define how a PHP program can be written and interpreted.

<span class="mw-page-title-main">Rexx</span> Command/scripting/programming language

Rexx is a programming language that can be interpreted or compiled. It was developed at IBM by Mike Cowlishaw. It is a structured, high-level programming language designed for ease of learning and reading. Proprietary and open source Rexx interpreters exist for a wide range of computing platforms; compilers exist for IBM mainframe computers.

Objective-C is a high-level general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its NeXTSTEP operating system. Due to Apple macOS’s direct lineage from NeXTSTEP, Objective-C was the standard programming language used, supported, and promoted by Apple for developing macOS and iOS applications until the introduction of the Swift programming language in 2014.

<span class="mw-page-title-main">Nim (programming language)</span> Programming language

Nim is a general-purpose, multi-paradigm, statically typed, compiled high-level systems programming language, designed and developed by a team around Andreas Rumpf. Nim is designed to be "efficient, expressive, and elegant", supporting metaprogramming, functional, message passing, procedural, and object-oriented programming styles by providing several features such as compile time code generation, algebraic data types, a foreign function interface (FFI) with C, C++, Objective-C, and JavaScript, and supporting compiling to those same languages as intermediate representations.

References

  1. Jim Connors (2012-11-05). "Sprinkle Some Magik on that Java Virtual Machine" . Retrieved 2012-11-06. With this new capability GE Energy has succeeded in hosting their Magik environment on top of the Java Virtual Machine