粤语编程语言.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

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

  • 完善语法错误检查
  • 加多啲语句
  • 编写编辑器插件
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 04, 2023
Cython implementation of Toolz: High performance functional utilities

CyToolz Cython implementation of the toolz package, which provides high performance utility functions for iterables, functions, and dictionaries. tool

894 Jan 02, 2023
A functional standard library for Python.

Toolz A set of utility functions for iterators, functions, and dictionaries. See the PyToolz documentation at https://toolz.readthedocs.io LICENSE New

4.1k Jan 03, 2023
Make your functions return something meaningful, typed, and safe!

Make your functions return something meaningful, typed, and safe! Features Brings functional programming to Python land Provides a bunch of primitives

dry-python 2.5k Jan 05, 2023
Simple, elegant, Pythonic functional programming.

Coconut Coconut (coconut-lang.org) is a variant of Python that adds on top of Python syntax new features for simple, elegant, Pythonic functional prog

Evan Hubinger 3.6k Jan 03, 2023
A fancy and practical functional tools

Funcy A collection of fancy functional tools focused on practicality. Inspired by clojure, underscore and my own abstractions. Keep reading to get an

Alexander Schepanovski 2.9k Dec 29, 2022
More routines for operating on iterables, beyond itertools

More Itertools Python's itertools library is a gem - you can compose elegant solutions for a variety of problems with the functions it provides. In mo

2.9k Jan 04, 2023
粤语编程语言.The Cantonese programming language.

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

Stepfen Shawn 895 Dec 24, 2022