MCPcopy Index your code
hub / github.com/StepfenShawn/Cantonese

github.com/StepfenShawn/Cantonese @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
676 symbols 1,627 edges 74 files 189 documented · 28% updated 4mo ago★ 1,19532 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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 Cantonese programming language?

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.

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:

<class 'int'>

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

Maze game

Code
You will get:
game_result

Various sorting and searching algorithms

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

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

More?

Here 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!

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

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

Core symbols most depended-on inside this repo

to_string
called by 78
src/parser/token_tree.rs
push
called by 77
src/parser/token_tree.rs
unwrap
called by 68
py_cantonese/can_utils/option.py
cantonese_func_def
called by 43
py_cantonese/can_libs/lib_gobals.py
build_expression
called by 40
src/parser/ast_builder.rs
get
called by 38
py_cantonese/web_core/can_web_parser.py
emit
called by 37
src/compiler/codegen.rs
emit
called by 35
py_cantonese/can_compiler/compiler.py

Shape

Method 350
Function 181
Class 126
Enum 19

Languages

Python53%
Rust40%
Ruby7%

Modules by API surface

py_cantonese/can_libs/std/impl.py61 symbols
src/parser/ast_builder.rs55 symbols
py_cantonese/llvm_core/can_llvm_build.py48 symbols
test.rb45 symbols
py_cantonese/can_libs/std/game/impl.py32 symbols
py_cantonese/can_ast/can_stat.py32 symbols
py_cantonese/web_core/can_web_parser.py31 symbols
src/parser/token_tree.rs25 symbols
py_cantonese/can_error/compile_time.py25 symbols
py_cantonese/can_ast/can_exp.py24 symbols
tests/syntax_tests.rs23 symbols
src/macros/macro_expander.rs20 symbols

For agents

$ claude mcp add Cantonese \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact