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; } """ )
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; } """ )
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; } """ )
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)
def process( self, val ): env = PepEnvironment( PepCppRenderer() ) builtins.add_builtins( env ) return env.render_exe( val )