粤语编程语言.The Cantonese programming language.

Overview

粤语编程语言

引言
咋咋淋入门
  Hello World
  赋值语句
  睇下变量嘅类型
  循环
  条件语句
  函数
  抛出异常
  断言语句
  错误捕捉语句
  调用Python库
  面向对象编程
更多例子
  睇睇时间
  暂停
  嚟个随机数
  海龟画图
  迷宫游戏仔
点样运行?
TODOs

引言

粤语编程语言系咩? 佢系一门用粤语嚟同计算机沟通嘅编程语言。
喺呢个语言度,计算机可以读明你写嘅粤语。所以话,你可以用粤语嚟操作(虐待)计算机。

咋咋淋入门

Hello World

用粤语写嘅第一个程序Hello World:

畀我睇下 " Hello World! " 点样先?

赋值语句

讲嘢 |A| 系 1
讲嘢 |B| 系 2

睇下变量嘅类型

讲嘢 |A| 系 1
起底: |A|

运行结果:


循环

打印从1到100:

讲嘢: |start| 系 0
落操场玩跑步
    讲嘢: |start| 系 |start + 1|
    畀我睇下 |start| 点样先?
玩到 |start < 100| 为止

当然用For循环都得:

|A| 从 1 行到 100
    畀我睇下 |A| 点样先?
行晒

条件语句

讲嘢: |A| 系 2
如果 |A 系 2| 嘅话 -> {
    畀我睇下 "A 系 2" 点样先?
}
唔系嘅话 -> {
    畀我睇下 "A 唔系 2" 点样先?
}

函数

用 Cantonese 实现嘅阶乘:

$factorial |项数| 要做咩:
    如果 |项数 系 0| 嘅话 -> {
        返转头 1
    }
    唔系嘅话 -> {
        返转头 |factorial(项数 - 1) * 项数|
    }
搞掂

返回最大值:

$get_max |数字1, 数字2| 要做咩:
    如果 |数字1 比唔上 数字2| 嘅话 -> {
        返转头 |数字2|
    }
    唔系嘅话 -> {
        返转头 |数字1|
    }
搞掂

调用函数:

用下 |get_max(23, 17)|

抛出异常

掟个 |ImportError| 来睇下?

断言语句

谂下: |1 + 1 == 3| ?

运行结果:

Traceback (most recent call last):
    ......
AssertionError

错误捕捉语句

try-except-finally:

执嘢 -> {
    讲嘢: |A| 系 |B|
}
揾到 |NameError| 嘅话 -> {
    畀我睇下 "揾到NameError" 点样先?
}
执手尾 -> {
    畀我睇下 "执手尾" 点样先?
    讲嘢: |A| 系 1
    讲嘢: |B| 系 1
    畀我睇下 |A, B| 点样先?
}

调用 Python 库

使下 os
使下 math

面向对象编程

声明对象duck, 继承至object, 分别有两个方法游水同埋睡觉, 仲有一个属性性别:

咩系 |duck|?
    佢个老豆叫 |object|
    佢嘅 |性别| 系 "公"
    佢识得 |游水| -> {
        畀我睇下 "Duck is swimming" 点样先?
    }
    佢识得 |睡觉| -> {
        畀我睇下 "Duck is sleeping" 点样先?
    }
明白未啊?

调用类中嘅方法:

用下 |duck().游水()|
用下 |duck().睡觉()|

运行结果:

Duck is swimming
Duck is sleeping

更多例子

显示当前时间

使下 datetime
畀我睇下 |宜家几点| 点样先?

运行结果:

2021-01-17 09:16:20.767191

暂停

使下 time
瞓阵先 /* 暂停2s */
瞓 5s /* 暂停5s */

嚟个随机数

使下 random
讲嘢: |A| 就 |求其啦|

运行结果:

0.15008236307867207

海龟绘图

老作一下 -> {
    首先: |画个圈(100)|
    跟住: |写隻字("Made By Cantonese\n")|
    最尾: |听我支笛()|
}

运行结果:
turtle_result

迷宫游戏仔

代码
运行结果:
game_result

喺呢度睇下更多例子.

点样运行?

Cantonese 语言运行喺 Python 虚拟机上,环境净系支持 Python3,因为噉先符合广东人先进嘅思想!

python src/cantonese.py [-文件名]

将 Cantonese 转化成 Python:

python src/cantonese.py [文件名] -to_py

例如:

python src/cantonese.py examples/helloworld.cantonese -to_py

运行嘅结果系:

print(" Hello World! ")
exit()

TODOs

本项目代码写得很陋(烂),欢迎各个粤语同埋编程爱好者一齐讨论同贡献!为粤语文化遗产嘅保护贡献出自己嘅一份力量! 今后要做咩:

  • 完善语法错误检查
  • 加多啲语句
  • 编写编辑器插件
Comments
  • #37 添加交互环境, 现仅支持单行语句

    #37 添加交互环境, 现仅支持单行语句

    #37 当前运行效果如下: Screen Shot 2021-07-28 at 1 22 41 AM

    本想将交互部分放在单独文件,但为用 cantonese_run 仍放在了同一文件中。今后为支持更复杂的语句段,还需逐步改进,代码量恐怕还会有相当增加。 建议尽早考虑拆分重构源码,以方便复用、避免积重难返:https://github.com/StepfenShawn/Cantonese/issues/25#issuecomment-881839389

    opened by nobodxbodon 3
  • 更正錯別字

    更正錯別字

    「扑」响廣州話係讀 bok1,解作敲打;「撲」讀音 pok3,後簡化作「扑」。

    puk1 解作跌倒,同上面兩個來源嘅「扑」都無關。puk1 正寫係「仆」,同僕 buk6 的簡化字「仆」同形。

    內地有唔少媒體都係用錯「扑」,應該係錯誤理解「puk1 gaai1」嘅來源,以為係「撲向地面」咁解。實際上「仆」本身就係跌倒,四字成語「前仆後繼」個「仆」正正就係呢個意思。「仆」讀作 fu6 係文讀音,白讀音就係 puk1,係同一個字來。

    opened by hfhchan 3
  • `six` package dependency error

    `six` package dependency error

    Hello, after installing Cantonese, it fails to run because of the import six that isn't included in your list of dependencies. https://github.com/StepfenShawn/Cantonese/blob/3072e3b346225cf2b62538fb6823ae32bee165a6/src/stack_vm.py#L9

    https://github.com/StepfenShawn/Cantonese/blob/4870538b948d5a03d2a83b72febeb5bcf2df36f6/setup.cfg#L31-L34

    You only support Python 3 in setup.cfg and the README, so it doesn't look like you would need six for Python 2 interoperability. https://github.com/StepfenShawn/Cantonese/blob/4870538b948d5a03d2a83b72febeb5bcf2df36f6/setup.cfg#L30

    It doesn't seem like you are using six at all. Can we delete the import six line?

    % Cantonese
    Traceback (most recent call last):
      File "/Users/dosentmatter/.pyenv/versions/3.9.6/bin/Cantonese", line 5, in <module>
        from src.cantonese import main
      File "/Users/dosentmatter/.pyenv/versions/3.9.6/lib/python3.9/site-packages/src/cantonese.py", line 12, in <module>
        from src.stack_vm import *
      File "/Users/dosentmatter/.pyenv/versions/3.9.6/lib/python3.9/site-packages/src/stack_vm.py", line 9, in <module>
        import six
    ModuleNotFoundError: No module named 'six'
    

    The workaround is to pip install six, but this shouldn't be necessary.

    opened by dosentmatter 2
  • 命令与参数的母语化

    命令与参数的母语化

    刚看到 to_web 等参数 (complie 好像是拼写笔误?),不妨考虑也母语化,比如 木兰重现里用的

     --版本,         -版   显示版本
     --python变木兰, -兰   将 Python 源码转换为木兰源码
     --语法树,       -树   语法树信息
    

    对了在 pypi 发布的时候可以把命令也母语化,像这样:

        entry_points = {
            "console_scripts": ['木兰 = 木兰.中:中']
            },
    

    就可以运行 $ 木兰 了。

    opened by nobodxbodon 1
  • 没有做参数检查

    没有做参数检查

    python3 cantonese.py Traceback (most recent call last): File "cantonese.py", line 702, in main() File "cantonese.py", line 690, in main with open(sys.argv[1], encoding = "utf-8") as f: IndexError: list index out of range

    opened by RonYoung666 1
  • Is the interactive interpreter (REPL) supposed to work?

    Is the interactive interpreter (REPL) supposed to work?

    • Running any statement does not seem to do anything.
    • The only way to exit is to kill the program with CTRL+C, 收工 doesn't work.
    % Cantonese 
    早晨!
    > 畀我睇下 " Hello World! " 点样先?
    > 
    > 收工
    > 
    > Traceback (most recent call last):
      File "/Users/dosentmatter/.pyenv/versions/3.9.6/bin/Cantonese", line 8, in <module>
        sys.exit(main())
      File "/Users/dosentmatter/.pyenv/versions/3.9.6/lib/python3.9/site-packages/src/cantonese.py", line 3125, in main
        sys.exit(开始交互())
      File "/Users/dosentmatter/.pyenv/versions/3.9.6/lib/python3.9/site-packages/src/cantonese.py", line 3089, in 开始交互
        交互().cmdloop("早晨!")
      File "/Users/dosentmatter/.pyenv/versions/3.9.6/lib/python3.9/cmd.py", line 126, in cmdloop
        line = input(self.prompt)
    KeyboardInterrupt
    
    bug 
    opened by dosentmatter 3
  • Close some issues for cleanup

    Close some issues for cleanup

    Can we close some of these issues that are not actually issues to reduce clutter?

    Here are a few examples, but there are a lot more: https://github.com/StepfenShawn/Cantonese/issues/26 https://github.com/StepfenShawn/Cantonese/issues/18 https://github.com/StepfenShawn/Cantonese/issues/51

    opened by dosentmatter 0
Owner
Stepfen Shawn
A 16-year-old high school student. Designer and Developer Of square-lang and Cantonese-lang.
Stepfen Shawn
Functional programming in Python: implementation of missing features to enjoy FP

Fn.py: enjoy FP in Python Despite the fact that Python is not pure-functional programming language, it's multi-paradigm PL and it gives you enough fre

Oleksii Kachaiev 3.3k Jan 4, 2023
A programming language built on top of Python to easily allow Swahili speakers to get started with programming without ever knowing English

pyswahili A programming language built over Python to easily allow swahili speakers to get started with programming without ever knowing english pyswa

Jordan Kalebu 72 Dec 15, 2022
Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

Advent Of Code 2021 - Python English Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels th

Coral Izquierdo Muñiz 2 Jan 9, 2022
Rick Astley Language is a rick roll oriented, dynamic, strong, esoteric programming language.

Rick Roll Language / Rick Astley Language A rick roll oriented, dynamic, strong, esoteric programming language. Prolegomenon The reasons that I made t

Rick Roll Programming Language 658 Jan 9, 2023
PyGo custom language, New but similar language programming

New but similar language programming. Now we are capable to program in a very similar language to Python but at the same time get the efficiency of Go.

Fernando Perez 4 Nov 19, 2022
NL. The natural language programming language.

NL A Natural-Language programming language. Built using Codex. A few examples are inside the nl_projects directory. How it works Write any code in pur

null 2 Jan 17, 2022
The Python programming language

This is Python version 3.10.0 alpha 5 Copyright (c) 2001-2021 Python Software Foundation. All rights reserved. See the end of this file for further co

Python 49.7k Dec 30, 2022
A faster and highly-compatible implementation of the Python programming language. The code here is out of date, please follow our blog

Pyston is a faster and highly-compatible implementation of the Python programming language. Version 2 is currently closed source, but you can find the

null 4.9k Dec 21, 2022
The Stackless Python programming language

This is Python version 3.7.0 alpha 4+ Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 20

Stackless Python 891 Jan 3, 2023
PostgreSQL database adapter for the Python programming language

psycopg2 - Python-PostgreSQL Database Adapter Psycopg is the most popular PostgreSQL database adapter for the Python programming language. Its main fe

The Psycopg Team 2.8k Jan 5, 2023
Short, introductory guide for the Python programming language

100 Page Python Intro This book is a short, introductory guide for the Python programming language.

Sundeep Agarwal 185 Dec 26, 2022
The swas programming language

The Swas programming language This is a language that was made for fun. Installation Step 0: Make sure you have python installed Step 1. Clone this re

Swas.py 19 Jul 18, 2022
A probabilistic programming language in TensorFlow. Deep generative models, variational inference.

Edward is a Python library for probabilistic modeling, inference, and criticism. It is a testbed for fast experimentation and research with probabilis

Blei Lab 4.7k Jan 9, 2023
PClean: A Domain-Specific Probabilistic Programming Language for Bayesian Data Cleaning

PClean: A Domain-Specific Probabilistic Programming Language for Bayesian Data Cleaning Warning: This is a rapidly evolving research prototype.

MIT Probabilistic Computing Project 190 Dec 27, 2022
Lightning fast and portable programming language!

Photon Documentation in English Lightning fast and portable programming language! What is Photon? Photon is a programming language aimed at filling th

William 58 Dec 27, 2022
sawa (ꦱꦮ) is an open source programming language, an interpreter to be precise, where you can write python code using javanese character.

ꦱꦮ sawa (ꦱꦮ) is an open source programming language, an interpreter to be precise, where you can write python code using javanese character. sawa iku

Rony Lantip 307 Jan 7, 2023
A simple programming language for manipulating images.

f-stop A simple programming language for manipulating images. Examples OPEN "image.png" AS image RESIZE image (300, 300) SAVE image "out.jpg" CLOSE im

F-Stop 6 Oct 27, 2022
Implemented fully documented Particle Swarm Optimization algorithm (basic model with few advanced features) using Python programming language

Implemented fully documented Particle Swarm Optimization (PSO) algorithm in Python which includes a basic model along with few advanced features such as updating inertia weight, cognitive, social learning coefficients and maximum velocity of the particle.

null 9 Nov 29, 2022
box is a text-based visual programming language inspired by Unreal Engine Blueprint function graphs.

Box is a text-based visual programming language inspired by Unreal Engine blueprint function graphs. $ cat factorial.box ┌─ƒ(Factorial)───┐

Pranav 104 Dec 24, 2022