Skip to content

csaye/flexscript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FlexScript

A programming language that compiles into multiple languages.

Currently supports Python, JavaScript, C#, Java, and C++.

Installation/Compilation

In order to install FlexScript, clone the project:

git clone https://github.com/csaye/flexscript
cd flexscript/

Then compile your .flex file like so:

./compile.sh <program.flex> <py | js | cs | java | cpp | all>

If necessary, give permission to execute compile.sh:

chmod +x compile.sh

Comments

Comments are written with hashtags and translated to all languages:

# this is a comment

If a comment begins with an underscore, it is parsed as a declaration:

#_MAIN

#_MAIN is used once to define where global class declarations end.

Variables

Variables can be defined with type varname; or type varname = value; and updated with varname = value;

int, double, char, string, and bool types are supported for all languages:

int a = 1;
double b = 0.5;
char c = 'a';
string d = "Hello World";
bool e = false;

Arrays

Arrays can be defined with type[] varname; or type[] varname = {};

Arrays can be updated with varname = type {}; and varname[index] = value;

int[] unset;
unset = int { 1, 2, 3 };
int[] array = { 1, 2, 3 };
array[1] = 0;
print(array[1]);

Functions

Function calls are written in function(); syntax with arguments separated by commas.

Function definitions are written in type function() {} syntax and should be done before #_MAIN:

void helloworld()
{
    print("Hello World");
}

#_MAIN
helloworld();

Certain functions are built-in:

print: outputs arguments to the console

print("Hello World");

slen: returns length of given string

string s = "Hello World";
int size = slen(s);

alen: returns length of given array

int[] array = { 1, 2, 3 };
int size = alen(array);

Conditional Statements

Conditional statements are written in the form statement (condition) {}.

if/elif/else (condition) statements:

if (false)
{
    print("false");
}
elif (false)
{
    print("false");
}
else
{
    print("true");
}

while (condition) statements:

int i = 0;

while (i < 10)
{
    print(i);
    i += 1;
}

for (int varname = low; varname < high; varname++) statements:

for (int i = 0; i < 10; i++)
{
    print(i);
}

foreach (type varname in varname) statements:

int[] array = { 1, 2, 3 };

foreach (int num in array)
{
    print(num);
}

Examples

HelloWorld.flex

Prints "Hello World".

#_MAIN
print("Hello World");

Fibonacci.flex

Prints the first 10 numbers of the Fibonacci sequence.

#_MAIN
int a = -1;
int b = 1;

for (int i = 0; i < 10; i++)
{
    int c = a + b;
    a = b;
    b = c;
    print(c);
}

FizzBuzz.flex

Prints numbers 1 to 100 (inclusive), replacing every multiple of 3 with "Fizz", every multiple of 5 with "Buzz", and every multiple of both with "FizzBuzz".

#_MAIN
for (int i = 1; i < 101; i++)
{
    string output = "";

    if (i % 3 == 0)
    {
        output += "Fizz";
    }
    if (i % 5 == 0)
    {
        output += "Buzz";
    }

    if (output == "")
    {
        print(i);
    }
    else
    {
        print(output);
    }
}

Primes.flex

Prints all prime numbers up to 100.

#_MAIN
for (int i = 2; i < 100; i++)
{
    bool prime = true;

    for (int j = 2; j < i; i++)
    {
        if (i % j == 0)
        {
            prime = false;
            break;
        }
    }

    if (prime)
    {
        print(i);
    }
}

Factorial.flex

Computes and prints the factorial of n using recursion.

int factorial(int n)
{
    if (n < 2)
    {
        return n;
    }
    else
    {
        return n * factorial(n - 1);
    }
}

#_MAIN
int n = 7;
int f = factorial(n);
print(f);

DoubleArray.flex

Doubles an integer array using a function and prints the results.

void doublearray(int[] array, int size)
{
    for (int i = 0; i < size; i++)
    {
        int newvalue = array[i] * 2;
        array[i] = newvalue;
    }
}

#_MAIN
int[] nums = { 1, 2, 3 };
int size = alen(nums);
doublearray(nums, size);

foreach (int num in nums)
{
    print(num);
}

More examples can be found in the examples folder.

About

A programming language that compiles into multiple languages.

Resources

Stars

Watchers

Forks