一种受 Forth 启发的网站开发语言
A Forth-inspired language for writing websites

原始链接: https://robida.net/entries/2026/05/21/a-forth-inspired-language-for-writing-websites

作者创造了“Forge”,这是一种独特的、基于栈的编程语言,专为构建网站而设计。通过定义自定义的“单词”(words),开发人员能够以极少的代码生成 HTML 并实现微格式或交互元素等复杂功能。 Forge 采用了一种巧妙的混合渲染模型:它为搜索引擎爬虫和 WebMentions 使用服务端渲染,但在进行类似单页应用(SPA)的无缝导航体验时,会通过 Service Worker 切换到客户端渲染。 数据持久化通过简单的仅追加(append-only)JSONL 日志来处理,使开发人员能够直接在基于栈的范式内管理状态,例如“点赞”计数或表单提交。尽管目前仍处于实验阶段,但 Forge 因其非传统的 Web 开发方法而脱颖而出,将基于栈语言的局限性转化为了一种高效、独特的网站构建框架。

Hacker News 最新 | 过往 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 一种受 Forth 语言启发用于编写网站的编程语言 (robida.net) 7 分,由 speckx 发布于 29 分钟前 | 隐藏 | 过往 | 收藏 | 1 条评论 帮助 hvs 1 分钟前 [–] HN 流量洪流:https://web.archive.org/web/20260522134016/https://robida.ne... 回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请加入 YC | 联系 搜索:
相关文章

原文

I don't remember where the idea came from, but I decided that it would be cool if I could write websites using a stack-based language. Something like this:

: h1  ( s -- )  "<h1>" emit  .  "</h1>" emit ;

"Hello, World!" h1

So I wrote Forge.

I quickly built a library of word definitions that let me easily add microformats to the HTML:

: post-content
  "Hello, world! This is my first post with Forge!" p ;

: post-body
  h-entry-start
    "<p class='byline'>" emit
      "2026-05-21T14:00:00Z"  "May 21, 2026"  dt-published
      " · by " emit
      "Beto"  "/about"  p-author
    "</p>" emit
  h-entry-end

  "On building a tiny stack-based web language." p-summary
  "post-content"                                 e-content
  "/hello-world"  "permalink"                    u-url ;

"Hello, world!" "post-body" blog-post

Each site is a collection of pages, a library of words, and a stylesheet:

my-site
├── lib.forge
├── style.css
└── pages
    ├── about.forge
    ├── hello.forge
    └── notes.forge

A single binary runs the website:

forge --log forge.log my-site/

The binary does a lot. It has a webassembly compiler that generates HTML from .forge files. When you visit a page the compiler runs on the backend, and you get the actual HTML in the source code, as well as the original .forge source. But when you navigate between pages, a service worker captures the network request to the page, say, /notes, fetches the source /notes.forge), and builds the HTML on the fly by running the compiler on the browser. So we have server-side rendering for crawlers and WebMentions, and client-side rendering for a SPA experience.

I love the limitations of the language. You can persiste things to state, localStorage, or to an append-only log on the server. For example example, I can add a "like" button to posts like this:

: like-button  ( -- )
    "❤"  "do-like"  on-click ;

: do-like
  "1" "likes:demo" log-append ;

: body
  "I liked this!" p
  like-button ;

When you click it, appends the value "1" to the topic "likes:demo" in the log. It's just JSONL (one JSON document per line). Forms can submit to other .forge pages, and they simply put the contents in the stack for them. It's up to the target to use log-append to store in the backend.

I like how weird it is. I might use it for my site, who knows? For now I'm just exploring some ideas.

联系我们 contact @ memedata.com