|
| 1 | +package jsonnet |
| 2 | + |
| 3 | +import jsonnet.Parser.expr |
| 4 | + |
| 5 | +sealed trait Expr |
| 6 | + |
| 7 | +object Expr: |
| 8 | + case class Num(value: Int) extends Expr |
| 9 | + case class Str(value: String) extends Expr |
| 10 | + case class Ident(name: String) extends Expr |
| 11 | + case class Plus(left: Expr, right: Expr) extends Expr |
| 12 | + case class Dict(pairs: Map[String, Expr]) extends Expr |
| 13 | + case class Local(name: String, assigned: Expr, body: Expr) extends Expr |
| 14 | + case class Func(argNames: Seq[String], body: Expr) extends Expr |
| 15 | + case class Call(expr: Expr, args: Seq[Expr]) extends Expr |
| 16 | + |
| 17 | + def evaluate(expr: Expr, scope: Map[String, Value]): Value = |
| 18 | + expr match |
| 19 | + case Expr.Ident(name) => scope(name) |
| 20 | + case Expr.Num(i) => Value.Num(i) |
| 21 | + case Expr.Str(s) => Value.Str(s) |
| 22 | + case Expr.Dict(kvs) => Value.Dict(kvs.map { case (k, v) => (k, evaluate(v, scope)) }) |
| 23 | + |
| 24 | + case Expr.Plus(left, right) => |
| 25 | + (evaluate(left, scope), evaluate(right, scope)) match |
| 26 | + case (Value.Num(leftNum), Value.Num(rightNum)) => Value.Num(leftNum + rightNum) |
| 27 | + case (Value.Str(leftStr), Value.Str(rightStr)) => Value.Str(leftStr + rightStr) |
| 28 | + |
| 29 | + case Expr.Local(name, assigned, body) => |
| 30 | + val assignedValue = evaluate(assigned, scope) |
| 31 | + evaluate(body, scope + (name -> assignedValue)) |
| 32 | + |
| 33 | + case Expr.Call(expr, args) => |
| 34 | + val Value.Func(call) = evaluate(expr, scope) |
| 35 | + val evaluatedArgs = args.map(evaluate(_, scope)) |
| 36 | + call(evaluatedArgs) |
| 37 | + |
| 38 | + case Expr.Func(argNames, body) => |
| 39 | + Value.Func(args => evaluate(body, scope ++ argNames.zip(args).toMap)) |
| 40 | + |
| 41 | + // Use this for printing as a compact json string |
| 42 | + private def serialize(v: Value): String = |
| 43 | + v match |
| 44 | + case Value.Num(i) => i.toString |
| 45 | + case Value.Str(s) => s"\"$s\"" |
| 46 | + case Value.Dict(kvs) => kvs.map((k, v) => s"\"$k\": ${serialize(v)}").mkString("{", ", ", "}") |
| 47 | + |
| 48 | + // Use this for pretty printing |
| 49 | + private def serialize2(v: Value): ujson.Value = |
| 50 | + v match |
| 51 | + case Value.Num(i) => ujson.Num(i) |
| 52 | + case Value.Str(s) => ujson.Str(s) |
| 53 | + case Value.Dict(kvs) => ujson.Obj.from(kvs.map { case (k, v) => (k, serialize2(v)) }) |
| 54 | + |
| 55 | + def jsonnet(input: String): String = |
| 56 | + // serialize(evaluate(fastparse.parse(input, expr(_)).get.value, Map.empty)) |
| 57 | + ujson.write( |
| 58 | + serialize2(evaluate(fastparse.parse(input, Parser.expr(_)).get.value, Map.empty)), |
| 59 | + indent = 2 |
| 60 | + ) |
| 61 | + |
| 62 | + def main(args: Array[String]): Unit = { |
| 63 | + Seq( |
| 64 | + evaluate(fastparse.parse("\"hello\"", expr(_)).get.value, Map.empty), |
| 65 | + evaluate(fastparse.parse("""{ "hello": "world", "key": "value" }""", expr(_)).get.value, Map.empty), |
| 66 | + evaluate(fastparse.parse("\"hello\" + \"world\"", expr(_)).get.value, Map.empty), |
| 67 | + |
| 68 | + // Call |
| 69 | + evaluate(fastparse.parse("""local greeting = "hello "; greeting + greeting""", expr(_)).get.value, Map.empty), |
| 70 | + evaluate(fastparse.parse("""local x = "Hello "; local y = "World"; x + y""", expr(_)).get.value, Map.empty), |
| 71 | + // evaluate(fastparse.parse("""local greeting = "Hello"; nope + nope""", expr(_)).get.value, Map.empty), |
| 72 | + |
| 73 | + // Func |
| 74 | + evaluate(fastparse.parse("""local f = function(a) a + "1"; f("123")""", expr(_)).get.value, Map.empty), |
| 75 | + evaluate( |
| 76 | + fastparse |
| 77 | + .parse("""local f = function(a, b) a + " " + b; f("hello", "world")""", expr(_)) |
| 78 | + .get |
| 79 | + .value, |
| 80 | + Map.empty |
| 81 | + ) |
| 82 | + ).foreach(println) |
| 83 | + |
| 84 | + // jsonnet |
| 85 | + println(jsonnet("""|local greeting = "Hello "; |
| 86 | + |local person = function (name) { |
| 87 | + | "name": name, |
| 88 | + | "welcome": greeting + name + "!" |
| 89 | + |}; |
| 90 | + |{ |
| 91 | + | "person1": person("Alice"), |
| 92 | + | "person2": person("Bob"), |
| 93 | + | "person3": person("Charlie") |
| 94 | + |}""".stripMargin)) |
| 95 | + |
| 96 | + println( |
| 97 | + jsonnet( |
| 98 | + """|local bonus = 15000; |
| 99 | + |local person = function (name, baseSalary) { |
| 100 | + | "name": name, |
| 101 | + | "totalSalary": baseSalary + bonus |
| 102 | + |}; |
| 103 | + |{"person1": person("Alice", 10000), "person2": person("Bob", 20000)} |
| 104 | + |""".stripMargin |
| 105 | + ) |
| 106 | + ) |
| 107 | + } |
| 108 | +end Expr |
0 commit comments