balls icon indicating copy to clipboard operation
balls copied to clipboard

methods cannot be composed in balls

Open disruptek opened this issue 4 years ago • 1 comments

edit: code in the results branch

I'm trying to write the result operator such that the following test works as you might expect:

  block:
    ## assignment shim with constant
    r = 0
    proc bar(a: int): int {.cps: Cont.} =
      inc r
      noop()
      return a * 2

    proc foo() {.cps: Cont.} =
      inc r
      let x = ... bar(4)  # ie. resolve the continuation
      inc r
      check x == 8

    trampoline foo()
    check r == 3

The ... should resolve the result stashed in the continuation into x.

I get the following error message because my output (a statement list) does not allow me to raise the method definition to top level. I don't expect a statement list to be a level, per se, and I don't know how a macro could ever return more than a single method unless this limit is lifted.

/home/adavidoff/git/cps/tests/taste.nim(48, 1) template/generic instantiation of `testes` from here
/home/adavidoff/git/cps/tests/taste.nim(539, 31) template/generic instantiation of `cps` from here
/home/adavidoff/git/cps/cps/environment.nim(214, 16) Error: 'method' is only allowed at top level

For clarity, here's a tree dump of the before/after AST.

=== .cps. on bar(original)  === /home/adavidoff/git/cps/tests/taste.nim(539, 6)
ProcDef
  Sym "bar"
  Empty
  Empty
  FormalParams
    Sym "int"
    IdentDefs
      Sym "a"
      Sym "int"
      Empty
  Empty
  Empty
  StmtList
    Command
      Sym "inc"
      Sym "r"
      IntLit 1
    Call
      Sym "noop"
    ReturnStmt
      Asgn
        Sym "result"
        Infix
          Sym "*"
          Sym "a"
          IntLit 2
  Sym "result"
nnkIdentDefs	a: int
storing type env_402657855
next type env_402657858
=== .cps. on bar(transform) === /home/adavidoff/git/cps/tests/taste.nim(539, 6)
StmtList
  MethodDef
    AccQuoted
      Ident "..."
    Empty
    Empty
    FormalParams
      Ident "int"
      IdentDefs
        Ident "continuation"
        Ident "env_402657855"
        Empty
    Pragma
      Sym "cpsLift"
    Empty
    DotExpr
      Call
        Ident "env_402657855"
        Ident "continuation"
      Ident "result_402657854"
  TypeSection
    TypeDef
      Ident "env_402657855"
      Empty
      RefTy
        ObjectTy
          Empty
          OfInherit
            Ident "Cont"
          RecList
            IdentDefs
              Ident "result_402657854"
              Ident "int"
              Empty
            IdentDefs
              Ident "a_402657856"
              Ident "int"
              Empty
  ProcDef
    Ident "afterCall_402657857"
    Empty
    Empty
    FormalParams
      Ident "Cont"
      IdentDefs
        Ident "continuation"
        Ident "Cont"
        Empty
    Pragma
      Sym "cpsLift"
    Empty
    Empty
  ProcDef
    Ident "afterCall_402657857"
    Empty
    Empty
    FormalParams
      Ident "Cont"
      IdentDefs
        Ident "continuation"
        Ident "Cont"
        Empty
    Pragma
      Sym "cpsLift"
    Empty
    StmtList
      CommentStmt "saften at 866 of cps.nim"
      StmtList
        Asgn
          DotExpr
            Call
              Ident "env_402657855"
              Ident "continuation"
            Ident "result_402657854"
          Infix
            Ident "*"
            DotExpr
              Call
                Ident "env_402657855"
                Ident "continuation"
              Ident "a_402657856"
            IntLit 2
        ReturnStmt
          Empty
      CommentStmt "omit return call from nnkStmtList"
  ProcDef
    Ident "bar"
    Empty
    Empty
    FormalParams
      Sym "Cont"
      IdentDefs
        Ident "continuation"
        Sym "Cont"
        Empty
    Pragma
      Ident "cpsCall"
    Empty
    StmtList
      CommentStmt "saften at 539 of taste.nim"
      StmtList
        CommentStmt "saften at 540 of taste.nim"
        Command
          Ident "inc"
          Ident "r"
          IntLit 1
        StmtList
          CommentStmt "re-use the local continuation by setting the fn"
          Asgn
            DotExpr
              Ident "continuation"
              Ident "fn"
            Ident "afterCall_402657857"
          ReturnStmt
            Call
              Ident "noop"
              Ident "continuation"
      CommentStmt "omit return call from nnkStmtList"
  ProcDef
    Ident "bar"
    Empty
    Empty
    FormalParams
      Ident "Cont"
      IdentDefs
        Ident "a"
        Ident "int"
        Empty
    Empty
    Empty
    StmtList
      Asgn
        Ident "result"
        ObjConstr
          Ident "env_402657855"
          ExprColonExpr
            Ident "fn"
            Ident "bar"
          ExprColonExpr
            Ident "a_402657856"
            Ident "a"

disruptek avatar Jan 11 '21 03:01 disruptek

So, yeah, it works if you're not a nimpleton who's trying to write tests using a unittest framework that might move methods out of toplevel because, what the fuck, why not?

disruptek avatar Jan 11 '21 04:01 disruptek