diff --git a/.DS_Store b/.DS_Store
index 47c1ed6..52afa97 100644
Binary files a/.DS_Store and b/.DS_Store differ
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 84972b2..8e4d57f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -4,13 +4,21 @@ project(cpp_lox LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
-set(CMAKE_AUTOUIC ON)
-set(CMAKE_AUTOMOC ON)
-set(CMAKE_AUTORCC ON)
-
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
-add_executable(cpp_lox
- main.cpp
-)
+set(SOURCE_FILES
+ LoxInstance.cpp LoxInstance.hpp
+ LoxClass.cpp LoxClass.hpp
+ Resolver.cpp Resolver.hpp
+ Token.cpp Token.hpp
+ LoxFunction.cpp LoxFunction.hpp
+ Environment.cpp Environment.hpp
+ Interpreter.cpp Interpreter.hpp
+ lox.cpp lox.hpp
+ Parser.cpp Parser.hpp
+ Scanner.cpp Scanner.hpp
+ Stmt.hpp Expr.hpp
+ main.cpp
+ )
+add_executable(cpp_lox ${SOURCE_FILES})
diff --git a/CMakeLists.txt.user b/CMakeLists.txt.user
deleted file mode 100644
index 7d054e4..0000000
--- a/CMakeLists.txt.user
+++ /dev/null
@@ -1,356 +0,0 @@
-
-
-
-
-
- EnvironmentId
- {a792350e-130b-4b17-9442-9f782b4e3dc6}
-
-
- ProjectExplorer.Project.ActiveTarget
- 0
-
-
- ProjectExplorer.Project.EditorSettings
-
- true
- false
- true
-
- Cpp
-
- CppGlobal
-
-
-
- QmlJS
-
- QmlJSGlobal
-
-
- 2
- UTF-8
- false
- 4
- false
- 80
- true
- true
- 1
- true
- false
- 0
- true
- true
- 0
- 8
- true
- 1
- true
- true
- true
- false
-
-
-
- ProjectExplorer.Project.PluginSettings
-
-
- true
-
- true
- Builtin.TidyAndClazy
- 2
-
-
-
- true
-
-
-
-
- ProjectExplorer.Project.Target.0
-
- Desktop (x86-darwin-generic-mach_o-64bit)
- Desktop (x86-darwin-generic-mach_o-64bit)
- {1dc348e8-307a-4675-a7df-a40c9c375037}
- 0
- 0
- 0
-
-
- CMAKE_BUILD_TYPE:STRING=Debug
- CMAKE_CXX_COMPILER:STRING=%{Compiler:Executable:Cxx}
- CMAKE_C_COMPILER:STRING=%{Compiler:Executable:C}
- CMAKE_PREFIX_PATH:STRING=%{Qt:QT_INSTALL_PREFIX}
- QT_QMAKE_EXECUTABLE:STRING=%{Qt:qmakeExecutable}
-
- /Users/tiger/Documents/learning/cplusplus/build-cpp_lox-Desktop_x86_darwin_generic_mach_o_64bit-Debug
-
-
-
-
- all
-
- true
- CMakeProjectManager.MakeStep
-
- 1
- Build
- Build
- ProjectExplorer.BuildSteps.Build
-
-
-
-
-
- clean
-
- true
- CMakeProjectManager.MakeStep
-
- 1
- Clean
- Clean
- ProjectExplorer.BuildSteps.Clean
-
- 2
- false
-
- Debug
- CMakeProjectManager.CMakeBuildConfiguration
-
-
-
- CMAKE_BUILD_TYPE:STRING=Release
- CMAKE_CXX_COMPILER:STRING=%{Compiler:Executable:Cxx}
- CMAKE_C_COMPILER:STRING=%{Compiler:Executable:C}
- CMAKE_PREFIX_PATH:STRING=%{Qt:QT_INSTALL_PREFIX}
- QT_QMAKE_EXECUTABLE:STRING=%{Qt:qmakeExecutable}
-
- /Users/tiger/Documents/learning/cplusplus/build-cpp_lox-Desktop_x86_darwin_generic_mach_o_64bit-Release
-
-
-
-
- all
-
- true
- CMakeProjectManager.MakeStep
-
- 1
- Build
- Build
- ProjectExplorer.BuildSteps.Build
-
-
-
-
-
- clean
-
- true
- CMakeProjectManager.MakeStep
-
- 1
- Clean
- Clean
- ProjectExplorer.BuildSteps.Clean
-
- 2
- false
-
- Release
- CMakeProjectManager.CMakeBuildConfiguration
-
-
-
- CMAKE_BUILD_TYPE:STRING=RelWithDebInfo
- CMAKE_CXX_COMPILER:STRING=%{Compiler:Executable:Cxx}
- CMAKE_C_COMPILER:STRING=%{Compiler:Executable:C}
- CMAKE_PREFIX_PATH:STRING=%{Qt:QT_INSTALL_PREFIX}
- QT_QMAKE_EXECUTABLE:STRING=%{Qt:qmakeExecutable}
-
- /Users/tiger/Documents/learning/cplusplus/build-cpp_lox-Desktop_x86_darwin_generic_mach_o_64bit-RelWithDebInfo
-
-
-
-
- all
-
- true
- CMakeProjectManager.MakeStep
-
- 1
- Build
- Build
- ProjectExplorer.BuildSteps.Build
-
-
-
-
-
- clean
-
- true
- CMakeProjectManager.MakeStep
-
- 1
- Clean
- Clean
- ProjectExplorer.BuildSteps.Clean
-
- 2
- false
-
- Release with Debug Information
- CMakeProjectManager.CMakeBuildConfiguration
-
-
-
- CMAKE_BUILD_TYPE:STRING=MinSizeRel
- CMAKE_CXX_COMPILER:STRING=%{Compiler:Executable:Cxx}
- CMAKE_C_COMPILER:STRING=%{Compiler:Executable:C}
- CMAKE_PREFIX_PATH:STRING=%{Qt:QT_INSTALL_PREFIX}
- QT_QMAKE_EXECUTABLE:STRING=%{Qt:qmakeExecutable}
-
- /Users/tiger/Documents/learning/cplusplus/build-cpp_lox-Desktop_x86_darwin_generic_mach_o_64bit-MinSizeRel
-
-
-
-
- all
-
- true
- CMakeProjectManager.MakeStep
-
- 1
- Build
- Build
- ProjectExplorer.BuildSteps.Build
-
-
-
-
-
- clean
-
- true
- CMakeProjectManager.MakeStep
-
- 1
- Clean
- Clean
- ProjectExplorer.BuildSteps.Clean
-
- 2
- false
-
- Minimum Size Release
- CMakeProjectManager.CMakeBuildConfiguration
-
- 4
-
-
- 0
- Deploy
- Deploy
- ProjectExplorer.BuildSteps.Deploy
-
- 1
- ProjectExplorer.DefaultDeployConfiguration
-
- 1
-
-
- dwarf
-
- cpu-cycles
-
-
- 250
-
- -e
- cpu-cycles
- --call-graph
- dwarf,4096
- -F
- 250
-
- -F
- true
- 4096
- false
- false
- 1000
-
- true
-
- false
- false
- false
- false
- true
- 0.01
- 10
- true
- kcachegrind
- 1
- 25
-
- 1
- true
- false
- true
- valgrind
-
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
-
- 2
-
- cpp_lox
- CMakeProjectManager.CMakeRunConfiguration.cpp_lox
- cpp_lox
-
- false
-
- false
- true
- false
- true
- false
- false
- true
-
- /private/var/folders/tw/ss12ypqj7dj84rk07_x7pzmw0000gp/T/QtCreator-nklAxp/qtc-cmake-popfLRfM
-
- 1
-
-
-
- ProjectExplorer.Project.TargetCount
- 1
-
-
- ProjectExplorer.Project.Updater.FileVersion
- 22
-
-
- Version
- 22
-
-
diff --git a/Interpreter.cpp b/Interpreter.cpp
index e7cb778..f8798c5 100644
--- a/Interpreter.cpp
+++ b/Interpreter.cpp
@@ -15,6 +15,7 @@
#include "./LoxCallable.hpp"
#include "./LoxFunction.hpp"
#include "./ReturnError.hpp"
+#include "./LoxClass.hpp"
#include "./lox.hpp"
using std::stod;
@@ -184,18 +185,24 @@ Object Interpreter::visitCallExpr(shared_ptr> expr) {
arguments.push_back(evaluate(argument));
}
- if (callee.type != Object::Object_fun) {
+ if (callee.type != Object::Object_fun && callee.type != Object::Object_class) {
throw RuntimeError(expr->paren,
"Can only call functions and classes.");
}
- shared_ptr function = callee.function;
- if (arguments.size() != function->arity()) {
+ shared_ptr callable;
+ if (callee.type == Object::Object_fun) {
+ callable = callee.function;
+ }
+ if (callee.type == Object::Object_class) {
+ callable = callee.lox_class;
+ }
+ if (arguments.size() != callable->arity()) {
throw RuntimeError(expr->paren, "Expected " +
- to_string(function->arity()) + " arguments but got " +
+ to_string(callable->arity()) + " arguments but got " +
to_string(arguments.size()) + ".");
}
- return function->call(shared_from_this(), arguments);
+ return callable->call(shared_from_this(), arguments);
}
void Interpreter::visitExpressionStmt(const Expression& stmt) {
@@ -226,6 +233,12 @@ void Interpreter::visitBlockStmt(const Block& stmt) {
executeBlock(stmt.statements, env);
}
+void Interpreter::visitClassStmt(const Class& stmt) {
+ environment->define(stmt.name.lexeme, Object::make_nil_obj());
+ auto klass = shared_ptr(new LoxClass(stmt.name.lexeme));
+ environment->assign(stmt.name, Object::make_class_obj(klass));
+}
+
void Interpreter::visitWhileStmt(const While& stmt) {
while (isTruthy(evaluate(stmt.condition))) {
Object obj = evaluate(stmt.condition);
diff --git a/Interpreter.hpp b/Interpreter.hpp
index dba77b1..a282aee 100644
--- a/Interpreter.hpp
+++ b/Interpreter.hpp
@@ -38,6 +38,7 @@ class Interpreter:
void visitPrintStmt(const Print& stmt);
void visitVarStmt(const Var& stmt);
void visitBlockStmt(const Block& stmt);
+ void visitClassStmt(const Class& stmt);
void visitIfStmt(const If& stmt);
void visitWhileStmt(const While& stmt);
void visitFunctionStmt(const Function& stmt);
diff --git a/LoxClass.cpp b/LoxClass.cpp
new file mode 100644
index 0000000..604ce2d
--- /dev/null
+++ b/LoxClass.cpp
@@ -0,0 +1,31 @@
+// Copyright 2020.2.23
+
+#include
+#include
+#include
+
+#include "./LoxClass.hpp"
+#include "./LoxInstance.hpp"
+#include "./Interpreter.hpp"
+#include "./Token.hpp"
+
+using std::string;
+using std::shared_ptr;
+using std::vector;
+
+
+LoxClass::LoxClass(string name_): name(name_) {}
+
+Object LoxClass::call(
+ shared_ptr interpreter,
+ vector