write-ups-challenges-2020-2021/scheming-schemer/third/calculator-app.rkt
2022-11-24 18:03:20 +01:00

64 lines
1.4 KiB
Racket

#lang r5rs
;You found the second flag as well >:(
;Fine, I'm no longer loading the flag...
;Assoc containing defined variables
(define env '())
;Add a variable to the environment
(define (add-variable name value)
(set! env (cons (cons name value) env))
"Variable added!")
;Read a variable from the environment
(define (read-variable name)
(define pair (assq name env))
(if pair
(cdr pair)
(begin
(display "Undefined variable")
(newline))))
;Load the very secret flag
(define (load-flag)
'NO!)
;Allowed operators
(define operators '(+ - * /))
;Evaluate an expression of the form (<op> <exp> ... <exp>)
(define (evaluate-pair expression)
(define operator (car expression))
(define arguments (cdr expression))
(cond
((eq? operator 'def)
(add-variable (cadr expression) (evaluate (caddr expression))))
(else
(apply (eval operator (scheme-report-environment 5)) (map evaluate arguments)))))
;Evaluate any expression
(define (evaluate expression)
(cond
((pair? expression)
(evaluate-pair expression))
((symbol? expression)
(read-variable expression))
(else
expression)))
;Start the read-eval-print loop
(define (read-eval-print)
(display ">> ")
(let*
((expression (read))
(result (if (eq? expression 'flag)
"Wait, thats illegal!"
(evaluate expression))))
(display result)
(newline)
(read-eval-print)))
(load-flag)
(read-eval-print)