<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" 
               xmlns:adobe="http://www.adobe.com/2009/flexUnitUIRunner"
               creationComplete="onCreationComplete()"
               implements="hdm.lispinterpreter.IUIPrinter"
               width="600" height="450"
               backgroundColor="#66666"
               name="Lisp mit AS3" viewSourceURL="srcview/index.html">
    
    
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    
    <fx:Script>
        <![CDATA[
            import hdm.lispinterpreter.IUIPrinter;
            import hdm.lispinterpreter.LispEvaluator;
            import hdm.lispinterpreter.LispPrinter;
            import hdm.lispinterpreter.LispReader;
            import hdm.lispinterpreter.TestSuite;
            
            import mx.events.FlexEvent;
            import mx.utils.StringUtil;
            
            import org.flexunit.internals.TraceListener;
            import org.flexunit.runner.FlexUnitCore;
            
            protected var lastCommand:String;
            
            protected var core:FlexUnitCore;
            
            protected var reader:LispReader;
            protected var evaluator:LispEvaluator;
            protected var printer:LispPrinter;
            
            protected function onCreationComplete():void
            {
                runTests();                
                init();
            }
            
            
            protected function runTests():void{
                core = new FlexUnitCore();
                core.addListener(new TraceListener());
                core.run(TestSuite);
            }
            
            
            protected function init():void{
                reader = new LispReader();
                evaluator = new LispEvaluator();
                printer = new LispPrinter();
                
                print("Welcome to ActionScript 3 Lisp!\n");
                
                input.setFocus();
            }
            
            
            protected function in_enterHandler(event:FlexEvent):void
            {
                var text:String = input.text;
                input.text = "";
                
                print("\n> " + text);
                
                if(text == ""){
                    return;
                }
                
                lastCommand = text;
                print("\n");
                
                if(StringUtil.trim(text)== "clear"){
                    output.text = "";
                    return;
                }
                
                try{
                    var result:* = evaluator.eval(reader.read(text));
                    printer.print(result, this);
                }
                catch(err:Error){
                    print("Error: " + err.message);
                }
                
                
            }
            
            public function print(str:String):void{
                output.appendText(str);
            }
            
            protected function input_keyDownHandler(event:KeyboardEvent):void
            {
                // up: KeyCode 38
                // down: keyCode 40
                if(event.keyCode==38 && lastCommand !=null && lastCommand != ""){
                    input.text = lastCommand;
                }
                else if(event.keyCode == 40){
                    
                }
            }
            
            
        ]]>
    </fx:Script>
    
    <s:VGroup x="0" y="0" width="100%" height="100%" paddingLeft="10" paddingRight="10" paddingTop="10" paddingBottom="10" color="#FFFFFF" symbolColor="#000000">
        <s:Label text="Einfacher Lisp-Interpreter mit ActionScript 3" color="#FFFFFF" fontWeight="bold" fontSize="19"/>
        <s:TextArea id="output" width="100%" height="100%" editable="false"  contentBackgroundColor="#000000" color="#02C307" fontWeight="bold" fontSize="15" fontFamily="Courier New"/>
        <s:TextInput id="input" width="100%" enter="in_enterHandler(event)" keyDown="input_keyDownHandler(event)" contentBackgroundColor="#000000" color="#02C307" fontWeight="bold"  fontSize="15" fontFamily="Courier New"/>
        <s:Label text="TYPES:" />
        <s:Label text="Integer, Float, String, Boolean (true, false), Nil" paddingLeft="10"/>
        <s:Label text="FUNCTIONS:" />
        <s:Label text="cons, first, rest, plus, minus, if, eq?, quote, define, lambda" paddingLeft="10"/>
        <s:Label text="'clear' leert die Konsole" />
    </s:VGroup>
    
    
</s:Application>