-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update(function): add support for functions
- Loading branch information
1 parent
484e49e
commit e3965db
Showing
12 changed files
with
127 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef FUNCTION_H | ||
#define FUNCTION_H | ||
|
||
#include "CruxCallable.h" | ||
#include "Statement.h" | ||
|
||
class CruxFunction : public CruxCallable { | ||
public: | ||
Function *declaration; | ||
CruxFunction(Function *declaration); | ||
virtual int arity() override; | ||
virtual Object call(Interpreter *interpreter, std::vector<Object>) override; | ||
virtual std::string str() override; | ||
}; | ||
|
||
#endif // FUNCTION_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ set(Sources Error.cpp | |
AstPrinter.cpp | ||
Interpreter.cpp | ||
Statement.cpp | ||
Function.cpp | ||
env/Env.cpp | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// created by rohith on 20-04-2024 | ||
// | ||
|
||
#include "Function.h" | ||
#include "Interpreter.h" | ||
#include "env/Env.h" | ||
#include "utls/Object.h" | ||
#include <string> | ||
|
||
CruxFunction::CruxFunction(Function *declaration) : declaration(declaration) {} | ||
|
||
int CruxFunction::arity() { return declaration->params.size(); } | ||
|
||
Object CruxFunction::call(Interpreter *interpreter, std::vector<Object> args) { | ||
Environment *env = new Environment(interpreter->globals); | ||
for (int i = 0; i < declaration->params.size(); i++) { | ||
env->define(declaration->params[i], args[i]); | ||
} | ||
interpreter->excecuteBlock(declaration->body, env); | ||
return Object(); | ||
} | ||
|
||
std::string CruxFunction::str() { | ||
return "<\"fn" + declaration->name->lexeme + ">\""; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters