Skip to content

Commit

Permalink
Merge pull request #12 from Katarni/interpreter
Browse files Browse the repository at this point in the history
Interpreter
  • Loading branch information
Katarni authored Dec 7, 2024
2 parents a119fc5 + 2a044a6 commit b7298c7
Show file tree
Hide file tree
Showing 31 changed files with 2,112 additions and 262 deletions.
59 changes: 56 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ jobs:
cd tests/trie/build
cmake ..
make
cd ..
- name: Run test
run: |
cd tests/trie
Expand All @@ -47,7 +46,6 @@ jobs:
cd tests/lexer/build
cmake ..
make
cd ..
- name: Run test
run: |
cd tests/lexer
Expand All @@ -72,8 +70,63 @@ jobs:
cd tests/sintex/build
cmake ..
make
cd ..
- name: Run test
run: |
cd tests/sintex
pytest .
segment-tree-test:
needs: sintex-test
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: setup Python
uses: actions/setup-python@v5
with:
python-version: "3.13"
- name: install pytest
run: pip install pytest
- name: Build yuhan segment-tree
run: |
mkdir tests/segment-tree/build
cd tests/segment-tree/build
cmake ..
make
- name: Build segment tree
run: |
cd tests/segment-tree
g++ segment-tree.cpp -o segment-tree
- name: Run test
run: |
cd tests/segment-tree
pytest .
sort-test:
needs: sintex-test
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: setup Python
uses: actions/setup-python@v5
with:
python-version: "3.13"
- name: install pytest
run: pip install pytest
- name: Build yuhan sort
run: |
mkdir tests/sort/build
cd tests/sort/build
cmake ..
make
- name: Build sort
run: |
cd tests/sort
g++ sort.cpp -o sort
- name: Run test
run: |
cd tests/sort
pytest .
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ add_executable(Yuhan main.cpp
source/ReservedMemory.cpp
include/PRNGenerator.h
source/PRNGenerator.cpp
source/Interpreter.cpp
include/Interpreter.h
)

54 changes: 54 additions & 0 deletions include/Interpreter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// Created by Timur Akhmetzianov on 22.11.2024
//

#pragma once

#include "PRNGenerator.h"


class Interpreter {
public:
explicit Interpreter(PRNGenerator *generator) : generator_(generator), cur_(0) {}

void global();

void callFunc(const std::string& func, const std::vector<std::variant<ReservedMemory*, Literal>>& vars);

private:
class FunctionCall {
public:
explicit FunctionCall(size_t return_address) : return_address_(return_address) {}
~FunctionCall();

ReservedMemory*& getVar(const Identifier& name);
bool findVar(const Identifier& name);
ReservedMemory*& createVar(const Identifier& name);

void setVar(const std::string& name, ReservedMemory* var_ptr);
void setVar(const std::string& name, const ReservedMemory& var);

[[nodiscard]]
size_t returnAddress() const;

private:
size_t return_address_;
std::map<std::string, ReservedMemory*> vars_;
std::map<std::string, bool> is_lvalue_arg_;
};

size_t cur_;

std::stack<std::stack<std::variant<ReservedMemory*, Literal, size_t, std::string>>> calc_stack_;
PRNGenerator *generator_;

std::map<std::string, ReservedMemory*> global_vars_;
std::stack<FunctionCall> function_stack_;

void operation(const Token& operation);
void operation(PRNGenerator::SysVals operation);
void lvalueOperation(const Token& operation);

ReservedMemory*& getVar(const Identifier& name);
ReservedMemory*& createVar(const Identifier& name);
};
6 changes: 5 additions & 1 deletion include/PRNGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class PRNGenerator {

enum class SysVals {
FuncEnd, FuncStart, Semicolon, Return,
GoTo, GoToByFalse, SwitchCmp, Scan, Print
GoTo, GoToByFalse, SwitchCmp, Scan, Print, Comma
};

enum class PRNType {
Expand Down Expand Up @@ -59,6 +59,10 @@ class PRNGenerator {
std::pair<std::variant<Identifier, Token, Literal,
size_t, std::string, SysVals>, PRNType> getById(size_t idx) const;

std::pair<size_t, size_t> getFuncParams(const std::string &id);

const std::string& mainId() const;

private:
size_t cur_;
std::string main_id_;
Expand Down
79 changes: 65 additions & 14 deletions include/ReservedMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class Type {

void clear();

std::map<std::string, Type*> getFields();

private:
std::string name_;
size_t size_array_;
Expand All @@ -60,7 +62,7 @@ class Identifier {
Identifier(std::string id, Type type) : name_(id), type_(type) {}

[[nodiscard]]
Type& getType();
Type getType() const;

void setType(Type other);

Expand All @@ -79,38 +81,87 @@ class Identifier {

class Literal {
public:
Literal() = default;
Literal(Type type, std::string data);

[[nodiscard]]
Type& getType();

Type getType() const;
void setType(Type other);

[[nodiscard]]
bool isTrue() const;

[[nodiscard]]
std::variant<int, char, bool, float, std::string>& getData();
std::variant<int, char, bool, float, std::string> getData() const;

private:
friend std::ostream& operator<<(std::ostream& os, const Literal& lit);

friend Literal operator!(const Literal& literal);

friend Literal operator+(const Literal& lhs, const Literal& rhs);
friend Literal operator-(const Literal& lhs, const Literal& rhs);
friend Literal operator*(const Literal& lhs, const Literal& rhs);
friend Literal operator/(const Literal& lhs, const Literal& rhs);
friend Literal operator%(const Literal& lhs, const Literal& rhs);
friend Literal operator&&(const Literal& lhs, const Literal& rhs);
friend Literal operator||(const Literal& lhs, const Literal& rhs);
friend Literal operator&(const Literal& lhs, const Literal& rhs);
friend Literal operator|(const Literal& lhs, const Literal& rhs);
friend Literal operator^(const Literal& lhs, const Literal& rhs);
friend Literal operator>>(const Literal& lhs, const Literal& rhs);
friend Literal operator<<(const Literal& lhs, const Literal& rhs);

friend Literal operator==(const Literal& lhs, const Literal& rhs);
friend Literal operator!=(const Literal& lhs, const Literal& rhs);
friend Literal operator<(const Literal& lhs, const Literal& rhs);
friend Literal operator>(const Literal& lhs, const Literal& rhs);
friend Literal operator<=(const Literal& lhs, const Literal& rhs);
friend Literal operator>=(const Literal& lhs, const Literal& rhs);

protected:
std::variant<int, char, bool, float, std::string> data_;
Type type_;

void setLvalue(bool lvalue) {
type_.setLvalue(lvalue);
}
};


class ReservedMemory {
class ReservedMemory : public Literal {
public:
ReservedMemory() = default;
explicit ReservedMemory(Type type);
ReservedMemory(const Literal& other);
ReservedMemory(const ReservedMemory& other);
ReservedMemory& operator=(const ReservedMemory& other);
ReservedMemory& operator=(const Literal& other);

[[nodiscard]]
Type& getType() {
return type_;
}

[[nodiscard]]
ReservedMemory* getFieldByName(const std::string& name) const;
ReservedMemory*& getFieldByName(const std::string& name);

void setFields(const std::vector<std::pair<std::string, Type>>& fields);

friend std::istream& operator>>(std::istream& is, ReservedMemory*& var);

ReservedMemory*& operator[](const Literal& idx);
ReservedMemory*& operator[](size_t idx);

ReservedMemory& operator+=(const Literal& other);
ReservedMemory& operator-=(const Literal& other);
ReservedMemory& operator/=(const Literal& other);
ReservedMemory& operator*=(const Literal& other);
ReservedMemory& operator%=(const Literal& other);
ReservedMemory& operator^=(const Literal& other);
ReservedMemory& operator&=(const Literal& other);
ReservedMemory& operator|=(const Literal& other);
ReservedMemory& operator<<=(const Literal& other);
ReservedMemory& operator>>=(const Literal& other);

friend Literal operator==(ReservedMemory& lhs, ReservedMemory& rhs);
friend Literal operator!=(ReservedMemory& lhs, ReservedMemory& rhs);

private:
Type type_;
std::variant<int, bool, float, char, std::string,
std::vector<ReservedMemory*>, std::map<std::string, ReservedMemory*>> data_;
std::variant<std::vector<ReservedMemory*>, std::map<std::string, ReservedMemory*>> structs_data_;
};
6 changes: 3 additions & 3 deletions include/SemanticStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class SemanticStack {
public:
SemanticStack(PRNGenerator* generator) : generator_(generator) {}
explicit SemanticStack(PRNGenerator* generator) : generator_(generator) {}

void push(const Type& operand) noexcept;
void push(const Token& operation) noexcept;
Expand All @@ -28,8 +28,8 @@ class SemanticStack {

class Error : public std::exception {
public:
explicit Error(std::string what) : std::exception(), what_(std::move(what)) {}
explicit Error(const char* what) : std::exception(), what_(what) {}
Error(const Type& lhs, const Token& operation, const Type& rhs);
Error(const Type& operand, const Token& operation);

[[nodiscard]]
const char* what() const noexcept override;
Expand Down
4 changes: 4 additions & 0 deletions include/TIDTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class TIDTree {

void checkContinue();

bool isStructScope();

std::vector<std::pair<std::string, Type>> getStructsFields(const std::string& name);

private:
class NodeTID {
public:
Expand Down
Loading

0 comments on commit b7298c7

Please sign in to comment.