Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion Interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ func (i *Interpreter) VisitLogicalExpr(expr ast.Logical) interface{} {
func (i *Interpreter) VisitWhileStmtStmt(stmt ast.WhileStmt) interface{} {
for i.isTruthy(i.evaluate(stmt.Condition)) {
i.execute(stmt.Body)

if state.ContinueException {
state.ContinueException = false
continue
}
if state.AbruptCompletion {
state.AbruptCompletion = false
break
Expand All @@ -281,8 +286,11 @@ func (i *Interpreter) executeBlock(statements []ast.Stmt, environment *environme
i.Environment = environment
for _, stmt := range statements {
i.execute(stmt)
if state.ContinueException {
return // Exit the block immediately for continue, let while loop handle the flag
}
if state.AbruptCompletion {
break
return // Exit the block immediately for break, let while loop handle the flag
}
}
}
Expand All @@ -291,3 +299,8 @@ func (i *Interpreter) VisitBreakStmtStmt(stmt ast.BreakStmt) interface{} {
state.AbruptCompletion = true
return nil
}

func (i *Interpreter) VisitContinueStmtStmt(stmt ast.ContinueStmt) interface{} {
state.ContinueException = true
return nil
}
35 changes: 18 additions & 17 deletions Scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,24 @@ type Scanner struct {
}

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

func (s *Scanner) isAtEnd() bool {
Expand Down
3 changes: 2 additions & 1 deletion Token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const (
VAR
WHILE
BREAK
CONTINUE

// End of file
EOF
Expand All @@ -67,7 +68,7 @@ func (t TokenType) String() string {
"LESS", "LESS_EQUAL",
"IDENTIFIER", "STRING", "NUMBER",
"AND", "CLASS", "ELSE", "FALSE", "FUN", "FOR", "IF", "NIL", "OR",
"PRINT", "RETURN", "SUPER", "THIS", "TRUE", "VAR", "WHILE", "BREAK",
"PRINT", "RETURN", "SUPER", "THIS", "TRUE", "VAR", "WHILE", "BREAK", "CONTINUE",
"EOF",
}[t]
}
Expand Down
8 changes: 8 additions & 0 deletions ast/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type StmtVisitor interface {
VisitVarStmtStmt(stmt VarStmt) interface{}
VisitWhileStmtStmt(stmt WhileStmt) interface{}
VisitBreakStmtStmt(stmt BreakStmt) interface{}
VisitContinueStmtStmt(stmt ContinueStmt) interface{}
}

type Stmt interface {
Expand Down Expand Up @@ -77,3 +78,10 @@ func (n BreakStmt) Accept(visitor StmtVisitor) interface{} {
return visitor.VisitBreakStmtStmt(n)
}

type ContinueStmt struct {
}

func (n ContinueStmt) Accept(visitor StmtVisitor) interface{} {
return visitor.VisitContinueStmtStmt(n)
}

21 changes: 16 additions & 5 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ func (p *Parser) statement() ast.Stmt {
if p.match(token.PRINT) {
return p.printStatement()
}
if p.match(token.CONTINUE) {
return p.continueStatement()
}
if p.match(token.BREAK) {
return p.breakStatement()
}
Expand Down Expand Up @@ -310,9 +313,9 @@ func (p *Parser) forStatement() ast.Stmt {
}
p.consume(token.RIGHT_PAREN, "Expect ')' after for clauses.")

state.CanInsertBreakStatement = true
state.CanInsertBreakOrContinueStatement = true
body := p.statement()
state.CanInsertBreakStatement = false
state.CanInsertBreakOrContinueStatement = false

if increment != nil {
body = ast.BlockStmt{
Expand Down Expand Up @@ -367,18 +370,26 @@ func (p *Parser) whileStatement() ast.Stmt {
condition := p.expression()
p.consume(token.RIGHT_PAREN, "Expect ')' after condition.")

state.CanInsertBreakStatement = true
state.CanInsertBreakOrContinueStatement = true
body := p.statement()
state.CanInsertBreakStatement = false
state.CanInsertBreakOrContinueStatement = false
return ast.WhileStmt{
Condition: condition,
Body: body,
}

}
func (p *Parser) continueStatement() ast.Stmt {
if !state.CanInsertBreakOrContinueStatement {
p.error(p.peek(), "continueStatemen can only exist inside valid iterator")

}
p.consume(token.SEMICOLON, "Expect ';' after value.")
return ast.ContinueStmt{}
}

func (p *Parser) breakStatement() ast.Stmt {
if !state.CanInsertBreakStatement {
if !state.CanInsertBreakOrContinueStatement {
p.error(p.peek(), "breakStatement can only exist inside valid iterator")

}
Expand Down
1 change: 1 addition & 0 deletions printer/generateAst.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,6 @@ func main() {
"VarStmt : token.Token name, Expr initializer",
"WhileStmt: Expr condition, Stmt body",
"BreakStmt: ",
"ContinueStmt: ",
}, []string{"github.com/shubhdevelop/YAPL/Token"})
}
3 changes: 2 additions & 1 deletion state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package state

var HadError bool = false
var HadRuntimeError = false
var CanInsertBreakStatement = false
var CanInsertBreakOrContinueStatement = false
var AbruptCompletion = false
var ContinueException = false
Loading