Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
Week 1
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
jrr1g18-space-cadets
Week 1
Commits
31e77e27
Verified
Commit
31e77e27
authored
6 years ago
by
Emily Rowlands
Browse files
Options
Downloads
Patches
Plain Diff
Added integer underflow
parent
4d8d17c6
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
wk2/Java/negative.bb
+2
-0
2 additions, 0 deletions
wk2/Java/negative.bb
wk2/Java/src/jrr1g18/bb/Interpreter.java
+27
-20
27 additions, 20 deletions
wk2/Java/src/jrr1g18/bb/Interpreter.java
wk2/Java/src/jrr1g18/bb/Main.java
+2
-2
2 additions, 2 deletions
wk2/Java/src/jrr1g18/bb/Main.java
with
31 additions
and
22 deletions
wk2/Java/negative.bb
0 → 100644
+
2
−
0
View file @
31e77e27
clear x;
decr x;
\ No newline at end of file
This diff is collapsed.
Click to expand it.
wk2/Java/src/jrr1g18/bb/Interpreter.java
+
27
−
20
View file @
31e77e27
...
@@ -4,8 +4,10 @@ import java.io.BufferedReader;
...
@@ -4,8 +4,10 @@ import java.io.BufferedReader;
import
java.io.FileNotFoundException
;
import
java.io.FileNotFoundException
;
import
java.io.FileReader
;
import
java.io.FileReader
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Map.Entry
;
import
java.util.Map.Entry
;
import
java.util.Objects
;
import
java.util.Objects
;
...
@@ -13,13 +15,14 @@ import java.util.regex.Matcher;
...
@@ -13,13 +15,14 @@ import java.util.regex.Matcher;
import
java.util.regex.Pattern
;
import
java.util.regex.Pattern
;
public
class
Interpreter
{
public
class
Interpreter
{
private
Map
<
String
,
Integer
>
m_vars
=
new
HashMap
<>();
private
Map
<
String
,
Integer
>
m_vars
=
new
HashMap
<>();
private
Map
<
Integer
,
String
>
m_code
=
new
HashMap
<>();
private
List
<
String
>
m_code
=
new
ArrayList
<>();
private
Map
<
Integer
,
Integer
>
whiles
=
new
HashMap
<>();
private
Map
<
Integer
,
Integer
>
m_
whiles
=
new
HashMap
<>();
private
int
m_lineCount
=
0
;
private
int
m_lineCount
=
0
;
private
int
m_whileDepth
=
0
;
private
int
m_whileDepth
=
0
;
private
final
String
m_filename
;
private
static
final
Map
<
InstructionType
,
String
>
INSTRUCTION_NAMES
;
private
static
final
Map
<
InstructionType
,
String
>
INSTRUCTION_NAMES
;
static
{
static
{
...
@@ -36,7 +39,7 @@ public class Interpreter {
...
@@ -36,7 +39,7 @@ public class Interpreter {
INCR
,
DECR
,
CLEAR
,
WHILE
,
END
,
INCR
,
DECR
,
CLEAR
,
WHILE
,
END
,
}
}
private
InstructionType
stringToInstructionType
(
String
instruction
)
{
private
static
InstructionType
stringToInstructionType
(
String
instruction
)
{
for
(
Entry
<
InstructionType
,
String
>
entry
:
INSTRUCTION_NAMES
for
(
Entry
<
InstructionType
,
String
>
entry
:
INSTRUCTION_NAMES
.
entrySet
())
{
.
entrySet
())
{
if
(
Objects
.
equals
(
instruction
,
entry
.
getValue
()))
{
if
(
Objects
.
equals
(
instruction
,
entry
.
getValue
()))
{
...
@@ -56,8 +59,14 @@ public class Interpreter {
...
@@ -56,8 +59,14 @@ public class Interpreter {
return
stripWhitespace
(
m
.
group
(
1
));
return
stripWhitespace
(
m
.
group
(
1
));
}
}
public
void
run
(
String
fileName
)
{
public
Interpreter
(
String
fileName
)
{
readCode
(
fileName
);
m_filename
=
fileName
;
}
public
void
run
()
{
readCode
();
validateCode
();
exec
();
}
}
public
String
variablesToString
()
{
public
String
variablesToString
()
{
...
@@ -147,12 +156,12 @@ public class Interpreter {
...
@@ -147,12 +156,12 @@ public class Interpreter {
}
}
}
}
private
void
readCode
(
String
nameOfTheFile
)
{
private
void
readCode
()
{
try
(
BufferedReader
b
r
=
try
(
BufferedReader
reade
r
=
new
BufferedReader
(
new
FileReader
(
nameOfTheFil
e
)))
{
new
BufferedReader
(
new
FileReader
(
m_filenam
e
)))
{
String
line
;
String
line
;
while
((
line
=
b
r
.
readLine
())
!=
null
)
{
while
((
line
=
reade
r
.
readLine
())
!=
null
)
{
m_code
.
put
(
m_lineCount
,
line
);
m_code
.
add
(
line
);
m_lineCount
++;
m_lineCount
++;
}
}
}
catch
(
FileNotFoundException
e
)
{
}
catch
(
FileNotFoundException
e
)
{
...
@@ -160,9 +169,6 @@ public class Interpreter {
...
@@ -160,9 +169,6 @@ public class Interpreter {
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
}
}
validateCode
();
exec
();
}
}
private
void
exec
()
{
private
void
exec
()
{
...
@@ -210,9 +216,10 @@ public class Interpreter {
...
@@ -210,9 +216,10 @@ public class Interpreter {
System
.
err
.
println
(
"Error: Variable "
+
name
+
" doesn't exist"
);
System
.
err
.
println
(
"Error: Variable "
+
name
+
" doesn't exist"
);
System
.
exit
(
1
);
System
.
exit
(
1
);
}
else
if
(
m_vars
.
get
(
name
)
==
0
)
{
}
else
if
(
m_vars
.
get
(
name
)
==
0
)
{
System
.
err
// System.err
.
println
(
"Error: Variable "
+
name
+
" cannot be negative"
);
// .println("Error: Variable " + name + " cannot be negative");
System
.
exit
(
1
);
// System.exit(1);
m_vars
.
put
(
name
,
Integer
.
MAX_VALUE
);
}
else
{
}
else
{
m_vars
.
put
(
name
,
m_vars
.
get
(
name
)
-
1
);
m_vars
.
put
(
name
,
m_vars
.
get
(
name
)
-
1
);
}
}
...
@@ -226,7 +233,7 @@ public class Interpreter {
...
@@ -226,7 +233,7 @@ public class Interpreter {
String
line
;
String
line
;
if
(
splitLine
[
0
].
equals
(
"while"
))
{
if
(
splitLine
[
0
].
equals
(
"while"
))
{
m_whileDepth
++;
m_whileDepth
++;
whiles
.
put
(
m_whileDepth
,
idx
);
m_
whiles
.
put
(
m_whileDepth
,
idx
);
int
condition
=
Integer
.
parseInt
(
splitLine
[
3
]);
int
condition
=
Integer
.
parseInt
(
splitLine
[
3
]);
if
(
isVariableNull
(
splitLine
[
1
])
if
(
isVariableNull
(
splitLine
[
1
])
...
@@ -238,12 +245,12 @@ public class Interpreter {
...
@@ -238,12 +245,12 @@ public class Interpreter {
}
}
}
}
}
else
{
}
else
{
line
=
m_code
.
get
(
whiles
.
get
(
m_whileDepth
));
line
=
m_code
.
get
(
m_
whiles
.
get
(
m_whileDepth
));
splitLine
=
splitLine
(
line
);
splitLine
=
splitLine
(
line
);
int
condition
=
Integer
.
parseInt
(
splitLine
[
3
]);
int
condition
=
Integer
.
parseInt
(
splitLine
[
3
]);
if
(
m_vars
.
get
(
splitLine
[
1
])
!=
condition
)
if
(
m_vars
.
get
(
splitLine
[
1
])
!=
condition
)
idx
=
whiles
.
get
(
m_whileDepth
);
idx
=
m_
whiles
.
get
(
m_whileDepth
);
else
{
else
{
m_whileDepth
--;
m_whileDepth
--;
}
}
...
...
This diff is collapsed.
Click to expand it.
wk2/Java/src/jrr1g18/bb/Main.java
+
2
−
2
View file @
31e77e27
...
@@ -3,8 +3,8 @@ package jrr1g18.bb;
...
@@ -3,8 +3,8 @@ package jrr1g18.bb;
public
class
Main
{
public
class
Main
{
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
Interpreter
interpreter
=
new
Interpreter
();
Interpreter
interpreter
=
new
Interpreter
(
"negative.bb"
);
interpreter
.
run
(
"multiply.bb"
);
interpreter
.
run
();
System
.
out
.
println
(
interpreter
.
variablesToString
());
System
.
out
.
println
(
interpreter
.
variablesToString
());
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment