Skip to content

Commit b02c331

Browse files
committed
slightly improved parser
1 parent 0878001 commit b02c331

File tree

6 files changed

+45
-12
lines changed

6 files changed

+45
-12
lines changed

.ruby-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
jruby-1.7.4
1+
1.9.3-p392

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Get inspired by [Bret Victor](http://worrydream.com/#!/LearnableProgramming) or
1010
This drawing was made from a 6 year old girl at the CoderDojo cologne...
1111

1212
There is a stand alone app to play with without need of installing all stuff.
13-
With "ellipse 50 50 80 80" or "rect/line/fill" you can draw and with mouse dragging change the values.
13+
With "ellipse 50, 50, 80, 80" or "rect/line/fill" you can draw and with mouse dragging change the values.
1414

1515

1616
Notice

main/canvas.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,31 @@ def initialize(lines)
66
end
77

88
def draw_canvas
9-
elements = @lines.elements
10-
elements.each do |str_obj|
11-
send_if_runnable(str_obj.methode, str_obj.arguments) if str_obj.methode
12-
end
9+
evaluate(@lines.elements)
1310
end
1411

1512

1613
private
1714

15+
def evaluate(elements)
16+
#eval all or only line by line
17+
begin
18+
code = ""
19+
elements.each do |str_obj|
20+
code += str_obj.content + "\n" if str_obj.methode
21+
end
22+
return eval(code)
23+
rescue Exception => e
24+
begin
25+
elements.each do |str_obj|
26+
send_if_runnable(str_obj.methode, str_obj.arguments) if str_obj.methode
27+
end
28+
rescue Exception => e
29+
puts "#{ e } (#{ e.class })!"
30+
end
31+
end
32+
end
33+
1834
def send_if_runnable(methode, args)
1935
begin
2036
send(methode, *args)

main/parse.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ def update_line(key, key_code)
1414
@lines.make_new_line(@cursor)
1515
when 8
1616
@lines.delete_last_key(@cursor)
17+
when 9
18+
string_object = @lines.on_position_of(@cursor)
19+
#update line key
20+
string_object.append(" ") if string_object
1721
when 38 #up
1822
@lines.previous_line(@cursor)
1923
when 40 #down
2024
@lines.next_line(@cursor)
2125

2226
when 37 #left
23-
when 39 #right
27+
when 39 # right
2428
when 157 #command
2529
when 17 #ctrl
2630
when 18 #alt

main/string_object.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ def methode
2727
def arguments
2828
array = @content.split.to_a
2929
#splits the string after the methode into arguments array
30-
(array.count > 2) ? array.map{ |e| e.to_i}[1..-1] : []
30+
if (array.count > 2)
31+
update(array)
32+
else
33+
[]
34+
end
3135
end
3236

3337
def update_argument(x_mouse, y_mouse)
@@ -36,7 +40,7 @@ def update_argument(x_mouse, y_mouse)
3640
if arg
3741
new_arg_value = -$app.width/2+x_mouse+$app.editor_left_margin-length((methode+ " ").to_s)-length(@object_list[1][0..arg].join(" ").to_s)+$app.initial_arg_value
3842
@object_list[1][arg] = new_arg_value
39-
@content = @object_list.first + " " + @object_list[1].join(" ") + "\n" if @object_list.first
43+
@content = @object_list.first + " " + @object_list[1].join(", ") + "\n" if @object_list.first
4044
end
4145
end
4246

@@ -62,6 +66,17 @@ def get_position
6266

6367
private
6468

69+
def update(array)
70+
a = array.map do |e|
71+
if /[[:alpha:]]/.match(e)
72+
e
73+
else
74+
e.to_i
75+
end
76+
end
77+
a[1..-1]
78+
end
79+
6580
def length(s)
6681
text_width(s.to_s).to_i unless s.nil?
6782
end

simple_live_coding.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ def draw
4646
fill color 104, 153, 0
4747
@editor.draw_content
4848

49-
5049
text "this is a DEMO app", $app.width/2, $app.height-40
51-
text "write 'rect 20 20 200 200' and drag the values...", $app.width/2, $app.height-20
50+
text "write 'rect 20, 20, 200, 200' and drag the values...", $app.width/2, $app.height-20
5251

5352
fill color 14, 13, 0
5453
@cursor.draw_line #we need a line marking the text position
@@ -75,7 +74,6 @@ def mouse_moved
7574
redraw
7675
end
7776

78-
7977
def mouse_pressed
8078
@pressed = [mouse_x, mouse_y]
8179
@initial_arg_value = @watcher.get_param(mouse_x, mouse_y) #get the initial value from watcher

0 commit comments

Comments
 (0)