I’m working on an interpreter for a simple language in Racket, and I need to implement a ternary conditional (Expr ? Expr : Expr) similar to JavaScript. My language supports basic operations like addition, subtraction, and equality, and now I’m adding booleans and this ternary operation.
here is my current code for parse ;; parse: Expr -> AST ;; Parses “custom Lang” Expr to AST
(define (parse expr)
(match expr
[(? number?) (num expr)]
[(? string?) (str expr)]
['TRUE (bool #t)]
['FALSE (bool #f)]
[`(+ ,x ,y) (add (parse x) (parse y))]
[`(- ,x ,y) (sub (parse x) (parse y))]
[`(=== ,x ,y) (equals (parse x) (parse y))]
[`(,test '? ,true ': ,false)
(condt (parse test) (parse true) (parse false))]
[_ (error "Invalid custom Lang expression")]))
Im learning racket atm, and I only have previous java experience. im assuming this is like the class in java, and im defining what can be parsed? and then I would have a run fn that takes parsed input and output results.
here is my run
;; run: AST -> Result
;; Computes the result of running a customLang AST
(define (run ast)
(match ast
[(num n) n]
[(str s) s]
[(bool b) b]
[(add x y) (cus+ (run x) (run y))]
[(sub x y) (cus- (run x) (run y))]
[(equals x y) (equal? (run x) (run y))]
[(condt test true-branch false-branch)
(if (truthy? (run test)) (run true-branch) (run false-branch))]
[_ NaN]))
so I understood this as if true run 1st, if false run 2nd. and my parse seem to have correct setup. but when I try to run the tests
;; JS "truthy" true
(check-equal? (evalcustom '(10 ? 100 : 200)) 100)
;; JS "truthy" false
(check-equal? (evalcustom '((- 100 100) ? "a" : "b")) "b")))
it could not recognize and just outputs my error msg that I created. Im confused on how to make this structure and AST tree thing worked together and why they should work. therefore I could not figure out how to debug my issue.
thanks for help I really appreciate it.
You have an extra level of quotes in the pattern.
[`(,test '? ,true ': ,false)
(condt (parse test) (parse true) (parse false))]
should be
[`(,test ? ,true : ,false)
(condt (parse test) (parse true) (parse false))]
to match the list you're providing; the current pattern would need '(10 '? 100 ': 200)
in order to match.