add recursive fn
This commit is contained in:
parent
a01c827935
commit
9ef121370b
2 changed files with 21 additions and 7 deletions
|
@ -32,7 +32,7 @@ $ make test
|
||||||
- [x] Librairie standard (multiplication, addition, comparateur, print, ...)
|
- [x] Librairie standard (multiplication, addition, comparateur, print, ...)
|
||||||
- [x] Conditions
|
- [x] Conditions
|
||||||
- [x] Boucles
|
- [x] Boucles
|
||||||
- [x] Fonctions utilisateurs (arguments et valeur de retour typé)
|
- [x] Fonctions utilisateurs récursives (arguments et valeur de retour typé)
|
||||||
<!-- - [ ] Allocation mémoire (`malloc`) -->
|
<!-- - [ ] Allocation mémoire (`malloc`) -->
|
||||||
<!-- - [ ] Pointeurs -->
|
<!-- - [ ] Pointeurs -->
|
||||||
<!-- - [ ] Listes + fonctions écrites dans notre language pour gérer ses listes -->
|
<!-- - [ ] Listes + fonctions écrites dans notre language pour gérer ses listes -->
|
||||||
|
|
26
semantics.ml
26
semantics.ml
|
@ -24,6 +24,8 @@ let rec analyze_expr env ua t = function
|
||||||
if new_t != t then errt t new_t v.pos;
|
if new_t != t then errt t new_t v.pos;
|
||||||
Var v.name, new_t
|
Var v.name, new_t
|
||||||
| Syntax.Call c ->
|
| Syntax.Call c ->
|
||||||
|
if not (Env.mem c.func env)
|
||||||
|
then raise (SemanticsError ("Unbound function \"" ^ c.func ^ "\"", c.pos));
|
||||||
(match Env.find c.func env with
|
(match Env.find c.func env with
|
||||||
| Func_t (ret_t, tl) ->
|
| Func_t (ret_t, tl) ->
|
||||||
if ret_t != t && t != Magic_t then errt ret_t t c.pos;
|
if ret_t != t && t != Magic_t then errt ret_t t c.pos;
|
||||||
|
@ -97,13 +99,25 @@ and analyze_block env ua ret_t pos = function
|
||||||
|
|
||||||
let analyze_func env ua = function
|
let analyze_func env ua = function
|
||||||
| Syntax.Func f ->
|
| Syntax.Func f ->
|
||||||
let rec add_args env = function
|
let add_fn =
|
||||||
| [] -> env
|
let rec add_args env2 = function
|
||||||
| h :: t ->
|
| [] -> env2
|
||||||
(match h with
|
| h :: t ->
|
||||||
| Syntax.Arg a -> add_args (Env.add a.name a.type_t env) t)
|
(match h with
|
||||||
|
| Syntax.Arg a -> add_args (Env.add a.name a.type_t env2) t)
|
||||||
|
in
|
||||||
|
Env.add
|
||||||
|
f.func
|
||||||
|
(Func_t
|
||||||
|
( f.type_t
|
||||||
|
, List.map
|
||||||
|
(fun a ->
|
||||||
|
match a with
|
||||||
|
| Syntax.Arg a -> a.type_t)
|
||||||
|
f.args ))
|
||||||
|
(add_args env f.args)
|
||||||
in
|
in
|
||||||
let block, _ = analyze_block (add_args env f.args) ua f.type_t f.pos f.code in
|
let block, _ = analyze_block add_fn ua f.type_t f.pos f.code in
|
||||||
( Func
|
( Func
|
||||||
( f.func
|
( f.func
|
||||||
, List.map
|
, List.map
|
||||||
|
|
Reference in a new issue