From 46c0fe87e6ef3322a1f044d0aeeea9da63fd3088 Mon Sep 17 00:00:00 2001 From: Bob Nystrom Date: Thu, 31 Oct 2013 21:49:15 -0700 Subject: [PATCH] Parentheses for grouping. --- src/compiler.c | 8 ++++++++ test/grammar.wren | 9 +++++++++ 2 files changed, 17 insertions(+) create mode 100644 test/grammar.wren diff --git a/src/compiler.c b/src/compiler.c index 47f1adee..4d615441 100644 --- a/src/compiler.c +++ b/src/compiler.c @@ -583,6 +583,14 @@ void primary(Compiler* compiler) string(compiler); return; } + + // Parentheses. + if (match(compiler, TOKEN_LEFT_PAREN)) + { + expression(compiler); + consume(compiler, TOKEN_RIGHT_PAREN); + return; + } } void number(Compiler* compiler) diff --git a/test/grammar.wren b/test/grammar.wren new file mode 100644 index 00000000..344b8995 --- /dev/null +++ b/test/grammar.wren @@ -0,0 +1,9 @@ + +// * has higher precedence than +. +io.write(2 + 3 * 4) // expect: 14 + +// * has higher precedence than -. +io.write(20 - 3 * 4) // expect: 8 + +// Using () for grouping. +io.write((2 * (6 - (2 + 2)))) // expect: 4