Switch cases are parsed now

This commit is contained in:
Sasha Koshka 2022-09-03 15:38:57 -04:00
parent bcd44828dc
commit 7bde082f36
4 changed files with 36 additions and 14 deletions

View File

@ -75,6 +75,14 @@ type Token struct {
value any value any
} }
// NewToken provides a way for a new token to be created by other modules for
// comparison purposes.
func NewToken (kind TokenKind, value any) (token Token) {
token.kind = kind
token.value = value
return
}
// Kind returns the semantic role of the token. // Kind returns the semantic role of the token.
func (token Token) Kind () (kind TokenKind) { func (token Token) Kind () (kind TokenKind) {
return token.kind return token.kind

View File

@ -5,6 +5,7 @@ import "git.tebibyte.media/arf/arf/lexer"
// operatorTokens lists all symbolic tokens that can act as a command to a // operatorTokens lists all symbolic tokens that can act as a command to a
// phrase. // phrase.
var operatorTokens = []lexer.TokenKind { var operatorTokens = []lexer.TokenKind {
lexer.TokenKindColon,
lexer.TokenKindPlus, lexer.TokenKindPlus,
lexer.TokenKindMinus, lexer.TokenKindMinus,
lexer.TokenKindIncrement, lexer.TokenKindIncrement,
@ -213,8 +214,9 @@ func (parser *ParsingOperation) parseBlockLevelPhrase (
err = parser.nextToken() err = parser.nextToken()
if err != nil { return } if err != nil { return }
// if this is a control flow statement, parse // if this is a control flow statement, parse block under it
if phrase.command.kind != ArgumentKindIdentifier { return } if phrase.command.kind == ArgumentKindIdentifier {
// perhaps it is a normal control flow statement?
command := phrase.command.value.(Identifier) command := phrase.command.value.(Identifier)
if len(command.trail) != 1 { return } if len(command.trail) != 1 { return }
@ -226,10 +228,20 @@ func (parser *ParsingOperation) parseBlockLevelPhrase (
} }
} }
if isControlFlow { if !isControlFlow { return }
phrase.block, err = parser.parseBlock(indent + 1)
} else if phrase.command.kind == ArgumentKindOperator {
// perhaps it is a switch case?
command := phrase.command.value.(lexer.TokenKind)
if command != lexer.TokenKindColon { return }
} else {
return
} }
// if it is any of those, parse the block under it
phrase.block, err = parser.parseBlock(indent + 1)
return return
} }

View File

@ -209,6 +209,8 @@ func (argument *Argument) ToString (indent int, breakLine bool) (output string)
case ArgumentKindOperator: case ArgumentKindOperator:
var stringValue string var stringValue string
switch argument.value.(lexer.TokenKind) { switch argument.value.(lexer.TokenKind) {
case lexer.TokenKindColon:
stringValue = ":"
case lexer.TokenKindPlus: case lexer.TokenKindPlus:
stringValue = "+" stringValue = "+"
case lexer.TokenKindMinus: case lexer.TokenKindMinus:

View File

@ -121,7 +121,7 @@ func ro gControlFlow
else else
otherThing otherThing
func hSetPhrase func ro hSetPhrase
--- ---
set x:Int 3 set x:Int 3
# TODO: this should be the "location of" phrase. update other things to # TODO: this should be the "location of" phrase. update other things to