← 都来码 / Dolaima← 例程索引python/demo/demo_std.py原文

python/demo/demo_std.py · 6241 字节 · 下载原文

# demo_std.py —— **第 1 层:标准输入输出**(Python 的 print)

# demo_std.py — **Layer 1: standard I/O** (Python's print)

#

# 这一层就是语言自己的标准输出:往 stdout 写文本。

# This layer is simply the language's own standard output: writing text to stdout.

# 也是最基础的一层,也是**唯一有逐字节确定性判据**的一层 —— 下面的输出

# It is also the most basic layer, and the **only one with a byte-for-byte deterministic criterion** — the output below

# 跑两遍完全一样(不读输入、不用随机数、不看时间)。

# is identical across two runs (it reads no input, uses no random numbers and never looks at the clock).

#

# ## 判据

# ## Criterion

#

#     vmlcli Examples/python/demo_std.py

#

# 期望 stdout 逐字节等于本文件末尾「期望输出」那段。

# Expect stdout to be byte-for-byte equal to the "expected output" block at the end of this file.

#

# ## ⚠ 本前端实测的三条限制(写 demo 时避开)

# ## ⚠ Three limits of this frontend measured in practice (avoid them when writing demos)

#

#   · **字符串里没有 `\xNN` / `\NNN` 转义** —— `PythonCompiler/Lexer.cs` 的

#   · **No `\xNN` / `\NNN` escape inside strings** — the escape

#     转义表只有 `\n \t \r \\ \' \"`,其余一律「丢掉反斜杠、留原字符」。

#     table in `PythonCompiler/Lexer.cs` only has `\n \t \r \\ \' \"`, everything else is "drop the backslash, keep the character".

#     所以 `"\x1b[31m"` 会打出字面量 `x1b[31m`。要 ESC 见 demo_tty.py。

#     So `"\x1b[31m"` prints the literal `x1b[31m`. For ESC see demo_tty.py.

#     ↳ 2026-10-04 复验仍成立:`"\x1b[31m"` 实测逐字节打出 `x1b[31m`。

#     ↳ Still true, re-checked 2026-10-04: `"\x1b[31m"` measures byte-for-byte `x1b[31m`.

#   · ~~**列表写不生效**(`b[i] = v` 之后读回来还是 0),所以本 demo 不碰列表。~~ ✅ **平铺写已修好(2026-10-04 复验)**:

#   · ~~**List writes have no effect** (after `b[i] = v` reading it back is still 0), so this demo does not touch lists.~~ ✅ **flat writes are fixed (re-checked 2026-10-04)**:

#     实测 `b[1] = 7` 读回 7、`b[i] = 9` 读回 9(常量/变量下标都对);**嵌套列表写仍不生效**(`g[0][1] = 5` 读回仍是 0)。

#     measured `b[1] = 7` reads 7 and `b[i] = 9` reads 9 (constant or variable subscript); **nested list writes still have no effect** (`g[0][1] = 5` still reads 0).

#     本 demo 不碰列表只是因为它已经写好、跑通了;能跑就别动,新写的代码里平铺列表直接写即可。

#     This demo avoids lists only because it is already written and working; existing code is fine as is, and new code may write flat lists directly.

#   · `chr(n)` 返回的是一个栈上临时缓冲的地址,**不是字符串** ⇒ `print(chr(65))`

#   · `chr(n)` returns the address of a temporary stack buffer, **not a string** ⇒ `print(chr(65))`

#     打出的是个地址数(当时实测 65516,2026-10-04 复验 1048556)。别用它拼字符串。

#     prints an address number (65516 measured then, 1048556 on 2026-10-04). Do not use it to build strings.

#

# 转义里能用的那几个(`\t` `\\` `\"`)正好压住了词法表那条路。

# The few escapes that do work (`\t` `\\` `\"`) happen to stay clear of that lexer path.


# ── 显式引用:下面用到的 `ui_*` 全部来自共享库 vmlui.vml ────────────

# ── explicit reference: every `ui_*` below comes from the shared library vmlui.vml ──

#    `Lib/python/vml_ui.py` 是 GenLib 从 `Lib/shared/src/vmlui.c` 生成的声明文件(勿手写),

#    `Lib/python/vml_ui.py` is the declaration file GenLib generates from `Lib/shared/src/vmlui.c` (do not hand-write);

#    前端读它登记名字与形参个数 —— 有了声明表,实参个数写错会在**编译期**报错。

#    the frontend reads it to register the names and their arities — with that table a wrong argument count is a **compile-time** error.

#    ↳ 2026-10-04 复验仍成立:`ui_scr_get_w(99)` 实测 `error: 'ui_scr_get_w' 需要 0 个参数,这里给了 1 个 [CodeGen_ArgCountMismatch]`。

#    ↳ Still true, re-checked 2026-10-04: `ui_scr_get_w(99)` measures `error: 'ui_scr_get_w' takes 0 arguments but 1 was given [CodeGen_ArgCountMismatch]`.

import vml_ui

lang = ui_scr_get_language()
print("=== demo_std (Python) ===")
if lang == 0: print("纯字符串一行")
if lang != 0: print("A plain string line")
if lang == 0: print("转义:制表\t反斜杠\\引号\"")
if lang != 0: print("escapes: tab\tbackslash\\quote\"")

a = 17
b = 25

print("a=", a, "b=", b)
print("a+b=", a + b, "a-b=", a - b, "a*b=", a * b)
print("a/b=", a / b, "a%b=", a % b)
if lang == 0: print("负数:", 0 - a, 0 - (a * b))
if lang != 0: print("negatives:", 0 - a, 0 - (a * b))

# 循环算一个结果,证明这一层和语言本身是通的

# Loop to compute a result, proving this layer and the language itself are wired together

i = 1
total = 0
while i <= 10:
    total = total + i * i
    i = i + 1
print("1^2+...+10^2 =", total)

# 九九表的一小段(多行)

# A small slice of the multiplication table (several lines)

i = 1
while i <= 5:
    print(i, "x 7 =", i * 7)
    i = i + 1

if lang == 0: print("=== 完成 ===")
if lang != 0: print("=== done ===")

# ── 期望输出(逐字节)────────────────────────────────────────────

# === demo_std (Python) ===

# 纯字符串一行

# A plain string line

# 转义:制表	反斜杠\引号"

# escapes: tab	backslash\quote"

# a= 17 b= 25

# a+b= 42 a-b= -8 a*b= 425

# a/b= 0.68 a%b= 17

# 负数: -17 -425

# negatives: -17 -425

# 1^2+...+10^2 = 385

# 1 x 7 = 7

# 2 x 7 = 14

# 3 x 7 = 21

# 4 x 7 = 28

# 5 x 7 = 35

# === 完成 ===

# === done ===

# ────────────────────────────────────────────────────────────────