Newer
Older
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package amyc
package interpreter
import utils._
import ast.SymbolicTreeModule._
import ast.Identifier
import analyzer.SymbolTable
// An interpreter for Amy programs, implemented in Scala
object Interpreter extends Pipeline[(Program, SymbolTable), Unit] {
// A class that represents a value computed by interpreting an expression
abstract class Value {
def asInt: Int = this.asInstanceOf[IntValue].i
def asBoolean: Boolean = this.asInstanceOf[BooleanValue].b
def asString: String = this.asInstanceOf[StringValue].s
override def toString: String = this match {
case IntValue(i) => i.toString
case BooleanValue(b) => b.toString
case StringValue(s) => s
case UnitValue => "()"
case CaseClassValue(constructor, args) =>
constructor.name + "(" + args.map(_.toString).mkString(", ") + ")"
}
}
case class IntValue(i: Int) extends Value
case class BooleanValue(b: Boolean) extends Value
case class StringValue(s: String) extends Value
case object UnitValue extends Value
case class CaseClassValue(constructor: Identifier, args: List[Value]) extends Value
def run(ctx: Context)(v: (Program, SymbolTable)): Unit = {
val (program, table) = v
// These built-in functions do not have an Amy implementation in the program,
// instead their implementation is encoded in this map
val builtIns: Map[(String, String), (List[Value]) => Value] = Map(
("Std", "printInt") -> { args => println(args.head.asInt); UnitValue },
("Std", "printString") -> { args => println(args.head.asString); UnitValue },
("Std", "readString") -> { args => StringValue(scala.io.StdIn.readLine()) },
("Std", "readInt") -> { args =>
val input = scala.io.StdIn.readLine()
try {
IntValue(input.toInt)
} catch {
case ne: NumberFormatException =>
ctx.reporter.fatal(s"""Could not parse "$input" to Int""")
}
},
("Std", "intToString") -> { args => StringValue(args.head.asInt.toString) },
("Std", "digitToString") -> { args => StringValue(args.head.asInt.toString) }
)
// Utility functions to interface with the symbol table.
def isConstructor(name: Identifier) = table.getConstructor(name).isDefined
def findFunctionOwner(functionName: Identifier) = table.getFunction(functionName).get.owner.name
def findFunction(owner: String, name: String) = {
program.modules.find(_.name.name == owner).get.defs.collectFirst {
case fd@FunDef(fn, _, _, _) if fn.name == name => fd
}.get
}
// Interprets a function, using evaluations for local variables contained in 'locals'
// TODO: Complete all missing cases. Look at the given ones for guidance.
def interpret(expr: Expr)(implicit locals: Map[Identifier, Value]): Value = {
expr match {
case Variable(name) =>
???
case IntLiteral(i) =>
???
case BooleanLiteral(b) =>
???
case StringLiteral(s) =>
???
case UnitLiteral() =>
???
case Plus(lhs, rhs) =>
IntValue(interpret(lhs).asInt + interpret(rhs).asInt)
case Minus(lhs, rhs) =>
???
case Times(lhs, rhs) =>
???
case Div(lhs, rhs) =>
???
case Mod(lhs, rhs) =>
???
case LessThan(lhs, rhs) =>
???
case LessEquals(lhs, rhs) =>
???
case And(lhs, rhs) =>
???
case Or(lhs, rhs) =>
???
case Equals(lhs, rhs) =>
??? // Hint: Take care to implement Amy equality semantics
case Concat(lhs, rhs) =>
???
case Not(e) =>
???
case Neg(e) =>
???
case Call(qname, args) =>
???
// Hint: Check if it is a call to a constructor first,
// then if it is a built-in function (otherwise it is a normal function).
// Use the helper methods provided above to retrieve information from the symbol table.
// Think how locals should be modified.
case Sequence(e1, e2) =>
???
case Let(df, value, body) =>
???
case Ite(cond, thenn, elze) =>
???
case Match(scrut, cases) =>
???
// Hint: We give you a skeleton to implement pattern matching
// and the main body of the implementation
val evS = interpret(scrut)
// None = pattern does not match
// Returns a list of pairs id -> value,
// where id has been bound to value within the pattern.
// Returns None when the pattern fails to match.
// Note: Only works on well typed patterns (which have been ensured by the type checker).
def matchesPattern(v: Value, pat: Pattern): Option[List[(Identifier, Value)]] = {
((v, pat): @unchecked) match {
case (_, WildcardPattern()) =>
???
case (_, IdPattern(name)) =>
???
case (IntValue(i1), LiteralPattern(IntLiteral(i2))) =>
???
case (BooleanValue(b1), LiteralPattern(BooleanLiteral(b2))) =>
???
case (StringValue(_), LiteralPattern(StringLiteral(_))) =>
???
case (UnitValue, LiteralPattern(UnitLiteral())) =>
???
case (CaseClassValue(con1, realArgs), CaseClassPattern(con2, formalArgs)) =>
???
}
}
// Main "loop" of the implementation: Go through every case,
// check if the pattern matches, and if so return the evaluation of the case expression
cases.to(LazyList).map(matchCase =>
val MatchCase(pat, rhs) = matchCase
(rhs, matchesPattern(evS, pat))
).find(_._2.isDefined) match {
case Some((rhs, Some(moreLocals))) =>
interpret(rhs)(locals ++ moreLocals)
case _ =>
// No case matched
ctx.reporter.fatal(s"Match error: ${evS.toString}@${scrut.position}")
}
case Error(msg) =>
???
}
}
for {
m <- program.modules
e <- m.optExpr
} {
interpret(e)(Map())
}
}
}