GET STARTED

HP 上手教程HP Tutorial

如果你会 Python,十分钟就能写出第一段 HP。下面从安装到所有权,循序渐进。If you know Python, you'll write HP in ten minutes. From install to ownership, step by step.

安装与编译Install & Compile

下载 hpc 编译器,确保系统已配置 LLVM / MSVC 工具链(Windows)或等价工具链(Linux/macOS)。Get hpc and ensure an LLVM / MSVC (Windows) or equivalent toolchain is configured.

terminal
$ hpc -O3 -o app.exe app.hp
$ ./app.exe

Hello, WorldHello, World

hello.hp
module {
    "<std.io>"
}

fn main() {
    std.io.print("Hello, HP!")
}

变量、类型与运算Variables, Types & Math

basics.hp
a = 10
b: i32 = 20
c = a + b * 2
name = "HP"
msg = f"Hello, {name}!  sum={c}"
print(msg)

提示:f-string 在编译期展开为字符串拼接,运行时零格式化开销。Tip: f-strings expand to string concatenation at compile time — zero formatting cost at runtime.

控制流Control Flow

flow.hp
for i in 1..6:
    if i % 2 == 0:
        print(f"{i} 是偶数")
    else:
        print(f"{i} 是奇数")

n = 0
while n < 3:
    print(n)
    n += 1

函数与闭包Functions & Closures

fn.hp
fn greet(name: str, prefix: str = "Hi") -> str {
    return f"{prefix}, {name}"
}

make = fn(base) { return fn(x) { return x + base } }
add10 = make(10)
print(add10(5))   # 15

结构体与接口Structs & Interfaces

oop.hp
struct Vec2 { x: num; y: num }

impl Vec2 {
    fn len(self) -> num { return (self.x**2 + self.y**2) ** 0.5 }
}

v = Vec2 { x: 3, y: 4 }
print(v.len())   # 5.0

match 与 switchmatch & switch

pat.hp
enum Status { Ok, Warn, Err }

fn label(s: Status) -> str {
    return match s {
        Status::Ok   => "成功"
        Status::Warn => "警告"
        Status::Err  => "错误"
    }
}

推导式与管道Comprehensions & Pipeline

comp.hp
nums = [1, 2, 3, 4, 5, 6]
doubled = nums |> map(fn(x) { return x * 2 })
                |> filter(fn(x) { return x > 5 })
# [12]

squares = [x * x for x in nums if x % 2 == 0]  # [4, 16, 36]

所有权:移动与借用Ownership: Move & Borrow

own.hp
data = [1, 2, 3]
owned = move data      # data 此后不可用
view  = &owned         # 只读借用
print(view[0])

fn take(s: str) { print(s) }
msg = "hi"
take(move msg)         # 显式转移所有权

零运行时:所有权转移与借用检查完全在编译期完成,运行时没有引用计数或 GC。Zero-runtime: ownership transfer and borrow-checking happen entirely at compile time — no refcount or GC at runtime.

想看真实代码?Want real code?

示例库收录了结构体、枚举、match、switch、接口、宏、defer、命名空间等真实 .hp 文件。The example corpus includes struct, enum, match, switch, interface, macro, defer and namespace .hp files.