forked from asutton/waffle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.cpp
101 lines (81 loc) · 2.13 KB
/
value.cpp
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
#include "value.hpp"
#include "ast.hpp"
#include "type.hpp"
// -------------------------------------------------------------------------- //
// Built-in literal values
//
// TODO: Consider add a new "value" module for these things.
namespace {
Unit* unit_;
True* true_;
False* false_;
} // namespace
void
init_values() {
unit_ = new Unit(get_unit_type());
true_ = new True(get_bool_type());
false_ = new False(get_bool_type());
}
Term*
get_unit() { return unit_; }
Term*
get_true() { return true_; }
Term*
get_false() { return false_; }
// -------------------------------------------------------------------------- //
// Term classification
//
// Determine if the given term is in the set of values. Note that this
// is distinct from typing.
// Returns true if t is true.
bool
is_true(Term* t) { return t->kind == true_term; }
// Returns true if t is false.
bool
is_false(Term* t) { return t->kind == false_term; }
// Returns true iff t is a boolean value.
//
// bv ::= true | false
bool
is_boolean_value(Term* t) { return is_true(t) || is_false(t); }
// Returns true if t is an integer value.
//
// nv ::= N
bool
is_integer_value(Term* t) { return t->kind == int_term; }
// Returns true when t is a lambda abstraction.
bool
is_abs(Term* t) { return t->kind == abs_term; }
// Returns true when t unit.
bool
is_unit(Term* t) { return t->kind == unit_term; }
// Returns true when t is a string value.
bool
is_string_value(Term* t) { return t->kind == str_term; }
// Returns true when t is a list value. A list term is a list value
// only when t has the form '[v1, ..., vn]' where each 'vi' is a value.
bool
is_list_value(Term* t) {
return false;
}
// Returns true if t is a value (in normal form), which is defined
// inductively as:
//
// v ::= unit
// | boolean-value
// | integer-value
// | string-value
// | list-value
// | \x:T.t
//
// TODO: We're missing value definitions for tuples, records, and
// variants.
bool
is_value(Term* t) {
return is_unit(t)
or is_boolean_value(t)
or is_integer_value(t)
or is_string_value(t)
or is_list_value(t)
or is_abs(t);
}