marcelogdeandrade / PythonCompiler

Code used on "Writing your own programming language and compiler with Python" post
GNU General Public License v3.0
235 stars 53 forks source link

How to create a `print` function for different types? #9

Closed AntonZelenin closed 1 year ago

AntonZelenin commented 1 year ago

Hi, can I get general guidance on the correct way to implement the print function to be able to print different types:

print(10)
print(7.3)

It's not possible to define a function with the same name for different types, maybe I can somehow cast arguments to strings first and then always pass only strings?

AntonZelenin commented 1 year ago

There might be some missing context, but that's what I did

  1. Changed the printf function creation to be
    ir.Function(
    self.module,
    ir.FunctionType(aye_types.INT8, [], var_arg=True),
    name='printf',
    )
  2. Added dynamic generation of voidptr and c_fmt, the final Print class implementation looks like this:

    
    class Print:
    _global_fmt_cache = {}
    
    def __init__(self, builder, module, value, printf):
        self.builder = builder
        self.module = module
        self.value = value
        self.printf = printf
    
    def eval(self):
        value = self.value.eval()
        fmt_arg = self.builder.bitcast(
            self._get_global_fmt(value.type),
            value.type.as_pointer(),
        )
        self.builder.call(self.printf, [fmt_arg, value])
    
    def _get_global_fmt(self, type_):
        if type_ not in self._global_fmt_cache:
            self._declare_global_fmt(type_)
        return self._global_fmt_cache[type_]
    
    def _declare_global_fmt(self, type_):
        fmt = f'{_get_c_format(type_)} \n\0'
        c_fmt = ir.Constant(
            ir.ArrayType(aye_types.INT8, len(fmt)),
            bytearray(fmt.encode('utf8'))
        )
        global_fmt = ir.GlobalVariable(self.module, c_fmt.type, name=f'fstr_{type_}')
        global_fmt.linkage = 'internal'
        global_fmt.global_constant = True
        global_fmt.initializer = c_fmt
    
        self._global_fmt_cache[type_] = global_fmt

def _get_cformat(type): if isinstance(type, ir.IntType): return '%i' elif isinstance(type, ir.DoubleType): return '%lf' raise ValueError(f'Unsupported type: {type}')