kaiser-ruby 0.5.1 → 0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0fc112c05db3c0d35fd18ff7002728b5870dd5d51cf75c97b1e70e281b4ee808
4
- data.tar.gz: 18bdbed256f36c972a54d61f9531694e6f178bc4cd3172fbab1b8a1787dd4c6f
3
+ metadata.gz: ed13667ff5f7efa6ac48fda708e248bf329466e999604dc3896eccc5b3b47cbe
4
+ data.tar.gz: be140e748235e62bab3e24d86a55aaea75b2a33521220355e9e9274dfdd58324
5
5
  SHA512:
6
- metadata.gz: 184a6cbcc1b4e67c82464c33aecdeea74ad1acd0a9e6b12de7e00497ba0f39245b1b3df46b3d08393bdeddecd09ec86d75fcb2e5e6049591e811ac2cde406135
7
- data.tar.gz: f635f13bbc469431462190f2e2bf9ebfdc7d6b99314f0f367cf2f04c0e31c45ecd22e489ce54e72e0bfebfe2d22ed0523468f4d8f1b73990fe3cbe42eb4f994c
6
+ metadata.gz: c16d9fb8793f6f81cd7bdddad78c14efc749a7d9ac47b9d25d6565c78443372dbbd57a77ac65de9531d168101a40f6825ec75e8c30d802df936799c3827468ec
7
+ data.tar.gz: 5a3fd840db588ca27ee1ceed35a651fa9ff019ea40cbf67f1ea6258a6808016b232bf2679a0ee683af50ae71268eef7b37f00cd58bd37ef078e95bb3f4c57447
data/CHANGELOG.md CHANGED
@@ -1,35 +1,52 @@
1
- # 0.1
1
+ # 0.7
2
2
 
3
- - [x] Initial implementation of the Rockstar Language based using `Parslet::Parser`
3
+ Language Implementation
4
4
 
5
- # 0.2
5
+ - [x] Full language implementation according to the spec
6
6
 
7
- - [x] Rewrote the Parser and Transform classes from scratch
8
- - [x] basic CLI and REPL
9
- - [x] Metal umlauts
10
- - [x] Most variable types, assignmnents, output, conditionals are working.
7
+ - [x] Added forgotten `break` and `continue` keywords
8
+ - [x] STDIN input now converts to Float or Integer as expected
9
+ - [x] Fixed indenting of nested blocks, deep nesting works too
10
+ - [x] Variables are scoped properly if you declare them before a function
11
+ - [x] Handle ',' and 'and' in function calls together with precedence of &&
12
+ - [x] Function calls should work with `, `, `, and`, `'n'` and `&` as argument separators. `and` without the comma is deprecated.
13
+ - [x] Poetic assignment works with string and number literals
14
+ - [x] All numbers are represented as floats
15
+ - [x] Implement refinements to make math operations and comparisons work properly
16
+ - [x] Handle NOT operator
17
+ - [x] Handle NOR operation
18
+ - [x] Handle equality
19
+ - [x] `Listen` should just wait for user input and do nothing with it
20
+ - [x] Multiple increments/decrements with `build X up, up up` and `knock Y down, down, down`
21
+ - [x] Handle global variables if they're declared after function definition that uses them
11
22
 
12
- # 0.3
23
+ Other
13
24
 
14
- Language Implementation:
25
+ - [x] Replaced parsing with Parslet with a hand-written parser in plain Ruby
26
+ - [x] Implemented reference tests
27
+ - [x] Handle testing STDIN in RSpec tests
28
+ - [x] CLI command reports which version it is
29
+ - [x] Updated README with new examples and info
30
+ - [x] Automatically adds require and using line to saved ruby file output
15
31
 
16
- - [x] Handle null type differently - nil in Ruby isn't really comparable to 0
17
- - [x] Handle mysterious type - probably this should be nil and what is now nil should be 0 instead
18
- - [x] Handle gt, gte, lt, lte comparisons
19
- - [x] Handle inequality
20
- - [x] While loop
21
- - [x] Until loop
22
- - [x] Break and continue
23
- - [x] And/Or keywords for conditionals
24
- - [x] Define functions
25
- - [x] Handle function calls
26
- - [x] Return can return math operations directly
32
+ # 0.6 - unreleased
27
33
 
28
- Other stuff:
34
+ - [x] Fixed error in input from STDIN
35
+ - [x] The transpiler now throws a SyntaxError instead of Parslet exception
29
36
 
30
- - [x] FizzBuzz example is working
31
- - [x] Fibonacci example is working
32
- - [x] Added comments to resulting ruby code flow control statements, so it's easier to see where what ends. This should help making the code more readable (and easier to figure out if it's actually correct), at least while the indentation feature is not fully working yet.
37
+ Test Suite
38
+
39
+ - [x] Refactored the test suite to make more sense
40
+ - [x] Added a ton of new negative and positive tests
41
+
42
+ # 0.5
43
+
44
+ - [x] Fixed converting decimals so "Conversion is lovestruck. lovestruck and essential seasick" results in "conversion = 0.0397" as one would expect.
45
+
46
+ Other stuff
47
+
48
+ - [x] Updated the REPL to work on Ruby versions earlier than 2.5 (2.3 is the minimum supported version)
49
+ - [x] Travis CI tests on all supported Ruby versions
33
50
 
34
51
  # 0.4
35
52
 
@@ -51,11 +68,35 @@ Other stuff:
51
68
  - [x] Move nesting indentation from main module to transformer
52
69
  - [x] Celsius to Fahrenheit example
53
70
 
54
- # 0.5
71
+ # 0.3
55
72
 
56
- - [x] Fixed converting decimals so "Conversion is lovestruck. lovestruck and essential seasick" results in "conversion = 0.0397" as one would expect.
73
+ Language Implementation:
57
74
 
58
- Other stuff
75
+ - [x] Handle null type differently - nil in Ruby isn't really comparable to 0
76
+ - [x] Handle mysterious type - probably this should be nil and what is now nil should be 0 instead
77
+ - [x] Handle gt, gte, lt, lte comparisons
78
+ - [x] Handle inequality
79
+ - [x] While loop
80
+ - [x] Until loop
81
+ - [x] Break and continue
82
+ - [x] And/Or keywords for conditionals
83
+ - [x] Define functions
84
+ - [x] Handle function calls
85
+ - [x] Return can return math operations directly
59
86
 
60
- - [x] Updated the REPL to work on Ruby versions earlier than 2.5 (2.3 is the minimum supported version)
61
- - [x] Travis CI tests on all supported Ruby versions
87
+ Other stuff:
88
+
89
+ - [x] FizzBuzz example is working
90
+ - [x] Fibonacci example is working
91
+ - [x] Added comments to resulting ruby code flow control statements, so it's easier to see where what ends. This should help making the code more readable (and easier to figure out if it's actually correct), at least while the indentation feature is not fully working yet.
92
+
93
+ # 0.2
94
+
95
+ - [x] Rewrote the Parser and Transform classes from scratch
96
+ - [x] basic CLI and REPL
97
+ - [x] Metal umlauts
98
+ - [x] Most variable types, assignmnents, output, conditionals are working.
99
+
100
+ # 0.1
101
+
102
+ - [x] Initial implementation of the Rockstar Language based using `Parslet::Parser`
data/Gemfile.lock CHANGED
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- kaiser-ruby (0.5.1)
5
- parslet (~> 1.8)
4
+ kaiser-ruby (0.7)
5
+ hashie
6
6
  thor (~> 0.20)
7
7
 
8
8
  GEM
@@ -10,8 +10,8 @@ GEM
10
10
  specs:
11
11
  coderay (1.1.2)
12
12
  diff-lcs (1.3)
13
+ hashie (3.6.0)
13
14
  method_source (0.9.0)
14
- parslet (1.8.2)
15
15
  pry (0.11.3)
16
16
  coderay (~> 1.1.0)
17
17
  method_source (~> 0.9.0)
data/README.md CHANGED
@@ -5,7 +5,9 @@
5
5
 
6
6
  This tool translates a file containing a program written in the [Rockstar language](https://github.com/dylanbeattie/rockstar) to Ruby code.
7
7
 
8
- This is still a work in progress, however most of the language implementation is already finished and working (as are the fibonacci sequence and fizzbuzz examples in the repository). For details on what is done, see the TODO.md and CHANGELOG.md files.
8
+ As of version 0.7, Kaiser-Ruby implements all of the Rockstar language spec and further versions will keep up with new language features as they are added.
9
+
10
+ For details on what is done and what I'm still working on, see the TODO.md and CHANGELOG.md files.
9
11
 
10
12
  ## Installation
11
13
 
@@ -17,7 +19,7 @@ $ gem install kaiser-ruby
17
19
 
18
20
  This gem works best on a current Ruby version and requires Ruby 2.3 at minimum. Running it on 2.3 has the downside of metal umlauts not being entirely correct as that Ruby version doesn't know how to `.downcase` a capital umlaut letter, which was fixed in 2.4.
19
21
 
20
- If you're not using the umlauts, all should be fine otherwise.
22
+ If you're not using the umlauts (or at least are careful to only replace lowercase letters with them), all should be fine otherwise.
21
23
 
22
24
  ## Usage
23
25
 
@@ -32,29 +34,48 @@ $ kaiser-ruby
32
34
  There are a few ways you can use it. First one will just output the result of the transpilation.
33
35
 
34
36
  ```
35
- $ kaiser-ruby transpile ./examples/assignment.rock
36
- tommy = 15
37
- puts tommy
37
+ $ kaiser-ruby transpile ./examples/simple.rock
38
+ @tommy = 15.0
39
+ puts @tommy
40
+
41
+ if "".to_bool
42
+ puts "empty strings are false"
43
+ end
38
44
 
39
45
  ```
40
46
 
41
47
  The `--show-source` flag will output the Rockstar code along with the resulting Ruby code like this:
42
48
 
43
49
  ```
44
- $ kaiser-ruby transpile ./examples/assignment.rock --show-source
50
+ $ kaiser-ruby transpile ./examples/simple.rock --show-source
45
51
  Tommy is a rebel
46
52
  Shout Tommy
53
+
54
+ if ""
55
+ Shout "empty strings are false"
47
56
  ----------------------------------------
48
- tommy = 15
49
- puts tommy
57
+ @tommy = 15.0
58
+ puts @tommy
59
+
60
+ if "".to_bool
61
+ puts "empty strings are false"
62
+ end
63
+
64
+ ```
65
+
66
+ You can also use the `--save=FILE` option to write the resulting transpiled code as a file instead of outputting it.
67
+
68
+ ```
69
+ $ kaiser-ruby transpile ./examples/simple.rock --save=simple.rb
70
+ Saved output in `simple.rb`
50
71
 
51
72
  ```
52
73
 
53
- You can also use the `--save=FILE` option to write the resulting transpiled code as a file instead of outputting it:
74
+ The saved output will have a few additional lines at the start that include the language changes necessary for Rockstar to work correctly. You need the gem installed to run this file:
54
75
 
55
76
  ```
56
- $ kaiser-ruby transpile ./examples/assignment.rock --save=a.rb
57
- Saved output in `a.rb`
77
+ $ ruby simple.rb
78
+ 15.0
58
79
 
59
80
  ```
60
81
 
@@ -64,10 +85,10 @@ Another option is to run an interactive console (REPL):
64
85
  $ kaiser-ruby rock --debug
65
86
  Type 'exit' to exit the console. Otherwise, rock on!
66
87
  \m/> Put "Hello San Francisco" into the message
67
- \m/> the_message = "Hello San Francisco"
88
+ \m/> @the_message = "Hello San Francisco"
68
89
  => Hello San Francisco
69
90
  \m/> Scream the message
70
- \m/> puts the_message
91
+ \m/> puts @the_message
71
92
  Hello San Francisco
72
93
  => nil
73
94
  \m/> exit
@@ -77,7 +98,7 @@ $
77
98
  Finally, you can also transpile and immediately execute the code, like this:
78
99
 
79
100
  ```
80
- $ kaiser-ruby execute ./examples/assignment.rock
101
+ $ kaiser-ruby execute ./examples/simple.rock
81
102
  15
82
103
 
83
104
  ```
@@ -86,37 +107,37 @@ Or even better, this:
86
107
 
87
108
  ```
88
109
  $ kaiser-ruby execute ./examples/fibonacci.rock
89
- 1
90
- 1
91
- 2
92
- 3
93
- 5
94
- 8
95
- 13
96
- 21
97
- 34
98
- 55
99
- 89
100
- 144
101
- 233
102
- 377
103
- 610
104
- 987
105
- 1597
106
- 2584
107
- 4181
108
- 6765
109
- 10946
110
- 17711
111
- 28657
112
- 46368
110
+ 1.0
111
+ 1.0
112
+ 2.0
113
+ 3.0
114
+ 5.0
115
+ 8.0
116
+ 13.0
117
+ 21.0
118
+ 34.0
119
+ 55.0
120
+ 89.0
121
+ 144.0
122
+ 233.0
123
+ 377.0
124
+ 610.0
125
+ 987.0
126
+ 1597.0
127
+ 2584.0
128
+ 4181.0
129
+ 6765.0
130
+ 10946.0
131
+ 17711.0
132
+ 28657.0
133
+ 46368.0
113
134
 
114
135
  $
115
136
  ```
116
137
 
117
138
  ## Contributing
118
139
 
119
- Bug reports and pull requests are welcome on GitHub at https://github.com/marcinruszkiewicz/kaiser-ruby.
140
+ Bug reports and pull requests are welcome on GitHub at https://github.com/marcinruszkiewicz/kaiser-ruby. I'm also available for questions at the [Rockstar Developers Discord Group](https://discord.gg/kEUe5bM)
120
141
 
121
142
  ## License
122
143
 
data/TODO.md CHANGED
@@ -2,22 +2,14 @@
2
2
 
3
3
  ## Language Implementation
4
4
 
5
- - [ ] Function calls can be used as expressions in flow statements
6
- - [ ] Handle ',' instead of 'and' in function calls
7
-
8
- - [ ] Somehow handle function variable scope
9
- - [ ] Handle object type
10
-
11
5
  ## Examples
12
6
 
13
7
  - [ ] Should be able to run the [Cellular Rockomata](https://github.com/Rifhutch/cellular-rocktomata)
14
8
  - [ ] Math module https://gist.github.com/wrenoud/6be6f7509c88a3d8f9867ae782fb768f
15
9
  - [ ] Primality checker https://www.reddit.com/r/RockstarDevs/comments/92i6sm/primality_checker_in_rockstar/
16
- - [ ] Make a demo command in the CLI that runs all examples (would that even work as a gem? it should be doable somehow)
17
10
 
18
11
  ## Other stuff
19
12
 
20
- - [ ] Fix indenting of nested blocks that doesn't really work well (this should also help deeper nesting)
21
13
  - [ ] Add code history to the REPL
22
14
  - [ ] Make REPL work with multiline input also, not only singular lines
23
15
  - [ ] Make a demo visitor that evals the code and waits a bit between commands, so it's more music video-ish. Maybe should change the console colors while at it?
data/exe/kaiser-ruby CHANGED
@@ -3,5 +3,6 @@ require "rubygems"
3
3
  require "bundler/setup"
4
4
  require "kaiser_ruby"
5
5
  require "kaiser_ruby/cli"
6
+ require "kaiser_ruby/refinements"
6
7
 
7
8
  KaiserRuby::CLI.start(ARGV)
data/kaiser-ruby.gemspec CHANGED
@@ -25,6 +25,6 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "rake", "~> 10.0"
26
26
  spec.add_development_dependency "rspec", "~> 3.0"
27
27
  spec.add_development_dependency "pry", "~> 0.11"
28
- spec.add_dependency "parslet", "~> 1.8"
29
28
  spec.add_dependency "thor", "~> 0.20"
29
+ spec.add_dependency "hashie"
30
30
  end
data/lib/kaiser_ruby.rb CHANGED
@@ -1,31 +1,42 @@
1
- require 'parslet'
2
- require 'kaiser_ruby/rockstar_parser'
3
- require 'kaiser_ruby/rockstar_transform'
1
+ require 'hashie'
2
+ require 'kaiser_ruby/parser'
3
+ require 'kaiser_ruby/transformer'
4
+ require 'kaiser_ruby/refinements'
5
+ require 'pry'
4
6
 
5
7
  module KaiserRuby
8
+ class RockstarSyntaxError < SyntaxError
9
+ end
10
+
6
11
  def self.parse(input)
7
12
  # eat comments since we don't care about them
8
- input = input.gsub(/\(.*?\)/, '')
13
+ input = input.gsub(/\n *\(.*?\) *\n/m, "\n\n")
14
+ input = input.gsub(/\(.*?\)\s*\n/m, "\n").gsub(/ +/, ' ')
15
+ parser = KaiserRuby::Parser.new(input)
16
+ parser.parse
17
+ end
18
+
19
+ def self.transpile(input)
20
+ tree = parse(input)
21
+ KaiserRuby::Transformer.new(tree).transform
22
+ end
9
23
 
10
- # expand contractions
11
- input = input.gsub(/'s\W+/, ' is ')
24
+ using KaiserRuby::Refinements
12
25
 
13
- # strings without a line ending (or single lines) should be fed into the alternative parser
14
- if input.split("\n").size == 1
15
- KaiserRuby::RockstarSingleLineParser.new.parse(input.chomp, reporter: Parslet::ErrorReporter::Deepest.new)
16
- else
17
- KaiserRuby::RockstarParser.new.parse(input, reporter: Parslet::ErrorReporter::Deepest.new)
26
+ def self.execute(input)
27
+ with_captured_stdout do
28
+ instance_eval transpile(input)
18
29
  end
19
- rescue Parslet::ParseFailed => failure
20
- puts input.inspect
21
- puts failure.parse_failure_cause.ascii_tree
22
30
  end
23
31
 
24
- def self.transform(tree)
25
- KaiserRuby::RockstarTransform.new.apply(tree)
26
- end
32
+ private
27
33
 
28
- def self.transpile(input)
29
- transform(parse(input))
34
+ def self.with_captured_stdout
35
+ old_stdout = $stdout
36
+ $stdout = StringIO.new
37
+ yield
38
+ $stdout.string
39
+ ensure
40
+ $stdout = old_stdout
30
41
  end
31
42
  end
@@ -2,6 +2,8 @@ require 'thor'
2
2
 
3
3
  module KaiserRuby
4
4
  class CLI < Thor
5
+ package_name "Kaiser-Ruby v#{KaiserRuby::VERSION}"
6
+
5
7
  desc "transpile FILE", "transpile a .rock FILE and output the result"
6
8
  option 'show-source'.to_sym, type: :boolean, desc: "prints out the source file along with the transpiled output"
7
9
  option :save, desc: "saves the transpiled output in SAVE"
@@ -16,6 +18,11 @@ module KaiserRuby
16
18
 
17
19
  if options[:save]
18
20
  out = File.new(options[:save], 'w')
21
+ out.write <<~REQ
22
+ require 'kaiser_ruby/refinements'
23
+ using KaiserRuby::Refinements
24
+
25
+ REQ
19
26
  out.write output
20
27
  out.close
21
28
  say "Saved output in `#{options[:save]}`", :green
@@ -25,11 +32,12 @@ module KaiserRuby
25
32
  say
26
33
  end
27
34
 
35
+ using KaiserRuby::Refinements
36
+
28
37
  desc "execute FILE", "transpiles and runs a .rock FILE"
29
38
  def execute(filename)
30
39
  file = File.read filename
31
40
  output = KaiserRuby.transpile(file)
32
-
33
41
  instance_eval output
34
42
  say
35
43
  end
@@ -53,8 +61,8 @@ module KaiserRuby
53
61
  output = b.eval(code)
54
62
  output = 'nil' if output.nil?
55
63
  say " => #{output}\n"
56
- rescue
57
- say "THE STAGE IS ON FIRE!"
64
+ rescue Exception => e
65
+ say "THE STAGE IS ON FIRE! #{e}: #{e.message}"
58
66
  end
59
67
  end
60
68
  end