python-simple-vm, 在 python 中,實現了一個簡單的虛擬機

分享于 

2分钟阅读

GitHub

  简体 双语
Simple Python VM
  • 源代码名称:python-simple-vm
  • 源代码网址:http://www.github.com/cslarsen/python-simple-vm
  • python-simple-vm源代码文档
  • python-simple-vm源代码下载
  • Git URL:
    git://www.github.com/cslarsen/python-simple-vm.git
    Git Clone代码到本地:
    git clone http://www.github.com/cslarsen/python-simple-vm
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/cslarsen/python-simple-vm
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    

    这是博客"在 python 中制作简单的VM解释器"的代码,你可以在 https://csl.name/post/vm/ 找到它。

    由 Stigen Larsen制作的,来自 R/python的人员的一些改进。 放入 public 域。

    要运行:

    
    Hit CTRL+D or type"exit" to quit.
    
    
    > 2 3 + 5 * println
    
    
    Constant-folded (2 + 3) to 5
    
    
    Constant-folded (5 * 5) to 25
    
    
    25
    
    
    > ^D
    
    
    
    

    要测试:

    
    $ python vm.py test
    
    
    Code before optimization: [2, 3, '+', 5, '*', 'println']
    
    
    Constant-folded (2 + 3) to 5
    
    
    Constant-folded (5 * 5) to 25
    
    
    Code after optimization: [25, 'println']
    
    
    Stack after running original program:
    
    
    25
    
    
    Data stack (top first):
    
    
    Stack after running optimized program:
    
    
    25
    
    
    Data stack (top first):
    
    
    Result: OK
    
    
    ** Program 1: Runs the code for `print((2+3)*4)`
    
    
    20
    
    
    
    ** Program 2: Ask for numbers, computes sum and product.
    
    
    Enter a number: 12
    
    
    Enter another number: 13
    
    
    Their sum is: 25
    
    
    Their product is: 156
    
    
    
    ** Program 3: Shows branching and looping (use CTRL+D to exit).
    
    
    Enter a number: 1
    
    
    The number 1 is odd.
    
    
    Enter a number: 2
    
    
    The number 2 is even.
    
    
    Enter a number: 3
    
    
    The number 3 is odd.
    
    
    Enter a number: ^D
    
    
    
    

    IMP  mac  MACH  Virtual  FOLD  虚拟机  
    相关文章