Skip to content

Supporting Callbacks in LPython #1608

@Smit-create

Description

@Smit-create

For providing functionalities like: #992 (comment) we need to support callbacks. A simple example is:

from ltypes import i32, Callable

def f(x: i32) -> i32:
    return x + 1

def g(func: Callable[[i32], i32], arg: i32):
    ret: i32
    ret = func(arg)
    print(ret)

g(f, 10)

Currently, this throws the following error:

Error

Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
  File "/Users/thebigbool/repos/lpython/src/bin/lpython.cpp", line 1602
    return emit_asr(arg_file, lpython_pass_manager, runtime_library_dir,
  File "/Users/thebigbool/repos/lpython/src/bin/lpython.cpp", line 194
    r = LCompilers::LPython::python_ast_to_asr(al, lm, *ast, diagnostics,
  File "/Users/thebigbool/repos/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 6237
    if (res.ok) {
  File "/Users/thebigbool/repos/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 3814
    v.visit_Module(ast);
  File "/Users/thebigbool/repos/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 3302
    visit_stmt(*x.m_body[i]);
  File "/Users/thebigbool/repos/lpython/src/lpython/python_ast.h", line 1882
    void visit_stmt(const stmt_t &b) { visit_stmt_t(b, self()); }
  File "/Users/thebigbool/repos/lpython/src/lpython/python_ast.h", line 1750
    case stmtType::FunctionDef: { v.visit_FunctionDef((const FunctionDef_t &)x); return; }
  File "/Users/thebigbool/repos/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 3389
    ASR::ttype_t *arg_type = ast_expr_to_asr_type(x.base.base.loc, *x.m_args.m_args[i].m_annotation);
  File "/Users/thebigbool/repos/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 1540
    this->visit_expr(*s->m_slice);
  File "/Users/thebigbool/repos/lpython/src/lpython/python_ast.h", line 1909
    void visit_expr(const expr_t &b) { visit_expr_t(b, self()); }
  File "/Users/thebigbool/repos/lpython/src/lpython/python_ast.h", line 1815
    case exprType::Tuple: { v.visit_Tuple((const Tuple_t &)x); return; }
  File "/Users/thebigbool/repos/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 2674
    this->visit_expr(*x.m_elts[i]);
  File "/Users/thebigbool/repos/lpython/src/lpython/python_ast.h", line 1909
    void visit_expr(const expr_t &b) { visit_expr_t(b, self()); }
  File "/Users/thebigbool/repos/lpython/src/lpython/python_ast.h", line 1814
    case exprType::List: { v.visit_List((const List_t &)x); return; }
  File "/Users/thebigbool/repos/lpython/src/lpython/python_ast.h", line 1941
    void visit_List(const List_t & /* x */) { throw LCompilersException("visit_List() not implemented"); }
LCompilersException: visit_List() not implemented

Metadata

Metadata

Assignees

Labels

asrASR related changesenhancementNew feature or request

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions