Added syntax highlighting
This commit is contained in:
parent
235653359c
commit
45ba1c924a
2 changed files with 86 additions and 23 deletions
|
@ -12,6 +12,7 @@ Run the file `gui.rkt` using *DrRacket* or the `racket` CLI tool.
|
|||
- Brainf\*ck interpreter works for the instructions `[`, `]` `+`, `-`, `<`, `>`
|
||||
, `.`;
|
||||
- Done simple editor GUI with *Run* button and output window.
|
||||
- Editor supports basic syntax highlighting.
|
||||
|
||||
## Bugs
|
||||
|
||||
|
|
94
gui.rkt
94
gui.rkt
|
@ -1,7 +1,8 @@
|
|||
#lang racket/gui
|
||||
|
||||
(require "interpreter.rkt"
|
||||
framework)
|
||||
framework
|
||||
racket/set)
|
||||
|
||||
; The editor width in pixels
|
||||
(define EDITOR-WIDTH 600)
|
||||
|
@ -13,7 +14,7 @@
|
|||
[width EDITOR-WIDTH]
|
||||
[height EDITOR-HEIGHT]))
|
||||
|
||||
; execute-content: -> ProgState
|
||||
; execute-content: () -> ProgState
|
||||
; Returns the last state of execution of the program loaded in the editor
|
||||
(define (execute-content)
|
||||
(execute
|
||||
|
@ -21,12 +22,9 @@
|
|||
(string->program
|
||||
(send editor get-flattened-text)))))
|
||||
|
||||
; The run button
|
||||
(define run-btn (new button% [parent f]
|
||||
[label "Run"]
|
||||
; Callback procedure for a button click:
|
||||
[callback
|
||||
(lambda (button event)
|
||||
; 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
|
||||
(send run-output set-value "")
|
||||
(send run-output set-field-background run-col)
|
||||
|
@ -36,21 +34,85 @@
|
|||
|
||||
; set output and change color to ended
|
||||
(send run-output set-value out)
|
||||
(send run-output set-field-background end-col))]))
|
||||
(send run-output set-field-background end-col))
|
||||
|
||||
; Definition of the run button widget
|
||||
(define run-btn (new button% [parent f]
|
||||
[label "Run"]
|
||||
; Callback procedure for a button click:
|
||||
[callback run-program]))
|
||||
|
||||
; Definition of the editor canves
|
||||
(define c (new editor-canvas% [parent f]))
|
||||
|
||||
; The editor text object
|
||||
(define text-ln% (text:line-numbers-mixin
|
||||
(editor:standard-style-list-mixin text%)))
|
||||
(define editor (new text-ln%))
|
||||
; 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%))))))
|
||||
(define editor (new text-pro%))
|
||||
|
||||
; Show line numbers in editor
|
||||
(send editor show-line-numbers! #t)
|
||||
|
||||
; Change font to monospace 16
|
||||
(send editor change-style (make-object style-delta% 'change-size 16))
|
||||
(send editor change-style (make-object style-delta% 'change-family 'modern))
|
||||
; 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))]))
|
||||
|
||||
; Syntax highlighting for brainf*ck[(symbol=? token 'loop) "Loop"]
|
||||
(send editor start-colorer bf-token->bf-style bf-lexer '())
|
||||
|
||||
; 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)
|
||||
|
||||
; Define style of comments
|
||||
(send delta set-delta-foreground "orange")
|
||||
(editor:set-standard-style-list-delta "Comment" delta)
|
||||
|
||||
; Running output color
|
||||
(define run-col (make-object color% 250 250 200))
|
||||
|
|
Loading…
Reference in a new issue