Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 148 lines (131 sloc) 3.72 kB
5ef88e8 @charliesome bundler to avoid crazy gem conflict stuff
authored
1 require "bundler/setup"
eac1a7a @charliesome super-commit, and it's now turing complete!
authored
2 $LOAD_PATH << File.expand_path("../lib", __FILE__)
3 require "twostroke"
c40e4c3 @charliesome type system overhaul complete
authored
4 T = Twostroke::Runtime::Types
5 require "paint"
7f69eae @charliesome small fixes, syntax highlighting on repl input
authored
6 require "coderay"
eac1a7a @charliesome super-commit, and it's now turing complete!
authored
7
8 bytecode = {}
9 vm = Twostroke::Runtime::VM.new bytecode
a636074 @charliesome Error, api changes, ability to write stdlib in js too!
authored
10 Twostroke::Runtime::Lib.setup_environment vm
eac1a7a @charliesome super-commit, and it's now turing complete!
authored
11
0d8176d @charliesome better syntax highlighting in repl
authored
12 def repl_inspect(obj)
13 if obj.is_a? T::String
14 Paint[obj.string.inspect, :green]
15 elsif obj.is_a? T::Number
16 Paint[obj.number.inspect, :blue, :bright]
17 elsif obj.is_a? T::Boolean
bb044dc @charliesome improved function highlighting
authored
18 Paint[obj.boolean.inspect, :cyan, :bright]
0d8176d @charliesome better syntax highlighting in repl
authored
19 elsif obj.is_a? T::Null
20 Paint["null", :yellow]
21 elsif obj.is_a? T::Undefined
22 Paint["undefined", :yellow]
bb044dc @charliesome improved function highlighting
authored
23 elsif obj.is_a? T::Function
24 Paint[obj.name.empty? ? "[Function]" : "[Function: #{obj.name}]", :cyan]
0313081 @charliesome regexp highlighting
authored
25 elsif obj.is_a? T::RegExp
26 Paint[obj.regexp.inspect + (obj.global ? "g" : ""), :bright, :red]
0d8176d @charliesome better syntax highlighting in repl
authored
27 elsif obj.is_a? T::Array
eb9c7c0 @charliesome fix join, sparse arrays, etc
authored
28 "[#{obj.items.reject(&:nil?).map(&method(:repl_inspect)).join(", ")}]"
0d8176d @charliesome better syntax highlighting in repl
authored
29 elsif obj.is_a? T::Object and obj._class == T::Object.constructor_function
30 "{ #{obj.enum_for(:each_enumerable_property).map { |k| Paint[k, :magenta] + ": " + repl_inspect(obj.get(k)) }.join ", "} }"
31 else
32 Twostroke::Runtime::Types.to_string(obj).string
33 end
34 end
35
b667ff3 @charliesome more passing tests
authored
36 ARGV.reject { |inc| inc =~ /^--/ }.each do |inc|
ee10dc4 @charliesome nearly all underscore.js array tests succeeding
authored
37 parser = Twostroke::Parser.new(Twostroke::Lexer.new(File.read inc))
38 parser.parse
39
40 compiler = Twostroke::Compiler::TSASM.new parser.statements, "include_#{inc}_"
41 compiler.compile
42
43 compiler.bytecode.each do |k,v|
44 bytecode[k] = v
45 end
46
47 vm.execute :"include_#{inc}_main", vm.global_scope
48 end
49
eac1a7a @charliesome super-commit, and it's now turing complete!
authored
50 sect = 0
51
c40e4c3 @charliesome type system overhaul complete
authored
52 trap "SIGINT" do
53 print "\r"
7f69eae @charliesome small fixes, syntax highlighting on repl input
authored
54 system "stty -raw echo"
c40e4c3 @charliesome type system overhaul complete
authored
55 exit!
56 end
57
eac1a7a @charliesome super-commit, and it's now turing complete!
authored
58 loop do
7f69eae @charliesome small fixes, syntax highlighting on repl input
authored
59 src = ""
60 system "stty raw -echo"
c40e4c3 @charliesome type system overhaul complete
authored
61 print Paint[">>> ", :bright]
7f69eae @charliesome small fixes, syntax highlighting on repl input
authored
62 loop do
63 c = STDIN.getc
64 if c.ord == 3
65 # ctrl+c
8cd0ed9 @charliesome after numerous bugfixed, bf hello world once again works
authored
66 if src.empty?
67 print "\r"
68 system "stty -raw echo"
69 exit!
70 else
71 print "\r #{' ' * src.size}"
72 src = ""
73 end
7f69eae @charliesome small fixes, syntax highlighting on repl input
authored
74 elsif c.ord == 127
75 # backspace
76 src = src[0...-1]
77 print "\e[1D \e[1D"
78 elsif c.ord == 24
79 # ctrl+x
80 src = ""
81 elsif c.ord == 13
82 # enter
83 break
84 elsif c =~ /[[:print:]]/
85 src << c
86 end
87 print "\r"
88 print Paint[">>> ", :bright]
89 print CodeRay.scan(src, :javascript).encode :terminal
90 end
91 system "stty -raw echo"
92 puts
93 src << ";"
eac1a7a @charliesome super-commit, and it's now turing complete!
authored
94
95 begin
96 parser = Twostroke::Parser.new(Twostroke::Lexer.new(src))
97 parser.parse
98
99 sect += 1
100 compiler = Twostroke::Compiler::TSASM.new parser.statements, "repl_#{sect}_"
101 compiler.compile
102
103 compiler.bytecode.each do |k,v|
104 bytecode[k] = v
105 end
106
107 bytecode[:"repl_#{sect}_main"][-2] = [:ret] # hacky way to make main return the last evaluated value
7eff0be @charliesome expression statements that automatically pop after evaluating to prev…
authored
108 if bytecode[:"repl_#{sect}_main"][-3] == [:pop]
109 # hack around the auto pop of expression statements:
110 bytecode[:"repl_#{sect}_main"][-3] = [:ret]
111 end
f4c6ab3 @charliesome repl catches exceptions
authored
112 obj = nil
a3f20af @charliesome repl fix for empty statements
authored
113 exception = catch(:exception) { obj = vm.execute(:"repl_#{sect}_main", vm.global_scope) || Twostroke::Runtime::Types::Undefined.new; nil }
f4c6ab3 @charliesome repl catches exceptions
authored
114 if exception
115 obj = exception
eb9c7c0 @charliesome fix join, sparse arrays, etc
authored
116 vm.global_scope.set_var "$EX", obj
117 else
118 vm.global_scope.set_var "$_", obj
f4c6ab3 @charliesome repl catches exceptions
authored
119 end
0d8176d @charliesome better syntax highlighting in repl
authored
120 str = repl_inspect obj
f4c6ab3 @charliesome repl catches exceptions
authored
121 if exception
122 print Paint["!!!", :white, :red] + " "
123 else
124 print " => "
125 end
126 puts str
c40e4c3 @charliesome type system overhaul complete
authored
127 rescue => e
128 if ARGV.include? "--debug"
129 puts "#{e.class.name}: #{e.message}"
130 e.backtrace.each { |s| puts " #{s}" }
131 else
132 puts "#{e.class.name}: #{e.message}"
133 end
eac1a7a @charliesome super-commit, and it's now turing complete!
authored
134 end
135 end
136
137
138 =begin
139 vm = Twostroke::Runtime::VM.new compiler.bytecode
140 Twostroke::Runtime::Lib.setup_environment vm.global_scope
141
142 if ARGV.include? "--pry"
143 require "pry"
144 pry binding
145 end
146
147 vm.execute
148 =end
Something went wrong with that request. Please try again.