nxlang

module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2026 License: MIT

README ΒΆ

Nxlang (Efficiency Language)

A lightweight, Go-based scripting language with Go-like syntax, bytecode virtual machine, and cross-platform support.

Go Report Card Go Version

δΈ­ζ–‡η‰ˆζœ¬

✨ Core Features

  • Familiar Syntax: Go-like syntax design, low learning curve, seamless adaptation for Go developers
  • Weak Type System: Automatic type conversion, flexible and efficient coding, reduces redundant type declarations
  • Bytecode Execution: Compiles to platform-agnostic bytecode (.nxb), runs faster than traditional interpreted languages
  • Built-in REPL: Interactive command line with syntax highlighting for quick debugging and prototyping
  • Integrated Editor: Built-in code editor, write and run scripts without additional tools
  • Rich Standard Library: Built-in support for concurrency, HTTP, file I/O, data processing, graphics, and more
  • Cross-Platform Support: Runs perfectly on Windows, Linux, macOS with no external dependencies
  • Module System: Supports module import/export with consistent function references, suitable for large project development
  • Native UTF-8: Full stack UTF-8 support, strings and files use UTF-8 encoding by default
  • High Performance: Built on Go, runtime performance far exceeds dynamic languages like Python/JavaScript

πŸš€ Quick Start

Installation

Download prebuilt binaries from Releases or build from source.

Run REPL (Interactive Mode)
nx
Execute a Script
nx path/to/script.nx
Compile to Bytecode
nx compile path/to/script.nx -o output.nxb
Run Precompiled Bytecode
nx run output.nxb

πŸ“ Example Code

// Hello World
pln("Hello, Nxlang! πŸ‘‹")

// Function definition
func factorial(n) {
    if n <= 1 { return 1 }
    return n * factorial(n - 1)
}

pln("Factorial of 10:", factorial(10))

// Classes and inheritance
class Animal {
    func init(name) {
        this.name = name
    }
    func speak() {
        return "..."
    }
}

class Dog < Animal {
    func speak() {
        return this.name + " says woof!"
    }
}

let dog = Dog("Rex")
pln(dog.speak())  // "Rex says woof!"

// Anonymous functions
let add = func(a, b) { return a + b }
pln("add(3, 4) =", add(3, 4))

// HTTP request
let response = httpGet("https://httpbin.org/get")
pln(response)

// for-in loop (now returns values!)
let nums = [1, 2, 3, 4, 5]
for v in nums {
    pln("value:", v)
}

πŸ“š Example Scripts

The scripts/ directory contains 30 example scripts covering common programming scenarios:

Script Description
01_hello.nx Hello World
02_variables.nx Variables, constants, arithmetic
03_arrays_maps.nx Arrays, maps, iteration
04_functions.nx Functions, recursion, default parameters
05_control_flow.nx if/else, for, while, switch
06_classes.nx Classes, inheritance
07_strings.nx String operations
08_json.nx JSON serialization
09_files.nx File I/O
10_time.nx Time/date functions
11_regex_encoding.nx Regex, base64, MD5, SHA
12_math.nx Math functions
13_threads.nx Concurrency
15_types.nx Type conversion
16_algorithms.nx Sorting, search
17_closures.nx Closure patterns
18_maps.nx Map operations
19_functional.nx Array operations
20_oop_patterns.nx OOP design patterns
21_http.nx HTTP client
22_args.nx Command line arguments
23_csv.nx CSV parsing
24_random.nx Random numbers
25_date_utils.nx Date formatting
26_url_utils.nx URL parsing
27_validation.nx Email/URL validation
28_table.nx Pretty table printing
29_stack.nx Stack data structure
30_queue.nx Queue data structure

Run any script:

./nx scripts/06_classes.nx

πŸ“¦ Standard Library

πŸ“¦ Standard Library

Math Functions

abs, sqrt, sin, cos, tan, floor, ceil, round, pow, random

String Functions

len, toUpper, toLower, trim, split, join, contains, replace, substr, startsWith, endsWith

Collection Functions

array, append, map, orderedMap, stack, queue, seq, keys, values, delete, sortMap, reverseMap

Type Conversion Functions

int, float, bool, string, byte, uint, char, bytes, chars

  • Static methods: int.parse(), float.parse(), string.parse(), bool.parse()
Object Reference Functions

ref(), deref(), setref() - Object reference and dereference support

Seq Type (Self-growing Sequence)

seq() - Create self-growing sequence with auto-expanding index access

  • Methods: Append(), Pop(), Clear(), Resize(), Fill(), Range(), Reverse(), Join(), Includes(), IndexOf(), Get(), Set()
Time Functions

now(), unix(), unixMilli(), formatTime(timestamp, format), parseTime(timeStr, format), sleep(seconds)

JSON Functions

toJson(value, indent=false) - Convert value to JSON string

Concurrency Functions

thread(func) - Spawn a new thread running the given function mutex() - Create a mutual exclusion lock rwMutex() - Create a read-write mutex waitForThreads - Wait for all threads to complete

I/O Functions

pln(...) - Print values with newline pr(...) - Print values without newline printf(format, ...) - Print formatted string readFile(filename), writeFile(filename, content) - File I/O

HTTP Functions

httpGet(url), httpPost(url, body), httpPostJSON(url, data), httpPut(url, body), httpDelete(url), httpRequest(method, url, body, headers)

  • Returns httpResponse object with fields: statusCode, body, headers
Graphics/Canvas Functions

canvas(width, height), clear(canvas, r, g, b, a), drawPoint(canvas, x, y, r, g, b, a) drawLine(canvas, x0, y0, x1, y1, r, g, b, a), drawCircle(canvas, cx, cy, radius, r, g, b, a) fillCircle(canvas, cx, cy, radius, r, g, b, a), drawRectangle(canvas, x, y, w, h, r, g, b, a) fillRectangle(canvas, x, y, w, h, r, g, b, a), getPixel(canvas, x, y) savePNG(canvas, filename), loadPNG(filename)

Data/CSV Functions

readCSV(filename), writeCSV(filename, data, headers), parseCSV(csvString), toCSV(data) openCSV(filename, hasHeader), readCSVRow(reader), readCSVAll(reader), closeCSV(reader) createCSVWriter(filename), writeCSVRow(writer, row), closeCSV(writer)

Plugin System

loadPlugin(path), unloadPlugin(name), callPlugin(pluginName, funcName, args...), listPlugins()

  • Load Go-based plugins and call plugin functions from Nxlang code

πŸ—οΈ Build from Source

# Clone repository
git clone https://github.com/topxeq/nxlang.git
cd nxlang

# Build binary
go build -o nx ./cmd/nx

# Install to system (Linux/macOS)
sudo mv nx /usr/local/bin/

πŸ§ͺ Running Tests

# Run all tests
go test ./...

# Run specific package tests
go test ./vm -v

πŸ›οΈ Architecture

Nxlang follows a standard compiler-VM architecture:

  1. Lexer: Converts source code to token stream
  2. Parser: Builds Abstract Syntax Tree (AST) from tokens
  3. Compiler: Transforms AST into platform-agnostic bytecode (.nxb)
  4. Virtual Machine: Executes bytecode with stack-based evaluation
  5. Standard Library: Built-in functions and types implemented in Go
  6. Plugin System: Go-based plugins for extensibility
Project Structure
nxlang/
β”œβ”€β”€ cmd/nx/          # CLI entry point
β”œβ”€β”€ compiler/        # Bytecode compiler
β”œβ”€β”€ parser/          # Source code parser
β”œβ”€β”€ vm/              # Virtual machine
β”œβ”€β”€ types/           # Type system (Object interface)
β”œβ”€β”€ plugin/          # Plugin system
β”œβ”€β”€ graphics/        # Graphics/canvas support
β”œβ”€β”€ data/            # Data processing (CSV, etc.)
β”œβ”€β”€ stdlib/          # Standard library
β”œβ”€β”€ scripts/         # Example scripts (30 common scenarios)
β”œβ”€β”€ tests/           # Test suites
β”‚   └── comprehensive_test.nx  # Comprehensive test
β”œβ”€β”€ examples/        # Example scripts
└── docs/           # Documentation

πŸ“„ License

MIT License - see LICENSE for details.


Made with ❀️ by TopXeQ

Directories ΒΆ

Path Synopsis
cmd
nx command
Package data provides data processing capabilities for Nxlang including CSV and Excel file handling
Package data provides data processing capabilities for Nxlang including CSV and Excel file handling
Package graphics provides image processing and graphics capabilities for Nxlang
Package graphics provides image processing and graphics capabilities for Nxlang
Package plugin provides plugin system for Nxlang Plugins are Go-based extensions that can be loaded into the Nxlang VM
Package plugin provides plugin system for Nxlang Plugins are Go-based extensions that can be loaded into the Nxlang VM
pluginhelper
Package pluginhelper provides helper functions for creating Nxlang plugins Plugin developers can use this package to simplify plugin development
Package pluginhelper provides helper functions for creating Nxlang plugins Plugin developers can use this package to simplify plugin development
plugins
example_plugin command
Example plugin for Nxlang Build this plugin with: go build -buildmode=plugin -o example.nxp plugins/example_plugin/main.go
Example plugin for Nxlang Build this plugin with: go build -buildmode=plugin -o example.nxp plugins/example_plugin/main.go
Package types defines the core type system and Object interface for Nxlang
Package types defines the core type system and Object interface for Nxlang

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL