Tuesday, 30 April 2013

What is the goal of MewaScript ?

Both dynamic programming languages and compiled programming languages have advantages and disadvantages when compared to each other.

With dynamic programming languages, software development is easier and faster, while compiled programming languages offer the best performance.

MewaScript aims to bring together the best of both worlds: To be a high-level programming language that seamlessly bridge with C/C++ source code.

Because most applications are developed using C/C++/Java (according to the most popular languages statistics), an interpreted programming language with a syntax similar to C/C+/Java would be a lot more intuitive and easier to learn.
Also, an interpreted language that can be easily translated to C/C++ would be very beneficial for prototyping and rapid development of algorithms that need, at a later stage, to be integrated in C/C++/Java projects.

Tuesday, 30 March 2010

Mewa, another programming language?

C/C++ is one of the most common, and complex, programming languages in use today. For many needs, like ptototyping and data analysis, C/C++ is very expensive and time consuming.

MewaScript is a dynamic language that aims to offer the flexibility of developing with a high level programming language and the performance of C/C++, a compiled language. This aim could only be achieved by designing a completely new programming language that throughout its design would bridge very well with C/C++. MewaScript bridges with C/C++ in 2 ways,
  1. Easy porting of scripts to C/C++ code, and vice versa.
  2. Easy integration of C/C++ code as a linked library.
Lets have a look at some MewaScript examples:

if( accel > 0 )
{
  velocity *= accel;
}
else
{
  velocity = 0;
} 
MewaScript conditional if


for( i = 0; i<=3; i++ )
{
  data(i) = 0;
} 
MewaScript loop
From the snippets above we can see that MewaScript has a very similar syntax to C/C++. Note that MewaScript is not a C++ interpreter, MewaScript is a dynamic programming language. The aim of MewaScript programming language is to increase development speed by exploring the advantages offered by dynamic programming and, at the same time, achieve the performance offered by compiled languages.

Lets compare side by side 2 examples, the first written in C++ and the second written in MewaScript  Both examples implement a min function, that returns the minimum of 2 values and print the result.
#include <iostream>
using namespace std;

int min (int a, int b)
{
  if( a < b )
    return a;
  else
    return b;
}

int main ()
{
  int z;
  z = min(5,3);
  cout << "min is " << z;
  return 0;
}
C++ example

function min( a, b )
{
  if( a < b )
    return a;
  else
    return b;
}

z = min(5,3);
cout << "min is " << z;
MewaScript example
MewaScript functions start with the keyword function followed by the name of the function and its arguments, passed by copy. Note that the MewaScript example of the min function accepts any data types (integers, floats, etc).

Comparing the above snapshots we can observe that,
  • MewaScript reduces the amount of typing when compared to C++;
  • There are no forward declarations and no header files; everything is declared exactly once;
  • Initialization is expressive, automatic, and easy to use, to initialize a string just type (str="my string";)
  • Syntax is clean and light on keywords;
These simplifications are not only beneficial for MewaScript developers, a simpler syntax means that the script engine can run faster when compared to interpreting a low level programming language.

Mewa is very versatil, although functions do not need to declare its input argument data types, Mewa offers the option to declare them. Below we have an example,

function min( int a, int b )
{
  if( a < b )
    return a;
  else
    return b;
}
Mewa function with declared input data types
By allowing functions to declare the data type of its input arguments we are bringing a feature that is not commonly found in scripting languages but is widely used by C++ and Java, its called function overloading.

function print( int i )
{
  cout << "Here is a Int " << i ;
}

function print( char c )
{
  cout << "Here is a Char " << c ;
}
Example of function overloading

Another feature borrowed from C++ and Java is default values in parameters.

function divide( a, b = 2 )
{
  r=a/b;
  return r;
}
Default values in parameters


Till now we have seen functions passing argments by copy, this means that the arguments a and b are copies and can be modified inside the function without affecting any variable outside the function. Like C++, MewaScript also allows to pass arguments to functions by reference. This is a practical feature that allows, inside functions, to modify variables send as arguments.

In MewaScript function arguments are always passed by reference. This decision might change in the future, but it was adopted because it simplifies both the language syntax and interpreter engine. Below is an example of a function that swaps the values of its arguments.

function swap( a, b )
{
  tmp = b;
  b = a;
  a = tmp;
}
Mewa function passing arguments by reference
The uniqueness of MewaScript is not only about being similar to C/C++, MewaScript stem from the desire to create a modern programming language that embraces state of the art concepts like "prototypal inheritance" and multi-threading.

MewaScript was designed from ground up to take advantage of dynamic programming, and at the same time, provide a very similar syntax compared to C/C++/Java, to save developers from learning a new programming language and make MewaScript code easily portable to C/C++/Java. The diferences between compiled languages and MewaScript start appearing when we create classes, and that's what I will talk about in a future post.