Exemplo n.º 1
0
def test_Define_and_call_fn_to_add_unknown_numbers():
    env = PepEnvironment( PepCppRenderer() )
    env.namespace["othernum"] = PepVariable( PepType( PepInt ), "othernum" )

    fndecl = PepDef(
        PepType( PepInt ),
        PepSymbol( "myfunc" ),
        (
            ( PepType( PepInt ), PepSymbol( "x" ) ),
            ( PepType( PepInt ), PepSymbol( "y" ) )
            ),
        (
            PepReturn( PepPlus( PepSymbol( "x" ), PepSymbol( "y" ) ) ),
            )
        )

    assert_equal( render_evald( fndecl, env ), "" )

    value = PepFunctionCall( PepSymbol( "myfunc" ),
        ( PepInt( "3" ), PepSymbol( "othernum" ) ) )

    assert_equal( render_evald( value, env ), "myfunc( 3, othernum )" )
    assert_multiline_equal( env.renderer._functions["myfunc"].values()[0][1],
"""int myfunc( int x, int y )
{
    return (x + y);
}

""" )
Exemplo n.º 2
0
def test_Define_and_call_fn_returning_void_unknown():
    env = PepEnvironment( PepCppRenderer() )
    add_builtins( env )

    env.namespace["othernum"] = PepVariable( PepType( PepInt ), "othernum" )

    fndecl = PepDef(
        PepSymbol( "void" ),
        PepSymbol( "myfunc" ),
        (
            ( PepSymbol( "int" ), PepSymbol( "x" ) ),
            ( PepSymbol( "int" ), PepSymbol( "y" ) )
            ),
        (
            PepSymbol( "pass" ),
        )
    )

    assert_equal( render_evald( fndecl, env ), "" )

    value = PepFunctionCall( PepSymbol( "myfunc" ),
        ( PepInt( "3" ), PepSymbol( "othernum" ) ) )

    assert_equal( render_evald( value, env ), "myfunc( 3, othernum )" )
    assert_multiline_equal( env.renderer._functions["myfunc"].values()[0][1],
"""void myfunc( int x, int y )
{
}

""" )
Exemplo n.º 3
0
def test_single_statement_if():
    env = PepEnvironment( PepCppRenderer() )
    builtins.add_builtins( env )

    # import sys
    #
    # if len( sys.argv ) > 1:
    #   print sys.argv[1]

    impt = PepImport( "sys" )

    ifstmt = PepIf(
        PepGreaterThan(
            PepFunctionCall(
                PepSymbol( "len" ), (
                    PepSymbol( "sys.argv" ),
                    )
                ),
            PepInt( "1" )
            ),
            (
                PepFunctionCall(
                    PepSymbol( "print" ), (
                        PepArrayLookup(
                            PepSymbol( "sys.argv" ),
                            PepInt( "1" )
                            ),
                        ),
                    ),
            ),
            None
        )

    program = ( impt, ifstmt )

    assert_multiline_equal( env.render_exe( program ), """#include <stdio.h>

int main( int argc, char* argv[] )
{
    if( (argc > 1) )
    {
        printf( "%s\\n", argv[1] );
    }

    return 0;
}
""" )
Exemplo n.º 4
0
def test_overloaded_functions():
    env = PepEnvironment( PepCppRenderer() )

    fnint = PepUserFunction(
        "myfn",
        PepType( PepVoid ),
        ( ( PepType( PepInt ), PepSymbol( "x" ) ), ),
        ( PepPass(), )
    )

    fnflt = PepUserFunction(
        "myfn",
        PepType( PepVoid ),
        ( ( PepType( PepFloat ), PepSymbol( "f" ) ), ),
        ( PepPass(), )
    )

    rtfn1 = PepRuntimeUserFunction( fnflt, ( PepFloat( "3.0" ), ), None )
    rtfn2 = PepRuntimeUserFunction( fnint, ( PepInt( "3" ),     ), None )
    rtfn3 = PepRuntimeUserFunction( fnint, ( PepInt( "4" ),     ), None )
    rtfn4 = PepRuntimeUserFunction( fnflt, ( PepFloat( "5.1" ), ), None )

    ans = env.renderer.render_exe( [rtfn1, rtfn2, rtfn3, rtfn4], env )

    assert_multiline_equal( ans, """
void myfn( double f );
void myfn_pep_1( int x );

void myfn( double f )
{
}

void myfn_pep_1( int x )
{
}

int main( int argc, char* argv[] )
{
    myfn( 3.0 );
    myfn_pep_1( 3 );
    myfn_pep_1( 4 );
    myfn( 5.1 );

    return 0;
}
""" )
Exemplo n.º 5
0
def test_Hello_World():
    env = PepEnvironment( PepCppRenderer() )
    builtins.add_builtins( env )

    value = PepFunctionCall( PepSymbol( "print" ),
        ( PepString( "Hello, World!" ), ) )

    assert_multiline_equal(
        env.render_exe( ( value, ) ),
        """#include <stdio.h>

int main( int argc, char* argv[] )
{
    printf( "Hello, World!\\n" );

    return 0;
}
""" )
Exemplo n.º 6
0
def test_Echo_arg1():
    env = PepEnvironment( PepCppRenderer() )
    builtins.add_builtins( env )

    # import sys
    #
    # def string getname( string name ):
    #     return name
    #
    # print sys.argv[1]

    impt = PepImport( "sys" )

#    fndef = PepDefine( PepSymbol( "getname" ),
#        PepUserFunction(
#            "getname",
#            PepType( PepString ),
#            (
#                ( PepType( PepString ), PepSymbol( "name" ) ),
#                ),
#            (
#                PepReturn( PepSymbol( "name" ) ),
#                )
#            )
#        )

    fncall = PepFunctionCall( PepSymbol( "print" ),
        ( PepArrayLookup( PepSymbol( "sys.argv" ), PepInt( "1" ) ), ) )

    program = ( impt, fncall )

    assert_multiline_equal(
        env.render_exe( program ),
        """#include <stdio.h>

int main( int argc, char* argv[] )
{
    printf( "%s\\n", argv[1] );

    return 0;
}
""" )
Exemplo n.º 7
0
def test_function_with_no_args():
    env = PepEnvironment( PepCppRenderer() )

    fn = PepUserFunction( "myfn", PepType( PepVoid ), (), ( PepPass(), ) )
    rtfn = PepRuntimeUserFunction( fn, (), None )

    ans = env.renderer.render_exe( [rtfn], env )

    assert_multiline_equal( ans, """
void myfn();

void myfn()
{
}

int main( int argc, char* argv[] )
{
    myfn();

    return 0;
}
""" )
Exemplo n.º 8
0
def assert_rendered_program_equals(expected, code_input):
    env = PepEnvironment(PepCppRenderer())
    builtins.add_builtins(env)
    statements = parse_program(code_input)
    actual = env.render_exe(statements)
    assert_multiline_equal(expected, actual)
Exemplo n.º 9
0
def test_Render_runtime_method_call():
    env = PepEnvironment( PepCppRenderer() )
    add_builtins( env )

    env.namespace['a'] = PepVariable( PepType( PepInt ), "a" )

    cls = PepClass(
        PepSymbol( 'MyClass' ),
        (),
        (
            PepDefInit(
                (
                    ( PepSymbol('MyClass'), PepSymbol('self') ),
                    ( PepSymbol('int'), PepSymbol('x') ),
                ),
                ( PepPass(), )
            ),
            PepDef(
                PepSymbol('void'),
                PepSymbol('my_meth'),
                ( ( PepSymbol('MyClass'), PepSymbol('self') ), ),
                ( PepPass(), )
            )
        )
    ).evaluate( env )

    init = PepInit(
        PepSymbol( 'MyClass' ),
        PepSymbol( 'mc' ),
        PepFunctionCall( PepSymbol( 'MyClass.init' ), ( PepSymbol( "a" ), ) )
    ).evaluate( env )

    meth = PepFunctionCall( PepSymbol( "mc.my_meth" ), () ).evaluate( env )

    ans = env.renderer.render_exe( [ cls, init, meth ], env )

    assert_multiline_equal(
        """
struct MyClass
{
};

void MyClass_pep_c_pep_my_meth( MyClass& self );
void MyClass_pep_c_pep___init__( MyClass& self, int x );

void MyClass_pep_c_pep_my_meth( MyClass& self )
{
}

void MyClass_pep_c_pep___init__( MyClass& self, int x )
{
}

int main( int argc, char* argv[] )
{
    MyClass mc; MyClass_pep_c_pep___init__( mc, a );
    MyClass_pep_c_pep_my_meth( mc );

    return 0;
}
""",
        ans
    )
Exemplo n.º 10
0
def test_Render_runtime_class():
    env = PepEnvironment( PepCppRenderer() )
    add_builtins( env )

    env.namespace['a'] = PepVariable( PepType( PepInt ), "a" )

    cls = PepClass(
        PepSymbol( 'MyClass' ),
        (),
        (
            PepDefInit(
                (
                    ( PepSymbol('MyClass'), PepSymbol('self') ),
                    ( PepSymbol('int'), PepSymbol('a') ),
                    ( PepSymbol('float'), PepSymbol('b') )
                ),
                (
                    PepVar(
                        (
                            PepInit(
                                PepSymbol('int'),
                                PepSymbol('self.a'),
                                PepSymbol('a')
                            ),
                            PepInit(
                                PepSymbol('float'),
                                PepSymbol('self.b'),
                                PepSymbol('b')
                            )
                        )
                    ),
                )
            ),
        )
    ).evaluate( env )

    init = PepInit(
        PepSymbol( 'MyClass' ),
        PepSymbol( 'mc' ),
        PepFunctionCall(
            PepSymbol( 'MyClass.init' ),
            ( PepSymbol( 'a' ), PepFloat('1.5') )
        )
    ).evaluate( env )

    ans = env.renderer.render_exe( [ cls, init ], env )

    assert_multiline_equal(
        """
struct MyClass
{
    int a;
    double b;
};

void MyClass_pep_c_pep___init__( MyClass& self, int a, double b );

void MyClass_pep_c_pep___init__( MyClass& self, int a, double b )
{
    self.a = a;
    self.b = b;
}

int main( int argc, char* argv[] )
{
    MyClass mc; MyClass_pep_c_pep___init__( mc, a, 1.5 );

    return 0;
}
""",
        ans
    )
Exemplo n.º 11
0
def _assert_indent_dedent_generated(before, after):
    assert_multiline_equal(_tokens_2_string(_indent_dedent_token_string(before)), after)