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

Extended "When" Expressions usage. #191

Open
AdamSpeight2008 opened this issue Oct 21, 2017 · 0 comments
Open

Extended "When" Expressions usage. #191

AdamSpeight2008 opened this issue Oct 21, 2017 · 0 comments

Comments

@AdamSpeight2008
Copy link
Contributor

AdamSpeight2008 commented Oct 21, 2017

When expr<Bool> has pre-existing use case.

  • Filtered Exceptions

I think a lot of these issues could be solved if make When a binary operator, especially in combination with Select Case, even more so with When guard clauses.
Grammar(Roughly)

WhenExpr ::= expr "When" expr WhenElse? 
WhenElse ::= "Else" expr
Operator [When]( L As DeclarationExpression , R As Expression(Of Boolean) ) As Expression
Operator [When]( L As Expression , R As Expression(Of Boolean) ) As Expression

Note: Out in these examples is defined as having the following meaning

If the referred to variable is already declared and in scope use that instance, provided it is a compatible type else it is an error. If referred to variable isn't declared or in scope, then declare and instance of the variable with the same type as the parameter is referencing.


Initialisation / Declaration

Dim x When Integer.TryParse( Text, Out x )
Dim x When Integer.TryParse( Text, Out x ) Else GoTo error_state
Dim x When Integer.TryParse( Text, Out x ) Else Throw UnspeakableException()

I'm not too sure about that one, the If form seems a better fit


While Loops

While  ( Dim bytesRead = stream.Read(buffer, 0, buffer.Length) When (bytesRead > 0 ) ) 
' Use the data you've read 
End While

That form I kinda like


Select Case

Function ParseTypeCharacter( here As Integer ) As SyntaxNode
  Select Case ch When TryGet(here, Out ch)
    Case "U"c, "u"c
      Select Case nc When TryGet(here+1, Out nc)
        Case "I"c, "i"c : Make_Unsigned_Integer(here) 
        Case "L"c, "l"c : Make_Unsigned_Long(here)
        Case "S"c, "s"c : Make_Unsigned_Short(here)
        Case Else       : Unexpected_Value(nc)
      Else Select
        Return Unexpected_EOS
      End Select
    Case "I"c, "i"c     : Make_Signed_Integer(here)
    Case "L"c, "l"c     : Make_Signed_Long(here)
    Case "S"c, "s"c     : Make_Signed_Short(here)
    Case Else           : Unexpected_Value(ch)
  Else Select
    Return Unexpected_EOS
  End Select
End Function

or

Function ParseTypeCharacter( here As Integer ) As SyntaxNode
  Select Case ch When TryGet(here, Out ch) Else Unexpected_EOS
    Case "U"c, "u"c
      Select Case nc When TryGet(here+1, Out nc) Else Unexpected_EOS
        Case "I"c, "i"c : Make_Unsigned_Integer(here) 
        Case "L"c, "l"c : Make_Unsigned_Long(here)
        Case "S"c, "s"c : Make_Unsigned_Short(here)
        Case Else       : Unexpected_Value(nc)
      End Select
    Case "I"c, "i"c     : Make_Signed_Integer(here)
    Case "L"c, "l"c     : Make_Signed_Long(here)
    Case "S"c, "s"c     : Make_Signed_Short(here)
    Case Else           : Unexpected_Value(ch)
  End Select
End Function

This form I like a lot


Grammar(Roughly)

When_Expr         ::= "When" expr
SelectCase_Kind   ::= "Case" | "Type"
SelectCase_Header ::= "Select" SelectCase_Kind? expr When_Expr?
SelectCase_Else   ::= "Else" SelectCase_Header
SelectCase_Footer ::= "End" "Select"
SlectCase_Block   ::= SelectCase_Header SelectCase_Clause* SelectCase_Else* SelectCase_Footer

SelectCase_Clause ::= ( Case_Clause | Case_Clause_Else ) When_Expr?
Clause_Else       ::= "Case" "Else"
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

No branches or pull requests

1 participant