ReefVM/examples/emoji.ts
2025-10-07 16:18:44 -07:00

98 lines
1.8 KiB
TypeScript

import { toBytecode, run } from "#reef"
// Example 1: Simple emoji variables
console.log("=== Emoji Variables ===")
const gems = toBytecode([
["PUSH", 5],
["STORE", "💎"],
["PUSH", 3],
["STORE", "🌟"],
["LOAD", "💎"],
["LOAD", "🌟"],
["ADD"],
["HALT"]
])
console.log("💎 (5) + 🌟 (3) =", (await run(gems)).value)
// Example 2: Money calculator
console.log("\n=== Money Calculator ===")
const money = toBytecode(`
PUSH 100
STORE 💰
PUSH 50
STORE 💵
PUSH 25
STORE 🪙
LOAD 💰
LOAD 💵
ADD
LOAD 🪙
ADD
HALT
`)
console.log("💰 (100) + 💵 (50) + 🪙 (25) =", (await run(money)).value)
// Example 3: Function with emoji parameters
console.log("\n=== Emoji Function ===")
const emojiFunc = toBytecode(`
MAKE_FUNCTION (🎯 🎨=50) .paint
STORE paint
JUMP .after
.paint:
LOAD 🎯
LOAD 🎨
MUL
RETURN
.after:
LOAD paint
PUSH 10
PUSH 1
PUSH 0
CALL
HALT
`)
console.log("paint(🎯=10, 🎨=50 default) =", (await run(emojiFunc)).value)
// Example 4: Unicode variables (Japanese)
console.log("\n=== Unicode Variables ===")
const japanese = toBytecode([
["PUSH", 42],
["STORE", "数字"],
["PUSH", 8],
["STORE", "ラッキー"],
["LOAD", "数字"],
["LOAD", "ラッキー"],
["ADD"],
["HALT"]
])
console.log("数字 (42) + ラッキー (8) =", (await run(japanese)).value)
// Example 5: Loop with emoji
console.log("\n=== Emoji Loop ===")
const loop = toBytecode([
["PUSH", 0],
["STORE", "🔢"],
[".🔁:"],
["LOAD", "🔢"],
["PUSH", 5],
["LT"],
["JUMP_IF_FALSE", ".🛑"],
["LOAD", "🔢"],
["PUSH", 1],
["ADD"],
["STORE", "🔢"],
["JUMP", ".🔁"],
[".🛑:"],
["LOAD", "🔢"],
["HALT"]
])
console.log("Count from 0 to 5 with 🔢:", (await run(loop)).value)