Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
K
kumir2
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
2
Issues
2
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
kumir
kumir2
Commits
89660954
Commit
89660954
authored
Jan 18, 2013
by
Victor Yacovlev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implemented checking integer constants for overflow at compile time
parent
21a21e91
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
57 additions
and
10 deletions
+57
-10
src/plugins/kumiranalizer/syntaxanalizer.cpp
src/plugins/kumiranalizer/syntaxanalizer.cpp
+20
-10
src/shared/stdlib/kumirstdlib.hpp
src/shared/stdlib/kumirstdlib.hpp
+37
-0
No files found.
src/plugins/kumiranalizer/syntaxanalizer.cpp
View file @
89660954
...
...
@@ -1455,14 +1455,23 @@ void SyntaxAnalizerPrivate::parseAssignment(int str)
AST
::
VariableBaseType
b
=
rightExpr
->
baseType
.
kind
;
if
(
a
==
AST
::
TypeInteger
)
{
if
(
b
==
AST
::
TypeReal
)
{
QString
strValue
=
QString
::
number
(
rightExpr
->
constant
.
toDouble
(),
'f'
,
100
);
while
(
strValue
.
endsWith
(
'0'
)
||
strValue
.
endsWith
(
'.'
))
{
strValue
=
strValue
.
left
(
strValue
.
length
()
-
1
);
bool
isRealConstant
=
false
;
static
const
QString
realSpecificSymbols
=
QString
::
fromUtf8
(
"еЕeE."
);
if
(
rightExpr
->
kind
==
AST
::
ExprConst
)
{
QString
strValue
;
foreach
(
const
AST
::
Lexem
*
lx
,
right
)
{
strValue
+=
lx
->
data
;
}
for
(
int
s
=
0
;
s
<
realSpecificSymbols
.
length
();
s
++
)
{
const
QChar
ss
=
realSpecificSymbols
[
s
];
if
(
strValue
.
contains
(
ss
))
{
isRealConstant
=
true
;
break
;
}
}
}
if
(
rightExpr
->
kind
==
AST
::
ExprConst
&&
!
strValue
.
contains
(
"."
)
&&
!
strValue
.
toLower
().
contains
(
"e"
)
)
if
(
!
isRealConstant
)
{
// Constant became real because of big integer representation
err
=
_
(
"Integer constant too big"
);
...
...
@@ -2515,13 +2524,14 @@ QVariant SyntaxAnalizerPrivate::parseConstant(const std::list<Lexem*> &constant
val
+=
(
*
it
)
->
data
;
}
bool
integerOverflow
=
false
;
bool
isHex
=
false
;
if
(
pt
==
AST
::
TypeInteger
)
{
//integerOverflow = !StdLib::IntegerOverflowChecker::checkFromString(val
);
// TODO check integer value from string
integerOverflow
=
!
Kumir
::
Math
::
isCorrectIntegerConstant
(
val
.
toStdWString
()
);
isHex
=
val
.
startsWith
(
"$"
)
||
val
.
startsWith
(
"-$"
)
||
val
.
startsWith
(
"0x"
)
||
val
.
startsWith
(
"-0x"
);
}
if
(
ct
==
AST
::
TypeReal
||
(
pt
==
AST
::
TypeReal
||
integerOverflow
)
(
pt
==
AST
::
TypeReal
||
(
integerOverflow
&&
!
isHex
)
)
)
{
ct
=
AST
::
TypeReal
;
...
...
src/shared/stdlib/kumirstdlib.hpp
View file @
89660954
...
...
@@ -333,6 +333,43 @@ public:
// TODO: implement for ARM/AVR platform!!!!
}
inline
static
bool
isCorrectIntegerConstant
(
const
String
&
value
)
{
size_t
start
=
0
;
if
(
value
.
length
()
<=
start
)
return
false
;
bool
isNegative
=
value
.
at
(
0
)
==
Char
(
'-'
);
if
(
isNegative
)
{
start
=
1
;
}
if
(
value
.
length
()
<=
start
)
return
false
;
bool
isHex
=
false
;
if
(
value
.
length
()
-
start
>=
1
)
{
isHex
=
value
.
at
(
start
)
==
Char
(
'$'
);
if
(
isHex
)
start
+=
1
;
}
if
(
!
isHex
&&
value
.
length
()
-
start
>=
2
)
{
isHex
=
value
.
at
(
start
)
==
Char
(
'0'
)
&&
value
.
at
(
start
+
1
)
==
Char
(
'x'
);
if
(
isHex
)
start
+=
2
;
}
if
(
value
.
length
()
<=
start
)
return
false
;
while
(
start
<
value
.
length
()
&&
value
.
at
(
start
)
==
Char
(
'0'
))
start
+=
1
;
const
String
digits
=
value
.
substr
(
start
);
static
const
String
maxHex
=
Core
::
fromAscii
(
"80000000"
);
static
const
String
maxDecimal
=
Core
::
fromAscii
(
"2147483648"
);
if
(
isHex
)
{
if
(
digits
.
length
()
==
maxHex
.
length
())
return
digits
<
maxHex
;
else
return
digits
.
length
()
<
maxHex
.
length
();
}
else
{
if
(
digits
.
length
()
==
maxDecimal
.
length
())
return
digits
<
maxDecimal
;
else
return
digits
.
length
()
<
maxDecimal
.
length
();
}
}
inline
static
real
abs
(
real
x
)
{
return
::
fabs
(
x
);
}
inline
static
int
imax
(
int
x
,
int
y
)
{
return
x
>
y
?
x
:
y
;
}
...
...
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