Description:
rewrote std-scripts/compile with ruby
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r107:c992b57df61b - - 1 file changed: 84 inserted, 82 deleted
@@ -1,4 +1,6 | |||||
|
1 | - #!/bin/sh |
|
1 | + #!/usr/bin/ruby |
|
|
2 | + | ||
|
|
3 | + require 'fileutils' | ||
|
2 |
|
4 | ||
|
3 | ############################## |
|
5 | ############################## |
|
4 | # |
|
6 | # |
@@ -9,99 +11,99 | |||||
|
9 | # |
|
11 | # |
|
10 | ############################## |
|
12 | ############################## |
|
11 |
|
13 | ||
|
12 |
- talk |
|
14 | + def talk(msg) |
|
13 | - { |
|
15 | + if ENV['TALKATIVE']!=nil |
|
14 | - if [ "$TALKATIVE" != "" ]; then |
|
16 | + puts str |
|
15 | - echo "$1" |
|
17 | + end |
|
16 | - fi |
|
18 | + if ENV['GRADER_LOGGING']!=nil |
|
17 | - } |
|
19 | + log_fname = ENV['GRADER_LOGGING'] |
|
|
20 | + fp = File.open(log_fname,"a") | ||
|
|
21 | + fp.puts("run: #{Time.new.strftime("%H:%M")} #{str}") | ||
|
|
22 | + fp.close | ||
|
|
23 | + end | ||
|
|
24 | + end | ||
|
18 |
|
25 | ||
|
19 |
- |
|
26 | + C_COMPILER = "/usr/bin/gcc" |
|
20 |
- |
|
27 | + CPLUSPLUS_COMPILER = "/usr/bin/g++" |
|
21 |
- |
|
28 | + PASCAL_COMPILER = "/usr/bin/fpc" |
|
22 |
|
29 | ||
|
23 |
- |
|
30 | + C_OPTIONS = "-O2 -s -static -std=c99 -DCONTEST -lm -Wall" |
|
24 |
- |
|
31 | + CPLUSPLUS_OPTIONS = "-O2 -s -static -DCONTEST -lm -Wall" |
|
25 |
- |
|
32 | + PASCAL_OPTIONS = "-O1 -XS -dCONTEST" |
|
26 |
|
33 | ||
|
27 | # Check for the correct number of arguments. Otherwise, print usage. |
|
34 | # Check for the correct number of arguments. Otherwise, print usage. |
|
28 | - if [ $# -eq 0 -o $# -gt 4 ] |
|
35 | + if ARGV.length == 0 or ARGV.length > 4 |
|
29 | - then |
|
36 | + puts "Usage: compile <language> [<source-file>] [<output-file>] [<message-file>]" |
|
30 | - echo "Usage: $0 <language> [<source-file>] [<output-file>] [<message-file>]" |
|
37 | + puts |
|
31 | - echo |
|
38 | + puts "<source-file> is defaulted to \"source\"." |
|
32 |
- |
|
39 | + puts "<output-file> is defaulted to \"a.out\"." |
|
33 |
- |
|
40 | + puts "<message-file> is defaulted to \"compiler_message\"." |
|
34 | - echo "<message-file> is defaulted to \"compiler_message\"." |
|
41 | + puts |
|
35 | - echo |
|
42 | + exit(127) |
|
36 | - exit 127 |
|
43 | + end |
|
37 | - fi |
|
||
|
38 | - |
|
||
|
39 | - # Retrieve the arguments. |
|
||
|
40 | - if [ $# -ge 1 ] |
|
||
|
41 | - then |
|
||
|
42 | - export PROG_LANG=$1 |
|
||
|
43 | - talk "programming language: ${PROG_LANG}" |
|
||
|
44 | - fi |
|
||
|
45 |
|
44 | ||
|
46 | - if [ $# -ge 2 ] |
|
45 | + PARAMS = { |
|
47 | - then |
|
46 | + :source_file => [1,'source'], |
|
48 | - export SOURCE_FILE=$2 |
|
47 | + :output_file => [2,'a.out'], |
|
49 | - else |
|
48 | + :message_file => [3,'compiler_message'] |
|
50 | - export SOURCE_FILE=source |
|
49 | + } |
|
51 | - fi |
|
||
|
52 | - talk " source file: $SOURCE_FILE" |
|
||
|
53 |
|
50 | ||
|
54 | - if [ $# -ge 3 ] |
|
51 | + params = {} |
|
55 | - then |
|
52 | + params[:prog_lang] = ARGV[0] |
|
56 | - export OUTPUT_FILE=$3 |
|
53 | + PARAMS.each_key do |param_name| |
|
57 | - else |
|
54 | + index, default = PARAMS[param_name] |
|
58 | - export OUTPUT_FILE=a.out |
|
55 | + if ARGV.length > index |
|
59 | - fi |
|
56 | + params[param_name] = ARGV[index] |
|
60 | - talk " output file: $OUTPUT_FILE" |
|
57 | + else |
|
61 | - |
|
58 | + params[param_name] = default |
|
62 | - if [ $# -eq 4 ] |
|
59 | + end |
|
63 | - then |
|
60 | + talk "#{param_name}: #{params[param_name]}" |
|
64 | - export MESSAGE_FILE=$4 |
|
61 | + end |
|
65 | - else |
|
||
|
66 | - export MESSAGE_FILE=compiler_message |
|
||
|
67 | - fi |
|
||
|
68 | - talk " message file: $MESSAGE_FILE" |
|
||
|
69 |
|
62 | ||
|
70 | # Remove any remaining output files or message files. |
|
63 | # Remove any remaining output files or message files. |
|
71 | - rm -Rf $OUTPUT_FILE |
|
64 | + if FileTest.exists? params[:output_file] |
|
72 | - rm -Rf $MESSAGE_FILE |
|
65 | + FileUtils.rm(params[:output_file]) |
|
|
66 | + end | ||
|
|
67 | + if FileTest.exists? params[:message_file] | ||
|
|
68 | + FileUtils.rm(params[:message_file]) | ||
|
|
69 | + end | ||
|
73 |
|
70 | ||
|
74 | # Check if the source file exists before attempt compiling. |
|
71 | # Check if the source file exists before attempt compiling. |
|
75 | - if [ ! -f $SOURCE_FILE ] |
|
72 | + if !FileTest.exists? params[:source_file] |
|
76 | - then |
|
73 | + talk("ERROR: The source file does not exist!") |
|
77 | - talk "ERROR: The source file does not exist!" |
|
74 | + open(params[:message_file],"w") do |f| |
|
78 |
- |
|
75 | + f.puts "ERROR: The source file did not exist." |
|
79 | - exit 127 |
|
76 | + end |
|
80 | - fi |
|
77 | + exit(127) |
|
|
78 | + end | ||
|
81 |
|
79 | ||
|
82 | # Compile. |
|
80 | # Compile. |
|
83 | - if [ $PROG_LANG = "c" ] |
|
81 | + case params[:prog_lang] |
|
84 | - then |
|
82 | + |
|
85 | - $C_COMPILER $SOURCE_FILE -o $OUTPUT_FILE $C_OPTIONS 2>$MESSAGE_FILE |
|
83 | + when "c" |
|
86 | - elif [ $PROG_LANG = "c++" ] |
|
84 | + command = "#{C_COMPILER} #{params[:source_file]} -o #{params[:output_file]} #{C_OPTIONS} 2> #{params[:message_file]}" |
|
87 | - then |
|
85 | + system(command) |
|
88 | - $CPLUSPLUS_COMPILER $SOURCE_FILE -o $OUTPUT_FILE $CPLUSPLUS_OPTIONS 2>$MESSAGE_FILE |
|
86 | + |
|
89 | - elif [ $PROG_LANG = "pas" ] |
|
87 | + when "c++" |
|
90 | - then |
|
88 | + command = "#{CPLUSPLUS_COMPILER} #{params[:source_file]} -o #{params[:output_file]} #{CPLUSPLUS_OPTIONS} 2> #{params[:message_file]}" |
|
91 | - $PASCAL_COMPILER $SOURCE_FILE -ooutpas $PASCAL_OPTIONS >$MESSAGE_FILE |
|
89 | + system(command) |
|
92 | - mv outpas $OUTPUT_FILE |
|
90 | + |
|
93 | - else |
|
91 | + when "pas" |
|
94 | - talk "ERROR: Invalid language specified!" |
|
92 | + command = "#{PASCAL_COMPILER} #{params[:source_file]} -ooutpas #{PASCAL_OPTIONS} > #{params[:message_file]}" |
|
95 | - echo "ERROR: Invalid language specified!" > $MESSAGE_FILE |
|
93 | + system(command) |
|
96 | - exit 127 |
|
94 | + FileUtils.mv("output", params[:output_file]) |
|
97 | - fi |
|
95 | + |
|
|
96 | + else | ||
|
|
97 | + talk("ERROR: Invalid language specified!") | ||
|
|
98 | + open(params[:message_file],"w") do |f| | ||
|
|
99 | + f.puts "ERROR: Invalid language specified!" | ||
|
|
100 | + end | ||
|
|
101 | + exit(127) | ||
|
|
102 | + end | ||
|
98 |
|
103 | ||
|
99 | # Report success or failure. |
|
104 | # Report success or failure. |
|
100 | - if [ -f $OUTPUT_FILE ] |
|
105 | + if FileTest.exists? params[:output_file] |
|
101 | - then |
|
106 | + talk "Compilation was successful!" |
|
102 | - talk "Compilation was successful!" |
|
||
|
103 | else |
|
107 | else |
|
104 |
- |
|
108 | + talk "ERROR: Something was wrong during the compilation!" |
|
105 | - talk "Dumping compiler message:" |
|
109 | + end |
|
106 | - #cat $MESSAGE_FILE |
|
||
|
107 | - fi |
|
You need to be logged in to leave comments.
Login now