Skip to content

Commit

Permalink
feat: parse continue and break
Browse files Browse the repository at this point in the history
  • Loading branch information
silverhairs committed Aug 13, 2023
1 parent 7ee1375 commit b273283
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
13 changes: 7 additions & 6 deletions glox/ast/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type StmtVisitor interface {
VisitBlockStmt(*BlockStmt) any
VisitIfStmt(*IfStmt) any
VisitWhile(*WhileStmt) any
VisitBreak(*Break) any
VisitBranch(*BranchStmt) any
}

type Statement interface {
Expand Down Expand Up @@ -94,14 +94,15 @@ func (stmt *WhileStmt) Accept(v StmtVisitor) any {
return v.VisitWhile(stmt)
}

type Break struct {
// BranchStmt statement represent `break` or `continue`
type BranchStmt struct {
Token token.Token
}

func NewBreakStmt(tok token.Token) *Break {
return &Break{Token: tok}
func NewBranch(tok token.Token) *BranchStmt {
return &BranchStmt{Token: tok}
}

func (b *Break) Accept(v StmtVisitor) any {
return v.VisitBreak(b)
func (b *BranchStmt) Accept(v StmtVisitor) any {
return v.VisitBranch(b)
}
46 changes: 25 additions & 21 deletions glox/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ const (
FUNCTION = "FUNCTION"
FOR = "FOR"
OR = "OR"
BREAK = "BREAK"
CONTINUE = "CONTINUE"
BREAK = "BREAK"
CONTINUE = "CONTINUE"
NIL = "NIL"
PRINT = "PRINT"
RETURN = "RETURN"
Expand All @@ -67,25 +67,25 @@ type Token struct {
}

var keywords = map[string]TokenType{
"and": AND,
"class": CLASS,
"else": ELSE,
"false": FALSE,
"true": TRUE,
"for": FOR,
"fn": FUNCTION,
"if": IF,
"nil": NIL,
"or": OR,
"print": PRINT,
"return": RETURN,
"super": SUPER,
"this": THIS,
"let": LET,
"while": WHILE,
"var": LET,
"break": BREAK,
"continue": CONTINUE,
"and": AND,
"class": CLASS,
"else": ELSE,
"false": FALSE,
"true": TRUE,
"for": FOR,
"fn": FUNCTION,
"if": IF,
"nil": NIL,
"or": OR,
"print": PRINT,
"return": RETURN,
"super": SUPER,
"this": THIS,
"let": LET,
"while": WHILE,
"var": LET,
"break": BREAK,
"continue": CONTINUE,
}

func LookupIdentifier(keyword string) TokenType {
Expand All @@ -96,3 +96,7 @@ func LookupIdentifier(keyword string) TokenType {

return IDENTIFIER
}

func IsLoopController(tok TokenType) bool {
return tok == CONTINUE || tok == BREAK
}

0 comments on commit b273283

Please sign in to comment.