]> rethought.computer Git - sorel-lang.git/commitdiff
implement memcopy in stdlib keep/f293ae63c4afd29d62db4b689bd13c49437121ce
authorBryan English <bryan@rethought.computer>
Mon, 9 Feb 2026 14:32:20 +0000 (09:32 -0500)
committerBryan English <bryan@rethought.computer>
Tue, 10 Feb 2026 04:08:55 +0000 (04:08 +0000)
rel-lang/stdlib/mem.sorel
rel-lang/tests/test1.sorel

index da5bfca6f505be6cd869055f5e7d30bf52dd7d0b..5ed53c862af5d5484c97caf3099aef5bfacb5024 100644 (file)
@@ -42,3 +42,33 @@ export free
   dealloc
 ; 
 
+import "std:out"
+
+: copy_byte ( addr_from addr_to -- addr_from addr_to )
+  swap \ ( addr_to addr_from )
+  dup \ ( addr_to addr_from addr_from )
+  @:8 \ ( addr_to addr_from byte )
+  rot \ ( addr_from byte addr_to )
+  dup \ ( addr_from byte addr_to addr_to)
+  rot \ ( addr_from addr_to addr_to byte )
+  swap \ ( addr_from addr_to byte addr_to )
+  !:8 \ ( addr_from addr_to )
+;
+
+export memcopy
+: memcopy ( addr_from addr_to count -- )
+  dup \ ( addr_from addr_to count count )
+  loop \ ( addr_from addr_to count )
+    rot \ ( addr_to count addr_from )
+    rot \ ( count addr_from addr_to )
+    copy_byte \ ( count addr_from addr_to )
+    1 + \ ( count addr_from addr_to+1 )
+    rot \ ( addr_from addr_to+1 count )
+    1 - \ ( addr_from addr_to+1 count-1 )
+    rot \ ( addr_to+1 count-1 addr_from )
+    1 + \ ( addr_to+1 count-1 addr_from+1 )
+    rot rot \ ( addr_from+1 addr_to+1 count-1 )
+    dup \ ( addr_from+1 addr_to+1 count-1 count-1)
+  endloop \ ( addr_from+count addr_to+count 0 )
+  drop drop drop \ ( )
+;
index 8ba74253a0b6da646547326322281fb1fe2d7319..325662510e825c6478225a2c888c72f1264146d4 100644 (file)
@@ -16,3 +16,21 @@ free
 
 0 assert
 
+import "std:string"
+import "std:mem"
+import "std:out"
+
+extern putstack
+
+16 alloc \ ( ptr )
+dup \ ( ptr ptr )
+"To be copied!" \ ( ptr ptr strptr )
+dup \ ( ptr ptr strptr strptr )
+strlen 1 + \ ( ptr ptr strptr len )
+rot \ ( ptr strptr len ptr )
+swap \ ( ptr strptr ptr len )
+memcopy \ ( ptr )
+puts \ ( ptr )
+free \ ( )
+
+