Repository: fika-lang/fika (elixir)

Issue:
Support pattern matching "record" types
To do this, add "record" type to the "pattern" parser.

Pull request description:
Allow record patterns in case expression
Closes #68 

Patch:
diff --git a/lib/fika/compiler/parser/pattern.ex b/lib/fika/compiler/parser/pattern.ex
index 16c2191..b3096bd 100644
--- a/lib/fika/compiler/parser/pattern.ex
+++ b/lib/fika/compiler/parser/pattern.ex
@@ -40,13 +40,37 @@ defmodule Fika.Compiler.Parser.Pattern do
     |> ignore(string("}"))
     |> Helper.to_ast(:tuple)
 
+  key_value =
+    allow_space
+    |> concat(identifier)
+    |> concat(allow_space)
+    |> ignore(string(":"))
+    |> concat(allow_space)
+    |> parsec(:pattern)
+    |> Helper.to_ast(:key_value)
+
+  record =
+    wrap(optional(string("Foo")))
+    |> ignore(string("{"))
+    |> concat(key_value)
+    |> repeat(
+      allow_space
+      |> ignore(string(","))
+      |> concat(allow_space)
+      |> concat(key_value)
+    )
+    |> optional(ignore(string(",")))
+    |> ignore(string("}"))
+    |> Helper.to_ast(:record)
+
   pattern =
     choice([
       atom_literal,
       string_literal,
       integer_literal,
       identifier,
-      tuple
+      tuple,
+      record
     ])
 
   defcombinator :pattern, pattern


Test patch:
diff --git a/test/fika/compiler/parser/pattern_test.exs b/test/fika/compiler/parser/pattern_test.exs
index 1ac00c0..b92f36e 100644
--- a/test/fika/compiler/parser/pattern_test.exs
+++ b/test/fika/compiler/parser/pattern_test.exs
@@ -36,4 +36,12 @@ defmodule Fika.Compiler.Parser.PatternTest do
              ]
            } == TestParser.pattern!(str)
   end
+
+  test "record pattern" do
+    str = "{foo: \"bar\"}"
+
+    assert {:record, {1, 0, 12}, nil,
+            [{{:identifier, {1, 0, 4}, :foo}, {:string, {1, 0, 11}, ["bar"]}}]} ==
+             TestParser.pattern!(str)
+  end
 end
diff --git a/test/fika/compiler/type_checker/match_test.exs b/test/fika/compiler/type_checker/match_test.exs
index 475f071..b53bb69 100644
--- a/test/fika/compiler/type_checker/match_test.exs
+++ b/test/fika/compiler/type_checker/match_test.exs
@@ -194,11 +194,6 @@ defmodule Fika.Compiler.TypeChecker.Types.MatchTest do
   test "match_case for record with a union inside" do
     str = "{name: String, id: Int, x: :a | :b | :c}"
     {:type, _, rhs} = TestParser.type_str!(str)
-    # env = %{scope: %{}}
-    # pattern = TestParser.expression!("{name: x}")
-    # assert {:ok, env, unmatched} = Match.match_case(env, pattern, rhs)
-    # assert unmatched == []
-    # assert env.scope[:x] == :String
 
     pattern = TestParser.expression!("{x: :a}")
     assert {:ok, env, unmatched} = Match.match_case(%{}, pattern, rhs)