Skip to content

Commit

Permalink
Added prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Berezin committed May 2, 2019
1 parent 2477ba4 commit 2fd7388
Show file tree
Hide file tree
Showing 15 changed files with 249 additions and 202 deletions.
12 changes: 12 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
bracketSpacing: true,
insertPragma: false,
parser: 'babel',
printWidth: 100,
proseWrap: 'preserve',
requirePragma: false,
semi: false,
singleQuote: true,
tabWidth: 2,
useTabs: false
}
3 changes: 0 additions & 3 deletions dist/poem.min.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/poem.min.map

This file was deleted.

10 changes: 5 additions & 5 deletions environment.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
const { runtimeError } = require('./errors')

class Environment {
constructor (enclosing) {
constructor(enclosing) {
this.map = new Map()
this.enclosing = enclosing
}

get (varToken) {
get(varToken) {
if (this.map.has(varToken.name.lexeme)) {
return this.map.get(varToken.name.lexeme)
}
if (this.enclosing) return this.enclosing.get(varToken)
throw runtimeError(`Undefined variable "${varToken.name.lexeme}"`, varToken.name)
}

set (token, value) {
set(token, value) {
if (this.map.has(token.lexeme)) {
throw runtimeError(`Duplicate variable declaration "${token.lexeme}"`, token)
// return this.map.set(token.lexeme, value)
}
return this.map.set(token.lexeme, value)
}

assign (token, value) {
assign(token, value) {
if (!this.map.has(token.lexeme)) {
if (this.enclosing) return this.enclosing.assign(token, value)
throw runtimeError(`Undefined variable "${token.lexeme}"`, token)
Expand All @@ -31,4 +31,4 @@ class Environment {
}
}

module.exports = Environment
module.exports = Environment
22 changes: 15 additions & 7 deletions errors.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,45 @@
const tokenizer = require('./tokenizer')
const token = tokenizer.tokenEnum

const nullable = str => str ? str : ''
const nullable = str => (str ? str : '')

class LoxError {
constructor (msg, startCoordinates, endCoordinates) {
constructor(msg, startCoordinates, endCoordinates) {
this.msg = msg
this.startCoordinates = startCoordinates
this.endCoordinates = endCoordinates
}

toString () {
toString() {
return this.msg
}
}

const error = (msg, startCoordinates, endCoordinates) =>
new LoxError(msg, startCoordinates, endCoordinates )
new LoxError(msg, startCoordinates, endCoordinates)

const parseError = (msg, token) => {
if (token.type === token.EOF) {
return new LoxError(msg, token.startCoordinates, token.endCoordinates)
} else {
return new LoxError(`${nullable(token.lexeme && `at "${token.lexeme}": `)}${msg}`, token.startCoordinates, token.endCoordinates)
return new LoxError(
`${nullable(token.lexeme && `at "${token.lexeme}": `)}${msg}`,
token.startCoordinates,
token.endCoordinates
)
}
}

const runtimeError = (msg, token) =>
new LoxError(`${nullable(token.lexeme && `at "${token.lexeme}": `)}${msg}`, token.startCoordinates, token.endCoordinates)
new LoxError(
`${nullable(token.lexeme && `at "${token.lexeme}": `)}${msg}`,
token.startCoordinates,
token.endCoordinates
)

module.exports = {
error,
LoxError,
runtimeError,
parseError
}
}
34 changes: 20 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ const run = (code, environment) => {
return lastStatement
} catch (e) {
if (e instanceof LoxError) {
console.error('Parse Error:', e.toString(), `at ${e.endCoordinates.line}:${e.endCoordinates.col + 1}`)
console.error(
'Parse Error:',
e.toString(),
`at ${e.endCoordinates.line}:${e.endCoordinates.col + 1}`
)

// Pre Error String
const frontIndex = code.lastIndexOf('\n', e.startCoordinates.index)
Expand Down Expand Up @@ -88,25 +92,27 @@ const runFile = filename => {

const optionRegex = /--(\w+)(?:=(.+))?/
const processOptions = args =>
args.map(arg => {
const match = optionRegex.exec(arg)
if (match) {
const [_, option, value] = match
if (!value) {
options[option] = !options[option]
args
.map(arg => {
const match = optionRegex.exec(arg)
if (match) {
const [_, option, value] = match
if (!value) {
options[option] = !options[option]
} else {
options[option] = value
}
} else {
options[option] = value
return arg
}
} else {
return arg
}
}).filter(Boolean)
})
.filter(Boolean)

const main = argv => {
const args = processOptions(argv.slice(2))
if (options.debug) console.log(options)
if (args.length > 1) {
console.error("Usage: jlox [script]")
console.error('Usage: jlox [script]')
return 64
} else if (args.length === 1) {
runFile(args[0])
Expand All @@ -115,4 +121,4 @@ const main = argv => {
}
}

return main(process.argv)
return main(process.argv)
51 changes: 34 additions & 17 deletions interpreter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
const { runtimeError } = require('./errors')
const { Binary, Unary, Literal, Var, Grouping, Block, ExpressionStatement, VarStatement, PrintStatement, Assignment, Condition } = 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 @@ -15,39 +27,44 @@ const checkNumber = (token, ...operands) => {
}

class Interpreter {
constructor (environment) {
constructor(environment) {
this.environment = environment || new Environment()
}

interpret (expr) {
interpret(expr) {
return this.evaluate(expr)
}

evaluate (expr) {
evaluate(expr) {
if (expr instanceof Block) return this.visitBlock(expr)
else if (expr instanceof Assignment) return this.visitAssignment(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
// Doesn't need it's own, it can just evaluate like grouping
else if (expr instanceof ExpressionStatement) return this.visitGrouping(expr)
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)
else if (expr instanceof Unary) return this.visitUnary(expr)
else if (expr instanceof Binary) return this.visitBinary(expr)
}

visitLiteral (expr) { return expr.value }
visitGrouping (expr) { return this.evaluate(expr.expression) }
visitPrintStatement (expr) {
visitLiteral(expr) {
return expr.value
}
visitGrouping(expr) {
return this.evaluate(expr.expression)
}
visitPrintStatement(expr) {
const val = this.evaluate(expr.expression)
console.log(!val ? 'nil' : val.toString())
return val
}
visitVar (variable) {
visitVar(variable) {
return this.environment.get(variable)
}
visitVarStatement (variable) {
visitVarStatement(variable) {
let value = null
if (variable.initializer !== null) {
value = this.evaluate(variable.initializer)
Expand All @@ -56,12 +73,12 @@ class Interpreter {
return null
}

visitBlock (expr) {
visitBlock(expr) {
this.interpretBlock(expr.statements, new Environment(this.environment))
return null
}

visitCondition (expr) {
visitCondition(expr) {
if (isTruthy(this.evaluate(expr.condition))) {
this.evaluate(expr.thenBranch)
} else if (expr.elseBranch) {
Expand All @@ -70,7 +87,7 @@ class Interpreter {
return null
}

interpretBlock (statements, env) {
interpretBlock(statements, env) {
const prevEnvironment = this.environment
try {
this.environment = env
Expand All @@ -84,13 +101,13 @@ class Interpreter {
}
}

visitAssignment (expr) {
visitAssignment(expr) {
const value = this.evaluate(expr.value)
this.environment.assign(expr.name, value)
return value
}

visitUnary (expr) {
visitUnary(expr) {
const right = this.evaluate(expr.right)
switch (expr.operator.type) {
case token.MINUS:
Expand All @@ -101,7 +118,7 @@ class Interpreter {
}
}

visitBinary (expr) {
visitBinary(expr) {
const left = this.evaluate(expr.left)
const right = this.evaluate(expr.right)
switch (expr.operator.type) {
Expand Down Expand Up @@ -139,4 +156,4 @@ class Interpreter {
}
}

module.exports = Interpreter
module.exports = Interpreter
Loading

0 comments on commit 2fd7388

Please sign in to comment.