Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Sven Gestegård Robertz
LabComm
Commits
69cb93b4
Commit
69cb93b4
authored
Sep 05, 2014
by
Sven Gestegård Robertz
Browse files
refactored compiler main method into separate methods
parent
df2a18ef
Changes
2
Hide whitespace changes
Inline
Side-by-side
compiler/C_CodeGen.jrag
View file @
69cb93b4
...
...
@@ -56,7 +56,10 @@ aspect C_CodeGenEnv {
this.qualid = qualid;
this.lcName = lcName;
this.rawPrefix = rawPrefix;
if (rawPrefix.equals("")) {
if (rawPrefix == null) {
System.err.println("WARNING: prefix==null");
this.prefix = "";
} else if (rawPrefix.equals("")) {
this.prefix = rawPrefix;
} else {
this.prefix = rawPrefix + "_";
...
...
compiler/LabComm.java
View file @
69cb93b4
...
...
@@ -37,25 +37,6 @@ public class LabComm {
println
(
" --typeinfo=TIFILE Generates typeinfo in TIFILE"
);
}
private
static
String
getCoreName
(
String
s
)
{
int
i
=
s
.
lastIndexOf
(
'.'
);
return
s
.
substring
(
0
,
i
>
0
?
i
:
s
.
length
());
}
private
static
String
getFileName
(
String
s
)
{
return
s
.
substring
(
s
.
lastIndexOf
(
'/'
)
+
1
,
s
.
length
());
}
private
static
String
getBaseName
(
String
s
)
{
s
=
getFileName
(
s
);
int
i
=
s
.
lastIndexOf
(
'.'
);
return
s
.
substring
(
0
,
i
>
0
?
i
:
s
.
length
());
}
private
static
String
getPrefix
(
String
s
)
{
return
s
.
substring
(
s
.
lastIndexOf
(
'/'
)
+
1
,
s
.
length
());
}
/** To be cleaned up.
*/
private
static
void
checkVersion
(
int
v
)
{
...
...
@@ -66,199 +47,6 @@ public class LabComm {
}
}
public
static
void
main
(
String
[]
args
)
{
String
fileName
=
null
;
// Scan for first non-option
for
(
int
i
=
0
;
i
<
args
.
length
;
i
++)
{
if
(!
args
[
i
].
startsWith
(
"-"
))
{
fileName
=
args
[
i
];
break
;
}
}
String
coreName
=
null
;
String
prefix
=
null
;
if
(
fileName
!=
null
)
{
coreName
=
getBaseName
(
fileName
);
prefix
=
getPrefix
(
coreName
);
}
boolean
verbose
=
false
;
int
ver
=
2013
;
//Version 2013 as default
String
cFile
=
null
;
String
hFile
=
null
;
Vector
cIncludes
=
new
Vector
();
String
cPrefix
=
prefix
;
String
csFile
=
null
;
String
csNamespace
=
null
;
String
javaDir
=
null
;
String
javaPackage
=
""
;
String
pythonFile
=
null
;
String
prettyFile
=
null
;
String
typeinfoFile
=
null
;
String
rapidFile
=
null
;
for
(
int
i
=
0
;
i
<
args
.
length
;
i
++)
{
if
(
fileName
==
null
||
args
[
i
].
equals
(
"-help"
)
||
args
[
i
].
equals
(
"-h"
)
||
args
[
i
].
equals
(
"--help"
))
{
print_help
();
System
.
exit
(
0
);
}
else
if
(
args
[
i
].
equals
(
"-v"
))
{
verbose
=
true
;
}
else
if
(
args
[
i
].
startsWith
(
"--ver="
))
{
ver
=
Integer
.
parseInt
(
args
[
i
].
substring
(
6
));
checkVersion
(
ver
);
}
else
if
(
args
[
i
].
equals
(
"-C"
))
{
cFile
=
coreName
+
".c"
;
hFile
=
coreName
+
".h"
;
}
else
if
(
args
[
i
].
startsWith
(
"--cinclude="
))
{
cIncludes
.
add
(
args
[
i
].
substring
(
11
));
}
else
if
(
args
[
i
].
startsWith
(
"--cprefix="
))
{
cPrefix
=
args
[
i
].
substring
(
10
);
}
else
if
(
args
[
i
].
startsWith
(
"--c="
))
{
cFile
=
args
[
i
].
substring
(
4
);
}
else
if
(
args
[
i
].
startsWith
(
"--h="
))
{
hFile
=
args
[
i
].
substring
(
4
);
}
else
if
(
args
[
i
].
equals
(
"--cs"
))
{
csFile
=
coreName
+
".cs"
;
}
else
if
(
args
[
i
].
startsWith
(
"--cs="
))
{
csFile
=
args
[
i
].
substring
(
5
);
}
else
if
(
args
[
i
].
startsWith
(
"--csnamespace="
))
{
csNamespace
=
args
[
i
].
substring
(
14
);
}
else
if
(
args
[
i
].
startsWith
(
"--java="
))
{
javaDir
=
args
[
i
].
substring
(
7
);
}
else
if
(
args
[
i
].
startsWith
(
"--javapackage="
))
{
javaPackage
=
args
[
i
].
substring
(
14
);
}
else
if
(
args
[
i
].
equals
(
"-P"
))
{
pythonFile
=
coreName
+
".py"
;
}
else
if
(
args
[
i
].
startsWith
(
"--python="
))
{
pythonFile
=
args
[
i
].
substring
(
9
);
}
else
if
(
args
[
i
].
startsWith
(
"--pretty="
))
{
prettyFile
=
args
[
i
].
substring
(
9
);
}
else
if
(
args
[
i
].
startsWith
(
"--typeinfo="
))
{
typeinfoFile
=
args
[
i
].
substring
(
11
);
}
else
if
(
args
[
i
].
equals
(
"--rapid"
))
{
rapidFile
=
coreName
+
".sys"
;
}
else
if
(
i
==
args
.
length
-
1
)
{
fileName
=
args
[
i
];
}
else
{
System
.
err
.
println
(
" Unknown argument "
+
args
[
i
]);
print_help
();
System
.
exit
(
2
);
}
}
if
(
fileName
==
null
)
{
print_help
();
System
.
exit
(
1
);
}
else
{
Program
ast
=
null
;
try
{
// Check for errors
LabCommScanner
scanner
=
new
LabCommScanner
(
new
FileReader
(
fileName
));
LabCommParser
parser
=
new
LabCommParser
();
Program
p
=
(
Program
)
parser
.
parse
(
scanner
);
Collection
errors
=
new
LinkedList
();
p
.
errorCheck
(
errors
);
if
(
errors
.
isEmpty
())
{
ast
=
p
;
}
else
{
for
(
Iterator
iter
=
errors
.
iterator
();
iter
.
hasNext
();
)
{
String
s
=
(
String
)
iter
.
next
();
System
.
out
.
println
(
s
);
}
}
}
catch
(
FileNotFoundException
e
)
{
System
.
err
.
println
(
"Could not find file: "
+
fileName
);
}
catch
(
IOException
e
)
{
System
.
err
.
println
(
"IOException: "
+
fileName
+
" "
+
e
);
}
catch
(
beaver
.
Parser
.
Exception
e
)
{
System
.
err
.
println
(
e
.
getMessage
());
}
if
(
ast
!=
null
)
{
Vector
hIncludes
=
new
Vector
(
cIncludes
);
if
(
hFile
!=
null
)
{
cIncludes
.
add
(
hFile
);
}
boolean
prettyOnStdout
=
true
;
if
(
cFile
!=
null
)
{
if
(
verbose
)
{
System
.
err
.
println
(
"Generating C: "
+
cFile
);
}
genC
(
ast
,
cFile
,
cIncludes
,
coreName
,
cPrefix
,
ver
);
prettyOnStdout
=
false
;
}
if
(
hFile
!=
null
)
{
if
(
verbose
)
{
System
.
err
.
println
(
"Generating H: "
+
hFile
);
}
genH
(
ast
,
hFile
,
hIncludes
,
coreName
,
cPrefix
,
ver
);
prettyOnStdout
=
false
;
}
if
(
csFile
!=
null
)
{
if
(
verbose
)
{
System
.
err
.
println
(
"Generating C#: "
+
csFile
);
}
genCS
(
ast
,
csFile
,
csNamespace
,
ver
);
prettyOnStdout
=
false
;
}
if
(
javaDir
!=
null
)
{
if
(
verbose
)
{
System
.
err
.
println
(
"Generating Java: "
+
javaDir
);
}
genJava
(
ast
,
javaDir
,
javaPackage
,
ver
);
prettyOnStdout
=
false
;
}
if
(
pythonFile
!=
null
)
{
if
(
verbose
)
{
System
.
err
.
println
(
"Generating Python: "
+
pythonFile
);
}
genPython
(
ast
,
pythonFile
,
prefix
,
ver
);
prettyOnStdout
=
false
;
}
if
(
rapidFile
!=
null
)
{
if
(
verbose
)
{
System
.
err
.
println
(
"Generating RAPID: "
+
rapidFile
);
}
genRAPID
(
ast
,
rapidFile
,
coreName
,
ver
);
prettyOnStdout
=
false
;
}
if
(
prettyFile
!=
null
)
{
if
(
verbose
)
{
System
.
err
.
println
(
"Generating Pretty: "
+
prettyFile
);
}
try
{
FileOutputStream
f
=
new
FileOutputStream
(
prettyFile
);
PrintStream
out
=
new
PrintStream
(
f
);
ast
.
pp
(
out
);
out
.
close
();
prettyOnStdout
=
false
;
}
catch
(
IOException
e
)
{
System
.
err
.
println
(
"IOException: "
+
prettyFile
+
" "
+
e
);
}
}
if
(
typeinfoFile
!=
null
)
{
if
(
verbose
)
{
System
.
err
.
println
(
"Generating TypeInfo: "
+
typeinfoFile
);
}
try
{
FileOutputStream
f
=
new
FileOutputStream
(
typeinfoFile
);
PrintStream
out
=
new
PrintStream
(
f
);
ast
.
C_info
(
out
,
cPrefix
,
ver
);
ast
.
Java_info
(
out
,
ver
);
ast
.
CS_info
(
out
,
csNamespace
,
ver
);
prettyOnStdout
=
false
;
}
catch
(
IOException
e
)
{
System
.
err
.
println
(
"IOException: "
+
typeinfoFile
+
" "
+
e
);
}
}
if
(
prettyOnStdout
)
{
ast
.
pp
(
System
.
out
);
}
}
else
{
// Catch-all for compilation errors
System
.
err
.
println
(
"Error in specification"
);
System
.
exit
(
3
);
}
}
}
private
static
void
genH
(
Program
p
,
String
hName
,
Vector
cIncludes
,
String
coreName
,
String
prefix
,
int
ver
)
{
try
{
...
...
@@ -328,4 +116,289 @@ public class LabComm {
System
.
err
.
println
(
"IOException: "
+
filename
+
" "
+
e
);
}
}
/** Helper class to contain command line options
and their associated behaviour
**/
private
static
class
Opts
{
final
String
[]
args
;
String
coreName
=
null
;
String
prefix
=
null
;
boolean
verbose
=
false
;
int
ver
=
2013
;
//Version 2013 as default
String
cFile
=
null
;
String
hFile
=
null
;
Vector
cIncludes
=
new
Vector
();
String
cPrefix
;
// gets default value (prefix) in processFilename
String
csFile
=
null
;
String
csNamespace
=
null
;
String
javaDir
=
null
;
String
javaPackage
=
""
;
String
pythonFile
=
null
;
String
prettyFile
=
null
;
String
typeinfoFile
=
null
;
String
rapidFile
=
null
;
String
fileName
=
null
;
Opts
(
String
[]
args
)
{
this
.
args
=
args
;
}
private
static
String
getCoreName
(
String
s
)
{
int
i
=
s
.
lastIndexOf
(
'.'
);
return
s
.
substring
(
0
,
i
>
0
?
i
:
s
.
length
());
}
private
static
String
getFileName
(
String
s
)
{
return
s
.
substring
(
s
.
lastIndexOf
(
'/'
)
+
1
,
s
.
length
());
}
private
static
String
getBaseName
(
String
s
)
{
s
=
getFileName
(
s
);
int
i
=
s
.
lastIndexOf
(
'.'
);
return
s
.
substring
(
0
,
i
>
0
?
i
:
s
.
length
());
}
private
static
String
getPrefix
(
String
s
)
{
return
s
.
substring
(
s
.
lastIndexOf
(
'/'
)
+
1
,
s
.
length
());
}
boolean
processFilename
(){
// Scan for first non-option
for
(
int
i
=
0
;
i
<
args
.
length
;
i
++)
{
if
(!
args
[
i
].
startsWith
(
"-"
))
{
fileName
=
args
[
i
];
break
;
}
}
if
(
fileName
!=
null
)
{
coreName
=
getBaseName
(
fileName
);
prefix
=
getPrefix
(
coreName
);
cPrefix
=
prefix
;
}
return
fileName
!=
null
;
}
void
processArgs
(){
for
(
int
i
=
0
;
i
<
args
.
length
;
i
++)
{
if
(
fileName
==
null
||
args
[
i
].
equals
(
"-help"
)
||
args
[
i
].
equals
(
"-h"
)
||
args
[
i
].
equals
(
"--help"
))
{
print_help
();
System
.
exit
(
0
);
}
else
if
(
args
[
i
].
equals
(
"-v"
))
{
verbose
=
true
;
}
else
if
(
args
[
i
].
startsWith
(
"--ver="
))
{
ver
=
Integer
.
parseInt
(
args
[
i
].
substring
(
6
));
checkVersion
(
ver
);
}
else
if
(
args
[
i
].
equals
(
"-C"
))
{
cFile
=
coreName
+
".c"
;
hFile
=
coreName
+
".h"
;
}
else
if
(
args
[
i
].
startsWith
(
"--cinclude="
))
{
cIncludes
.
add
(
args
[
i
].
substring
(
11
));
}
else
if
(
args
[
i
].
startsWith
(
"--cprefix="
))
{
cPrefix
=
args
[
i
].
substring
(
10
);
}
else
if
(
args
[
i
].
startsWith
(
"--c="
))
{
cFile
=
args
[
i
].
substring
(
4
);
}
else
if
(
args
[
i
].
startsWith
(
"--h="
))
{
hFile
=
args
[
i
].
substring
(
4
);
}
else
if
(
args
[
i
].
equals
(
"--cs"
))
{
csFile
=
coreName
+
".cs"
;
}
else
if
(
args
[
i
].
startsWith
(
"--cs="
))
{
csFile
=
args
[
i
].
substring
(
5
);
}
else
if
(
args
[
i
].
startsWith
(
"--csnamespace="
))
{
csNamespace
=
args
[
i
].
substring
(
14
);
}
else
if
(
args
[
i
].
startsWith
(
"--java="
))
{
javaDir
=
args
[
i
].
substring
(
7
);
}
else
if
(
args
[
i
].
startsWith
(
"--javapackage="
))
{
javaPackage
=
args
[
i
].
substring
(
14
);
}
else
if
(
args
[
i
].
equals
(
"-P"
))
{
pythonFile
=
coreName
+
".py"
;
}
else
if
(
args
[
i
].
startsWith
(
"--python="
))
{
pythonFile
=
args
[
i
].
substring
(
9
);
}
else
if
(
args
[
i
].
startsWith
(
"--pretty="
))
{
prettyFile
=
args
[
i
].
substring
(
9
);
}
else
if
(
args
[
i
].
startsWith
(
"--typeinfo="
))
{
typeinfoFile
=
args
[
i
].
substring
(
11
);
}
else
if
(
args
[
i
].
equals
(
"--rapid"
))
{
rapidFile
=
coreName
+
".sys"
;
}
else
if
(
i
==
args
.
length
-
1
)
{
fileName
=
args
[
i
];
}
else
{
System
.
err
.
println
(
" Unknown argument "
+
args
[
i
]);
print_help
();
System
.
exit
(
2
);
}
}
if
(
prefix
==
null
){
System
.
err
.
println
(
" WARNING! prefix==null"
);
prefix
=
""
;
}
}
Program
parseFile
(){
Program
ast
=
null
;
try
{
// Check for errors
LabCommScanner
scanner
=
new
LabCommScanner
(
new
FileReader
(
fileName
));
LabCommParser
parser
=
new
LabCommParser
();
Program
p
=
(
Program
)
parser
.
parse
(
scanner
);
Collection
errors
=
new
LinkedList
();
p
.
errorCheck
(
errors
);
if
(
errors
.
isEmpty
())
{
ast
=
p
;
}
else
{
for
(
Iterator
iter
=
errors
.
iterator
();
iter
.
hasNext
();
)
{
String
s
=
(
String
)
iter
.
next
();
System
.
out
.
println
(
s
);
}
}
}
catch
(
FileNotFoundException
e
)
{
System
.
err
.
println
(
"Could not find file: "
+
fileName
);
}
catch
(
IOException
e
)
{
System
.
err
.
println
(
"IOException: "
+
fileName
+
" "
+
e
);
}
catch
(
beaver
.
Parser
.
Exception
e
)
{
System
.
err
.
println
(
e
.
getMessage
());
}
return
ast
;
}
boolean
generateC
(
Program
ast
)
{
boolean
wroteFile
=
false
;
Vector
hIncludes
=
new
Vector
(
cIncludes
);
if
(
hFile
!=
null
)
{
cIncludes
.
add
(
hFile
);
}
if
(
cFile
!=
null
)
{
if
(
verbose
)
{
System
.
err
.
println
(
"Generating C: "
+
cFile
);
}
genC
(
ast
,
cFile
,
cIncludes
,
coreName
,
cPrefix
,
ver
);
wroteFile
=
true
;
}
if
(
hFile
!=
null
)
{
if
(
verbose
)
{
System
.
err
.
println
(
"Generating H: "
+
hFile
);
}
genH
(
ast
,
hFile
,
hIncludes
,
coreName
,
cPrefix
,
ver
);
wroteFile
=
true
;
}
return
wroteFile
;
}
boolean
generateCS
(
Program
ast
)
{
boolean
wroteFile
=
false
;
if
(
csFile
!=
null
)
{
if
(
verbose
)
{
System
.
err
.
println
(
"Generating C#: "
+
csFile
);
}
genCS
(
ast
,
csFile
,
csNamespace
,
ver
);
wroteFile
=
true
;
}
return
wroteFile
;
}
boolean
generateJava
(
Program
ast
)
{
boolean
wroteFile
=
false
;
if
(
javaDir
!=
null
)
{
if
(
verbose
)
{
System
.
err
.
println
(
"Generating Java: "
+
javaDir
);
}
genJava
(
ast
,
javaDir
,
javaPackage
,
ver
);
wroteFile
=
true
;
}
return
wroteFile
;
}
boolean
generatePython
(
Program
ast
)
{
boolean
wroteFile
=
false
;
if
(
pythonFile
!=
null
)
{
if
(
verbose
)
{
System
.
err
.
println
(
"Generating Python: "
+
pythonFile
);
}
genPython
(
ast
,
pythonFile
,
prefix
,
ver
);
wroteFile
=
true
;
}
return
wroteFile
;
}
boolean
generateRAPID
(
Program
ast
)
{
boolean
wroteFile
=
false
;
if
(
rapidFile
!=
null
)
{
if
(
verbose
)
{
System
.
err
.
println
(
"Generating RAPID: "
+
rapidFile
);
}
genRAPID
(
ast
,
rapidFile
,
coreName
,
ver
);
wroteFile
=
true
;
}
return
wroteFile
;
}
boolean
generatePrettyPrint
(
Program
ast
)
{
boolean
wroteFile
=
false
;
if
(
prettyFile
!=
null
)
{
if
(
verbose
)
{
System
.
err
.
println
(
"Generating Pretty: "
+
prettyFile
);
}
try
{
FileOutputStream
f
=
new
FileOutputStream
(
prettyFile
);
PrintStream
out
=
new
PrintStream
(
f
);
ast
.
pp
(
out
);
out
.
close
();
wroteFile
=
true
;
}
catch
(
IOException
e
)
{
System
.
err
.
println
(
"IOException: "
+
prettyFile
+
" "
+
e
);
}
}
return
wroteFile
;
}
boolean
generateTypeinfo
(
Program
ast
)
{
boolean
wroteFile
=
false
;
if
(
typeinfoFile
!=
null
)
{
if
(
verbose
)
{
System
.
err
.
println
(
"Generating TypeInfo: "
+
typeinfoFile
);
}
try
{
FileOutputStream
f
=
new
FileOutputStream
(
typeinfoFile
);
PrintStream
out
=
new
PrintStream
(
f
);
ast
.
C_info
(
out
,
cPrefix
,
ver
);
ast
.
Java_info
(
out
,
ver
);
ast
.
CS_info
(
out
,
csNamespace
,
ver
);
wroteFile
=
true
;
}
catch
(
IOException
e
)
{
System
.
err
.
println
(
"IOException: "
+
typeinfoFile
+
" "
+
e
);
}
}
return
wroteFile
;
}
}
public
static
void
main
(
String
[]
args
)
{
Opts
opts
=
new
Opts
(
args
);
if
(!
opts
.
processFilename
())
{
print_help
();
System
.
exit
(
1
);
}
else
{
opts
.
processArgs
();
Program
ast
=
opts
.
parseFile
();
if
(
ast
!=
null
)
{
boolean
fileWritten
=
false
;
fileWritten
|=
opts
.
generateC
(
ast
);
fileWritten
|=
opts
.
generateCS
(
ast
);
fileWritten
|=
opts
.
generateJava
(
ast
);
fileWritten
|=
opts
.
generatePython
(
ast
);
fileWritten
|=
opts
.
generateRAPID
(
ast
);
fileWritten
|=
opts
.
generatePrettyPrint
(
ast
);
fileWritten
|=
opts
.
generateTypeinfo
(
ast
);
// if no output to files, prettyprint on stdout
if
(!
fileWritten
)
{
ast
.
pp
(
System
.
out
);
}
}
else
{
// Catch-all for compilation errors
System
.
err
.
println
(
"Error in specification"
);
System
.
exit
(
3
);
}
}
}
}
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