Advertisement
snake5

stdlib.lispy v1

Jul 11th, 2017
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.39 KB | None | 0 0
  1.  
  2. (impfunc createStringFromBytes (ret string) (arg intptr) (arg u32))
  3. (impfunc sprintf (conv C)
  4.     (ret i32) // int
  5.     (arg intptr) // char* buf
  6.     (arg intptr) // const char* fmt
  7.     (vararg)
  8. )
  9. (impfunc memcpy (conv C)
  10.     (ret intptr) // void*
  11.     (arg intptr) // void* dst
  12.     (arg intptr) // const void* src
  13.     (arg intptr) // size_t size
  14. )
  15.  
  16. (class stringbuffer
  17.    
  18.     (var buffer (? (handle (array u8))))
  19.     (var size u32)
  20.    
  21.     (func getString (ret string) (body
  22.         (return (call createStringFromBytes (cast intptr buffer) (prop buffer size)))
  23.     ))
  24.    
  25.     (func reserve (ret void) (arg u32 sz) (body
  26.         (if (eq buffer null) (block
  27.             (set buffer (new (array u8) sz))
  28.             (return)
  29.         ))
  30.         (if (lt (prop buffer size) sz) (block
  31.             (var newsz (add size sz))
  32.             (var newbuf (new (array u8) newsz))
  33.             // TODO arrayCopy
  34.             (if buffer
  35.                 (call memcpy newbuf buffer (cast intptr size))
  36.             )
  37.             (set buffer newbuf)
  38.         ))
  39.     ))
  40.    
  41.     (func writeRaw (ret void) (arg intptr buf) (arg u32 sz) (body
  42.         (call reserve (add sz size))
  43.         (call memcpy (add (cast intptr buffer) (cast intptr size)) buf (cast intptr sz))
  44.         (set size (add size sz))
  45.     ))
  46.    
  47.     (func write (ret void) (arg string str) (body
  48.         (call writeRaw (prop str cStringPtr) (prop str size))
  49.     ))
  50.    
  51.     (func write (ret void) (arg i32 i32val) (body
  52.         (var buf (array u8 32))
  53.         (call writeRaw buf (call sprintf (cast intptr buf) (prop "%d" cStringPtr) i32val))
  54.     ))
  55. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement