Top Banner
瀬尾佳隆 ([email protected]) Microsoft MVP for Visual C# 1 技術ひろば.net 20107
25

Rpn and forth 超入門

Jul 01, 2015

Download

Technology

Yoshitaka Seo

Rpn and forth 超入門

技術ひろば.net 2010年7月勉強会 セッション資料
2010年7月10日開催
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Rpn and forth 超入門

瀬尾佳隆 ([email protected]) Microsoft MVP for Visual C#

1

技術ひろば.net 2010年7月

Page 2: Rpn and forth 超入門

瀬尾 佳隆 (せお よしたか) ◦ MVP for Visual C# (Jan 2009 – Dec 2010)

◦ 実は Forth 系の言語が大好き!

◦ メールは [email protected] / Twitterは @seosoft

◦ 個人事業主です(屋号は瀬尾ソフト)

◦ 根っからの開発屋

インフラ苦手です ◦ どうでもいい情報としては、ミュージカルが大好きです

2

Page 3: Rpn and forth 超入門

真面目に聞いていただくセッションではありません ◦ 「何か身につく」とか「仕事に役に立つ」とか期待しては いけません でも、普段とは違う「物の見方」を通して視野を広げる参考になれば

多分途中で時間切れになります ◦ (意図的に)ページ数多すぎ

そもそも30分でやるようなネタじゃないし w

◦ ついうっかり興味を持ってしまった方は資料をダウンロード して読んでください

3

Page 4: Rpn and forth 超入門

明日の仕事に全く役立たない技術でも 「楽しければいいじゃん!」ということを理解 (または同情)していただくこと

普段とは違う考え方を通して脳のストレッチをして いただくこと

ついうっかり興味を持つ人が一人でも二人でも 出てくることを密かに期待すること

4

Page 5: Rpn and forth 超入門

RPN ◦ RPNとは

◦ RPNの具体例

◦ 計算機内部の動き

◦ RPNの言語的特徴

Forth ◦ Forthとは

◦ Forthの基本的な文法

◦ (デモ)

5

Page 6: Rpn and forth 超入門

逆ポーランド記法

6

Page 7: Rpn and forth 超入門

RPN = Reverse Polish Notation ◦ 逆ポーランド記法

演算子をオペランドの後ろに記述する方法 ◦ 後置記法とも言う

◦ 皆さんお馴染みなのは中間記法

HP電卓ではRPNで計算します ◦ HP-15Cなんて名機も

7

Page 8: Rpn and forth 超入門

中置記法 RPN

1 + 2 1 2 +

(1 + 2) * (3 + 4) 1 2 + 3 4 + *

8.33 ∗ 4 − 5.2 ÷ 8.33 − 7.46 ∗ 0.32

4.3 ∗ 3.15 − 2.75 − (1.71 ∗ 2.01)

8.33 4 5.2 - * 8.33 7.46 - / 0.32 * 4.3 3.15 2.75 - * 1.71 2.01 * - /

8

• 頭から順番に計算できる • 括弧がいらない • 複雑な式でも1行で表現できる

Page 9: Rpn and forth 超入門

20 4 / の場合・・・20 / 4

9

Starting Forth http://www.forth.com/starting-forth/sf2/sf2.html

Page 10: Rpn and forth 超入門

20 4 / の場合・・・20 / 4

10

Starting Forth http://www.forth.com/starting-forth/sf2/sf2.html

Page 11: Rpn and forth 超入門

3 9 + 4 6 + * の場合・・・(3 + 9) * (4 + 6)

11

Starting Forth http://www.forth.com/starting-forth/sf2/sf2.html

Page 12: Rpn and forth 超入門

3 9 + 4 6 + * の場合・・・(3 + 9) * (4 + 6)

12

Starting Forth http://www.forth.com/starting-forth/sf2/sf2.html

Page 13: Rpn and forth 超入門

問題: 太郎君は鉛筆を1本、花子さんは鉛筆を2本持っています。 あわせて何本でしょうか。

13

1 + 2

1 2 +

鉛筆1本 鉛筆2本 あわせて何本

中間記法

RPN

順番が・・・

あら、キレイ

Page 14: Rpn and forth 超入門

問題: 太郎君は鉛筆を1本、花子さんは鉛筆を2本持っています。 あわせて何本でしょうか。

14

1 + 2 1 2 + 中間記法 RPN

1 足す 2 1と2を足す

日本語に似ている!

Page 15: Rpn and forth 超入門

RPNに基づくプログラミング言語

15

Page 16: Rpn and forth 超入門

RPNとデータスタック使用の考え方に基づいた プログラミング言語

言語仕様が非常に簡単 ◦ ワード(トークン)を辞書中で検索

◦ 辞書中に見つかったら関連づけられたコードを実行

◦ 見つからなかったら “数” と見なしてスタックに積む

Forth プログラムは「ワードの並び」 ◦ Forth ではすべてがワードであり、演算子、制御構造、 組み込みの手続き、ユーザー定義の手続きの区別がない

◦ ワードが順番に並んでいるだけで、構文解析が不要

16

Page 17: Rpn and forth 超入門

スタック操作 処理

. スタックの1番目を取り出してコンソール出力

.s スタックの内容表示(値を取り出さない)

clearstack スタックのクリア

drop スタックの1番目を捨てる

dup スタックの1番目を複製して上に積む

over スタックの2番目を複製して上に積む

swap スタックの1番目と2番目とを交換する

rot スタックの3番目を取り出して上に積む

nip swap drop と同じ

tuck swap over と同じ

2drop drop drop と同じ

2swap 1番目/2番目の組と3番目/4番目の組とを交換する

17

Page 18: Rpn and forth 超入門

四則演算 ◦ + - * / mod ・・・想像通り

文字列 ◦ .s” 文字列” ・・・ .s” のあとに半角スペースが必要

ワード定義 ◦ : <ワード名> <コード> ;

コロンで始まりセミコロンで終わる

制御構造 ◦ <条件> if <処理> else <処理> then ・・・if文 ◦ <終値> <初期値> ?do <処理> loop ・・・for文 ◦ begin <処理> <終了条件> until ・・・while文

18

Page 19: Rpn and forth 超入門

変数 ◦ variable <変数名> ・・・定義

◦ <変数名> ! ・・・代入

◦ <変数名> @ ・・・参照

浮動小数 ◦ f+, f-, f*, f/ など ・・・頭に f を付ける必要がある

◦ d>f, f>d ・・・小数化、整数化

19

Page 20: Rpn and forth 超入門

時間があれば・・・

20

Page 21: Rpn and forth 超入門

今日のまとめと参考情報

21

Page 22: Rpn and forth 超入門

RPN ◦ 演算子をオペランドの後ろに記述する

◦ 頭から順番に計算していくことができる

◦ 日本語の文法に近いので日本人には実は案外理解しやすい 言語(かも?)

Forth ◦ RPNとデータスタックを使ったプログラミング言語

◦ 言語仕様が非常に簡単

◦ Forth プログラムは「ワードの並び」

22

Page 23: Rpn and forth 超入門

HP-35s が今なら5000円くらいで買えるかも ◦ Amazonあたりで検索してください

Windows上で動作する HP-15Cのエミュレータが あります ◦ http://www.thgsoft.ch/ThGHome_EN.html#HP15c

23

Page 24: Rpn and forth 超入門

Gforth ◦ ANS Forthの実装 ◦ http://sourceforge.jp/projects/freshmeat_gforth/

Starting FORTH ◦ http://www.forth.com/starting-forth/

Thinking FORTH ◦ http://thinking-forth.sourceforge.net/

日本語プログラミング言語Mind ◦ http://www.scripts-lab.co.jp/mind/whatsmind.html

◦ 実は私にとっての Forth とは Mind のこと

.NET用のForthコンパイラもあります ◦ DeltaForth / Forth in the .Net 3.5 Framework (ForthAPI)

24

Page 25: Rpn and forth 超入門

25