Skip to content

Commit

Permalink
[mycpp] Translate AsdlType.Create() -> AsdlType::Create()
Browse files Browse the repository at this point in the history
It works in mycpp/examples/parse!
  • Loading branch information
Andy C committed Feb 4, 2023
1 parent 14ab990 commit 8b80941
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build/py.sh
Expand Up @@ -186,7 +186,7 @@ py-codegen() {
gen-asdl-py 'frontend/syntax.asdl' 'frontend.syntax_abbrev'

# For tests
gen-asdl-py 'mycpp/examples/expr.asdl'
gen-asdl-py 'mycpp/examples/expr.asdl' --init-zero-N
}

py-asdl-examples() {
Expand Down
5 changes: 4 additions & 1 deletion mycpp/NINJA_subgraph.py
Expand Up @@ -89,7 +89,10 @@ def DefineTargets(ru):
phony_prefix = 'mycpp-unit')

# ASDL schema that examples/parse.py depends on
ru.asdl_library('mycpp/examples/expr.asdl')
ru.asdl_library(
'mycpp/examples/expr.asdl',
init_zero_n = True,
)


#
Expand Down
17 changes: 11 additions & 6 deletions mycpp/cppgen_pass.py
Expand Up @@ -563,14 +563,19 @@ def visit_member_expr(self, o: 'mypy.nodes.MemberExpr') -> T:
# self.log('member o = %s, o.expr = %s', o, o.expr)
pass

# This is an approximate hack that assumes that locals don't shadow
# imported names. Might be a problem with names like 'word'?
if (isinstance(o.expr, NameExpr) and (
is_asdl = o.name == 'Create' # hack for MyType.Create()

is_module = isinstance(o.expr, NameExpr) and (
o.expr.name in self.imported_names or
o.expr.name in ('mylib', 'libc', 'posix', 'fcntl_',
'time_', 'termios', 'signal_', 'fanos') or
o.name == '__init__'
)):
'time_', 'termios', 'signal_', 'fanos')
)
# TODO: Do we need this?
is_init = o.name == '__init__'

# This is an approximate hack that assumes that locals don't shadow
# imported names. Might be a problem with names like 'word'?
if is_asdl or is_module or is_init:
op = '::'
else:
op = '->' # Everything is a pointer
Expand Down
36 changes: 35 additions & 1 deletion mycpp/examples/parse.py
Expand Up @@ -14,6 +14,7 @@
from mycpp.mylib import log, tagswitch
from mycpp import mylib
from _devbuild.gen.expr_asdl import (
expr,
expr_e, # for translation only?
expr_t, expr__Var, expr__Const, expr__Binary, tok_e, tok_t
)
Expand Down Expand Up @@ -150,8 +151,10 @@ def Parse(self):
return self.ParseExpr()


def run_tests():

def TestParse():
# type: () -> None

lex = Lexer('abc')
while True:
tok_type, tok_val = lex.Read()
Expand Down Expand Up @@ -207,6 +210,37 @@ def run_tests():
log('Other')


def TestCreate():
# type: () -> None

c = expr.Const.Create()
log('c.i %d', c.i)

v = expr.Var.Create()
log('v.name %r', v.name)

b = expr.Binary.Create()
log('b.op %r', b.op)
b.op = '+'

# Must assign these
b.left = c
b.right = v

htree = b.PrettyTree()
ast_f = fmt.AnsiOutput(mylib.Stdout())

fmt.PrintTree(htree, ast_f)
ast_f.write('\n')


def run_tests():
# type: () -> None

TestParse()
TestCreate()


def run_benchmarks():
# type: () -> None
n = 100000
Expand Down

0 comments on commit 8b80941

Please sign in to comment.