-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.cc
97 lines (85 loc) · 2.8 KB
/
types.cc
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
/*******************************************************************************
File: types.cc
Author: Dylan Bryan
Date: 8/9/21, 8:40 PM
Project: project-four
Purpose: This file contains the bodies of the type checking functions
******************************************************************************/
#include <string>
#include <vector>
using namespace std;
#include "types.h"
#include "listing.h"
void checkAssignment(Types lValue, Types rValue, string message)
{
if (lValue != MISMATCH && rValue != MISMATCH)
{
if (lValue == INT_TYPE && rValue == BOOL_TYPE)
appendError(GENERAL_SEMANTIC, "Type Mismatch on " + message);
if (lValue == REAL_TYPE && rValue == BOOL_TYPE)
appendError(GENERAL_SEMANTIC, "Type Mismatch on " + message);
if (lValue == BOOL_TYPE && rValue == INT_TYPE)
appendError(GENERAL_SEMANTIC, "Type Mismatch on " + message);
if (lValue == BOOL_TYPE && rValue == REAL_TYPE)
appendError(GENERAL_SEMANTIC, "Type Mismatch on " + message);
}
}
void checkIf(Types ifCheck, Types statement1, Types statement2)
{
if (ifCheck != BOOL_TYPE)
appendError(GENERAL_SEMANTIC, "If Expression Must Be Boolean");
if (statement1 != statement2)
appendError(GENERAL_SEMANTIC, "Type Mismatch on Then Statements");
}
void checkCase(Types caseCheck)
{
if (caseCheck != INT_TYPE)
appendError(GENERAL_SEMANTIC, "Case Expression Must Be Integer");
}
void checkCases(Types lValue, Types rValue, string message)
{
if (lValue != rValue)
appendError(GENERAL_SEMANTIC, "Case Expressions Must Match");
}
Types checkRemainder(Types left, Types right)
{
if (left == MISMATCH || right == MISMATCH)
return MISMATCH;
if (left != INT_TYPE || right != INT_TYPE)
{
appendError(GENERAL_SEMANTIC, "Integer Type Required");
return MISMATCH;
}
return INT_TYPE;
}
Types checkArithmetic(Types left, Types right)
{
if (left == MISMATCH || right == MISMATCH)
return MISMATCH;
if (left == BOOL_TYPE || right == BOOL_TYPE)
{
appendError(GENERAL_SEMANTIC, "Numeric Type Required");
return MISMATCH;
}
if (left == INT_TYPE || right == INT_TYPE)
return INT_TYPE;
return REAL_TYPE;
}
Types checkLogical(Types left, Types right)
{
if (left == MISMATCH || right == MISMATCH)
return MISMATCH;
if (left != BOOL_TYPE || right != BOOL_TYPE)
{
appendError(GENERAL_SEMANTIC, "Boolean Type Required");
return MISMATCH;
}
return BOOL_TYPE;
return MISMATCH;
}
Types checkRelational(Types left, Types right)
{
if (checkArithmetic(left, right) == MISMATCH)
return MISMATCH;
return BOOL_TYPE;
}