Giter Site home page Giter Site logo

interpreter-'s Introduction

Simple Interpreter

This project is a simple interpreter that tokenizes, parses, and evaluates arithmetic expressions and assignments in a basic scripting language. It includes a lexer, parser, and evaluator to process and execute the input code.

Features

  • Lexer: Tokenizes the input string into tokens such as numbers, identifiers, operators, and parentheses.
  • Parser: Builds an Abstract Syntax Tree (AST) from the tokens.
  • Evaluator: Evaluates the AST to compute results and handle variable assignments.
  • Supports:
    • Arithmetic operations: +, -, *, /
    • Assignment operations: =
    • Parentheses for grouping
    • Variables

Components

Token Types

Enumerations representing different types of tokens:

  • NUMBER
  • IDENTIFIER
  • ASSIGN
  • PLUS
  • MINUS
  • MULTIPLY
  • DIVIDE
  • LPAREN
  • RPAREN
  • END
  • INVALID

Token Structure

Structure to hold token type and value:

struct Token {
    TokenType type;
    string value;
};

Lexer

Class that tokenizes the input string into a vector of tokens.

class Lexer {
public:
    explicit Lexer(const string &input);
    vector<Token> tokenize();
private:
    string input;
    size_t pos;
    string readNumber();
    string readIdentifier();
};

Abstract Syntax Tree (AST) Nodes

Various node types representing different components of the AST:

  • NUMBER_NODE
  • IDENTIFIER_NODE
  • ASSIGN_NODE
  • PLUS_NODE
  • MINUS_NODE
  • MULTIPLY_NODE
  • DIVIDE_NODE

Base Node Class

struct Node {
    virtual ~Node() = default;
    virtual NodeType getType() const = 0;
};

Number Node

struct NumberNode : Node {
    int value;
    explicit NumberNode(int value);
    NodeType getType() const override;
};

Identifier Node

struct IdentifierNode : Node {
    string name;
    explicit IdentifierNode(const string &name);
    NodeType getType() const override;
};

Binary Operation Node

struct BinaryOpNode : Node {
    NodeType type;
    unique_ptr<Node> left;
    unique_ptr<Node> right;
    BinaryOpNode(NodeType type, unique_ptr<Node> left, unique_ptr<Node> right);
    NodeType getType() const override;
};

Assignment Node

struct AssignNode : Node {
    unique_ptr<IdentifierNode> left;
    unique_ptr<Node> right;
    AssignNode(unique_ptr<IdentifierNode> left, unique_ptr<Node> right);
    NodeType getType() const override;
};

Parser

Class that builds the AST from the tokens.

class Parser {
public:
    explicit Parser(const vector<Token> &tokens);
    unique_ptr<Node> parse();
private:
    const vector<Token> &tokens;
    size_t pos;
    unique_ptr<Node> assignment();
    unique_ptr<Node> expression();
    unique_ptr<Node> term();
    unique_ptr<Node> factor();
    void consume(TokenType type);
    Token currentToken() const;
};

Evaluator

Class that evaluates the AST to produce results and handle variable assignments.

class Evaluator {
public:
    int eval(unique_ptr<Node> &node);
    unordered_map<string, int> getEnv() const;
private:
    unordered_map<string, int> env;
};

How to Run

  1. Compile the code using a C++ compiler:
g++ -o interpreter main.cpp
  1. Run the compiled executable:
./interpreter

interpreter-'s People

Contributors

sahil-vaidya avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.