From c28f65a42e2dc36afdec9a6bdbad7f6be1e14b85 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Fri, 29 Mar 2019 14:34:04 -0400 Subject: [PATCH] added total hack version of struct member access and UFCS --- example.src | 5 +++++ source/parser.c | 22 +++++++++++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) 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; } -- 2.54.0