Skip to content

gzhiahia/function-plotter-interpreter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Function Plotter Interpreter

C++ GoogleTest Windows GDI License

Function Plotter Demo

Overview

This interpreter processes a simple plotting language and generates 2D parametric curves. It demonstrates core compiler design concepts:

  • Front-End: Lexer (DFA-based scanner) → Parser (recursive-descent with operator precedence)
  • Middle-End: Semantic analyzer with expression evaluator and coordinate transformation engine
  • Back-End: Dual rendering systems (console debugger and Windows GDI visualizer)

Requirements

  • Compiler: GCC 7+, Clang 5+, or MSVC 2017+ with C++17 support
  • CMake: 3.15 or higher
  • Operating System:
    • Windows: Full support (console + GUI)
    • Linux/macOS: Console mode only
  • Google Test: All dependencies are included in the repository (lib/googletest/) for out-of-the-box building

Quick Start

Clone and Build

git clone https://github.com/gzhiahia/function-plotter-interpreter.git
cd function-plotter-interpreter

# Windows (MinGW)
cmake -B build -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release
cmake --build build

# Windows (Visual Studio)
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --config Release

# Linux/macOS
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build -j $(nproc)

Run Examples

Basic Usage:

# Console version
.\build\bin\interpreter_console.exe example\interpreter_test.txt

# Windows GUI version
.\build\bin\interpreter_window.exe example\interpreter_test.txt

Command-line Options:

# Show help information
.\build\bin\interpreter_console.exe -h
.\build\bin\interpreter_console.exe --help

# Enable debug mode (shows detailed execution info)
.\build\bin\interpreter_console.exe example\interpreter_test.txt -d
.\build\bin\interpreter_console.exe example\interpreter_test.txt --debug

Run Tests

cd build

# Run all tests
ctest --output-on-failure

# Run specific test suite
ctest -R LexerTest
ctest -R ParserTest

# Verbose output
ctest -V

Language Syntax

Basic Structure

// Comments start with double slash

-- Set coordinate system
ORIGIN IS (x, y);          -- Translation
SCALE IS (sx, sy);         -- Scaling factors
ROT IS angle;              -- Rotation angle (radians)

-- Draw parametric curve
FOR variable FROM start TO end STEP increment DRAW (x_expr, y_expr);

Example

// Draw a circle
ORIGIN IS (400, 300);
SCALE IS (100, 100);
ROT IS 0;
FOR T FROM 0 TO 2*PI STEP 0.01 DRAW (COS(T), SIN(T));

Built-in Functions

  • Trigonometric: SIN(x), COS(x), TAN(x)
  • Exponential: EXP(x)
  • Logarithmic: LN(x)
  • Square Root: SQRT(x)

Constants

  • PI - π (3.14159...)
  • E - Euler's number (2.71828...)

Operators

  • Arithmetic: +, -, *, /, **
  • Parentheses: (, )

Project Structure

function-plotter-interpreter/
├── CMakeLists.txt
├── LICENSE
├── README.md
├── .gitignore
│
├── assets/
│   └── demo.jpg
│
├── include/                     # Public header files
│   ├── core/
│   │   └── Interpreter.h          # Main interpreter
│   ├── lexer/
│   │   ├── DFA.h                  # Finite automaton
│   │   ├── Scanner.h              # Lexical scanner
│   │   └── Token.h                # Token definitions
│   ├── parser/
│   │   ├── ExpressionNode.h       # AST nodes
│   │   └── Parser.h               # Recursive-descent parser
│   ├── semantic/
│   │   ├── ExpressionEvaluator.h  # Expression evaluator
│   │   ├── SemanticAnalyzer.h     # Semantic analyzer
│   │   └── TransformationEngine.h # Coordinate transforms
│   └── ui/
│       ├── IRenderer.h            # Renderer interface
│       ├── ConsoleRenderer.h      # Console renderer
│       └── WindowRenderer.h       # Windows GDI renderer
│
├── src/                         # Implementation files
│   ├── core/
│   │   └── Interpreter.cpp
│   ├── lexer/
│   │   ├── DFA.cpp
│   │   ├── Scanner.cpp
│   │   └── Token.cpp
│   ├── parser/
│   │   ├── ExpressionNode.cpp
│   │   └── Parser.cpp
│   ├── semantic/
│   │   ├── ExpressionEvaluator.cpp
│   │   ├── SemanticAnalyzer.cpp
│   │   └── TransformationEngine.cpp
│   ├── ui/
│   │   ├── ConsoleRenderer.cpp
│   │   └── WindowRenderer.cpp
│   └── main/
│       ├── ConsoleMain.cpp        # Console entry point
│       └── WindowMain.cpp         # Windows GUI entry point
│
├── test/                        # Unit tests
│   ├── LexerTest.cpp
│   └── ParserTest.cpp
│
├── example/                     # Example scripts
│   ├── interpreter_test.txt
│   ├── lexer_test.txt
│   └── parser_test.txt
│
└── lib/                         # Third-party libraries
    └── googletest/

Architecture Overview

┌───────────────────────────────────────────────────┐
│                  Main Application                 │
│             (ConsoleMain / WindowMain)            │
└───────────────────────┬───────────────────────────┘
                        │
                        ▼
┌───────────────────────────────────────────────────┐
│                    Interpreter                    │
│       (Orchestrates the compilation process)      │
└────┬───────────┬─────────────┬──────────────┬─────┘
     │           │             │              │
     ▼           ▼             ▼              ▼
 ┌───────┐   ┌────────┐   ┌──────────┐   ┌──────────┐
 │ Lexer │ → │ Parser │ → │ Semantic │ → │ Renderer │
 │ (DFA) │   │ (AST)  │   │ Analyzer │   │   (UI)   │
 └───────┘   └────────┘   └──────────┘   └──────────┘

Roadmap

  • Color & Line Width customization
  • Cross-platform GUI (SFML/Qt/wxWidgets)
  • Export to image files (PNG/SVG)

About

A C++ interpreter for a domain-specific function plotting language.

Resources

License

Stars

Watchers

Forks

Contributors