Skip to content

Repository files navigation

Pangea Java Interpreter

A robust Java implementation of the Pangea programming language interpreter featuring an innovative annotation-driven architecture.

🚀 Current Status: v1.0.0-PREVIEW.1

  • Core interpreter functionality: Working and stable
  • Reflection-based architecture: 80% robustness score
  • Comprehensive error handling: Input validation and bounds checking
  • Maven build system: JDK 17 ready
  • Clean codebase: Well-organized and documented

🎯 Quick Start

Prerequisites

  • Java 17 or higher
  • Maven 3.6+

Build and Run

# Clone and build
git clone <repository-url>
cd jova
mvn compile

# Run examples
java -cp target/classes pangea.PangeaMain examples/hello.pangea
java -cp target/classes pangea.PangeaMain examples/factorial_simple.pangea

# Or use Maven
mvn exec:java -Dexec.args="examples/hello.pangea"

🏗️ Architecture

Traditional Approach vs. Annotation-Driven

// Traditional manual registration (old way)
namespace.put("add", new FunctionEntry(2, "infix", this::addFunction));
namespace.put("print", new FunctionEntry(1, "prefix", this::printFunction));
// ... dozens more lines

// New annotation-driven approach
@PangeaFunction(names = {"add", "+"}, arity = 2, type = "infix")
private Value addFunc(List<Integer> params, Interpreter interp) {
    // Implementation
}

@PangeaFunction(names = {"print"}, arity = 1)
private Value printFunc(List<Integer> params, Interpreter interp) {
    // Implementation
}

Key Benefits

  • 🎯 90% less registration code - Functions auto-register via annotations
  • 🔍 Better discoverability - Function metadata co-located with implementation
  • 🛡️ Type safety - Compile-time validation of function signatures
  • 📚 Self-documenting - Annotation parameters describe function behavior

📖 Language Overview

Pangea is a stack-based functional programming language with unique syntax:

  • Function definitions with explicit arity declarations
  • Infix, prefix, and postfix operators
  • Block-based control structures
  • Arrays and objects
  • Recursive functions
  • Iterator constructs

Features

Core Language Features

  • Dynamic typing: Supports numbers, strings, booleans, arrays, and objects
  • Function definitions: def function_name#arity ( body )
  • Conditional logic: if condition then-branch else-branch
  • Loops: times count ( block ), each iterable ( block )
  • Operators: Arithmetic, comparison, and logical operators
  • Comments: Lines starting with # are comments

Syntax Rules

  • All code must be wrapped in parentheses: ( ... )
  • Strings use + for spaces: "Hello+World!" becomes "Hello World!"
  • Function arity is explicit: def factorial#1 means 1 parameter
  • Arguments are 1-based: arg 1, arg 2, etc.
  • No commas or semicolons: Space-separated tokens
  • Postfix operators: 5 squared → 25
  • Infix operators: 3 + 4, 5 > 2

Built-in Functions

  • print value - Output values
  • arg index - Access function arguments (1-based indexing)
  • times count block - Execute block count times
  • each iterable block - Iterate over arrays/objects
  • when condition value - Conditional execution
  • unless condition block - Negative conditional
  • times_count depth - Get current iteration count
  • each_item - Get current item in each loop
  • each_key - Get current key in each loop

Operators

  • Arithmetic: +, -, *, %, ** (exponentiation)
  • Comparison: ==, <, >, <=
  • Special: squared (postfix), when (infix)
  • Logical: Truthy/falsy evaluation

Building and Running

Prerequisites

  • Java 11 or later

Quick Start

Use the provided build and run scripts:

# Build the project
./build.sh

# Run the main demo
./run.sh

# Run tests
./run.sh test

# Run a specific Pangea program
./run.sh examples/factorial.pangea

Using Maven (optional)

If you have Maven installed, you can also use standard Maven commands:

# Compile the project
mvn compile

# Run the main program
mvn exec:java

# Run with arguments (example file)
mvn exec:java -Dexec.args="examples/factorial.pangea"

# Run tests
mvn test

# Create a JAR file
mvn package

Manual Compilation (if needed)

# Compile all sources
javac -d target/classes src/main/java/pangea/*.java

# Compile tests
javac -cp target/classes -d target/classes src/test/java/pangea/*.java

# Run main program
java -cp target/classes pangea.PangeaMain

# Run tests
java -cp target/classes pangea.SimpleTest

Project Structure

jova/
├── src/
│   ├── main/java/pangea/     # Core interpreter source files
│   │   ├── Value.java        # Value representation and operations
│   │   ├── Interpreter.java  # Main interpreter logic
│   │   ├── FunctionEntry.java # Function definitions
│   │   ├── NativeFunction.java # Built-in functions
│   │   ├── StackFrame.java   # Function call frames
│   │   ├── IterationFrame.java # Loop iteration context
│   │   └── PangeaMain.java   # Main demo program
│   └── test/java/pangea/     # Test files
│       └── SimpleTest.java   # Basic functionality tests
├── examples/                 # Example Pangea programs
│   ├── factorial.pangea
│   ├── fizzbuzz.pangea
│   └── arrays_objects.pangea
├── target/classes/           # Compiled class files
├── build.sh                  # Build script
├── run.sh                    # Run script
├── pom.xml                   # Maven configuration
├── README.md
├── MIGRATION_SUMMARY.md
└── .gitignore

Example Programs

Simple Hello World

( print "Hello+from+file!" )

Factorial Function

( def factorial#1
  if ( arg 1 ) == 0
  1
  ( arg 1 ) * factorial ( ( arg 1 ) - 1 )
  print "Computing+factorial+of+5:"
  print factorial 5 )

FizzBuzz

( def fizzbuzz#1
  ( arg 1 ) times (
    print if ( times_count 1 ) % 15 == 0 "FizzBuzz"
          if ( times_count 1 ) % 3 == 0 "Fizz"
          if ( times_count 1 ) % 5 == 0 "Buzz"
          times_count 1 )
  print "FizzBuzz+from+1+to+20:"
  fizzbuzz 20 )

Basic Operations

( def greet#1
  print "Hello+" + arg 1 + "!"
  greet "World"
  print "Basic+arithmetic:"
  print 5 + 3
  print 10 * 2
  print "Array+example:"
  print [ 1 2 3 4 5 ] )

Arrays and Objects

( print "Array+iteration:"
  [ "apple" "banana" "cherry" ] each (
    print each_item
  )

  print "Object+example:"
  print { "name" "Alice" "age" 30 "city" "New+York" }

  print "Times+loop:"
  5 times (
    print times_count 1
  ) )

Available Example Files

The examples/ directory contains several complete Pangea programs:

  • hello.pangea - Simple hello world
  • basics.pangea - Basic operations and function definition
  • factorial_simple.pangea - Recursive factorial function
  • fizzbuzz.pangea - Classic FizzBuzz implementation
  • arrays_objects.pangea - Array and object manipulation
  • advanced.pangea - Complex functions and expressions

Run any example with:

./run.sh examples/filename.pangea
[ "apple" "banana" "cherry" ] each (
    print each_item
)

Architecture

The Java implementation consists of:

Core Classes

  • Value: Represents all Pangea data types using enums and type-safe accessors
  • Interpreter: Main interpreter class with execution engine
  • FunctionEntry: Function metadata and implementation storage
  • StackFrame: Function call stack management
  • IterationFrame: Iteration context management

Key Components

  • Parser: Converts source code into tokenized words
  • Executor: Interprets and executes tokenized programs
  • Namespace: Function and operator registry using HashMap
  • Stack Management: Call stack for function parameters and iteration contexts

Memory Management

  • Uses Java's automatic garbage collection
  • ArrayList and HashMap for dynamic collections
  • Stack-based execution model with proper cleanup

Differences from JavaScript Version

  1. Type Safety: Strong typing with compile-time checks
  2. Memory Management: Automatic garbage collection vs. manual management
  3. Performance: JVM optimization vs. interpreted execution
  4. Error Handling: Exception-based error handling
  5. Collections: Java Collections Framework vs. native JS arrays/objects

Language Syntax

Function Definition

def function_name#arity ( body )

Function Call

function_name arg1 arg2 ... argN

Blocks

( expression1 expression2 ... )  # Parentheses block
[ item1 item2 ... ]              # Array literal
{ "key1" value1 "key2" value2 }  # Object literal

String Literals

Spaces in strings are represented with +:

"hello+world"  # Represents "hello world"

Special sequence (+) represents literal +:

"1(+)2=3"      # Represents "1+2=3"

Parsing and Tokenization

The Pangea interpreter features intelligent parsing that handles the dual use of the # symbol:

Hash Symbol (#) Usage

  1. Function Arity Declaration: functionName#arity

    • factorial#1 - function named "factorial" with 1 parameter
    • greet#2 - function named "greet" with 2 parameters
    • The # is part of the function identifier, NOT a comment
  2. Comment Marker: # comment text

    • # This is a comment - entire line is a comment
    • print 42 # inline comment - everything after # is a comment

Parsing Rules

Critical Rule: If # is preceded by non-whitespace, it's part of a function name (NOT a comment).

# Examples of # usage:

def factorial#1      # ✓ factorial#1 = function name, # comment = comment
  print arg 1        # ✓ Only this part is a comment

func#3 arg 1 arg 2   # ✓ func#3 = function name, rest = comment

Tokenization Guarantee

The interpreter displays a words array showing all parsed tokens. This array contains ONLY meaningful code tokens - no whitespace characters (spaces, tabs, newlines) are included.

Example:

# Input file with comments and formatting:
# Factorial function example
(
  def factorial#1    # Define function
    if ( arg 1 ) == 0
      1              # Base case
)

# Output tokens (words array):
[(, def, factorial#1, if, (, arg, 1, ), ==, 0, 1, )]

Key Points:

  • Comments are completely stripped
  • Indentation and formatting removed
  • Function names like factorial#1 preserved as single tokens
  • Only meaningful code elements remain

About

Java-based Pangea system . Pangea is a Language . Panji is a Java-based implementation .

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages