Skip to content

Commit

Permalink
Added if statements
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Berezin committed May 2, 2019
1 parent 560a9e8 commit 2477ba4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
17 changes: 14 additions & 3 deletions interpreter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { runtimeError } = require('./errors')
const { Binary, Unary, Literal, Var, Grouping, Block, ExpressionStatement, VarStatement, PrintStatement, Assignment } = require('./types')
const { Binary, Unary, Literal, Var, Grouping, Block, ExpressionStatement, VarStatement, PrintStatement, Assignment, Condition } = require('./types')
const Environment = require('./environment')
const tokenizer = require('./tokenizer')
const token = tokenizer.tokenEnum
Expand All @@ -26,8 +26,10 @@ class Interpreter {
evaluate (expr) {
if (expr instanceof Block) return this.visitBlock(expr)
else if (expr instanceof Assignment) return this.visitAssignment(expr)
else if (expr instanceof PrintStatement) return this.visitPrintStatement(expr)
else if (expr instanceof Condition) return this.visitCondition(expr)
else if (expr instanceof VarStatement) return this.visitVarStatement(expr)
else if (expr instanceof PrintStatement) return this.visitPrintStatement(expr)
else if (expr instanceof ExpressionStatement) return this.visitGrouping(expr) // Doesn't need it's own, it can just evaluate like grouping
else if (expr instanceof Grouping) return this.visitGrouping(expr)
else if (expr instanceof Var) return this.visitVar(expr)
else if (expr instanceof Literal) return this.visitLiteral(expr)
Expand All @@ -39,7 +41,7 @@ class Interpreter {
visitGrouping (expr) { return this.evaluate(expr.expression) }
visitPrintStatement (expr) {
const val = this.evaluate(expr.expression)
console.log(val.toString())
console.log(!val ? 'nil' : val.toString())
return val
}
visitVar (variable) {
Expand All @@ -59,6 +61,15 @@ class Interpreter {
return null
}

visitCondition (expr) {
if (isTruthy(this.evaluate(expr.condition))) {
this.evaluate(expr.thenBranch)
} else if (expr.elseBranch) {
this.evaluate(expr.elseBranch)
}
return null
}

interpretBlock (statements, env) {
const prevEnvironment = this.environment
try {
Expand Down
14 changes: 13 additions & 1 deletion parser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const tokenizer = require('./tokenizer')
const { Binary, Unary, Var, Literal, Grouping, PrintStatement, ExpressionStatement, VarStatement, Assignment, Block } = require('./types')
const { Binary, Unary, Var, Literal, Grouping, PrintStatement, ExpressionStatement, VarStatement, Assignment, Block, Condition } = require('./types')
const { parseError: ParseError } = require('./errors')
const token = tokenizer.tokenEnum

Expand Down Expand Up @@ -55,12 +55,24 @@ class Parser {
}

statement () {
if (this.match(token.IF)) return this.ifStatement()
if (this.match(token.PRINT)) return this.printStatement()
if (this.match(token.LEFT_BRACE)) return new Block(this.block())

return this.expressionStatement()
}

ifStatement () {
this.consume(token.LEFT_PAREN, 'Expected "(" after "if"')
const cond = this.expression()
this.consume(token.RIGHT_PAREN, 'Expected ")" after expression')
const ifBranch = this.statement()
let elseBranch = null
if (this.match(token.ELSE)) elseBranch = this.statement()

return new Condition(cond, ifBranch, elseBranch)
}


block () {
let statements = []
Expand Down
9 changes: 9 additions & 0 deletions types.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ class Block {
}
}

class Condition {
constructor (condition, thenBranch, elseBranch) {
this.condition = condition
this.thenBranch = thenBranch
this.elseBranch = elseBranch
}
}


module.exports = {
Var,
Expand All @@ -62,6 +70,7 @@ module.exports = {
Block,
Literal,
Grouping,
Condition,
ExpressionStatement,
PrintStatement,
VarStatement,
Expand Down

0 comments on commit 2477ba4

Please sign in to comment.