美文网首页
变量版解释器

变量版解释器

作者: 浑身演技 | 来源:发表于2017-05-10 17:40 被阅读10次
package com.moon.tina

/**
  * Created by lin on 2017/5/10.
  */
case class Instruction(name:String,pos:Int)

case class ExecuteInstructions(instructions:List[Instruction],numbers:List[Int],names:List[String])

case class TinaInterpreter() {
  var stack:List[Int]=List[Int]()
  var environment:Map[String,Int]=Map[String,Int]()
  var answer:Int= _

  def loadValue(num:Int): Unit ={
    stack=num +:stack
  }

  def printAnswer(): Unit ={
    answer=stack.head
  }

  def addTwoValues(): Unit ={
    val firstNum=stack.head
    stack=stack.drop(1)
    val secondNum=stack.head
    stack=stack.drop(1)
    val total=firstNum+secondNum
    stack=total+:stack
  }

  def storeName(name:String): Unit ={
    val value=stack.head
    stack=stack.drop(1)
    environment=environment+(name->value)
  }

  def loadName(name:String): Unit ={
    val value=environment(name)
    stack=value+:stack
  }

  def parseArgument(name:String,pos:Int,eInstructions:ExecuteInstructions):Any={
    if (name=="LOAD_VALUE"){
      pos
    }else if(name=="LOAD_NAME"||name=="STORE_NAME"){
      eInstructions.names(pos)
    }else None
  }

  def run(einstructions: ExecuteInstructions): Unit ={
    val instructions=einstructions.instructions
    val numbers=einstructions.numbers
    for(eachStep <- instructions){
      val argument=parseArgument(eachStep.name,eachStep.pos,einstructions)
      if(eachStep.name=="LOAD_VALUE"){
        val number=numbers(argument.asInstanceOf[Int])
        loadValue(number)
      }else if(eachStep.name=="ADD_TWO_VALUES"){
        addTwoValues()
      }else if(eachStep.name=="PRINT_ANSWER"){
        printAnswer()
      }else if(eachStep.name=="STORE_NAME"){
        storeName(argument.asInstanceOf[String])
      }else if(eachStep.name=="LOAD_NAME"){
        loadName(argument.asInstanceOf[String])
      }

    }
  }
}

object TinaInterpreter{
  def main(args: Array[String]) {
    val whatToExecute=ExecuteInstructions(List(
      Instruction("LOAD_VALUE",0),
      Instruction("STORE_NAME",0),
      Instruction("LOAD_VALUE",1),
      Instruction("STORE_NAME",1),
      Instruction("LOAD_NAME",0),
      Instruction("LOAD_NAME",1),
      Instruction("ADD_TWO_VALUES",-1),
      Instruction("LOAD_VALUE",2),
      Instruction("ADD_TWO_VALUES",-1),
      Instruction("PRINT_ANSWER",-1)
    ),
      List(7,13,21),
      List("a","b")
    )

    val interpreter=TinaInterpreter()
    interpreter.run(whatToExecute)
    println(interpreter.stack)
  }
}

相关文章

  • 变量版解释器

  • shell基础(一)

    一:shell脚本内容解释器 二:shell脚本的执行方式 三:变量 变量分为:环境(全局)变量和普通(局部)变量...

  • 简单版解释器

  • JS写得更漂亮

    1.定义变量的时候要指明类型,告诉 JS解释器这个变量是什么数据类型的,而不要让解释器去猜,例如不好的写法: 声明...

  • 2018-10-11:JS与jQuery基础

    JS基础 Java: 面向对象 编译型 强变量 服务器JS: 基于对象 解释型 弱变量 客户端 编译和解释:编...

  • 2020-05-25 bash脚本编程 一 变量、变量类型等

    一 变量、变量类型等 shell编程 编译器,解释器 编程语言:机器语言(0/1)、汇编语言、高级语言: 静态...

  • Python3 模块

    用 python 解释器来编程从 Python 解释器退出再进入,那么你定义的所有的方法和变量就都消失了。 为此...

  • 黑猴子的家:Python Anaconda 运行 Jupyter

    1、概述 IPython 是一个 python 的交互式解释器,比默认的python 解释器好用得多,支持变量自动...

  • runoob js第六天

    JavaScript 变量提升 变量提升:函数声明和变量声明总是会被解释器悄悄地被"提升"到方法体的最顶部。变量可...

  • lua解释器(变量以及表)

    本篇主要来结合着lua的代码,配合lua源码来阅读lua的汇编代码,具体反汇编工具可以用ChunkSpy这个工具,...

网友评论

      本文标题:变量版解释器

      本文链接:https://www.haomeiwen.com/subject/nemgtxtx.html