Skip to content

Takmg/flight

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

flight

flight is a lightweight programming language.

summary

The source code of this program is for learning programming languages.
Also, it's not practical because arrays is not supported and only Japanese are supported.


how to use

The fastest way to call a scripting language from C++ is to see 'flight-test/flight-test.cpp' and 'flight/flight.h'.

#include "flight.h"

void main()
{
    flight f;

    do {
        // load script file
        if(!f.load_file("script.f")){ break; }
        if(!f.load_file("script2.f")){ break; }

        // call when all scripts are loaded
        if(!f.load_complete()){ break; }
        
        // script execution
        f.exec();
    } while(false);

    // Get the error
    if (f.get_last_error() != flight_errors::error_none)
    {
      printf("%s", f.get_last_error_string().c_str());
    }
}

script

See 'flight-test/script/test.f' for a sample script.
It can be written like C language.

The details are described below.

variable

var a;
var b = 10;
var c = a * b + 3;
var str = "string";
var str2 = str + "aaaa";
var ch = 'a';
var oct = 0123;
var hex = 0xff1;
var val1 = 10 , val2 = 20; // multiple declare

comment

// comment
/* comment */

if

var a = 10;
if( a == 10 ){ /* execute */ }
else if( a != 20 ) {}
else if( a > 10 ) {}
else {}

for

for( var i = 0 ; i < 10 ; i += 1 ) { /* execute */ }

while

var count = 0;
while( count < 5 ){ count += 1; }

function

func fuctorial( var value )
{
    if(value <= 1) { return 1; }
    return value * fuctorial( value - 1);
}

factorial(5);

print function (embedded)

print( 5 );
print( "abc" );
print( "value => " + 10 );

yield

yield returns processing to host.

yield;
yield 2; // Can be picked up by the host.
How to receive the yield value on the host side
flight_variable var;
f.get_exec_data(var);
printf("last data => %s\n", var.value.c_str());  // yield 2 is returned 2, yield is returned ""

function pointer

func mul( var a , var b ) { return a * b; }
var fp_prt = print;
var fp_mul = mul;

fp_prt( fp_mul( 2 , 3 ) );

Addition of embedded functions

cpp

Call regisiter_function method.

#include "flight.h"

void add(flight_variable& ret_var, const flight_variable& arg1, const flight_variable& arg2)
{
    ret_var.value = std::to_string(atoi(arg1.value.c_str()) + atoi(arg2.value.c_str()));
    ret_var.type = flight_data_type::FDT_VARNUMBER;
    return;
}

void main()
{
    flight f;

    do {
        // load script file
        if(!f.load_file("script.f")){ break; }
        if(!f.load_file("script2.f")){ break; }

        // call when all scripts are loaded
        if(!f.load_complete()){ break; }

        // !!! add embedded function
        if (!f.register_function("add", add)) { break; }

        // script execution
        f.exec();

script

// call host function
print( "value => " + add( 1 , 2 ) );

References

http://www.hpcs.cs.tsukuba.ac.jp/~msato/lecture-note/comp-lecture/note1.html


Explanation end

About

flight is a lightweight programming language.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published