Open master-q opened 5 years ago
C language header should be translated into:
A. sats
, or
B. hats
which includes sats
?
If there are static functions in a C language header, we should choose the choice B.
Or, we only need hats
files, which define function with extern
?
Umm...
"What does hats include?" https://gist.github.com/master-q/b65918409d99aa40e4a6acbd865fc7eb
hats
file does...
#include "foo.dats"
#include "foo.hats"
#staload FOO = "foo.dats"
#staload FOO = "foo.sats"
staload FOO = "foo.sats"
staload _ = "foo.dats"
I believe hats
including hats
is a bad manner.
static __inline
can be translated into macdef
?
No. The macdef
is for short alias.
I think libats/ML/DATS/string.dats
style is the standard to translate static __inline
.
$ cat doc/EXAMPLE/ATSLIB/libats_ML_string.dats
// --snip--
staload "libats/ML/SATS/string.sats"
(* ****** ****** *)
//
staload _(*anon*) = "libats/ML/DATS/array0.dats"
staload _(*anon*) = "libats/ML/DATS/string.dats"
// --snip--
$ cat libats/ML/SATS/string.sats
// --snip--
fun
{a:t0p}
string_list0_map
(cs: string, fopr: cfun(charNZ, a)): list0(a)
// --snip--
$ cat libats/ML/DATS/string.dats
// --snip--
implement
{a}(*tmp*)
string_list0_map
(cs, fopr) = let
//
fun
loop
(
p0: ptr, res: &ptr? >> list0(a)
) : void = let
val c0 =
$UN.ptr0_get<char>(p0)
in
// --snip--
On this issue, I focus to convert a C header to a sats file.
casper$ cat regress/inc/hello_world/main.c
#include "puts.h"
int main() {
puts("Hello World");
return 0;
}
casper$ cat regress/inc/hello_world/puts.h
#ifndef _PUTS_H_
#define _PUTS_H_
extern int puts (const char *__s);
#endif /* !_PUTS_H_ */
should be translated into following:
#include "share/atspre_staload.hats"
staload UN = "prelude/SATS/unsafe.sats"
extern fun i9a_puts (string): int = "mac#puts"
implement main () =
let
val _ = i9a_puts "Hello World"
in
0
end
But... who decide to understand string
or chat@l | ptr l
?
Fact 1.
If main
function applies CStrConst
to the puts
function, the puts
should have string
argument.
Fact 2.
strptr
or strnptr
for nonsense on this puts
function, because the user main
function doesn't create the other memory space for string literal.
https://pawoo.net/@masterq/102592139606375737
Conclusion: Decide the type with following steps:
I should apply...
mac#
for C functions implemented in libraryext#
for ATS functions called by C codeHow to know the functions are implemented in C or ATS?
Option:
If we don't include any C language headers, we can export ATS functions using ext#
.
It means we should create own prelude without C headers.
Umm... How about _ATS_CCOMP_PRELUDE_NONE_
define deletes all C language headers included...
ATS2's prelude depends on libc such as stdio.h
.
Need better patch:
--- a/src/Language/Idiomaticca.hs
+++ b/src/Language/Idiomaticca.hs
@@ -270,6 +270,9 @@ makeFunc fname (args, unis) body ret = do
iEnvRecordFun fname
-- Introduce `var` on args
let body' = makeArgs args body
+ body'' = case body' of
+ Nothing -> Just $ A.StringLit ("\"" ++ "mac#" ++ fname ++ "\"")
+ _ -> body'
return $ A.Func dummyPos
(A.Fun A.PreF { A.fname = A.Unqualified fname
, A.sig = Just ""
@@ -278,7 +281,7 @@ makeFunc fname (args, unis) body ret = do
, A.args = fmap reverse args
, A.returnType = ret
, A.termetric = Nothing
- , A._expression = body'
+ , A._expression = body''
})
-- | Implement ATS function.
Choose mac#
and fix the error caused by this choice:
https://github.com/metasepi/idiomaticca/issues/48
Should use
parseCFile
instead ofparseCFilePre
?