From: Jérémie Astor Date: Wed, 19 May 2021 17:58:43 +0000 (+0200) Subject: Argument defaults X-Git-Tag: nightly~628 X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=1c9ce3b7ca06ac4e1d877ee6e5c49c2aa6a1dfd2;p=gwion.git Argument defaults --- diff --git a/ast b/ast index 40397642..aa209a79 160000 --- a/ast +++ b/ast @@ -1 +1 @@ -Subproject commit 40397642ad13c18fc0edc65791b475e29d832d8a +Subproject commit aa209a7911f18d117fd133e1c52fbc2c6ddb181b diff --git a/fmt b/fmt index 99588790..17bb2115 160000 --- a/fmt +++ b/fmt @@ -1 +1 @@ -Subproject commit 9958879019a9bca3aaf3cb92f73aff5c516d3ec3 +Subproject commit 17bb211543ca36b9c53d884515afded758547945 diff --git a/src/parse/scan0.c b/src/parse/scan0.c index 43334f1b..6da11478 100644 --- a/src/parse/scan0.c +++ b/src/parse/scan0.c @@ -366,6 +366,59 @@ ANN static m_bool scan0_stmt_list(const Env env, Stmt_List list) { return GW_OK; } +//#define scan0_func_def dummy_func + +ANN static m_bool fdef_defaults(const Func_Def fdef) { + Arg_List list = fdef->base->args; + while(list) { + if(list->exp) + return true; + list = list->next; + } + return false; +} + +ANN static Exp arglist2exp(MemPool p, Arg_List arg, const Exp default_arg) { + Exp exp = new_prim_id(p, arg->var_decl->xid, arg->var_decl->pos); + if(arg->next) + exp->next = arglist2exp(p, arg->next, default_arg); + else + exp->next = cpy_exp(p, default_arg); + return exp; +} + +ANN static Ast scan0_func_def_default(const MemPool p, const Ast ast, const Ast next) { + const Func_Def base_fdef = ast->section->d.func_def; + Arg_List base_arg = base_fdef->base->args, former = NULL; + while(base_arg) { + if(!base_arg->next && base_arg->exp) { + if(former) + former->next = NULL; + // use cpy_func_base? + Func_Base *base = new_func_base(p, cpy_type_decl(p, base_fdef->base->td), + base_fdef->base->xid, former ? cpy_arg_list(p, base_fdef->base->args) : NULL, + base_fdef->base->flag, base_fdef->base->pos); + const Exp efunc = new_prim_id(p, base_fdef->base->xid, base_fdef->base->pos); + Exp arg_exp = former ? arglist2exp(p, base_fdef->base->args, base_arg->exp) : + cpy_exp(p, base_arg->exp); + const Exp ecall = new_exp_call(p, efunc, arg_exp, base_fdef->base->pos); + const Stmt code = new_stmt_exp(p, ae_stmt_return, ecall, base_fdef->base->pos); + const Stmt_List slist = new_stmt_list(p, code, NULL); + const Stmt body = new_stmt_code(p, slist, base_fdef->base->pos); + const Func_Def fdef = new_func_def(p, base, body); + Section *new_section = new_section_func_def(p, fdef); + if(former) + former->next = base_arg; + const Ast tmp_ast = new_ast(p, new_section, NULL); + ast->next = scan0_func_def_default(p, tmp_ast, next); + return ast; + } + former = base_arg; + base_arg = base_arg->next; + } + return ast->next = next; +} + #define scan0_func_def dummy_func ANN static m_bool scan0_extend_def(const Env env, const Extend_Def xdef) { @@ -417,7 +470,14 @@ ANN m_bool scan0_class_def(const Env env, const Class_Def c) { } ANN m_bool scan0_ast(const Env env, Ast ast) { - do CHECK_BB(scan0_section(env, ast->section)); + do { + CHECK_BB(scan0_section(env, ast->section)); + if(ast->section->section_type != ae_section_func || !fdef_defaults(ast->section->d.func_def)) + continue; + const Ast next = ast->next; + scan0_func_def_default(env->gwion->mp, ast, next); + ast = next; + } while((ast = ast->next)); return GW_OK; }