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 arg =
| Arg of
{ type_t : type_t
; name : ident
}
type args = arg list
type def =
| Func of
{ func : ident
; type_t : type_t
; args : ident list
; args : args
; code : block
; pos : Lexing.position
}

View file

@ -18,7 +18,7 @@
%type <Ast.Syntax.block> block
%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
%%
@ -52,17 +52,20 @@ args_ident:
| Lpardeb ; s = args_ident { s }
/* 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) */
| a = arg_ident ; Lparfin { a }
| t = Ltype ; a = arg_ident ; Lparfin { [ Arg { type_t = t
; name = a } ] }
/* ) */
| Lparfin { [] }
arg_ident:
/* Argument */
| a = Lvar { [ a ] }
| a = Lvar { a }
block:
/* { */

View file

@ -69,7 +69,15 @@ let rec analyze_block 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

View file

@ -25,7 +25,13 @@ let debug_parser oc parsed =
^ ", \""
^ 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
^ "])\n"

View file

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