From 29e2fbce52c020a58163c33614d935315cd871e7 Mon Sep 17 00:00:00 2001
From: Mark Laptop <mark.wheelhouse@imperial.ac.uk>
Date: Wed, 12 Feb 2020 23:38:09 +0000
Subject: [PATCH] added a refEmulate script for command-line access to the ARM
 Emulator on Teaching server (plus some minor refactors of refCompile script)

---
 refCompile |  5 +--
 refEmulate | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 94 insertions(+), 2 deletions(-)
 create mode 100755 refEmulate

diff --git a/refCompile b/refCompile
index 8b7ba15..da67ca3 100755
--- a/refCompile
+++ b/refCompile
@@ -1,5 +1,5 @@
 #!/usr/bin/ruby
-# comand line script that sends an HTTPS request to the WACC compiler web interface and interprets the JSON response
+# command line script that sends an HTTPS request to the WACC compiler web interface and interprets the JSON response
 
 require "rest-client"
 require "json"
@@ -106,13 +106,14 @@ end
 files.each do |file_path|
   puts "calling the reference compiler on #{file_path}"
   
-  #get stdin from the user to pass to the reference compiler
+  # get stdin from the user to pass to the reference compiler
   stdin = ""
   if $options[:execute] then
     puts "please provide a stdin stream to use when executing the program:"
     stdin = STDIN.gets
   end
 
+  # make the call to the reference compiler
   RestClient.post("https://teaching.doc.ic.ac.uk/wacc_compiler/run.cgi", 
                     :stdin => stdin,
                     :options => $opts,
diff --git a/refEmulate b/refEmulate
new file mode 100755
index 0000000..8bf2152
--- /dev/null
+++ b/refEmulate
@@ -0,0 +1,91 @@
+#!/usr/bin/ruby
+# command line script that sends an HTTPS request to the WACC compiler web interface and interprets the JSON response
+
+require "rest-client"
+require "json"
+require "optparse"
+
+
+ARGV << "-h" if ARGV.empty?
+
+$options = {}
+$opts = []
+OptionParser.new do |opts|
+  opts.banner = "Usage: #{$0} [options] <target.s> \n  options:"
+  
+  $options[:assemble_only] = false
+  opts.on("-a", "--assemble_only",
+    "Assemble only. Check the input file for assembly errors, but do not execute the generated binary file.") do
+    $opts << "-a" 
+    $options[:assemble_only] = true
+  end  
+
+  $options[:directory] = false
+  opts.on("-d", "--directory",
+    "Give directory of ARM assembly files." ) do
+    $options[:directory] = true
+  end
+
+  opts.on_tail("-h", "--help",
+    "Show this message") do
+    puts opts
+    puts ""
+    puts "  target.s: path to ARM assembly program file to emulate (or target directory if -dir option set)"
+    exit
+  end
+
+end.parse!
+
+# set up empty array of file_paths to process
+files = []
+puts ARGV[0]
+if $options[:directory] then
+  # add the results of a search to the array
+  files += Dir.glob(File.join(ARGV[0], "**", "*.s"))
+else
+  # just add the target file to the array
+  files << ARGV[0]
+end
+
+# check that there is at least one file to process
+if files == [] then
+  puts "Error: no file(s) targeted"
+  exit 1
+end
+
+# now process each target file
+files.each do |file_path|
+  puts "calling the ARM emulator on #{file_path}"
+  
+  # get stdin from the user to pass to the ARM emulator
+  stdin = ""
+  unless $options[:assemble_only] then
+    puts "please provide a stdin stream to use when emulating the program:"
+    stdin = STDIN.gets
+  end
+
+  # make the call to the ARM emulator
+  RestClient.post("https://teaching.doc.ic.ac.uk/wacc_compiler/emulate.cgi",
+                    :stdin => stdin,
+                    :options => $opts,
+                    :testfile => File.new(file_path)
+                   ) do |response|
+    json = JSON.load(response)
+    puts "-- Test: #{json['test']}"
+    puts ""
+    puts "-- Uploaded file: "
+    puts "---------------------------------------------------------------"
+    puts json['upload']
+    puts "---------------------------------------------------------------"
+    puts ""
+    puts "-- Assembly Output:"
+    puts json['assemble_out']
+    puts ""
+    unless $options[:assemble_only] then
+      puts "-- Emulation Output:" 
+      puts json['emulator_out']
+      puts ""
+    end
+  end
+  
+end
-- 
GitLab