2011年9月26日 星期一

Amber Compiler--如何使用amberc

傳言Google秘密開發的新程式語言Dart,可能跟Smalltalk有點淵源,雖然目前我們還不清楚Dart語言到底會長成什麼樣子但是倒是可以從Amber身上想像一下,Jimmy's papa最近試玩一下Amber的Server端編譯器amberc,它負責將Smalltalk指令轉譯為Javascript指令

關於如何安裝Amber的官方網址在此
https://github.com/NicolasPetton/amber/wiki/Getting-started

請下載壓縮檔並解壓縮
https://github.com/NicolasPetton/amber/zipball/amber
安裝Nodejs--這是Javascript V8引擎的VM

apt-get install nodejs
記得,amberc第 268行
node $TMPDIR/compiler.js $DEPLOY $SUFFIX $COMPILE
必須改成
nodejs $TMPDIR/compiler.js $DEPLOY $SUFFIX $COMPILE
否則無法執行

這是/amber/examples/nodejs/hello/原有的內容

寄件者 scratchlab

將/amber/examples/nodejs/hello/Hello.st原有的內容改為

Widget subclass: #Hello
    instanceVariableNames: ''
    classVariableNames: ''
    poolDictionaries: ''
    category: 'Hello'!

!Hello methodsFor: 'Instance main' stamp: 'Anonymous 9/26/2011 02:01'!
renderOn: html
    "Render some html"
   | aArray |
   aArray := #( 69 8 5 7 1 -3  )  .
    html div class: 'section';
    id: 'headerSection';
    with: [ html h1:  ( Hello main) , '   ' , aArray  , ' -----> ' ,  ( Hello sort: aArray  )  ] ! !

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

Hello class
    instanceVariableNames: ''!

!Hello class methodsFor: 'class main' stamp: 'Anonymous 9/26/2011 01:57'!
main
    ^ 'JimmyScratchLab test Bubble Sort'
! !

!Hello class methodsFor: 'class main' stamp: 'Anonymous 9/26/2011 01:57'!
sort: anArray
   "I expect an array of numbers and I answer an array of those number, 
    but in sorted order. I am the actual guts of the bubble sort."
|answer swap itemCount hasChanged|
answer := anArray copy.
swap := 
    [:indexOne :indexTwo| 
    |temp|
    temp := answer at: indexOne.
    answer at: indexOne put: (answer at: indexTwo).
    answer at: indexTwo put: temp].
 
itemCount := answer size.
[hasChanged := false.
itemCount := itemCount - 1.
1 to: itemCount do:
    [:index | 
    (answer at: index) > (answer at: index + 1) ifTrue:
        [swap value: index value: index + 1.
        hasChanged := true]].
hasChanged] whileTrue.
^ answer
! !

打開檔案/amber/examples/nodejs/hello/Makefile將原來的內容

Program.js: Hello.st
    ../../../bin/amberc -m Hello Hello.st Program

改為

Program.js: Hello.st
    ../../../bin/amberc Canvas.st Hello.st  -m Hello   Program

run: Program.js
    ./hello

clean:
    rm -f Program.js Hello.js

記得將Canvas.st複製到/amber/examples/nodejs/hello/

執行
/amber/examples/nodejs/hello$ make

會增加幾個.js檔案

寄件者 scratchlab

複製Hello.js到/amber/examples/myproject/js
然後將/amber/examples/myproject/index.html的內容改為

<html>
<head>
<title>My Project</title>
<script src="../../js/amber.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript"> 
    loadAmber({
        files: ['Hello.js'],
        prefix: 'examples/myproject/js',
        ready: function() {
            smalltalk.Browser._open();
        }}); 
</script>
</body>
</html>

用FireFox打開index.html

寄件者 scratchlab

在Workspace填入Smalltalk指令
Hello new appendToJQuery: 'body' asJQuery
執行DoIt

寄件者 scratchlab

沒有留言:

張貼留言