diff --git a/ast.ml b/ast.ml index f27437c..00b3e14 100644 --- a/ast.ml +++ b/ast.ml @@ -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 } diff --git a/parser.mly b/parser.mly index aae18b8..1c739a3 100644 --- a/parser.mly +++ b/parser.mly @@ -18,7 +18,7 @@ %type block %type prog -%type args_ident +%type args_ident %type 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: /* { */ diff --git a/semantics.ml b/semantics.ml index 406cb4a..8ba91cf 100644 --- a/semantics.ml +++ b/semantics.ml @@ -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 diff --git a/test.ml b/test.ml index f1d9b39..d80a663 100644 --- a/test.ml +++ b/test.ml @@ -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" diff --git a/tests/11_def.test b/tests/11_def.test index 80c7e77..ad8e75b 100644 --- a/tests/11_def.test +++ b/tests/11_def.test @@ -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); }