metasepi / idiomaticca

Translate IDIOMATIC C into human-readable ATS
http://metasepi.org/
GNU Affero General Public License v3.0
3 stars 0 forks source link

Call puts function using C headers #30

Open master-q opened 5 years ago

master-q commented 4 years ago

Should use parseCFile instead of parseCFilePre?

master-q commented 4 years ago

C language header should be translated into:

A. sats, or B. hats which includes sats?

master-q commented 4 years ago

If there are static functions in a C language header, we should choose the choice B.

master-q commented 4 years ago

Or, we only need hats files, which define function with extern?

master-q commented 4 years ago

Umm...

"What does hats include?" https://gist.github.com/master-q/b65918409d99aa40e4a6acbd865fc7eb

master-q commented 4 years ago

hats file does...

master-q commented 4 years ago

I believe hats including hats is a bad manner.

master-q commented 4 years ago

static __inline can be translated into macdef?

master-q commented 4 years ago

No. The macdef is for short alias.

master-q commented 4 years ago

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--
master-q commented 4 years ago

On this issue, I focus to convert a C header to a sats file.

master-q commented 4 years ago
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
master-q commented 4 years ago

But... who decide to understand string or chat@l | ptr l?

master-q commented 4 years ago

Fact 1. If main function applies CStrConst to the puts function, the puts should have string argument.

master-q commented 4 years ago

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.

master-q commented 4 years ago

https://pawoo.net/@masterq/102592139606375737

Conclusion: Decide the type with following steps:

  1. Apply simple at-views to pointers with reading C code
  2. Switch the at-views in function signature to higher order type with watching usage of the functions
  3. Change the function body fits the function signature, when the signature was changed
master-q commented 4 years ago

I should apply...

  1. mac# for C functions implemented in library
  2. ext# for ATS functions called by C code
  3. no post fix for the other functions
master-q commented 4 years ago

How to know the functions are implemented in C or ATS?

master-q commented 4 years ago

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.

master-q commented 4 years ago

Umm... How about _ATS_CCOMP_PRELUDE_NONE_ define deletes all C language headers included...

master-q commented 4 years ago

ATS2's prelude depends on libc such as stdio.h.

master-q commented 4 years ago

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.
master-q commented 4 years ago

Choose mac# and fix the error caused by this choice: https://github.com/metasepi/idiomaticca/issues/48