summaryrefslogtreecommitdiff
path: root/DSL/Types.hs
blob: 2c33aad25ce98e0166c6d3c4330e505a76fed271 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module DSL.Types where

import DSL.BaseParsers (Parser(..))

data StackData
  = StackInt Integer
  | StackBool Bool
    deriving (Eq)

instance Show StackData where
  show (StackInt x)      = show x
  show (StackBool True)  = "true"
  show (StackBool False) = "false"

type TypeCheck = StackData -> Bool

type Stack = [StackData]

data Intrinsic
  -- core stack operations
  = I_DUMP
  | I_DROP
  | I_SWAP
  | I_DUP
  | I_OVER
  | I_ROT
  -- core arithmetic operations
  | I_PLUS
  | I_MINUS
  | I_TIMES
  | I_DIVMOD
  -- core boolean operations
  | I_NOT
  | I_AND
  | I_OR
  | I_XOR
  -- core logical operations
  | I_EQUAL
  | I_LESSTHAN
  | I_GREATERTHAN
    deriving (Show, Eq)

data Operation
  = OpPushData StackData
  | OpIntrinsic Intrinsic
    deriving (Show)

data StackModifier = StackModifier { smName :: String
                                   , smTypes :: [TypeCheck]
                                   , smFunc :: Stack -> IO Stack
                                   }

data Block
  = BLinear [Operation]
  | BIf [Block] [Block]
  | BIfElse [Block] [Block] [Block]
  | BWhile [Block] [Block]
    deriving (Show)

type Program = [Block]

data Machine = Machine { ok :: Bool
                       , stack :: Stack
                       }

data TokenData
  = T_WHITESPACE
  | T_IF
  | T_ELSE
  | T_WHILE
  | T_DO
  | T_END
  | T_BOOL_LITERAL Bool
  | T_INT_LITERAL Integer
  | T_INTRINSIC Intrinsic
    deriving (Show, Eq)

data DSLToken = DSLToken { tStr :: String -- original text
                         , tData :: TokenData -- actual data
                         } deriving (Show)

type DSLLexer  = Parser Char DSLToken
type DSLParser = Parser DSLToken