DELINE/ENLINE (community effort) Part 2


This is Part 2 yes, the previous post was Part 1 but I did not call it that.

So now we proceed and make a first version for trying out, we add to
in #enum natives! after last entry the line


We also add the deline function to

deline: make native! [[
		"Converts string terminators to standard format, e.g. CRLF to LF. (Modifies)"
		value [string!] "The string to convert"
		/lines "return a block of lines"
			str [char! string!] "Character or string to represent new line terminator"
	#get-definition NAT_DELINE

Then we add code in natives.reds. At this moment we are not interested in making everything correct and functioning. Red/System code with all the added types and using pointers and fields within the declared struct!s can get pretty confusing resulting in many possible compile errors. For every compile error you need to adapt your code and open a new Rebol console, load the %red.r file and compile the console again. So I give a compiling version that does not do anything but as a starting point to work from.

	deline*: func [
		check?   [logic!]
		_with [integer!]									;-- with is reserved 
		lines [integer!]
		return: [c-string!]
			data 	[red-string!]
			buffer	[red-string!]
			val  	[red-value!]
			with-arg [red-value!]							;-- with char! or string! value
			char 	[red-char!]
			s	 	[series!]
			p		[byte-ptr!]
			len  	[integer!]
			unit 	[integer!]
			c1   	[integer!]
			blk		[red-block!]
			tail      [byte-ptr!]
			cnt       [integer!]
			size      [integer!]
		#typecheck [deline _with lines]
		data: as red-string! stack/arguments
		print lines print _with
		if positive? lines [
			print "lines refinement found"
			blk: as red-block! 0

			;return ""
			; To do: return a block of lines using some split functionality
		either positive? _with [
			print "with refinement found"
			val: as red-value! data + 1
			either TYPE_OF(val) = TYPE_CHAR [
				print "char type"
				;char: as red-char! val
				print val
			][  print "string type"
				print val
		][print "char" ]

		s:  GET_BUFFER(data)
		unit: GET_UNIT(s)
		p:	  string/rs-head data
		len:  string/rs-length? data
		tail: as byte-ptr! s/tail

		;buffer: string/rs-make-at stack/push* ( len + ( cnt * ( size - 1 ) ) ) * unit

		return ""

In the init function we also add the deline* function.

As you see, lot’s of commented code, too many unused variables, print statements are present. It is still a mess. But it compiles. And it gives some hints to how things work.

These are some results:

red>> deline/lines "hello"
1-1lines refinement foundchar== "hello"
red>> deline/with "hello" "a"
-11with refinement foundstring type02333EE8== "hello"
red>> deline/with "hello" #"a"
-11with refinement foundchar type02333EE8== "hello"

First thing to repair is that all native functions work without the RETURN. When a different type needs to be returned the type of the return variable is changed.
Examples are bool/header: TYPE_LOGIC or pair/header: TYPE_PAIR or f/header: TYPE_FLOAT.

So next is experimenting with returning a block! in case of the /lines refinement and a string! in all other cases.

But that I will post soon…

This entry was posted in Programmeren. Bookmark the permalink.

Geef een reactie

Your email address will not be published. Required fields are marked *

Vul dit nog even in: * Time limit is exhausted. Please reload CAPTCHA.