0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【妄想】プログラミング言語HTML

Last updated at Posted at 2025-01-30

はじめに

タグが多すぎて困っちゃう幻のプログラミング言語HTMLの基本的な文法を解説します。
なお、HTMLというよりかはXMLです。

コンパイル方法

htmlc [ソースファイル名] [実行ファイル名]

また以下のコマンドで実行することもできます。

htmlc -execute [ソースファイル名]

基本文法

<タグ名>値 値 値 …</タグ名>

プログラミング言語HTMLでは関数などのことをタグとも呼びます。
すべてのタグは最後に評価された値を返します。
(何も評価されずにタグが終了した場合はfalseが返ります。)

Hello, World!

<print>"Hello, World!"</print>

基本的な演算

<!-- 足し算 -->
<plus>1 2</plus>   <!-- 3 -->
<plus>1 2 3</plus> <!-- 6 -->

<!-- 引き算 -->
<minus>6 2</minus>   <!-- 4 -->
<minus>6 2 1</minus> <!-- 3 -->

<!-- 掛け算 -->
<times>2 3</times>   <!--  6 -->
<times>2 3 4</times> <!-- 24 -->

<!-- 割り算 -->
<divide>12 2</divide>   <!-- 6 -->
<divide>12 2 3</divide> <!-- 2 -->

<!-- 割り算の余り -->
<mod>26 7</mod>   <!-- 5 -->
<mod>26 7 2</mod> <!-- 1 -->

分岐

<if>条件式 真の処理 偽の処理</if>
<when>条件式 真の処理</when>
<unless>条件式 偽の処理</unless>

if-else-ifとしたい場合はcondタグを使います。
上から順に奇数番目の引数の条件式が真か判定し、真ならば紐づけられた真の処理を実行し、処理を終えます。

<cond>
  条件式 真の処理
  条件式 真の処理
  条件式 真の処理
  …
  true 真の処理
</cond>

ブロック文

複数の処理をまとめたい場合は以下のようにします。

<block>
  処理
  処理
  処理
  …
</block>

ループ

<loop>処理</loop>

ループから抜けるときはbreakタグを使います。

<loop>
  処理
  <break>戻り値</break>
</loop>

変数

定義

<defparam>変数名 初期値</defparam>

複数同時に定義することもできます。

<defparam>
  変数名 初期値
  変数名 初期値
  変数名 初期値
  …
</defparam>

代入

<setparam>変数名 値</setparam>

複数同時に代入することもできます。

<setparam>
  変数名 値
  変数名 値
  変数名 値
  …
</setparam>

リスト

定義

<deflist>
  変数名
  要素 要素 要素 …
</deflist>

要素を取り出す

<nth>変数名 インデックス</nth>

要素を上書く

<setparam>
  <nth>変数名 インデックス</nth></setparam>

関数

定義

<defun 関数名 引数 引数 引数 >
  処理
</defun>

途中で戻り値を返したい場合はreturnタグを使います。

<defun 関数名 引数 引数 引数 >
  処理
  <return>戻り値</return>
  処理
</defun>

エラー

0割りなどのエラーが発生するとどうなるのかは分かりません。
気を付けてください。

おまけ:FizzBuzz

<defparam>
  num 1
  num-max 100
</defparam>

<loop>
  <cond>
    <!-- if (num % 15 == 0) -->
    <eq>
      <mod>num 15</mod>
      0
    </eq>
    <print>"fizzbuzz"</print>

    <!-- else if (num % 3 == 0) -->
    <eq>
      <mod>num 3</mod>
      0
    </eq>
    <print>"fizz"</print>

    <!-- else if (num % 5 == 0) -->
    <eq>
      <mod>num 5</mod>
      0
    </eq>
    <print>"buzz"</print>

    <!-- else -->
    true
    <print>num</print>
  </cond>
  
  <setparam>
    num
    <plus>num 1</plus>
  </setparam>

  <!-- if (num > num-max) -->
  <when>
    <gt>num num-max</gt>
    <break/>
  </when>
</loop>

追記

HTMLというよりXMLだった…。
HTMLのタグを使用する方針にしたほうがよかった(ブラウザでも見れるし)。
loopはmarqueeタグにしたほうがオシャレかも。

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?