let virtualMachine = function (program) { let programCounter = 0 let stack = [] let stackPointer = 0 while (programCounter < program.length) { let currentInstruction = program[programCounter] switch (currentInstruction) { case 'PUSH': stack[stackPointer] = program[programCounter + 1] stackPointer++ programCounter++ break case 'ADD': right = stack[stackPointer - 1] stackPointer-- left = stack[stackPointer - 1] stackPointer-- stack[stackPointer] = left + right stackPointer++ break case 'MINUS': right = stack[stackPointer - 1] stackPointer-- left = stack[stackPointer - 1] stackPointer-- stack[stackPointer] = left - right stackPointer++ break } programCounter++ } console.log('stacktop: ', stack[stackPointer - 1]) } // (3 + 4) - 5 let program = ['PUSH', 3, 'PUSH', 4, 'ADD', 'PUSH', 5, 'MINUS'] virtualMachine(program)

  转载请注明: malred-blog

 上一篇
nuxt3 教程 nuxt3 教程
什么是 Nuxt 根据 Vue 官网的说法 而 Nuxt 是由 Vue 官方团队开发的 SSR 框架 创建项目npx nuxi init todo 项目结构 创建完需要手动安装依赖 cd todo npm i # 启动
2023-08-11
下一篇 
3D教程 Three.js 3D教程 Three.js
2_什么是 webgl 作者做的一些 threejs 案例 3_basic_scene从官网下载 threejs, 然后复制里面的 threejs min 到项目文件夹下 scene const scene = new THRE
2023-07-23
  目录