Skip to content

Commit

Permalink
Ready for Release 1.02
Browse files Browse the repository at this point in the history
  • Loading branch information
cpp-tutor committed Jan 6, 2024
1 parent acff31f commit 573e7ce
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 19 deletions.
10 changes: 4 additions & 6 deletions Call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@
#include "Class.hpp"
#include <utility>

Value LoxFunction::call(Interpreter& interpreter, const std::vector<std::shared_ptr<Expr>>& arguments) {
Value LoxFunction::call(const Interpreter& parent, const std::vector<std::shared_ptr<Expr>>& arguments) {
Interpreter interpreter{ parent };
interpreter.environment = Environment::makeShared(closure);
for (size_t i = 0; i != arguments.size(); ++i) {
closure->define(params.at(i), arguments.at(i)->accept(interpreter));
interpreter.environment->define(params.at(i), arguments.at(i)->accept(parent));
}
auto environment = Environment::makeShared(/* interpreter.environment, */ closure);
std::swap(environment, interpreter.environment);
try {
body->accept(interpreter);
std::swap(environment, interpreter.environment);
return isInitializer ? closure->getAt(0, "this") : std::monostate{};
}
catch (const Return& r) {
std::swap(environment, interpreter.environment);
return isInitializer ? closure->getAt(0, "this") : r.get();
}
}
Expand Down
6 changes: 3 additions & 3 deletions Call.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class LoxInstance;

class LoxCallable {
public:
virtual Value call(Interpreter&, const std::vector<std::shared_ptr<Expr>>&) = 0;
virtual Value call(const Interpreter&, const std::vector<std::shared_ptr<Expr>>&) = 0;
virtual const size_t arity() const = 0;
virtual const std::string toString() const = 0;
};
Expand All @@ -31,14 +31,14 @@ class LoxFunction : public LoxCallable {
LoxFunction(const StmtFunction& declaration, std::shared_ptr<Environment> closure, bool isInitializer)
: name{ declaration.getName() }, params{ declaration.getParams() },
body { declaration.getBody() }, closure{ closure }, isInitializer{ isInitializer } {}
virtual Value call(Interpreter&, const std::vector<std::shared_ptr<Expr>>&) override;
virtual Value call(const Interpreter&, const std::vector<std::shared_ptr<Expr>>&) override;
virtual const size_t arity() const override { return params.size(); }
virtual const std::string toString() const override { return "<fn " + name + '>'; }
std::shared_ptr<LoxFunction> bind(std::shared_ptr<LoxInstance>);
};

class Clock : public LoxCallable {
virtual Value call(Interpreter&, const std::vector<std::shared_ptr<Expr>>&) override {
virtual Value call(const Interpreter&, const std::vector<std::shared_ptr<Expr>>&) override {
return static_cast<double>(clock());
}
virtual const size_t arity() const override { return 0; }
Expand Down
4 changes: 2 additions & 2 deletions Class.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class LoxClass : public LoxCallable, public std::enable_shared_from_this<LoxClas
LoxClass(const std::string& name, std::shared_ptr<LoxClass> superclass,
std::unordered_map<std::string,std::shared_ptr<LoxFunction>>&& methods)
: superclass{ superclass }, name{ name }, methods{ methods } {}
virtual Value call(Interpreter&, const std::vector<std::shared_ptr<Expr>>&) override;
virtual Value call(const Interpreter&, const std::vector<std::shared_ptr<Expr>>&) override;
virtual const size_t arity() const override;
virtual const std::string toString() const override { return "<class " + name + '>'; }
Value findMethod(const std::string&) const;
Expand Down Expand Up @@ -68,7 +68,7 @@ inline Value LoxClass::findMethod(const std::string& name) const {
}
}

inline Value LoxClass::call(Interpreter& interpreter, const std::vector<std::shared_ptr<Expr>>& arguments) {
inline Value LoxClass::call(const Interpreter& interpreter, const std::vector<std::shared_ptr<Expr>>& arguments) {
auto instance = std::make_shared<LoxInstance>(shared_from_this());
auto initializer = findMethod("init");
if (static_cast<ValueType>(initializer.index()) == ValueType::Callable) {
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ nmake /f Makefile.nmake
The GNU Makefile can be run wih

```
make [flexcpp] [bisoncpp] [all]
make [all] [flexcpp] [bisoncpp] [all]
```

For the `flexcpp` and `bisoncpp` targets to work, the `flexc++` and `bisonc++` programs must be installed (these are Linux only and a port to Windows is unlikely). I recommend building from scratch using the source from GitLab:
Expand Down Expand Up @@ -63,3 +63,5 @@ Don't be fooled by the 1.00+ version number&mdash;this is alpha-development qual
* *2023/08/27*: **1.00** First upload to GitHub with Windows executable. Many bugs, features and defects: please raise issues and submit pull requests.

* *2023/08/30*: **1.01** Second upload to GitHub with Windows executable. Believed to be complete and correct coverage of all of the material in Part II.

* *2024/06/01*: **1.02** Third upload to GitHub with Windows executable. Fix to `LoxFunction::call()` to handle recursive functions correctly.
2 changes: 1 addition & 1 deletion lexer/Loxerbase.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated by Flexc++ V2.14.00 on Wed, 30 Aug 2023 16:28:00 +0100
// Generated by Flexc++ V2.14.00 on Sat, 06 Jan 2024 15:25:29 +0000

#ifndef LoxerBASE_H_INCLUDED
#define LoxerBASE_H_INCLUDED
Expand Down
2 changes: 1 addition & 1 deletion lexer/loxer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated by Flexc++ V2.14.00 on Wed, 30 Aug 2023 16:28:00 +0100
// Generated by Flexc++ V2.14.00 on Sat, 06 Jan 2024 15:25:29 +0000

#include <iostream>
#include <fstream>
Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <functional>
#include <fstream>

#define VER_STR "1.01 (2023-Aug-30)"
#define VER_STR "1.02 (2024-Jan-06)"

#ifdef _MSC_VER
#define OPTION_SWITCH '/'
Expand Down
2 changes: 1 addition & 1 deletion parser/Loxgrambase.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated by Bisonc++ V6.05.00 on Wed, 30 Aug 2023 16:28:04 +0100
// Generated by Bisonc++ V6.05.00 on Sat, 06 Jan 2024 15:25:30 +0000

// hdr/includes
#ifndef LoxgramBase_h_included
Expand Down
2 changes: 1 addition & 1 deletion parser/loxgram.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated by Bisonc++ V6.05.00 on Wed, 30 Aug 2023 16:28:04 +0100
// Generated by Bisonc++ V6.05.00 on Sat, 06 Jan 2024 15:25:30 +0000

// base/comment

Expand Down
19 changes: 19 additions & 0 deletions test/10-super2.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class A {
method() {
print "A method";
}
}

class B < A {
method() {
print "B method";
}

test() {
super.method();
}
}

class C < B {}

C().test();
7 changes: 7 additions & 0 deletions test/11-fib2.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fun fib(n) {
if (n < 2) return n;
else return fib(n-1) + fib(n-2);
}
for (var i = 0; i < 20; i = i + 1) {
print fib(i);
}
5 changes: 3 additions & 2 deletions visitor/Interpreter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ class Interpreter : public ExprVisitor, public StmtVisitor {
std::ostream& out;
mutable Value recent;
mutable std::unordered_map<const Expr*,int> locals;
friend Value LoxFunction::call(Interpreter&, const std::vector<std::shared_ptr<Expr>>&);
friend Value LoxFunction::call(const Interpreter&, const std::vector<std::shared_ptr<Expr>>&);
public:
Interpreter(std::shared_ptr<Environment> environment, std::ostream& out = std::cout)
: globals{ environment }, environment{ environment }, out{ out } {}

Interpreter(const Interpreter&) = default;

const Value& lastValue() const { return recent; }

virtual Value operator()(const ExprAssign& e) const override {
Expand Down

0 comments on commit 573e7ce

Please sign in to comment.