Skip to content

Commit e244b01

Browse files
committed
[feature] Type checker for the 'ForStmt'
1 parent 040a11a commit e244b01

2 files changed

Lines changed: 134 additions & 89 deletions

File tree

src/parser/parser_type.rs

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,28 @@ use crate::ir::ast::{Type, ValueConstructor};
1313

1414
use crate::parser::parser_common::{identifier, keyword, separator};
1515

16+
// String constants for type names
17+
const INT_TYPE: &str = "Int";
18+
const REAL_TYPE: &str = "Real";
19+
const BOOLEAN_TYPE: &str = "Boolean";
20+
const STRING_TYPE: &str = "String";
21+
const UNIT_TYPE: &str = "Unit";
22+
const ANY_TYPE: &str = "Any";
23+
24+
// String constants for special type constructors
25+
const MAYBE_TYPE: &str = "Maybe";
26+
const RESULT_TYPE: &str = "Result";
27+
28+
// String constants for ADT keywords
29+
const DATA_KEYWORD: &str = "data";
30+
const END_KEYWORD: &str = "end";
31+
32+
// String constants for operators and symbols
33+
const FUNCTION_ARROW: &str = "->";
34+
const PIPE_SYMBOL: &str = "|";
35+
const COLON_SYMBOL: &str = ":";
36+
const COMMA_SYMBOL: &str = ",";
37+
1638
pub fn parse_type(input: &str) -> IResult<&str, Type> {
1739
alt((
1840
parse_basic_types,
@@ -28,20 +50,20 @@ pub fn parse_type(input: &str) -> IResult<&str, Type> {
2850
fn parse_basic_types(input: &str) -> IResult<&str, Type> {
2951
map(
3052
alt((
31-
keyword("Int"),
32-
keyword("Real"),
33-
keyword("Boolean"),
34-
keyword("String"),
35-
keyword("Unit"),
36-
keyword("Any"),
53+
keyword(INT_TYPE),
54+
keyword(REAL_TYPE),
55+
keyword(BOOLEAN_TYPE),
56+
keyword(STRING_TYPE),
57+
keyword(UNIT_TYPE),
58+
keyword(ANY_TYPE),
3759
)),
3860
|t| match t {
39-
"Int" => Type::TInteger,
40-
"Real" => Type::TReal,
41-
"Boolean" => Type::TBool,
42-
"String" => Type::TString,
43-
"Unit" => Type::TVoid,
44-
"Any" => Type::TAny,
61+
INT_TYPE => Type::TInteger,
62+
REAL_TYPE => Type::TReal,
63+
BOOLEAN_TYPE => Type::TBool,
64+
STRING_TYPE => Type::TString,
65+
UNIT_TYPE => Type::TVoid,
66+
ANY_TYPE => Type::TAny,
4567
_ => unreachable!(),
4668
},
4769
)(input)
@@ -62,7 +84,10 @@ fn parse_tuple_type(input: &str) -> IResult<&str, Type> {
6284
map(
6385
tuple((
6486
preceded(multispace0, char('(')),
65-
preceded(multispace0, separated_list1(separator(","), parse_type)),
87+
preceded(
88+
multispace0,
89+
separated_list1(separator(COMMA_SYMBOL), parse_type),
90+
),
6691
preceded(multispace0, char(')')),
6792
)),
6893
|(_, ts, _)| Type::TTuple(ts),
@@ -72,7 +97,7 @@ fn parse_tuple_type(input: &str) -> IResult<&str, Type> {
7297
fn parse_maybe_type(input: &str) -> IResult<&str, Type> {
7398
map(
7499
tuple((
75-
preceded(multispace0, keyword("Maybe")),
100+
preceded(multispace0, keyword(MAYBE_TYPE)),
76101
preceded(multispace0, char('[')),
77102
preceded(multispace0, parse_type),
78103
preceded(multispace0, char(']')),
@@ -84,7 +109,7 @@ fn parse_maybe_type(input: &str) -> IResult<&str, Type> {
84109
fn parse_result_type(input: &str) -> IResult<&str, Type> {
85110
map(
86111
tuple((
87-
preceded(multispace0, keyword("Result")),
112+
preceded(multispace0, keyword(RESULT_TYPE)),
88113
preceded(multispace0, char('[')),
89114
preceded(multispace0, parse_type),
90115
preceded(multispace0, char(',')),
@@ -99,9 +124,12 @@ fn parse_function_type(input: &str) -> IResult<&str, Type> {
99124
map(
100125
tuple((
101126
preceded(multispace0, char('(')),
102-
preceded(multispace0, separated_list0(separator(","), parse_type)),
127+
preceded(
128+
multispace0,
129+
separated_list0(separator(COMMA_SYMBOL), parse_type),
130+
),
103131
preceded(multispace0, char(')')),
104-
preceded(multispace0, tag("->")),
132+
preceded(multispace0, tag(FUNCTION_ARROW)),
105133
preceded(multispace0, parse_type),
106134
)),
107135
|(_, t_args, _, _, t_ret)| Type::TFunction(Box::new(Some(t_ret)), t_args),
@@ -111,11 +139,11 @@ fn parse_function_type(input: &str) -> IResult<&str, Type> {
111139
fn parse_adt_type(input: &str) -> IResult<&str, Type> {
112140
map(
113141
tuple((
114-
keyword("data"),
142+
keyword(DATA_KEYWORD),
115143
preceded(multispace0, identifier),
116144
preceded(multispace0, char(':')),
117145
many1(parse_adt_cons),
118-
preceded(multispace0, keyword("end")),
146+
preceded(multispace0, keyword(END_KEYWORD)),
119147
)),
120148
|(_, name, _, cons, _)| Type::Tadt(name.to_string(), cons),
121149
)(input)

0 commit comments

Comments
 (0)