public
Description: Prototype of the Swarm Distributed Programming Language
Homepage: http://blog.locut.us/category/projects/swarm/
Clone URL: git://github.com/sanity/swarm-proto.git
swarm-proto / src / dpl / Interpreter.scala
100644 69 lines (59 sloc) 1.679 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package dpl
import scala.collection.immutable.IntMap
import scala.collection.mutable.HashMap
 
class Interpreter(myNode : Node) {
  
  def interpret(program : IntMap[Instruction]) : (Stack, LocalVars) = {
    interpret(program, (new Stack, new LocalVars, 0))
  }
  
  def interpret(program : IntMap[Instruction], state : (Stack, LocalVars, Int)) : (Stack, LocalVars) = {
    Stats.instructionsExecuted += 1
    if (program.contains(state._3)) {
// println(""+state+" -> "+state._3+":"+program(state._3))
    } else {
// println("End")
    }
    val (stack, localVars, pc) = state;
    program.get(pc) match {
      case Some(i) => interpret(program, i.execute(myNode, stack, localVars, pc))
      case None => (stack, localVars)
    }
  }
}
 
class LineLabel(label : String) extends ProgramLine {
  var ln = -1
  
  def lineNo = {
    if (ln != -1) ln else throw new RuntimeException("Uninitialized label: "+this)
  }
    override def toString() : String = label+":"+lineNo
}
 
object LL {
  var llMap = HashMap[String, LineLabel]()
  
  def apply(name : String) : LineLabel = {
    llMap.get(name) match {
      case Some(o) => o
      case None => {
        val newll = new LineLabel(name)
        llMap + ((name, newll))
        newll
      }
    }
  }
  
 
}
 
object Interpreter {
  def compile(input : List[ProgramLine]) : IntMap[Instruction] = {
    var line : Int = 0;
    var program : IntMap[Instruction] = IntMap()
    for (i <- input) {
      i match {
        case in : Instruction => {
          program = program.update(line, in)
          line += 1;
        }
        case ll : LineLabel => {
          ll.ln = line
        }
      }
    }
    program
  }
}