Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement pattern matching #21

Open
5 tasks
daniel-p-gonzalez opened this issue Apr 29, 2017 · 0 comments
Open
5 tasks

Implement pattern matching #21

daniel-p-gonzalez opened this issue Apr 29, 2017 · 0 comments

Comments

@daniel-p-gonzalez
Copy link
Owner

daniel-p-gonzalez commented Apr 29, 2017

Implement Erlang/Prolog/Elixir style pattern matching on complex types, e.g.

x = [1,2,3]
[a,b,c] = x
print(a) # 1
print(c) # 3

obj = {'name': 'Emmett', 'age': 1}
{'age': his_age} = obj
print(his_age)

Tasks:

  • Implement pattern match assignment ast in parser
    • arrays
    • dicts
  • Use unification in type inference pass to bind targets and types
  • Unfold multiple assignments in code gen pass

TODO when structs and anonymous structs / tuples are implemented:

x = ("the", 1720512, true)
(token, count, is_stopword) = x
print(token)

# or using struct
struct MyType(token, count, is_stopword)
x = MyType("the", 1720512, true)
(token, count, is_stopword) = x
print(token)

TODO when exceptions are implemented:

x = [1,2,3]
[a, 2, c] = x # a=1, c=3
[a, 5, c] = x # raises pattern match exception

Keep in mind future usage in match construct:

x = ("the", 1720512, true)
match x:
  case (token, count, true):
      print("Matched stopword: " + token)
  case (token, count, false):
      print("Matched non-stopword: " + token)
end

# and possible an expression flavor:
x = ("the", 1720512, true)
message = match x:
  case (token, count, true):
      "Matched stopword: " + token
  case (token, count, false):
      "Matched non-stopword: " + token
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant