Description:
rewrote std-scripts/compile with ruby
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

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