Skip to content

Latest commit

 

History

History
356 lines (346 loc) · 8.76 KB

README-en.md

File metadata and controls

356 lines (346 loc) · 8.76 KB

logo
pypi pypi

The Cantonese programming language

If you don't know Cantonese or programming, please see learn Cantonese while learning programming in 5 minutes

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 the cantonese programming language? It is a programming language that communicates with computers in Cantonese.
The computer can read the Cantonese you write and you can operate (abuse) the computer in Cantonese.

The first program written in Cantonese: Hello World:

畀我睇下 " Hello World! " 點樣先??
介紹返 |A| 係 1
介紹返 |B| 係 2
介紹返 |A| 係 1
起底: |A|

You will get:

<class 'int'>

Print numbers from 1 to 100:

介紹返: |start| 係 0
介紹返: |结束| 係 唔啱
落操场玩跑步
    介紹返: |start| 係 |start + 1|
    畀我睇下 |start| 點樣先??
    如果 |start >= 100| 嘅话 => {
        饮茶先啦
    }
玩到 |结束| 为止
收工

You can also use For Loop:

|A| 从 1 行到 100
    畀我睇下 |A| 點樣先??
到步
介紹返: |A| 係 2
如果 |A 係 2| 嘅话 => {
    畀我睇下 "A 係 2" 點樣先??
}
唔係 嘅话 => {
    畀我睇下 "A 唔係 2" 點樣先??
}

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)|
掟个 |ImportError| 嚟睇下?

You will get:

濑嘢: ImportError()!
谂下: |1 + 1 == 3| ?

You will get:

濑嘢: AssertionError()!

try-except-finally:

执嘢 -> {
    介紹返: |A| 係 |B|
}
揾到 |NameError| 嘅话 -> {
    畀我睇下 "揾到NameError" 點樣先?
}
执手尾 -> {
    畀我睇下 "执手尾" 點樣先?
    介紹返: |A| 係 1
    介紹返: |B| 係 1
    畀我睇下 |A, B| 點樣先?
}
使下 os
使下 math

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
有条仆街叫 |Deo哥|
顶你 -> |Deo哥|: 1
顶你 -> |Deo哥|: 2
顶你 -> |Deo哥|: 3
丢你 -> |Deo哥|

You will get:

Stack: [1,2]
使下 datetime
畀我睇下 |宜家几点()| 點樣先?

You will get:

2021-01-17 09:16:20.767191
瞓阵先 /* 暂停2s */
瞓 5s /* 暂停5s */
使下 random
介紹返: |A| 就 |求其啦()|

You will get:

0.15008236307867207

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

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

You will get:
turtle_result

Code
You will get:
game_result

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

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

More?

Here you can see more examples.

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!

python src/cantonese.py [-filename]

Convert Cantonese to Python:

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:

python src/cantonese.py examples/web/web_hello.cantonese -to_web -compile
<html>
<head>
<meta charset="utf-8" />
</head>
<title>我嘅第一个网页</title>
<h1>Hello World</h1>
</html>

Run in traditional Chinese:

python src/cantonese.py [-filename] -use_tr

Vscode plus for Cantonese: https://github.com/Cantonese-community/vscode-cantonese

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