XGo Programming Language

repository·main·Indexed 27 days ago

https://github.com/goplus/xgo

A programming language built on a Go foundation designed to bridge the gap between engineering and natural language. XGo provides a unified ecosystem to integrate C/C++, Python, and JavaScript/TypeScript, allowing developers to mix standard Go (.go) and XGo (.xgo) source files in the same package. It includes DQL (DOM Query Language) for querying structured data like JSON, HTML, and ASTs, and supports specialized classfiles such as yap for HTTP web frameworks, spx for 2D game engines, and gsh for DevOps shell scripting.

Tokens
49.4K
Snippets
164
Records
291
Agent score
91%

What's inside XGo

  1. Overview of DQL (DOM Query Language)

    main
    DQL is a universal, expressive query language for structured and tree-shaped data in XGo. It provides a unified interface for querying JSON, YAML, HTML, XML, ASTs, file systems, and any custom tree structure. Queries are performed using a NodeSet, which is lazily evaluated to ensure low memory usage and efficient composition.
  2. Understand the Unified Function Model in XGo

    main

    In XGo, commands, function calls, and operators are all conceptually function invocations. You can express intent using three different syntactic styles:

    1. Command Style: Natural language syntax where parentheses are optional. Best for simple, top-level statements.
    2. Function Call Style: Traditional syntax with mandatory parentheses. Best for complex nesting and composition.
    3. Operator Style: Mathematical notation (e.g., +, *, ==) which are actually function calls in disguise.

    Example of the three styles:

    echo "Hello"           // Command style
    echo("Hello")          // Function call style
    3 + 4                  // Operator style
  3. Understand XGo Slices (Lists)

    main

    A slice (also referred to as a list) is a dynamically-sized, flexible view into the elements of an array. Slices are composed of three parts:

    1. Pointer: Points to the first element in the underlying array.
    2. Length: The current number of elements in the slice.
    3. Capacity: The number of elements from the start of the slice to the end of the underlying array.

    Unlike arrays, slices can grow and shrink dynamically.

  4. Understand XGo Code Style Specifications

    main

    XGo applies a specific code style transformation to Go code. The formatter converts standard Go syntax into a specialized XGo syntax characterized by the removal of boilerplate, the use of built-in primitives instead of the fmt package, and a command-style execution pattern.

    Key transformations include:

    • Boilerplate Removal: package main and func main are stripped.
    • Built-in Primitives: fmt package calls are replaced with built-in functions like echo, print, printf, and errorf.
    • Command Style: Outermost function calls are converted to a command-style syntax (e.g., `echo
  5. Understand the role of gox.mod in XGo

    main

    gox.mod is a module configuration file used exclusively by class framework packages to define how their class system is structured. It is not used in ordinary XGo application projects.

    Key functions of gox.mod:

    • Maps file extension patterns to specific class types (e.g., main.spx $\rightarrow$ Game).
    • Defines which packages should be automatically imported into every source file.
    • Tells the XGo toolchain how to parse source files via xgo/parser.

    Note: For backward compatibility, the legacy name gop.mod and the gop directive are still supported.

  6. XGo Programming Language Overview

    main

    XGo is a programming language designed for engineering, STEM education, and data science. It is built on a Go foundation but features a syntax that approaches natural language expression. Key capabilities include:

    • Unified Ecosystem: Integrates assets from C/C++, Go, Python, and JavaScript/TypeScript.
    • Hybrid Programming: Fully compatible with Go; you can mix Go and XGo code within the same package.
    • Simplified Syntax: Uses a command-style syntax (e.g., echo "text" instead of fmt.Println("text")) and reduces boilerplate like package main and func main.
    • SDF (Specific Domain Friendliness): Supports domain-specific friendliness through XGo Classfiles and Domain Text Literals.
  7. Understand the design philosophy of XGo vs. Go

    main

    XGo and Go serve different primary purposes and target different user groups:

    XGo: Low-Code Engineering Fusion

    • Goal: To achieve a natural fusion of engineering and low-code, aiming to be a "better Python."
    • Target Audience: Non-professionals, STEM students, and data scientists.
    • Core Philosophy: "Enable everyone to become a builder of the world."
    • Key Characteristics:
      • Specific Domain Friendliness (SDF): Instead of creating Domain Specific Languages (DSLs), XGo abstracts domain knowledge to provide domain-friendly support.
      • Progressive Complexity: Allows users to start with simple scripts and gradually scale to large projects using a smaller syntax set than Python.
      • Multi-Language Ecosystem Fusion: Designed to integrate multiple paradigms using the formula XGo := C * Go * Python * JavaScript + Scratch.

    Go: Systems Programming Engineering

    • Goal: To provide a way to easily build simple, reliable, and efficient software, aiming to be a "better C."
    • Target Audience: Systems engineers and professional developers.
    • Core Philosophy: Focus on engineering practices for systems programming, emphasizing performance, concurrency, and maintainability.
    • Key Characteristics:
      • General-Purpose: Provides abstraction through interfaces and composition without specific optimization for particular application domains.
      • Consistent Complexity: Designed for systems programming with a relatively flat learning curve but a higher starting point than XGo.
      • Go Ecosystem Focus: Concentrates on building and maintaining its own ecosystem.
  8. Implement Common Slice Patterns

    main

    XGo supports several common patterns for manipulating slices using traditional loops and the <- operator:

    • Filtering: Iterate and use if to append to a new slice.
    • Mapping: Iterate and transform elements into a new slice.
    • Finding: Iterate with i, v in nums to find a target value and its index.
    • Reversing: Iterate backwards from len(nums) - 1 to 0.
    • Removing Duplicates: Use a map (seen := {}) to track encountered values.
    • Merging: Use the spread operator ... to append multiple slices: merged <- a....
    • Stacks/Queues: Use <- for push/enqueue and slicing/indexing for pop/dequeue.
    • Sliding Window: Use range slicing nums[i:i + windowSize] within a loop.
    // Merging multiple slices
    a := [1, 2, 3]
    b := [4, 5, 6]
    merged := []
    merged <- a...
    merged <- b...
    
    // Using a slice as a stack (Push/Pop)
    stack := []
    stack <- 1 // Push
    if len(stack) > 0 {
        top := stack[len(stack) - 1]
        stack = stack[:len(stack) - 1] // Pop
    }
    
    // Sliding Window
    windowSize := 3
    for i := 0; i <= len(nums) - windowSize; i++ {
        window := nums[i:i + windowSize]
    }
  9. Follow XGo MiniSpec recommendation for data organization

    main

    The XGo MiniSpec recommends a streamlined approach to data and behavior by avoiding struct in favor of a combination of tuple and classfile:

    1. Use Tuples: For lightweight data containers and function return values.
    2. Use Classfiles: For complex types requiring encapsulation, methods, and object-oriented features.

    This approach aims to reduce conceptual overhead by providing a cleaner model than the overlapping constructs found in standard Go.

  10. Call Functions using Command-style Syntax

    main

    XGo supports two styles of function and method calls. While traditional parentheses are supported, XGo recommends command-style syntax for a cleaner, shell-like appearance.

    Function-call style (Traditional)

    echo("Hello world")
    fmt.Println("Hello, world")

    Omit parentheses and use a space between the identifier and arguments:

    echo "Hello world"
    fmt.Println "Hello, world"
    os.Exit 1

    Variadic Arguments

    Use the ... operator to pass variadic arguments:

    echo elements...
    echo "Hello world"
    fmt.Println "Hello, world"
    os.Exit 1