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
f608a142
Commit
f608a142
authored
Dec 12, 2020
by
zygzagZ
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
arguments check in Fun Type compare, check const integer overflow, check void: arg, declaration
parent
519005ca
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
52 additions
and
17 deletions
+52
-17
Absyn.cpp
Absyn.cpp
+6
-1
Absyn.h
Absyn.h
+3
-1
Lexer.cpp
Lexer.cpp
+1
-1
ParseError.cpp
ParseError.cpp
+3
-1
Parser.cpp
Parser.cpp
+1
-1
Parser.h
Parser.h
+1
-1
TypeCheck.cpp
TypeCheck.cpp
+31
-10
TypeCheck.h
TypeCheck.h
+6
-1
No files found.
Absyn.cpp
View file @
f608a142
...
@@ -2896,7 +2896,12 @@ bool Fun::isEqual(Type const *other, bool sub) const {
...
@@ -2896,7 +2896,12 @@ bool Fun::isEqual(Type const *other, bool sub) const {
if
(
!
type_
->
isEqual
(
casted
->
type_
,
sub
)
||
listtype_
->
size
()
!=
casted
->
listtype_
->
size
())
{
if
(
!
type_
->
isEqual
(
casted
->
type_
,
sub
)
||
listtype_
->
size
()
!=
casted
->
listtype_
->
size
())
{
return
false
;
return
false
;
}
}
// TODO: czy sprawdzać argumenty?
for
(
unsigned
int
i
=
0
;
i
<
listtype_
->
size
();
i
++
)
{
if
(
!
casted
->
listtype_
->
at
(
i
)
->
isEqual
(
listtype_
->
at
(
i
),
true
))
{
return
false
;
}
}
return
true
;
}
}
return
false
;
return
false
;
}
}
...
...
Absyn.h
View file @
f608a142
...
@@ -9,7 +9,7 @@
...
@@ -9,7 +9,7 @@
/******************** TypeDef Section ********************/
/******************** TypeDef Section ********************/
typedef
int
Integer
;
typedef
long
long
int
Integer
;
typedef
char
Char
;
typedef
char
Char
;
typedef
double
Double
;
typedef
double
Double
;
typedef
std
::
string
String
;
typedef
std
::
string
String
;
...
@@ -229,6 +229,8 @@ public:
...
@@ -229,6 +229,8 @@ public:
virtual
Type
*
clone
()
const
=
0
;
virtual
Type
*
clone
()
const
=
0
;
virtual
std
::
string
printName
()
const
=
0
;
virtual
std
::
string
printName
()
const
=
0
;
virtual
bool
isEqual
(
Type
const
*
f
,
bool
sub
=
false
)
const
=
0
;
virtual
bool
isEqual
(
Type
const
*
f
,
bool
sub
=
false
)
const
=
0
;
template
<
typename
T
>
bool
isEqual
(
shared_ptr
<
T
>
f
,
bool
sub
=
false
)
const
{
return
isEqual
((
T
*
)
&*
f
,
sub
);
};
bool
operator
==
(
Type
const
&
f
)
const
{
return
isEqual
(
&
f
);
}
bool
operator
==
(
Type
const
&
f
)
const
{
return
isEqual
(
&
f
);
}
bool
operator
!=
(
Type
const
&
f
)
const
{
return
!
isEqual
(
&
f
);
}
bool
operator
!=
(
Type
const
&
f
)
const
{
return
!
isEqual
(
&
f
);
}
};
};
...
...
Lexer.cpp
View file @
f608a142
...
@@ -1137,7 +1137,7 @@ YY_BUFFER_APPEND(yytext); BEGIN STRING;
...
@@ -1137,7 +1137,7 @@ YY_BUFFER_APPEND(yytext); BEGIN STRING;
case
60
:
case
60
:
YY_RULE_SETUP
YY_RULE_SETUP
#line 90 "Grammar.l"
#line 90 "Grammar.l"
yylval
.
int_
=
atoi
(
yytext
);
return
_INTEGER_
;
yylval
.
int_
=
((
strlen
(
yytext
)
>
18
)
?
(
1ll
<<
60
)
:
atoll
(
yytext
)
);
return
_INTEGER_
;
YY_BREAK
YY_BREAK
case
61
:
case
61
:
/* rule 61 can match eol */
/* rule 61 can match eol */
...
...
ParseError.cpp
View file @
f608a142
...
@@ -6,7 +6,9 @@ using namespace std;
...
@@ -6,7 +6,9 @@ using namespace std;
ParseError
::
ParseError
(
string
reason
,
int
line
)
:
runtime_error
(
"ParseError"
),
line
(
line
)
ParseError
::
ParseError
(
string
reason
,
int
line
)
:
runtime_error
(
"ParseError"
),
line
(
line
)
{
{
msg
=
reason
+
"
\n
At line "
+
to_string
(
line
);
msg
=
reason
;
if
(
line
!=
-
1
)
msg
+=
"
\n
At line "
+
to_string
(
line
);
}
}
ParseError
::
ParseError
(
string
reason
,
Visitable
*
expr
)
:
runtime_error
(
"ParseError"
)
ParseError
::
ParseError
(
string
reason
,
Visitable
*
expr
)
:
runtime_error
(
"ParseError"
)
...
...
Parser.cpp
View file @
f608a142
...
@@ -262,7 +262,7 @@ union YYSTYPE
...
@@ -262,7 +262,7 @@ union YYSTYPE
{
{
#line 100 "Grammar.y"
#line 100 "Grammar.y"
int
int_
;
long
long
int
int_
;
char
char_
;
char
char_
;
double
double_
;
double
double_
;
char
*
string_
;
char
*
string_
;
...
...
Parser.h
View file @
f608a142
...
@@ -30,7 +30,7 @@ class RelOp;
...
@@ -30,7 +30,7 @@ class RelOp;
typedef
union
typedef
union
{
{
int
int_
;
long
long
int
int_
;
char
char_
;
char
char_
;
double
double_
;
double
double_
;
char
*
string_
;
char
*
string_
;
...
...
TypeCheck.cpp
View file @
f608a142
...
@@ -3,6 +3,7 @@
...
@@ -3,6 +3,7 @@
#include <stdexcept>
#include <stdexcept>
#include <iostream>
#include <iostream>
#include "Printer.h"
#include "Printer.h"
#include <climits>
void
TypeCheck
::
visitClassDef
(
ClassDef
*
t
)
{
void
TypeCheck
::
visitClassDef
(
ClassDef
*
t
)
{
const
string
name
=
t
->
getName
()
->
string_
;
const
string
name
=
t
->
getName
()
->
string_
;
...
@@ -134,11 +135,15 @@ void TypeCheck::visitDecl(Decl *decl)
...
@@ -134,11 +135,15 @@ void TypeCheck::visitDecl(Decl *decl)
Type
*
type
=
decl
->
type_
;
Type
*
type
=
decl
->
type_
;
type
->
accept
(
this
);
type
->
accept
(
this
);
if
(
dynamic_cast
<
Void
*>
(
type
))
{
throw
ParseError
(
"Void variable"
,
type
);
}
for
(
Item
*
el
:
*
decl
->
listitem_
)
{
for
(
Item
*
el
:
*
decl
->
listitem_
)
{
// el->accept(this); // nie trzeba bo el to tylko ident = expr
// el->accept(this); // nie trzeba bo el to tylko ident = expr
if
(
el
->
expr_
)
{
if
(
el
->
expr_
)
{
auto
exprType
=
evalExpr
(
el
->
expr_
);
auto
exprType
=
evalExpr
(
el
->
expr_
);
if
(
!
type
->
isEqual
(
&*
exprType
,
true
))
{
if
(
!
type
->
isEqual
(
exprType
,
true
))
{
throw
InvalidTypeError
(
*
type
,
{
exprType
},
el
->
expr_
);
throw
InvalidTypeError
(
*
type
,
{
exprType
},
el
->
expr_
);
}
}
}
}
...
@@ -153,7 +158,7 @@ void TypeCheck::visitAss(Ass *ass)
...
@@ -153,7 +158,7 @@ void TypeCheck::visitAss(Ass *ass)
{
{
auto
a
=
evalExpr
(
ass
->
expr_1
);
auto
a
=
evalExpr
(
ass
->
expr_1
);
auto
b
=
evalExpr
(
ass
->
expr_2
);
auto
b
=
evalExpr
(
ass
->
expr_2
);
if
(
!
a
->
isEqual
(
&*
b
,
true
))
{
if
(
!
a
->
isEqual
(
b
,
true
))
{
throw
InvalidTypeError
(
*
a
,
{
b
},
ass
->
expr_1
);
throw
InvalidTypeError
(
*
a
,
{
b
},
ass
->
expr_1
);
}
}
}
}
...
@@ -214,7 +219,10 @@ void TypeCheck::visitWhile(While *while_)
...
@@ -214,7 +219,10 @@ void TypeCheck::visitWhile(While *while_)
{
{
evalExpr
<
Bool
>
(
while_
->
expr_
);
evalExpr
<
Bool
>
(
while_
->
expr_
);
while_
->
stmt_
->
accept
(
this
);
while_
->
stmt_
->
accept
(
this
);
if
(
!
dynamic_cast
<
ELitTrue
*>
(
while_
->
expr_
))
{
returnType
=
nullptr
;
returnType
=
nullptr
;
}
}
}
void
TypeCheck
::
visitSExp
(
SExp
*
s_exp
)
void
TypeCheck
::
visitSExp
(
SExp
*
s_exp
)
...
@@ -228,7 +236,7 @@ void TypeCheck::visitForEach(ForEach *for_each)
...
@@ -228,7 +236,7 @@ void TypeCheck::visitForEach(ForEach *for_each)
for_each
->
type_
->
accept
(
this
);
for_each
->
type_
->
accept
(
this
);
Array
expect
(
for_each
->
type_
);
Array
expect
(
for_each
->
type_
);
auto
arrType
=
evalExpr
<
Array
>
(
for_each
->
expr_
);
auto
arrType
=
evalExpr
<
Array
>
(
for_each
->
expr_
);
if
(
*
arrType
!=
expect
)
{
if
(
!
expect
.
isEqual
(
&*
arrType
,
true
)
)
{
throw
InvalidTypeError
(
expect
,
{
arrType
},
for_each
->
expr_
);
throw
InvalidTypeError
(
expect
,
{
arrType
},
for_each
->
expr_
);
}
}
...
@@ -328,7 +336,7 @@ void TypeCheck::visitEApp(EApp *e_app)
...
@@ -328,7 +336,7 @@ void TypeCheck::visitEApp(EApp *e_app)
Expr
*
expr
=
e_app
->
listexpr_
->
at
(
i
);
Expr
*
expr
=
e_app
->
listexpr_
->
at
(
i
);
auto
exprType
=
evalExpr
(
expr
);
auto
exprType
=
evalExpr
(
expr
);
auto
*
expect
=
type
->
listtype_
->
at
(
i
);
auto
*
expect
=
type
->
listtype_
->
at
(
i
);
if
(
*
expect
!=
*
exprType
)
{
if
(
!
expect
->
isEqual
(
exprType
,
true
)
)
{
throw
InvalidTypeError
(
*
expect
,
{
exprType
},
expr
);
throw
InvalidTypeError
(
*
expect
,
{
exprType
},
expr
);
}
}
}
catch
(
const
ParseError
&
e
)
{
}
catch
(
const
ParseError
&
e
)
{
...
@@ -345,6 +353,9 @@ void TypeCheck::visitEApp(EApp *e_app)
...
@@ -345,6 +353,9 @@ void TypeCheck::visitEApp(EApp *e_app)
void
TypeCheck
::
visitELitInt
(
ELitInt
*
e_lit_int
)
void
TypeCheck
::
visitELitInt
(
ELitInt
*
e_lit_int
)
{
{
visitInteger
(
e_lit_int
->
integer_
);
visitInteger
(
e_lit_int
->
integer_
);
if
(
e_lit_int
->
integer_
>
INT_MAX
||
e_lit_int
->
integer_
<
INT_MIN
)
{
throw
ParseError
(
"constant too big"
,
e_lit_int
->
lineno
);
}
lastType
=
make_shared
<
Int
>
();
lastType
=
make_shared
<
Int
>
();
}
}
...
@@ -369,7 +380,7 @@ void TypeCheck::visitENewArray(ENewArray *e)
...
@@ -369,7 +380,7 @@ void TypeCheck::visitENewArray(ENewArray *e)
evalExpr
<
Int
>
(
e
->
expr_
);
evalExpr
<
Int
>
(
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
(
"
Void table
"
,
e
);
}
}
auto
ret
=
make_shared
<
Array
>
(
e
->
type_
->
clone
());
// make type
auto
ret
=
make_shared
<
Array
>
(
e
->
type_
->
clone
());
// make type
ret
->
type_
->
binding
=
scope
->
currentBinding
;
ret
->
type_
->
binding
=
scope
->
currentBinding
;
...
@@ -437,7 +448,7 @@ void TypeCheck::visitERel(ERel *e)
...
@@ -437,7 +448,7 @@ void TypeCheck::visitERel(ERel *e)
auto
a
=
evalExpr
(
e
->
expr_1
);
auto
a
=
evalExpr
(
e
->
expr_1
);
auto
b
=
evalExpr
(
e
->
expr_2
);
auto
b
=
evalExpr
(
e
->
expr_2
);
if
(
dynamic_cast
<
EQU
*>
(
e
->
relop_
)
||
dynamic_cast
<
NE
*>
(
e
->
relop_
))
{
if
(
dynamic_cast
<
EQU
*>
(
e
->
relop_
)
||
dynamic_cast
<
NE
*>
(
e
->
relop_
))
{
if
(
*
a
!=
*
b
)
{
if
(
!
a
->
isEqual
(
b
,
true
)
&&
!
b
->
isEqual
(
a
,
true
)
)
{
throw
InvalidTypeError
(
*
a
,
{
b
},
e
);
throw
InvalidTypeError
(
*
a
,
{
b
},
e
);
}
}
}
else
{
}
else
{
...
@@ -591,6 +602,16 @@ void TypeCheck::check(Visitable *v)
...
@@ -591,6 +602,16 @@ void TypeCheck::check(Visitable *v)
for
(
auto
f
:
scope
->
functions
)
{
for
(
auto
f
:
scope
->
functions
)
{
checkFunction
(
f
);
checkFunction
(
f
);
}
}
if
(
FunctionInfoPtr
main
=
scope
->
functions
.
local
(
"main"
))
{
Fun
expected
(
new
Int
(),
new
ListType
());
if
(
!
main
->
type
->
isEqual
(
&
expected
,
false
))
{
throw
InvalidTypeError
(
expected
,
{
main
->
type
},
main
->
ident
);
}
}
else
{
throw
ParseError
(
"No
\"
main
\"
function declared"
);
}
}
}
void
TypeCheck
::
checkReturnStatement
(
shared_ptr
<
Type
>
type
,
Stmt
*
stmt
)
{
void
TypeCheck
::
checkReturnStatement
(
shared_ptr
<
Type
>
type
,
Stmt
*
stmt
)
{
...
@@ -600,7 +621,7 @@ void TypeCheck::checkReturnStatement(shared_ptr<Type> type, Stmt *stmt) {
...
@@ -600,7 +621,7 @@ void TypeCheck::checkReturnStatement(shared_ptr<Type> type, Stmt *stmt) {
if
(
!
funType
)
throw
runtime_error
(
"FunctionInfo "
+
f
->
name
+
":"
+
to_string
(
f
->
lineLocation
)
+
" nie ma typu Fun!"
);
if
(
!
funType
)
throw
runtime_error
(
"FunctionInfo "
+
f
->
name
+
":"
+
to_string
(
f
->
lineLocation
)
+
" nie ma typu Fun!"
);
auto
funRet
=
funType
->
type_
;
auto
funRet
=
funType
->
type_
;
if
(
*
type
!=
*
funRet
)
{
if
(
!
funRet
->
isEqual
(
type
,
true
)
)
{
throw
InvalidTypeError
(
*
funRet
,
{
type
},
stmt
);
throw
InvalidTypeError
(
*
funRet
,
{
type
},
stmt
);
}
}
}
}
...
@@ -648,11 +669,11 @@ void TypeCheck::checkFunctionRet() {
...
@@ -648,11 +669,11 @@ void TypeCheck::checkFunctionRet() {
if
(
!
returnType
||
returnType
==
nullptr
)
{
if
(
!
returnType
||
returnType
==
nullptr
)
{
throw
ParseError
(
"Function "
+
f
->
name
+
" did not return!"
,
f
->
lineLocation
);
throw
ParseError
(
"Function "
+
f
->
name
+
" did not return!"
,
f
->
lineLocation
);
}
}
if
(
*
returnType
!=
*
funRet
)
{
if
(
!
funRet
->
isEqual
(
returnType
,
true
)
)
{
throw
InvalidTypeError
(
*
returnType
,
{
funRet
},
f
->
block
);
throw
InvalidTypeError
(
*
returnType
,
{
funRet
},
f
->
block
);
}
}
}
else
{
}
else
{
if
(
returnType
&&
*
returnType
!=
*
funRet
)
{
if
(
returnType
&&
!
funRet
->
isEqual
(
returnType
,
true
)
)
{
throw
InvalidTypeError
(
*
returnType
,
{
funRet
},
f
->
block
);
throw
InvalidTypeError
(
*
returnType
,
{
funRet
},
f
->
block
);
}
}
}
}
...
...
TypeCheck.h
View file @
f608a142
...
@@ -147,7 +147,12 @@ protected:
...
@@ -147,7 +147,12 @@ protected:
void
visitEmpty
(
Empty
*
p
)
{};
void
visitEmpty
(
Empty
*
p
)
{};
void
visitNoInit
(
NoInit
*
p
)
{
};
void
visitNoInit
(
NoInit
*
p
)
{
};
void
visitInit
(
Init
*
p
)
{
};
void
visitInit
(
Init
*
p
)
{
};
void
visitAr
(
Ar
*
p
)
{
p
->
type_
->
accept
(
this
);
};
void
visitAr
(
Ar
*
p
)
{
p
->
type_
->
accept
(
this
);
if
(
dynamic_cast
<
Void
*>
(
p
->
type_
))
{
throw
ParseError
(
"Void argument"
,
p
->
type_
);
}
};
// exprs
// exprs
void
visitPlus
(
Plus
*
p
)
{};
void
visitPlus
(
Plus
*
p
)
{};
...
...
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