Skip to content

Commit

Permalink
[Fix] Functional calls as dictionary keys
Browse files Browse the repository at this point in the history
  • Loading branch information
melvic-ybanez committed Nov 4, 2023
1 parent ceb5c70 commit ddd55f2
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ like this:
[Success] Ducks should quack!
[Success] Denji should say 'Woof!'
[Success] Class properties should be updated
Ran 168 tests. Successful: 168. Failed: 0.
Ran 169 tests. Successful: 169. Failed: 0.
```

The tests themselves are written in Dry (while the `testDry` command is written in Scala). You can see the directory
Expand Down
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
ThisBuild / version := "0.5.0-SNAPSHOT"
ThisBuild / version := "0.4.1"

ThisBuild / scalaVersion := "2.13.8"

lazy val root = (project in file("."))
.settings(
name := "dry",
assembly / assemblyJarName := "dry-0.5.0-SNAPSHOT.jar",
assembly / assemblyJarName := "dry-0.4.1.jar",
libraryDependencies ++= Seq(
"org.scalactic" %% "scalactic" % "3.2.17",
"org.scalatest" %% "scalatest" % "3.2.17" % "test",
Expand Down
5 changes: 2 additions & 3 deletions src/main/scala/com/melvic/dry/interpreter/eval/EvalExpr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.melvic.dry.Token.TokenType
import com.melvic.dry.ast.Expr
import com.melvic.dry.ast.Expr.{List => _, _}
import com.melvic.dry.aux.implicits.ListOps
import com.melvic.dry.interpreter.Interpret
import com.melvic.dry.interpreter.{Env, Interpret}
import com.melvic.dry.interpreter.Value.{Bool, Num, Str, None => VNone}
import com.melvic.dry.interpreter.eval.Context.implicits._
import com.melvic.dry.interpreter.eval.Evaluate.Out
Expand Down Expand Up @@ -260,7 +260,7 @@ private[eval] trait EvalExpr {

private[eval] def index[A](obj: Expr, key: Expr, token: Token)(
ifCanBeIndexed: PartialFunction[(Value, Value), Out]
)(implicit context: Context[A]): Out = {
)(implicit context: Context[A]): Out =
for {
evaluatedObj <- Evaluate.expr(obj)
evaluatedKey <- Evaluate.expr(key)
Expand All @@ -269,7 +269,6 @@ private[eval] trait EvalExpr {
}
result <- ifCanBeIndexed.orElse(orElse)(evaluatedObj, evaluatedKey)
} yield result
}

private def varLookup(name: Token, expr: Expr)(implicit context: Context[Expr]): Out =
locals
Expand Down
28 changes: 14 additions & 14 deletions src/main/scala/com/melvic/dry/resolver/Resolve.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ object Resolve {
Resolve.expr(callee) >=> { scopes =>
arguments.foldLeft(scopes.ok)((acc, arg) => acc.flatMap(Resolve.expr(arg)))
}
case lambda: Lambda => enterFunction(Resolve.lambda(_)(lambda))
case Get(obj, _) => Resolve.expr(obj)
case Set(obj, _, value) => Resolve.expr(value) >=> Resolve.expr(obj)
case IndexGet(obj, _, _) => Resolve.expr(obj)
case IndexSet(obj, _, value, _) => Resolve.expr(value) >=> Resolve.expr(obj)
case self: Self => Resolve.self(self)
case list: Expr.List => Resolve.list(list)
case tuple: Tuple => Resolve.tuple(tuple)
case dict: Dictionary => Resolve.dictionary(dict)
case lambda: Lambda => enterFunction(Resolve.lambda(_)(lambda))
case Get(obj, _) => Resolve.expr(obj)
case Set(obj, _, value) => Resolve.expr(value) >=> Resolve.expr(obj)
case IndexGet(obj, key, _) => Resolve.expr(obj) >=> Resolve.expr(key)
case IndexSet(obj, key, value, _) => Resolve.expr(obj) >=> Resolve.expr(key) >=> Resolve.expr(value)
case self: Self => Resolve.self(self)
case list: Expr.List => Resolve.list(list)
case tuple: Tuple => Resolve.tuple(tuple)
case dict: Dictionary => Resolve.dictionary(dict)
}

def variable: Variable => Resolve = { case expr @ Variable(name) =>
Expand Down Expand Up @@ -154,19 +154,19 @@ object Resolve {
}

def list: Expr.List => Resolve = { case Expr.List(elems) =>
sequence(elems)(identity)
sequence(elems)(Resolve.expr)
}

def tuple: Tuple => Resolve = { case Tuple(elems) =>
sequence(elems)(identity)
sequence(elems)(Resolve.expr)
}

def dictionary: Dictionary => Resolve = { case Dictionary(table) =>
sequence(table.toList)(_._2)
sequence(table.toList) { case (key, value) => Resolve.expr(key) >=> Resolve.expr(value) }
}

private def sequence[A](elems: List[A])(f: A => Expr): Resolve = context =>
elems.foldFailFast(context.ok) { case (context, elem) => Resolve.expr(f(elem))(context) }
private def sequence[A](elems: List[A])(f: A => Resolve): Resolve = context =>
elems.foldFailFast(context.ok) { case (context, elem) => f(elem)(context) }

def local(name: Token): Expr => Resolve = { expr => context =>
val maybeFound = context.scopes.zipWithIndex.find { case (scope, _) =>
Expand Down
29 changes: 17 additions & 12 deletions tests/data_structs/test_dictionaries.dry
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,23 @@ class TestDictionary {
}

def test_non_constant_keys() {
let dict = {1 + 1: 1};
assert_equals("Dictionary with a non-constant key", 1, dict[2]);
let dict_arith = {1 + 1: 1};
assert_equals("Dictionary with an arithmetic expression key", 1, dict_arith[2]);

def foo() { return "hello"; }

let dict_call = {foo(): 10};
assert_equals("Dictionary with a function call key", 10, dict_call[foo()]);
}
}

let test_dict = TestDictionary();
test_dict.test_init();
test_dict.test_size();
test_dict.test_get();
test_dict.test_set();
test_dict.test_delete();
test_dict.test_nested();
test_dict.test_type();
test_dict.test_fields_retrieval();
test_dict.test_non_constant_keys();
let dictionaries = TestDictionary();
dictionaries.test_init();
dictionaries.test_size();
dictionaries.test_get();
dictionaries.test_set();
dictionaries.test_delete();
dictionaries.test_nested();
dictionaries.test_type();
dictionaries.test_fields_retrieval();
dictionaries.test_non_constant_keys();
16 changes: 8 additions & 8 deletions tests/data_structs/test_lists.dry
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ class TestList {
}
}

let test_list = TestList();
test_list.test_str();
test_list.test_get();
test_list.test_size();
test_list.test_type();
test_list.test_add_method();
test_list.test_set();
test_list.test_delete();
let lists = TestList();
lists.test_str();
lists.test_get();
lists.test_size();
lists.test_type();
lists.test_add_method();
lists.test_set();
lists.test_delete();

0 comments on commit ddd55f2

Please sign in to comment.