Initial snarf.
[shack.git] / naml / make_tast_type.pl
blob34895ad2d4ac58ed0bb4071e58725afd3e2ab1e2
1 #!/usr/bin/perl
2 # utility to convert a string representing an ocaml type to a string representing the Tast type construct
3 # currently does only very very simple types, and makes a lot of assumptions
5 use strict;
7 my %typesub = (
8 'int' => 'TyInt',
9 'char' => 'TyChar',
10 'float' => 'TyFloat',
11 'exn' => 'TyExn',
12 'string' => 'TyArray TyChar',
13 'bool' => 'bool_ty',
14 'unit' => 'unit_ty'
17 sub do_base ($)
19 local $_ = shift;
20 if (/^(.*)array$/)
22 return "TyArray " . do_base($1)
24 elsif (exists $typesub{$_})
26 return $typesub{$_}
28 else
30 return $_
34 sub do_tuple ($)
36 local $_ = shift;
37 my @t = split /\*/;
38 if (@t > 1)
40 return 'TyTuple [' . join('; ', map { do_base $_ } @t) . ']';
42 else
44 return do_base @t[0];
48 sub do_fun ($)
50 local $_ = shift;
51 my @t = split /->/;
52 if (@t > 1)
54 my $ret = pop @t;
55 my @args = map { do_tuple $_ } @t;
56 return 'TyFun ([' . join('; ', @args) . '], ' . do_tuple($ret) . ')';
58 else
60 return do_tuple @t[0];
64 sub make_type ($)
66 local $_ = shift;
67 s/\s//g; # it just gets in the way, and we don't support constructed types
68 $_ = do_fun($_);
69 my $a = 'a';
70 while (/'$a/)
72 s/'$a/$a/g;
73 $_ = "let $a = TyVar (new_symbol_string \"$a\") in $_";
74 $a++;
76 return $_;
79 if (@ARGV)
81 print make_type("@ARGV"), "\n";
82 exit;
85 while (<>)
87 chomp;
88 print make_type($_), "\n";