Skip to content

Commit

Permalink
update(block): added bock but need to sole errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohith-Raju committed Apr 5, 2024
1 parent d69ffd0 commit 8c281c9
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 5 deletions.
4 changes: 4 additions & 0 deletions include/Interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Interpreter {

void excecute(Statement *stmnt);

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

Object evaluate(Expr *expr);

bool isTruthy(Object right);
Expand All @@ -35,6 +37,8 @@ class Interpreter {

void visitPrintStmnt(Print *expr);

void visitBlockStmnt(Block *expr);

void visitExprStmnt(Expression *expr);

void visitVarStmnt(Var *expr);
Expand Down
1 change: 1 addition & 0 deletions include/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Parser {
Statement *statement();
Statement *printStatement();
Statement *expressionStatement();
std::vector<Statement *> blockStatement();

// Variable stuff
Statement *declaration();
Expand Down
15 changes: 14 additions & 1 deletion include/Statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
#define CRUX_STATEMENT_H

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

enum Statement_type { StmntExpr_type, StmntPrint_type, StmntVar_type };
enum Statement_type {
StmntExpr_type,
StmntPrint_type,
StmntVar_type,
StmntBlock_type
};

class Statement {
public:
Expand Down Expand Up @@ -37,4 +43,11 @@ class Var : public Statement {
~Var();
};

class Block : public Statement {
public:
std::vector<Statement *> stmnt;
Block();
Block(std::vector<Statement *> stmtn);
};

#endif
4 changes: 4 additions & 0 deletions include/env/Env.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@

class Environment {
private:
Environment *enclosing;
std::unordered_map<std::string, Object> values;

public:
Environment();
~Environment();
Environment(Environment *enclosing);
void define(std::string name, Object value);
void assign(Token *name, Object value);
Object get(Token *name);
Expand Down
19 changes: 19 additions & 0 deletions src/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Interpreter.h"
#include "Error.h"
#include "Expr.h"
#include "Statement.h"
#include "utls/Object.h"
#include "utls/RuntimeError.h"
#include <iostream>
Expand All @@ -22,6 +23,8 @@ void Interpreter::excecute(Statement *stmnt) {
case StmntVar_type:
visitVarStmnt((Var *)stmnt);
break;
case StmntBlock_type:
visitBlockStmnt((Block *)stmnt);
}
}

Expand Down Expand Up @@ -114,6 +117,22 @@ void Interpreter::visitVarStmnt(Var *stmnt) {
environment->define(stmnt->name->lexeme, value);
}

void Interpreter::visitBlockStmnt(Block *stmnt) {
excecuteBlock(stmnt->stmnt, environment);
}

void Interpreter::excecuteBlock(std::vector<Statement *> stmnts,
Environment *env) {
Environment *previous = environment;
environment = env;
for (Statement *stmnt : stmnts) {
excecute(stmnt);
if (crux::hadRuntimeError)
break;
}
environment = previous;
}

Object Interpreter::visitVariableExp(Variable *expr) {
return environment->get(expr->name);
}
Expand Down
15 changes: 14 additions & 1 deletion src/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,22 @@ Statement *Parser::varDeclaration() {
Statement *Parser::statement() {
if (match(PRINT))
return printStatement();
if (match(LEFT_BRACE))
return new Block(blockStatement());
return expressionStatement();
}

std::vector<Statement *> Parser::blockStatement() {
std::vector<Statement *> stmnts;

if (!check(RIGHT_BRACE) && !isAtEnd()) {
stmnts.push_back(declaration());
}

consume(RIGHT_BRACE, "Expect closing } after the block");
return stmnts;
}

Statement *Parser::printStatement() {
Expr *expr = expression();
consume(SEMICOLON, "Expected ; at the end of the statement");
Expand All @@ -74,7 +87,7 @@ Expr *Parser::assignment() {
Token *name = var->name;
return new Assignment(name, value);
}
error(peek(), "Invalid assignment target.");
error(equals, "Invalid assignment target.");
}
return expr;
}
Expand Down
6 changes: 6 additions & 0 deletions src/Statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//

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

Statement::Statement(Statement_type type) : type(type) {}

Expand All @@ -21,3 +22,8 @@ Var::~Var() {
delete name;
delete expression;
}

Block::Block() : Statement(StmntBlock_type) {}

Block::Block(std::vector<Statement *> stmnt)
: Statement(StmntBlock_type), stmnt(stmnt) {}
19 changes: 17 additions & 2 deletions src/env/Env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include "env/Env.h"
#include "utls/RuntimeError.h"

Environment::Environment() : enclosing(nullptr) {}

Environment::Environment(Environment *enclosing) : enclosing(enclosing) {}

void Environment::define(std::string name, Object value) {
values.insert({name, value});
}
Expand All @@ -14,13 +18,24 @@ void Environment::assign(Token *name, Object value) {
values[name->lexeme] = value;
return;
}

if (enclosing != nullptr) {
enclosing->assign(name, value);
return;
}

throw new RuntimeError(*name, "Undefined variable " + name->lexeme);
}

Object Environment::get(Token *name) {
if (values.find(name->lexeme) != values.end())
return values[name->lexeme];
else {
throw new RuntimeError(*name, "Unexpected variable " + name->lexeme);

if (enclosing != nullptr) {
return enclosing->get(name);
}

throw new RuntimeError(*name, "Unexpected variable " + name->lexeme);
}

Environment::~Environment() { delete enclosing; }

0 comments on commit 8c281c9

Please sign in to comment.