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

Add support for instance calls #47

Merged
merged 1 commit into from Jan 9, 2020
Merged

Conversation

BastianBlokland
Copy link
Owner

Add support for providing the first argument for a function on the left hand side ([firstArg].funcName([otherArgs]).

fun double(int i)
  i * 2

print(i = 42; i.double())

A more complicated example:

struct None
union Option{T} = T, None

fun string{T}(Option{T} o)
  o is T v ? v.string() : "none"

fun isDigit(char c)
  c >= '0' && c <= '9'

fun parseInt(char c)  -> Option{int}
  if c.isDigit()  -> int(c) - '0'
  else            -> None()

fun parseInt(string str)
  str.parseIntImpl(0, 0)

fun parseIntImpl(string str, int idx, int ac) -> Option{int}
  if idx >= str.length()           -> idx == 0 ? None() : ac
  if str[idx].parseInt() is int v  -> str.parseIntImpl(idx + 1, ac * 10 + v)
  else                             -> None()

print("1337".parseInt().string()      + '\n') // Output: 1337
print("13a37".parseInt().string()     + '\n') // Output: none
print("001".parseInt().string()       + '\n') // Output: 1
print("21232345".parseInt().string()  + '\n') // Output: 21232345
print("".parseInt().string()          + '\n') // Output: none

@BastianBlokland BastianBlokland self-assigned this Jan 9, 2020
@BastianBlokland BastianBlokland merged commit 27d6b3c into master Jan 9, 2020
@BastianBlokland BastianBlokland deleted the feature/instance-calls branch January 9, 2020 20:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant