Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
L
latte
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zygzagZ
latte
Commits
b4061d29
Commit
b4061d29
authored
Dec 12, 2020
by
zygzagZ
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor v2
parent
37fabd11
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
26 additions
and
51 deletions
+26
-51
Absyn.cpp
Absyn.cpp
+1
-0
Absyn.h
Absyn.h
+2
-1
Info.h
Info.h
+1
-1
TypeCheck.cpp
TypeCheck.cpp
+22
-49
No files found.
Absyn.cpp
View file @
b4061d29
...
@@ -1427,6 +1427,7 @@ ClassT::ClassT(PIdent *p1)
...
@@ -1427,6 +1427,7 @@ ClassT::ClassT(PIdent *p1)
ClassT
::
ClassT
(
const
ClassT
&
other
)
ClassT
::
ClassT
(
const
ClassT
&
other
)
{
{
pident_
=
other
.
pident_
->
clone
();
pident_
=
other
.
pident_
->
clone
();
binding
=
other
.
binding
;
}
}
ClassT
&
ClassT
::
operator
=
(
const
ClassT
&
other
)
ClassT
&
ClassT
::
operator
=
(
const
ClassT
&
other
)
...
...
Absyn.h
View file @
b4061d29
...
@@ -719,11 +719,12 @@ public:
...
@@ -719,11 +719,12 @@ public:
Array
(
const
Array
&
);
Array
(
const
Array
&
);
Array
&
operator
=
(
const
Array
&
);
Array
&
operator
=
(
const
Array
&
);
Array
(
Type
*
p1
);
Array
(
Type
*
p1
);
Array
()
:
type_
(
nullptr
)
{};
~
Array
();
~
Array
();
virtual
void
accept
(
Visitor
*
v
);
virtual
void
accept
(
Visitor
*
v
);
virtual
Array
*
clone
()
const
;
virtual
Array
*
clone
()
const
;
void
swap
(
Array
&
);
void
swap
(
Array
&
);
std
::
string
printName
()
const
{
return
type_
->
printName
()
+
"[]
"
;
}
std
::
string
printName
()
const
{
return
type_
?
type_
->
printName
()
+
"[]"
:
"array
"
;
}
bool
isEqual
(
Type
const
*
other
)
const
{
bool
isEqual
(
Type
const
*
other
)
const
{
if
(
const
Array
*
casted
=
dynamic_cast
<
const
Array
*>
(
other
))
if
(
const
Array
*
casted
=
dynamic_cast
<
const
Array
*>
(
other
))
return
*
type_
==
*
casted
->
type_
;
return
*
type_
==
*
casted
->
type_
;
...
...
Info.h
View file @
b4061d29
...
@@ -36,7 +36,7 @@ public:
...
@@ -36,7 +36,7 @@ public:
weak_ptr
<
ClassInfo
>
klass
;
weak_ptr
<
ClassInfo
>
klass
;
weak_ptr
<
Binding
>
binding
;
weak_ptr
<
Binding
>
binding
;
FunctionInfo
(
FuncDef
*
expr
,
ClassInfoPtr
klass
=
nullptr
);
FunctionInfo
(
FuncDef
*
expr
,
ClassInfoPtr
klass
=
nullptr
);
FunctionInfo
(
string
name
,
TypePtr
type
)
:
VarInfo
(
name
,
type
)
{};
FunctionInfo
(
string
name
,
TypePtr
type
)
:
VarInfo
(
name
,
type
)
,
block
(
nullptr
)
{};
// FunctionInfo(PIdent *ident, ClassInfoPtr klass = nullptr) : VarInfo(ident), block(NULL), klass(klass) {};
// FunctionInfo(PIdent *ident, ClassInfoPtr klass = nullptr) : VarInfo(ident), block(NULL), klass(klass) {};
virtual
string
kind
()
const
{
return
"function"
;
}
virtual
string
kind
()
const
{
return
"function"
;
}
...
...
TypeCheck.cpp
View file @
b4061d29
...
@@ -208,18 +208,14 @@ void TypeCheck::visitCondElse(CondElse *cond_else)
...
@@ -208,18 +208,14 @@ void TypeCheck::visitCondElse(CondElse *cond_else)
void
TypeCheck
::
visitWhile
(
While
*
while_
)
void
TypeCheck
::
visitWhile
(
While
*
while_
)
{
{
while_
->
expr_
->
accept
(
this
);
evalExpr
<
Bool
>
(
while_
->
expr_
);
Bool
expect
;
if
(
*
lastType
!=
expect
)
{
throw
InvalidTypeError
(
expect
,
{
lastType
},
while_
->
expr_
);
}
while_
->
stmt_
->
accept
(
this
);
while_
->
stmt_
->
accept
(
this
);
returnType
=
nullptr
;
returnType
=
nullptr
;
}
}
void
TypeCheck
::
visitSExp
(
SExp
*
s_exp
)
void
TypeCheck
::
visitSExp
(
SExp
*
s_exp
)
{
{
s_exp
->
expr_
->
accept
(
this
);
evalExpr
(
s_exp
->
expr_
);
}
}
void
TypeCheck
::
visitForEach
(
ForEach
*
for_each
)
void
TypeCheck
::
visitForEach
(
ForEach
*
for_each
)
...
@@ -228,8 +224,8 @@ void TypeCheck::visitForEach(ForEach *for_each)
...
@@ -228,8 +224,8 @@ void TypeCheck::visitForEach(ForEach *for_each)
for_each
->
type_
->
accept
(
this
);
for_each
->
type_
->
accept
(
this
);
Type
&
iterType
=
*
lastType
;
Type
&
iterType
=
*
lastType
;
Array
expect
(
for_each
->
type_
);
Array
expect
(
for_each
->
type_
);
for_each
->
expr_
->
accept
(
this
);
auto
arrType
=
evalExpr
<
Array
>
(
for_each
->
expr_
);
if
(
*
last
Type
!=
expect
)
{
if
(
*
arr
Type
!=
expect
)
{
throw
InvalidTypeError
(
expect
,
{
lastType
},
for_each
->
expr_
);
throw
InvalidTypeError
(
expect
,
{
lastType
},
for_each
->
expr_
);
}
}
...
@@ -283,16 +279,9 @@ void TypeCheck::visitEVar(EVar *e_var)
...
@@ -283,16 +279,9 @@ void TypeCheck::visitEVar(EVar *e_var)
void
TypeCheck
::
visitEIndexAcc
(
EIndexAcc
*
e_index_acc
)
void
TypeCheck
::
visitEIndexAcc
(
EIndexAcc
*
e_index_acc
)
{
{
e_index_acc
->
expr_1
->
accept
(
this
);
auto
type
=
evalExpr
<
Array
>
(
e_index_acc
->
expr_1
);
shared_ptr
<
Array
>
type
=
dynamic_pointer_cast
<
Array
>
(
lastType
);
evalExpr
<
Int
>
(
e_index_acc
->
expr_2
);
if
(
!
type
)
{
throw
ParseError
(
"Table expected"
,
e_index_acc
->
expr_1
);
}
e_index_acc
->
expr_2
->
accept
(
this
);
Int
expect
;
if
(
*
lastType
!=
expect
)
{
throw
InvalidTypeError
(
expect
,
{
lastType
},
e_index_acc
->
expr_2
);
}
shared_ptr
<
Type
>
last
(
type
->
type_
->
clone
());
shared_ptr
<
Type
>
last
(
type
->
type_
->
clone
());
last
->
binding
=
scope
->
currentBinding
;
last
->
binding
=
scope
->
currentBinding
;
lastType
=
last
;
lastType
=
last
;
...
@@ -300,11 +289,11 @@ void TypeCheck::visitEIndexAcc(EIndexAcc *e_index_acc)
...
@@ -300,11 +289,11 @@ void TypeCheck::visitEIndexAcc(EIndexAcc *e_index_acc)
void
TypeCheck
::
visitEClsMmbr
(
EClsMmbr
*
e_cls_mmbr
)
void
TypeCheck
::
visitEClsMmbr
(
EClsMmbr
*
e_cls_mmbr
)
{
{
e_cls_mmbr
->
expr_
->
accept
(
this
);
auto
exprType
=
evalExpr
(
e_cls_mmbr
->
expr_
);
shared_ptr
<
ClassT
>
type
=
dynamic_pointer_cast
<
ClassT
>
(
last
Type
);
shared_ptr
<
ClassT
>
type
=
dynamic_pointer_cast
<
ClassT
>
(
expr
Type
);
if
(
!
type
)
{
if
(
!
type
)
{
// it is an array and we ask for length
// it is an array and we ask for length
if
(
dynamic_pointer_cast
<
Array
>
(
last
Type
))
{
if
(
dynamic_pointer_cast
<
Array
>
(
expr
Type
))
{
if
(
e_cls_mmbr
->
pident_
->
string_
==
"length"
)
{
if
(
e_cls_mmbr
->
pident_
->
string_
==
"length"
)
{
lastType
=
make_shared
<
Int
>
();
lastType
=
make_shared
<
Int
>
();
return
;
return
;
...
@@ -330,11 +319,10 @@ void TypeCheck::visitEApp(EApp *e_app)
...
@@ -330,11 +319,10 @@ void TypeCheck::visitEApp(EApp *e_app)
for
(
unsigned
int
i
=
0
;
i
<
type
->
listtype_
->
size
();
i
++
)
{
for
(
unsigned
int
i
=
0
;
i
<
type
->
listtype_
->
size
();
i
++
)
{
try
{
try
{
Expr
*
expr
=
e_app
->
listexpr_
->
at
(
i
);
Expr
*
expr
=
e_app
->
listexpr_
->
at
(
i
);
expr
->
accept
(
this
);
auto
exprType
=
evalExpr
(
expr
);
if
(
!
lastType
)
throw
ParseError
(
"No expr return type"
,
expr
);
auto
*
expect
=
type
->
listtype_
->
at
(
i
);
auto
*
expect
=
type
->
listtype_
->
at
(
i
);
if
(
*
expect
!=
*
last
Type
)
{
if
(
*
expect
!=
*
expr
Type
)
{
throw
InvalidTypeError
(
*
expect
,
{
last
Type
},
expr
);
throw
InvalidTypeError
(
*
expect
,
{
expr
Type
},
expr
);
}
}
}
catch
(
const
ParseError
&
e
)
{
}
catch
(
const
ParseError
&
e
)
{
throw
ParseError
(
string
(
e
.
what
())
+
"
\n
In argument "
+
to_string
(
i
+
1
)
+
" of function call:"
,
e_app
);
throw
ParseError
(
string
(
e
.
what
())
+
"
\n
In argument "
+
to_string
(
i
+
1
)
+
" of function call:"
,
e_app
);
...
@@ -368,18 +356,13 @@ void TypeCheck::visitEString(EString *e)
...
@@ -368,18 +356,13 @@ void TypeCheck::visitEString(EString *e)
void
TypeCheck
::
visitENewArray
(
ENewArray
*
e
)
void
TypeCheck
::
visitENewArray
(
ENewArray
*
e
)
{
{
e
->
expr_
->
accept
(
this
);
evalExpr
<
Int
>
(
e
->
expr_
);
auto
a
=
lastType
;
Int
expect
;
if
(
*
a
!=
expect
)
{
throw
InvalidTypeError
(
expect
,
{
a
},
e
->
expr_
);
}
e
->
type_
->
accept
(
this
);
e
->
type_
->
accept
(
this
);
if
(
dynamic_cast
<
Void
*>
(
e
->
type_
))
{
if
(
dynamic_cast
<
Void
*>
(
e
->
type_
))
{
throw
ParseError
(
"Tablica typu void!"
,
e
);
throw
ParseError
(
"Tablica typu void!"
,
e
);
}
}
auto
ret
=
make_shared
<
Array
>
(
e
->
type_
);
// make type
auto
ret
=
make_shared
<
Array
>
(
e
->
type_
->
clone
()
);
// make type
ret
->
binding
=
scope
->
currentBinding
;
ret
->
type_
->
binding
=
scope
->
currentBinding
;
lastType
=
ret
;
lastType
=
ret
;
}
}
...
@@ -455,26 +438,16 @@ void TypeCheck::visitERel(ERel *e)
...
@@ -455,26 +438,16 @@ void TypeCheck::visitERel(ERel *e)
void
TypeCheck
::
visitEAnd
(
EAnd
*
e
)
void
TypeCheck
::
visitEAnd
(
EAnd
*
e
)
{
{
e
->
expr_1
->
accept
(
this
);
auto
a
=
evalExpr
<
Bool
>
(
e
->
expr_1
);
auto
a
=
lastType
;
auto
b
=
evalExpr
<
Bool
>
(
e
->
expr_2
);
e
->
expr_2
->
accept
(
this
);
lastType
=
b
;
auto
b
=
lastType
;
Bool
expect
;
if
(
!
a
||
!
b
||
*
a
!=
expect
||
*
a
!=
*
b
)
{
throw
InvalidTypeError
(
expect
,
{
a
,
b
},
e
);
}
}
}
void
TypeCheck
::
visitEOr
(
EOr
*
e
)
void
TypeCheck
::
visitEOr
(
EOr
*
e
)
{
{
e
->
expr_1
->
accept
(
this
);
auto
a
=
evalExpr
<
Bool
>
(
e
->
expr_1
);
auto
a
=
lastType
;
auto
b
=
evalExpr
<
Bool
>
(
e
->
expr_2
);
e
->
expr_2
->
accept
(
this
);
lastType
=
b
;
auto
b
=
lastType
;
Bool
expect
;
if
(
!
a
||
!
b
||
*
a
!=
expect
||
*
a
!=
*
b
)
{
throw
InvalidTypeError
(
expect
,
{
a
,
b
},
e
);
}
}
}
void
TypeCheck
::
visitListTopDef
(
ListTopDef
*
list_top_def
)
void
TypeCheck
::
visitListTopDef
(
ListTopDef
*
list_top_def
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment