Skip to content
Merged
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
4 changes: 4 additions & 0 deletions integration_tests/test_c_interop_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def _lfortran_bgt32(i: i32, j: i32) -> i32:
def _lfortran_bgt64(i: i64, j: i64) -> i64:
pass

@ccall
def _lfortran_random_number(n: i64, v: f64[:]):
pass

assert abs(_lfortran_dsin(pi) - 0) < 1e-12
assert abs(_lfortran_dsin(pi/2) - 1) < 1e-12
assert abs(_lfortran_ssin(pi) - 0) < 1e-6
Expand Down
36 changes: 27 additions & 9 deletions src/runtime/ltypes/ltypes.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
from inspect import getfullargspec, getcallargs
from typing import types
import os
import ctypes
import platform

# data-types

i32 = types.new_class("i32")
i64 = types.new_class("i64")
f32 = types.new_class("f32")
f64 = types.new_class("f64")
c32 = types.new_class("c32")
c64 = types.new_class("c64")
class Type:
def __init__(self, name):
self._name = name

def __getitem__(self, params):
return Array(self, params)

class Array:
def __init__(self, type, dims):
self._type = type
self._dims = dims

i32 = Type("i32")
i64 = Type("i64")
f32 = Type("f32")
f64 = Type("f64")
c32 = Type("c32")
c64 = Type("c64")

# Overloading support

Expand Down Expand Up @@ -93,6 +104,11 @@ def convert_type_to_ctype(arg):
return ctypes.c_int64
elif arg == i32:
return ctypes.c_int32
elif arg is None:
raise NotImplementedError("Type cannot be None")
elif isinstance(arg, Array):
type = convert_type_to_ctype(arg._type)
return ctypes.POINTER(type)
else:
raise NotImplementedError("Type not implemented")
def get_rtlib_dir():
Expand Down Expand Up @@ -121,8 +137,10 @@ def get_crtlib_path():
arg_ctype = convert_type_to_ctype(arg_type)
argtypes.append(arg_ctype)
self.cf.argtypes = argtypes
res_type = self.annotations["return"]
self.cf.restype = convert_type_to_ctype(res_type)
if "return" in self.annotations:
res_type = self.annotations["return"]
if res_type is not None:
self.cf.restype = convert_type_to_ctype(res_type)

def __call__(self, *args, **kwargs):
if len(kwargs) > 0:
Expand Down