From e26655c63879a9357fc3d63f2433ec2ef0386620 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Sun, 3 Apr 2022 19:18:31 +0530 Subject: [PATCH 1/3] Add interface decorator --- src/runtime/ltypes/ltypes.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/runtime/ltypes/ltypes.py b/src/runtime/ltypes/ltypes.py index da5fe5ab70..783e38ec31 100644 --- a/src/runtime/ltypes/ltypes.py +++ b/src/runtime/ltypes/ltypes.py @@ -92,6 +92,9 @@ def overload(f): return overloaded_f +def interface(f): + raise Exception("Unexpected to be called by CPython") + # C interoperation support From 7ff2f3413b1605d1335e036e99be6c5cae5dbc22 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Sun, 3 Apr 2022 19:18:54 +0530 Subject: [PATCH 2/3] Use interface decorator --- src/lpython/semantics/python_ast_to_asr.cpp | 2 +- src/runtime/lpython_builtin.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 7c0e6792fe..459ee32901 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -1230,7 +1230,7 @@ class SymbolTableVisitor : public CommonVisitor { current_procedure_abi_type = ASR::abiType::BindC; } else if (name == "overload") { overload = true; - } else { + } else if (name != "interface") { throw SemanticError("Decorator: " + name + " is not supported", x.base.base.loc); } diff --git a/src/runtime/lpython_builtin.py b/src/runtime/lpython_builtin.py index 4f83e81179..ce58b24fce 100644 --- a/src/runtime/lpython_builtin.py +++ b/src/runtime/lpython_builtin.py @@ -148,6 +148,7 @@ def bool(c: c64) -> bool: pass +@interface def len(s: str) -> i32: """ Return the length of the string `s`. @@ -273,32 +274,41 @@ def round(b: bool) -> i32: #: complex() as a generic procedure. #: supported types for arguments: #: (i32, i32), (f64, f64), (i32, f64), (f64, i32) +@interface @overload def complex(x: f64, y: f64) -> c64: pass +@interface @overload def complex(x: i32, y: i32) -> c64: pass +@interface @overload def complex(x: i32, y: f64) -> c64: pass +@interface @overload def complex(x: f64, y: i32) -> c64: pass + +@interface def divmod(x: i32, y: i32) -> tuple[i32, i32]: #: TODO: Implement once we have tuple support in the LLVM backend pass + def lbound(x: i32[:], dim: i32) -> i32: pass + def ubound(x: i32[:], dim: i32) -> i32: pass + @ccall def _lfortran_caimag(x: c32) -> f32: pass From 7c4a1931b867888106cf3a2615c6ca207b5fb37d Mon Sep 17 00:00:00 2001 From: Smit-create Date: Mon, 4 Apr 2022 16:42:43 +0530 Subject: [PATCH 3/3] Apply suggestions --- src/lpython/semantics/python_ast_to_asr.cpp | 4 +++- src/runtime/ltypes/ltypes.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 459ee32901..f7b825737d 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -1230,7 +1230,9 @@ class SymbolTableVisitor : public CommonVisitor { current_procedure_abi_type = ASR::abiType::BindC; } else if (name == "overload") { overload = true; - } else if (name != "interface") { + } else if (name == "interface") { + // TODO: Implement @interface + } else { throw SemanticError("Decorator: " + name + " is not supported", x.base.base.loc); } diff --git a/src/runtime/ltypes/ltypes.py b/src/runtime/ltypes/ltypes.py index 783e38ec31..7b8d166bde 100644 --- a/src/runtime/ltypes/ltypes.py +++ b/src/runtime/ltypes/ltypes.py @@ -93,7 +93,9 @@ def overload(f): def interface(f): - raise Exception("Unexpected to be called by CPython") + def inner_func(): + raise Exception("Unexpected to be called by CPython") + return inner_func # C interoperation support