Sanchayan Maity
997a0d11f8
This is for the fourth chapter of Crafting Interpreters. https://craftinginterpreters.com/scanning.html This is only a first pass. Still need to test complex expressions. Basic scanning/parsing works. See test.
115 lines
1.9 KiB
Haskell
115 lines
1.9 KiB
Haskell
module LoxTypes where
|
|
|
|
import Prelude hiding (False, True)
|
|
|
|
data CharToken
|
|
= LeftParen
|
|
| RightParen
|
|
| LeftBrace
|
|
| RightBrace
|
|
| Comma
|
|
| Dot
|
|
| Minus
|
|
| Plus
|
|
| Semicolon
|
|
| Slash
|
|
| Star
|
|
deriving (Eq)
|
|
|
|
instance Show CharToken where
|
|
show LeftParen = "("
|
|
show RightParen = ")"
|
|
show LeftBrace = "{"
|
|
show RightBrace = "}"
|
|
show Comma = ","
|
|
show Dot = "."
|
|
show Minus = "-"
|
|
show Plus = "+"
|
|
show Semicolon = ";"
|
|
show Slash = "/"
|
|
show Star = "*"
|
|
|
|
data MultiCharToken
|
|
= Bang
|
|
| BangEqual
|
|
| Equal
|
|
| EqualEqual
|
|
| Greater
|
|
| GreaterEqual
|
|
| Less
|
|
| LessEqual
|
|
deriving (Eq)
|
|
|
|
instance Show MultiCharToken where
|
|
show Bang = "!"
|
|
show BangEqual = "!="
|
|
show Equal = "="
|
|
show EqualEqual = "=="
|
|
show Greater = ">"
|
|
show GreaterEqual = ">="
|
|
show Less = "<"
|
|
show LessEqual = "<="
|
|
|
|
data Keywords
|
|
= And
|
|
| Class
|
|
| Else
|
|
| False
|
|
| Func
|
|
| For
|
|
| If
|
|
| Nil
|
|
| Or
|
|
| Print
|
|
| Return
|
|
| Super
|
|
| This
|
|
| True
|
|
| Var
|
|
| While
|
|
deriving (Eq)
|
|
|
|
instance Show Keywords where
|
|
show And = "and"
|
|
show Class = "class"
|
|
show Else = "else"
|
|
show False = "false"
|
|
show Func = "fun"
|
|
show For = "for"
|
|
show If = "if"
|
|
show Nil = "nil"
|
|
show Or = "or"
|
|
show Print = "print"
|
|
show Return = "return"
|
|
show Super = "super"
|
|
show This = "this"
|
|
show True = "true"
|
|
show Var = "var"
|
|
show While = "while"
|
|
|
|
data Literal
|
|
= Identifier Keywords
|
|
| Str String
|
|
| Number Double
|
|
deriving (Eq, Show)
|
|
|
|
data LoxToken
|
|
= Char CharToken
|
|
| Op MultiCharToken
|
|
| Lit Literal
|
|
| Eof
|
|
deriving (Eq, Show)
|
|
|
|
-- This is how Class Token is defined in Crafting Interpreters but we will
|
|
-- make use of the State in Megaparsec to report errors.
|
|
-- data LoxToken = Token
|
|
-- { tokenType :: TokenType,
|
|
-- lexeme :: Text,
|
|
-- literal :: Literal,
|
|
-- line :: Int
|
|
-- }
|
|
-- deriving (Eq)
|
|
--
|
|
-- instance Show LoxToken where
|
|
-- show (Token tokenType lexeme literal _) = show tokenType ++ " " ++ show lexeme ++ " " ++ show literal
|