string from a char

62 views
Skip to first unread message

Chotu S

unread,
Jul 2, 2014, 8:56:18 AM7/2/14
to ats-lan...@googlegroups.com
I know how to make a string from a given char , but I need to know how to do it in ATS without using C snippets.


Say I have function make_string that takes a char and return a string :

fun make_string (c : char) : string

This function return empty string when c = 0

Example : make_string ('a') => "a"
                make_string ('\0') => ""


In particular I want to make use of  "string_sing" inside the function.

I am unable to compile the above function even when I take '\0' into account.


Thanks


Brandon Barker

unread,
Jul 2, 2014, 8:58:57 AM7/2/14
to ats-lang-users
Could you include your code? I'm not sure what is meant by your last statement about '\0'.

Brandon Barker
brandon...@gmail.com


--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Visit this group at http://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/CAAZxu4yOBaEb6ZJze6NK-_So93LxQCMAZAV-bPB02-fS8TvOmw%40mail.gmail.com.

Chotu S

unread,
Jul 2, 2014, 9:11:25 AM7/2/14
to ats-lan...@googlegroups.com
I meant something like this :

fun make_string (c : char) : string =
  if c = '\0' then ""
  else strnptr2string (string_sing (c))




gmhwxi

unread,
Jul 2, 2014, 9:17:59 AM7/2/14
to ats-lan...@googlegroups.com
string_sing can only be applied to a non-zero char:

fun make_string (c: char) = let
  val c = g1ofg0 (c)
in
  if iseqz (c) then "" else strnptr2string(string_sing(c))
end

Chotu S

unread,
Jul 2, 2014, 9:37:30 AM7/2/14
to ats-lan...@googlegroups.com
So main problem was forgetting to convert type  to dependent type  i.e char to char (c) .


Chotu S

unread,
Jul 2, 2014, 10:38:52 AM7/2/14
to ats-lan...@googlegroups.com
Now the code passes the type checker , but unfortunately I am still getting compiler errors.

implement main0 () = {
  val c = '\0'
  val s = make_string (c)
}

this does not compile and give me an error (I used to get this kind of error a lot , but have forgotten how I fixed it before , may be something to do with size or template )

Thanks



On Wed, Jul 2, 2014 at 6:47 PM, gmhwxi <gmh...@gmail.com> wrote:

gmhwxi

unread,
Jul 2, 2014, 10:44:32 AM7/2/14
to ats-lan...@googlegroups.com
Did you have the following line at the top:

#include "share/atspre_staload.hats"

gmhwxi

unread,
Jul 2, 2014, 11:04:21 AM7/2/14
to ats-lan...@googlegroups.com
I noted that string_sing was not implemented. I just fixed it.

implement
{}(*tmp*)
string_sing
(c) = let
  val
(pfat, pfgc | p) = malloc_gc (i2sz(2))
  val
((*void*)) = $UN.ptr0_set<char> (p, c)
  val
((*void*)) = $UN.ptr0_set_at<char> (p, 1, '\000')
in
  $UN
.castvwtp0{strnptr(1)}((pfat, pfgc | p))
end // end of [string_sing]

Chotu S

unread,
Jul 2, 2014, 11:14:31 AM7/2/14
to ats-lan...@googlegroups.com
Great , thanks for fixing it , I'll  update my copy soon.



Reply all
Reply to author
Forward
0 new messages