2018-12-01 21:06:12 +00:00
|
|
|
#lang racket/gui
|
|
|
|
|
2018-12-02 17:05:50 +00:00
|
|
|
(require "interpreter.rkt"
|
2018-12-03 19:58:43 +00:00
|
|
|
framework
|
2018-12-05 15:25:29 +00:00
|
|
|
racket/set
|
|
|
|
racket/block)
|
2018-12-01 21:06:12 +00:00
|
|
|
|
|
|
|
; The editor width in pixels
|
|
|
|
(define EDITOR-WIDTH 600)
|
|
|
|
(define EDITOR-HEIGHT 600)
|
|
|
|
|
|
|
|
; The frame racket/gui base object for the editor
|
2018-12-05 15:25:29 +00:00
|
|
|
(define F (new frame%
|
2018-12-01 21:06:12 +00:00
|
|
|
[label "DrBrainf*ck"]
|
|
|
|
[width EDITOR-WIDTH]
|
|
|
|
[height EDITOR-HEIGHT]))
|
|
|
|
|
2018-12-05 15:25:29 +00:00
|
|
|
; execute-content: ((ProgState) -> _) -> _
|
2018-12-04 09:28:34 +00:00
|
|
|
; Given a callback, calls the callback with the last state of execution of the
|
|
|
|
; program loaded in the editor
|
|
|
|
(define (execute-content done)
|
2018-12-01 21:06:12 +00:00
|
|
|
(execute
|
2018-12-02 17:05:50 +00:00
|
|
|
(program->prog-state
|
2018-12-01 21:06:12 +00:00
|
|
|
(string->program
|
2018-12-05 15:25:29 +00:00
|
|
|
(send EDITOR get-flattened-text)))
|
|
|
|
done on-input))
|
|
|
|
|
|
|
|
; on-input: (Byte -> _) -> _
|
|
|
|
; Given a a callback to resume execution, sets up the run input area (callback
|
|
|
|
; included).
|
|
|
|
(define (on-input callback)
|
|
|
|
(send RUN-INPUT set-field-background run-col)
|
|
|
|
(if (non-empty-string? (send RUN-INPUT get-value))
|
|
|
|
(on-input-done callback)
|
|
|
|
(set! input-done-cb (lambda (_ __) (on-input-done callback)))))
|
|
|
|
|
|
|
|
; on-input-done: (Byte -> _) -> _
|
|
|
|
; Given a callback to resume execution, fetches the first character from
|
|
|
|
; `RUN-INPUT` "consuming" it and resumes execution.
|
|
|
|
(define (on-input-done callback)
|
|
|
|
(define old-str (send RUN-INPUT get-value))
|
|
|
|
; Continue only if there is input
|
|
|
|
(if (non-empty-string? old-str)
|
|
|
|
(block
|
|
|
|
; Disable input callback
|
|
|
|
(set! input-done-cb (lambda (_ __) (void)))
|
|
|
|
; Pick first char from run input "consuming" it
|
|
|
|
(define in (string-ref old-str 0))
|
|
|
|
(define new-str (substring old-str 1))
|
|
|
|
(send RUN-INPUT set-value new-str)
|
|
|
|
; Remove waiting color from run-input
|
|
|
|
(send RUN-INPUT set-field-background in-done-col)
|
|
|
|
; Resume execution
|
|
|
|
(callback (char->integer in)))
|
|
|
|
(void)))
|
2018-12-01 21:06:12 +00:00
|
|
|
|
2018-12-03 19:58:43 +00:00
|
|
|
; run-program: Button ControlEvent -> Nothing
|
|
|
|
; Given a button and an event, runs the program currently loaded in the editor.
|
|
|
|
(define (run-program button event)
|
|
|
|
; empty output text and set color to running
|
2018-12-05 15:25:29 +00:00
|
|
|
(send RUN-OUTPUT set-value "")
|
|
|
|
(send RUN-OUTPUT set-field-background run-col)
|
2018-12-03 19:58:43 +00:00
|
|
|
; run the program
|
2018-12-04 09:28:34 +00:00
|
|
|
(execute-content
|
|
|
|
(lambda (ps)
|
|
|
|
(define out (prog-state-output ps))
|
2018-12-10 17:48:28 +00:00
|
|
|
; update data tape inspector
|
|
|
|
(make-tape-cells ps)
|
2018-12-04 09:28:34 +00:00
|
|
|
; set output and change color to ended
|
2018-12-05 15:25:29 +00:00
|
|
|
(send RUN-OUTPUT set-value out)
|
|
|
|
(send RUN-OUTPUT set-field-background end-col))))
|
2018-12-03 19:58:43 +00:00
|
|
|
|
|
|
|
; Definition of the run button widget
|
2018-12-05 15:25:29 +00:00
|
|
|
(define RUN-BTN (new button% [parent F]
|
2018-12-02 17:05:50 +00:00
|
|
|
[label "Run"]
|
|
|
|
; Callback procedure for a button click:
|
2018-12-03 19:58:43 +00:00
|
|
|
[callback run-program]))
|
2018-12-01 21:06:12 +00:00
|
|
|
|
2018-12-03 19:58:43 +00:00
|
|
|
; Definition of the editor canves
|
2018-12-05 15:25:29 +00:00
|
|
|
(define C (new editor-canvas% [parent F]))
|
2018-12-01 21:06:12 +00:00
|
|
|
|
2018-12-03 19:58:43 +00:00
|
|
|
; Definition of editor text object.
|
|
|
|
(define text-pro% (text:line-numbers-mixin
|
|
|
|
(editor:standard-style-list-mixin
|
|
|
|
(color:text-mixin
|
|
|
|
(text:basic-mixin
|
|
|
|
(editor:basic-mixin text%))))))
|
2018-12-05 15:25:29 +00:00
|
|
|
(define EDITOR (new text-pro%))
|
2018-12-02 17:05:50 +00:00
|
|
|
|
|
|
|
; Show line numbers in editor
|
2018-12-05 15:25:29 +00:00
|
|
|
(send EDITOR show-line-numbers! #t)
|
2018-12-03 19:58:43 +00:00
|
|
|
|
|
|
|
; A BFToken is one of:
|
|
|
|
; - 'comment
|
|
|
|
; - 'instruction
|
|
|
|
; - 'shift
|
|
|
|
; - 'loop
|
|
|
|
; Interp: category of a bf program token
|
|
|
|
|
|
|
|
; A BFStyle is one of:
|
|
|
|
; - "Comment"
|
|
|
|
; - "Instruction"
|
|
|
|
; - "Shift"
|
|
|
|
; - "Loop"
|
|
|
|
; Interp: name of the style for a bf program token
|
|
|
|
|
|
|
|
; bf-token->bf-style: BFToken -> BFStyle
|
|
|
|
; Given a BFToken, returns the corresponding BFStyle.
|
|
|
|
(define (bf-token->bf-style token)
|
|
|
|
(cond [(symbol=? token 'comment) "Comment"]
|
|
|
|
[(symbol=? token 'loop) "Loop"]
|
|
|
|
[(symbol=? token 'shift) "Shift"]
|
|
|
|
[(symbol=? token 'instruction) "Instruction"]))
|
|
|
|
|
|
|
|
; bf-lexer: InputPort -> (values 1String BFToken #f Option<Nat> Option<Nat>)
|
|
|
|
; Given an input port, returns the BFToken of the instruction pointed by the
|
|
|
|
; port.
|
|
|
|
(define (bf-lexer port)
|
|
|
|
(define-values (_ __ pos) (port-next-location port))
|
|
|
|
(define c (read-char port))
|
|
|
|
(cond [(eof-object? c) (values c 'eof #f #f #f)]
|
|
|
|
[else
|
|
|
|
(define 1str (string c))
|
|
|
|
(define bf-token
|
|
|
|
(cond [(set-member? (set "+" "-" "," ".") 1str) 'instruction]
|
|
|
|
[(set-member? (set "<" ">") 1str) 'shift]
|
|
|
|
[(set-member? (set "[" "]") 1str) 'loop]
|
|
|
|
[else 'comment]))
|
|
|
|
(values 1str bf-token #f (+ pos) (+ pos 1))]))
|
|
|
|
|
2018-12-04 09:08:09 +00:00
|
|
|
; Syntax highlighting for brainf*ck
|
2018-12-05 15:25:29 +00:00
|
|
|
(send EDITOR start-colorer bf-token->bf-style bf-lexer '())
|
2018-12-03 19:58:43 +00:00
|
|
|
|
|
|
|
; Define basic style for instructions (+ - , .)
|
|
|
|
(define delta (make-object style-delta%))
|
|
|
|
(send delta set-delta-foreground "blue")
|
|
|
|
(editor:set-standard-style-list-delta "Instruction" delta)
|
|
|
|
|
|
|
|
; Define style for shifting operations (< >)
|
|
|
|
(send delta set-delta-foreground "red")
|
|
|
|
(editor:set-standard-style-list-delta "Shift" delta)
|
|
|
|
|
|
|
|
; Define basic style for looping instructions ([ ])
|
|
|
|
(send delta set-delta-foreground "forestgreen")
|
|
|
|
(editor:set-standard-style-list-delta "Loop" delta)
|
2018-12-02 17:05:50 +00:00
|
|
|
|
2018-12-03 19:58:43 +00:00
|
|
|
; Define style of comments
|
|
|
|
(send delta set-delta-foreground "orange")
|
|
|
|
(editor:set-standard-style-list-delta "Comment" delta)
|
2018-12-02 17:05:50 +00:00
|
|
|
|
2018-12-05 15:25:29 +00:00
|
|
|
; Input done color
|
|
|
|
(define in-done-col (make-object color% 255 255 255))
|
|
|
|
|
2018-12-02 17:05:50 +00:00
|
|
|
; Running output color
|
|
|
|
(define run-col (make-object color% 250 250 200))
|
|
|
|
|
|
|
|
; Output completed color
|
|
|
|
(define end-col (make-object color% 200 250 200))
|
2018-12-01 21:06:12 +00:00
|
|
|
|
2018-12-05 15:25:29 +00:00
|
|
|
; Populate menu bar
|
|
|
|
(define MB (new menu-bar% [parent F]))
|
|
|
|
(define m-file (new menu% [label "File"] [parent MB]))
|
|
|
|
(define m-edit (new menu% [label "Edit"] [parent MB]))
|
|
|
|
|
2018-12-10 17:17:46 +00:00
|
|
|
; The tape panel
|
|
|
|
(define tape-panel
|
|
|
|
(new horizontal-panel%
|
|
|
|
[parent F]
|
|
|
|
[min-height 50]
|
|
|
|
[style (list 'auto-hscroll)]
|
|
|
|
[stretchable-height #f]))
|
|
|
|
|
2018-12-10 17:48:28 +00:00
|
|
|
; make-cell: Nat Byte Boolean -> Canvas
|
|
|
|
; Given a cell index, the cell contents, and a boolean set to #t when the cell
|
|
|
|
; is pointed by the data pointer, returns the rendered cell as a Canvas.
|
2018-12-10 17:17:46 +00:00
|
|
|
(define (make-cell index content hl)
|
|
|
|
(new canvas%
|
|
|
|
[parent tape-panel]
|
|
|
|
[min-height 50]
|
|
|
|
[min-width 50]
|
2018-12-10 17:48:28 +00:00
|
|
|
[stretchable-width #f]
|
2018-12-10 17:17:46 +00:00
|
|
|
[stretchable-height #f]
|
|
|
|
[label (string-append (number->string index) ":")]
|
|
|
|
[paint-callback
|
|
|
|
(lambda (c dc)
|
|
|
|
(send c set-canvas-background (if hl end-col run-col))
|
2018-12-10 17:48:28 +00:00
|
|
|
(send dc draw-text (send c get-label) 12.5 10)
|
|
|
|
(send dc draw-text content 12.5 30))]))
|
|
|
|
|
|
|
|
; make-tape-cells: ProgState -> _
|
|
|
|
; Given a ProgState, empties the data tape inspector and creates the
|
|
|
|
; corresponding cells as canvasas in the tape inspector.
|
|
|
|
(define (make-tape-cells ps)
|
|
|
|
(define dt (prog-state-tape ps))
|
|
|
|
(send tape-panel change-children (lambda (_) '()))
|
|
|
|
(for ([i (in-range 0 (prog-state-tape-len ps))]
|
|
|
|
[tp dt])
|
|
|
|
(make-cell i (number->string tp) (= i (prog-state-dp ps)))))
|
2018-12-10 17:17:46 +00:00
|
|
|
|
2018-12-05 15:25:29 +00:00
|
|
|
; The input horizontal panel
|
|
|
|
(define input-panel
|
|
|
|
(new horizontal-panel%
|
|
|
|
[parent F]
|
|
|
|
[min-height 50]
|
|
|
|
[stretchable-height #f]))
|
|
|
|
|
|
|
|
; Input needed in program execution
|
|
|
|
(define RUN-INPUT (new text-field%
|
|
|
|
[label "Execution input:"]
|
|
|
|
[parent input-panel]))
|
|
|
|
|
|
|
|
; The input done callback (defaults to nothing)
|
|
|
|
; Type: (Button Event) -> _
|
2018-12-10 17:17:46 +00:00
|
|
|
(define input-done-cb (lambda (btn ev) (void)))
|
2018-12-05 15:25:29 +00:00
|
|
|
|
|
|
|
; Definition of the run button widget
|
|
|
|
(define INPUT-DONE-BTN (new button%
|
|
|
|
[parent input-panel]
|
|
|
|
[label "Confirm"]
|
|
|
|
[callback (lambda (btn ev) (input-done-cb btn ev))]))
|
2018-12-01 21:06:12 +00:00
|
|
|
|
2018-12-02 17:05:50 +00:00
|
|
|
; The run output text object
|
2018-12-05 15:25:29 +00:00
|
|
|
(define RUN-OUTPUT (new text-field%
|
2018-12-02 17:05:50 +00:00
|
|
|
[style (list 'multiple 'vertical-label)]
|
|
|
|
[label "Execution output:"]
|
2018-12-05 15:25:29 +00:00
|
|
|
[parent F]))
|
|
|
|
|
2018-12-01 21:06:12 +00:00
|
|
|
(define mi-open
|
|
|
|
(new menu-item%
|
|
|
|
[label "Open"]
|
|
|
|
[parent m-file]
|
|
|
|
[callback
|
|
|
|
(lambda (i e)
|
2018-12-05 15:25:29 +00:00
|
|
|
(define path (get-file #f F))
|
2018-12-01 21:06:12 +00:00
|
|
|
(when path
|
2018-12-05 15:25:29 +00:00
|
|
|
(send EDITOR load-file path 'text)))]
|
2018-12-01 21:06:12 +00:00
|
|
|
[shortcut #\o]
|
|
|
|
[shortcut-prefix '(ctl)]))
|
|
|
|
|
|
|
|
(define mi-save
|
|
|
|
(new menu-item%
|
|
|
|
[label "Save"]
|
|
|
|
[parent m-file]
|
|
|
|
[callback
|
|
|
|
(lambda (i e)
|
2018-12-05 15:25:29 +00:00
|
|
|
(send EDITOR save-file #f 'text))]
|
2018-12-01 21:06:12 +00:00
|
|
|
[shortcut #\s]
|
|
|
|
[shortcut-prefix '(ctl)]))
|
|
|
|
|
|
|
|
(append-editor-operation-menu-items m-edit #f)
|
2018-12-05 15:25:29 +00:00
|
|
|
(send EDITOR set-max-undo-history 100)
|
|
|
|
(send C set-editor EDITOR)
|
|
|
|
(send F show #t)
|