We are currently working on new rules for what content should and shouldn't be allowed on this website, and are looking for feedback! See Esolang:2026 topicality proposal to view and give feedback on the current draft.

Chinese (PSTF)

From Esolang
Jump to navigation Jump to search

Chinese is a variant of English. At least a billion people know this programming language without knowing any other programming languages.

You might saw some detail of this programming language at !中文.

Example Programs

Cat Program

这个程序原样输出用户输入的东西。如果用户没有任何输入,则停止程序。

Quine

这个程序输出它的源代码。

Self-interpreter

这个程序接受一段程序的描述然后运行对应的程序。

Turing-Completeness Proof

这个程序接受一串字符,然后将其解释为Brainfuck。

Since brainfuck is Turing-complete, this programming language is also Turing-complete.

Computational Class

这个程序将解决停机问题。

Obviously uncomputable.

Errors

听不懂

When the language you provide isn’t Chinese, this error will be thrown. For example,

ይህ ፕሮግራም ቁጥር 4 ን ያወጣል.

But the language accepts the use of dialects, for example:

个斑马的

Even though the program does nothing and it's in the Hubei dialect, it's still a valid program.

语病

This error will be thrown when the program doesn’t follow Chinese grammar. For example,

通过这个程序使停机问题被解决。

Here, the simultaneous use of 通过 and 使 causes the sentence to lack a subject, so this sentence clearly has a grammatical problem.

Compilers

Like English, the compilers of this language are mainly humans, especially Chinese people.

Machine Learning

With the development of AI technology, we can now have machines recognize what we say. The following programs were all generated by the DeepSeek R1 671b web version.

Examples

Quine (Python)

s = 's = %r; print(s %% s)'
print(s % s)

Open or Create (Bash)

#!/bin/bash
filename="$1"
if [ -f "$filename" ]; then
    # file exists – open it (e.g., with less, cat, or your editor)
    cat "$filename"
else
    # file doesn't exist – create it
    touch "$filename"
    echo "File created: $filename"
fi

Self-interpreter through input (Node.js)

#!/usr/bin/env node
const fs = require('fs');

let input = fs.readFileSync(0, 'utf-8'); // Read from stdin

if (!input.trim()) {
    console.error("Error: No input received to execute.");
    process.exit(1);
}

try {
    // 'eval' evaluates expressions, but for statements we can use 'vm' module or Function constructor
    // Using the Function constructor is safer than eval for strict mode, but still dangerous.
    const result = new Function(input)();
    if (result !== undefined) console.log(result);
} catch (e) {
    console.error(`Execution error: ${e.message}`);
    process.exit(1);
}

Nope. interpreter (C/C++)

#include <stdio.h>

int main() {
    int c = getchar();  // Attempt to read the very first byte

    if (c == EOF) {
        // No input at all – halt silently
        return 0;
    }

    // We received at least one byte
    printf("Nope.\n");
    return 0;
}

Python But Bad interpreter (Bash and C/C++)

#!/bin/bash
# Usage: 
#   ./runner.sh source.py   -> runs the file, deletes it if exception occurs
#   cat source.py | ./runner.sh  -> runs from stdin (saved to temp file)

set -e  # Exit on error, but we handle errors manually

SOURCE_FILE=""
IS_TEMP=false

# Determine source
if [ $# -ge 1 ]; then
    # Source is a file provided as argument
    SOURCE_FILE="$1"
    if [ ! -f "$SOURCE_FILE" ]; then
        echo "Error: File '$SOURCE_FILE' not found." >&2
        exit 1
    fi
else
    # Source comes from STDIN
    SOURCE_FILE=$(mktemp)
    cat > "$SOURCE_FILE"
    IS_TEMP=true
fi

# Execute the source code as Python
# We run it in a subshell and capture the exit code
python3 "$SOURCE_FILE"
EXIT_CODE=$?

# If the Python script raised an exception (non-zero exit), delete the source
if [ $EXIT_CODE -ne 0 ]; then
    # "Directly delete the code" – if it was a user file, this deletes it permanently.
    # If it was a temp file from stdin, we delete the temp.
    rm -f "$SOURCE_FILE"
    # Note: We do NOT exit here. We let the script finish with the python exit code.
fi

# Clean up temp file if it came from stdin (even on success)
if [ "$IS_TEMP" = true ]; then
    rm -f "$SOURCE_FILE"
fi

exit $EXIT_CODE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main(int argc, char *argv[]) {
    char *filename = NULL;
    int is_temp = 0;
    pid_t pid;
    int status;

    if (argc >= 2) {
        filename = argv[1];
    } else {
        // Read from stdin into a temp file
        filename = "/tmp/code_XXXXXX";
        int fd = mkstemp(filename);
        if (fd == -1) {
            perror("mkstemp");
            return 1;
        }
        char buffer[1024];
        ssize_t n;
        while ((n = read(0, buffer, sizeof(buffer))) > 0) {
            write(fd, buffer, n);
        }
        close(fd);
        is_temp = 1;
    }

    // Fork and execute Python
    pid = fork();
    if (pid == 0) {
        // Child: execute python3
        execlp("python3", "python3", filename, NULL);
        perror("execlp");
        exit(1);
    } else if (pid > 0) {
        // Parent: wait for child
        waitpid(pid, &status, 0);
        if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
            // Exception occurred (non-zero exit) -> delete the code
            remove(filename);
        }
        if (is_temp) {
            remove(filename); // Cleanup temp file
        }
        return WEXITSTATUS(status);
    } else {
        perror("fork");
        return 1;
    }
}
Same feature but in Windows and also a WinBAT version
// runner.c - Windows NT / Windows 10+ compatible
// Compile with: cl runner.c  (MSVC)  OR  gcc runner.c -o runner.exe (MinGW)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>       // for _access
#include <process.h>  // for _spawnvp
#include <fcntl.h>    // for O_BINARY (optional, for strict binary reads)

int main(int argc, char *argv[]) {
    char *filename = NULL;
    int is_temp = 0;
    int ret = 0;

    if (argc >= 2) {
        // Source is provided as a file argument
        filename = argv[1];
        // Check if the file actually exists
        if (_access(filename, 0) == -1) {
            fprintf(stderr, "Error: File '%s' not found.\n", filename);
            return 1;
        }
    } else {
        // Source comes from STDIN. Create a temporary file.
        char temp_path[256];
        if (getenv("TEMP") != NULL) {
            snprintf(temp_path, sizeof(temp_path), "%s\\pycode_%d.py", getenv("TEMP"), rand());
        } else {
            // Fallback to current directory if TEMP isn't set
            snprintf(temp_path, sizeof(temp_path), "pycode_%d.py", rand());
        }
        filename = temp_path;
        is_temp = 1;

        // Read STDIN byte-by-byte and write to the temp file
        FILE *f = fopen(filename, "w");
        if (!f) {
            perror("Failed to create temp file");
            return 1;
        }

        int c;
        while ((c = getchar()) != EOF) {
            fputc(c, f);
        }
        fclose(f);
    }

    // Execute the Python script using _spawnvp
    // _P_WAIT makes the parent process wait for the child to finish.
    const char *args[] = { "python", filename, NULL };
    ret = (int)_spawnvp(_P_WAIT, "python", args);

    // If spawn fails (e.g., python not in PATH), ret is -1
    if (ret == -1) {
        perror("Failed to spawn Python");
        // If it's a temp file, clean it up
        if (is_temp) remove(filename);
        return 1;
    }

    // If the Python script threw an exception, it returns a non-zero exit code.
    if (ret != 0) {
        // Directly delete the source code because an exception occurred.
        remove(filename);
    }

    // If the source came from STDIN (temp file), delete it now regardless of success 
    // to avoid leaving garbage in %TEMP%.
    if (is_temp) {
        remove(filename);
    }

    return ret;
}
@echo off
setlocal enabledelayedexpansion

set SCRIPT_FILE=%1
set IS_TEMP=0

REM ------------------------------------------------------------
REM If no file argument is provided, read from STDIN into a temp file
REM ------------------------------------------------------------
if "%SCRIPT_FILE%"=="" (
    set IS_TEMP=1
    set SCRIPT_FILE=%TEMP%\pycode_%RANDOM%.py
    REM Read all lines from STDIN (piped or typed) and write to the temp file
    findstr /r ".*" > "%SCRIPT_FILE%"
)

REM ------------------------------------------------------------
REM Check if the file exists (if it was an argument)
REM ------------------------------------------------------------
if not "%1"=="" (
    if not exist "%SCRIPT_FILE%" (
        echo Error: File "%SCRIPT_FILE%" not found.
        exit /b 1
    )
)

REM ------------------------------------------------------------
REM Execute the Python script
REM ------------------------------------------------------------
python "%SCRIPT_FILE%"

REM Capture the exit code (0 = success, non-zero = exception)
set EXIT_CODE=%ERRORLEVEL%

REM ------------------------------------------------------------
REM If an exception occurred (non-zero), delete the source code.
REM ------------------------------------------------------------
if %EXIT_CODE% NEQ 0 (
    echo Exception detected. Deleting source code...
    del /f "%SCRIPT_FILE%" 2>nul
)

REM ------------------------------------------------------------
REM If it was a temp file from STDIN, delete it cleanly 
REM (even if it succeeded, to avoid clutter).
REM ------------------------------------------------------------
if %IS_TEMP%==1 (
    if exist "%SCRIPT_FILE%" (
        del /f "%SCRIPT_FILE%" 2>nul
    )
)

exit /b %EXIT_CODE%

See Also

English

Categories