Hi
I am porting SML to Racket. Managed to create a recursive data type like this. I reuse a macro by Alexis King for this.
(define-datatype Expr
( BoolValue Boolean )
( IntValue Number )
( Op Expr (U Plus GTEQ) Expr)
( If Expr Expr Expr)
( Assign String Expr)
( Deref String)
( Seq Expr Expr)
( While Expr Expr)
Skip
)
I also have
(struct None ()
#:transparent)
(struct (i) Some ([v : I])
#:transparent)
(define-type (Opt a) (U None (Some a)))
After this I tried to match some patterns but couldn't proceed beyond a point. I suspect syntax is the major issue. Could you take a look ?
This is part of a longer method and I match all the Expr. Error will show identifiers are unbound at this stage. No other error but there are malformed braces. I suspect.
I believe the pattern isn't generally specified as per the type annotation.
(: reduce ((Opt (Expr (Listof Store) ))->
(Opt (Expr (Listof Store )))))
(define (reduce expr store )
(match expr
[ Some (( Op (? integer? n1) Plus (? integer? n2)) store ) Some ( (IntValue (+ n1 n2)) store)]
[ Some (( Op (? integer? n1) GTEQ (? integer? n2)) store ) Some ( (BoolValue (>= n1 n2)) store)]
[ Some (Op (? integer? n1) Skip (? boolean? n2)) store
(match (reduce n2 store)
[ Some (IntValue nn2) store (Some ((Op n1 Skip nn2) store))]
[ (None) (None) ]
)
(match (reduce n1 store)
[ Some (IntValue nn1) store (Some ((Op nn1 Skip n2) store))]
[ (None) (None) ]
)]
[ Some(IntValue n ) store (None)]
[ Some (If e1 e2 e3) store
(match e1
[#t Some e2 store ]
[#f Some e3 store ]
[_ (match (reduce e1 store )
[Some (IntValue ee1) store (Some ((If ee1 e2 e3) store ))]
[ (None) (None) ]
)]
)]
I also have code that doesn't show compiler errors (e.g)
(: printexpr (Expr -> String))
(define (printexpr expr)
(match expr
[(IntValue n) (string-append (format "~a" n))]
[(BoolValue b) (string-append (format "~a" b))]
[(Deref l) (string-append (format "( ~a ~a )" "!" l))]
[( Op e1 operate e2 )
(string-append (format "( ~a ~c ~a)" (printexpr e1) (operator operate)
(printexpr e2 )))]
[ (Seq e1 e2 ) (string-append (format " ~a ; ~a" (printexpr e1 )
(printexpr e2)))]
[ (While e1 e2 ) (string-append (format "while ~a do ~a " (printexpr e1 )
(printexpr e2)))]
))
Thanks