Skip to content

Commit

Permalink
"vox" -> "lox" in Java stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
munificent committed Aug 25, 2016
1 parent 164d110 commit 950d17b
Show file tree
Hide file tree
Showing 24 changed files with 318 additions and 318 deletions.
36 changes: 18 additions & 18 deletions Makefile
@@ -1,6 +1,6 @@
BUILD_DIR := build

default: cvox jvox
default: cvox jlox

debug:
@ $(MAKE) -f util/c.make NAME=cvoxd MODE=debug SOURCE_DIR=c
Expand All @@ -15,7 +15,7 @@ test: test_java test_c
test_c: debug
@ python util/test.py c

test_java: jvox
test_java: jlox
@ python util/test.py java

# Remove all build outputs and intermediate files.
Expand All @@ -32,60 +32,60 @@ cvox:
generate_ast:
@ $(MAKE) -f util/java.make DIR=java PACKAGE=tool
@ java -cp build/java com.craftinginterpreters.tool.GenerateAst \
java/com/craftinginterpreters/vox
java/com/craftinginterpreters/lox

# Compile the Java interpreter .java files to .class files.
jvox: generate_ast
@ $(MAKE) -f util/java.make DIR=java PACKAGE=vox
jlox: generate_ast
@ $(MAKE) -f util/java.make DIR=java PACKAGE=lox

run_generate_ast = @ java -cp build/gen/$(1) \
com.craftinginterpreters.tool.GenerateAst \
gen/$(1)/com/craftinginterpreters/vox
gen/$(1)/com/craftinginterpreters/lox

chapters:
@ python util/split_chapters.py

@ $(MAKE) -f util/java.make DIR=gen/chap03_pancake PACKAGE=pancake

@ $(MAKE) -f util/java.make DIR=gen/chap04_framework PACKAGE=vox
@ $(MAKE) -f util/java.make DIR=gen/chap04_framework PACKAGE=lox

@ $(MAKE) -f util/java.make DIR=gen/chap05_scanning PACKAGE=vox
@ $(MAKE) -f util/java.make DIR=gen/chap05_scanning PACKAGE=lox

@ $(MAKE) -f util/java.make DIR=gen/chap06_representing PACKAGE=tool
$(call run_generate_ast,chap06_representing)
@ $(MAKE) -f util/java.make DIR=gen/chap06_representing PACKAGE=vox
@ $(MAKE) -f util/java.make DIR=gen/chap06_representing PACKAGE=lox

@ $(MAKE) -f util/java.make DIR=gen/chap07_parsing PACKAGE=tool
$(call run_generate_ast,chap07_parsing)
@ $(MAKE) -f util/java.make DIR=gen/chap07_parsing PACKAGE=vox
@ $(MAKE) -f util/java.make DIR=gen/chap07_parsing PACKAGE=lox

@ $(MAKE) -f util/java.make DIR=gen/chap08_evaluating PACKAGE=tool
$(call run_generate_ast,chap08_evaluating)
@ $(MAKE) -f util/java.make DIR=gen/chap08_evaluating PACKAGE=vox
@ $(MAKE) -f util/java.make DIR=gen/chap08_evaluating PACKAGE=lox

@ $(MAKE) -f util/java.make DIR=gen/chap09_statements PACKAGE=tool
$(call run_generate_ast,chap09_statements)
@ $(MAKE) -f util/java.make DIR=gen/chap09_statements PACKAGE=vox
@ $(MAKE) -f util/java.make DIR=gen/chap09_statements PACKAGE=lox

@ $(MAKE) -f util/java.make DIR=gen/chap10_control PACKAGE=tool
$(call run_generate_ast,chap10_control)
@ $(MAKE) -f util/java.make DIR=gen/chap10_control PACKAGE=vox
@ $(MAKE) -f util/java.make DIR=gen/chap10_control PACKAGE=lox

@ $(MAKE) -f util/java.make DIR=gen/chap11_functions PACKAGE=tool
$(call run_generate_ast,chap11_functions)
@ $(MAKE) -f util/java.make DIR=gen/chap11_functions PACKAGE=vox
@ $(MAKE) -f util/java.make DIR=gen/chap11_functions PACKAGE=lox

@ $(MAKE) -f util/java.make DIR=gen/chap12_resolving PACKAGE=tool
$(call run_generate_ast,chap12_resolving)
@ $(MAKE) -f util/java.make DIR=gen/chap12_resolving PACKAGE=vox
@ $(MAKE) -f util/java.make DIR=gen/chap12_resolving PACKAGE=lox

@ $(MAKE) -f util/java.make DIR=gen/chap13_classes PACKAGE=tool
$(call run_generate_ast,chap13_classes)
@ $(MAKE) -f util/java.make DIR=gen/chap13_classes PACKAGE=vox
@ $(MAKE) -f util/java.make DIR=gen/chap13_classes PACKAGE=lox

@ $(MAKE) -f util/java.make DIR=gen/chap14_inheritance PACKAGE=tool
$(call run_generate_ast,chap14_inheritance)
@ $(MAKE) -f util/java.make DIR=gen/chap14_inheritance PACKAGE=vox
@ $(MAKE) -f util/java.make DIR=gen/chap14_inheritance PACKAGE=lox

# TODO: Unify with make chapters, and make more demand-driven.
c_chapters:
Expand Down Expand Up @@ -144,4 +144,4 @@ diffs:
@ -diff --new-file gen/chap31_methods/ gen/chap32_inheritance/ > build/diffs/chap32_inheritance.diff
@ -diff --new-file gen/chap32_inheritance/ gen/chap33_native/ > build/diffs/chap33_native.diff

.PHONY: clean cvox debug default diffs jvox test test_c test_java watch
.PHONY: clean cvox debug default diffs jlox test test_c test_java watch
@@ -1,5 +1,5 @@
//>= Representing Code
package com.craftinginterpreters.vox;
package com.craftinginterpreters.lox;

import java.util.Arrays;

Expand Down
@@ -1,5 +1,5 @@
//>= Functions
package com.craftinginterpreters.vox;
package com.craftinginterpreters.lox;

import java.util.List;

Expand Down
@@ -1,5 +1,5 @@
//>= Statements and State
package com.craftinginterpreters.vox;
package com.craftinginterpreters.lox;

import java.util.HashMap;
import java.util.Map;
Expand Down
@@ -1,5 +1,5 @@
//>= Framework
package com.craftinginterpreters.vox;
package com.craftinginterpreters.lox;

class ErrorReporter {
boolean hadError = false;
Expand Down
@@ -1,4 +1,4 @@
package com.craftinginterpreters.vox;
package com.craftinginterpreters.lox;

import java.util.List;

Expand Down
@@ -1,5 +1,5 @@
//>= Evaluating Expressions
package com.craftinginterpreters.vox;
package com.craftinginterpreters.lox;

//>= Functions
import java.util.ArrayList;
Expand Down Expand Up @@ -103,12 +103,12 @@ public Void visitBlockStmt(Stmt.Block stmt) {
public Void visitClassStmt(Stmt.Class stmt) {
environment.declare(stmt.name);

Map<String, VoxFunction> methods = new HashMap<>();
Map<String, LoxFunction> methods = new HashMap<>();
//>= Inheritance
Object superclass = null;
if (stmt.superclass != null) {
superclass = evaluate(stmt.superclass);
if (!(superclass instanceof VoxClass)) {
if (!(superclass instanceof LoxClass)) {
throw new RuntimeError(stmt.name,
"Superclass must be a class.");
}
Expand All @@ -119,17 +119,17 @@ public Void visitClassStmt(Stmt.Class stmt) {

//>= Classes
for (Stmt.Function method : stmt.methods) {
VoxFunction function = new VoxFunction(method, environment,
LoxFunction function = new LoxFunction(method, environment,
method.name.text.equals("init"));
methods.put(method.name.text, function);
}

/*== Classes
VoxClass klass = new VoxClass(stmt.name.text, methods);
LoxClass klass = new LoxClass(stmt.name.text, methods);
*/
//>= Inheritance
VoxClass klass = new VoxClass(stmt.name.text,
(VoxClass)superclass, methods);
LoxClass klass = new LoxClass(stmt.name.text,
(LoxClass)superclass, methods);

if (superclass != null) {
environment = environment.enclosing;
Expand All @@ -152,10 +152,10 @@ public Void visitExpressionStmt(Stmt.Expression stmt) {
public Void visitFunctionStmt(Stmt.Function stmt) {
environment.declare(stmt.name);
/*>= Functions <= Resolving and Binding
VoxFunction function = new VoxFunction(stmt, environment);
LoxFunction function = new LoxFunction(stmt, environment);
*/
//>= Classes
VoxFunction function = new VoxFunction(stmt, environment, false);
LoxFunction function = new LoxFunction(stmt, environment, false);
//>= Functions
environment.assign(stmt.name, function);
return null;
Expand Down Expand Up @@ -314,8 +314,8 @@ public Object visitCallExpr(Expr.Call expr) {
@Override
public Object visitGetExpr(Expr.Get expr) {
Object object = evaluate(expr.object);
if (object instanceof VoxInstance) {
return ((VoxInstance) object).getProperty(expr.name);
if (object instanceof LoxInstance) {
return ((LoxInstance) object).getProperty(expr.name);
}

throw new RuntimeError(expr.name,
Expand Down Expand Up @@ -355,8 +355,8 @@ public Object visitSetExpr(Expr.Set expr) {
Object value = evaluate(expr.value);
Object object = evaluate(expr.object);

if (object instanceof VoxInstance) {
((VoxInstance)object).fields.put(expr.name.text, value);
if (object instanceof LoxInstance) {
((LoxInstance)object).fields.put(expr.name.text, value);
return value;
}

Expand All @@ -367,10 +367,10 @@ public Object visitSetExpr(Expr.Set expr) {
@Override
public Object visitSuperExpr(Expr.Super expr) {
int distance = locals.get(expr);
VoxClass superclass = (VoxClass)environment.getAt(distance, "super");
VoxInstance receiver = (VoxInstance)environment.getAt(distance, "this");
LoxClass superclass = (LoxClass)environment.getAt(distance, "super");
LoxInstance receiver = (LoxInstance)environment.getAt(distance, "this");

VoxFunction method = superclass.findMethod(receiver, expr.method.text);
LoxFunction method = superclass.findMethod(receiver, expr.method.text);
if (method == null) {
throw new RuntimeError(expr.method,
"Undefined property '" + expr.method.text + "'.");
Expand Down
@@ -1,5 +1,5 @@
//>= Framework
package com.craftinginterpreters.vox;
package com.craftinginterpreters.lox;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -13,13 +13,13 @@
import java.util.Map;
//>= Framework

public class Vox {
public class Lox {
private final ErrorReporter reporter = new ErrorReporter();
//>= Evaluating Expressions
private final Interpreter interpreter;
//>= Evaluating Expressions

private Vox() {
private Lox() {
interpreter = new Interpreter(reporter);
}
//>= Framework
Expand Down Expand Up @@ -48,14 +48,14 @@ private void repl() throws IOException {
}

public static void main(String[] args) throws IOException {
Vox vox = new Vox();
Lox lox = new Lox();

if (args.length > 1) {
System.out.println("Usage: jvox [script]");
System.out.println("Usage: jlox [script]");
} else if (args.length == 1) {
vox.runFile(args[0]);
lox.runFile(args[0]);
} else {
vox.repl();
lox.repl();
}
}

Expand Down
@@ -1,22 +1,22 @@
//>= Classes
package com.craftinginterpreters.vox;
package com.craftinginterpreters.lox;

import java.util.List;
import java.util.Map;

class VoxClass implements Callable {
class LoxClass implements Callable {
final String name;
//>= Inheritance
final VoxClass superclass;
final LoxClass superclass;
//>= Classes
private final Map<String, VoxFunction> methods;
private final Map<String, LoxFunction> methods;

/*== Classes
VoxClass(String name, Map<String, VoxFunction> methods) {
LoxClass(String name, Map<String, LoxFunction> methods) {
*/
//>= Inheritance
VoxClass(String name, VoxClass superclass,
Map<String, VoxFunction> methods) {
LoxClass(String name, LoxClass superclass,
Map<String, LoxFunction> methods) {
//>= Classes
this.name = name;
//>= Inheritance
Expand All @@ -25,7 +25,7 @@ class VoxClass implements Callable {
this.methods = methods;
}

VoxFunction findMethod(VoxInstance instance, String name) {
LoxFunction findMethod(LoxInstance instance, String name) {
/*== Classes
if (methods.containsKey(name)) {
return methods.get(name).bind(instance);
Expand All @@ -35,7 +35,7 @@ VoxFunction findMethod(VoxInstance instance, String name) {
return null;
*/
//>= Inheritance
VoxClass klass = this;
LoxClass klass = this;
while (klass != null) {
if (klass.methods.containsKey(name)) {
return klass.methods.get(name).bind(instance);
Expand All @@ -56,16 +56,16 @@ public String toString() {

@Override
public int requiredArguments() {
VoxFunction initializer = methods.get("init");
LoxFunction initializer = methods.get("init");
if (initializer == null) return 0;
return initializer.requiredArguments();
}

@Override
public Object call(Interpreter interpreter, List<Object> arguments) {
VoxInstance instance = new VoxInstance(this);
LoxInstance instance = new LoxInstance(this);

VoxFunction initializer = methods.get("init");
LoxFunction initializer = methods.get("init");
if (initializer != null) {
initializer.bind(instance).call(interpreter, arguments);
}
Expand Down
@@ -1,20 +1,20 @@
//>= Functions
package com.craftinginterpreters.vox;
package com.craftinginterpreters.lox;

import java.util.List;

class VoxFunction implements Callable {
class LoxFunction implements Callable {
private final Stmt.Function declaration;
private final Environment closure;
//>= Classes
private final boolean isInitializer;
//>= Functions

/*>= Functions <= Resolving and Binding
VoxFunction(Stmt.Function declaration, Environment closure) {
LoxFunction(Stmt.Function declaration, Environment closure) {
*/
//>= Classes
VoxFunction(Stmt.Function declaration, Environment closure,
LoxFunction(Stmt.Function declaration, Environment closure,
boolean isInitializer) {
//>= Functions
this.declaration = declaration;
Expand All @@ -25,10 +25,10 @@ class VoxFunction implements Callable {
}

//>= Classes
VoxFunction bind(VoxInstance self) {
LoxFunction bind(LoxInstance self) {
Environment environment = closure.enterScope();
environment.define("this", self);
return new VoxFunction(declaration, environment, isInitializer);
return new LoxFunction(declaration, environment, isInitializer);
}

//>= Functions
Expand Down

0 comments on commit 950d17b

Please sign in to comment.