2025-09-22 07:54:25 +09:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
|
|
# Dev sugar pre-expander (safe, zero-semantics changes)
|
|
|
|
|
# Applies, in order:
|
|
|
|
|
# - @name[: Type] = expr => local name[: Type] = expr
|
|
|
|
|
# - elif cond { => else if cond {
|
|
|
|
|
# - a += b (and -=, *=, /=) as statements
|
|
|
|
|
# - i++ / i-- as statements (not expressions)
|
|
|
|
|
# Usage:
|
2025-11-06 15:41:52 +09:00
|
|
|
# tools/dev/dev_sugar_preexpand.sh < input.hako > output.hako
|
|
|
|
|
# or tools/dev/dev_sugar_preexpand.sh input.hako > output.hako
|
2025-09-22 07:54:25 +09:00
|
|
|
|
|
|
|
|
in="${1:-}"
|
|
|
|
|
if [ -n "$in" ]; then
|
|
|
|
|
exec <"$in"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# 1) @ local alias
|
|
|
|
|
"$(dirname "$0")/at_local_preexpand.sh" | \
|
|
|
|
|
|
|
|
|
|
# 2) compound assignments (+=, -=, *=, /=), statements only; skip comment lines
|
|
|
|
|
sed -E \
|
|
|
|
|
-e '/^[[:space:]]*\/\//b' \
|
|
|
|
|
-e 's/^([[:space:]]*)([A-Za-z_][A-Za-z0-9_]*)[[:space:]]*\+=\s*(.+)$/\1\2 = \2 + \3/' \
|
|
|
|
|
-e 's/^([[:space:]]*)([A-Za-z_][A-Za-z0-9_]*)[[:space:]]*\-=\s*(.+)$/\1\2 = \2 - \3/' \
|
|
|
|
|
-e 's/^([[:space:]]*)([A-Za-z_][A-Za-z0-9_]*)[[:space:]]*\*=\s*(.+)$/\1\2 = \2 * \3/' \
|
|
|
|
|
-e 's/^([[:space:]]*)([A-Za-z_][A-Za-z0-9_]*)[[:space:]]*\/=\s*(.+)$/\1\2 = \2 \/ \3/' | \
|
|
|
|
|
|
|
|
|
|
# 3) i++ / i-- as statements, skip comment lines
|
|
|
|
|
sed -E \
|
|
|
|
|
-e '/^[[:space:]]*\/\//b' \
|
|
|
|
|
-e 's/^([[:space:]]*)([A-Za-z_][A-Za-z0-9_]*)[[:space:]]*\+\+[[:space:]]*$/\1\2 = \2 + 1/' \
|
|
|
|
|
-e 's/^([[:space:]]*)([A-Za-z_][A-Za-z0-9_]*)[[:space:]]*--[[:space:]]*$/\1\2 = \2 - 1/' | \
|
|
|
|
|
|
|
|
|
|
# 4) when cond { ... } → if cond { ... } (line-head only), skip comments
|
|
|
|
|
sed -E '/^[[:space:]]*\/\//b; s/^([[:space:]]*)when\b/\1if/' | \
|
|
|
|
|
|
|
|
|
|
# 5) print! expr → print(expr) (line-head only; allow zero or more spaces after '!')
|
|
|
|
|
sed -E '/^[[:space:]]*\/\//b; s/^([[:space:]]*)print![[:space:]]*(.+)$/\1print(\2)/'
|