Changeset 6a37819 in sasmodels


Ignore:
Timestamp:
Jan 3, 2018 7:35:12 PM (7 years ago)
Author:
Paul Kienzle <pkienzle@…>
Children:
d7f33e5
Parents:
4c87de0
Message:

fix True/False/None? for python 3

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/py2c.py

    r4c87de0 r6a37819  
    110110""" 
    111111 
    112  
    113 """ 
    114 Update Notes 
    115 ============ 
    116 11/22/2017, O.E.   Each 'visit_*' method is to build a C statement string. It 
    117                     shold insert 4 blanks per indentation level. 
    118                     The 'body' method will combine all the strings, by adding 
    119                     the 'current_statement' to the c_proc string list 
    120    11/2017, OE: variables, argument definition implemented. 
    121    Note: An argument is considered an array if it is the target of an 
    122         assignment. In that case it is translated to <var>[0] 
    123 11/27/2017, OE: 'pow' basicly working 
    124   /12/2017, OE: Multiple assignment: a1,a2,...,an=b1,b2,...bn implemented 
    125   /12/2017, OE: Power function, including special cases of 
    126                 square(x)(pow(x,2)) and cube(x)(pow(x,3)), implemented in 
    127                 translate_power, called from visit_BinOp 
    128 12/07/2017, OE: Translation of integer division, '\\' in python, implemented 
    129                 in translate_integer_divide, called from visit_BinOp 
    130 12/07/2017, OE: C variable definition handled in 'define_c_vars' 
    131               : Python integer division, '//', translated to C in 
    132                 'translate_integer_divide' 
    133 12/15/2017, OE: Precedence maintained by writing opening and closing 
    134                 parenthesesm '(',')', in procedure 'visit_BinOp'. 
    135 12/18/2017, OE: Added call to 'add_current_line()' at the beginning 
    136                 of visit_Return 
    137 2018-01-03, PK: Update interface for use in sasmodels 
    138 2018-01-03, PK: support "expr if cond else expr" syntax 
    139 2018-01-03, PK: x//y => (int)((x)/(y)) and x/y => ((double)(x)/(double)(y)) 
    140 2018-01-03, PK: True/False => true/false 
    141 2018-01-03, PK: f(x) was introducing an extra semicolon 
    142 2018-01-03, PK: simplistic print function, for debugging 
    143 """ 
     112# Update Notes 
     113# ============ 
     114# 11/22/2017, O.E.   Each 'visit_*' method is to build a C statement string. It 
     115#                     shold insert 4 blanks per indentation level. 
     116#                     The 'body' method will combine all the strings, by adding 
     117#                     the 'current_statement' to the c_proc string list 
     118#    11/2017, OE: variables, argument definition implemented. 
     119#    Note: An argument is considered an array if it is the target of an 
     120#         assignment. In that case it is translated to <var>[0] 
     121# 11/27/2017, OE: 'pow' basicly working 
     122#   /12/2017, OE: Multiple assignment: a1,a2,...,an=b1,b2,...bn implemented 
     123#   /12/2017, OE: Power function, including special cases of 
     124#                 square(x)(pow(x,2)) and cube(x)(pow(x,3)), implemented in 
     125#                 translate_power, called from visit_BinOp 
     126# 12/07/2017, OE: Translation of integer division, '\\' in python, implemented 
     127#                 in translate_integer_divide, called from visit_BinOp 
     128# 12/07/2017, OE: C variable definition handled in 'define_c_vars' 
     129#               : Python integer division, '//', translated to C in 
     130#                 'translate_integer_divide' 
     131# 12/15/2017, OE: Precedence maintained by writing opening and closing 
     132#                 parenthesesm '(',')', in procedure 'visit_BinOp'. 
     133# 12/18/2017, OE: Added call to 'add_current_line()' at the beginning 
     134#                 of visit_Return 
     135# 2018-01-03, PK: Update interface for use in sasmodels 
     136# 2018-01-03, PK: support "expr if cond else expr" syntax 
     137# 2018-01-03, PK: x//y => (int)((x)/(y)) and x/y => ((double)(x)/(double)(y)) 
     138# 2018-01-03, PK: True/False => true/false 
     139# 2018-01-03, PK: f(x) was introducing an extra semicolon 
     140# 2018-01-03, PK: simplistic print function, for debugging 
     141# 2018-01-03, PK: while expr: ... => while (expr) { ... } 
     142 
     143from __future__ import print_function 
     144 
     145import sys 
    144146import ast 
    145 import sys 
    146147from ast import NodeVisitor 
    147148 
     
    825826 
    826827    TRANSLATE_CONSTANTS = { 
     828        # python 2 uses normal name references through vist_Name 
    827829        'True': 'true', 
    828830        'False': 'false', 
    829831        'None': 'NULL',  # "None" will probably fail for other reasons 
     832        # python 3 uses NameConstant 
     833        True: 'true', 
     834        False: 'false', 
     835        None: 'NULL',  # "None" will probably fail for other reasons 
    830836        } 
     837 
    831838    def visit_Name(self, node): 
    832839        translation = self.TRANSLATE_CONSTANTS.get(node.id, None) 
     
    851858            else: 
    852859                self.c_vars.append(node.id) 
     860 
     861    def visit_NameConstant(self, node): 
     862        translation = self.TRANSLATE_CONSTANTS.get(node.value, None) 
     863        if translation is not None: 
     864            self.write_c(translation) 
     865        else: 
     866            self.unsupported(node, "don't know how to translate %r"%node.value) 
    853867 
    854868    def visit_Str(self, node): 
Note: See TracChangeset for help on using the changeset viewer.