Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ _build
.benchmarks
.hypothesis
build

# pyenv
.python-version
72 changes: 0 additions & 72 deletions src/fluent_compiler/ast_compat.py

This file was deleted.

18 changes: 13 additions & 5 deletions src/fluent_compiler/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
Utilities for doing Python code generation
"""

import ast
import keyword
import platform
import re

from . import ast_compat as ast
from .utils import allowable_keyword_arg_name, allowable_name

# This module provides simple utilities for building up Python source code. It
Expand Down Expand Up @@ -406,7 +406,7 @@ def as_ast(self):
def add_lineno(node):
node.lineno = self.source.row

ast.traverse(func_def, add_lineno)
traverse(func_def, add_lineno)
return func_def

def add_return(self, value):
Expand Down Expand Up @@ -531,7 +531,7 @@ def __init__(self, string_value):
self.string_value = string_value

def as_ast(self):
return ast.Str(
return ast.Constant(
self.string_value,
kind=None, # 3.8, indicates no prefix, needed only for tests
**DEFAULT_AST_ARGS,
Expand All @@ -552,7 +552,7 @@ def __init__(self, number):
self.type = type(number)

def as_ast(self):
return ast.Num(n=self.number, **DEFAULT_AST_ARGS)
return ast.Constant(value=self.number, **DEFAULT_AST_ARGS)

def __repr__(self):
return f"Number({repr(self.number)})"
Expand Down Expand Up @@ -729,7 +729,7 @@ def as_ast(self):
ast.keyword(
arg=None,
value=ast.Dict(
keys=[ast.Str(k, kind=None, **DEFAULT_AST_ARGS) for k in kwarg_names],
keys=[ast.Constant(k, kind=None, **DEFAULT_AST_ARGS) for k in kwarg_names],
values=[v.as_ast() for v in kwarg_values],
**DEFAULT_AST_ARGS,
),
Expand Down Expand Up @@ -859,6 +859,14 @@ def rewriter(node):
return codegen_ast


def traverse(ast_node, func):
"""
Apply 'func' to ast_node (which is `ast.*` object)
"""
for node in ast.walk(ast_node):
func(node)


def rewriting_traverse(node, func):
"""
Apply 'func' to node and all sub PythonAst nodes
Expand Down