GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - koshox/pythonvm: 《自己动手写python虚拟机》C++实现

BranchesTags
Open more actions menu

Latest commit

 

History

35 Commits
35 Commits

Folders and files

NameName
Last commit message
Last commit date
code
code
 
 
extlib
extlib
 
 
inc
inc
 
 
lib
lib
 
 
memory
memory
 
 
object
object
 
 
runtime
runtime
 
 
test
test
 
 
util
util
 
 
.gitignore
.gitignore
 
 
CMakeLists.txt
CMakeLists.txt
 
 
README.md
README.md
 
 
main.cpp
main.cpp
 
 

Repository files navigation

自己动手写Python虚拟机

《自己动手写Python虚拟机》源码

开发环境

win10 + cygwin + clion

主要功能

  1. 基本实现了书上所有的功能,从零构建一个简单的Python虚拟机。

  2. 除了GC还存在一些问题,需要参考作者的新项目HiLang改造为链表记录对象的创建。

  3. 对随书源码的几个小问题做了修改,CMakeLists也做了一点调整。

最后一章实现了yield和generator以后,就可以写出一个最简单的协程:

# Two simple generator functions
def countdown(n):
    while n > 0:
        print('T-minus', n)
        yield
        n -= 1
    print('Blastoff!')


def countup(n):
    x = 0
    while x < n:
        print('Counting up', x)
        yield
        x += 1


class TaskScheduler:
    def __init__(self):
        self._task_queue = []

    def new_task(self, task):
        '''
        Admit a newly started task to the scheduler
        '''
        self._task_queue.append(task)

    def run(self):
        '''
        Run until there are no more tasks
        '''
        while len(self._task_queue) > 0:
            task = self._task_queue.popleft()
            try:
                # Run until the next yield statement
                task.next()
                self._task_queue.append(task)
            except StopIteration:
                # Generator is no longer executing
                pass


# Example use
sched = TaskScheduler()
sched.new_task(countdown(2))
sched.new_task(countup(5))
sched.run()

输出结果:

[T-minus, 2]
[Counting up, 0]
[T-minus, 1]
[Counting up, 1]
Blastoff!
[Counting up, 2]
[Counting up, 3]
[Counting up, 4]

About

《自己动手写python虚拟机》C++实现

Topics

pythonvmvirtual-machine

Resources

Readme
Activity

Stars

79 stars

Watchers

4 watching

Forks

27 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.