• <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  評(píng)論-2670  文章-0  trackbacks-0
                這次終于實(shí)現(xiàn)了兩個(gè)exe,一個(gè)是編譯器,一個(gè)是提供控制臺(tái)API的虛擬機(jī)。等提供GUI的虛擬機(jī)出來(lái)之后就開(kāi)放出來(lái)。

                假設(shè)有代碼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>

                然后執(zhí)行:
            ..\..\Release\KfpCompiler.exe Program.xml
            ..\..\Release\KfpConsole.exe Executable.xml

                就可以運(yùn)行一個(gè)程序啦!

                讓我們分析一下代碼。首先f(wàn)inc是一個(gè)將浮點(diǎn)數(shù)加一的函數(shù),那么iterate finc 1.0就是一個(gè)從1.0開(kāi)始,每次遞增1.0的無(wú)窮數(shù)組,然后take 10返回[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0]。然后||>sqr將所有數(shù)字開(kāi)方,||>ftoa將所有數(shù)字轉(zhuǎn)成字符串,然后||>writeln將所有的字符串變成10個(gè)輸出字符串的函數(shù),最后|>ioseq運(yùn)行這10個(gè)函數(shù),結(jié)果如下:


                接下來(lái)打算實(shí)現(xiàn)一個(gè)VL_CompressedStream用于壓縮產(chǎn)生的可執(zhí)行鏡像,然后再開(kāi)發(fā)一個(gè)支持簡(jiǎn)單繪圖功能的虛擬機(jī),就開(kāi)放出來(lái)。所需要的時(shí)間應(yīng)該不久,因?yàn)橐粋€(gè)新的虛擬機(jī)只需要實(shí)現(xiàn)API就可以了。偉大的插件系統(tǒng),滅哈哈…… 

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

                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 {返回列表長(zhǎng)度}
             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 {返回列表的第一個(gè)元素}
             17 def head xs =
             18     select xs of
             19         case list x tail : x
             20     end
             21 
             22 {返回列表的第二個(gè)元素開(kāi)始的列表}
             23 def tail xs =
             24     select xs of
             25         case list x tail : tail
             26     end
             27 
             28 {連接兩個(gè)列表}
             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 {將列表通過(guò)映射函數(shù)轉(zhuǎn)換為另一個(gè)列表}
             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 {將列表反轉(zhuǎn)}
             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 {將“列表的列表”的所有元素連接起來(lái)成為一個(gè)長(zhǎng)的新列表}
             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 {將兩個(gè)列表組合成一個(gè)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 {將列表應(yīng)用到一個(gè)左結(jié)合操作符上}
             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 {將列表應(yīng)用到一個(gè)右結(jié)合操作符上}
             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 {判斷列表的所有元素是否符合某個(gè)約束}
            103 def all constraint xs = foldl true and (transform constraint xs)
            104 
            105 {判斷列表的是否存在元素是否符合某個(gè)約束}
            106 def any constraint xs = foldl false or (transform constraint xs)
            107 
            108 {遞歸無(wú)窮列表}
            109 def iterate op init = list init (iterate op (op init))
            110 
            111 {重復(fù)無(wú)窮列表}
            112 def repeat x = list x (repeat x)
            113 
            114 {循環(huán)無(wú)窮列表}
            115 def cycle xs = concat xs (cycle xs)
            116 
            117 {取列表前n個(gè)元素組成子列表}
            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個(gè)元素以后的字列表}
            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 {判斷一個(gè)列表是否另一個(gè)列表的前綴}
            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 {判斷一個(gè)列表是否另一個(gè)列表的后綴}
            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) 閱讀(2056) 評(píng)論(5)  編輯 收藏 引用 所屬分類: 腳本技術(shù)

            評(píng)論:
            # re: Kernel FP編譯器工具實(shí)現(xiàn) 2009-01-19 06:36 | jge
            你蠻厲害的哦,不過(guò)...代碼或是思想都挺山寨的...有好處也有壞處,但我的建議是: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.
            哈哈,我是蠻無(wú)聊的,在別人博客上羅嗦,不過(guò)思考的深入一點(diǎn)對(duì)你有好處,加油。  回復(fù)  更多評(píng)論
              
            # re: Kernel FP編譯器工具實(shí)現(xiàn) 2009-01-19 06:50 | 陳梓瀚(vczh)
            《The Implementation of Functional Programming Language》和類型理論都看過(guò)了,G-Machine的指令集根本不算指令,跟x86的模式相差太遠(yuǎn)了,因此根本就沒(méi)有解決問(wèn)題。

            不過(guò)我這次實(shí)現(xiàn)用的是最簡(jiǎn)單的辦法,反正G-Machine的指令集也要構(gòu)造樹(shù),所以我一開(kāi)始就把樹(shù)構(gòu)造好了。這就是我編譯的結(jié)果是xml的原因了……Kfp是個(gè)強(qiáng)類型語(yǔ)言,這造成了一點(diǎn)小麻煩。  回復(fù)  更多評(píng)論
              
            # re: Kernel FP編譯器工具實(shí)現(xiàn) 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 `強(qiáng)類型' 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.
              回復(fù)  更多評(píng)論
              
            # re: Kernel FP編譯器工具實(shí)現(xiàn) 2009-01-19 07:41 | jge
            according to `VisualStudio' above, i mean `dotnet' strictly. 哈哈,不騷擾你了,你的代碼讓我想起了本科時(shí)同樣的山寨勁,所以打字多了點(diǎn)。  回復(fù)  更多評(píng)論
              
            # re: Kernel FP編譯器工具實(shí)現(xiàn) 2009-01-19 07:49 | 陳梓瀚(vczh)
            運(yùn)行時(shí)也有一棵樹(shù)的,所以跟G-Machine是一樣的。G-Machine的指令如果你仔細(xì)觀察的話,會(huì)發(fā)現(xiàn)分成了兩批。至于類型理論,實(shí)際上也有另外的兩本書講了,只不過(guò)只對(duì)語(yǔ)法的設(shè)計(jì)有指導(dǎo)作用。  回復(fù)  更多評(píng)論
              
            久久精品国产亚洲77777| 一本一本久久aa综合精品| 丰满少妇人妻久久久久久| 精品国产一区二区三区久久久狼| 99精品国产在热久久无毒不卡| 狠狠干狠狠久久| 伊人久久大香线蕉综合网站| 97精品国产97久久久久久免费| 久久国产精品久久久| yy6080久久| 99久久亚洲综合精品成人| 99久久国产亚洲综合精品| 久久久国产精品网站| 久久天天躁狠狠躁夜夜躁2014| 成人免费网站久久久| 久久婷婷五月综合97色直播| 91久久精品国产成人久久| 久久婷婷五月综合国产尤物app| 久久国产免费| 日本福利片国产午夜久久| 久久久久亚洲AV无码麻豆| 久久精品国产一区二区三区不卡| 精品永久久福利一区二区| 久久亚洲精品无码VA大香大香| 久久中文精品无码中文字幕| 麻豆精品久久久一区二区| 精品久久久久久国产潘金莲| 久久成人国产精品免费软件| 亚洲精品无码专区久久同性男| 91精品国产91久久久久久青草| 国产成人精品免费久久久久| 久久人与动人物a级毛片| 性做久久久久久久久浪潮| 亚洲国产精品成人AV无码久久综合影院 | 日韩精品久久久久久免费| 一本一道久久a久久精品综合| 欧美日韩中文字幕久久久不卡 | 亚洲精品午夜国产VA久久成人| 2020久久精品亚洲热综合一本| 免费久久人人爽人人爽av| 久久久久亚洲AV片无码下载蜜桃|