-
Notifications
You must be signed in to change notification settings - Fork 0
/
famalam.rb
executable file
·81 lines (67 loc) · 1.8 KB
/
famalam.rb
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env ruby
module FAM; end
module FAM::Syntax; end; # Tell older v. of Ruby to shut up.
require 'famalam'
require 'sinatra'
configure do
use Rack::Session::Pool
end
set :server, 'unicorn'
set :port, 8080
set :threads, []
get '/' do
haml :index
end
post '/program' do
code = params[:code]
session[:code] = code
session[:clock] = params[:clock] || 2
session[:alloc] = params[:alloc] || 120
lexed = FAM::Syntax::Lexer.lex code
parsed = FAM::Syntax::Parser.parse lexed.tokens
tree = parsed.ast
ram = FAM::Machine::RAM.new session[:alloc]
cpu = FAM::Machine::CPU.new ram
session[:cpu] = cpu
session[:tree] = tree
session[:running] = true
end
post '/program/input' do
session[:input] = params[:input].to_i || -1 # #to_i won't work for chars, use #ord IF char
session[:cpu].inject_input session[:input]
end
get '/program/step.json' do
response = {}
puts "code: #{session[:code]}"
unless session[:cpu].nil?
status = session[:cpu].step session[:tree]
if status[:state] == 'running'
content_type :json
response = {
:ram => session[:cpu].ram.to_a,
:registers => session[:cpu].registers,
:running => session[:running],
:output => status[:output] ? session[:cpu].output : '',
:inputting => status[:input]
}
session[:cpu].inject_input session[:input]
else
response = { :ERROR => 'Program halted.' }
end
else
response = { :ERROR => 'Code not sent!' }
end
response.to_json
end
get '/program' do
haml :program
end
get '/.well-known/acme-challenge/:token' do
status 200
file_path = "/var/www/letsencrypt/.well-known/acme-challenge/#{params[:token].gsub ':', ''}"
sleep 0.1 until File.exist? file_path
file_contents = File.read file_path
body file_contents
file_contents
return
end