Skip to content

Instantly share code, notes, and snippets.

@mohanr
Last active May 3, 2023 17:18
Show Gist options
  • Save mohanr/b8b1735456fe7112865014bcf2ed182b to your computer and use it in GitHub Desktop.
Save mohanr/b8b1735456fe7112865014bcf2ed182b to your computer and use it in GitHub Desktop.
Lexer in Racket
#lang typed/racket
(require racket/list)
(define (operator c )
(match c
['+' Plus]
['-' Minus]
['/' Div]))
(struct (i) Literal ([v : i]))
(struct Plus ())
(struct Minus ())
(struct Div ())
(struct (i) InvalidChar ([v : i]))
(struct EndOfInput())
(struct Empty() )
(struct (i) Unexpected([val : i]))
(define-type (Expected i e) (U Integer Char))
(struct (i) ExpectedEndOfFile([val : i]))
(struct (i) BeginningOfInput ([val : i]))
(define-type (ErrorType i e)
(U Empty EndOfInput
( Unexpected i)
( Expected i e)
( ExpectedEndOfFile i)
( BeginningOfInput i)))
(define-type (Error i e) (U Integer
(ErrorType i e)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment