Source

jaspyon / jaspyon / operations.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
# encoding: utf-8
"""
operations.py
Implements the javascript operations nodes for the interpretation tree
"""
try:
    from pypy.rlib.unroll import unrolling_iterable
except ImportError:
    from jaspyon.rlib.unroll import unrolling_iterable

from jaspyon.rlib.parsing.ebnfparse import Symbol, Nonterminal
from jaspyon.jsobj import W_IntNumber, W_FloatNumber, W_Object,\
     w_Undefined, W_NewBuiltin, W_String, create_object, W_List,\
     W_PrimitiveObject, ActivationObject, W_Array, W_Boolean,\
     w_Null, W_BaseNumber, isnull_or_undefined
from jaspyon.execution import JsTypeError, ThrowException
from jaspyon.jscode import JsCode, JsFunction
from constants import unescapedict

import sys
import os

class Position(object):
    def __init__(self, lineno=-1, start=-1, end=-1):
        self.lineno = lineno
        self.start = start
        self.end = end

    
class Node(object):
    """
    Node is the base class for all the other nodes.
    """
    def __init__(self, pos):
        """
        Initializes the content from the AST specific for each node type
        """
        raise NotImplementedError()

    def emit(self, bytecode):
        """ Emits bytecode
        """
        raise NotImplementedError()
    
    def get_literal(self):
        raise NotImplementedError()
    
    def get_args(self, ctx):
        raise NotImplementedError()
    
    def __str__(self):
        return "%s()"%(self.__class__)

class Statement(Node):
    def __init__(self, pos):
        self.pos = pos

class ExprStatement(Node):
    def __init__(self, pos, expr):
        self.pos = pos
        self.expr = expr

    def emit(self, bytecode):
        self.expr.emit(bytecode)
        bytecode.emit('POP')

class Expression(Statement):
    pass

class ListOp(Expression):
    def __init__(self, pos, nodes):
        self.pos = pos
        self.nodes = nodes

def create_unary_op(name):
    class UnaryOp(Expression):
        def __init__(self, pos, expr, postfix=False):
            self.pos = pos
            self.expr = expr
            self.postfix = postfix

        def emit(self, bytecode):
            self.expr.emit(bytecode)
            bytecode.emit(name)
    UnaryOp.__name__ = name
    return UnaryOp

def create_binary_op(name):
    class BinaryOp(Expression):
        def __init__(self, pos, left, right):
            self.pos = pos
            self.left = left
            self.right = right

        def emit(self, bytecode):
            self.left.emit(bytecode)
            self.right.emit(bytecode)
            bytecode.emit(name)
    BinaryOp.__name__ = name
    return BinaryOp

class Undefined(Statement):
    def emit(self, bytecode):
        bytecode.emit('LOAD_UNDEFINED')

class PropertyInit(Expression):
    def __init__(self, pos, lefthand, expr):
        self.pos = pos
        self.lefthand = lefthand
        self.expr = expr
    
    def emit(self, bytecode):
        self.expr.emit(bytecode)
        if isinstance(self.lefthand, Identifier):
            bytecode.emit('LOAD_STRINGCONSTANT', self.lefthand.name)
        else:
            self.lefthand.emit(bytecode)

class Array(ListOp):
    def emit(self, bytecode):
        for element in self.nodes:
            element.emit(bytecode)
        bytecode.emit('LOAD_ARRAY', len(self.nodes))

class Assignment(Expression):
    def _get_name(self):
        addoper = OPERANDS[self.operand]
        if addoper:
            addoper = '_' + self.prefix.upper() + addoper
        else:
            addoper = ''
        return addoper

OPERANDS = {
    '='  : '',
    '+=' : 'ADD',
    '-=' : 'SUB',
    '*=' : 'MUL',
    '/=' : 'DIV',
    '++' : 'INCR',
    '--' : 'DECR',
    '%=' : 'MOD',
    '&=' : 'BITAND',
    '|=' : 'BITOR',
    '^=' : 'BITXOR',
    }

class SimpleIncrement(Expression):
    def __init__(self, pos, left, atype):
        self.pos   = pos
        self.left  = left
        self.atype = atype

    def emit(self, bytecode):
        self.left.emit(bytecode)
        if self.atype == '++':
            bytecode.emit('INCR')
        elif self.atype == '--':
            bytecode.emit('DECR')

class SimpleAssignment(Assignment):
    def __init__(self, pos, left, right, operand, prefix=''):
        self.identifier = left.get_literal()
        self.right = right
        self.pos = pos
        self.operand = operand
        self.prefix = prefix

    def emit(self, bytecode):
        if self.right is not None:
            self.right.emit(bytecode)
        bytecode_name = 'STORE' + self._get_name()
        bytecode.emit_store(bytecode_name, self.identifier)

class VariableAssignment(Assignment):
    def __init__(self, pos, left, right, operand):
        xxx # shall never land here for now
        self.identifier = left.identifier
        self.right = right
        self.pos = pos
        self.operand = operand
        self.depth = left.depth

    def emit(self, bytecode):
        self.right.emit(bytecode)
        bytecode.emit('STORE_VAR', self.depth, self.identifier)

class MemberAssignment(Assignment):
    def __init__(self, pos, what, item, right, operand, prefix=''):
        # XXX we can optimise here what happens if what is identifier,
        #     but let's leave it alone for now
        self.pos = pos
        self.what = what
        self.item = item
        self.right = right
        self.operand = operand
        self.prefix = prefix

    def emit(self, bytecode):
        if self.right is not None:
            self.right.emit(bytecode)
        self.item.emit(bytecode)
        self.what.emit(bytecode)
        bytecode.emit_store_member('STORE_MEMBER' + self._get_name())

class MemberDotAssignment(Assignment):
    def __init__(self, pos, what, name, right, operand, prefix=''):
        self.pos = pos
        self.what = what
        self.itemname = name
        self.right = right
        self.operand = operand
        self.prefix = prefix

    def emit(self, bytecode):
        # XXX optimize this a bit
        if self.right is not None:
            self.right.emit(bytecode)
        bytecode.emit('LOAD_STRINGCONSTANT', self.itemname)
        self.what.emit(bytecode)
        bytecode.emit_store_member('STORE_MEMBER' + self._get_name())

class Block(Statement):
    def __init__(self, pos, nodes):
        self.pos = pos
        self.nodes = nodes

    def emit(self, bytecode):
        for node in self.nodes:
            node.emit(bytecode)
    
BitwiseAnd = create_binary_op('BITAND')
BitwiseXor = create_binary_op('BITXOR')
BitwiseOr = create_binary_op('BITOR')

BitwiseNot = create_unary_op('BITNOT')

class Unconditional(Statement):
    def __init__(self, pos, target):
        self.pos = pos
        self.target = target
    
class Break(Unconditional):
    def emit(self, bytecode):
        assert self.target is None
        bytecode.emit_break()    

class Continue(Unconditional):
    def emit(self, bytecode):
        assert self.target is None
        bytecode.emit_continue()

class Call(Expression):
    def __init__(self, pos, left, args):
        self.pos = pos
        self.left = left
        self.args = args
    
    def emit(self, bytecode):
        self.args.emit(bytecode)
        left = self.left
        if isinstance(left, MemberDot):
            left.left.emit(bytecode)
            # XXX optimise
            bytecode.emit('LOAD_STRINGCONSTANT', left.name)
            bytecode.emit('CALL_METHOD')
        elif isinstance(left, Member):
            raise NotImplementedError
        else:
            left.emit(bytecode)
            bytecode.emit('CALL')

Comma = create_binary_op('COMMA')    

class Conditional(Expression):
    def __init__(self, pos, condition, truepart, falsepart):
        self.pos = pos
        self.condition = condition
        self.truepart = truepart
        self.falsepart = falsepart

class Member(Expression):
    "this is for object[name]"
    def __init__(self, pos, left, expr):
        self.pos = pos
        self.left = left
        self.expr = expr

    def emit(self, bytecode):
        self.left.emit(bytecode)
        self.expr.emit(bytecode)
        bytecode.emit('LOAD_ELEMENT')

class MemberDot(Expression):
    "this is for object.name"
    def __init__(self, pos, left, name):
        self.name = name.get_literal()
        self.left = left
        self.pos = pos
    
    def emit(self, bytecode):
        self.left.emit(bytecode)
        bytecode.emit('LOAD_MEMBER', self.name)    

class FunctionStatement(Statement):
    def __init__(self, pos, name, params, body):
        self.pos = pos
        if name is None:
            self.name = None
        else:
            self.name = name.get_literal()
        self.body = body
        self.params = params

    def emit(self, bytecode):
        code = JsCode()
        if self.body is not None:
            self.body.emit(code)
        funcobj = JsFunction(self.name, self.params, code)
        bytecode.emit('DECLARE_FUNCTION', funcobj)
        if self.name is None:
            bytecode.emit('LOAD_FUNCTION', funcobj)
        #else:
        #    bytecode.emit('LOAD_FUNCTION', funcobj)
        #    bytecode.emit('STORE', self.name)
        #    bytecode.emit('POP')

class Identifier(Expression):
    def __init__(self, pos, name):
        self.pos = pos
        self.name = name

    def emit(self, bytecode):
        bytecode.emit('LOAD_VARIABLE', self.name)
        
    def get_literal(self):
        return self.name
    

class This(Identifier):
    pass
    

class If(Statement):
    def __init__(self, pos, condition, thenpart, elsepart=None):
        self.pos = pos
        self.condition = condition
        self.thenPart = thenpart
        self.elsePart = elsepart

    def emit(self, bytecode):
        self.condition.emit(bytecode)
        one = bytecode.prealocate_label()
        bytecode.emit('JUMP_IF_FALSE', one)
        self.thenPart.emit(bytecode)
        if self.elsePart is not None:
            two = bytecode.prealocate_label()
            bytecode.emit('JUMP', two)
            bytecode.emit('LABEL', one)
            self.elsePart.emit(bytecode)
            bytecode.emit('LABEL', two)
        else:
            bytecode.emit('LABEL', one)

#class Group(UnaryOp):
#    def eval(self, ctx):
#        return self.expr.eval(ctx)

##############################################################################
#
# Binary logic comparison ops and suporting abstract operation
#
##############################################################################

class And(Expression):
    def __init__(self, pos, left, right):
        self.pos = pos
        self.left = left
        self.right = right
    
    def emit(self, bytecode):
        self.left.emit(bytecode)
        one = bytecode.prealocate_label()
        bytecode.emit('JUMP_IF_FALSE_NOPOP', one)
        self.right.emit(bytecode)
        bytecode.emit('LABEL', one)

class Or(Expression):
    def __init__(self, pos, left, right):
        self.pos = pos
        self.left = left
        self.right = right

    def emit(self, bytecode):
        self.left.emit(bytecode)
        one = bytecode.prealocate_label()
        bytecode.emit('JUMP_IF_TRUE_NOPOP', one)
        self.right.emit(bytecode)
        bytecode.emit('LABEL', one)        
        
Ge = create_binary_op('GE')
Gt = create_binary_op('GT')    
Le = create_binary_op('LE')    
Lt = create_binary_op('LT')        

##############################################################################
#
# Bitwise shifts
#
##############################################################################

Ursh = create_binary_op('URSH')
Rsh = create_binary_op('RSH')
Lsh = create_binary_op('LSH')

##############################################################################
#
# Equality and unequality (== and !=)
#
##############################################################################

Eq = create_binary_op('EQ')
Ne = create_binary_op('NE')

##############################################################################
#
# Strict Equality and unequality, usually means same place in memory
# or equality for primitive values
#
##############################################################################

StrictEq = create_binary_op('IS')
StrictNe = create_binary_op('ISNOT')    

In = create_binary_op('IN')

class Typeof(Expression):
    def __init__(self, pos, left):
        self.pos = pos
        self.left = left

    def emit(self, bytecode):
        # obscure hack to be compatible
        if isinstance(self.left, Identifier):
            bytecode.emit('TYPEOF_VARIABLE', self.left.name)
        else:
            self.left.emit(bytecode)
            bytecode.emit('TYPEOF')

class Delete(Expression):
    def __init__(self, pos, what):
        self.pos = pos
        self.what = what

    def emit(self, bytecode):
        what = self.what
        if isinstance(what, Identifier):
            bytecode.emit('DELETE', what.name)
        elif isinstance(what, VariableIdentifier):
            bytecode.emit('DELETE', what.identifier)
        elif isinstance(what, MemberDot):
            what.left.emit(bytecode)
            # XXX optimize
            bytecode.emit('LOAD_STRINGCONSTANT', what.name)
            bytecode.emit('DELETE_MEMBER')
        elif isinstance(what, Member):
            what.left.emit(bytecode)
            what.expr.emit(bytecode)
            bytecode.emit('DELETE_MEMBER')
        else:
            what.left.emit(bytecode)
            bytecode.emit('LOAD_BOOLCONSTANT', True)

    #def emit(self, bytecode):
    #    

#class Index(BinaryOp):
#    def eval(self, ctx):
#        w_obj = self.left.eval(ctx).GetValue().ToObject(ctx)
#        name= self.right.eval(ctx).GetValue().ToString(ctx)
#        return W_Reference(name, w_obj)

class ArgumentList(ListOp):
    def emit(self, bytecode):
        for node in self.nodes:
            node.emit(bytecode)
        bytecode.emit('LOAD_LIST', len(self.nodes))

##############################################################################
#
# Math Ops
#
##############################################################################

Plus = create_binary_op('ADD')
Mult = create_binary_op('MUL')
Mod = create_binary_op('MOD')
Division = create_binary_op('DIV')
Sub = create_binary_op('SUB')

class Null(Expression):
    def emit(self, bytecode):
        bytecode.emit('LOAD_NULL')

##############################################################################
#
# Value and object creation
#
##############################################################################

class New(Expression):
    def __init__(self, pos, left):
        self.pos = pos
        self.left = left

    def emit(self, bytecode):
        self.left.emit(bytecode)
        bytecode.emit('NEW_NO_ARGS')
    
class NewWithArgs(Expression):
    def __init__(self, pos, left, right):
        self.pos = pos
        self.left = left
        self.right = right
    
    def emit(self, bytecode):
        self.left.emit(bytecode)
        self.right.emit(bytecode)
        bytecode.emit('NEW')

class BaseNumber(Expression):
    pass
    
class IntNumber(BaseNumber):
    def __init__(self, pos, num):
        self.pos = pos
        self.num = num

    def emit(self, bytecode):
        bytecode.emit('LOAD_INTCONSTANT', self.num)

class FloatNumber(BaseNumber):
    def __init__(self, pos, num):
        self.pos = pos
        self.num = num

    def emit(self, bytecode):
        bytecode.emit('LOAD_FLOATCONSTANT', self.num)

class String(Expression):
    def __init__(self, pos, strval):
        self.pos = pos
        self.strval = self.string_unquote(strval)

    def emit(self, bytecode):
        bytecode.emit('LOAD_STRINGCONSTANT', self.strval)
    
    def string_unquote(self, string):
        # XXX I don't think this works, it's very unlikely IMHO
        #     test it
        temp = []
        stop = len(string)-1
        # XXX proper error
        assert stop >= 0
        last = ""
        
        #removing the begining quotes (" or \')
        if string.startswith('"'):
            singlequote = False
        else:
            singlequote = True

        internalstring = string[1:stop]
        
        for c in internalstring:
            if last == "\\":
                # Lookup escape sequence. Ignore the backslash for 
                # unknown escape sequences (like SM)
                unescapeseq = unescapedict.get(last+c, c)
                temp.append(unescapeseq)
                c = ' ' # Could be anything
            elif c != "\\":
                temp.append(c)
            last = c
        return ''.join(temp)

class ObjectInit(ListOp):
    def emit(self, bytecode):
        for prop in self.nodes:
            prop.emit(bytecode)
        bytecode.emit('LOAD_OBJECT', len(self.nodes))

class SourceElements(Statement):
    """
    SourceElements nodes are found on each function declaration and in global code
    """
    def __init__(self, pos, var_decl, func_decl, nodes, sourcename = ''):
        self.pos = pos
        self.var_decl = var_decl
        self.func_decl = func_decl
        self.nodes = nodes
        self.sourcename = sourcename

    def emit(self, bytecode):
        for varname in self.var_decl:
            bytecode.emit('DECLARE_VAR', varname)
        for funcname, funccode in self.func_decl.items():
            funccode.emit(bytecode)

        for node in self.nodes:
            node.emit(bytecode)    

class Program(Statement):
    def __init__(self, pos, body):
        self.pos = pos
        self.body = body

    def emit(self, bytecode):
        self.body.emit(bytecode)

class Return(Statement):
    def __init__(self, pos, expr):
        self.pos = pos
        self.expr = expr

    def emit(self, bytecode):
        if self.expr is None:
            bytecode.emit('LOAD_UNDEFINED')
        else:
            self.expr.emit(bytecode)
        bytecode.emit('RETURN')

class Throw(Statement):
    def __init__(self, pos, exp):
        self.pos = pos
        self.exp = exp

    def emit(self, bytecode):
        self.exp.emit(bytecode)
        bytecode.emit('THROW')

class Try(Statement):
    def __init__(self, pos, tryblock, catchparam, catchblock, finallyblock):
        self.pos = pos
        self.tryblock = tryblock
        self.catchparam = catchparam
        self.catchblock = catchblock
        self.finallyblock = finallyblock

    def emit(self, bytecode):
        # a bit naive operator for now
        trycode = JsCode()
        self.tryblock.emit(trycode)
        if self.catchblock:
            catchcode = JsCode()
            self.catchblock.emit(catchcode)
        else:
            catchcode = None
        if self.finallyblock:
            finallycode = JsCode()
            self.finallyblock.emit(finallycode)
        else:
            finallycode = None
        bytecode.emit('TRYCATCHBLOCK', trycode, self.catchparam.get_literal(),
                      catchcode, finallycode)    

class VariableDeclaration(Expression):
    def __init__(self, pos, identifier, expr=None):
        self.pos = pos
        self.identifier = identifier.get_literal()
        self.expr = expr

    def emit(self, bytecode):
        if self.expr is not None:
            self.expr.emit(bytecode)
            bytecode.emit('STORE', self.identifier)

class VariableIdentifier(Expression):
    def __init__(self, pos, depth, identifier):
        self.pos = pos
        self.depth = depth
        self.identifier = identifier

    def emit(self, bytecode):
        bytecode.emit('LOAD_VARIABLE', self.identifier)

    def get_literal(self):
        return self.identifier

class VariableDeclList(Statement):
    def __init__(self, pos, nodes):
        self.pos = pos
        self.nodes = nodes

    def emit(self, bytecode):
        for node in self.nodes:
            node.emit(bytecode)
            if isinstance(node, VariableDeclaration) and node.expr is not None:
                bytecode.emit('POP')                
    
class Variable(Statement):
    def __init__(self, pos, body):
        self.pos = pos
        self.body = body

    def emit(self, bytecode):
        self.body.emit(bytecode)

class Empty(Expression):
    def __init__(self, pos):
        self.pos = pos

    def emit(self, bytecode):
        pass

class Void(Expression):
    def __init__(self, pos, expr):
        self.pos = pos
        self.expr = expr
    
    def emit(self, bytecode):
        self.expr.emit(bytecode)
        bytecode.emit('POP')
        bytecode.emit('LOAD_UNDEFINED')

class With(Statement):
    def __init__(self, pos, expr, body):
        self.pos = pos
        self.expr = expr
        self.body = body

    def emit(self, bytecode):
        self.expr.emit(bytecode)
        bytecode.emit('WITH_START')
        self.body.emit(bytecode)
        bytecode.emit('WITH_END')

class WhileBase(Statement):
    def __init__(self, pos, condition, body):
        self.pos = pos
        self.condition = condition
        self.body = body

class Do(WhileBase):
    opcode = 'DO'
    
    def emit(self, bytecode):
        startlabel = bytecode.emit_startloop_label()
        end = bytecode.prealocate_endloop_label()
        self.body.emit(bytecode)
        self.condition.emit(bytecode)
        bytecode.emit('JUMP_IF_TRUE', startlabel)
        bytecode.emit_endloop_label(end)
    
class While(WhileBase):
    def emit(self, bytecode):
        startlabel = bytecode.emit_startloop_label()
        self.condition.emit(bytecode)
        endlabel = bytecode.prealocate_endloop_label()
        bytecode.emit('JUMP_IF_FALSE', endlabel)
        self.body.emit(bytecode)
        bytecode.emit('JUMP', startlabel)
        bytecode.emit_endloop_label(endlabel)    

class ForVarIn(Statement):
    def __init__(self, pos, vardecl, lobject, body):
        self.pos = pos
        assert isinstance(vardecl, VariableDeclaration)
        self.iteratorname = vardecl.identifier
        self.object = lobject
        self.body = body

    
    def emit(self, bytecode):
        bytecode.emit('DECLARE_VAR', self.iteratorname)
        self.object.emit(bytecode)
        bytecode.emit('LOAD_ITERATOR')
        precond = bytecode.emit_startloop_label()
        finish = bytecode.prealocate_endloop_label()
        bytecode.emit('JUMP_IF_ITERATOR_EMPTY', finish)
        bytecode.emit('NEXT_ITERATOR', self.iteratorname)
        self.body.emit(bytecode)
        bytecode.emit('JUMP', precond)
        bytecode.emit_endloop_label(finish)
        bytecode.emit('POP')    

class ForIn(Statement):
    def __init__(self, pos, name, lobject, body):
        self.pos = pos
        #assert isinstance(iterator, Node)
        self.iteratorname = name
        self.object = lobject
        self.body = body

    def emit(self, bytecode):
        self.object.emit(bytecode)
        bytecode.emit('LOAD_ITERATOR')
        precond = bytecode.emit_startloop_label()
        finish = bytecode.prealocate_endloop_label()
        bytecode.emit('JUMP_IF_ITERATOR_EMPTY', finish)
        bytecode.emit('NEXT_ITERATOR', self.iteratorname)
        self.body.emit(bytecode)
        bytecode.emit('JUMP', precond)
        bytecode.emit_endloop_label(finish)
        bytecode.emit('POP')

class For(Statement):
    def __init__(self, pos, setup, condition, update, body):
        self.pos = pos
        self.setup = setup
        self.condition = condition
        self.update = update
        self.body = body

    def emit(self, bytecode):
        self.setup.emit(bytecode)
        if isinstance(self.setup, Expression):
            bytecode.emit('POP')
        precond = bytecode.emit_startloop_label()
        finish = bytecode.prealocate_endloop_label()
        update = bytecode.prealocate_updateloop_label()
        self.condition.emit(bytecode)
        bytecode.emit('JUMP_IF_FALSE', finish)
        self.body.emit(bytecode)
        bytecode.emit_updateloop_label(update)
        self.update.emit(bytecode)
        bytecode.emit('POP')
        bytecode.emit('JUMP', precond)
        bytecode.emit_endloop_label(finish)
    
class Boolean(Expression):
    def __init__(self, pos, boolval):
        self.pos = pos
        self.bool = boolval

    def emit(self, bytecode):
        bytecode.emit('LOAD_BOOLCONSTANT', self.bool)

Not = create_unary_op('NOT')
UMinus = create_unary_op('UMINUS')
UPlus = create_unary_op('UPLUS')

unrolling_classes = unrolling_iterable((If, Return, Block, While))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.