Skip to content

Commit

Permalink
First draft of "classes".
Browse files Browse the repository at this point in the history
  • Loading branch information
munificent committed Oct 2, 2017
1 parent 370320a commit fba138b
Show file tree
Hide file tree
Showing 20 changed files with 2,557 additions and 323 deletions.
1,199 changes: 983 additions & 216 deletions book/classes.md

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion c/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ static void runtimeError(const char* format, ...) {
//> Closures not-yet
ObjFunction* function = frame->closure->function;
//< Closures not-yet
size_t instruction = frame->ip - function->chunk.code;
// -1 because the IP is sitting on the next instruction to be executed.
size_t instruction = frame->ip - function->chunk.code - 1;
fprintf(stderr, "[line %d] in ", function->chunk.lines[instruction]);
if (function->name == NULL) {
fprintf(stderr, "script\n");
Expand Down
10 changes: 5 additions & 5 deletions java/com/craftinginterpreters/lox/Interpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -387,15 +387,15 @@ public Object visitLogicalExpr(Expr.Logical expr) {
//> Classes interpreter-visit-set
@Override
public Object visitSetExpr(Expr.Set expr) {
Object value = evaluate(expr.value);
Object object = evaluate(expr.object);

if (object instanceof LoxInstance) {
((LoxInstance)object).set(expr.name, value);
return value;
if (!(object instanceof LoxInstance)) {
throw new RuntimeError(expr.name, "Only instances have fields.");
}

throw new RuntimeError(expr.name, "Only instances have fields.");
Object value = evaluate(expr.value);
((LoxInstance)object).set(expr.name, value);
return value;
}
//< Classes interpreter-visit-set
//> Inheritance not-yet
Expand Down
11 changes: 6 additions & 5 deletions java/com/craftinginterpreters/lox/LoxFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ class LoxFunction implements LoxCallable {
//< closure-constructor
this.declaration = declaration;
}
//> Classes bind-self
LoxFunction bind(LoxInstance self) {
//> Classes bind-instance
LoxFunction bind(LoxInstance instance) {
Environment environment = new Environment(closure);
environment.define("this", self);
/* Classes bind-self < Classes lox-function-bind-with-initializer
environment.define("this", instance);
/* Classes bind-instance < Classes lox-function-bind-with-initializer
return new LoxFunction(declaration, environment);
*/
//> lox-function-bind-with-initializer
return new LoxFunction(declaration, environment, isInitializer);
//< lox-function-bind-with-initializer
}
//< Classes bind-self
//< Classes bind-instance
//> function-to-string
@Override
public String toString() {
Expand Down Expand Up @@ -75,6 +75,7 @@ public Object call(Interpreter interpreter, List<Object> arguments) {
}
//< catch-return
//> Classes return-this

if (isInitializer) return closure.getAt(0, "this");
//< Classes return-this
return null;
Expand Down
2 changes: 1 addition & 1 deletion java/com/craftinginterpreters/lox/LoxInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Object get(Token name) {
if (method != null) return method;

//< lox-instance-get-method
throw new RuntimeError(name,
throw new RuntimeError(name, // [hidden]
"Undefined property '" + name.lexeme + "'.");
}
//< lox-instance-get-property
Expand Down
2 changes: 1 addition & 1 deletion java/com/craftinginterpreters/lox/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ private Stmt classDeclaration() {
}
//< Inheritance not-yet

List<Stmt.Function> methods = new ArrayList<>();
consume(LEFT_BRACE, "Expect '{' before class body.");

List<Stmt.Function> methods = new ArrayList<>();
while (!check(RIGHT_BRACE) && !isAtEnd()) {
methods.add(function("method"));
}
Expand Down
15 changes: 8 additions & 7 deletions java/com/craftinginterpreters/lox/Resolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,26 @@ public Void visitClassStmt(Stmt.Class stmt) {
//< Inheritance not-yet
//> resolve-methods

for (Stmt.Function method : stmt.methods) {
//> resolver-begin-this-scope
beginScope();
scopes.peek().put("this", true);
beginScope();
scopes.peek().put("this", true);

//< resolver-begin-this-scope
FunctionType declaration = FunctionType.METHOD;
for (Stmt.Function method : stmt.methods) {
FunctionType declaration = FunctionType.METHOD; // [local]
//> resolver-initializer-type
if (method.name.lexeme.equals("init")) {
declaration = FunctionType.INITIALIZER;
}

//< resolver-initializer-type
resolveFunction(method, declaration);
//> resolver-end-this-scope
endScope();
//< resolver-end-this-scope
}

//> resolver-end-this-scope
endScope();

//< resolver-end-this-scope
//< resolve-methods
//> Inheritance not-yet
if (currentClass == ClassType.SUBCLASS) endScope();
Expand Down
13 changes: 13 additions & 0 deletions note/log.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
2017/10/02 - 897 words, design note for "classes", finish first draft
2017/10/01 - work on challenges for "classes"
2017/09/30 - 179 words, outline design note for "classes"
2017/09/29 - 2155 words across two sessions, first draft "classes"
2017/09/28 - 828 words, first draft "classes"
2017/09/27 - 835 words, first draft "classes"
2017/09/26 - broke the chain, busy in aarhus and forgot :(
made up on 09/29
2017/09/25 - 417 words, first draft "classes"
2017/09/24 - 712 words, first draft "classes"
2017/09/23 - 891 words, first draft "classes"
2017/09/22 - 344 words, first draft "classes"
2017/09/21 - 327 words, first draft "classes"
2017/09/20 - finish rough outline "classes"
2017/09/19 - 773 words outline "classes"
2017/09/18 - 278 words outline "classes"
Expand Down
Loading

0 comments on commit fba138b

Please sign in to comment.