Now I have added the support for pattern matching.
Here is an implementation of the list-length function in python
that is generated from some ATS source:
import sys
sys.path.append(
‘/home/hwxi/research/Postiats-contrib/git/projects/MEDIUM/ATS-parse-emit/Python/prelude/CATS’
)
from integer_cats import *
(* ****** ****** *)
def nil(): return None
def cons(x, xs): return (x, xs)
def ATSCKptrisnil(xs): return (xs == None)
def ATSCKptriscons(xs): return (xs != None)
(* ****** ****** *)
extern
fun
listlen
: {a:t@ype} List0 (a) → int = “ext#”
//
(* ****** ****** *)
implement
listlen{a}
(xs) = let
//
prval () = lemma_list_param (xs)
//
fun
loop{i,j:nat} ..
(
xs: list (a, i), res: int(j)
) : int(i+j) = let
in
//
case+ xs of
| list_nil () => res | list_cons (_, xs) => loop (xs, res+1)
//
end // end of [loop]
//
in
loop (xs, 0)
end // end of [intlistlen]
def listlen(arg0):
tmpret0 = None
funlab_py = None
tmplab_py = None
#__patsflab_listlen
tmpret0 = loop_1(arg0, 0)
return tmpret0
def loop_1(arg0, arg1):
apy0 = None
apy1 = None
tmpret1 = None
tmp3 = None
tmp4 = None
funlab_py = None
tmplab_py = None
mbranch_1 = None
def __atstmplab0():
nonlocal arg0, arg1
nonlocal apy0, apy1, tmpret1, tmp3, tmp4
nonlocal funlab_py, tmplab_py
nonlocal mbranch_1
tmplab_py = 0
if (ATSCKptriscons(arg0)): tmplab_py = 4 ; return#__atstmplab3
__atstmplab1()
return
def __atstmplab1():
nonlocal arg0, arg1
nonlocal apy0, apy1, tmpret1, tmp3, tmp4
nonlocal funlab_py, tmplab_py
nonlocal mbranch_1
tmplab_py = 0
tmpret1 = arg1
return
def __atstmplab2():
nonlocal arg0, arg1
nonlocal apy0, apy1, tmpret1, tmp3, tmp4
nonlocal funlab_py, tmplab_py
nonlocal mbranch_1
tmplab_py = 0
__atstmplab3()
return
def __atstmplab3():
nonlocal arg0, arg1
nonlocal apy0, apy1, tmpret1, tmp3, tmp4
nonlocal funlab_py, tmplab_py
nonlocal mbranch_1
tmplab_py = 0
tmp3 = arg0[1]
tmp4 = atspre2py_add_int1_int1(arg1, 1)
#tailcal_beg
apy0 = tmp3
apy1 = tmp4
arg0 = apy0
arg1 = apy1
funlab_py = 1 #__patsflab_loop_1
#tailcal_end
return
mbranch_1 = { 1: __atstmplab0, 2: __atstmplab1, 3: __atstmplab2, 4:
__atstmplab3 }
while (1):
funlab_py = 0
#__patsflab_loop_1
#ATScaseof_beg
tmplab_py = 1
while (1):
mbranch_1.get(tmplab_py)()
if (tmplab_py == 0): break
#ATScaseof_end
if (funlab_py == 0): break
return tmpret1
(* ****** ****** *)
xs = cons(1, cons(2, cons(3, nil())))
print(“listlen(”, xs, “) =”, listlen(xs))
(* ****** ****** *)On Wednesday, August 6, 2014 10:43:13 PM UTC-4, gmhwxi wrote:
Hi,
I have been working on documenting the target language of ATS/Postiats.
It is a very small and standard subset of C. I have now got a functioning
parser
for it.
Today, I wrote some code to demonstrate how one might generate Python
code
from ATS source by making use of this parser:
https://github.com/githwxi/ATS-Postiats-contrib/tree/master/projects/MEDIUM/ATS-parse-emit/Python
As an example, here is some generated Python code
import sys
sys.path.append(
‘/home/hwxi/research/Postiats-contrib/git/projects/MEDIUM/ATS-parse-emit/Python/prelude’
)
from integer_cats import *
def fact(arg0):
#__patsflab_fact
tmp1 = ATSLIB_056_prelude__gt_g0int_int__1__1(arg0, 0)
if(tmp1):
tmp7 = atspre_g0int_sub_int(arg0, 1)
tmp6 = fact(tmp7)
tmpret0 = atspre_g0int_mul_int(arg0, tmp6)
else:
tmpret0 = 1
#endif
return(tmpret0)
def ATSLIB_056_prelude__gt_g0int_int__1__1(arg0, arg1):
#__patsflab_gt_g0int_int
tmp3__1 = atspre_g0int2int_int_int(arg1)
tmpret2__1 = atspre_g0int_gt_int(arg0, tmp3__1)
return(tmpret2__1)
print “fact(100) =”, fact(100)
Yes, the above Python code works!
At this point, there is still a lot of work to be done before all of this
can actually be useful.
If someone is interested in making it work, I will be happy to offer a
hand. Unfortunately,
I myself do not have time to go further along this path. At least, not now.
Cheers!