phaad 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Guardfile ADDED
@@ -0,0 +1,7 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^(lib|spec)/(.+)\.rb$}) { "spec/" }
6
+ end
7
+
@@ -27,7 +27,7 @@ module Phaad
27
27
  should_not_be = [ [:bodystmt, [[:void_stmt]], nil, nil, nil] ]
28
28
  first_should_not_be = [:void_stmt, :def, :bodystmt, :if, :else, :elsif,
29
29
  :unless, :while, :until, :while_mod, :until_mod, :if_mod, :unless_mod,
30
- :massign, :class, :for]
30
+ :massign, :class, :for, :case, :defs]
31
31
  emit ";\n" if !should_not_be.include?(sexp) && !first_should_not_be.include?(sexp.first)
32
32
  end
33
33
  outdent unless options[:indent] == false
@@ -69,7 +69,10 @@ module Phaad
69
69
  when :string_literal
70
70
  process(sexp[1])
71
71
  when :string_embexpr
72
+ no_brackets = [:var_ref, :method_add_arg, :command]
73
+ emit "(" unless no_brackets.include?(sexp[1][0][0])
72
74
  process sexp[1][0]
75
+ emit ")" unless no_brackets.include?(sexp[1][0][0])
73
76
  when :regexp_literal
74
77
  emit '"/'
75
78
  emit sexp[1][0][1]
@@ -79,6 +82,8 @@ module Phaad
79
82
  emit sexp[1]
80
83
  when :symbol_literal
81
84
  emit sexp[1][1][1].inspect
85
+ when :@label
86
+ emit sexp[1][0..-2].inspect
82
87
  when :dyna_symbol
83
88
  process(sexp[1][0])
84
89
  when :assign
@@ -102,6 +107,15 @@ module Phaad
102
107
  process r
103
108
  emit ";\n"
104
109
  end
110
+ when :opassign
111
+ raise NotImplementedError, sexp.inspect unless sexp[2][0] == :@op
112
+ process(sexp[1])
113
+ if sexp[2][1] == "<<="
114
+ emit " .= "
115
+ else
116
+ emit " #{sexp[2][1]} "
117
+ end
118
+ process(sexp[3])
105
119
  when :return
106
120
  emit "return "
107
121
  process sexp[1]
@@ -116,13 +130,17 @@ module Phaad
116
130
  emit sexp[1][1..-1]
117
131
  when :@const
118
132
  emit sexp[1]
133
+ when :break
134
+ emit "break"
135
+ when :next
136
+ emit "continue"
119
137
  when :method_add_arg
120
138
  if sexp[1][0] == :fcall && sexp[1][1][0] == :@ident
121
139
  emit sexp[1][1][1]
122
140
  emit "("
123
141
  process sexp[2][1] if sexp[2][1]
124
142
  emit ")"
125
- elsif sexp[1][0] == :call
143
+ elsif sexp[1][0] == :call
126
144
  process sexp[1]
127
145
  emit "("
128
146
  process sexp[2][1] if sexp[2][1]
@@ -138,7 +156,7 @@ module Phaad
138
156
  process sexp[3]
139
157
  when :command
140
158
  if sexp[1][0] == :@ident
141
- no_brackets = %w{global echo}
159
+ no_brackets = %w{global echo var public}
142
160
  emit sexp[1][1]
143
161
  emit no_brackets.include?(sexp[1][1]) ? " " : "("
144
162
  if sexp[2][0] == :args_add_block
@@ -156,9 +174,20 @@ module Phaad
156
174
  emit ", " unless s == sexp[1].last
157
175
  end
158
176
  when :call
159
- process sexp[1]
160
- emit "->"
161
- emit sexp[3][1]
177
+ if sexp[1][1][0] == :@const
178
+ if sexp[3][1] == "new"
179
+ emit "new "
180
+ process sexp[1]
181
+ else
182
+ process sexp[1]
183
+ emit "::"
184
+ emit sexp[3][1]
185
+ end
186
+ else
187
+ process sexp[1]
188
+ emit "->"
189
+ emit sexp[3][1]
190
+ end
162
191
  when :command_call
163
192
  if sexp[1][0] == :var_ref && sexp[1][1][0] == :@ident && sexp[2] == :"." &&
164
193
  sexp[3][0] == :@ident && sexp[4][0] == :args_add_block
@@ -168,6 +197,19 @@ module Phaad
168
197
  emit "("
169
198
  process sexp[4]
170
199
  emit ")"
200
+ elsif sexp[1][0] == :var_ref && sexp[1][1][0] == :@const &&
201
+ sexp[3][0] == :@ident && sexp[4][0] == :args_add_block
202
+ if sexp[3][1] == "new"
203
+ emit "new "
204
+ process sexp[1]
205
+ else
206
+ process sexp[1]
207
+ emit "::"
208
+ emit sexp[3][1]
209
+ end
210
+ emit "("
211
+ process sexp[4]
212
+ emit ")"
171
213
  else
172
214
  raise NotImplementedError, sexp.inspect
173
215
  end
@@ -249,6 +291,37 @@ module Phaad
249
291
  emit ") {\n"
250
292
  process_statements sexp[3]
251
293
  emit "}\n"
294
+ when :case
295
+ emit "switch("
296
+ process sexp[1]
297
+ emit ") {\n"
298
+ indent
299
+ # exp stores the next statement.
300
+ exp = sexp[2]
301
+ while exp
302
+ if exp[0] == :when
303
+ exp[1].each_with_index do |each_case, i|
304
+ emit "case "
305
+ process each_case
306
+ emit ":"
307
+ emit " " if i < exp[1].size - 1
308
+ end
309
+ emit "\n"
310
+ process_statements exp[2] if exp[2]
311
+ elsif exp[0] == :else
312
+ emit "default:"
313
+ emit "\n"
314
+ process_statements exp[1] if exp[1]
315
+ else
316
+ raise NotImplementedError, sexp.inspect
317
+ end
318
+ indent
319
+ emit "break;\n"
320
+ outdent
321
+ exp = exp[3]
322
+ end
323
+ outdent
324
+ emit "}\n"
252
325
  when :def
253
326
  raise NotImplementedError, sexp.inspect unless sexp[1][0] == :@ident
254
327
  emit "function "
@@ -264,6 +337,21 @@ module Phaad
264
337
  emit ") {\n"
265
338
  process_statements [sexp[3]]
266
339
  emit "}\n"
340
+ when :defs
341
+ raise NotImplementedError, sexp.inspect unless sexp[3][0] == :@ident
342
+ emit "static function "
343
+ emit sexp[3][1]
344
+ emit "("
345
+ if sexp[4][0] == :params
346
+ process sexp[4] # params
347
+ elsif sexp[4][0] == :paren && sexp[4][1][0] == :params
348
+ process sexp[4][1]
349
+ else
350
+ raise NotImplementedError, sexp.inspect
351
+ end
352
+ emit ") {\n"
353
+ process_statements [sexp[5]]
354
+ emit "}\n"
267
355
  when :class
268
356
  if sexp[1][1][0] != :@const || (sexp[2] && sexp[2][1][0] != :@const)
269
357
  raise NotImplementedError, sexp.inspect
@@ -295,7 +383,7 @@ module Phaad
295
383
  process param[1]
296
384
  end
297
385
  end
298
- when :array, :hash
386
+ when :array, :hash, :bare_assoc_hash
299
387
  emit "array("
300
388
  if sexp[1]
301
389
  if sexp[1][0] == :assoclist_from_args
data/lib/phaad/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Phaad
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/phaad.gemspec CHANGED
@@ -22,4 +22,6 @@ Gem::Specification.new do |s|
22
22
  s.add_dependency 'slop', '~>1.6.0'
23
23
  s.add_dependency 'fssm', '~>0.2.7'
24
24
  s.add_development_dependency 'rspec', '~>2.6.0'
25
+ s.add_development_dependency 'guard-rspec', '~>0.4.3'
26
+ s.add_development_dependency 'growl_notify', '~>0.0.1' if RUBY_PLATFORM.include?("darwin")
25
27
  end
@@ -24,6 +24,7 @@ describe Phaad::Generator, 'array creation' do
24
24
  it "should create simple arrays" do
25
25
  compile("{a => :b, 1 => 2, /foo/ => /bar/}").should ==
26
26
  'array($a => "b", 1 => 2, "/foo/" => "/bar/");'
27
+ compile("{a: b, c: 1}").should == 'array("a" => $b, "c" => 1);'
27
28
  end
28
29
 
29
30
  it "should create nested arrays" do
@@ -52,4 +52,29 @@ describe Phaad::Generator, "binary" do
52
52
  it "should parse << as ." do
53
53
  compile("'foo' << 'bar'").should == '"foo" . "bar";'
54
54
  end
55
+
56
+ context "operator assign" do
57
+ it "should parse += -= *= /= %=" do
58
+ compile("a += 1").should == "$a += 1;"
59
+ compile("a -= 1").should == "$a -= 1;"
60
+ compile("a *= 1").should == "$a *= 1;"
61
+ compile("a /= 1").should == "$a /= 1;"
62
+ compile("a %= 1").should == "$a %= 1;"
63
+ end
64
+
65
+ it "should parse |= &= ^=" do
66
+ compile("a |= 1").should == "$a |= 1;"
67
+ compile("a &= 1").should == "$a &= 1;"
68
+ compile("a ^= 1").should == "$a ^= 1;"
69
+ end
70
+
71
+ it "should parse && ||" do
72
+ compile("a &&= false").should == "$a &&= FALSE;"
73
+ compile("a ||= false").should == "$a ||= FALSE;"
74
+ end
75
+
76
+ it "should parse <<= into .=" do
77
+ compile("a <<= b").should == "$a .= $b;"
78
+ end
79
+ end
55
80
  end
@@ -10,28 +10,57 @@ describe Phaad::Generator, 'call a function' do
10
10
  compile("a b c d, e, f()").should == "a(b(c($d, $e, f())));"
11
11
  end
12
12
 
13
- it "should not add brackets when calling global and echo" do
13
+ it "should not add brackets when calling global, echo, var, public" do
14
14
  compile("echo a").should == "echo $a;"
15
15
  compile("echo a, b").should == "echo $a, $b;"
16
16
  compile("global a").should == "global $a;"
17
17
  compile("global a, b").should == "global $a, $b;"
18
+ compile("var a").should == "var $a;"
19
+ compile("var a, b").should == "var $a, $b;"
20
+ compile("public a").should == "public $a;"
21
+ compile("public a, b").should == "public $a, $b;"
22
+ end
23
+
24
+ it "should print new ClassName when ClassName.new is called" do
25
+ compile("Foo.new").should == "new Foo;"
26
+ compile("Foo.new a, b, c").should == "new Foo($a, $b, $c);"
27
+ compile("Foo.new(a, b, c)").should == "new Foo($a, $b, $c);"
28
+ end
29
+
30
+ it "should call using :: when a function is called on a Constant" do
31
+ compile("Foo.bar()").should == "Foo::bar();"
32
+ compile("Foo.bar a, b, c").should == "Foo::bar($a, $b, $c);"
33
+ compile("Foo.bar(a, b, c)").should == "Foo::bar($a, $b, $c);"
34
+ end
35
+
36
+ it "should accept named params and pass it as an array" do
37
+ compile("Foo.bar :a => 1, 'b' => 2").should == 'Foo::bar(array("a" => 1, "b" => 2));'
18
38
  end
19
39
  end
20
40
 
21
41
  describe Phaad::Generator, 'define a function' do
22
- it "should define a function without params" do
23
- compile("def f\nend").should start_with("function f()")
24
- end
42
+ context "normal function" do
43
+ it "should define a function without params" do
44
+ compile("def f\nend").should == "function f() {\n}"
45
+ end
46
+
47
+ it "should define a function with basic params" do
48
+ compile("def f a\nend").should == "function f($a) {\n}"
49
+ compile("def f(a)\nend").should == "function f($a) {\n}"
50
+ compile("def f a, b\nend").should == "function f($a, $b) {\n}"
51
+ compile("def f(a, b)\nend").should == "function f($a, $b) {\n}"
52
+ end
25
53
 
26
- it "should define a function with basic params" do
27
- compile("def f a\nend").should start_with("function f($a)")
28
- compile("def f(a)\nend").should start_with("function f($a)")
29
- compile("def f a, b\nend").should start_with("function f($a, $b)")
30
- compile("def f(a, b)\nend").should start_with("function f($a, $b)")
54
+ it "should define a function with params with default values" do
55
+ compile("def f a = 4\nend").should == "function f($a = 4) {\n}"
56
+ compile("def f a, b = 4\nend").should == "function f($a, $b = 4) {\n}"
57
+ end
31
58
  end
32
59
 
33
- it "should define a function with params with default values" do
34
- compile("def f a = 4\nend").should start_with("function f($a = 4)")
35
- compile("def f a, b = 4\nend").should start_with("function f($a, $b = 4)")
60
+ context "static (self) function" do
61
+ it "should define a function with/without params" do
62
+ compile("def self.f\nend").should == "static function f() {\n}"
63
+ compile("def self.f(a, b)\nend").should == "static function f($a, $b) {\n}"
64
+ end
36
65
  end
37
66
  end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Phaad::Generator, 'keywords' do
4
+ it "should compile break" do
5
+ compile("break").should == "break;"
6
+ end
7
+
8
+ it "should compile next into continue" do
9
+ compile("next").should == "continue;"
10
+ end
11
+ end
@@ -31,6 +31,11 @@ describe Phaad::Generator, 'string' do
31
31
  '"a " . $b . " " . foo("bar", "baz") . " ";'
32
32
  end
33
33
 
34
+ it "should add brackets to interpolation except simple variable / function calls" do
35
+ compile('"a #{b + c} d"').should == '"a " . ($b + $c) . " d";'
36
+ compile('"a #{b(e) + c} d"').should == '"a " . (b($e) + $c) . " d";'
37
+ end
38
+
34
39
  it "should handle empty \#{} properly" do
35
40
  compile('"a #{b} #{} "').should == '"a " . $b . " " . " ";'
36
41
  end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe Phaad::Generator, "switch" do
4
+ it "should parse case statements" do
5
+ compile("case a\nwhen b\nc\nend").should == "switch($a) {\n case $b:\n $c;\n break;\n}"
6
+ end
7
+
8
+ it "should parse multiple when statements" do
9
+ compile("case a\nwhen b\nc\nwhen d\ne\nend").should ==
10
+ "switch($a) {\n case $b:\n $c;\n break;\n case $d:\n $e;\n break;\n}"
11
+ end
12
+
13
+ it "should parse else statement" do
14
+ compile("case a\nwhen b\nc\nwhen d\ne\nelse\nf\nend").should ==
15
+ "switch($a) {\n case $b:\n $c;\n break;\n case $d:\n $e;\n break;\n" +
16
+ " default:\n $f;\n break;\n}"
17
+ end
18
+
19
+ it "should parse multiple case in single when statement" do
20
+ compile("case a\nwhen b, c, d\ne\nend").should ==
21
+ "switch($a) {\n case $b: case $c: case $d:\n $e;\n break;\n}"
22
+ end
23
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phaad
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-06-01 00:00:00.000000000Z
12
+ date: 2011-09-04 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: slop
16
- requirement: &75976660 !ruby/object:Gem::Requirement
16
+ requirement: &70365216887360 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.6.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *75976660
24
+ version_requirements: *70365216887360
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: fssm
27
- requirement: &76004900 !ruby/object:Gem::Requirement
27
+ requirement: &70365216886480 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.2.7
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *76004900
35
+ version_requirements: *70365216886480
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &76018460 !ruby/object:Gem::Requirement
38
+ requirement: &70365216884980 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,29 @@ dependencies:
43
43
  version: 2.6.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *76018460
46
+ version_requirements: *70365216884980
47
+ - !ruby/object:Gem::Dependency
48
+ name: guard-rspec
49
+ requirement: &70365216883640 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.4.3
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70365216883640
58
+ - !ruby/object:Gem::Dependency
59
+ name: growl_notify
60
+ requirement: &70365216882460 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 0.0.1
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70365216882460
47
69
  description: A beautiful way to write PHP
48
70
  email:
49
71
  - utkarshkukreti@gmail.com
@@ -55,6 +77,7 @@ files:
55
77
  - .gitignore
56
78
  - .rspec
57
79
  - Gemfile
80
+ - Guardfile
58
81
  - README.md
59
82
  - Rakefile
60
83
  - bin/phaad
@@ -72,11 +95,13 @@ files:
72
95
  - spec/generator/function_spec.rb
73
96
  - spec/generator/if_spec.rb
74
97
  - spec/generator/ivar_spec.rb
98
+ - spec/generator/keywords_spec.rb
75
99
  - spec/generator/literal_spec.rb
76
100
  - spec/generator/massign_spec.rb
77
101
  - spec/generator/paren_spec.rb
78
102
  - spec/generator/return_spec.rb
79
103
  - spec/generator/string_spec.rb
104
+ - spec/generator/switch_spec.rb
80
105
  - spec/generator/ternary_spec.rb
81
106
  - spec/generator/unary_spec.rb
82
107
  - spec/generator/while_spec.rb
@@ -103,8 +128,30 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
128
  version: '0'
104
129
  requirements: []
105
130
  rubyforge_project: phaad
106
- rubygems_version: 1.8.4
131
+ rubygems_version: 1.8.10
107
132
  signing_key:
108
133
  specification_version: 3
109
134
  summary: A beautiful way to write PHP
110
- test_files: []
135
+ test_files:
136
+ - spec/generator/array_spec.rb
137
+ - spec/generator/assign_spec.rb
138
+ - spec/generator/binary_spec.rb
139
+ - spec/generator/class_spec.rb
140
+ - spec/generator/constants_spec.rb
141
+ - spec/generator/for_spec.rb
142
+ - spec/generator/function_spec.rb
143
+ - spec/generator/if_spec.rb
144
+ - spec/generator/ivar_spec.rb
145
+ - spec/generator/keywords_spec.rb
146
+ - spec/generator/literal_spec.rb
147
+ - spec/generator/massign_spec.rb
148
+ - spec/generator/paren_spec.rb
149
+ - spec/generator/return_spec.rb
150
+ - spec/generator/string_spec.rb
151
+ - spec/generator/switch_spec.rb
152
+ - spec/generator/ternary_spec.rb
153
+ - spec/generator/unary_spec.rb
154
+ - spec/generator/while_spec.rb
155
+ - spec/generator/whitespace_spec.rb
156
+ - spec/phaad_spec.rb
157
+ - spec/spec_helper.rb