add function semantics

This commit is contained in:
Mylloon 2022-12-10 14:57:21 +01:00
parent 7e4d3a3790
commit 1c1e816457
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -24,7 +24,7 @@ let rec analyze_expr env ua t = function
| Syntax.Call c -> | Syntax.Call c ->
(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 then errt t ret_t c.pos; if ret_t != t then errt ret_t t c.pos;
( Call ( Call
( c.func ( c.func
, List.map2 , List.map2
@ -62,27 +62,47 @@ let analyze_instr env ua = function
;; ;;
let rec analyze_block env ua = function let rec analyze_block env ua = function
| [] -> [] | [] -> [], ua
| instr :: new_block -> | instr :: new_block ->
let new_instr, new_env, new_ua = analyze_instr env ua instr in let new_instr, new_env, ua1 = analyze_instr env ua instr in
new_instr :: analyze_block new_env new_ua new_block let new_block, ua2 = analyze_block new_env ua1 new_block in
new_instr :: new_block, ua2
;; ;;
let analyze_func env ua = function let analyze_func env ua = function
| Syntax.Func f -> | Syntax.Func f ->
Func let rec add_args env = function
( f.func | [] -> env
, List.map | h :: t ->
(fun a -> (match h with
match a with | Syntax.Arg a -> add_args (Env.add a.name a.type_t env) t)
| Syntax.Arg a -> a.name) in
f.args let block, _ = analyze_block (add_args env f.args) ua f.code in
, analyze_block env ua f.code ) ( Func
( f.func
, List.map
(fun a ->
match a with
| Syntax.Arg a -> a.name)
f.args
, block )
, Env.add
f.func
(Func_t
( f.type_t
, List.map
(fun a ->
match a with
| Syntax.Arg a -> a.type_t)
f.args ))
env )
;; ;;
let rec analyze_prog env ua = function let rec analyze_prog env ua = function
| [] -> [] | [] -> []
| fn :: suite -> analyze_func env ua fn :: analyze_prog env ua suite | fn :: suite ->
let fn, new_env = analyze_func env ua fn in
fn :: analyze_prog new_env ua suite
;; ;;
let analyze parsed = analyze_prog _types_ [] parsed let analyze parsed = analyze_prog _types_ [] parsed