Talk:Statement Reference

Revision as of 16:21, 4 November 2012 by imported>Cipscis (→‎Lazy evaluation of expressions?: Yes, it does)

Lazy evaluation of expressions?

Does Papyrus use it?

e.g.: Given two functions "A" and "B". Both return Bool. Assuming Lazy Evaluation. In the following code example function B would not be executed if function A returned TRUE.

If ( A() || B() )
  ; some code
EndIf

ref: Lazy Evaluation --Jbezorg (talk) 13:16, 4 November 2012 (EST)

Yes, Papyrus does do this. Here's the generated assembly for something like the code you've given (with "Return True" inside the conditional statement):
        .function Foo 
          .userFlags 0
          .docString ""
          .return Bool
          .paramTable
          .endParamTable
          .localTable
            .local ::temp0 bool
            .local ::temp1 bool
          .endLocalTable
          .code
            CALLMETHOD A self ::temp0  ;@line 11
            CAST ::temp0 ::temp0 ;@line 11
            JUMPT ::temp0 label1 ;@line 11
            CALLMETHOD B self ::temp1  ;@line 11
            CAST ::temp0 ::temp1 ;@line 11
            label1:
            JUMPF ::temp0 label2 ;@line 11
            RETURN True ;@line 12
            JUMP label0
            label2:
            label0:
          .endCode
        .endFunction
And yes, those CAST instructions are superfluous. There are a lot of optimisations that *could* be done in the compilation process that aren't done.
The && operator also uses lazy evaluation:
Function Bar()
	Bool Foobar = 0 && 3
EndFunction
        .function Bar 
          .userFlags 0
          .docString ""
          .return NONE
          .paramTable
          .endParamTable
          .localTable
            .local ::temp0 bool
            .local Foobar Bool
          .endLocalTable
          .code
            CAST ::temp0 0 ;@line 4
            JUMPF ::temp0 label0 ;@line 4
            CAST ::temp0 3 ;@line 4
            label0:
            ASSIGN Foobar ::temp0 ;@line 4
          .endCode
        .endFunction
Note that it is entirely possible to write your own functions in Papyrus assembly and assemble them directly, or edit the assembly by hand if you want to optimise it further than the compiler does.
-- Cipscis (talk) 16:21, 4 November 2012 (EST)
Return to "Statement Reference" page.