Skip to content
Wesley Yan S. B. edited this page May 24, 2025 · 2 revisions

Bytecharge Programming Language


Table of Contents

  1. Introduction
  2. Getting Started
  3. Language Syntax
  4. Native Functions
  5. Compilation and Execution
  6. Advanced Features
  7. Standard Library and Modules
  8. Examples
  9. Command Line Interface (CLI)
  10. Appendix: Built-in Types

Introduction

Bytecharge is a fast, safe, and multi-purpose programming language designed for high productivity and performance. It compiles to C and then to native code, offering a rich set of native functions for system, math, string, file, network, concurrency, and more. Bytecharge is suitable for scripting, automation, systems programming, and rapid prototyping.


Getting Started

Installation

Bytecharge is distributed as a Python script (main.py). You need Python 3.7+ and GCC (or compatible C compiler) installed.

  1. Place main.py in your working directory.
  2. Write your Bytecharge code in files with the .charge extension.

Running a Program

Bytecharge --run your_program.charge

Compiling to Native Executable

Bytecharge --compile your_program.charge -o output_binary

Language Syntax

Comments

  • Single-line comments:
    // This is a comment
  • Multi-line comments:
    /* 
       This is a 
       multi-line comment 
    */
    

Variables and Types

Declare variables using let:

let x: int = 42;
let name: string = "Bytecharge";
let pi: float = 3.14;
let flag: bool = true;

Supported types:

  • int
  • float
  • double
  • string or str
  • bool
  • list, dict, tuple, set, bytes, object (for advanced/native use)

Functions

Define functions with fn:

fn add(a: int, b: int) -> int {
    return a + b;
}
  • Arguments are typed.
  • Return type is specified after ->.
  • If omitted, defaults to void.

Example without explicit return type:

fn greet(name: string) {
    print("Hello, " + name);
}

Control Flow

Note: The current parser does not natively support if, for, or while constructs, but you can use native functions and C-style statements in function bodies.

Imports and Modules

Import other Bytecharge modules:

import math;
import utils;
  • Imports look for math.charge or math.h in the current directory.
  • Modules are compiled automatically when imported.

Native Functions

Bytecharge provides a rich set of native functions, all prefixed with bc_. They cover system, math, string, file, network, concurrency, and more.

System and Process

  • bc_detect_os(): Returns the OS name.
  • bc_exec_shell(cmd): Executes a shell command.
  • bc_hostname(), bc_ip(), bc_ip_public(): Host and IP info.
  • bc_ping(host, count=1, timeout=1): Ping a host.
  • bc_cpu_count(), bc_memory_info(), bc_swap_info(): System info.
  • bc_process_list(), bc_process_kill(pid), bc_uptime(): Process management.
  • bc_env(), bc_env_get(key), bc_env_set(key, value): Environment variables.
  • bc_is_admin(): Checks for admin/root privileges.
  • bc_reboot(), bc_shutdown(): System control.

Math and Statistics

  • bc_randint(a, b), bc_random(), bc_seed(seed): Random numbers.
  • bc_sqrt(x), bc_pow(x, y), bc_log(x, base), bc_exp(x): Math functions.
  • bc_mean(*args), bc_median(*args), bc_stdev(*args), bc_variance(*args), bc_mode(*args): Statistics.
  • bc_gcd(a, b), bc_lcm(a, b), bc_factorial(x): Integer math.
  • bc_round(x, ndigits), bc_floor(x), bc_ceil(x), bc_trunc(x): Rounding.
  • bc_sin(x), bc_cos(x), bc_tan(x), ...: Trigonometry.

String and Encoding

  • bc_str_upper(s), bc_str_lower(s), bc_str_title(s), ...: String manipulation.
  • bc_base64_encode(s), bc_base64_decode(s): Base64.
  • bc_url_encode(s), bc_url_decode(s): URL encoding.
  • bc_html_escape(s), bc_html_unescape(s): HTML.
  • bc_md5(s), bc_sha1(s), bc_sha256(s), bc_sha512(s): Hashing.
  • bc_uuid4(): Generate UUID.

File and Compression

  • bc_file_read(path), bc_file_write(path, data), bc_file_append(path, data): File IO.
  • bc_file_exists(path), bc_file_size(path), bc_file_lines(path): File info.
  • bc_file_copy(src, dst), bc_file_move(src, dst), bc_file_remove(path): File operations.
  • bc_file_mkdir(path), bc_file_rmdir(path), bc_file_listdir(path): Directory operations.
  • bc_gzip_compress(data), bc_gzip_decompress(data): Compression.
  • bc_bz2_compress(data), bc_bz2_decompress(data), bc_lzma_compress(data), bc_lzma_decompress(data), bc_zlib_compress(data), bc_zlib_decompress(data): More compression.

Networking and HTTP

  • bc_http_get(url, timeout), bc_http_post(url, data, headers, timeout), bc_http_head(url, timeout): HTTP requests.
  • bc_tcp_connect(host, port, timeout), bc_tcp_send(host, port, data, timeout), bc_tcp_recv(host, port, bufsize, timeout): TCP.
  • bc_dns_lookup(host), bc_port_open(host, port, timeout): DNS and port checking.

Concurrency and Threading

  • bc_thread_start(target, *args): Start a thread.
  • bc_thread_count(), bc_thread_id(), bc_thread_name(), bc_thread_set_name(name): Thread info.
  • bc_sleep(seconds): Sleep.
  • bc_lock(), bc_rlock(), bc_event(), bc_semaphore(value): Synchronization.

Data Structures

  • bc_list_sort(lst), bc_list_reverse(lst), bc_list_unique(lst), bc_list_flatten(lst), bc_list_shuffle(lst), bc_list_sample(lst, k): List operations.
  • bc_dict_keys(d), bc_dict_values(d), bc_dict_items(d), bc_dict_get(d, key, default), bc_dict_set(d, key, value), bc_dict_has(d, key), bc_dict_update(d, other): Dictionary operations.
  • bc_tuple(*args), bc_set(*args): Tuples and sets.
  • bc_heap_push(heap, item), bc_heap_pop(heap): Heap operations.
  • bc_counter(lst): Counter.

Serialization and Parsing

  • bc_pickle(obj), bc_unpickle(data): Pickle serialization.
  • bc_json_loads(s), bc_json_dumps(obj): JSON.
  • bc_csv_read(path), bc_csv_write(path, rows), bc_csv_dict_read(path), bc_csv_dict_write(path, rows): CSV.
  • bc_xml_parse(s), bc_xml_tostring(tag, attrib, text), bc_xml_prettify(s): XML.

Compilation and Execution

Running a Bytecharge Program

Bytecharge --run program.charge
  • Compiles the .charge file to C, then to a native executable, and runs it.
  • Temporary files are cleaned up automatically.

Compiling to Executable

Bytecharge --compile program.charge -o myprogram
  • Produces a native executable (myprogram).

Advanced Features

  • Module System: Import and compile modules automatically.
  • Performance Profiling: Use bc_timeit, bc_profile_func, bc_benchmark_native, bc_benchmark_code.
  • System Control: Daemonization (bc_daemonize), forking (bc_fork), process priority and affinity.
  • Terminal Utilities: TTY, terminal size, etc.
  • Serialization: Pickle, JSON, CSV, XML.
  • Compression: gzip, bz2, lzma, zlib.
  • Networking: HTTP, TCP, DNS, port scanning.
  • Threading and Synchronization: Locks, semaphores, events.

Standard Library and Modules

  • Place .charge files in the same directory to use as modules.
  • Import with import modulename;.
  • Standard library modules can be listed with:
    Bytecharge --list-stdlib

Examples

Hello World

fn main() {
    print("Hello, Bytecharge!");
}

Using Native Functions

fn main() {
    let os: string = detect_os();
    print("Running on: " + os);
}

Function with Arguments

fn add(a: int, b: int) -> int {
    return a + b;
}

fn main() {
    let result: int = add(5, 7);
    print("Sum: " + result);
}

Command Line Interface (CLI)

Run Bytecharge --help for all options.

  • --run file.charge : Run a Bytecharge program.
  • --compile file.charge -o output : Compile to executable.
  • --version : Show version.
  • --list-natives : List all native functions.
  • --list-modules : List available modules.
  • --list-examples : List example programs.
  • --list-features : List language features.
  • --list-types : List built-in types.
  • --list-stdlib : List standard library modules.
  • --list-benchmarks : List benchmarkable native functions.
  • --list-advanced : List advanced features.
  • --list-encoding : List encoding/decoding functions.
  • --list-math : List math/statistics functions.
  • --list-io : List file and IO functions.
  • --list-network : List network functions.
  • --list-concurrency : List concurrency/threading functions.

Appendix: Built-in Types

  • int : Integer
  • float : Floating point
  • double : Double precision float
  • string / str : String
  • bool : Boolean
  • list : List/Array
  • dict : Dictionary/Map
  • tuple : Tuple
  • set : Set
  • bytes : Byte array
  • object : Generic object

Notes

  • Bytecharge is statically typed, but type inference is possible in some cases.
  • The language is designed to be simple, safe, and fast, with a focus on practical programming.
  • The parser and code generator are extensible; you can add more native functions or language features as needed.

Bytecharge is a powerful tool for both beginners and advanced users, offering a unique blend of scripting simplicity and native performance.
For more examples and updates, check the project repository or use the CLI to explore available features.