lrb 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +16 -19
  3. data/bin/lrb +4 -1
  4. data/lib/lrb.rb +127 -18
  5. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f1cd3e16d04456b052cdabcd6ad3fd3509d5f4bd
4
- data.tar.gz: 7db9624b48b357c1bb23fe4b280018c70945d73f
3
+ metadata.gz: 91b1b3463d0a8c5618c45cfe15e829cdd7340ac6
4
+ data.tar.gz: 2318438a0a671cc8b1e7f12adb30e35e9a5f7bac
5
5
  SHA512:
6
- metadata.gz: f9f53921407081952ac231180a3728a38ac2a612557e6d3a70ff8ce4948a5fbb44dcee6572cbf5110d6cf2184a6447f64df3f61744fd0f121f98fe284b5f0707
7
- data.tar.gz: 5b068a2f5f1b8dfd166e12fae9dbca72ca2cd46b0976eaa90745fa7869b630f1a31c09fedabafa387200206ef3eedaf134f712863824918f7a36f31d459c3d86
6
+ metadata.gz: 180ec2126d1c55a186d55c7b6ac69fdabc3f7991098d88b5b27f5cbaa7e8d54299974e5ab3f8dc2a5553da37942342c9ca1ed9e9b94c0b33feaa9af5593412b6
7
+ data.tar.gz: 9839e93cc80d0cec69d4f40dcf0561d0954d3aa2e5b2aad884062b2630d2580cc222363d2f90cf6674ff6b20e17ea8ce28cb6bcf1db888b8621d4ce63fabdf64
data/README.md CHANGED
@@ -5,6 +5,18 @@
5
5
 
6
6
  gem install lrb
7
7
 
8
+ \* To Start REPL:
9
+
10
+ lrb
11
+
12
+ \* for debug mode:
13
+
14
+ [debug]
15
+
16
+ \* To compile & run a source file:
17
+
18
+ lrb test.lrb
19
+
8
20
  \* LRB source look like lisp, but with braces:
9
21
 
10
22
  [require "colorize"]
@@ -168,19 +180,6 @@
168
180
  elsif true
169
181
  puts "as always"
170
182
  end
171
- 100
172
- 3628800
173
- only block !
174
- 1st statement
175
- 2nd statement
176
- true
177
- Trung
178
- Hello Trung !
179
- [4]
180
- Heello
181
- 1
182
- 2
183
- 3
184
183
 
185
184
 
186
185
  \* And evaluate everything:
@@ -199,14 +198,12 @@
199
198
  1
200
199
  2
201
200
  3
201
+ one is not lesser than zero
202
+ 4
203
+ 2
204
+ 1 >= 0
202
205
 
203
206
 
204
- \* To run a source such as `test.lrb` in this repo:
205
-
206
- lrb file_name.lrb
207
-
208
- It will compile and run just as above.
209
-
210
207
  Currently, it's only experiment toy,
211
208
  not yet support `repl` or `macros`.
212
209
 
data/bin/lrb CHANGED
@@ -16,13 +16,16 @@ spec = Oyster.spec do
16
16
  'Start an interactive Scheme session'
17
17
  end
18
18
 
19
+ require "rubygems"
19
20
 
20
21
  begin
21
22
  options = spec.parse
22
23
 
23
24
  if options[:interactive] or options[:unclaimed].empty?
24
- puts "Not implemented yet"
25
+ puts "lrb:repl".light_yellow
26
+ Lrb.new.repl
25
27
  else
28
+ puts "lrb:compile".light_yellow
26
29
  lrb = Lrb.new
27
30
  lrb.eval_file File.expand_path(options[:unclaimed].first)
28
31
  end
data/lib/lrb.rb CHANGED
@@ -1,5 +1,7 @@
1
+ require "readline"
1
2
  require "colorize"
2
3
  require "bracer"
4
+ # require "sexpistol"
3
5
  require "pry"
4
6
 
5
7
  class Lrb
@@ -11,27 +13,65 @@ class Lrb
11
13
  @stack = Array.new
12
14
  @parser = Bracer.new
13
15
  @parser.ruby_keyword_literals = true
16
+ readline_autocompletion
14
17
  end
15
18
 
16
19
  def parse source
17
20
  @parser.parse_string source
18
21
  end
19
22
 
23
+ def repl
24
+ begin
25
+ while line = readline_with_hist_management
26
+ @stack << line
27
+ if balanced?
28
+ e = eval_source @stack.join " "
29
+ puts e.to_s.blue
30
+ @stack.clear
31
+ @indent = 0
32
+ end
33
+ end
34
+ rescue Interrupt => e
35
+ puts "\nGoodbye !".light_yellow
36
+ exit
37
+ end
38
+ end
39
+
20
40
  def eval_file path
21
41
  source = File.read path
22
- puts "[LRB]:".light_yellow
23
- puts source.light_yellow
24
-
25
- e = to_ruby source
26
- puts e.light_green
27
-
28
- puts "\n[evaluated]:".light_yellow
29
- puts "#{eval e}".blue
42
+ eval_source source, mode = :compile
30
43
  end
31
44
 
32
- def to_ruby source
33
- ast = parse source
34
- puts "parsed: #{ast}".green if @debug
45
+ def eval_source source, mode = :repl
46
+ begin
47
+ e = to_ruby source
48
+ evaluated = eval e
49
+ rescue Exception => ex
50
+ if @debug
51
+ puts ex.backtrace.join("\n").red
52
+ binding.pry
53
+ end
54
+ puts ex.message.red
55
+ end
56
+ if @debug
57
+ if mode == :compile
58
+ puts "[LRB]:".light_yellow
59
+ puts source.light_yellow
60
+ end
61
+ puts e.light_green
62
+ if mode == :compile
63
+ puts "\n[evaluated]:".light_yellow
64
+ puts "#{evaluated}".blue
65
+ end
66
+ end
67
+ evaluated
68
+ end
69
+
70
+ def eval_list list
71
+ eval list_to_ruby(list)
72
+ end
73
+
74
+ def list_to_ruby ast
35
75
  ast = ast.map do |sexp|
36
76
  result = transform sexp
37
77
  puts result if @debug
@@ -40,6 +80,12 @@ class Lrb
40
80
  ast.join "\n\n"
41
81
  end
42
82
 
83
+ def to_ruby source
84
+ ast = parse source
85
+ puts "parsed: #{ast}".green if @debug
86
+ list_to_ruby ast
87
+ end
88
+
43
89
  def transform sexp
44
90
  puts line if @debug
45
91
  if sexp.class == Array
@@ -69,11 +115,37 @@ class Lrb
69
115
  indent + result
70
116
  else
71
117
  puts "#{sexp}".yellow if @debug
72
- sexp.to_s
118
+ sexp
73
119
  end
74
120
  end
75
121
 
76
122
  private
123
+ def balanced?
124
+ source = @stack.join " "
125
+ @left = source.count "["
126
+ @right = source.count "]"
127
+ @indent = @left - @right if (@left - @right) > 0
128
+ @left == @right
129
+ end
130
+
131
+ def readline_autocompletion
132
+ @dict = KEYWORD.keys.sort
133
+
134
+ comp = proc { |s| @dict.grep( /^#{Regexp.escape(s)}/ ) }
135
+
136
+ Readline.completion_append_character = " "
137
+ Readline.completion_proc = comp
138
+ end
139
+
140
+ def readline_with_hist_management
141
+ line = Readline.readline('> ' + indent, true)
142
+ return nil if line.nil?
143
+ if line =~ /^\s*$/ or Readline::HISTORY.to_a[-2] == line
144
+ Readline::HISTORY.pop
145
+ end
146
+ line
147
+ end
148
+
77
149
  def class_method? function
78
150
  function[0] == "."
79
151
  end
@@ -87,11 +159,22 @@ class Lrb
87
159
  if arg.class == String
88
160
  "\"#{arg}\""
89
161
  else
90
- arg
162
+ replace_parenthese arg
91
163
  end
92
164
  end
93
165
  end
94
166
 
167
+ def replace_parenthese arg
168
+ if arg.to_s["("] && arg.to_s[")"]
169
+ old = arg
170
+ arg = arg.to_s.gsub(/\((.*)\)/, '[\1]').to_sym
171
+ puts "arg: #{old} => #{arg}".light_yellow if @debug
172
+ arg
173
+ else
174
+ arg
175
+ end
176
+ end
177
+
95
178
  def transform_exp exps
96
179
  exps.map do |arg|
97
180
  unless arg.class == String
@@ -124,7 +207,11 @@ class Lrb
124
207
  end
125
208
 
126
209
  def operator op, args
127
- args.join " #{op.to_s} "
210
+ args = args.map do |arg|
211
+ transform arg
212
+ end
213
+ e = args.join " #{op.to_s} "
214
+ "(#{e})"
128
215
  end
129
216
 
130
217
  def transform_body block
@@ -171,7 +258,9 @@ class Lrb
171
258
  :'=' => lambda {|left, right| "#{left} = #{transform right}"},
172
259
  :== => lambda {|left, right| "#{transform left} == #{transform right}"},
173
260
 
174
-
261
+ :quote => lambda {|*args|
262
+ "#{args.first}"
263
+ },
175
264
  :list => lambda {|*args|
176
265
  "[#{args.join ', '}]"
177
266
  },
@@ -184,6 +273,8 @@ class Lrb
184
273
  ].join("\n")
185
274
  },
186
275
  :begin => lambda {|do_block, rescue_block|
276
+ do_block = transform_body do_block
277
+ rescue_block = transform_body rescue_block
187
278
  ["begin",
188
279
  " #{do_block}",
189
280
  "rescue Exception => e",
@@ -191,6 +282,11 @@ class Lrb
191
282
  "end"
192
283
  ].join("\n")
193
284
  },
285
+ :debug => lambda {
286
+ @debug = !@debug
287
+ puts "[DEBUG = #{@debug}]".light_yellow
288
+ ""
289
+ },
194
290
  :do => lambda {|fun, args, bodies|
195
291
  fun = transform fun
196
292
  args = args.join ", "
@@ -200,6 +296,13 @@ class Lrb
200
296
  "end"
201
297
  ].join("\n")
202
298
  },
299
+ :class => lambda {|name, body|
300
+ body = transform_body body
301
+ ["class #{name}",
302
+ " #{body}",
303
+ "end"
304
+ ].join("\n")
305
+ },
203
306
  :def => lambda {|args, body|
204
307
  name = args.first
205
308
  args = args.drop(1).join ", "
@@ -234,8 +337,14 @@ class Lrb
234
337
  }
235
338
  end
236
339
  # @test = Lrb.new
237
- # #@test.debug = true
340
+ # @test.debug = true
238
341
  # x = @test.to_ruby <<-REC
342
+ # [= @a Hash.new]
343
+
344
+ # [= @a(:test) 1]
345
+
346
+ # [puts
347
+ # [.blue "HELLO ROSE"]]
239
348
  # REC
240
- # puts x.blue
241
- # eval x
349
+ # eval x
350
+ # @test.repl
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - The Trung
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-24 00:00:00.000000000 Z
11
+ date: 2015-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -66,7 +66,7 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- description: It transform Ruby to a Lisp with braces. Compile and run as ruby code.
69
+ description: It transform Ruby to a Lisp with braces, included REPL mode.
70
70
  email: deulamco@gmail.com
71
71
  executables:
72
72
  - lrb