Arrays, lists, and records

63 views
Skip to first unread message

Andrew Knapp

unread,
Oct 22, 2017, 6:34:08 PM10/22/17
to ats-lang-users
I'd like to write something like the following code:

#include "share/atspre_staload.hats"
#include "share/atspre_define.hats"

typedef test = () -> int        (* better type later *)

typedef suite(data:t0ype,n:int) = @{
  name
= string,
  setup
= (data -> void),
  teardown
= (data -> void),
  tests
= @[test][n]
}

fun foo_setup
(a:int): void = println!("setting up")
fun foo_teardown
(a:int): void = println!("tearing down")

fun baz
(): int =
let
  val
() = println!("baz")
in
 
3
end

fun bar
(): int =
let
  val
() = println!("bar")
in
 
4
end

var foo_tests = @[test][2](bar, baz)
                                               
val foo
: suite(int,2) = @{
  name
= "hello world",
  setup
= foo_setup,
  teardown
= foo_teardown,
  tests
= foo_tests
}

implement main0
() =
let
in
  println
!("Hello, world!")
end

but the C compiler fails with these errors

modules_dats.c:262:1: warning: parameter names (without types) in function declaration
 atstype_funptr atstyarr_field
(atslab__tests) ;
 
^~~~~~~~~~~~~~
In file included from modules_dats.c:14:0:
/usr/lib/ats2-postiats-0.3.7/ccomp/runtime/pats_ccomp_typedefs.h:176:31: error: field atstyarr_field_undef declared as a function
 
#define atstyarr_field(fname) atstyarr_field_undef(fname)
                               
^
modules_dats
.c:262:16: note: in expansion of macro atstyarr_field
 atstype_funptr atstyarr_field
(atslab__tests) ;
               
^~~~~~~~~~~~~~
modules_dats
.c:269:1: warning: parameter names (without types) in function declaration
 atstype_funptr atstyarr_field
(atslab__tests) ;
 
^~~~~~~~~~~~~~
In file included from modules_dats.c:14:0:
/usr/lib/ats2-postiats-0.3.7/ccomp/runtime/pats_ccomp_typedefs.h:176:31: error: field atstyarr_field_undef declared as a function
 
#define atstyarr_field(fname) atstyarr_field_undef(fname)
                               
^
modules_dats
.c:269:16: note: in expansion of macro atstyarr_field
 atstype_funptr atstyarr_field
(atslab__tests) ;
               
^~~~~~~~~~~~~~
In file included from modules_dats.c:15:0:
modules_dats
.c: In function _057_home_057_andy_057_tmp_057_modules_057_modules_056_dats__dynload’:
modules_dats
.c:623:52: error: postiats_tyrec_0 {aka struct <anonymous>}’ has no member named atslab__tests’; did you mean atslab__setup’?
 
ATSINSstore_fltrec_ofs(statmp12, postiats_tyrec_1, atslab__tests, statmpref10) ;
                                                   
^
/usr/lib/ats2-postiats-0.3.7/ccomp/runtime/pats_ccomp_instrset.h:321:61: note: in definition of macro ATSINSstore_fltrec_ofs
 
#define ATSINSstore_fltrec_ofs(tmp, tyrec, lab, val) ((tmp).lab = val)
                                                             
^~

I would use viewtype lists instead of arrays, but the compiler complains about requiring fields to be of sort t0ype, not viewt0ype.

Thanks,
Andrew

gmhwxi

unread,
Oct 22, 2017, 8:29:43 PM10/22/17
to ats-lang-users

Unfortunately, this is not currently supported in ATS.

You need to go through C in order to create a value of type suite(a, n):

%{^
typedef
struct {
 
void *name,
 
void *setup,
 
void *teardown,
 
void *tests[0]
} suite    ;
%}


typedef
suite
(data:t0ype,n:int) =

$extype_struct
"suite" of {                                                                                                      
  name
= string,                                                                                                                
  setup
= (data -> void),                                                                                                        
  teardown
= (data -> void),                                                                                                    
  tests
= @[test][n]      
}
Reply all
Reply to author
Forward
0 new messages