Ccmmutty logo
Commutty IT
0 pv4 min read

小さい言語を作ってみた

https://picsum.photos/seed/f2b2aadd71af48e1a4144bec0c1049bb/1200/630
少し前にHaskellを勉強中にマークアップ言語を作ったので紹介します。
本当はすぐにアウトプットとして記事にしたかったのですが、 私にとってHaskellは難解で、モナドと格闘しているうちに季節が2回変わりました。
決して、先延ばし癖のせいではありません( ˘ω˘ )

作ったもの(ソースコード)
クオリティはコンパイラ(笑)ですがご容赦ください。
本気でやりたい人は https://www.cs.princeton.edu/~appel/modern/ など買ってください。

使い方

# クローン
git clone https://github.com/ryotarofr/zin_lang.git

# ビルド
cabal build

# インストール
cabal install

# 使用例
zin-compiler mock/example_simple.zin output.html

サンプルコード

拡張子は「zin」です。
h1 bold[0:15] : Getting Started with Zin

p : Welcome to the zin markup language documentation.
  : This guide will help you understand all the features.

h2 : Text Styling Examples

p bold[0:4] italic[5:9] strike[10:16] : Bold text sample here
p link[0:9] url[https://zin-lang.org] : Visit our website for more information.

ul : Simple and intuitive syntax
   : Powerful styling capabilities
   : Multiple output formats supported

cb lang[python]:
: def fibonacci(n)
:    if n <= 1:
:      return n
:    return fibonacci(n- fibonacci(n-2))

t : | Framework | Performance | Learning Curve |
r : | React | High | Medium |
r : | Vue | High | Low |
r : | Svelte | Very High | Low |

q italic[0:8] : Remember: simplicity is the ultimate sophistication.
cssは適当に作ってください。
↓↓ サンプルソースをスタイルなしで生成した場合は次のような見た目になります。
まあ、動けばいいかくらいの感覚でやってます。

アーキテクチャ

Zin コンパイラのモジュール構成:
  1. Lexer - 字句解析(テキスト → トークン)
  2. Parser - 構文解析(トークン → AST)
  3. Semantic - 意味解析と妥当性検証
  4. CodeGen - HTML 生成(直接変換)
  5. VirtualDOM - 仮想 DOM 経由での変換
  6. Renderer - レンダリング機能
zin → Lexer → Parser → Semantic → CodeGen/VirtualDOM → HTML

基本構文

tag  : text

タグ

対応してるhtmlタグ
h1,
h2,
h3,
h4,
p,
ul,
ol,
li,
table,
tr,
td,
th,
strong,
em,
del,
a,
blockquote,
pre,
code,

エラー通知

構文エラーがある場合はコンパイル時に通知してくれます。
例えば、サンプルコードの1行目をh100に変えます。
h100 bold[0:15] : Getting Started with Zin
実行
$ zin-compiler mock/example_simple.zin output.htm
Compiling mock/example_simple.zin -> output.htm
mock/example_simple.zin:1:1: error: Unexpected token: Text "h100 bold[0:15] : Getting Started with Zin"

コンパイラについて

コンパイルの種類は4つ用意しています。
  • FullCompilation -- 全体を毎回コンパイル
  • IncrementalOnly -- 変更部分のみコンパイル
  • SmartIncremental -- 依存関係も追跡してコンパイル
  • FastIncremental -- ハッシュベースでコンパイル(最速)
未完成ですが、今回は増分ビルド(差分のみをビルドするような仕組みを)検討しています。

余談

Haskellは難しいし、GHCは遅いし、実行速度もそんなに早くないが、 美しく、謎の中毒性がある言語です。挫折さえもで楽しむことができるのです。

Discussion

コメントにはログインが必要です。