From e11793d340e9cb6b498e9b6a8f4f538f1393fbc1 Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Tue, 28 Feb 2023 00:30:27 +0800 Subject: [PATCH] :rocket: add operator `^=` `&=` `|=` fix bug of parsing expressions beginning with floater --- README.md | 6 +++- doc/README_zh.md | 6 +++- doc/nasal.ebnf | 13 ++++++-- main.cpp | 12 ++++---- nasal_ast.h | 76 +++++++++++++++++++++++++---------------------- nasal_codegen.h | 43 +++++++++++++++++---------- nasal_dbg.h | 30 +++++++++++-------- nasal_import.h | 14 +++++---- nasal_lexer.h | 13 +++++--- nasal_opt.h | 23 ++++++++------ nasal_parse.h | 14 +++++++-- nasal_vm.h | 61 +++++++++++++++++++++++++++++-------- test/scalar.nas | 6 ++++ test/watchdog.nas | 1 + 14 files changed, 214 insertions(+), 104 deletions(-) diff --git a/README.md b/README.md index 5d11cb5..631b8bb 100644 --- a/README.md +++ b/README.md @@ -234,7 +234,7 @@ Bitwise operators `~` `|` `&` `^` have the same function as C/C++. 0x8^0x1; # xor ``` -Operators `=` `+=` `-=` `*=` `/=` `~=` are used in assignment expressions. +Operators `=` `+=` `-=` `*=` `/=` `~=` `^=` `&=` `|=` are used in assignment expressions. ```javascript a=b=c=d=1; @@ -243,6 +243,10 @@ a-=1; a*=1; a/=1; a~="string"; + +a^=0xff; +a&=0xca; +a|=0xba; ``` diff --git a/doc/README_zh.md b/doc/README_zh.md index 4eabb01..3a9cbd0 100644 --- a/doc/README_zh.md +++ b/doc/README_zh.md @@ -219,7 +219,7 @@ Nasal拥有基本的四种数学运算符 `+` `-` `*` `/`以及一个特别的 0x8^0x1; # 按位异或 ``` -赋值运算符`=` `+=` `-=` `*=` `/=` `~=`正如其名,用于进行赋值。 +赋值运算符`=` `+=` `-=` `*=` `/=` `~=` `^=` `&=` `|=`正如其名,用于进行赋值。 ```javascript a=b=c=d=1; @@ -228,6 +228,10 @@ a-=1; a*=1; a/=1; a~="string"; + +a^=0xff; +a&=0xca; +a|=0xba; ``` diff --git a/doc/nasal.ebnf b/doc/nasal.ebnf index c1a2334..a2f9e35 100644 --- a/doc/nasal.ebnf +++ b/doc/nasal.ebnf @@ -41,8 +41,17 @@ exprs::= ; calculation::= calculation '?' calculation ':' calculation - |or_expr - |calculation ('=' | '+=' | '-=' | '*=' | '/=' | '~=') calculation + |bitwise_or + |calculation ('=' | '+=' | '-=' | '*=' | '/=' | '~=' | '^=' | '&=' | '|=') calculation +; +bitwise_or::= + bitwise_xor '|' bitwise_xor +; +bitwise_xor::= + bitwise_and '^' bitwise_and +; +bitwise_and::= + or_expr '&' or_expr ; or_expr::= and_expr or and_expr diff --git a/main.cpp b/main.cpp index 976f9c3..74af311 100644 --- a/main.cpp +++ b/main.cpp @@ -32,14 +32,14 @@ std::ostream& help(std::ostream& out) { #endif <<"\nnasal