Advertisement
snake5

SGScript -> C

Jul 18th, 2014
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.83 KB | None | 0 0
  1. //// IN
  2. a = 5;
  3. function f() use( a )
  4. {
  5.     a = 10;
  6. }
  7. f();
  8. println( a );
  9.  
  10. //// OUT
  11. #include <sgscript.h>
  12.  
  13. static int Fsgs__f( SGS_CTX, sgs_VarObj* obj )
  14. {
  15.     int ret = 0;
  16.     SGSFN( "f" );
  17.     sgs_Variable* CN = (sgs_Variable*) obj->data;
  18.     // setclsr
  19.     sgs_ClSetItem( C, 0, CN+0 );
  20.     // ---
  21.     _ret:
  22.     return ret;
  23. }
  24.  
  25. static int Dsgs__f( SGS_CTX, sgs_VarObj* obj )
  26. {
  27.     sgs_ReleaseArray( C, (sgs_Variable*) obj->data, 1 );
  28. }
  29.  
  30. static int Gsgs__f( SGS_CTX, sgs_VarObj* obj )
  31. {
  32.     sgs_GCMarkArray( C, (sgs_Variable*) obj->data, 1 );
  33. }
  34.  
  35. static sgs_ObjInterface Tsgs__f[1] =
  36. {{
  37.     "func",
  38.     Dsgs__f, Gsgs__f,
  39.     NULL, NULL,
  40.     NULL, NULL, NULL, NULL,
  41.     Fsgs__f, NULL
  42. }};
  43.  
  44. static int Isgs__f( SGS_CTX, sgs_Variable* out )
  45. {
  46.     sgs_Variable* CN = (sgs_Variable*)
  47.         sgs_InitObjectIPA( C, out, sizeof(sgs_Variable) * 1, Tsgs__f );
  48.     sgs_InitInt( CN+0, 10LL );
  49. }
  50.  
  51. int sgscript_main( SGS_CTX )
  52. {
  53.     int ret = 0;
  54.     SGSFN( "<main>" );
  55.     sgs_Variable T[ 3 ];
  56.     sgs_Variable CN[ 4 ];
  57.     { int i; for( i = 0; i < 3; ++i )
  58.         T[ i ].type = SGS_VT_NULL; }
  59.     sgs_InitInt( CN+0, 5LL );
  60.     Isgs__f( C, CN+1 );
  61.     sgs_InitStringBuf( C, CN+2, "f", 1 );
  62.     sgs_InitStringBuf( C, CN+3, "println", 7 );
  63.     // genclsr
  64.     sgs_ClPushNulls( C, 1 );
  65.     // setclsr
  66.     sgs_ClSetItem( C, 0, CN+0 );
  67.     // pushclsr
  68.     sgs_ClPushItem( C, 0 );
  69.     // makeclsr
  70.     sgs_MakeClosure( C, CN+1, 1, T+0 );
  71.     // setvar
  72.     sgs_SetGlobalPP( C, CN+2, T+0 );
  73.     // getvar
  74.     sgs_Release( C, T+0 );
  75.     sgs_GetGlobalPP( C, CN+2, T+0 );
  76.     // call
  77.     sgs_FCallP( C, T+0, 0, 0, 0 );
  78.     // getvar
  79.     sgs_Release( C, T+1 );
  80.     sgs_GetGlobalPP( C, CN+3, T+1 );
  81.     // getclsr
  82.     sgs_Release( C, T+2 );
  83.     sgs_ClGetItem( C, 0, T+2 );
  84.     // set
  85.     sgs_Assign( C, T+0, T+2 );
  86.     // call
  87.     sgs_PushVariable( C, T+0 );
  88.     sgs_FCallP( C, T+1, 1, 0, 0 );
  89.     // ---
  90.     _ret:
  91.     sgs_ReleaseArray( C, T, 3 );
  92.     sgs_ReleaseArray( C, CN, 4 );
  93.     return ret;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement