{"id":3939,"date":"2014-01-28T09:58:31","date_gmt":"2014-01-28T00:58:31","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=3939"},"modified":"2018-11-14T16:33:52","modified_gmt":"2018-11-14T07:33:52","slug":"forth%e3%82%92%e4%bd%9c%e3%82%8b%e3%81%ae%e3%81%98%e3%82%83","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2014\/01\/28\/forth%e3%82%92%e4%bd%9c%e3%82%8b%e3%81%ae%e3%81%98%e3%82%83\/","title":{"rendered":"Forth\u3092\u4f5c\u308b\u306e\u3058\u3083"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>Forth \u30d7\u30ed\u30b0\u30e9\u30e0\u3092<a title=\"Forth\u3092\u4f7f\u3046\u306e\u3058\u3083\" href=\"http:\/\/www.techscore.com\/blog\/2014\/01\/27\/forth%e3%82%92%e4%bd%bf%e3%81%86%e3%81%ae%e3%81%98%e3%82%83\/\">\u66f8\u3044\u3066\u307f\u3066<\/a>\u3001\u7d50\u69cb\u697d\u3057\u3044\u3001\u3068\u3044\u3046\u3053\u3068\u304c\u5206\u304b\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u306a\u3093\u3068\u306a\u304f Forth \u30a4\u30f3\u30bf\u30d7\u30ea\u30bf\u3092\u66f8\u3044\u3066\u307f\u305f\u3044\u6c17\u6301\u3061\u306b\u306a\u3063\u3066\u304d\u307e\u3057\u305f\u3002<\/p>\n<p>\u307e\u3060\u8a00\u8a9e\u4ed5\u69d8\u306e\u4e00\u90e8\u3057\u304b\u77e5\u308a\u307e\u305b\u3093\u306e\u3067\u9014\u4e2d\u3067\u7834\u7dbb\u3059\u308b\u6c17\u3082\u3057\u307e\u3059\u304c\u3001\u6c17\u306b\u305b\u305a\u30c1\u30e3\u30ec\u30f3\u30b8\u3057\u307e\u3059\uff01<\/p>\n<p>&nbsp;<\/p>\n<h2>\u3068\u3044\u3046\u3053\u3068\u3067\u66f8\u3044\u3066\u307f\u305f<\/h2>\n<p>\u3042\u307e\u308a\u5fd7\u3092\u9ad8\u304f\u3057\u3059\u304e\u308b\u3068\u3059\u3050\u306b\u632b\u6298\u3057\u305d\u3046\u306a\u306e\u3067\u3001\u307e\u305a\u306f\u5358\u7d14\u306a\u8a08\u7b97\u304c\u3067\u304d\u308b\u30ec\u30d9\u30eb\u3092\u76ee\u6307\u3057\u307e\u3059\u3002<\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u66f8\u3044\u3066\u307f\u307e\u3057\u305f\u3002\u8a00\u8a9e\u306f Ruby \u3067\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true \">class MyForth\r\n\r\n  def initialize\r\n    @stack = []\r\n    @words = {\r\n      '.s' =&gt; lambda { p @stack },\r\n      '+'  =&gt; lambda { lhs, rhs = @stack.pop(2); @stack.push(lhs + rhs) },\r\n      '-'  =&gt; lambda { lhs, rhs = @stack.pop(2); @stack.push(lhs - rhs) },\r\n      '*'  =&gt; lambda { lhs, rhs = @stack.pop(2); @stack.push(lhs * rhs) },\r\n      '\/'  =&gt; lambda { lhs, rhs = @stack.pop(2); @stack.push(lhs \/ rhs) },\r\n    }\r\n  end\r\n\r\n  def eval_word(word)\r\n    if word =~ \/^-?\\d+$\/\r\n      @stack.push(word.to_i)\r\n    elsif @words[word]\r\n      @words[word].call\r\n    else\r\n      puts \"ERROR: Unsupported word: #{word}\"\r\n    end\r\n  end\r\n\r\nend<\/pre>\n<p>MyForth \u3068\u3044\u3046\u30af\u30e9\u30b9\u3092\u4f5c\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u306a\u306b\u306f\u3068\u3082\u3042\u308c\u30b9\u30bf\u30c3\u30af\u304c\u7121\u3044\u3068\u59cb\u307e\u3089\u306a\u3044\u306e\u3067\u3001\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3067 @stack \u3092\u7a7a\u306e\u914d\u5217\u3067\u521d\u671f\u5316\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u306b push\/pop \u3059\u308b\u3053\u3068\u3067\u30b9\u30bf\u30c3\u30af\u3068\u3057\u3066\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<p>@words \u306b\u306f\u5b9a\u7fa9\u6e08\u307f\u306e\u30ef\u30fc\u30c9\u3092\u767b\u9332\u3057\u3066\u3044\u307e\u3059\u3002\u3068\u308a\u3042\u3048\u305a\u30b9\u30bf\u30c3\u30af\u306e\u72b6\u614b\u3092\u78ba\u8a8d\u3059\u308b\u305f\u3081\u306e\u300c.s\u300d\u3068\u56db\u5247\u6f14\u7b97\u3060\u3051\u767b\u9332\u3057\u3066\u3044\u307e\u3059\u3002@words \u306b\u306f\u30ef\u30fc\u30c9\u540d\u3092\u30ad\u30fc\u3068\u3057\u3066\u51e6\u7406\u3092\u884c\u3046 Proc \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u767b\u9332\u3057\u307e\u3059\u3002<\/p>\n<p>eval_word(word) \u30e1\u30bd\u30c3\u30c9\u306f\u6e21\u3055\u308c\u305f\u30ef\u30fc\u30c9\u3092\u9806\u756a\u306b\u51e6\u7406\u3057\u307e\u3059\u3002\u3084\u3063\u3066\u3044\u308b\u3053\u3068\u306f\u5358\u7d14\u3067\u3001\u6570\u5024\u3067\u3042\u308c\u3070 @stack \u306b push \u3059\u308b\u3001\u5b9a\u7fa9\u6e08\u307f\u306e\u30ef\u30fc\u30c9\u306a\u3089\u51e6\u7406\u3092\u5b9f\u884c\u3059\u308b\u3001\u305d\u3046\u3067\u306a\u3051\u308c\u3070\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u8868\u793a\u3057\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u52d5\u304b\u3057\u3066\u307f\u308b<\/h2>\n<p>\u305d\u308c\u3067\u306f\u52d5\u304b\u3057\u3066\u307f\u307e\u3059\u3002Forth \u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u30db\u30ef\u30a4\u30c8\u30b9\u30da\u30fc\u30b9\u3067\u533a\u5207\u3089\u308c\u305f\u30ef\u30fc\u30c9\u306e\u7f85\u5217\u3067\u3057\u305f\u306e\u3067\u3001\u5165\u529b\u3092\u30db\u30ef\u30a4\u30c8\u30b9\u30da\u30fc\u30b9\u3067\u533a\u5207\u3063\u3066\u304b\u3089 MyForth#eval_word(word) \u306b\u6e21\u3059\u8db3\u5834\u3092\u4f5c\u308c\u3070 REPL \u306e\u5b8c\u6210\u3067\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">forth = MyForth.new\r\n\r\nwhile line = gets\r\n  line.split(\/\\s\/).each do |word|\r\n    forth.eval_word(word)\r\n  end\r\nend<\/pre>\n<p>\u8a66\u3057\u306b 1 \u304b\u3089 10 \u306e\u5408\u8a08\u3092\u6c42\u3081\u3066\u307f\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">1 2 3 4 5 6 7 8 9 10 + + + + + + + + + .s<\/pre>\n<p>\u304d\u3063\u3068 55 \u306b\u306a\u3063\u3066\u304f\u308c\u308b\u3068\u601d\u3044\u307e\u3059\u3001\u3001<\/p>\n<pre class=\"lang:default decode:true\">[55]<\/pre>\n<p>\u3061\u3083\u3093\u3068 55 \u306b\u306a\u308a\u307e\u3057\u305f\uff01<\/p>\n<p>Forth \u306e\u69cb\u6587\u306f\u30b7\u30f3\u30d7\u30eb\u306a\u306e\u3067\u3001\u57fa\u672c\u90e8\u5206\u306f\u7c21\u5358\u306b\u4f5c\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3057\u305f\u3002<\/p>\n<p>\u307e\u3060\u307e\u3060\u6a5f\u80fd\u304c\u8db3\u308a\u306a\u3044\u306e\u3067\u3001\u30ef\u30fc\u30c9\u5b9a\u7fa9\u3084\u6761\u4ef6\u5206\u5c90\u304f\u3089\u3044\u306f\u3067\u304d\u308b\u3088\u3046\u306b\u6539\u826f\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>Forth \u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u66f8\u3044\u3066\u307f\u3066\u3001\u7d50\u69cb\u697d\u3057\u3044\u3001\u3068\u3044\u3046\u3053\u3068\u304c\u5206\u304b\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u306a\u3093\u3068\u306a\u304f Forth \u30a4\u30f3\u30bf\u30d7\u30ea\u30bf\u3092\u66f8\u3044\u3066\u307f\u305f\u3044\u6c17\u6301\u3061\u306b\u306a\u3063\u3066\u304d\u307e\u3057\u305f\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2014\/01\/28\/forth%e3%82%92%e4%bd%9c%e3%82%8b%e3%81%ae%e3%81%98%e3%82%83\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[133,18,84],"tags":[130],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/3939"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=3939"}],"version-history":[{"count":7,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/3939\/revisions"}],"predecessor-version":[{"id":17881,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/3939\/revisions\/17881"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=3939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=3939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=3939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}