Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/StepfenShawn/Cantonese
粤语編程語言.The Cantonese programming language.
https://github.com/StepfenShawn/Cantonese
cantonese cantonese-language compiler programming-language python3
Last synced: 3 months ago
JSON representation
粤语編程語言.The Cantonese programming language.
- Host: GitHub
- URL: https://github.com/StepfenShawn/Cantonese
- Owner: StepfenShawn
- License: mit
- Created: 2020-12-20T08:30:23.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-07-07T08:12:24.000Z (7 months ago)
- Last Synced: 2024-07-07T09:28:43.014Z (7 months ago)
- Topics: cantonese, cantonese-language, compiler, programming-language, python3
- Language: Python
- Homepage: https://cantonese-community.github.io/
- Size: 1.96 MB
- Stars: 1,145
- Watchers: 13
- Forks: 45
- Open Issues: 33
-
Metadata Files:
- Readme: README-en.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
![logo](img/logo.jpg)
[![pypi](https://img.shields.io/pypi/dm/Cantonese)](https://pypi.org/project/Cantonese/)
[![pypi](https://img.shields.io/pypi/v/Cantonese)](https://pypi.org/project/Cantonese/)
# The Cantonese programming language
If you don't know Cantonese or programming, please see [learn Cantonese while learning programming in 5 minutes](doc/cantonese.md)
# Installation
```
pip install Cantonese
```
# Introduction
What is Cantonese?
Quick Start
Hello World
Assignment statement
Get the type of variable
Loop
If statement
Function
Throw exception
Assertion statement
Catch statement
Call python library
object-oriented programming
Stack
More examples
Get the time
Sleep
Get the random number
Calculate the correlation coefficient
Machine learning
Turtle drawing
Maze game
Various sorting and searching algorithms
Website design
Develop an app in Cantonese
How to run?
TODOs
# What is Cantonese programming language?
What is the cantonese programming language? It is a programming language that communicates with computers in [Cantonese](https://en.wikipedia.org/wiki/Cantonese).
The computer can read the Cantonese you write and you can operate (abuse) the computer in Cantonese.# Quick Start
### Hello World
The first program written in Cantonese: `Hello World`:
```
畀我睇下 " Hello World! " 點樣先??
```
### Assignment statement
```
介紹返 |A| 係 1
介紹返 |B| 係 2
```
### Get the type of variable
```
介紹返 |A| 係 1
起底: |A|
```
You will get:
``````
### Loop
Print numbers from 1 to 100:
```
介紹返: |start| 係 0
介紹返: |结束| 係 唔啱
落操场玩跑步
介紹返: |start| 係 |start + 1|
畀我睇下 |start| 點樣先??
如果 |start >= 100| 嘅话 => {
饮茶先啦
}
玩到 |结束| 为止
收工
```
You can also use `For` Loop:
```
|A| 从 1 行到 100
畀我睇下 |A| 點樣先??
到步
```
### If statement
```
介紹返: |A| 係 2
如果 |A 係 2| 嘅话 => {
畀我睇下 "A 係 2" 點樣先??
}
唔係 嘅话 => {
畀我睇下 "A 唔係 2" 點樣先??
}
```
### Function
Factorial algorithm in Cantonese:
```
$factorial |项数| 點部署:
如果 |项数 係 0| 嘅话 -> {
还数 1
}
唔係 嘅话 -> {
还数 |factorial(项数 减 1) 乘 项数|
}
搞掂
```
Get the max value:
```
$get_max |数字1, 数字2| 點部署:
如果 |数字1 比唔上 数字2| 嘅话 -> {
还数 |数字2|
}
唔係 嘅话 -> {
还数 |数字1|
}
搞掂
```
Call the function:
```
用下 |get_max(23, 17)|
```
### Throw exception
```
掟个 |ImportError| 嚟睇下?
```
You will get:
```
濑嘢: ImportError()!
```
### Assertion statement
```
谂下: |1 + 1 == 3| ?
```
You will get:
```
濑嘢: AssertionError()!
```
### Catch statement
try-except-finally:
```
执嘢 -> {
介紹返: |A| 係 |B|
}
揾到 |NameError| 嘅话 -> {
畀我睇下 "揾到NameError" 點樣先?
}
执手尾 -> {
畀我睇下 "执手尾" 點樣先?
介紹返: |A| 係 1
介紹返: |B| 係 1
畀我睇下 |A, B| 點樣先?
}
```
### Call Python library
```
使下 os
使下 math
```
### object-oriented programming
Declare object `duck` and extend from `object`, define two methods: `游水` and `睡觉`, and attribute: `性别`
```
咩係 |duck|?
佢個老豆叫 |object|
佢嘅 |性别| 係 "公"
佢識得 |游水| -> {
畀我睇下 "Duck is swimming" 點樣先?
}
佢識得 |睡觉| -> {
畀我睇下 "Duck is sleeping" 點樣先?
}
明白未啊?
```
Call the methods which in class:
```
|duck()| -> |游水|: ||
|duck()| -> |睡觉|: ||
```
You will get:
```
Duck is swimming
Duck is sleeping
```
### Stack
```
有条仆街叫 |Deo哥|
顶你 -> |Deo哥|: 1
顶你 -> |Deo哥|: 2
顶你 -> |Deo哥|: 3
丢你 -> |Deo哥|
```
You will get:
```
Stack: [1,2]
```
# More examples
### Get the time
```
使下 datetime
畀我睇下 |宜家几点()| 點樣先?
```
You will get:
```
2021-01-17 09:16:20.767191
```
### Sleep
```
瞓阵先 /* 暂停2s */
瞓 5s /* 暂停5s */
```
### Get the random number
```
使下 random
介紹返: |A| 就 |求其啦()|
```
You will get:
```
0.15008236307867207
```
### Calculate the correlation coefficient
Define two list and calculate the correlation coefficient:
```
使下 math
|2.165, 1.688, 1.651, 2.229| 拍住上 => |A|
|2.060, 1.822, 1.834, 2.799| 拍住上 => |B|
畀我睇下 |A同B有几衬| 點樣先?
```
You will get:
```
0.8066499427138474
```
### Machine learning
KNN algorithm in Cantonese:
```
使下 math
|[5, 1], [4, 0], [1, 3], [0, 4]| 拍住上 -> |数据|
|'动作片', '动作片', '科幻片', '科幻片'| 拍住上 -> |标签|
介紹返: |K| 係 3
嗌 KNN 过嚟估下 => |[3, 0]|
```
You will get:
```
动作片
```
Linear regression:
```
使下 math
|300.0 , 400.0 , 400.0 , 550.0 , 720.0 , 850.0 , 900.0 , 950.0| 拍住上 -> |X|
|300.0 , 350.0 , 490.0 , 500.0 , 600.0 , 610.0 , 700.0 , 660.0| 拍住上 -> |Y|
嗌 L_REG 过嚟估下 => |900.0|
```
You will get:
```
Linear function is:
y=0.530960991635149x+189.75347155122432
667.6183640228585
```
### Turtle drawing
```
老作一下 -> {
首先: |画个圈(100)|
跟住: |写隻字("Made By Cantonese\n")|
最尾: |听我支笛()|
}
```
You will get:
![turtle_result](img/turtle_etc.jpg)### Maze game
[Code](examples/games/game.cantonese)
You will get:
![game_result](img/game_result.jpg)### Various sorting and searching algorithms
* [Binary Search](examples/algorithms/binary_search.cantonese)
* [Linear Search](examples/algorithms/linear_search.cantonese)
* [Bubble Sort](examples/algorithms/bubble_sort.cantonese)
* [Insert Sort](examples/algorithms/insert_sort.cantonese)### A simple web page
This is a simple web page:
```
老作一下 {
打标题 => [ "我嘅第一个网页" ]
拎笔 => [ "Hello World" ]
}
```
After running, open '127.0.0.1:80' to view the running results:
```
python src/cantonese.py ../examples/web/hello_web.cantonese -to_web
```
![web_result](img/web_result.jpg)### Develop an app in Cantonese
At first, you need install `kivy`:
```
pip install kivy
```
The first App `HelloWord` in Cantonese:
```
使下 kivy
咩係 HelloApp?
佢個老豆叫 App
佢識得 |HelloWorld| => {
|同我show| 下 => "Hello World" @ |做嘢|
还数 |做嘢|
}
明白未啊?|App运行| 下 => |HelloApp, HelloApp().HelloWorld|
```
![App](img/HelloApp.jpg)# More?
[Here](examples/) you can see more examples.
# How to run?
The Cantonese language runs on the python virtual machine, and the environment supports Python 3, because it conforms to the advanced ideas of the Cantonese!
```shell
python src/cantonese.py [-filename]
```
Convert Cantonese to Python:
```shell
python src/cantonese.py [-filename]] -to_py
```
For example:
```
python src/cantonese.py examples/basic/helloworld.cantonese -to_py
```
You will get:
```
print(" Hello World! ")
exit()
```
It can also generate `HTML` code:
```shell
python src/cantonese.py examples/web/web_hello.cantonese -to_web -compile
```
```html我嘅第一个网页
Hello World
```
Run in traditional Chinese:
```
python src/cantonese.py [-filename] -use_tr
```
Vscode plus for Cantonese: https://github.com/Cantonese-community/vscode-cantonese# TODOs
All cantonese or programming lovers are welcome to discuss and contribute together! Contribute to the protection of Cantonese cultural heritage!
TODOs:
* Improve and perfect the syntax error checking
* Add more statements
* Improve own virtual machine