-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathohm
executable file
·47 lines (39 loc) · 1.54 KB
/
ohm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env ruby
# -*- mode: ruby -*-
require 'optparse'
require_relative 'ohm'
trap('SIGINT') {exit!}
opts = {
code_page: false,
debug: false,
eval: false,
length: false,
safe: false,
time: false
}
ARGV << '-h' if ARGV.empty? # Print help if no arguments passed
OptionParser.new do |parser|
parser.banner << ' <circuit>' # This is ARGV[0] after parsing
parser.on('-c', '--code-page', 'Read file <circuit> with the Ohm encoding (default: UTF-8)') {opts[:code_page] = true}
parser.on('-d', '--debug', 'Enter debug mode') {opts[:debug] = true}
parser.on('-e', '--eval', 'Evaluate <circuit> as Ohm code') {opts[:eval] = true}
parser.on('-h', '--help', 'Prints this help') {puts parser; exit}
parser.on('-l', '--length', 'Print length of program') {opts[:length] = true}
parser.on('-s', '--safe', 'Disable components like `·G` that perform network requests') {opts[:safe] = true}
parser.on('-t', '--time', 'Print time taken to execute <circuit>') {opts[:time] = true}
end.parse!
code =
if opts[:eval]
ARGV[0]
elsif opts[:code_page]
Ohm::Helpers.bin_to_ohm(File.binread(ARGV[0]))
else
File.read(ARGV[0], encoding: 'utf-8')
end
start = Time.now if opts[:time]
circuit = Ohm.new(code, debug: opts[:debug], safe: opts[:safe]).exec
the_end = Time.now if opts[:time]
puts Ohm::Helpers.untyped_to_s(circuit.stack.last[0]) unless circuit.printed
puts "Stack at end of circuit: #{circuit.stack}" if opts[:debug]
puts "Program length: #{code.length} bytes" if opts[:length]
puts "Time taken: #{the_end - start} seconds" if opts[:time]