Code Monkey home page Code Monkey logo

qi's Introduction

Lines of Code License Website Size


Logo

气 (Qi) lang

Qi is a lightweight, fast, and open source Chinese programming language.
qilang.tk »

English | 中文

Background

About

After reading an article on how programming languages are created, I was inspired to create an interpreter myself. I took a look at how several languages were implemented, and realized that all of them were based on english. I wanted to make a programming language based on a "foreign" natural language, such as Chinese, and see what drawbacks it might lead to. I started simple by referencing various resources posted online (most notably Crafting Interpreters), and then slowly began implementing concepts mostly on my own. Turns out that creating an entire programming language from scratch is pretty tough, but that was what made it fun.

Why name it Qi? Qi was derived from the pinyin of the Chinese character "气", which means "air" in English. I wanted this language to be quick and lightweight, as well as fully transparent, just like air!

打印行"你好,世界"

Features

  • Qi is Chinese-based. Us English-speaking people don't often take for granted the fact that the programming languages we learn are all based on native tongues we're already fluent in. A native English speaker can reasonably infer what a piece of Python code does just from reading the keywords ("if", "int", "while", etc.). Making this language foreign-based allows programming to be more accessible to more people around the globe.

  • Qi is fast. Qi utilizes a single-pass compiler that compiles to efficient bytecode that runs exceptionally down to the metal. Along with various optimizations such as Nan Boxing and hash table probing, Qi's speed squares up to competing dynamic languages.

  • Qi is light. With less than 5000 lines of code, the entire project could be read in a day. Although the code is short, everything is well-formatted and easy to understand. The final executable is only about 140 kb in size, allowing you to run it on nearly any device imaginable.

  • Qi is object-oriented. Placing classes front and center, object-oriented programming provides a clear modular structure that is significantly better at handling complexity than functional programming though concepts such as abstraction and encapsulation.

Performance

Although a couple of benchmarks aren't that good at judging performance, they're pretty cool to look at... so here's a couple.

Each benchmark was run 5 times, and the average time was taken for each language.

Performance Graph

These tests were run on my Macbook Air 2017 with a Dual-Core i5 CPU and 8 GB RAM.

The code for the benchmarks can be found in the test folder.

Quick Start

Prerequisites

  • make
    • macOS
      brew install make
    • Windows
      choco install make
    • Linux
      • Should be built into the system

Installation

  1. Clone or download zip of git repo.
  2. Open terminal, and cd to /src/cmake-build-release
  3. Run make.
  4. All done!

Unit Tests

Unit testing is so important (especially for a programming language) that I have included a full Qi test suite to make sure that every aspect of the interpreter does what it's supposed to do. The test programs can be found in the /test directory here. To automate the testing procedure, the Go program /utils/test.go provided here runs each test program with the inputted interpreter, captures the output, and validates the result.

Running the Testing Program

To run the tests, you'll need to have Go installed onto your system. Download and install it from here.

Executing the Go program is fairly easy. Simply run go run test.go and pass the path to the desired interpreter into the -interpreter= argument.

go run test.go -interpreter=PATH_TO_INTERPRETER_HERE

Here is the output of the testing program if you forget to implement the negate operator:

$ go run test.go -interpreter=PATH
FAIL ../test/array/indexing.qi
     Expected output '' on line 7 and got 'b'.
FAIL ../test/array/methods.qi
     Expected output '【零,1,2,3】' on line 9 and got '【零,2,3,4】'.
FAIL ../test/for/scope.qi
     Expected output '-1' on line 10 and got '1'.
FAIL ../test/number/literals.qi
     Expected output '-0' on line 4 and got '0'.
     Expected output '-0.001' on line 7 and got '0.001'.
FAIL ../test/operator/negate.qi
     Expected output '-3' on line 1 and got '3'.
FAIL ../test/string/indexing.qi
     Expected output 'g' on line 7 and got 't'.
268 tests passed. 6 tests failed.
exit status 1

And here is the output if all tests pass:

$ go run test.go -interpreter=PATH
All 274 tests passed (641 expectations).

Syntax

The Quick Start page is a great guide to install and set up the language.

For code examples, please refer to the Tests.

For a more extensive look into Qi, go to the Language Guide.

Roadmap

  • Classes
  • Inheritance
  • Lists
  • Increment/decrement operators
  • Switch/Case statements
  • Optimization
  • Translate everything to Chinese
  • Make a logo
  • Finish Readme
  • Translate Readme to Chinese
  • Create Website
    • Front Page
    • Quick Start
    • Language Guide
  • Escape Sequences
  • Bitwise operators
  • Remove semicolons
  • Support scientific notation, binary, etc. numbers
  • More string methods
  • More list methods
  • Modules system

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE.txt for more information.

Contact

Andrew Yang - @anonymousaaardvark - [email protected]

Project Link: https://github.com/anonymousaaardvark/qi

Acknowledgments

qi's People

Contributors

anonymousaaardvark avatar nobodxbodon avatar wenj91 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

qi's Issues

文档无法从英文切换到中文

问题描述

文档无法从英文切换到中文

问题复现步骤

进入英文文档以后,点击右上角的中文按钮,会跳转到中文的文档首页,但是紧接着点击开始按钮后进入的文档依旧是英文的

导致问题可能的原因

中文文档首页的开始按钮链接的是英文文档而非中文文档
image

在 linux 下链接出错

使用以下补丁可修复:

--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -5,5 +5,8 @@ set(CMAKE_C_STANDARD 11)
 
 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}" )
 set(CMAKE_CXX_FLAGS_RELEASE "-O3")
+if(CMAKE_SYSTEM_NAME MATCHES "Linux")
+  set(CMAKE_EXE_LINKER_FLAGS "-lm")
+endif()
 
 add_executable(qi main.c common.h chunk.h chunk.c memory.h memory.c debug.h debug.c value.h value.c vm.h vm.c compiler.h compiler.c scanner.h scanner.c object.h object.c table.h table.c common.h chunk.h chunk.c compiler.c compiler.h core_module.c core_module.h)

斐波的例子无法运行。

官网教程例子如下:

1 功能 斐波数字)「
2    如果数字  2返回 数字
3    返回 斐波数字 - 2+ 斐波数字 - 145 
6 系统打印行斐波20))

报错如下:

> qi hello.qi
操作数必须是数字。
【行 3】在 斐波()
【行 3】在 斐波()
【行 3】在 斐波()
【行 3】在 斐波()
【行 3】在 斐波()
【行 3】在 斐波()
【行 3】在 斐波()
【行 3】在 斐波()
【行 3】在 斐波()
【行 3】在 斐波()
【行 6】在 脚本

请问这是什么原因?是我哪里不对吗?

系统。扫描() reads at most 98 characters

Hello! This is a really cool language concept & fun to use. I noticed when writing some code in this that the 系统。扫描() function seems to have some strange behavior with long lines.

If you read a line more than 98 characters wide, 系统。扫描() will return only the first 98 bytes. If you then call 系统。扫描() again, it will return the next 98 characters, but it will skip one character.

So:

[---first 98 bytes---][1 byte][---next 98 bytes--]\n
 ^ first 系统。扫描()          ^ second 系统。扫描()

It looks like this is just because the 系统。扫描() function has a static array on the stack.

Let me know if you'd like me to write a quick patch for that to allocate the array dynamically! Besides that the language seems to be really solid. I wrote a binary heap and Dijkstra's algorithm, and the performance is good.

(I also noticed that the language will hang indefinitely if 扫描 gets an EOF.)

Linux编译问题

环境

# 系统版本
Linux kensan 5.13.0-30-generic #33~20.04.1-Ubuntu SMP Mon Feb 7 14:25:10 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
# gcc 版本
gcc (Ubuntu 9.4.0-1ubuntu1~20.04) 9.4.0
# cmake 版本
cmake version 3.22.2
# make 版本
GNU Make 4.2.1

步骤

  • cmake .
-- The C compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /gitrepositry/qi/src
  • make
/usr/bin/ld: CMakeFiles/qi.dir/vm.c.o: in function `run':
vm.c:(.text+0x43e0): undefined reference to `fmod'
/usr/bin/ld: CMakeFiles/qi.dir/core_module.c.o: in function `sqrtNative':
core_module.c:(.text+0x5cb): undefined reference to `sqrt'
/usr/bin/ld: CMakeFiles/qi.dir/core_module.c.o: in function `powNative':
core_module.c:(.text+0x6d5): undefined reference to `pow'
/usr/bin/ld: CMakeFiles/qi.dir/core_module.c.o: in function `roundNative':
core_module.c:(.text+0xa3c): undefined reference to `pow'
/usr/bin/ld: core_module.c:(.text+0xa5e): undefined reference to `round'
/usr/bin/ld: CMakeFiles/qi.dir/core_module.c.o: in function `logNative':
core_module.c:(.text+0xcd9): undefined reference to `log'
/usr/bin/ld: core_module.c:(.text+0xcec): undefined reference to `log'
/usr/bin/ld: CMakeFiles/qi.dir/core_module.c.o: in function `sinNative':
core_module.c:(.text+0xd8c): undefined reference to `sin'
/usr/bin/ld: CMakeFiles/qi.dir/core_module.c.o: in function `cosNative':
core_module.c:(.text+0xe1f): undefined reference to `cos'
/usr/bin/ld: CMakeFiles/qi.dir/core_module.c.o: in function `tanNative':
core_module.c:(.text+0xeb2): undefined reference to `tan'
/usr/bin/ld: CMakeFiles/qi.dir/core_module.c.o: in function `asinNative':
core_module.c:(.text+0xf45): undefined reference to `asin'
/usr/bin/ld: CMakeFiles/qi.dir/core_module.c.o: in function `acosNative':
core_module.c:(.text+0xfd8): undefined reference to `acos'
/usr/bin/ld: CMakeFiles/qi.dir/core_module.c.o: in function `atanNative':
core_module.c:(.text+0x106b): undefined reference to `atan'
/usr/bin/ld: CMakeFiles/qi.dir/core_module.c.o: in function `ceilNative':
core_module.c:(.text+0x10fe): undefined reference to `ceil'
/usr/bin/ld: CMakeFiles/qi.dir/core_module.c.o: in function `floorNative':
core_module.c:(.text+0x1191): undefined reference to `floor'
/usr/bin/ld: CMakeFiles/qi.dir/core_module.c.o: in function `randNative':
core_module.c:(.text+0x12dc): undefined reference to `round'
/usr/bin/ld: core_module.c:(.text+0x12f9): undefined reference to `round'
/usr/bin/ld: core_module.c:(.text+0x1318): undefined reference to `round'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/qi.dir/build.make:257:qi] 错误 1
make[1]: *** [CMakeFiles/Makefile2:83:CMakeFiles/qi.dir/all] 错误 2
make: *** [Makefile:91:all] 错误 2

具体编译信息make_info.txt

类的例子无法运行

代码是直接复制官网例子的

 功能 打印()「
        系统打印行"我是一个树!")
    」
」

变量  = ()
系统打印行打印())

命令行运行直接出现错误

> qi hello.qi
【行 2】错误在「功能」:期待方法名。
【行 7】错误在「变量」:期待方法名。
【行 9】错误在末尾:块后期待「 」」。

官网代码错误:“数到串未定义”

「打断」陈述和下面的“继续”陈述里有这样两段代码

// 打断代码
变量 日志 ="好""好""好""错误""好""好"对于变量 i = 0i  日志长度();i++)「
    如果日志i "错误")「
        系统打印行"错误在:" + 数到串i+ "行"打断
    」
」

// 继续代码
变量 列表 =482965486167205738204518397对于变量 i = 0i  列表长度();i++)「
    如果列表i】% 2  0继续
    系统打印数到串列表i】)+ " ")
」
系统打印行""

这两段的无法运行,打印语句都使用了方法“数到串()”,但是该方法并没有引入或定义,直接运行代码就报错了。得到错误提示如下

未定义的变量「数到串」。

关于语法设计中的 。含义

系统。打印行("一"),看起来对应着英文编程语言中的句点。
个人感觉,英文编程语言设计中选择句点是取其“从属”之意(比如章节号 1.2.3、A.1,版本号 3.4.0 等)而非句尾之意,而中文句点传统上只有句尾标志一种含义。
不知考虑过用其他符号如间隔号 · 吗?它可用于书名与篇(章、卷)名之间的分界。例如:《论语·里仁》《孟子·梁惠王》《三国志·蜀志·诸葛亮传》

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.