-
Notifications
You must be signed in to change notification settings - Fork 0
/
PLangEvaluator.kt
34 lines (25 loc) · 978 Bytes
/
PLangEvaluator.kt
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
package org.plang
import org.antlr.v4.runtime.CharStreams
import org.antlr.v4.runtime.CommonTokenStream
import org.plang.api.AST
import org.plang.api.LangEvaluator
import java.io.InputStream
class PLangEvaluator : LangEvaluator {
private fun createAST(inputStream: InputStream): List<AST> {
val input = CharStreams.fromStream(inputStream)
val lexer = org.plang.PLangLexer(input)
val tokens = CommonTokenStream(lexer)
val parser = org.plang.PLangParser(tokens)
val visitor = PLangASTVisitor()
return visitor.visit(parser.program())
}
private fun evaluateAST(ast: List<AST>, ctx: PLangContext) =
ast.map { it.evaluate(ctx) }
override fun evaluate(input: String, ctx: PLangContext) =
evaluate(input.byteInputStream(), ctx)
override fun evaluate(inputStream: InputStream, ctx: PLangContext) =
evaluateAST(
ast = createAST(inputStream),
ctx = ctx
)
}