diff --git a/docs/features/ternary.md b/docs/features/ternary.md deleted file mode 100644 index 1b4f0690..00000000 --- a/docs/features/ternary.md +++ /dev/null @@ -1,30 +0,0 @@ ---- -icon: dot ---- - -# ternary op - -Ternary operator is a short hand `if...else`. -pkpy supports both c and python style ternary. - - -## Syntax - -``` - ? : -``` - -``` - if else -``` - -## Example - -```python -a = 1 -s = a == 1 ? "a is 1" : "a is not 1" -print(s) # a is 1 - -# which equals to -s = "a is 1" if a == 1 else "a is not 1" -``` \ No newline at end of file diff --git a/python/builtins.py b/python/builtins.py index 2ed0b17a..3435ea36 100644 --- a/python/builtins.py +++ b/python/builtins.py @@ -5,20 +5,20 @@ def print(*args, sep=' ', end='\n'): def round(x, ndigits=0): assert ndigits >= 0 if ndigits == 0: - return x >= 0 ? int(x + 0.5) : int(x - 0.5) + return int(x + 0.5) if x >= 0 else int(x - 0.5) if x >= 0: return int(x * 10**ndigits + 0.5) / 10**ndigits else: return int(x * 10**ndigits - 0.5) / 10**ndigits def abs(x): - return x < 0 ? -x : x + return -x if x < 0 else x def max(a, b): - return a > b ? a : b + return a if a > b else b def min(a, b): - return a < b ? a : b + return a if a < b else b def all(iterable): for i in iterable: diff --git a/src/compiler.h b/src/compiler.h index bc732eb2..e6c906da 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -104,7 +104,6 @@ class Compiler { rules[TK("&")] = { nullptr, METHOD(exprBinaryOp), PREC_BITWISE_AND }; rules[TK("|")] = { nullptr, METHOD(exprBinaryOp), PREC_BITWISE_OR }; rules[TK("^")] = { nullptr, METHOD(exprBinaryOp), PREC_BITWISE_XOR }; - rules[TK("?")] = { nullptr, METHOD(exprTernary), PREC_TERNARY }; rules[TK("if")] = { nullptr, METHOD(exprTernary), PREC_TERNARY }; rules[TK(",")] = { nullptr, METHOD(exprTuple), PREC_TUPLE }; rules[TK("not in")] = { nullptr, METHOD(exprBinaryOp), PREC_TEST }; @@ -253,25 +252,14 @@ class Compiler { void exprTernary(){ auto e = make_expr(); - if(prev().type == TK("if")){ - e->true_expr = ctx()->s_expr.popx(); - // cond - parse_expression(PREC_TERNARY + 1); - e->cond = ctx()->s_expr.popx(); - consume(TK("else")); - // if false - parse_expression(PREC_TERNARY + 1); - e->false_expr = ctx()->s_expr.popx(); - }else{ // ?: - e->cond = ctx()->s_expr.popx(); - // if true - parse_expression(PREC_TERNARY + 1); - e->true_expr = ctx()->s_expr.popx(); - consume(TK(":")); - // if false - parse_expression(PREC_TERNARY + 1); - e->false_expr = ctx()->s_expr.popx(); - } + e->true_expr = ctx()->s_expr.popx(); + // cond + parse_expression(PREC_TERNARY + 1); + e->cond = ctx()->s_expr.popx(); + consume(TK("else")); + // if false + parse_expression(PREC_TERNARY + 1); + e->false_expr = ctx()->s_expr.popx(); ctx()->s_expr.push(std::move(e)); } diff --git a/tests/03_bool.py b/tests/03_bool.py index 315491f1..a9b2f219 100644 --- a/tests/03_bool.py +++ b/tests/03_bool.py @@ -17,9 +17,4 @@ assert bool(1) == True assert bool([]) == False assert bool("abc") == True assert bool([1,2]) == True -assert bool('') == False - -# test ?: -a = 5 -assert ((a > 3) ? 1 : 0) == 1 -assert ((a < 3) ? 1 : 0) == 0 \ No newline at end of file +assert bool('') == False \ No newline at end of file