• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆-341  評論-2670  文章-0  trackbacks-0
                這次終于實現了兩個exe,一個是編譯器,一個是提供控制臺API的虛擬機。等提供GUI的虛擬機出來之后就開放出來。

                假設有代碼Program.txt:
            1 module program
            2 import console
            3 import list
            4 
            5 def main = take 10 (iterate finc 1.0||> sqr ||> ftoa ||> writeln |> ioseq

                那么提供Program.xml:
            1 <kfpProject>
            2   <inherit path="..\..\Include\ConsoleApplication.xml"/>
            3   <output path="Executable.xml"/>
            4   <report path="Report.txt"/>
            5   <code>
            6     <include path="Program.txt"/>
            7   </code>
            8 </kfpProject>

                然后執行:
            ..\..\Release\KfpCompiler.exe Program.xml
            ..\..\Release\KfpConsole.exe Executable.xml

                就可以運行一個程序啦!

                讓我們分析一下代碼。首先finc是一個將浮點數加一的函數,那么iterate finc 1.0就是一個從1.0開始,每次遞增1.0的無窮數組,然后take 10返回[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0]。然后||>sqr將所有數字開方,||>ftoa將所有數字轉成字符串,然后||>writeln將所有的字符串變成10個輸出字符串的函數,最后|>ioseq運行這10個函數,結果如下:


                接下來打算實現一個VL_CompressedStream用于壓縮產生的可執行鏡像,然后再開發一個支持簡單繪圖功能的虛擬機,就開放出來。所需要的時間應該不久,因為一個新的虛擬機只需要實現API就可以了。偉大的插件系統,滅哈哈…… 

                下面是上面MakeFile所引用到的庫文件(預定義的):

                ConsoleApplication.xml
            1 <kfpProject>
            2   <inherit path="Library.xml"/>
            3   <code>
            4     <include path="ConsoleModule.txt"/>
            5   </code>
            6 </kfpProject>

                Library.xml
            1 <kfpProject>
            2   <code>
            3     <include path="SysUtils.txt"/>
            4     <include path="List.txt"/>
            5   </code>
            6 </kfpProject>

                所需要的代碼文件:

                ConsoleModule.txt
            1 module console
            2 import system
            3 
            4 func read :: (IO string) alias "console::read"
            5 
            6 func write :: (string -> (IO void)) alias "console::write"
            7 
            8 func writeln :: (string -> (IO void)) alias "console::writeln"
            9 

                SysUtils.txt
             1 module sysutils
             2 import system
             3 
             4 def (+= iadd
             5 def (+= fadd
             6 def (-= isub
             7 def (-= fsub
             8 def (*= imul
             9 def (*= fmul
            10 def (/= idiv
            11 def (/= fdiv
            12 def (>= igt
            13 def (>= fgt
            14 def (>= cgt
            15 def (>== iegt
            16 def (>== fegt
            17 def (>== cegt
            18 def (<= ilt
            19 def (<= flt
            20 def (<= clt
            21 def (<== ielt
            22 def (<== felt
            23 def (<== celt
            24 def (=== iequ
            25 def (=== fequ
            26 def (=== cequ
            27 def (!== ineq
            28 def (!== fneq
            29 def (!== cneq
            30 def (&&= and
            31 def (||= or
            32 def (^= xor
            33 def (|>) param op = op param
            34 def oprev op a b = op b a
            35 
            36 def not a = select a of
            37               case true : false
            38               case false : true
            39             end
            40 
            41 def and a b = select a of
            42                 case true : b
            43                 case false : false
            44               end
            45 
            46 def or a b = select a of
            47                 case true : true
            48                 case false : b
            49               end
            50 
            51 def xor a b = select a of
            52                 case true : not b
            53                 case false : b
            54               end
            55 
            56 def if cond t f = select cond of
            57                     case true : t
            58                     case false : f
            59                   end
            60 
            61 def ineg num = isub 0 num
            62 
            63 def fneg num = fsub 0.0 num
            64 
            65 def inc n = iadd n 1
            66 
            67 def dec n = isub n 1
            68 
            69 def finc n = fadd n 1.0
            70 
            71 def fdec n = fsub n 1.0
            72 
            73 def pairfirst p = select p of
            74   case pair a b : a
            75 end
            76 
            77 def pairsecond p = select p of
            78   case pair a b : b
            79 end
            80 
            81 def pairop op =\p->
            82     select p of
            83         case pair a b : op a b
            84     end
            85 
            86 func return T :: T -> IO T
            87 def return x e = success (pair x e)
            88 
            89 func ioerror T :: string -> IO T
            90 def ioerror s = \env->fail(ioemessage s)

                List.txt
              1 module list
              2 import sysutils
              3 
              4 def (+= concat
              5 def (||>) param op = transform op param
              6 
              7 def ioseq = foldr iovoid (>>>)
              8 
              9 {返回列表長度}
             10 def length xs =
             11     select xs of
             12         case list x tail : iadd 1 (length tail)
             13         case empty : 0
             14     end
             15 
             16 {返回列表的第一個元素}
             17 def head xs =
             18     select xs of
             19         case list x tail : x
             20     end
             21 
             22 {返回列表的第二個元素開始的列表}
             23 def tail xs =
             24     select xs of
             25         case list x tail : tail
             26     end
             27 
             28 {連接兩個列表}
             29 def concat as bs =
             30     select as of
             31         case list a tail : list a (concat tail bs)
             32         case empty : bs
             33     end
             34 
             35 {判讀列表是否為空}
             36 def isempty xs =
             37     select xs of
             38         case list x tail : false
             39         case empty : true
             40     end
             41 
             42 {將列表通過映射函數轉換為另一個列表}
             43 def transform mapper xs =
             44     select xs of
             45         case list x tail : list (mapper x) (transform mapper tail)
             46         case empty : empty
             47     end
             48 
             49 {將列表反轉}
             50 def reverse xs =
             51     let
             52         def _reverse xs r =
             53             select xs of
             54                 case list x tail : _reverse tail (list x r)
             55                 case empty : r
             56             end
             57     in _reverse xs empty
             58 
             59 {為列表插入分隔符}
             60 def intersperse spliter xs =
             61     select xs of
             62         case list x xtail : 
             63             select xtail of
             64                 case list y ytail : list x (list spliter (intersperse spliter xtail))
             65                 case empty : list x empty
             66             end
             67         case empty : empty
             68     end
             69 
             70 {將“列表的列表”的所有元素連接起來成為一個長的新列表}
             71 def flatten xs =
             72     select xs of
             73         case list x tail : concat x (flatten tail)
             74         case empty : empty
             75     end
             76 
             77 {將兩個列表組合成一個pair的列表}
             78 def pairlist as bs =
             79     select as of
             80         case list a atail :
             81             select bs of
             82                 case list b btail : list (pair a b) (pairlist atail btail)
             83                 case empty : empty
             84             end
             85         case empty : empty
             86     end
             87 
             88 {將列表應用到一個左結合操作符上}
             89 def foldl init op xs =
             90     select xs of
             91         case list x tail : foldl (op init x) op tail
             92         case empty : init
             93     end
             94 
             95 {將列表應用到一個右結合操作符上}
             96 def foldr final op xs =
             97     select xs of
             98         case list x tail : op x (foldr final op tail) 
             99         case empty : final
            100     end
            101 
            102 {判斷列表的所有元素是否符合某個約束}
            103 def all constraint xs = foldl true and (transform constraint xs)
            104 
            105 {判斷列表的是否存在元素是否符合某個約束}
            106 def any constraint xs = foldl false or (transform constraint xs)
            107 
            108 {遞歸無窮列表}
            109 def iterate op init = list init (iterate op (op init))
            110 
            111 {重復無窮列表}
            112 def repeat x = list x (repeat x)
            113 
            114 {循環無窮列表}
            115 def cycle xs = concat xs (cycle xs)
            116 
            117 {取列表前n個元素組成子列表}
            118 def take n xs =
            119     if (iequ n 0)
            120         empty
            121         select xs of
            122             case list x tail : list x (take (isub n 1) tail)
            123             case empty : empty
            124         end
            125 
            126 {取列表n個元素以后的字列表}
            127 def drop n xs =
            128     if (iequ n 0)
            129         xs
            130         select xs of
            131             case list x tail : drop (isub n 1) tail
            132             case empty : empty
            133         end
            134 
            135 {取列表中符合條件的元素組成的新列表}
            136 def takeif constraint xs =
            137     select xs of
            138         case list x tail : if (constraint x) (list x (takeif constraint tail)) (takeif constraint tail)
            139         case empty : empty
            140     end
            141 
            142 {取列表中不符合條件的元素組成的新列表}
            143 def dropif constraint xs =
            144     select xs of
            145         case list x tail : if (constraint x) (dropif constraint tail) (list x (dropif constraint tail))
            146         case empty : empty
            147     end
            148 
            149 {判斷一個列表是否另一個列表的前綴}
            150 def isprefix eq as bs =
            151     select as of
            152         case list a atail :
            153             select bs of
            154                 case list b btail : and (eq a b) (isprefix eq atail btail)
            155                 case empty : false
            156             end
            157         case empty : true
            158     end
            159     
            160 {判斷一個列表是否另一個列表的后綴}
            161 def ispostfix eq as bs = isprefix eq (reverse as) (reverse bs)
            162 
            163 {取出列表中指定位置的元素}
            164 def elemof n xs = if (iequ n 0) (head xs) (elemof (isub n 1) (tail xs))
            165 
            166 {判斷符合條件的元素在列表中的位置}
            167 def findfirst constraint xs =
            168     let
            169         def _findfirst n xs =
            170             select xs of
            171                 case list x tail : if (constraint x) n (_findfirst (iadd n 1) tail)
            172                 case empty : ineg 1
            173             end
            174     in _findfirst 0 xs
            175 
            176 {判斷符合條件的元素在列表中的位置}
            177 def find constraint xs =
            178     let
            179         def _find indices n xs =
            180             select xs of
            181                 case list x tail : _find (if (constraint x) (list n indices) indices) (iadd n 1) tail
            182                 case empty : indices
            183             end
            184     in reverse (_find empty 0 xs)
            posted on 2008-12-26 08:07 陳梓瀚(vczh) 閱讀(2052) 評論(5)  編輯 收藏 引用 所屬分類: 腳本技術

            評論:
            # re: Kernel FP編譯器工具實現 2009-01-19 06:36 | jge
            你蠻厲害的哦,不過...代碼或是思想都挺山寨的...有好處也有壞處,但我的建議是:why not dig into something deeper? i checked out your posts before, of cource you can make your language live easily, but the result is far from satisfaction, especially when you deal with a lazy one:
            intuitively a programming language is called `lazy' because it's able to reduce its equivalence relations(say function bindings) from left from right(rewrite) rather than commonly from right to left (calculate). but when we talk about`lazy programming language', we mean `a programming language with full laziness' which ensures every common expression is evaluated at most once. if a full lazy language needs to be *compiled*, the solution is mature and clearly stated in internet known as `super-combinator' which roughly means top level combinator without free variables. to compile your code to `super-combinators', you have to do `lambda-lifting' for all lambda/let/where, then `full-laziness lifting' to all super-combinators, and alpha/beta/eta/peephole reductions. with that super-combinators can be compiled to a VM called G-machine.
            well, modern haskell compiler (say GHC) is much more complex from upper process, but the upper one contains *huge* stuff as well and idea is similar: `laziness' composes code node and `pattern matching' decomposes them. i don't even mention the most important stuff: type system. since most memory is located at heap, you can allow every operation that you can imagine in your language because almost everything is the same size : a pointer. like adding two functions, applying a 2-arg function with 3 args. a duck-typeing language simply allows them, but there're more sophisticated solutions. i implied you'd better read some material on Hindley–Milner type system(and System-F) in a previous comment, i don't know if you do, but i don't see much from your posts on the implementions. a flexiable polymorphic type system is the *key* of a functional programming language. if you become a guru on these, then you can contribute to a more modern compiler (say GHC), come on, do learn some lambda-calculus/SystemF first and think deeply, i'm looking forward to your new milestone.
            哈哈,我是蠻無聊的,在別人博客上羅嗦,不過思考的深入一點對你有好處,加油。  回復  更多評論
              
            # re: Kernel FP編譯器工具實現 2009-01-19 06:50 | 陳梓瀚(vczh)
            《The Implementation of Functional Programming Language》和類型理論都看過了,G-Machine的指令集根本不算指令,跟x86的模式相差太遠了,因此根本就沒有解決問題。

            不過我這次實現用的是最簡單的辦法,反正G-Machine的指令集也要構造樹,所以我一開始就把樹構造好了。這就是我編譯的結果是xml的原因了……Kfp是個強類型語言,這造成了一點小麻煩。  回復  更多評論
              
            # re: Kernel FP編譯器工具實現 2009-01-19 07:28 | jge
            G-machine is the only way to effectively *compile* a full-lazy programming language till now, this is also why although haskell development is leaded by MSR Cambridge, but M$ still can't bring it to VisualStudio. interpreting a tree is compilation? and i think the tree G-machine makes during runtime totally differs from your static tree. super-combinators and full-lazy lifting are really slick solutions IMO. you probably need to read the book twice.
            i guess `強類型' you mentioned actually means `static typeing', well, according to my knowledge, i don't know any functional pl isn't static...(except LISP which is not only dynamic typeing but also dynamic scopeing, it's not a modern style we want to talk about). type-level polymophism is almost the least requirement for a lazy fp, and this is where the real fun emerges. in modern fp, a type also has `type' called `kind' (although recent haskell implement unifies it with `unsyntactic type coercion'). the book you mentioned is old and uses miranda as example, it can't touch much on type system.
              回復  更多評論
              
            # re: Kernel FP編譯器工具實現 2009-01-19 07:41 | jge
            according to `VisualStudio' above, i mean `dotnet' strictly. 哈哈,不騷擾你了,你的代碼讓我想起了本科時同樣的山寨勁,所以打字多了點。  回復  更多評論
              
            # re: Kernel FP編譯器工具實現 2009-01-19 07:49 | 陳梓瀚(vczh)
            運行時也有一棵樹的,所以跟G-Machine是一樣的。G-Machine的指令如果你仔細觀察的話,會發現分成了兩批。至于類型理論,實際上也有另外的兩本書講了,只不過只對語法的設計有指導作用。  回復  更多評論
              
            国产精品成人99久久久久91gav | 久久亚洲AV无码精品色午夜麻豆| 欧美va久久久噜噜噜久久| 午夜精品久久久内射近拍高清| 99久久综合狠狠综合久久止| AV无码久久久久不卡网站下载| 久久SE精品一区二区| 狠狠色婷婷久久一区二区| 久久亚洲国产成人影院网站| 久久久久久国产a免费观看不卡| 国产精品久久久久久搜索| 99久久综合狠狠综合久久止| 97久久久久人妻精品专区 | 国产精品99久久久久久猫咪| 中文精品久久久久国产网址| 国产亚州精品女人久久久久久 | 国产亚州精品女人久久久久久 | 亚洲国产天堂久久久久久| 久久久精品视频免费观看| 亚洲精品无码久久久| 亚洲欧洲日产国码无码久久99| 精品久久久久久久久午夜福利| 国产精品一久久香蕉产线看| 99热都是精品久久久久久| 久久久久国产一区二区| 久久久久久国产精品无码下载 | 韩国三级中文字幕hd久久精品| 久久久受www免费人成| 伊人色综合久久天天人手人婷 | 国产亚洲精久久久久久无码AV| 日日狠狠久久偷偷色综合96蜜桃 | 国产精品九九久久免费视频 | 99久久综合狠狠综合久久| 国产精品久久久久蜜芽| 99久久99久久精品免费看蜜桃 | 亚洲狠狠综合久久| 模特私拍国产精品久久| 久久国产一区二区| 国产精品久久久久蜜芽| 国产精品伦理久久久久久| 久久永久免费人妻精品下载|