コード例 #1
0
ファイル: testvalues.py プロジェクト: andybalaam/pepper
def test_unknown_initialisation_renders():
    env = PepEnvironment( PepCppRenderer() )
    add_builtins( env )
    env.namespace["unknown"] = PepVariable( PepType( PepInt ), "unknown" )
    init = PepInit( PepSymbol( "int" ), PepSymbol( "i" ),
        PepSymbol( "unknown" ) )
    assert_equal( init.render( env ), "int i = unknown" )
コード例 #2
0
ファイル: testvalues.py プロジェクト: andybalaam/pepper
def test_Unknown_variable_renders_as_symbol():
    env = PepEnvironment( PepCppRenderer() )
    env.namespace["myvariable"] = PepVariable( PepType( PepInt ), "myvariable" )

    value = PepSymbol( "myvariable" )

    assert_equal( value.render( env ), "myvariable" )
コード例 #3
0
ファイル: testvalues.py プロジェクト: andybalaam/pepper
def test_Variable_referring_to_known_int_renders_like_an_int():
    env = PepEnvironment( PepCppRenderer() )
    env.namespace["myvariable"] = PepInt( "23" )

    value = PepSymbol( "myvariable" )

    assert_equal( value.render( env ), "23" )
コード例 #4
0
ファイル: testvalues.py プロジェクト: andybalaam/pepper
def test_Add_Unknown_to_known_literal_renders_uncalculated_sum():
    env = PepEnvironment( PepCppRenderer() )
    env.namespace["input"] = PepVariable( PepType( PepInt ), "input" )

    value = PepPlus( PepInt( "4" ), PepSymbol( "input" ) )

    assert_equal( value.render( env ), "(4 + input)" )
コード例 #5
0
ファイル: testfunctions.py プロジェクト: andybalaam/pepper
def test_args_match_for_calculated_type():

    env = PepEnvironment( PepCppRenderer() )

    env.namespace["a"] = PepInt( "3" )
    env.namespace["b"] = PepString( "foo" )

    fndecl = PepUserFunction(
        "myfunc",
        PepType( PepVoid ),
        (
            ( PepType( PepInt ), PepSymbol( "x" ) ),
        ),
        (
            PepPass(),
        )
    )

    assert_true(
        fndecl.args_match(
            (
                PepSymbol( "a" ),
            ),
            env
        )
    )

    assert_false(
        fndecl.args_match(
            (
                PepSymbol( "b" ),
            ),
            env
        )
    )
コード例 #6
0
ファイル: testfunctions.py プロジェクト: andybalaam/pepper
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 )
{
}

""" )
コード例 #7
0
ファイル: testfunctions.py プロジェクト: andybalaam/pepper
def test_args_dont_match_error_when_they_do():
    env = PepEnvironment( PepCppRenderer() )

    env.namespace["a"] = PepInt( "3" )

    fndecl = PepUserFunction(
        "myfunc",
        PepType( PepVoid ),
        (
            ( PepType( PepInt ), PepSymbol( "x" ) ),
        ),
        (
            PepPass(),
        )
    )

    overload = PepFunctionOverloadList( [fndecl] )

    # Should throw since the args do match!
    overload.args_dont_match_error(
        fndecl,
        (
            PepSymbol( "a" ),
        ),
        env
    )
コード例 #8
0
ファイル: testfunctions.py プロジェクト: andybalaam/pepper
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);
}

""" )
コード例 #9
0
ファイル: testvalues.py プロジェクト: andybalaam/pepper
def test_symbol_find_namespace_and_name():

    class MyNsHolder( object ):
        def __init__( self ):
            self.namespace = PepNamespace()

        def evaluate( self, env ):
            return self

        def get_namespace( self ):
            return self.namespace

    env = PepEnvironment( PepCppRenderer() )

    a = MyNsHolder()
    env.namespace["a"] = a
    b = MyNsHolder()
    a.namespace["b"] = b

    (namespace, name, base_sym) = PepSymbol( "a.b.c" ).find_namespace_and_name(
        env )

    assert_equals( b.namespace, namespace )
    assert_equals( "c", name )
    assert_equals( "a.b", base_sym )
コード例 #10
0
ファイル: testvalues.py プロジェクト: andybalaam/pepper
def test_Unknown_inside_nested_plus_causes_whole_sum_to_be_uncalculated():
    env = PepEnvironment( PepCppRenderer() )
    env.namespace["input"] = PepVariable( PepType( PepInt ), "input" )

    value = PepPlus( PepInt( "4" ),
        PepPlus( PepInt( "5" ), PepSymbol( "input" ) ) )

    assert_equal( value.render( env ), "(4 + (5 + input))" )
コード例 #11
0
ファイル: run_snippet.py プロジェクト: andybalaam/pepper
def eval_program( prog ):
    env = PepEnvironment( PepCppRenderer() )
    builtins.add_builtins( env )

    res = None
    for ln in prog:
        res = env.render_value( ln.evaluate( env ) )

    return res
コード例 #12
0
ファイル: testfunctions.py プロジェクト: andybalaam/pepper
def test_Overloaded_functions_supply_correct_return_type_based_on_args():
    env = PepEnvironment( PepCppRenderer() )
    add_builtins( env )

    # Make a function taking ints, and another taking strings, with different
    # return types

    int_function = PepUserFunction(
        "int_function",
        PepType( PepFloat ),
        (
            ( PepSymbol( "int" ), PepSymbol( "a1" ) ),
            ( PepSymbol( "int" ), PepSymbol( "a2" ) ),
        ),
        ( PepPass(), )
    )

    string_function = PepUserFunction(
        "string_function",
        PepType( PepInt ),
        (
            ( PepSymbol( "string" ), PepSymbol( "b1" ) ),
            ( PepSymbol( "string" ), PepSymbol( "b2" ) ),
        ),
        ( PepPass(), )
    )

    # Make an overload list that consists of these 2 functions

    overload = PepFunctionOverloadList( [ int_function, string_function ] )

    # Set up some variables to use as arguments

    env.namespace["i1"] = PepInt( "3" )
    env.namespace["i2"] = PepInt( "4" )
    env.namespace["s1"] = PepString( "s1" )
    env.namespace["s2"] = PepString( "s2" )

    s_i1 = PepSymbol( "i1" )
    s_i2 = PepSymbol( "i2" )
    s_s1 = PepSymbol( "s1" )
    s_s2 = PepSymbol( "s2" )

    # This is what we are testing: ask for the overload's return type,
    # supplying arguments to disambiguate which function we really mean

    assert_equal(
        PepType( PepFloat ),
        overload.return_type( ( s_i1, s_i2 ), env )
    )

    assert_equal(
        PepType( PepInt ),
        overload.return_type( ( s_s1, s_s2 ), env )
    )
コード例 #13
0
ファイル: testvalues.py プロジェクト: andybalaam/pepper
def test_known_array_lookup():
    env = PepEnvironment( PepCppRenderer() )

    # int[] myarr = [3,4,5]
    # myarr[1]

    env.namespace["myarr"] = PepArray( PepType( PepInt ), (
        PepInt( "3" ), PepInt( "4" ), PepInt( "5" ), ) )

    value = now( PepArrayLookup( PepSymbol( "myarr" ), PepInt( "1" ) ) )

    assert_equal( value.render( env ), "4" )
コード例 #14
0
ファイル: testrendering.py プロジェクト: andybalaam/pepper
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;
}
""" )
コード例 #15
0
ファイル: testrendering.py プロジェクト: andybalaam/pepper
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;
}
""" )
コード例 #16
0
ファイル: testrendering.py プロジェクト: andybalaam/pepper
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;
}
""" )
コード例 #17
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)
コード例 #18
0
def test_Multiply_unknown():
    env = PepEnvironment(PepCppRenderer())
    env.namespace["x"] = PepVariable(PepType(PepInt), "x")
    value = PepTimes(PepSymbol("x"), PepInt("17"))
    assert_equal(value.render(env), "(x * 17)")
コード例 #19
0
ファイル: testrendering.py プロジェクト: andybalaam/pepper
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
    )
コード例 #20
0
ファイル: testrendering.py プロジェクト: andybalaam/pepper
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
    )
コード例 #21
0
ファイル: renderbuildstep.py プロジェクト: andybalaam/pepper
 def process( self, val ):
     env = PepEnvironment( PepCppRenderer() )
     builtins.add_builtins( env )
     return env.render_exe( val )