### Example LSPServer Startup Command Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cangjie-language-server/LSPServer_manual.md Demonstrates how to start the LSPServer with specific configurations for logging, log path, crash log generation, and auto-import. ```shell LSPServer.exe --enable-log=true --log-path=D:/CangjieLSPLog -V --disableAutoImport ``` -------------------------------- ### cjfmt Command Help Example Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjfmt_manual.md Example of how to display the help information for the cjfmt command. ```shell cjfmt -h ``` -------------------------------- ### Source Set Configuration Example Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjpm_manual.md Example syntax for defining source sets with specific directories and conditions. ```toml # Example syntax: # Source file directory for Cangjie code [source-set.epoll] src-dir = "src/net/select/epoll" condition = [ "feature.os.epoll" ] [source-set.kqueue] src-dir = "src/net/select/kqueue" condition = [ "feature.os.kqueue" ] ``` -------------------------------- ### CJPM Build Script Example with Pre/Post Build Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjpm_manual.md Example `build.cj` script demonstrating pre- and post-build actions with console output. ```cangjie import std.process.* func stagePreBuild(): Int64 { println("PRE-BUILD") 0 } func stagePostBuild(): Int64 { println("POST-BUILD") 0 } main(): Int64 { match (Process.current.arguments[0]) { ``` -------------------------------- ### Verify ObjCInteropGen Installation Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/multiplatform/cangjie-ios-objc.md Executes the ObjCInteropGen tool with the --help flag to confirm successful installation. If usage instructions are displayed, the installation is successful. ```bash ObjCInteropGen --help ``` -------------------------------- ### Configure System Environment Variables (Windows) Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/first_understanding/install.md Example values for setting CANGJIE_HOME and Path environment variables in Windows. These are illustrative and should be adapted to your specific installation path. ```text D:\cangjie\bin D:\cangjie\tools\bin D:\cangjie\tools\lib D:\cangjie\runtime\lib\windows_x86_64_cjnative C:\Users\bob\.cjpm\bin ``` -------------------------------- ### End-to-end Example: Compile and Sign Interop Classes Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/multiplatform/cjmp-pg-ios.md A complete example demonstrating the compilation and signing process for interop classes within a specific project directory. ```bash cd cjworld cjc --target=arm64-apple-ios-simulator \ --sysroot=$(xcrun --show-sdk-path --sdk iphonesimulator) \ --output-type=dylib \ --int-overflow=wrapping \ *.cj \ -o libcjworld.dylib \ --link-options "-undefined dynamic_lookup" xcrun codesign --sign - libcjworld.dylib ``` -------------------------------- ### Benchmark Configuration Example Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjpm_manual.md Example of how to configure benchmark settings in the 'profile.bench' section. These options control benchmark compilation and execution. ```text [profile.bench] # Example usage no-color = true random-seed = 10 report-path = "bench_report" baseline-path = "" report-format = "csv" verbose = true ``` -------------------------------- ### Run Cangjie Setup Script (CMD) Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/first_understanding/install.md Execute the environment setup script for Windows Command Prompt (CMD). ```bash path\to\cangjie\envsetup.bat ``` -------------------------------- ### cjfmt Version Example Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjfmt_manual.md Example of how to display the version information for the cjfmt command. ```shell cjfmt -v ``` -------------------------------- ### HTTP Client-Server Example Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/Net/net_http.md Demonstrates a basic HTTP client-server interaction. The client sends a GET request to a server, which responds with a predefined message. Ensure 'net' and 'log' libraries are configured from the 'stdx' module. ```cangjie import stdx.net.http.* import std.time.* import std.sync.* import stdx.log.* // 1. Build a Server instance let server = ServerBuilder().addr("127.0.0.1").port(0).build() func startServer(): Unit { // 2. Register request handling logic server .distributor .register("/hello", { httpContext => httpContext.responseBuilder.body("Hello Cangjie!") }) server .logger .level = LogLevel.OFF // 3. Start the service server.serve() } func startClient(): Unit { // 1. Build a client instance let client = ClientBuilder().build() // 2. Send a request let response = client.get("http://127.0.0.1:${server.port}/hello") // 3. Read the response body let buffer = Array(32, repeat: 0) let length = response.body.read(buffer) println(String.fromUtf8(buffer[..length])) // 4. Close the connection client.close() } main() { spawn { startServer() } sleep(Duration.second) startClient() } ``` -------------------------------- ### Example java.xml Package List with Wildcard Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/multiplatform/cjmp-pg-android.md An example of a package list file for `java.xml`, demonstrating the use of wildcards (`.*`) to include a package and all its subpackages. ```plaintext javax.xml.* org.w3c.dom . . . ``` -------------------------------- ### Example java.base Package List Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/multiplatform/cjmp-pg-android.md This is an example format for the `./java.base.txt` file, listing public packages exported from the `java.base` JDK API module. ```plaintext java.io java.lang java.lang.annotation . . . ``` -------------------------------- ### Example Command to Generate Mirror Declarations Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/multiplatform/cjmp-pg-android.md An example command demonstrating how to run `java-mirror-gen` to mirror types `com.example.a.A`, `com.example.b.B`, and `com.example.c.C`. It specifies the package name, classpath, and output directory. ```bash java-mirror-gen \ --package-name javaworld \ --class-path /home/user/Android/Sdk/platforms/android-35/android.jar:App.jar \ --destination ./src/cj \ com.example.a.A com.example.b.B com.example.c.C ``` -------------------------------- ### Install Central Repository Artifact Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/central-repo/source_en/client/download.md Use this command to install an executable artifact from the central repository to your local machine. Ensure the artifact is marked as installable on the repository website. ```text cjpm install cangjie_repository_artifact-1.0.0 ``` -------------------------------- ### Linux Target Configuration Example Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjpm_manual.md Example configuration for the x86_64-unknown-linux-gnu target, including compilation options, linker options, and various dependency configurations. ```text [target.x86_64-unknown-linux-gnu] # Configuration items for Linux systems compile-option = "value1" # Additional compilation command options override-compile-option = "value2" # Additional global compilation command options link-option = "value3" # Linker passthrough options [target.x86_64-unknown-linux-gnu.dependencies] # Source dependency configuration [target.x86_64-unknown-linux-gnu.test-dependencies] # Test-phase dependency configuration [target.x86_64-unknown-linux-gnu.bin-dependencies] # Cangjie binary library dependencies path-option = ["./test/pro0", "./test/pro1"] [target.x86_64-unknown-linux-gnu.bin-dependencies.package-option] "pro0.xoo" = "./test/pro0/pro0.xoo.cjo" "pro0.yoo" = "./test/pro0/pro0.yoo.cjo" "pro1.zoo" = "./test/pro1/pro1.zoo.cjo" [target.x86_64-unknown-linux-gnu.ffi.c] # C language binary library dependencies "ctest" = "./test/c" [target.x86_64-unknown-linux-gnu.debug] # Debug configuration for Linux systems [target.x86_64-unknown-linux-gnu.debug.test-dependencies] [target.x86_64-unknown-linux-gnu.release] # Release configuration for Linux systems [target.x86_64-unknown-linux-gnu.release.bin-dependencies] ``` -------------------------------- ### Run Cangjie Setup Script (MSYS/Bash) Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/first_understanding/install.md Execute the environment setup script for MSYS shell or bash environments. ```bash source path/to/cangjie/envsetup.sh ``` -------------------------------- ### CJPM TOML Configuration Example Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/compile_and_build/cjpm_usage.md This example demonstrates the structure and fields of a `cjpm.toml` file, including package metadata, workspace settings, dependency declarations, and target-specific configurations. ```toml [package] cjc-version = "1.0.0" name = "demo" description = "nothing here" version = "1.0.0" compile-option = "" override-compile-option = "" link-option = "" output-type = "executable" src-dir = "" target-dir = "" package-configuration = {} [workspace] members = [] build-members = [] test-members = [] compile-option = "" override-compile-option = "" link-option = "" target-dir = "" [dependencies] coo = { git = "xxx", branch = "dev" } doo = { path = "./pro1" } [test-dependencies] [script-dependencies] [replace] [ffi.c] clib1.path = "xxx" [profile] build = {} test = {} bench = {} customized-option = {} [target.x86_64-unknown-linux-gnu] compile-option = "value1" override-compile-option = "value2" link-option = "value3" [target.x86_64-w64-mingw32.dependencies] [target.x86_64-w64-mingw32.test-dependencies] [target.x86_64-unknown-linux-gnu.bin-dependencies] path-option = ["./test/pro0", "./test/pro1"] [target.x86_64-unknown-linux-gnu.bin-dependencies.package-option] "pro0.xoo" = "./test/pro0/pro0.xoo.cjo" "pro0.yoo" = "./test/pro0/pro0.yoo.cjo" "pro1.zoo" = "./test/pro1/pro1.zoo.cjo" ``` -------------------------------- ### Install Project from Local Path Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjpm_manual.md Installs a Cangjie project located at a specific local path. This command compiles the project and installs its artifacts to the configured root directory. ```text cjpm install --path path/to/project ``` -------------------------------- ### Install LLVM 15 Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/multiplatform/cangjie-ios-objc.md Installs LLVM version 15 using Homebrew. Ensure LLVM 15 is installed before proceeding with ObjCInteropGen. ```bash brew install llvm@15 ``` -------------------------------- ### Install NVM and Node.js Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/HLE_manual.md Installs Node Version Manager (nvm) and Node.js v22. Verifies the installed versions of Node.js and npm. ```shell # Download and install nvm: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash # in lieu of restarting the shell . "$HOME/.nvm/nvm.sh" # Download and install Node.js: nvm install 22 # Verify the Node.js version: node -v # Should print "v22.17.1". nvm current # Should print "v22.17.1". # Verify npm version: npm -v # Should print "10.9.2". ``` -------------------------------- ### Run Cangjie Setup Script (PowerShell) Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/first_understanding/install.md Execute the environment setup script for PowerShell. Note the leading dot and space. ```powershell . path\to\cangjie\envsetup.ps1 ``` -------------------------------- ### Cangjie Compiler Example Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/multiplatform/cjmp-pg-android.md An example of the cjc command with specific source and output directories, and libraries to link. ```bash cjc --output-type=dylib \ -p src/cj \ -ljava.lang -linteroplib.interop \ --output-javagen-dir=src/java ``` -------------------------------- ### Cangjie Build Script Example Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjpm_manual.md This example demonstrates a complete Cangjie build script, including importing standard and custom modules, defining pre-build and post-build functions, and handling command-line arguments to control execution flow. It shows how to call a function from an imported module and print output. ```cangjie import std.process.* import aoo.* func stagePreBuild(): Int64 { aaa() 0 } func stagePostBuild(): Int64 { println("POST-BUILD") 0 } main(): Int64 { match (Process.current.arguments[0]) { case "pre-build" => stagePreBuild() case "post-build" => stagePostBuild() case _ => 0 } } ``` -------------------------------- ### Install Project from Git URL Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjpm_manual.md Installs a Cangjie project directly from a Git repository URL. The project is compiled and its artifacts are installed to the configured root directory. ```text cjpm install --git url ``` -------------------------------- ### cjpm.toml Configuration Example Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/central-repo/source_en/client/upload.md This is a comprehensive example of a `cjpm.toml` file, demonstrating the configuration for artifact package creation and publishing. It includes settings for versioning, naming, organization, description, output type, authors, repository, homepage, documentation, tags, categories, licenses, include/exclude paths, and various dependency types. ```toml [package] cjc-version = "1.0.0" name = "demo" organization = "cangjie" description = "demo of cangjie central repository" version = "1.0.0" output-type = "executable" authors = ["Tom", "Joan"] repository = "https://cangjie-demo.git" homepage = "https://cangjie-demo.com" documentation = "https://cangjie-demo.com/docs" tag = ["cangjie", "demo"] category = ["Network", "UI"] license = ["Apache-2.0"] include = ["src"] exclude = ["*.txt"] [dependencies] aoo = "1.0.0" [test-dependencies] boo = "2.0.0" [script-dependencies] "org::coo" = "3.0.0" ``` -------------------------------- ### Install OpenSSL Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/Appendix/linux_toolchain_install.md Installs the compiled OpenSSL to the system or the specified --prefix directory. Root privileges may be required. ```shell $ make install ``` ```shell $ sudo make install ``` -------------------------------- ### Example Java Mirror Generator Command Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/multiplatform/cjmp-pg-android.md This example demonstrates generating mirror types for 'com.example.a.A' and 'com.example.b.B' in the 'javaworld' package, using a specified application classpath and output directory. ```bash java-mirror-gen \ --package-name javaworld \ --class-path /home/user/Android/Sdk/platforms/android-35/android.jar:App.jar \ --destination ./src/cj \ com.example.a.A com.example.b.B ``` -------------------------------- ### Verify ObjCInteropGen Installation Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/multiplatform/cjmp-pg-ios.md Run the ObjCInteropGen command to confirm that the mirror generator tool is installed and accessible. Successful execution displays usage instructions. ```bash ObjCInteropGen ``` -------------------------------- ### Cangjie Repository Configuration Example Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjpm_manual.md An example of the `cangjie-repo.toml` file, showing configurations for the repository cache path, central registry URL, user token, and global TLS strictness. ```toml [repository.cache] path = "/path/to/repository/cache" [repository.home] registry = "central/repo/url" token = "user-token" [global] strict-tls = true ``` -------------------------------- ### cjfmt Custom Configuration File Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjfmt_manual.md Example of specifying a custom configuration file for formatting. ```shell cjfmt -f a.cj -c ./cangjie-format.toml ``` -------------------------------- ### Get Value from Option or Throw Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/error_handle/use_option.md This example shows how to use the getOrThrow function to retrieve a value from an Option, throwing an exception if it's None. ```cangjie main() { let a = Some(1) ``` -------------------------------- ### Correct Multi-Line Suppression Example 3 Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjlint_manual.md Suppresses the G.FUN.02 rule for a block of code using a Javadoc-style comment for the start and a line comment for the end. ```cangjie /** * cjlint-ignore -start !G.FUN.02 description */ func foo(a: Int64, b: Int64, c: Int64, d: Int64) { return a + b + c } // cjlint-ignore -end description ``` -------------------------------- ### Workspace Configuration Example Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjpm_manual.md Demonstrates how to define a workspace with multiple members, specify modules for building and testing, and set compilation options. Dependencies and FFI configurations are also shown. ```toml [workspace] members = ["aoo", "boo", "coo"] build-members = ["aoo", "boo"] test-members = ["aoo"] compile-option = "-Woff all" override-compile-option = "-O2" [dependencies] xoo = { path = "path_xoo" } [ffi.c] abc = { path = "libs" } ``` -------------------------------- ### Invalid Regular Identifiers in Cangjie Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/basic_programming_concepts/identifier.md Examples of invalid regular identifiers, illustrating common mistakes such as using disallowed characters, starting with Arabic numerals, or using keywords. ```text ab&c // & is not an XID_Continue character 3abc // Arabic numerals are not XID_Start characters, so they cannot be used as starting characters _ // An underscore must be followed by at least one XID_Continue character while // "while" is a Cangjie keyword and cannot be used as a regular identifier ``` -------------------------------- ### Example Build Output for Multiple Binaries Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjpm_manual.md Demonstrates the expected output after running `cjpm build` when multiple binary artifacts are configured. ```text Input: cjpm build Output: cjpm build success Input: tree target/release/bin Output: target/release/bin |-- demo.aoo |-- demo.boo `-- demo ``` -------------------------------- ### Valid Regular Identifiers in Cangjie Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/basic_programming_concepts/identifier.md Examples of valid regular identifiers that follow the specified naming rules, including sequences starting with XID_Start characters or an underscore, followed by XID_Continue characters. ```text abc _abc abc_ a1b2c3 a_b_c a1_b2_c3 Cangjie __こんにちは ``` -------------------------------- ### Launch and Sample a New Application Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjprof_manual.md Starts a new application and simultaneously collects its CPU performance data. Supports setting sampling frequency to maximum and uses default output file name. ```text cjprof record -f max -- ./test arg1 arg2 ``` -------------------------------- ### Install TypeScript and cjbind Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/HLE_manual.md Installs TypeScript and cjbind dependencies for the HLE tool. Navigate to the dtsparser directory before running npm install. ```shell cd ${CANGJIE_HOME}/tools/dtsparser/ npm install ``` -------------------------------- ### Cangjie Language Server Startup Parameters Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cangjie-language-server/LSPServer_manual.md Lists the available command-line arguments for configuring the LSPServer, including options for enabling logging, specifying log paths, and disabling auto-import. ```shell -V Optional parameter, enables crash log generation capability for LSPServer --enable-log= Optional parameter, controls whether to enable log printing. If not set, defaults to true (enabling log printing) --log-path= Optional parameter, specifies the directory for generating log files and crash logs. If not set, logs will be generated in the LSPServer's working directory by default --disableAutoImport Optional parameter, disables automatic package import during code completion --test Optional parameter, starts test mode for running LSPServer test cases ``` -------------------------------- ### Install macOS Dependencies with Homebrew Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/first_understanding/install.md Installs the 'libffi' dependency required for the macOS version of the Cangjie toolchain. This command should be executed before proceeding with the installation. ```bash brew install libffi ``` -------------------------------- ### Example Package List Content Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/multiplatform/cjmp-pg-android.md Illustrates the format of the package list file, including standard package names and wildcard usage. ```text com.example.model com.example.ui.* ``` -------------------------------- ### Example Input and Output for Extended Command Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjpm_manual.md Illustrates the expected input and output when running a custom cjpm command ('cjpm demo') with an argument ('hello,world'), demonstrating the functionality of the extended command. ```text Input: cjpm demo hello,world Output: Output: hello,world ``` -------------------------------- ### Running tests with report path and format Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjpm_manual.md This example demonstrates how to run tests and specify a path for saving the test execution report, along with the desired report format. The `--report-path` and `--report-format` options are used here. ```text cjpm test src --report-path=reports --report-format=xml ``` -------------------------------- ### Install Cangjie Toolchain Dependencies on Ubuntu 20.04 Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/Appendix/linux_toolchain_install.md Use this command to install necessary development tools for the Cangjie toolchain on Ubuntu 20.04. Ensure OpenSSL 3 is installed separately if not available. ```shell $ apt-get install \ binutils \ libc-dev \ libc++-dev \ libgcc-9-dev ``` -------------------------------- ### Install Cangjie Toolchain Dependencies on Ubuntu 18.04 Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/Appendix/linux_toolchain_install.md Use this command to install necessary development tools for the Cangjie toolchain on Ubuntu 18.04. Ensure OpenSSL 3 is installed separately if not available. ```shell $ apt-get install \ binutils \ libc-dev \ libc++-dev \ libgcc-7-dev ``` -------------------------------- ### Installing llvm-cov on Ubuntu Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjcov_manual.md Install the `llvm-cov` command on Ubuntu using the apt package manager. ```shell apt install llvm-cov ``` -------------------------------- ### Example Obfuscated Stack Trace Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjtrace_recover_manual.md This is an example of an exception stack trace from an obfuscated Cangjie program. ```text An exception has occurred: MyException: this is myexception at a0(SOURCE:0) at ah(SOURCE:0) at c3(SOURCE:0) at cm0(SOURCE:0) at cm1(SOURCE:0) at ci0(:0) ``` -------------------------------- ### Examples of Token Construction Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/Macro/Tokens_types_and_quote_expressions.md Demonstrates the creation of various `Token` instances, including operators, keywords, identifiers, and literals. ```cangjie import std.ast.* let tk1 = Token(TokenKind.ADD) // '+' operator let tk2 = Token(TokenKind.FUNC) // func keyword let tk3 = Token(TokenKind.IDENTIFIER, "x") // x identifier let tk4 = Token(TokenKind.INTEGER_LITERAL, "3") // integer literal let tk5 = Token(TokenKind.STRING_LITERAL, "xyz") // string literal ``` -------------------------------- ### Basic Try-with-Resources Usage Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/error_handle/handle.md Demonstrates the fundamental use of try-with-resources to manage a custom `Worker` resource, ensuring it's properly closed after use. Shows scenarios with and without tools, and exception handling. ```cangjie class Worker <: Resource { var hasTools: Bool = false let name: String public init(name: String) { this.name = name } public func getTools() { println("${name} picks up tools from the warehouse.") hasTools = true } public func work() { if (hasTools) { println("${name} does some work with tools.") } else { println("${name} doesn't have tools, does nothing.") } } public func isClosed(): Bool { if (hasTools) { println("${name} hasn't returned the tool.") false } else { println("${name} has no tools") true } } public func close(): Unit { println("${name} returns the tools to the warehouse.") hasTools = false } } main() { try (r = Worker("Tom")) { r.getTools() r.work() } try (r = Worker("Bob")) { r.work() } try (r = Worker("Jack")) { r.getTools() throw Exception("Jack left, because of an emergency.") } } ``` -------------------------------- ### cjfmt File Formatting Example Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjfmt_manual.md Example of formatting a single Cangjie file and overwriting the source file. ```shell cjfmt -f ../../../test/uilang/Thread.cj ``` -------------------------------- ### Install Cangjie Toolchain Dependencies on UnionTech OS Server 20 Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/Appendix/linux_toolchain_install.md Use this command to install necessary development tools for the Cangjie toolchain on UnionTech OS Server 20. Ensure OpenSSL 3 is installed separately if not available. ```shell $ yum install \ binutils \ glibc-devel \ libstdc++-devel \ gcc \ ``` -------------------------------- ### Main function with Array parameter Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/package/entry.md Example of a `main` function that accepts an array of strings as arguments and returns Unit. It iterates through the arguments and prints each one. ```cangjie // main.cj main(args: Array): Unit { for (arg in args) { println(arg) } } ``` -------------------------------- ### Correct Example for -f Option Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjlint_manual.md This demonstrates the correct usage of the -f option, pointing to a 'src' directory. ```bash cjlint -f xxx/xxx/src ``` -------------------------------- ### Build Project with Verbose Output Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjpm_manual.md This example demonstrates building a Cangjie project with verbose output enabled, showing the underlying `cjc` compilation commands and the final success message. ```text Input: cjpm build -V Output: compile package module1.package1: cjc --import-path "target/release" --output-dir "target/release/module1" -p "src/package1" --output-type=staticlib -o libmodule1.package1.a compile package module1: cjc --import-path "target/release" --output-dir "target/release/bin" -p "src" --output-type=exe -o main cjpm build success ``` -------------------------------- ### Launch Debugger First, Then Load Program Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjdb_manual.md Use this method to start the debugger independently and then load the target program using the 'file' command. This is useful when you want to prepare the debugger environment before specifying the target. ```text ~/0901/cangjie_test$ cjdb (cjdb) file test Current executable set to '/0901/cangjie/test' (x86_64). (cjdb) ``` -------------------------------- ### Example of Tokens Creation and Debugging Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/Macro/Tokens_types_and_quote_expressions.md Shows how to construct a `Tokens` object with literal and operator tokens, and then print its contents for debugging. ```cangjie import std.ast.* let tks = Tokens( [ Token(TokenKind.INTEGER_LITERAL, "1"), Token(TokenKind.ADD), Token(TokenKind.INTEGER_LITERAL, "2") ] ) main() { println(tks) tks.dump() } ``` -------------------------------- ### Create and Open a File Instance Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/Basic_IO/basic_IO_source_stream.md Use `File.create` for write-only file instances or the `File` constructor with an `OpenMode` (e.g., `Read`, `Write`) for more specific access. Files created with `create` will throw an exception on read operations. ```cangjie // Create internal import std.fs.* internal import std.io.* main() { let file = File.create("./tempFile.txt") file.write("hello, world!".toArray()) // Open let file2 = File("./tempFile.txt", Read) let bytes = readToEnd(file2) // Reads all data println(bytes) } ``` -------------------------------- ### Install LLVM 16 via Homebrew Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/multiplatform/cjmp-pg-ios.md Install the required LLVM version using Homebrew. This is a prerequisite for the Objective-C interop features. ```bash brew install llvm@16 ``` -------------------------------- ### Running tests with a filter Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjpm_manual.md This example shows how to run all tests using a wildcard filter. The `--filter=*` option ensures that all tests are considered for execution. ```text cjpm test --filter=* ``` -------------------------------- ### Defining Packages for Import Examples Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/package/import.md These are sample package definitions used in the import examples. They define functions and classes that will be imported into other modules. ```cangjie // a.cj package p1 public func f1() {} ``` ```cangjie // d.cj package p2 public func f3() {} ``` ```cangjie // b.cj package p1 public func f2() {} ``` ```cangjie // c.cj package pkgc public func f1() {} ``` -------------------------------- ### TCP Server and Client Example Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/Net/net_socket.md Demonstrates a basic TCP server that accepts a connection, reads data, and a client that connects and sends data. Ensure the server port is correctly set or dynamically assigned. ```cangjie import std.time.* import std.sync.* import std.net.* var SERVER_PORT: UInt16 = 0 func runTcpServer() { try (serverSocket = TcpServerSocket(bindAt: SERVER_PORT)) { serverSocket.bind() SERVER_PORT = (serverSocket.localAddress as IPSocketAddress)?.port ?? 0 try (client = serverSocket.accept()) { let buf = Array(10, repeat: 0) let count = client.read(buf) // Data read by server: [1, 2, 3, 0, 0, 0, 0, 0, 0, 0] println("Server read ${count} bytes: ${buf}") } } } main(): Int64 { let future = spawn { runTcpServer() } sleep(Duration.millisecond * 500) try (socket = TcpSocket("127.0.0.1", SERVER_PORT)) { socket.connect() socket.write([1, 2, 3]) } future.get() return 0 } ``` -------------------------------- ### Construct and Use BufferedInputStream Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/Basic_IO/basic_IO_process_stream.md Demonstrates constructing a `BufferedInputStream` with a `ByteBuffer` and reading data into a byte array. Ensure the `io` package and `ByteBuffer` are imported. ```cangjie import std.io.{ByteBuffer, BufferedInputStream} main(): Unit { let arr1 = "0123456789".toArray() let byteBuffer = ByteBuffer() byteBuffer.write(arr1) let bufferedInputStream = BufferedInputStream(byteBuffer) let arr2 = Array(20, repeat: 0) /* Reads data from the stream and returns the length of the data read */ let readLen = bufferedInputStream.read(arr2) println(String.fromUtf8(arr2[..readLen])) // 0123456789 } ``` -------------------------------- ### Java Class Example Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/multiplatform/cangjie-android-Java.md An example of a Java class 'A' with a constructor and methods. This class serves as a basis for generating its corresponding Mirror Type in Cangjie. ```java package com.example.a; public class A { private static int lastId = 0; private int id = lastId++; private String name; public A(String name) { this.name = name; } protected final int getId() { return id; } public String getName() { return name; } } ``` -------------------------------- ### Example Target Configuration in CJPM Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjpm_manual.md Illustrates the structure of target-specific configurations in cjpm.toml, including package, dependencies, and debug/release modes for a specific target architecture. ```toml [package] compile-option = "compile-0" override-compile-option = "override-compile-0" link-option = "link-0" [dependencies] dep0 = { path = "./dep0" } [test-dependencies] devDep0 = { path = "./devDep0" } [target.x86_64-unknown-linux-gnu] compile-option = "compile-1" override-compile-option = "override-compile-1" link-option = "link-1" [target.x86_64-unknown-linux-gnu.dependencies] dep1 = { path = "./dep1" } [target.x86_64-unknown-linux-gnu.test-dependencies] devDep1 = { path = "./devDep1" } [target.x86_64-unknown-linux-gnu.bin-dependencies] path-option = ["./test/pro1"] [target.x86_64-unknown-linux-gnu.bin-dependencies.package-option] "pro1.xoo" = "./test/pro1/pro1.xoo.cjo" [target.x86_64-unknown-linux-gnu.debug] compile-option = "compile-2" override-compile-option = "override-compile-2" link-option = "link-2" [target.x86_64-unknown-linux-gnu.debug.dependencies] dep2 = { path = "./dep2" } [target.x86_64-unknown-linux-gnu.debug.test-dependencies] devDep2 = { path = "./devDep2" } [target.x86_64-unknown-linux-gnu.debug.bin-dependencies] path-option = ["./test/pro2"] [target.x86_64-unknown-linux-gnu.debug.bin-dependencies.package-option] "pro2.xoo" = "./test/pro2/pro2.xoo.cjo" ``` -------------------------------- ### Basic Try-Catch Example Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/error_handle/handle.md A simple example demonstrating how to catch a specific exception type (NegativeArraySizeException) and execute code within a finally block. ```cangjie main() { try { throw NegativeArraySizeException("I am an Exception!") } catch (e: NegativeArraySizeException) { println(e) println("NegativeArraySizeException is caught!") } println("This will also be printed!") } ``` -------------------------------- ### Output of LINQ Macro Example Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/Macro/practical_case.md This is the expected output when the 'linq' macro example is executed. It shows the squares of odd numbers from 1 to 10. ```text 1 9 25 49 81 ``` -------------------------------- ### Initialize a new Cangjie project with cjpm Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/first_understanding/hello_world.md Use the 'cjpm init' command to create a new Cangjie project structure, including a configuration file (cjpm.toml) and a default source file (src/main.cj). ```bash cjpm init ``` -------------------------------- ### Compare-and-Swap (CAS) Example Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/concurrency/sync.md Demonstrates the usage of the compareAndSwap method for atomic updates. It shows scenarios where CAS succeeds and fails based on the current value of the object. ```Go println(y1) var t2 = A() var y2 = obj2.compareAndSwap(t2, A()) // x and t1 are not the same object, CAS fails, y2: false println(y2) y2 = obj2.compareAndSwap(t1, A()) // CAS successes, y2: true println(y2) ``` -------------------------------- ### Usage Example of dprint2 Macro Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/Macro/practical_case.md This example shows how to call the dprint2 macro with multiple expressions. Ensure the 'define' macro package is imported. ```cangjie import define.* main() { let x = 3 let y = 2 @dprint2(x, y, x + y) } ``` -------------------------------- ### Start lldb-server on Android Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjdb_manual.md Before remote debugging on Android, start the `lldb-server` on the Android device. This command listens for incoming connections on the specified port. ```text adb shell /data/local/tmp/lldb-server platform --listen "*:1234" ``` -------------------------------- ### Basic cjpm bench Usage Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjpm_manual.md Demonstrates the basic usage of the cjpm bench command with and without a source path. ```text Input: cjpm bench Output: cjpm bench success ``` ```text Input: cjpm bench src Output: cjpm bench success ``` -------------------------------- ### Valid Raw Identifiers in Cangjie Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/basic_programming_concepts/identifier.md Examples of valid raw identifiers, which are regular identifiers or Cangjie keywords enclosed in backticks. This includes examples with keywords. ```text `abc` `_abc` `a1b2c3` `if` `while` `à֮̅̕b` ``` -------------------------------- ### Basic Project Build Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjpm_manual.md This is the simplest form of the `cjpm build` command, which compiles the project with default settings and indicates success. ```text Input: cjpm build Output: cjpm build success ``` -------------------------------- ### Get Source Line Number Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/Macro/builtin_compilation_flags.md Use @sourceLine() to get the line number of the source file. This tag can be used as a default value for function parameters. ```cangjie func test2(n!: Int64 = @sourceLine()) { /* at line 5 */ // The default value of `n` is the source file line number of the definition of `test2` println(n) // print 5 } ``` -------------------------------- ### Verify Cangjie Toolchain Installation Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/first_understanding/install.md Checks if the Cangjie toolchain has been installed correctly by querying the Cangjie compiler version. 'cjc' is the executable filename for the Cangjie compiler. ```bash cjc -v ``` -------------------------------- ### CJPM Feature Deduction Examples Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjpm_manual.md Examples of CJPM features that can be inferred from compilation options. Demonstrates short and full commands for running and building with feature deduction. ```bash # Short command cjpm run # Full command cjpm run --enable-features=feature.os.linux,feature.env.gnu ``` ```bash # Short command cjpm build --target=aarch64-linux-android # Full command cjpm build --target=aarch64-linux-android --enable-features=feature.arch.aarch64,feature.os.linux,feature.env.android ``` -------------------------------- ### Customized Option Configuration Example Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjpm_manual.md Example of defining custom options for the 'cjc' compiler within the 'profile.customized-option' section. These options are enabled via command-line flags. ```text [profile.customized-option] cfg1 = "--cfg=\"feature1=lion, feature2=cat\"" cfg2 = "--cfg=\"feature1=tiger, feature2=dog\"" cfg3 = "-O2" ``` -------------------------------- ### Launch Target Program Simultaneously Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjdb_manual.md Use this method to start the debugger and load the target program at the same time. Ensure the target executable is in the current directory or provide its full path. ```text ~/0901/cangjie_test$ cjdb test (cjdb) target create "test" Current executable set to '/0901/cangjie-linux-x86_64-release/bin/test' (x86_64). (cjdb) ``` -------------------------------- ### Instantiating Range Types Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/basic_data_type/range.md Demonstrates how to instantiate Range types with explicit start, end, step, and boundary inclusion parameters. ```cangjie // Range(start: T, end: T, step: Int64, hasStart: Bool, hasEnd: Bool, isClosed: Bool) let r1 = Range(0, 10, 1, true, true, true) // r1 contains 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 let r2 = Range(0, 10, 1, true, true, false) // r2 contains 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 let r3 = Range(10, 0, -2, true, true, false) // r3 contains 10, 8, 6, 4, 2 ``` -------------------------------- ### Debugging Macro Usage Example Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/Macro/macro_introduction.md This example shows how to use the `dprint` macro to print both the expression and its evaluated value during debugging. Macros are invoked using the `@` symbol. ```cangjie let x = 3 let y = 2 @dprint(x) // Prints "x = 3" @dprint(x + y) // Prints "x + y = 5" ``` -------------------------------- ### Cangjie Future Class Definition Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/concurrency/use_thread.md Defines the Future class with methods for retrieving thread results, including blocking get(), timed get(), and non-blocking tryGet(). ```cangjie public class Future { // Blocking the current thread, waiting for the result of the thread corresponding to the current Future object. // If an exception occurs in the corresponding thread, the method will throw the exception. public func get(): T // Blocking the current thread, waiting for the result of the thread corresponding to the current Future object. // If the corresponding thread has not completed execution within Duration, the method will throws TimeoutException. // If `timeout` <= Duration.Zero, its behavior is the same as `get()`. public func get(timeout: Duration): T // Non-blocking method that immediately returns Option.None if thread has not finished execution. // Returns the computed result otherwise. // If an exception occurs in the corresponding thread, the method will throw the exception. public func tryGet(): Option } ``` -------------------------------- ### Basic `cjc` Command Syntax Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/compile_and_build/cjc_usage.md Illustrates the general syntax for using the `cjc` compiler with options and source files. ```shell cjc [option] file... ``` -------------------------------- ### Get Source File Location Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/Macro/builtin_compilation_flags.md Use @sourceFile() to get the package name of the source file. This tag can be used within any expression that complies with type-checking rules. ```cangjie func test1() { let s: String = @sourceFile() // The value of `s` is the current source file name } ``` -------------------------------- ### Example Script Log Output Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/tools/source_en/cmd-tools/cjpm_manual.md This shows the expected content of the `script-log` file after `cjpm build` has executed the pre-build and post-build stages. ```text PRE-BUILD POST-BUILD ``` -------------------------------- ### Usage Example of LINQ Macro Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/Macro/practical_case.md This example demonstrates how to use the defined 'linq' macro with a specific query. It imports the macro package and calls the macro with a range and conditions. ```cangjie import define.* main() { @linq(from x in 1..=10 where x % 2 == 1 select x * x) } ``` -------------------------------- ### Install Ubuntu Dependencies Source: https://github.com/cangjielanguage/cangjie_docs/blob/main/docs/dev-guide/source_en/first_understanding/install.md Installs necessary dependency packages for the Cangjie toolchain on Ubuntu 18.04. Ensure glibc, Linux Kernel, and libstdc++ meet the specified version requirements. ```bash apt-get install binutils libc-dev libc++-dev libgcc-7-dev ```