-
Notifications
You must be signed in to change notification settings - Fork 1
/
CoreLanguage.hs
115 lines (93 loc) · 4.14 KB
/
CoreLanguage.hs
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
module CoreLanguage where
-- Description: The core of the language. Includes the abstract grammars and foundational types.
-- Authors:
-- > Faaiq Waqar (waqarf)
-- > Jonathan Alexander (alexajon)
-- > Julian Fortune (fortunej)
import qualified Data.Map.Strict as Map
import Prelude hiding ( and
, or
, subtract
)
-- A literal value that can be stored in a variable or passed as a parameter.
data VarVal = Int Int
| Float Float
| Boolean Bool
| Character Char
| IntList [Int]
| FloatList [Float]
| BoolList [Bool]
| String [Char]
deriving Show
-- All built in types in the language.
data Type = TInt
| TFlt
| TBool
| TChar
| TIntList
| TFltList
| TBoolList
| TString
deriving (Show, Prelude.Eq, Ord)
-- ??
data CompVal = Loaded | Syntaxerror | Datatypeerror
deriving Show
-- Maps variable names to the stored value.
type VarAssociation = Map.Map String VarVal
-- The data associated with a function.
data FuncData = FuncDataCon [String] [Type] Type Prog
deriving Show
-- Maps function names to the desired function.
type FuncAssociation = Map.Map String FuncData
-- Numeric (only works for Floats and Ints) operations
data Operation = Add | Sub | Mul | Div | Equal | Less | And
deriving Show
-- Expressions evaluate to VarVal's.
data Expr = Operation Operation Expr Expr -- Applies the operation to the 2 expressions to produce a VarVal.
| Not Expr -- Literal value. Negates Bool values.
| Variable String -- References a variable by the name (String). Evaluates to the value of the variable.
| Literal VarVal -- Literal value.
| Element Expr Expr -- Fetch the value of a specific element in a list ex: `a = b + myList[3]`
| Length Expr -- Get the length of a list
| Concat Expr Expr -- Concatenate two lists
| Cast Expr Type-- Cast from a VarVal of one type to another (specified by Type).
| Function String [Expr] -- Calls a function.
deriving Show
-- Cmd's modify state and may modify the program flow.
data Cmd = Def String FuncData -- Define a function with a name specified by String, with parameters FuncData ex: `a = 5`.
| Set String Expr -- Assign the VarVal specified by Expr to the variable named by the String ex: `a = 5`.
| Insert String Expr Expr -- Assign a value to a specific element in a list ex: `myList[3] = 4`. (params: list name, index, value)
| Delete String Expr -- Deletes the element in list variable (w/ name `String`) at index (`Expr`). (params: list name, index)
| If Expr Prog
| While Expr Prog
| ForEach String Expr Prog -- Iterates and performs block of code for each item in a list. Ex: For <var> in <list> { <prog> }
| Return Expr
deriving Show
data MaybeError x = Result x
| Error String
deriving Show
data ErrorLine = Line Int
| NoError
deriving Show
-- A program is composed a list of commands.
type Prog = [Cmd]
type FunctionName = String
-- A program state includes the variables, functions, and the program itself.
data State = ProgState VarAssociation FuncAssociation Prog
deriving Show
--------------------------------------------------------------
-- Syntactic sugar
--------------------------------------------------------------
or :: Expr -> Expr -> Expr
or left right = Not (Operation And (Not left) (Not right)) -- Demorgan's law babyyy :)
lessOrEqual :: Expr -> Expr -> Expr
lessOrEqual left right =
or (Operation Less (left) (right)) (Operation Equal (left) (right))
greater :: Expr -> Expr -> Expr
greater left right = Not (lessOrEqual left right)
greaterOrEqual :: Expr -> Expr -> Expr
greaterOrEqual left right = Not (Operation Less left right)
-- i.e.: list[index] = value
assign :: String -> Expr -> Expr -> Cmd
assign list index value =
If (Literal (Boolean True)) [Delete list index, Insert list index value]