From: Michael D. Lowis Date: Fri, 29 Mar 2019 18:34:04 +0000 (-0400) Subject: added total hack version of struct member access and UFCS X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=c28f65a42e2dc36afdec9a6bdbad7f6be1e14b85;p=proto%2Fsclpl.git added total hack version of struct member access and UFCS --- diff --git a/example.src b/example.src index 0c6e513..b60c809 100644 --- a/example.src +++ b/example.src @@ -46,5 +46,10 @@ fun main(args string[]) int { fun submain(args string[]) int { 123 } + # UFCS call (call regular function like a method) foo.bar() +# foo.bar.baz() + # Struct/Union field access + foo.bar +# foo.bar.baz } diff --git a/source/parser.c b/source/parser.c index 55a0ecc..9192190 100644 --- a/source/parser.c +++ b/source/parser.c @@ -170,15 +170,19 @@ static AST* expression(Parser* p) { exp = constant(p); } - /* determine if this is a function call */ - if (matches(p, '(')) { - exp = Apply(exp, expr_list(p, '(', ')')); - } else if (accept(p, '.')) { - AST* func = identifier(p); - AST* args = expr_list(p, '(', ')'); - explist_prepend(args, exp); - exp = Apply(func, args); - } + /* check for member reference or UFCS call */ + AST *member = NULL, *args = NULL; + if (accept(p, '.')) + member = identifier(p); + if (matches(p, '(')) + args = expr_list(p, '(', ')'); + + if (member && !args) + exp = exp; // ??? AST for struct reference + else if (member && args) + exp = Apply(member, args); + else if (!member && args) + exp = args; return exp; }