From d2daf22df40bfee6c003030531acfc1597023181 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Fri, 17 Apr 2020 07:37:17 -0600 Subject: [PATCH] Add math_expression and fix ast2 tests --- nimterop/exprparser.nim | 6 ++++-- tests/include/tast2.h | 12 ++++++++++++ tests/tast2.nim | 14 +++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/nimterop/exprparser.nim b/nimterop/exprparser.nim index 1f72a9a..05df0e0 100644 --- a/nimterop/exprparser.nim +++ b/nimterop/exprparser.nim @@ -101,7 +101,7 @@ proc processNumberLiteral*(exprParser: ExprParser, node: TSNode): PNode = let nodeVal = node.val var match: RegexMatch - const reg = re"(\-)?(0\d+|0[xX][0-9a-fA-F]+|0[bB][01]+|\d+\.?\d*[fFlL]?|\d*\.?\d+[fFlL]?|\d+)([ulUL]*)" + const reg = re"(\-)?(0\d+|0[xX][0-9a-fA-F]+|0[bB][01]+|\d+|\d+\.?\d*[fFlL]?|\d*\.?\d+[fFlL]?)([ulUL]*)" let found = nodeVal.find(reg, match) if found: let @@ -199,6 +199,8 @@ proc processBitwiseExpression*(exprParser: ExprParser, node: TSNode): PNode = nimSym = "and" of "^": nimSym = "xor" + of "+", "-", "*", "/": + nimSym = binarySym else: raise newException(ExprParseError, &"Unsupported binary symbol \"{binarySym}\"") @@ -257,7 +259,7 @@ proc processTSNode*(exprParser: ExprParser, node: TSNode): PNode = of "parenthesized_expression": result = exprParser.processParenthesizedExpr(node) - of "bitwise_expression": + of "bitwise_expression", "math_expression": result = exprParser.processBitwiseExpression(node) of "shift_expression": result = exprParser.processShiftExpression(node) diff --git a/tests/include/tast2.h b/tests/include/tast2.h index bdf8823..04ecb15 100644 --- a/tests/include/tast2.h +++ b/tests/include/tast2.h @@ -8,6 +8,18 @@ extern "C" { #define D "hello" #define E 'c' +#define UEXPR (1234u << 1) +#define ULEXPR (1234ul << 2) +#define ULLEXPR (1234ull << 3) +#define LEXPR (1234l << 4) +#define LLEXPR (1234ll << 5) + +#define SHL1 (1u << 1) +#define SHL2 (1u << 2) +#define SHL3 (1u << 3) + +#define ALLSHL (SHL1 | SHL2 | SHL3) + struct A0; struct A1 {}; typedef struct A2; diff --git a/tests/tast2.nim b/tests/tast2.nim index e13c4ac..0749b11 100644 --- a/tests/tast2.nim +++ b/tests/tast2.nim @@ -105,6 +105,18 @@ assert C == 0x10 assert D == "hello" assert E == 'c' +assert UEXPR == (1234.uint shl 1) +assert ULEXPR == (1234.uint32 shl 2) +assert ULLEXPR == (1234.uint64 shl 3) +assert LEXPR == (1234.int32 shl 4) +assert LLEXPR == (1234.int64 shl 5) + +assert SHL1 == (1.uint shl 1) +assert SHL2 == (1.uint shl 2) +assert SHL3 == (1.uint shl 3) + +assert ALLSHL == (SHL1 or SHL2 or SHL3) + assert A0 is object testFields(A0, "f1!cint") checkPragmas(A0, pHeaderBy, istype = false) @@ -271,7 +283,7 @@ var a21p: A21p a21p = addr a20 assert A22 is object -testFields(A22, "f1|f2!ptr ptr cint|array[123 + 132, ptr cint]") +testFields(A22, "f1|f2!ptr ptr cint|array[123 + cast[typeof(123)](132), ptr cint]") checkPragmas(A22, pHeaderBy, istype = false) var a22: A22 a22.f1 = addr a15.a2[0]