「烂活」一分钟打造一款自研编程语言!

先来极速版操作流程:

操作流程

克隆CPython储存库

git clone https://github.com/python/cpython --depth=1

执行此脚本(更改替换的名称)

import os
import shutil

def replace_in_file(file_path, rules):
    """读取文件内容并替换字符串,然后重新写入文件"""
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            content = file.read()

        modified = False
        for old, new in rules.items():
            if old in content:
                content = content.replace(old, new)
                modified = True

        if modified:
            with open(file_path, 'w', encoding='utf-8') as file:
                file.write(content)
            print(f"Updated content in {file_path}")
    except UnicodeDecodeError as e:
        print(f"Error reading {file_path}: {e}")
    except Exception as e:
        print(f"Error processing {file_path}: {e}")

def replace_in_path(path, rules):
    """替换文件夹路径和文件名中的字符串"""
    for root, dirs, files in os.walk(path, topdown=False):
        # 替换文件名
        for name in files:
            new_name = name
            for old, new in rules.items():
                new_name = new_name.replace(old, new)
            if new_name != name:
                original_path = os.path.join(root, name)
                new_path = os.path.join(root, new_name)
                shutil.move(original_path, new_path)
                print(f"Renamed {original_path} to {new_path}")

        # 替换目录名
        for name in dirs:
            new_name = name
            for old, new in rules.items():
                new_name = new_name.replace(old, new)
            if new_name != name:
                original_path = os.path.join(root, name)
                new_path = os.path.join(root, new_name)
                shutil.move(original_path, new_path)
                print(f"Renamed directory {original_path} to {new_path}")


def walk_and_replace(root_path, rules):
    """遍历目录并应用替换规则到文件名、文件夹名和文件内容"""
    for root, dirs, files in os.walk(root_path):
        # 替换文件内容
        for file in files:
            file_path = os.path.join(root, file)
            replace_in_file(file_path, rules)

        # 替换文件名和目录名
        replace_in_path(root, rules)

# 替换规则:键是原始字符串,值是替换后的字符串
replacement_rules = {
    'Python': 'Genshin',
    'python': 'genshin',
    'PYTHON': 'GENSHIN'
}

# 指定要处理的根目录
root_directory = './cpython'

# 执行替换操作
walk_and_replace(root_directory, replacement_rules)

然后 编译cpython即可
Tips:这样操作后会导致make install无法正常安装,禁用编译pip即可解决
配置时使用

./configure --without-ensurepip

编译cpython时突发奇想,通过关键词替换的方式能 创造 一门全新的编程语言吗?于是,带着疑问,让GPT帮我写下了这个关键词替换脚本,发现关键词替换后的源码竟然神奇的能够通过编译,遂跟佬友们分享经历

13 个赞

感谢分享这个

这个我之前我也看过,不过开发一款全新的语言是很难的,不然国内早就开发出中文式的编程语言了,就是英文类的编程语言占据了绝对优势,再开发一款全新语言简直是吃力不讨好

1 个赞

佬友把自研给研究明白了

2 个赞

替换一下就是自研了?

不明觉厉呼儿嗨吆

你在说 木兰 ?

又让我想起那个东北话编程了 挪挪哈哈哈

啊这,还可以这样吗

印象中有中文编程语言!

易语言?
当年好像就是类似这样照抄c语言。

易语言大概吧

你在暗示哄梦吗 tieba_028


还不少 AIGC的答案

最自研的一集 :rofl: :point_right:

还是要学习c++最优秀

想起了那个梗,宏盟,中文语言进程显示宏盟,系统语言切换到英文,显示android

2 个赞

这语言都让我取名了那能不是自研吗:thinking:

是这样的哈哈哈哈哈哈

这理由完全没毛病……