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,
- Easy porting of scripts to C/C++ code, and vice versa.
- 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.
I just don't see what problem you are trying to solve. I am sure it's there but you don't seem to say what it is.
ReplyDeleteHave you looked at Objective-C? It has some very interesting dynamic features which might be what you are looking for.
See SPL for a description of SPL, a dynamic C/C++ based numerical analysis language that combines C/C++ syntax with Matlab-like functionality.
ReplyDelete