Skip to content

Commit

Permalink
[mycpp] Write else: body connected with if 0:
Browse files Browse the repository at this point in the history
This is useful for comparing Pythona and C++.

Note that mycpp can't print if 0:, because it happens at the top level
too (outside of functions).

Found a translation bug with arg_jw.pretty in 'json write'.
  • Loading branch information
Andy C committed Dec 23, 2023
1 parent b6ffa26 commit b31dd88
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
10 changes: 8 additions & 2 deletions builtin/json_ysh.py
Expand Up @@ -15,6 +15,7 @@
from frontend import args
from frontend import typed_args
from mycpp import mylib
from mycpp.mylib import log
from ysh import cpython

import sys
Expand All @@ -25,6 +26,8 @@
if TYPE_CHECKING:
from core.ui import ErrorFormatter

_ = log

_JSON_ACTION_ERROR = "builtin expects 'read' or 'write'"


Expand Down Expand Up @@ -72,14 +75,17 @@ def Run(self, cmd_val):
val = rd.PosValue()
rd.Done()

if arg_jw.pretty:
if arg_jw.pretty: # C++ BUG Here!
indent = arg_jw.indent
#log('arg_jw indent %d', indent)
extra_newline = False
else:
# How yajl works: if indent is -1, then everything is on one line.
indent = -1
extra_newline = True

#log('json write indent %d', indent)

if mylib.PYTHON:
#if 0:
obj = cpython._ValueToPyObj(val)
Expand All @@ -90,7 +96,7 @@ def Run(self, cmd_val):
sys.stdout.write('\n')
else:
buf = mylib.BufWriter()
self.printer.Print(val, buf, indent=indent)
self.printer.Print(val, buf, indent)
self.stdout_.write(buf.getvalue())
self.stdout_.write('\n')

Expand Down
4 changes: 2 additions & 2 deletions mycpp/const_pass.py
Expand Up @@ -474,8 +474,8 @@ def visit_if_stmt(self, o: 'mypy.nodes.IfStmt') -> T:
return

# Omit if 0:
if isinstance(cond, IntExpr) and cond.value == 0:
return
#if isinstance(cond, IntExpr) and cond.value == 0:
# return

# Omit if TYPE_CHECKING blocks. They contain type expressions that
# don't type check!
Expand Down
4 changes: 4 additions & 0 deletions mycpp/cppgen_pass.py
Expand Up @@ -2738,6 +2738,10 @@ def visit_if_stmt(self, o: 'mypy.nodes.IfStmt') -> T:

# Omit if 0:
if isinstance(cond, IntExpr) and cond.value == 0:
# But write else: body
# Note: this would be invalid at the top level!
if o.else_body:
self.accept(o.else_body)
return

# Omit if TYPE_CHECKING blocks. They contain type expressions that
Expand Down

0 comments on commit b31dd88

Please sign in to comment.