Advertisement
snake5

SGScript - recent API updates

May 25th, 2013
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -----------------------------------------------------------
  2. I/O API
  3. -----------------------------------------------------------
  4. include_library("io");
  5.  
  6. printlns
  7. (
  8.     "FILE:         " $ ( f = io_file( "output.dir", FILE_READ ) ),
  9.     "REOPEN:       " $ f.open( "tmp", FILE_READ | FILE_WRITE ),
  10.     "SETBUF:       " $ f.setbuf( 123 ),
  11.     "SIZE BEFORE:  " $ f.size,
  12.     "WRITE 'test': " $ f.write( "test" ),
  13.     "FLUSH:        " $ f.flush(),
  14.     "SIZE AFTER:   " $ f.size,
  15.     "SEEK -1,end:  " $ f.seek( -1, SEEK_END ),
  16.     "SEEK -1,cur:  " $ f.seek( -1, SEEK_CUR ),
  17.     "TELL:         " $ f.offset,
  18.     "SEEK 1,set:   " $ f.seek( 1 ),
  19.     "READ:         " $ f.read( 4 ),
  20.     "ERROR?:       " $ f.error,
  21.     "EOF:          " $ f.eof,
  22.     "CLOSE:        " $ f.close(),
  23.     "CLEANUP:      " $ io_file_delete( "tmp" )
  24. );
  25.  
  26. foreach( t, i : io_dir( "." ) )
  27.     printvars( t, i );
  28.  
  29. -----------------------------------------------------------
  30. SGSVM [SGScript v0.8.2]
  31. FILE:         file
  32. REOPEN:       true
  33. SETBUF:       true
  34. SIZE BEFORE:  0
  35. WRITE 'test': true
  36. FLUSH:        true
  37. SIZE AFTER:   4
  38. SEEK -1,end:  true
  39. SEEK -1,cur:  true
  40. TELL:         2
  41. SEEK 1,set:   true
  42. READ:         est
  43. ERROR?:       false
  44. EOF:          true
  45. CLOSE:        true
  46. CLEANUP:      true
  47. bool (false)
  48. string [1] "."
  49. bool (false)
  50. string [2] ".."
  51. bool (true)
  52. string [7] "cat.txt"
  53. bool (true)
  54. string [8] "fapi.sgs"
  55. bool (true)
  56. string [7] "fmt.sgs"
  57. bool (true)
  58. string [10] "misapi.sgs"
  59. bool (true)
  60. string [9] "osapi.sgs"
  61.  
  62. -----------------------------------------------------------
  63. Formatting API
  64. -----------------------------------------------------------
  65.  
  66. include_library( "string" );
  67. include_library( "fmt" );
  68.  
  69. function hexit( data )
  70. {
  71.     tbl = "0123456789ABCDEF";
  72.     out = "";
  73.     for( i = 0; i < data.length; ++i )
  74.     {
  75.         if( out.length )
  76.             out $= " ";
  77.         cc = string_charcode( data, i );
  78.         out $= tbl[ cc >> 4 ];
  79.         out $= tbl[ cc & 15 ];
  80.     }
  81.     return out;
  82. }
  83.  
  84. printlns
  85. (
  86.     "",
  87.     "3s4c : numbers,51,52,53,54 = " $ fmt_pack( "3s4c", "numbers", 51, 52, 53, 54 ),
  88.     "+REV = " $ fmt_unpack( "3s4c", fmt_pack( "3s4c", "numbers", 51, 52, 53, 54 ) ),
  89.     "",
  90.     "w : 1024 = " $ hexit( fmt_pack( "w", 1024 ) ),
  91.     "+REV = " $ fmt_unpack( "w", fmt_pack( "w", 1024 ) ),
  92.     "",
  93.     " fxd : 13.37, 13.37 = " $ hexit( fmt_pack( "fxd", 13.37, 13.37 ) ),
  94.     "+REV = " $ fmt_unpack( "fxd", fmt_pack( "fxd", 13.37, 13.37 ) ),
  95.     "",
  96.     ">fxd : 13.37, 13.37 = " $ hexit( fmt_pack( ">fxd", 13.37, 13.37 ) ),
  97.     "+REV = " $ fmt_unpack( ">fxd", fmt_pack( ">fxd", 13.37, 13.37 ) ),
  98.     "",
  99.     "b64 Man = " $ fmt_base64_encode( "Man" ),
  100.     "b64 A = " $ fmt_base64_encode( "the red fo" ),
  101.     "b64 B = " $ fmt_base64_encode( "the red fox" ),
  102.     "b64 C = " $ fmt_base64_encode( "the red foxx" ),
  103.     "b64 +R A = " $ fmt_base64_decode( fmt_base64_encode( "the red fo" ) ),
  104.     "b64 +R B = " $ fmt_base64_decode( fmt_base64_encode( "the red fox" ) ),
  105.     "b64 +R C = " $ fmt_base64_decode( fmt_base64_encode( "the red foxx" ) )
  106. );
  107.  
  108. -----------------------------------------------------------
  109. SGSVM [SGScript v0.8.2]
  110.  
  111. 3s4c : numbers,51,52,53,54 = num3456
  112. +REV = [num,51,52,53,54]
  113.  
  114. w : 1024 = 00 04
  115. +REV = [1024]
  116.  
  117.  fxd : 13.37, 13.37 = 85 EB 55 41 00 3E 0A D7 A3 70 BD 2A 40
  118. +REV = [13.37,13.37]
  119.  
  120. >fxd : 13.37, 13.37 = 41 55 EB 85 00 40 2A BD 70 A3 D7 0A 3E
  121. +REV = [13.37,13.37]
  122.  
  123. b64 Man = TWFu
  124. b64 A = dGhlIHJlZCBmbw==
  125. b64 B = dGhlIHJlZCBmb3g=
  126. b64 C = dGhlIHJlZCBmb3h4
  127. b64 +R A = the red fo
  128. b64 +R B = the red fox
  129. b64 +R C = the red foxx
  130.  
  131. -----------------------------------------------------------
  132. Iterable API
  133. -----------------------------------------------------------
  134.  
  135. a1 = [1,3,5];
  136. a2 = [2,4,6];
  137. a3 = [7,8,9];
  138. a4 = [1,2,3,4,2,5,4];
  139. d1 = { a = "b", c = "d" };
  140. printvars
  141. (
  142.     get_concat( a1, a2, a3 ),
  143.     get_merged( d1, a4, a1 ),
  144.     get_keys( d1 ),
  145.     get_values( d1 ),
  146.     a1.find( 3 ),
  147.     a1.find( "3" ),
  148.     a1.find( "3", true ),
  149.     clone(a4).remove("2",false,true)
  150. );
  151.  
  152. -----------------------------------------------------------
  153. SGSVM [SGScript v0.8.2]
  154. object (004CE7C0) [2] array
  155. [
  156.   int (1)
  157.   int (3)
  158.   int (5)
  159.   int (2)
  160.   int (4)
  161.   int (6)
  162.   int (7)
  163.   int (8)
  164.   int (9)
  165. ]
  166. object (004CE820) [2] dict
  167. {
  168.   a = string [1] "b"
  169.   c = string [1] "d"
  170.   0 = int (1)
  171.   1 = int (3)
  172.   2 = int (5)
  173.   3 = int (4)
  174.   4 = int (2)
  175.   5 = int (5)
  176.   6 = int (4)
  177. }
  178. object (004CE880) [2] array
  179. [
  180.   string [1] "a"
  181.   string [1] "c"
  182. ]
  183. object (004CE8E0) [2] array
  184. [
  185.   string [1] "b"
  186.   string [1] "d"
  187. ]
  188. int (1)
  189. int (1)
  190. null
  191. int (2)
  192.  
  193. -----------------------------------------------------------
  194. O/S API
  195. -----------------------------------------------------------
  196.  
  197. include_library( "string" );
  198. include_library( "os" );
  199.  
  200. formats = string_explode( "a,A,b,B,c,x,X,Z,U,W,C,d,e,F,H,I,j,m,M,p,R,S,T,u,w,y,Y,f,t,%", "," );
  201. dateproc = {};
  202. foreach( fmt : formats )
  203.     dateproc[ fmt ] = os_date_string( "%" $ fmt );
  204.  
  205. printlns
  206. (
  207.     "OS type = " $ os_gettype(),
  208.     "OS time =       " $ os_time(),
  209.     "OS time +2 =    " $ os_time(2),
  210.     "OS time +2:30 = " $ os_time(2.5),
  211.     "OS time +3 =    " $ os_time(3),
  212.     "",
  213.     "timezone (R) =  " $ os_get_timezone(),
  214.     "timezone (S) =  " $ os_get_timezone(true),
  215.     "",
  216.     "date formats >", dumpvar( dateproc ),
  217.     "",
  218.     "parsed time >", dumpvar( os_parse_time() ),
  219.     "",
  220.     "made time >", dumpvar( os_make_time( 30, 30, 12, 25, 5, 2013 ) )
  221. );
  222.  
  223. -----------------------------------------------------------
  224. SGSVM [SGScript v0.8.2]
  225. OS type = Windows
  226. OS time =       1369508130
  227. OS time +2 =    1369504530
  228. OS time +2:30 = 1369506330
  229. OS time +3 =    1369508130
  230.  
  231. timezone (R) =  3
  232. timezone (S) =  +03:00
  233.  
  234. date formats >
  235. object (005D95C8) [1] dict
  236. {
  237.   a = string [3] "Sat"
  238.   A = string [8] "Saturday"
  239.   b = string [3] "May"
  240.   B = string [3] "May"
  241.   c = string [17] "05/25/13 21:55:30"
  242.   x = string [8] "05/25/13"
  243.   X = string [8] "21:55:30"
  244.   Z = string [17] "FLE Daylight Time"
  245.   U = string [2] "20"
  246.   W = string [2] "20"
  247.   C = string [2] "20"
  248.   d = string [2] "25"
  249.   e = string [2] "25"
  250.   F = string [10] "2013-05-25"
  251.   H = string [2] "21"
  252.   I = string [2] "09"
  253.   j = string [3] "145"
  254.   m = string [2] "05"
  255.   M = string [2] "55"
  256.   p = string [2] "PM"
  257.   R = string [5] "21:55"
  258.   S = string [2] "30"
  259.   T = string [8] "21:55:30"
  260.   u = string [1] "6"
  261.   w = string [1] "6"
  262.   y = string [2] "13"
  263.   Y = string [4] "2013"
  264.   f = string [19] "2013-05-25_21-55-30"
  265.   t = string [10] "1369508130"
  266.   % = string [1] "%"
  267. }
  268.  
  269. parsed time >
  270. object (005DBEE0) [1] dict
  271. {
  272.   year = int (2013)
  273.   month = int (5)
  274.   day = int (25)
  275.   weekday = int (6)
  276.   yearday = int (145)
  277.   hours = int (21)
  278.   minutes = int (55)
  279.   seconds = int (30)
  280. }
  281.  
  282. made time >
  283. int (1369477830)
  284.  
  285. -----------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement