-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcompile
More file actions
executable file
·51 lines (37 loc) · 1.09 KB
/
compile
File metadata and controls
executable file
·51 lines (37 loc) · 1.09 KB
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
48
49
50
51
#!/usr/bin/env ruby -s
# Script to compile a NotRuby file to Bytecode.
interpreter_path = File.expand_path("../../interpreter", __FILE__)
unless File.directory?(interpreter_path)
abort "`interpreter` directory not found.\nMove or clone the interpreter repo at: #{interpreter_path}"
end
$:.unshift File.expand_path("../../interpreter", __FILE__)
if $h # -h option
abort <<USAGE
Usage:
./compile file.rb # compile a file
./compile -e='print("hi")' # compile some code
USAGE
end
require "compiler"
require "bytecode_generator"
require "erb"
if $e
code = $e
else
code = File.read(ARGV.first)
end
result = Compiler.new(BytecodeGenerator).compile(code)
literals = result[:literals]
instructions = result[:instructions]
c = ERB.new(<<-'ERB').result(binding)
/*
This file was compiled by the `compile` script.
It contains the bytecode for the following code:
<%= code.split("\n").join("\n ") %>
*/
#define LITERALS { \<% literals.each do |literal| %>
(void *) <%= literal.inspect %>, \<% end %>
}
#define INSTRUCTIONS { <%= instructions.join(', ') %> }
ERB
File.write("bytecode.h", c)