Skip to content

Commit

Permalink
Merge branch 'statements'
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohith-Raju committed May 17, 2024
2 parents d726d9f + cd5bc0c commit b645ca6
Show file tree
Hide file tree
Showing 22 changed files with 402 additions and 43 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ jobs:
- name: Configure Project
uses: threeal/cmake-action@v1.3.0
with:
c-compiler: gcc
cxx-compiler: g++
c-compiler: clang
cxx-compiler: clang++
cxx-flags: "--std=c++17"

- name: Build Project
run: cmake --build build

- name: Run-Tests
run: ./build/test/run_crux_test
run: ./build/test/run_crux_test
19 changes: 19 additions & 0 deletions include/CruxCallable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Cruxcallable.h contains function definations that is overridden
// by CruxFunction and Crux Class
//
//
#pragma once

#include "utls/Object.h"
#include <string>
#include <vector>

class Interpreter;

class CruxCallable {
public:
virtual int arity() = 0;
virtual Object call(Interpreter *interpreter, std::vector<Object>) = 0;
virtual std::string str() = 0;
};
17 changes: 14 additions & 3 deletions include/Expr.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//
// Created by Rohith on 1/25/24.
//

#ifndef CRUX_EXPR_H
#define CRUX_EXPR_H

#include "Token.h"
#include <vector>

enum ExprType {
ExprType_Binary,
Expand All @@ -15,7 +14,8 @@ enum ExprType {
ExprType_Ternary,
ExprType_Variable,
ExprType_Assignment,
ExprType_Logical
ExprType_Logical,
ExprType_Call
};

class Expr {
Expand All @@ -38,6 +38,17 @@ class Binary : public Expr {
~Binary();
};

class Call : public Expr {
public:
Expr *callee;
Token *paren;
std::vector<Expr *> arguments;

Call(Expr *callee, Token *paren, std::vector<Expr *> arguments);

~Call();
};

class Grouping : public Expr {
public:
Expr *expression;
Expand Down
16 changes: 16 additions & 0 deletions include/Function.h
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
26 changes: 20 additions & 6 deletions include/Interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@

class Interpreter {
private:
Environment *environment = new Environment();

void excecute(Statement *stmnt);

void excecuteBlock(std::vector<Statement *> stmnts, Environment *env);

Object evaluate(Expr *expr);

bool isTruthy(Object right);
Expand All @@ -33,8 +29,22 @@ class Interpreter {
bool checkCompatibility(Token *op, Object left, Object right);

public:
Environment *globals = new Environment();

private:
Environment *environment = globals;

public:
Interpreter();

bool isBreakUsed = false;

bool isReturnUsed = false;

Object returnObj;

Object excecuteBlock(std::vector<Statement *> stmnts, Environment *env);

void interpret(std::vector<Statement *> &statements);

void visitPrintStmnt(Print *stmnt);
Expand All @@ -49,6 +59,10 @@ class Interpreter {

void visitWhileStmnt(While *stmnt);

void visitFuncStmnt(Function *stmnt);

void visitReturnStmnt(Return *stmnt);

Object visitAssignment(Assignment *expr);

Object visitLogicalExp(Logical *expr);
Expand All @@ -57,15 +71,15 @@ class Interpreter {

Object visitGroupExp(Grouping *expr);

Object visitCall(Call *stmnt);

Object visitUnaryExp(Unary *expr);

Object visitBinaryExp(Binary *expr);

Object visitTernaryExp(Ternary *expr);

Object visitVariableExp(Variable *expr);

~Interpreter();
};

#endif // CRUX_INTERPRETER_H
23 changes: 23 additions & 0 deletions include/NativeFunction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include "CruxCallable.h"
#include <chrono>
#include <string>
#include <vector>
class Interpreter;

class ClockFunction : public CruxCallable {
virtual int arity() override { return 0; }

virtual Object call(Interpreter *interpreter, std::vector<Object>) override {
auto millisec_since_epoch =
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();

Object value((double)millisec_since_epoch / 1000.0);
return value;
}

virtual std::string str() override { return "<native fn>"; }
};
8 changes: 7 additions & 1 deletion include/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ class Parser {
Expr *term();
Expr *factor();
Expr *unary();
Expr *call();
Expr *primary();

// Expr helper functions
Expr *finishCall(Expr *expr);

// Statement helper functions
Statement *statement();
Statement *printStatement();
Expand All @@ -40,13 +44,15 @@ class Parser {
Statement *forStatement();
Statement *breakStatement();
Statement *expressionStatement();
Statement *function(std::string str);
Statement *returnStatement();
std::vector<Statement *> blockStatement();

// Variable stuff
Statement *declaration();
Statement *varDeclaration();

// helper functions
// Helper functions
Expr *equality();
bool check(TokenType type);
Token advance();
Expand Down
6 changes: 6 additions & 0 deletions include/Return.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "utls/Object.h"

struct ReturnValue {
Object value;
ReturnValue(Object value) : value(value){};
};
22 changes: 21 additions & 1 deletion include/Statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define CRUX_STATEMENT_H

#include "Expr.h"
#include "Token.h"
#include <vector>

enum Statement_type {
Expand All @@ -15,7 +16,9 @@ enum Statement_type {
StmntBlock_type,
StmntIf_type,
StmntWhile_type,
StmntBreak_type
StmntBreak_type,
StmntFunc_type,
StmntReturn_type
};

class Statement {
Expand Down Expand Up @@ -76,4 +79,21 @@ class Break : public Statement {
Break(bool breakSet);
};

class Function : public Statement {
public:
Token *name;
std::vector<Token *> params;
std::vector<Statement *> body;

Function(Token *name, std::vector<Token *> params,
std::vector<Statement *> body);
};

class Return : public Statement {
public:
Token *keyword;
Expr *value;
Return(Token *keyword, Expr *value);
};

#endif
10 changes: 8 additions & 2 deletions include/env/Env.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
// Created by rohith on 26/03/2024
//

#ifndef ENV_H
#define ENV_H

#include "Token.h"
#include "utls/Object.h"
#include <string>
#include <unordered_map>

class Environment {
Expand All @@ -14,9 +16,13 @@ class Environment {

public:
Environment();
~Environment();
//~Environment();
Environment(Environment *enclosing);
void define(Token *tkn, Object value);
void define(std::string name, Object value);
void assign(Token *name, Object value);
Object get(Token *name);
void deepClean(Environment *enclosing);
};

#endif
12 changes: 6 additions & 6 deletions include/utls/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@

#include <string>

enum ObjType {
nullptr_type,
num_type,
string_type,
bool_type,
};
class CruxCallable;

enum ObjType { nullptr_type, num_type, string_type, bool_type, function_type };
class Object {
public:
ObjType type;
double num_literal;
bool bool_literal;
std::string string_literal;
CruxCallable *function;

Object();
Object(bool type);
Object(double type);
Object(std::string type);
Object(CruxCallable *func);
std::string str();
};
#endif // CRUX_OBJECT_H
47 changes: 43 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,42 @@
#include "Interpreter.h"
#include "Scanner.h"
#include <Parser.h>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <memory>
#include <ostream>
#include <sstream>
#include <string>
#include <vector>

namespace fs = std::filesystem;

void runCode(std::string source);

std::string readFile(fs::path path) {
if (path.extension() != ".crux") {
std::cerr << "Filename " << path.filename()
<< " should end with the extension \".crux\" \n";
exit(64);
}

std::ostringstream data;
std::fstream file(path, std::ios_base::in);
if (!file.is_open()) {
std::cerr << "File could't be found or opened \n";
exit(64);
}
data << file.rdbuf();
return data.str();
}

void runFile(std::string path) {
fs::path fPath(path);
runCode(readFile(fPath));
}

void runPromt() {
for (;;) {
std::cout << "> ";
Expand All @@ -25,16 +55,25 @@ void runPromt() {
}
}

Interpreter interpreter{};

void runCode(std::string source) {
Scanner scanner(source);
std::vector<Token> tokens = scanner.scanTokens();
Parser parser(tokens);
std::vector<Statement *> expression = parser.parse();
std::unique_ptr<Interpreter> interpreter = std::make_unique<Interpreter>();
interpreter->interpret(expression);
interpreter.interpret(expression);
}

int main() {
runPromt();
int main(int argc, char *argv[]) {
if (argc > 2) {
std::cout << "usage: crux <script>\n";
std::cout << "using repl: ./crux\n";
exit(64);
} else if (argc == 2) {
runFile(argv[1]);
} else {
runPromt();
}
return 0;
}
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(Sources Error.cpp
AstPrinter.cpp
Interpreter.cpp
Statement.cpp
Function.cpp
env/Env.cpp
)

Expand Down
Loading

0 comments on commit b645ca6

Please sign in to comment.