Git Product home page Git Product logo

ts-python-interpreter's People

Contributors

nbr avatar nbraga avatar ppegusii avatar r3d3y3 avatar

Watchers

 avatar  avatar  avatar  avatar

ts-python-interpreter's Issues

How is __name__ loaded into the locals/globals/builtins

def main():
    print 'Hello World!'

if __name__ == '__main__':
    main()
./doc/nedpycparser.py ./sample_pycs/helloworld.pyc 
magic 03f30d0a
moddate some (time)
code
   argcount 0
   nlocals 0
   stacksize 2
   flags 0040
   code
      6400008400005a00006501006401006b0200721f00650000830000016e00
      0064020053
  1           0 LOAD_CONST               0 (<code object main at 0x7fbead369e30, file "helloworld.py", line 1>)
              3 MAKE_FUNCTION            0
              6 STORE_NAME               0 (main)

  4           9 LOAD_NAME                1 (__name__)
             12 LOAD_CONST               1 ('__main__')
             15 COMPARE_OP               2 (==)
             18 POP_JUMP_IF_FALSE       31

  5          21 LOAD_NAME                0 (main)
             24 CALL_FUNCTION            0
             27 POP_TOP             
             28 JUMP_FORWARD             0 (to 31)
        >>   31 LOAD_CONST               2 (None)
             34 RETURN_VALUE        
   consts
      code
         argcount 0
         nlocals 0
         stacksize 1
         flags 0043
         code 640100474864000053
  2           0 LOAD_CONST               1 ('Hello World!')
              3 PRINT_ITEM          
              4 PRINT_NEWLINE       
              5 LOAD_CONST               0 (None)
              8 RETURN_VALUE        
         consts
            None
            'Hello World!'
         names ()
         varnames ()
         freevars ()
         cellvars ()
         filename 'helloworld.py'
         name 'main'
         firstlineno 1
         lnotab 0001
      '__main__'
      None
   names ('main', '__name__')
   varnames ()
   freevars ()
   cellvars ()
   filename 'helloworld.py'
   name '<module>'
   firstlineno 1
   lnotab 09030c01

Notice that 4 9 LOAD_NAME 1 (__name__) is called before on tuple index 1 before any STORE_* is called from tuple index 1. Therefore, __name__ must be implicitly stored in locals/globals/builtins at some point before LOAD_NAME. I can't figure out where this is happening. So I'm switching pyc files. ๐Ÿ˜ Here's the new version of helloworld and its corresponding disassembled pyc file:

def phw():
    print 'Hello World!'

phw()
 ./doc/nedpycparser.py ./sample_pycs/helloworld2.pyc 
magic 03f30d0a
moddate some (time)
code
   argcount 0
   nlocals 0
   stacksize 1
   flags 0040
   code 6400008400005a00006500008300000164010053
  1           0 LOAD_CONST               0 (<code object phw at 0x7f169375de30, file "sample_pycs/helloworld2.py", line 1>)
              3 MAKE_FUNCTION            0
              6 STORE_NAME               0 (phw)

  4           9 LOAD_NAME                0 (phw)
             12 CALL_FUNCTION            0
             15 POP_TOP             
             16 LOAD_CONST               1 (None)
             19 RETURN_VALUE        
   consts
      code
         argcount 0
         nlocals 0
         stacksize 1
         flags 0043
         code 640100474864000053
  2           0 LOAD_CONST               1 ('Hello World!')
              3 PRINT_ITEM          
              4 PRINT_NEWLINE       
              5 LOAD_CONST               0 (None)
              8 RETURN_VALUE        
         consts
            None
            'Hello World!'
         names ()
         varnames ()
         freevars ()
         cellvars ()
         filename 'sample_pycs/helloworld2.py'
         name 'phw'
         firstlineno 1
         lnotab 0001
      None
   names ('phw',)
   varnames ()
   freevars ()
   cellvars ()
   filename 'sample_pycs/helloworld2.py'
   name '<module>'
   firstlineno 1
   lnotab 0903

Create classes for all parsed types?

I started writing some stack machine methods and I ran into the following situation with the operation UNARY_INVERT, a more thorough description here.

Consider this situation. We have parsed a TYPE_BINARY_FLOAT whose value is 1.0. After parsing we cannot differentiate it from TYPE_INT with a value of 1. Now this value, a JS 1, is on top of the stack and we have to perform the operation UNARY_INVERT. We should put a TypeError on top of the stack, but we would have no way of knowing this.

I think there are more examples of this such as BINARY_DIVIDE.

Represent 64 bit integers

All numbers in JS are 64 bit floating point. That's a maximum of a 53 bit integer I believe. We need a library or have to create one.

Python version

What version of Python bytecode must we be able to interpret?
If we use the py_compile module to create a pyc, is the same pyc file created no matter what system created it? If not, does this change how we must implement the interpreter?

Represent complex numbers

I have found math.js which is written in JS and MathLib.js writen in TS.

I added MathLib.js to the project by adding it as a requirement in package.json, then added a reference to MathLib.d.ts. I got this error when trying to compile:

node_modules/mathlib/build/MathLib.d.ts(1220,1): error TS1038: 'declare' modifier not allowed for code already in an ambient context.
node_modules/mathlib/build/MathLib.d.ts(1471,1): error TS1038: 'declare' modifier not allowed for code already in an ambient context.
node_modules/mathlib/build/MathLib.d.ts(2625,1): error TS1038: 'declare' modifier not allowed for code already in an ambient context.

After reading this Stack Overflow post, it seems as though the definition file has nested declares. They may have been legal in earlier versions of TS, but no longer. I removed the nested declare key words then got a bunch more errors.

I may be better off trying to write a definition file for math.js, since it's much more popular anyways.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.