my_precious 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +146 -10
- data/lib/my_precious/parser.rb +102 -66
- data/lib/my_precious/version.rb +1 -1
- data/lib/my_precious.rb +11 -5
- metadata +1 -2
- data/lib/my_precious/parser_2.rb +0 -222
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dea782367a57a0e807099bf2eeb607fbb2f9c8ec86543a32eff54b9922357558
|
4
|
+
data.tar.gz: 37e93e35f2b66f4488d8425eae01486754c1ed330a4c3b9cf5aa5603e599bf66
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 46ee3935305e54029251a2fda9e81356675d88e9fe3dffafadea51368a76126d420cefdc0f37c5ad86fdf471970f479d8d12806bff620f76146b082ab6232b54
|
7
|
+
data.tar.gz: c3f2911ea732dc91adf5b3accdf8d68af1466d8970fa096e47308edf20d04835638355c12c79a80613d14ffe058588ab8a03fee636a2b8f77b2daf31115b9825
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
-
#
|
1
|
+
# Precious
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
I wanted to make a fun programming language so I did it.
|
4
|
+
It's all my own, my precious... Precious is a LOTR esoteric programming language translator.
|
5
|
+
Precious uses lore keywords and english to create simple programming functionality.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
9
9
|
Add this line to your application's Gemfile:
|
10
10
|
|
11
11
|
```ruby
|
12
|
-
gem '
|
12
|
+
gem 'precious'
|
13
13
|
```
|
14
14
|
|
15
15
|
And then execute:
|
@@ -18,11 +18,147 @@ And then execute:
|
|
18
18
|
|
19
19
|
Or install it yourself as:
|
20
20
|
|
21
|
-
$ gem install
|
21
|
+
$ gem install precious
|
22
|
+
|
23
|
+
## Syntax:
|
24
|
+
Welcome to the Fellowship!
|
25
|
+
|
26
|
+
the my_precious gem is designed to find and hold on to keywords and ignore everything else.
|
27
|
+
this allows users to create their own stories in the Precious language that is then translated to valid Ruby code.
|
28
|
+
|
29
|
+
each piece of code you write in Precious must be written on its own new line in your text editor of choice.
|
30
|
+
|
31
|
+
now, in the words of Gandalf the Gray, “All we have to decide is what to do with the time that is given us.”
|
32
|
+
|
33
|
+
|
34
|
+
# variables
|
35
|
+
use capitols
|
36
|
+
|
37
|
+
Frodo is 8 years old
|
38
|
+
|
39
|
+
# strings
|
40
|
+
use double quotes
|
41
|
+
|
42
|
+
Frodo is "a hobbit"
|
43
|
+
|
44
|
+
# integers
|
45
|
+
write normally
|
46
|
+
|
47
|
+
Orcs are 2 big
|
48
|
+
|
49
|
+
# commenting
|
50
|
+
use the keywords: 'second breakfast', 'wear the ring'
|
51
|
+
|
52
|
+
`i love second breakfast Sam is going to be a number`
|
53
|
+
`wear the ring Frodo`
|
54
|
+
|
55
|
+
everything that comes after 'second breakfast' will be commented out.
|
56
|
+
everything before 'second breakfast' will not.
|
57
|
+
|
58
|
+
# puts
|
59
|
+
use the keywords: 'bring forth the ring', 'says', 'screams', 'exclaims', 'sobbs' or 'coughs'
|
60
|
+
|
61
|
+
`bring forth the ring Frodo`
|
62
|
+
`gandalf says "they're taking the hobbits to Isengard"`
|
63
|
+
|
64
|
+
# assignment
|
65
|
+
use the keywords: 'is', 'has', 'are'
|
66
|
+
|
67
|
+
`Frodo is 8`
|
68
|
+
`Elves are worried when they saw 7 hobbits approaching the lair`
|
69
|
+
|
70
|
+
# increment
|
71
|
+
use the keywords: 'eats lembas bread', 'fortifies stronghold', 'rests', 'recieves Evenstar' or 'reforges Narsil'
|
72
|
+
|
73
|
+
`Frodo eats lembas bread`
|
74
|
+
|
75
|
+
# decrement
|
76
|
+
use the keywords: 'runs out of lembas bread', 'lost' or 'hunted by orcs'
|
77
|
+
|
78
|
+
`Sam runs out of lembas bread`
|
79
|
+
`Pippin gets hunted by orcs`
|
80
|
+
|
81
|
+
# addition
|
82
|
+
use the keywords: 'joins', 'and' or 'accompanies'
|
83
|
+
|
84
|
+
Frodo joins the fellowship with a Nazgul
|
85
|
+
Gollum and Frodo went inside mordor
|
86
|
+
|
87
|
+
# subtraction
|
88
|
+
use the keywords: 'leaves the fellowship', 'stabs', 'banishes' or 'steals'
|
89
|
+
|
90
|
+
Sam leaves the fellowship without telling Sam
|
91
|
+
a Wraith accidentally stabs Sauron in the face at a really important business meeting
|
92
|
+
|
93
|
+
# multiplication
|
94
|
+
use the keywords: 'gives aid to', 'procreates' or 'bolsters'
|
95
|
+
|
96
|
+
Arigorn bolsters Helmsdeep
|
97
|
+
|
98
|
+
# division
|
99
|
+
use the keywords: 'decapitates' or 'dismembers'
|
100
|
+
|
101
|
+
Gimly dismembers Legolas
|
102
|
+
|
103
|
+
# true
|
104
|
+
use the keywords: 'precious'
|
105
|
+
|
106
|
+
Legolas is precious
|
107
|
+
|
108
|
+
#negation
|
109
|
+
use keywords: 'not'
|
110
|
+
|
111
|
+
the Balrog is not precious
|
112
|
+
|
113
|
+
# loops
|
114
|
+
use the keywords: 'whilst' or 'during the journey'
|
115
|
+
|
116
|
+
whilst Frodo climed as strong as he could, Saruman cried, nooo!!
|
117
|
+
|
118
|
+
# comparisons
|
119
|
+
use keywords: 'equal', 'same' or 'similar'
|
120
|
+
|
121
|
+
Gollum can be the same as Smeagol
|
122
|
+
|
123
|
+
# greater/less than
|
124
|
+
use keywords: 'stronger than' or 'more' and 'weaker than' or 'less'
|
125
|
+
|
126
|
+
Bilbo ate more tree food than Treebeard
|
127
|
+
|
128
|
+
# conditionals
|
129
|
+
use keywords: 'does', 'if' or 'will'
|
130
|
+
|
131
|
+
will Mary equal Pippin?
|
132
|
+
|
133
|
+
# end
|
134
|
+
use keywords: 'you shall not pass'
|
135
|
+
|
136
|
+
you shall not pass
|
137
|
+
|
138
|
+
#defining a function
|
139
|
+
use keywords: 'transcribe' or 'tell a story'
|
140
|
+
|
141
|
+
transcribe the book about Mordor
|
142
|
+
|
143
|
+
#calling a function
|
144
|
+
use keywords: "theyre taking the hobbits to"
|
145
|
+
|
146
|
+
they are taking the hobbits to Mordor
|
147
|
+
|
148
|
+
#establishing parameters
|
149
|
+
use keywords: 'with'
|
150
|
+
|
151
|
+
they are taking the hobbits to Mordor with Nazgul, Frodo
|
152
|
+
|
153
|
+
everything after the keyword 'with' is a parameter. use a ',' or ' ' to separate parameters
|
154
|
+
|
155
|
+
#class words
|
156
|
+
use keywords: 'chapter :'
|
22
157
|
|
23
|
-
|
158
|
+
chapter : TheMines
|
24
159
|
|
25
|
-
|
160
|
+
the format here is important. everything after the ':' will be considered a classname.
|
161
|
+
classnames must be capitalized
|
26
162
|
|
27
163
|
## Development
|
28
164
|
|
@@ -32,7 +168,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
168
|
|
33
169
|
## Contributing
|
34
170
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/<github username>/
|
171
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/<github username>/precious. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
172
|
|
37
173
|
## License
|
38
174
|
|
@@ -40,4 +176,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
40
176
|
|
41
177
|
## Code of Conduct
|
42
178
|
|
43
|
-
Everyone interacting in the
|
179
|
+
Everyone interacting in the Precious project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/<github username>/precious/blob/master/CODE_OF_CONDUCT.md).
|
data/lib/my_precious/parser.rb
CHANGED
@@ -1,38 +1,33 @@
|
|
1
|
-
require 'pry'
|
2
|
-
|
3
1
|
class Parser
|
2
|
+
WRITER_FILE = 'output.rb'
|
4
3
|
# KEYWORDS
|
4
|
+
#LINE_KEYWORDS = [',', '.', '?', '!', ';']
|
5
5
|
PUT_KEYWORDS = ['bring forth the ring', 'says', 'screams', 'exclaims', 'sobbs', 'coughs']
|
6
6
|
COMMENT_KEYWORDS = ['second breakfast', 'wear the ring']
|
7
|
-
ASSIGNMENT_KEYWORDS = ['is', '
|
7
|
+
ASSIGNMENT_KEYWORDS = ['is', 'are', 'was', 'has']
|
8
8
|
INCREMENT_KEYWORDS = ['eats lembas bread', 'fortifies stronghold', 'rests', 'recieves Evenstar', 'reforges Narsil']
|
9
9
|
DECREMENT_KEYWORDS = ['runs out of lembas bread', 'lost', 'hunted by orcs']
|
10
10
|
ADDITION_KEYWORDS = ['joins', 'join', 'and', 'accompanies']
|
11
11
|
SUBTRACTION_KEYWORDS = ['leaves the fellowship', 'stabs', 'banishes', 'steals']
|
12
12
|
MULTIPLICATION_KEYWORDS = ['gives aid to', 'procreates', 'bolsters']
|
13
13
|
DIVISION_KEYWORDS = ['decapitates', 'dismembers']
|
14
|
-
COMPARISON_KEYWORDS = ['same', '
|
14
|
+
COMPARISON_KEYWORDS = ['equal', 'same', 'similar']
|
15
15
|
CONDITION_KEYWORDS = ['does', 'if', 'will']
|
16
16
|
END_KEYWORDS = ['you shall not pass']
|
17
17
|
TRUE_KEYWORDS = ['precious']
|
18
|
-
FALSE_KEYWORDS = ['not precious']
|
19
18
|
LOOP_KEYWORDS = ['whilst', 'during the journey']
|
20
19
|
GREATER_THAN_KEYWORDS = ['stronger than', 'more']
|
21
20
|
LESS_THAN_KEYWORDS = ['weaker than', 'less']
|
22
|
-
|
23
|
-
|
21
|
+
FUNCTION_DEF_KEYWORDS = ['transcribe', 'tell a story']
|
22
|
+
FUNCTION_CALL_KEYWORDS = ["theyre taking the hobbits to"]
|
23
|
+
PARAM_KEYWORDS = ['with']
|
24
|
+
NEGATION_KEYWORDS = ['not']
|
25
|
+
CLASS_KEYWORDS = ['chapter']
|
26
|
+
OPERATOR_KEYWORDS = ['#', '+', '-', '=', '*', '/', '+=1', '-=1', 'puts', '==', 'if', 'end', 'true', 'while', '>', '<', '!', 'def', '(', ')', 'class']
|
24
27
|
|
25
|
-
ALICIA_KEYS = [
|
26
|
-
INCREMENT_KEYWORDS, DECREMENT_KEYWORDS, ADDITION_KEYWORDS,
|
27
|
-
SUBTRACTION_KEYWORDS, DIVISION_KEYWORDS, MULTIPLICATION_KEYWORDS,
|
28
|
-
COMPARISON_KEYWORDS, CONDITION_KEYWORDS, END_KEYWORDS, TRUE_KEYWORDS, FALSE_KEYWORDS, LOOP_KEYWORDS, GREATER_THAN_KEYWORDS, LESS_THAN_KEYWORDS]
|
28
|
+
ALICIA_KEYS = [END_KEYWORDS, PUT_KEYWORDS, ASSIGNMENT_KEYWORDS, INCREMENT_KEYWORDS, DECREMENT_KEYWORDS, ADDITION_KEYWORDS, SUBTRACTION_KEYWORDS, DIVISION_KEYWORDS, MULTIPLICATION_KEYWORDS, COMPARISON_KEYWORDS, CONDITION_KEYWORDS, TRUE_KEYWORDS, LOOP_KEYWORDS, GREATER_THAN_KEYWORDS, LESS_THAN_KEYWORDS, NEGATION_KEYWORDS, FUNCTION_DEF_KEYWORDS, FUNCTION_CALL_KEYWORDS, CLASS_KEYWORDS]
|
29
29
|
|
30
|
-
MAP = [{'puts': PUT_KEYWORDS}, {'#': COMMENT_KEYWORDS},
|
31
|
-
{'=': ASSIGNMENT_KEYWORDS}, {'+=1': INCREMENT_KEYWORDS},
|
32
|
-
{'-=1': DECREMENT_KEYWORDS}, {'+': ADDITION_KEYWORDS},
|
33
|
-
{'-': SUBTRACTION_KEYWORDS}, {'*': MULTIPLICATION_KEYWORDS},
|
34
|
-
{'/': DIVISION_KEYWORDS}, {'==': COMPARISON_KEYWORDS},
|
35
|
-
{'if': CONDITION_KEYWORDS}, {'end': END_KEYWORDS}, {'true': TRUE_KEYWORDS}, {'false': FALSE_KEYWORDS}, {'while': LOOP_KEYWORDS}, {'>': GREATER_THAN_KEYWORDS}, {'<': LESS_THAN_KEYWORDS}]
|
30
|
+
MAP = [{'puts': PUT_KEYWORDS}, {'#': COMMENT_KEYWORDS}, {'=': ASSIGNMENT_KEYWORDS}, {'+=1': INCREMENT_KEYWORDS}, {'-=1': DECREMENT_KEYWORDS}, {'+': ADDITION_KEYWORDS}, {'-': SUBTRACTION_KEYWORDS}, {'*': MULTIPLICATION_KEYWORDS}, {'/': DIVISION_KEYWORDS}, {'==': COMPARISON_KEYWORDS}, {'if': CONDITION_KEYWORDS}, {'end': END_KEYWORDS}, {'true': TRUE_KEYWORDS}, {'while': LOOP_KEYWORDS}, {'>': GREATER_THAN_KEYWORDS}, {'<': LESS_THAN_KEYWORDS}, {'!': NEGATION_KEYWORDS}, {'def': FUNCTION_DEF_KEYWORDS}, {'': FUNCTION_CALL_KEYWORDS},{'(': PARAM_KEYWORDS}, {'class': CLASS_KEYWORDS}]
|
36
31
|
|
37
32
|
def self.parse_file(file, output_file_name)
|
38
33
|
str = ""
|
@@ -47,7 +42,6 @@ class Parser
|
|
47
42
|
end
|
48
43
|
|
49
44
|
def self.parse_line(line, index)
|
50
|
-
|
51
45
|
#error handeling
|
52
46
|
#ignore lines of length 1, its empty
|
53
47
|
if line.length == 1
|
@@ -55,54 +49,78 @@ class Parser
|
|
55
49
|
return line
|
56
50
|
else
|
57
51
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
52
|
+
#check if there are strings in line
|
53
|
+
quote = ""
|
54
|
+
if line.include? '"'
|
55
|
+
quote = get_quote(line)
|
56
|
+
line = line.gsub(quote, 'quote_placeholder')
|
57
|
+
end
|
64
58
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
59
|
+
#check if line is a comment
|
60
|
+
comment = ""
|
61
|
+
if COMMENT_KEYWORDS.any? { |word| line.include?(word) }
|
62
|
+
line = parse(line, COMMENT_KEYWORDS)
|
63
|
+
comment = store_important(line, '#')
|
64
|
+
line = line.gsub(comment, 'comment_placeholder')
|
65
|
+
end
|
72
66
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
67
|
+
#check if line has params
|
68
|
+
params = ""
|
69
|
+
if ((PARAM_KEYWORDS.any? { |word| line.include?(word) }) &&
|
70
|
+
((FUNCTION_DEF_KEYWORDS.any? { |word| line.include?(word) }) ||
|
71
|
+
(FUNCTION_CALL_KEYWORDS.any? { |word| line.include?(word) })))
|
72
|
+
line = parse(line, PARAM_KEYWORDS)
|
73
|
+
params = remove_newline(store_important(line, '('))
|
74
|
+
line = line.gsub(params, 'param_placeholder ') + ')'
|
75
|
+
end
|
78
76
|
|
79
|
-
|
80
|
-
|
81
|
-
line =
|
82
|
-
|
77
|
+
class_word = ""
|
78
|
+
if CLASS_KEYWORDS.any? { |word| line.include?(word) }
|
79
|
+
line = parse(line, CLASS_KEYWORDS)
|
80
|
+
class_word = store_important(line, ':')
|
81
|
+
line = line.gsub(class_word, ' class_word_placeholder')
|
82
|
+
end
|
83
83
|
|
84
|
-
|
85
|
-
|
84
|
+
#get rid of special chars
|
85
|
+
line_array = line.split(" ")
|
86
|
+
line_array.each_with_index do |word, index|
|
87
|
+
line_array[index] = purify(word)
|
88
|
+
end
|
86
89
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
90
|
+
#check for keywords
|
91
|
+
#calls if statements
|
92
|
+
line = line_array.join(" ")
|
93
|
+
line = check_for_keywords(line)
|
91
94
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
95
|
+
#remove extra english words
|
96
|
+
line = only_valuable_words(line).downcase
|
97
|
+
|
98
|
+
#put string back into line
|
99
|
+
if line.include? ('"quote_placeholder"')
|
100
|
+
line = line.gsub('quote_placeholder', quote)
|
101
|
+
end
|
102
|
+
|
103
|
+
if line.include? ('comment_placeholder')
|
104
|
+
line = line.gsub('comment_placeholder', comment)
|
105
|
+
end
|
106
|
+
|
107
|
+
if line.include? ('param_placeholder')
|
108
|
+
line = line.gsub('param_placeholder', params.downcase)
|
109
|
+
line = format_function(line)
|
110
|
+
end
|
111
|
+
|
112
|
+
if line.include? ('class_word_placeholder')
|
113
|
+
line = line.gsub('class_word_placeholder', "#{class_word}")
|
114
|
+
end
|
115
|
+
|
116
|
+
# write("#{line}")
|
117
|
+
return line
|
98
118
|
end
|
99
119
|
end
|
100
120
|
|
101
121
|
def self.check_for_keywords(line)
|
102
122
|
ALICIA_KEYS.each do |keywords|
|
103
|
-
|
104
|
-
line = parse(line, keywords)
|
105
|
-
end
|
123
|
+
line = parse(line, keywords)
|
106
124
|
end
|
107
125
|
line
|
108
126
|
end
|
@@ -111,6 +129,7 @@ class Parser
|
|
111
129
|
def self.parse(line, keywords)
|
112
130
|
keywords.each do |keyword|
|
113
131
|
if line.include? keyword
|
132
|
+
puts keyword
|
114
133
|
replacement = find_replacement(keywords)
|
115
134
|
line = line.gsub(keyword, replacement)
|
116
135
|
end
|
@@ -119,7 +138,11 @@ class Parser
|
|
119
138
|
end
|
120
139
|
|
121
140
|
def self.purify(word)
|
122
|
-
word = word.gsub(/[!@%&.?,]/,'')
|
141
|
+
word = word.gsub(/[!@%&.?,]/,'')# get rid of special chars
|
142
|
+
end
|
143
|
+
|
144
|
+
def self.remove_newline(line)
|
145
|
+
line.gsub(/\n/, "")
|
123
146
|
end
|
124
147
|
|
125
148
|
def self.only_valuable_words(line)
|
@@ -138,9 +161,13 @@ class Parser
|
|
138
161
|
valuable = false
|
139
162
|
if /[[:upper:]]/.match(word[0]) #if variable
|
140
163
|
valuable = true
|
141
|
-
elsif word == '"quote_placeholder"' #if string
|
164
|
+
elsif word == '"quote_placeholder"' #if string/quote
|
165
|
+
valuable = true
|
166
|
+
elsif word == '#comment_placeholder' #if comment
|
167
|
+
valuable = true
|
168
|
+
elsif word == '(param_placeholder' #if params
|
142
169
|
valuable = true
|
143
|
-
elsif word == '
|
170
|
+
elsif word == 'class_word_placeholder' #if class
|
144
171
|
valuable = true
|
145
172
|
elsif word.to_i.to_s == word #if num
|
146
173
|
valuable = true
|
@@ -150,7 +177,7 @@ class Parser
|
|
150
177
|
valuable
|
151
178
|
end
|
152
179
|
|
153
|
-
def self.
|
180
|
+
def self.get_quote(phrase)
|
154
181
|
phrase_array = phrase.split('')
|
155
182
|
index = phrase_array.find_index { |i| i == '"'} + 1
|
156
183
|
quote = ""
|
@@ -161,15 +188,15 @@ class Parser
|
|
161
188
|
quote
|
162
189
|
end
|
163
190
|
|
164
|
-
def self.
|
191
|
+
def self.store_important(phrase, key)
|
165
192
|
phrase_array = phrase.split('')
|
166
|
-
index = phrase_array.find_index { |i| i ==
|
167
|
-
|
193
|
+
index = phrase_array.find_index { |i| i == key} + 1
|
194
|
+
string = ""
|
168
195
|
while index < phrase_array.length
|
169
|
-
|
196
|
+
string << phrase_array[index]
|
170
197
|
index += 1
|
171
198
|
end
|
172
|
-
|
199
|
+
string
|
173
200
|
end
|
174
201
|
|
175
202
|
def self.find_replacement(keywords)
|
@@ -180,11 +207,20 @@ class Parser
|
|
180
207
|
end
|
181
208
|
end
|
182
209
|
end
|
210
|
+
return nil
|
211
|
+
end
|
212
|
+
|
213
|
+
def self.format_function(line)
|
214
|
+
phrase_array = line.split('')
|
215
|
+
index = phrase_array.find_index { |i| i == "("}
|
216
|
+
phrase_array.delete_at(index - 1)
|
217
|
+
phrase_array.delete_at(index - 2)
|
218
|
+
phrase_array.join("")
|
183
219
|
end
|
184
220
|
|
185
221
|
def self.write(str, writer_file_name)
|
186
222
|
writer_file = File.open(writer_file_name, 'w')
|
187
223
|
writer_file.write(str)
|
188
224
|
end
|
189
|
-
|
190
|
-
end
|
225
|
+
|
226
|
+
end #end of Parser Class
|
data/lib/my_precious/version.rb
CHANGED
data/lib/my_precious.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'thor'
|
2
2
|
require 'my_precious/version'
|
3
|
-
require 'my_precious/
|
3
|
+
require 'my_precious/parser'
|
4
4
|
|
5
5
|
module MyPrecious
|
6
6
|
class Error < StandardError; end
|
@@ -8,22 +8,28 @@ module MyPrecious
|
|
8
8
|
class CLI < Thor
|
9
9
|
|
10
10
|
desc 'bring_forth FILE', 'creates a .precious FILE where users can write their LOTR code'
|
11
|
-
def
|
11
|
+
def forge(file_name)
|
12
12
|
file = File.open(file_name + '.precious', 'w')
|
13
|
-
puts "You have succesfully
|
13
|
+
puts "You have succesfully forged #{file_name}.precious"
|
14
14
|
end
|
15
15
|
|
16
16
|
desc 'parse FILE', 'parse a .precious FILE and output the result'
|
17
|
-
def
|
17
|
+
def bring_forth(reader_file_name, writer_file_name)
|
18
18
|
exts_are_valid = check_reader_and_writer_exts(reader_file_name, writer_file_name)
|
19
19
|
if exts_are_valid
|
20
20
|
file = File.open(reader_file_name, 'r')
|
21
|
-
|
21
|
+
Parser.parse_file file, writer_file_name
|
22
22
|
puts "You have successfully transcribed #{reader_file_name}.precious into #{writer_file_name}"
|
23
23
|
file.close
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
+
def destroy(file_name)
|
28
|
+
if File.exist?(file_name)
|
29
|
+
File.delete(file_name)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
27
33
|
# def speak_friend file_name
|
28
34
|
# isvalid = check_ext(file_name)
|
29
35
|
# if isvalid
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: my_precious
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jessicabettsftw
|
@@ -79,7 +79,6 @@ files:
|
|
79
79
|
- lib/my_precious.rb
|
80
80
|
- lib/my_precious/cli.rb
|
81
81
|
- lib/my_precious/parser.rb
|
82
|
-
- lib/my_precious/parser_2.rb
|
83
82
|
- lib/my_precious/version.rb
|
84
83
|
- middle_earth.precious
|
85
84
|
- my_precious.gemspec
|
data/lib/my_precious/parser_2.rb
DELETED
@@ -1,222 +0,0 @@
|
|
1
|
-
class Parser2
|
2
|
-
WRITER_FILE = 'output.rb'
|
3
|
-
# KEYWORDS
|
4
|
-
#LINE_KEYWORDS = [',', '.', '?', '!', ';']
|
5
|
-
PUT_KEYWORDS = ['bring forth the ring', 'says', 'screams', 'exclaims', 'sobbs', 'coughs']
|
6
|
-
COMMENT_KEYWORDS = ['second breakfast', 'wear the ring']
|
7
|
-
ASSIGNMENT_KEYWORDS = ['is', 'are', 'was', 'has']
|
8
|
-
INCREMENT_KEYWORDS = ['eats lembas bread', 'fortifies stronghold', 'rests', 'recieves Evenstar', 'reforges Narsil']
|
9
|
-
DECREMENT_KEYWORDS = ['runs out of lembas bread', 'lost', 'hunted by orcs']
|
10
|
-
ADDITION_KEYWORDS = ['joins', 'join', 'and', 'accompanies']
|
11
|
-
SUBTRACTION_KEYWORDS = ['leaves the fellowship', 'stabs', 'banishes', 'steals']
|
12
|
-
MULTIPLICATION_KEYWORDS = ['gives aid to', 'procreates', 'bolsters']
|
13
|
-
DIVISION_KEYWORDS = ['decapitates', 'dismembers']
|
14
|
-
COMPARISON_KEYWORDS = ['equal', 'same', 'similar']
|
15
|
-
CONDITION_KEYWORDS = ['does', 'if', 'will']
|
16
|
-
END_KEYWORDS = ['you shall not pass']
|
17
|
-
TRUE_KEYWORDS = ['precious']
|
18
|
-
LOOP_KEYWORDS = ['whilst', 'during the journey']
|
19
|
-
GREATER_THAN_KEYWORDS = ['stronger than', 'more']
|
20
|
-
LESS_THAN_KEYWORDS = ['weaker than', 'less']
|
21
|
-
FUNCTION_DEF_KEYWORDS = ['transcribe', 'tell a story']
|
22
|
-
FUNCTION_CALL_KEYWORDS = ["theyre taking the hobbits to"]
|
23
|
-
PARAM_KEYWORDS = ['with']
|
24
|
-
NEGATION_KEYWORDS = ['not']
|
25
|
-
CLASS_KEYWORDS = ['chapter']
|
26
|
-
OPERATOR_KEYWORDS = ['#', '+', '-', '=', '*', '/', '+=1', '-=1', 'puts', '==', 'if', 'end', 'true', 'while', '>', '<', '!', 'def', '(', ')', 'class']
|
27
|
-
|
28
|
-
ALICIA_KEYS = [END_KEYWORDS, PUT_KEYWORDS, ASSIGNMENT_KEYWORDS, INCREMENT_KEYWORDS, DECREMENT_KEYWORDS, ADDITION_KEYWORDS, SUBTRACTION_KEYWORDS, DIVISION_KEYWORDS, MULTIPLICATION_KEYWORDS, COMPARISON_KEYWORDS, CONDITION_KEYWORDS, TRUE_KEYWORDS, LOOP_KEYWORDS, GREATER_THAN_KEYWORDS, LESS_THAN_KEYWORDS, NEGATION_KEYWORDS, FUNCTION_DEF_KEYWORDS, FUNCTION_CALL_KEYWORDS, CLASS_KEYWORDS]
|
29
|
-
|
30
|
-
MAP = [{'puts': PUT_KEYWORDS}, {'#': COMMENT_KEYWORDS}, {'=': ASSIGNMENT_KEYWORDS}, {'+=1': INCREMENT_KEYWORDS}, {'-=1': DECREMENT_KEYWORDS}, {'+': ADDITION_KEYWORDS}, {'-': SUBTRACTION_KEYWORDS}, {'*': MULTIPLICATION_KEYWORDS}, {'/': DIVISION_KEYWORDS}, {'==': COMPARISON_KEYWORDS}, {'if': CONDITION_KEYWORDS}, {'end': END_KEYWORDS}, {'true': TRUE_KEYWORDS}, {'while': LOOP_KEYWORDS}, {'>': GREATER_THAN_KEYWORDS}, {'<': LESS_THAN_KEYWORDS}, {'!': NEGATION_KEYWORDS}, {'def': FUNCTION_DEF_KEYWORDS}, {'': FUNCTION_CALL_KEYWORDS},{'(': PARAM_KEYWORDS}, {'class': CLASS_KEYWORDS}]
|
31
|
-
|
32
|
-
def self.parse_file(file, output_file_name)
|
33
|
-
str = ""
|
34
|
-
writer_file_name = output_file_name
|
35
|
-
file.each_with_index do |file, index|
|
36
|
-
file.each_line do |line|
|
37
|
-
line = parse_line(line, index)
|
38
|
-
str << (line + "\n")
|
39
|
-
end
|
40
|
-
end
|
41
|
-
write(str, writer_file_name)
|
42
|
-
end
|
43
|
-
|
44
|
-
def self.parse_line(line, index)
|
45
|
-
#error handeling
|
46
|
-
#ignore lines of length 1, its empty
|
47
|
-
if line.length == 1
|
48
|
-
# write("#{line}")
|
49
|
-
return line
|
50
|
-
else
|
51
|
-
|
52
|
-
#check if there are strings in line
|
53
|
-
quote = ""
|
54
|
-
if line.include? '"'
|
55
|
-
quote = get_quote(line)
|
56
|
-
line = line.gsub(quote, 'quote_placeholder')
|
57
|
-
end
|
58
|
-
|
59
|
-
#check if line is a comment
|
60
|
-
comment = ""
|
61
|
-
if COMMENT_KEYWORDS.any? { |word| line.include?(word) }
|
62
|
-
line = parse(line, COMMENT_KEYWORDS)
|
63
|
-
comment = store_important(line, '#')
|
64
|
-
line = line.gsub(comment, 'comment_placeholder')
|
65
|
-
end
|
66
|
-
|
67
|
-
#check if line has params
|
68
|
-
params = ""
|
69
|
-
if ((PARAM_KEYWORDS.any? { |word| line.include?(word) }) &&
|
70
|
-
((FUNCTION_DEF_KEYWORDS.any? { |word| line.include?(word) }) ||
|
71
|
-
(FUNCTION_CALL_KEYWORDS.any? { |word| line.include?(word) })))
|
72
|
-
line = parse(line, PARAM_KEYWORDS)
|
73
|
-
params = remove_newline(store_important(line, '('))
|
74
|
-
line = line.gsub(params, 'param_placeholder ') + ')'
|
75
|
-
end
|
76
|
-
|
77
|
-
class_word = ""
|
78
|
-
if CLASS_KEYWORDS.any? { |word| line.include?(word) }
|
79
|
-
line = parse(line, CLASS_KEYWORDS)
|
80
|
-
class_word = store_important(line, ':')
|
81
|
-
line = line.gsub(class_word, ' class_word_placeholder')
|
82
|
-
end
|
83
|
-
|
84
|
-
#get rid of special chars
|
85
|
-
line_array = line.split(" ")
|
86
|
-
line_array.each_with_index do |word, index|
|
87
|
-
line_array[index] = purify(word)
|
88
|
-
end
|
89
|
-
|
90
|
-
#check for keywords
|
91
|
-
#calls if statements
|
92
|
-
line = line_array.join(" ")
|
93
|
-
line = check_for_keywords(line)
|
94
|
-
|
95
|
-
#remove extra english words
|
96
|
-
line = only_valuable_words(line).downcase
|
97
|
-
|
98
|
-
#put string back into line
|
99
|
-
if line.include? ('"quote_placeholder"')
|
100
|
-
line = line.gsub('quote_placeholder', quote)
|
101
|
-
end
|
102
|
-
|
103
|
-
if line.include? ('comment_placeholder')
|
104
|
-
line = line.gsub('comment_placeholder', comment)
|
105
|
-
end
|
106
|
-
|
107
|
-
if line.include? ('param_placeholder')
|
108
|
-
line = line.gsub('param_placeholder', params.downcase)
|
109
|
-
line = format_function(line)
|
110
|
-
end
|
111
|
-
|
112
|
-
if line.include? ('class_word_placeholder')
|
113
|
-
line = line.gsub('class_word_placeholder', "#{class_word}")
|
114
|
-
end
|
115
|
-
|
116
|
-
# write("#{line}")
|
117
|
-
return line
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
def self.check_for_keywords(line)
|
122
|
-
ALICIA_KEYS.each do |keywords|
|
123
|
-
line = parse(line, keywords)
|
124
|
-
end
|
125
|
-
line
|
126
|
-
end
|
127
|
-
|
128
|
-
# METHODS
|
129
|
-
def self.parse(line, keywords)
|
130
|
-
keywords.each do |keyword|
|
131
|
-
if line.include? keyword
|
132
|
-
puts keyword
|
133
|
-
replacement = find_replacement(keywords)
|
134
|
-
line = line.gsub(keyword, replacement)
|
135
|
-
end
|
136
|
-
end
|
137
|
-
return line
|
138
|
-
end
|
139
|
-
|
140
|
-
def self.remove_newline(line)
|
141
|
-
line.gsub(/\n/, "")
|
142
|
-
end
|
143
|
-
|
144
|
-
def self.only_valuable_words(line)
|
145
|
-
important_words = []
|
146
|
-
line_array = line.split(" ")
|
147
|
-
line_array.each do |word|
|
148
|
-
valuable_word = valuable?(word)
|
149
|
-
if valuable_word
|
150
|
-
important_words << word
|
151
|
-
end
|
152
|
-
end
|
153
|
-
line = important_words.join(" ")
|
154
|
-
end
|
155
|
-
|
156
|
-
def self.valuable?(word)
|
157
|
-
valuable = false
|
158
|
-
if /[[:upper:]]/.match(word[0]) #if variable
|
159
|
-
valuable = true
|
160
|
-
elsif word == '"quote_placeholder"' #if string/quote
|
161
|
-
valuable = true
|
162
|
-
elsif word == '#comment_placeholder' #if comment
|
163
|
-
valuable = true
|
164
|
-
elsif word == '(param_placeholder' #if params
|
165
|
-
valuable = true
|
166
|
-
elsif word == 'class_word_placeholder' #if class
|
167
|
-
valuable = true
|
168
|
-
elsif word.to_i.to_s == word #if num
|
169
|
-
valuable = true
|
170
|
-
elsif OPERATOR_KEYWORDS.include? word #if operator
|
171
|
-
valuable = true
|
172
|
-
end
|
173
|
-
valuable
|
174
|
-
end
|
175
|
-
|
176
|
-
def self.get_quote(phrase)
|
177
|
-
phrase_array = phrase.split('')
|
178
|
-
index = phrase_array.find_index { |i| i == '"'} + 1
|
179
|
-
quote = ""
|
180
|
-
while phrase_array[index] != '"'
|
181
|
-
quote << phrase_array[index]
|
182
|
-
index += 1
|
183
|
-
end
|
184
|
-
quote
|
185
|
-
end
|
186
|
-
|
187
|
-
def self.store_important(phrase, key)
|
188
|
-
phrase_array = phrase.split('')
|
189
|
-
index = phrase_array.find_index { |i| i == key} + 1
|
190
|
-
string = ""
|
191
|
-
while index < phrase_array.length
|
192
|
-
string << phrase_array[index]
|
193
|
-
index += 1
|
194
|
-
end
|
195
|
-
string
|
196
|
-
end
|
197
|
-
|
198
|
-
def self.find_replacement(keywords)
|
199
|
-
MAP.each do |hash|
|
200
|
-
hash.each do |key, value|
|
201
|
-
if value == keywords
|
202
|
-
return key.to_s
|
203
|
-
end
|
204
|
-
end
|
205
|
-
end
|
206
|
-
return nil
|
207
|
-
end
|
208
|
-
|
209
|
-
def self.format_function(line)
|
210
|
-
phrase_array = line.split('')
|
211
|
-
index = phrase_array.find_index { |i| i == "("}
|
212
|
-
phrase_array.delete_at(index - 1)
|
213
|
-
phrase_array.delete_at(index - 2)
|
214
|
-
phrase_array.join("")
|
215
|
-
end
|
216
|
-
|
217
|
-
def self.write(str, writer_file_name)
|
218
|
-
writer_file = File.open(writer_file_name, 'w')
|
219
|
-
writer_file.write(str)
|
220
|
-
end
|
221
|
-
|
222
|
-
end #end of Parser Class
|