add type to args of function

This commit is contained in:
Mylloon 2022-12-10 13:49:05 +01:00
parent ee9eef4b43
commit f34245bbf0
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
5 changed files with 36 additions and 10 deletions

10
ast.ml
View file

@ -49,11 +49,19 @@ module Syntax = struct
type block = instr list type block = instr list
type arg =
| Arg of
{ type_t : type_t
; name : ident
}
type args = arg list
type def = type def =
| Func of | Func of
{ func : ident { func : ident
; type_t : type_t ; type_t : type_t
; args : ident list ; args : args
; code : block ; code : block
; pos : Lexing.position ; pos : Lexing.position
} }

View file

@ -18,7 +18,7 @@
%type <Ast.Syntax.block> block %type <Ast.Syntax.block> block
%type <Ast.Syntax.prog> prog %type <Ast.Syntax.prog> prog
%type <Ast.Syntax.ident list> args_ident %type <Ast.Syntax.args> args_ident
%type <Ast.Syntax.expr list> args_expr %type <Ast.Syntax.expr list> args_expr
%% %%
@ -52,17 +52,20 @@ args_ident:
| Lpardeb ; s = args_ident { s } | Lpardeb ; s = args_ident { s }
/* a, ... */ /* a, ... */
| a = arg_ident ; Lcomma ; s = args_ident { a @ s } | t = Ltype ; a = arg_ident ; Lcomma ; s = args_ident { Arg { type_t = t
; name = a
} :: s }
/* c) */ /* c) */
| a = arg_ident ; Lparfin { a } | t = Ltype ; a = arg_ident ; Lparfin { [ Arg { type_t = t
; name = a } ] }
/* ) */ /* ) */
| Lparfin { [] } | Lparfin { [] }
arg_ident: arg_ident:
/* Argument */ /* Argument */
| a = Lvar { [ a ] } | a = Lvar { a }
block: block:
/* { */ /* { */

View file

@ -69,7 +69,15 @@ let rec analyze_block env ua = function
;; ;;
let analyze_func env ua = function let analyze_func env ua = function
| Syntax.Func f -> Func (f.func, f.args, analyze_block env ua f.code) | Syntax.Func f ->
Func
( f.func
, List.map
(fun a ->
match a with
| Syntax.Arg a -> a.name)
f.args
, analyze_block env ua f.code )
;; ;;
let rec analyze_prog env ua = function let rec analyze_prog env ua = function

View file

@ -25,7 +25,13 @@ let debug_parser oc parsed =
^ ", \"" ^ ", \""
^ d.func ^ d.func
^ ", [" ^ ", ["
^ String.concat "\n; " d.args ^ String.concat
"\n; "
(List.map
(fun a ->
match a with
| Syntax.Arg a -> " (" ^ string_of_type_t a.type_t ^ ")" ^ a.name ^ " ")
d.args)
^ "], [" ^ "], ["
^ fmt_b d.code ^ fmt_b d.code
^ "])\n" ^ "])\n"

View file

@ -1,8 +1,9 @@
int cops_calculator () { int cops_calculator (int a) {
int res = 13 * 100 + 20 - 8; int res = a * 100 + 20 - 8;
return res * 2 / 2; return res * 2 / 2;
} }
void main () { void main () {
cops_calculator(); int magic_number = 13;
cops_calculator(magic_number);
} }