Skip to content

Simple

熟悉的语法,明确的语义。

先读懂、运行、修改一个小程序,再看 Norm 如何表达应用,以及如何帮助 Agent 理解代码。

simple.norm
main() {
  String language = "Norm"
  List<String> steps = ["Read", "Run", "Change"]

  printLine("Hello, ${language}")
  for step : steps {
    printLine(step)
  }
}

运行 norm simple.norm → Hello, Norm Read Run Change

Agent first

给 Agent 可以查询、可以验证的代码事实。

@Document 描述声明意图,用受检查的引用连接相关 API。编译器提供签名与类型,测试关联来自 @Test。

agent_document.norm
import std.annotation.Document
import std.testing.Test

@Document(description: "A task visible to the user.")
value Task {
  String title
}

@Document(description: "Formats a task title for a summary.")
String describe(Task task) {
  return "Task: " + task.title
}

@Test(functions: [describe.function])
Void describesTask() {
  require(
    condition: describe(Task(title: "Learn Norm")) == "Task: Learn Norm",
    message: "task summary"
  )
}

@Document(
  description: "Entry point for agents exploring task summaries.",
  types: [Task.class],
  functions: [describe.function]
)
class TaskApi {}

main() {
  printLine(describe(Task(title: "Learn Norm")))
}
  1. 理解norm query agent_document.norm describe --source --references --testsnorm query agent_document.norm TaskApi --source
  2. 预览norm refactor name agent_document.norm describe --to summarize --preview
  3. 验证norm check agent_document.norm --format jsonnorm test agent_document.norm --format json

查询结果节选describe.documentation: Formats a task title for a summary. describe.references: 4 describe.tests: 1 TaskApi.documentation: Entry point for agents exploring task summaries.

程序输出Task: Learn Norm

这里的语义查询和重构需要开发版工具链。Agent 在验证前应用预览中的编辑。

了解 Agent 工具 →

Application development

让应用规则直接写在代码里。

每个示例都是可独立运行的完整文件,旁边展示经过验收的结果。选择想了解的问题。

把身份、值、有限状态、命名调用和表达式结果组合在一起。

application_data.norm
interface Labeled {
  String label()
}

value TaskId {
  Integer number
}

enum State {
  Ready,
  Doing(Integer percent),
  Done
}

class Task implements Labeled {
  TaskId id
  String title
  State state

  String label() {
    String progress = switch state {
      case Ready { break "ready" }
      case Doing(Integer percent) { break "${percent}%" }
      case Done { break "done" }
    }
    if state == State.Done { "${title} ✓" } else { "${title}: ${progress}" }
  }
}

Task newTask(String title, Integer number, State state = State.Ready) {
  Task(id: TaskId(number: number), title: title, state: state)
}

main() {
  Task task = newTask(title: "Learn Norm", number: 7)
  Task same = task
  printLine(task.label())
  same.state = State.Doing(percent: 50)
  printLine(task.label())
  same.state = State.Done
  printLine(task.label())
}

运行 norm application_data.norm

验收输出Learn Norm: ready Learn Norm: 50% Learn Norm ✓

这些示例需要开发版工具链。

继续了解语言特性 →

从一个特性开始。

每节教学都有可执行示例,并由编译器持续验收。

Norm 0.24 开发线