About the "ugliness" of C-style of ATS code

100 views
Skip to first unread message

gmhwxi

unread,
Apr 11, 2014, 4:02:59 PM4/11/14
to ats-lan...@googlegroups.com

ATS has a fairly sophisticated macro system, which, unfortunately, has barely been
documented.

Say I want to prettify Chiris Double's code:

https://github.com/doublec/openssl/blob/ats/ssl/d1_both.dats

I can first introduce the following macros:

macdef int(x) = $UN.cast2int(,(x))
macdef ucharptr_succ
(p) = ptr0_succ<char> (,(p))
macdef ucharptr_get
(p) = $UN.ptr0_get<uchar> (,(p))
macdef ucharptr_set
(p, c) = $UN.ptr0_set<uchar> (,(p), ,(c))


Instead of writing $UN.cast2int(payload), I can write (int)payload, which a C programmer
would probably prefer.

Similarly, a C programmer probably prefers ucharptr_set(p, c) over $UN.ptr0_set<uchar> (p, c).

Actually, n2s and s2n in Chris's code can take advantage of the support for call-by-reference
in ATS:

fun n2s (p: &ptr >> _): usint
fun s2n (p: &ptr >> _, x: usint): void

We can have the following if p is a left-value:

macdef ucharptr_incby1 (p) = ,(p) := ptr0_succ<char> (,(p))


Yannick Duchêne

unread,
May 9, 2015, 10:20:38 PM5/9/15
to ats-lan...@googlegroups.com
Please, how to understand the commas in these definitions? Why are there commas which seems to separate blanks? Also, what is the reasons of the seemingly unnecessary parentheses around what looks to be atomic expressions? Is this for the same reason as with C defines, which is to avoid issue in case the expression is composed with operators which could cause precedence issues?

Hongwei Xi

unread,
May 9, 2015, 10:43:35 PM5/9/15
to ats-lan...@googlegroups.com
In Lisp, there is so-called backquote-comma-notation for defining macros. ATS borrowed it.

macdef foo(x) = ,(x) + ,(x) // short form

is shorthand fo

macrodef foo(x) = `(,(x) + ,(x)) // long form

`(...) means to treat whatever inside literally; ,(...) cancels the effect of `(...). Search for backquote-comma-notation,
and you should find plenty explanation.

--
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/9d254cb6-8705-4315-a28a-d41351186687%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages