tyrano_dsl 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.travis.yml +3 -0
  4. data/CODE_OF_CONDUCT.md +74 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +290 -0
  8. data/Rakefile +10 -0
  9. data/bin/setup +8 -0
  10. data/bin/tyrano_dsl.rb +21 -0
  11. data/lib/tyrano_dsl/elements/background.rb +31 -0
  12. data/lib/tyrano_dsl/elements/character.rb +49 -0
  13. data/lib/tyrano_dsl/elements/elements_module.rb +7 -0
  14. data/lib/tyrano_dsl/elements/jump_target.rb +18 -0
  15. data/lib/tyrano_dsl/elements/label.rb +19 -0
  16. data/lib/tyrano_dsl/elements/scene.rb +21 -0
  17. data/lib/tyrano_dsl/elements/stance.rb +26 -0
  18. data/lib/tyrano_dsl/elements/title_screen.rb +16 -0
  19. data/lib/tyrano_dsl/elements/variable.rb +22 -0
  20. data/lib/tyrano_dsl/elements/world.rb +62 -0
  21. data/lib/tyrano_dsl/elements_writers/background_writer.rb +30 -0
  22. data/lib/tyrano_dsl/elements_writers/character_writer.rb +32 -0
  23. data/lib/tyrano_dsl/elements_writers/characters_writer.rb +43 -0
  24. data/lib/tyrano_dsl/elements_writers/elements_writers_module.rb +27 -0
  25. data/lib/tyrano_dsl/elements_writers/scene_writer.rb +30 -0
  26. data/lib/tyrano_dsl/elements_writers/title_screen_writer.rb +87 -0
  27. data/lib/tyrano_dsl/elements_writers/variables_writer.rb +30 -0
  28. data/lib/tyrano_dsl/file_actions/clear_directory.rb +34 -0
  29. data/lib/tyrano_dsl/file_actions/create_file.rb +33 -0
  30. data/lib/tyrano_dsl/file_actions/file_copy.rb +36 -0
  31. data/lib/tyrano_dsl/file_actions/files_actions_module.rb +28 -0
  32. data/lib/tyrano_dsl/file_actions/json_patch.rb +47 -0
  33. data/lib/tyrano_dsl/main.rb +18 -0
  34. data/lib/tyrano_dsl/parsed_word.rb +22 -0
  35. data/lib/tyrano_dsl/parser.rb +58 -0
  36. data/lib/tyrano_dsl/parsing_context.rb +18 -0
  37. data/lib/tyrano_dsl/parsing_words/ask_question.rb +32 -0
  38. data/lib/tyrano_dsl/parsing_words/conditional_jump.rb +42 -0
  39. data/lib/tyrano_dsl/parsing_words/declare_background.rb +32 -0
  40. data/lib/tyrano_dsl/parsing_words/declare_character.rb +38 -0
  41. data/lib/tyrano_dsl/parsing_words/declare_label.rb +20 -0
  42. data/lib/tyrano_dsl/parsing_words/declare_variable.rb +30 -0
  43. data/lib/tyrano_dsl/parsing_words/display_text.rb +22 -0
  44. data/lib/tyrano_dsl/parsing_words/hide_character.rb +20 -0
  45. data/lib/tyrano_dsl/parsing_words/hide_message_window.rb +16 -0
  46. data/lib/tyrano_dsl/parsing_words/include_file.rb +26 -0
  47. data/lib/tyrano_dsl/parsing_words/jump.rb +22 -0
  48. data/lib/tyrano_dsl/parsing_words/parsing_words_module.rb +72 -0
  49. data/lib/tyrano_dsl/parsing_words/set_background.rb +21 -0
  50. data/lib/tyrano_dsl/parsing_words/set_character_stance.rb +23 -0
  51. data/lib/tyrano_dsl/parsing_words/set_title_screen_background.rb +25 -0
  52. data/lib/tyrano_dsl/parsing_words/show_character.rb +26 -0
  53. data/lib/tyrano_dsl/parsing_words/show_message_window.rb +16 -0
  54. data/lib/tyrano_dsl/parsing_words/start_scene.rb +30 -0
  55. data/lib/tyrano_dsl/parsing_words/update_variable.rb +37 -0
  56. data/lib/tyrano_dsl/tyrano_dsl.rb +3 -0
  57. data/lib/tyrano_dsl/tyrano_exception.rb +6 -0
  58. data/lib/tyrano_dsl/vocabulary.rb +84 -0
  59. data/lib/tyrano_dsl/writer.rb +114 -0
  60. data/lib/tyrano_dsl/writing_context.rb +123 -0
  61. data/lib/tyrano_dsl/writing_words/ask_question.rb +28 -0
  62. data/lib/tyrano_dsl/writing_words/conditional_jump.rb +37 -0
  63. data/lib/tyrano_dsl/writing_words/declare_background.rb +5 -0
  64. data/lib/tyrano_dsl/writing_words/declare_character.rb +5 -0
  65. data/lib/tyrano_dsl/writing_words/declare_label.rb +18 -0
  66. data/lib/tyrano_dsl/writing_words/declare_variable.rb +5 -0
  67. data/lib/tyrano_dsl/writing_words/display_text.rb +16 -0
  68. data/lib/tyrano_dsl/writing_words/hide_character.rb +13 -0
  69. data/lib/tyrano_dsl/writing_words/hide_message_window.rb +12 -0
  70. data/lib/tyrano_dsl/writing_words/include_file.rb +5 -0
  71. data/lib/tyrano_dsl/writing_words/jump.rb +18 -0
  72. data/lib/tyrano_dsl/writing_words/nop.rb +8 -0
  73. data/lib/tyrano_dsl/writing_words/set_background.rb +18 -0
  74. data/lib/tyrano_dsl/writing_words/set_character_stance.rb +20 -0
  75. data/lib/tyrano_dsl/writing_words/set_title_screen_background.rb +4 -0
  76. data/lib/tyrano_dsl/writing_words/show_character.rb +31 -0
  77. data/lib/tyrano_dsl/writing_words/show_message_window.rb +12 -0
  78. data/lib/tyrano_dsl/writing_words/start_scene.rb +9 -0
  79. data/lib/tyrano_dsl/writing_words/update_variable.rb +28 -0
  80. data/lib/tyrano_dsl/writing_words/writing_words_module.rb +43 -0
  81. data/tyrano_dsl.gemspec +37 -0
  82. metadata +210 -0
@@ -0,0 +1,42 @@
1
+ require_relative '../elements/jump_target'
2
+ require_relative '../vocabulary'
3
+ require_relative 'parsing_words_module'
4
+
5
+ module TyranoDsl::ParsingWords::ConditionalJump
6
+
7
+ include TyranoDsl::ParsingWords::ParsingWordsModule
8
+
9
+ VALID_OPERATORS = [
10
+ '<',
11
+ '>',
12
+ '==',
13
+ '!=',
14
+ ]
15
+
16
+ # @param [String] variable
17
+ # @param [String] operator
18
+ # @param [String, Symbol, Float] value
19
+ # @param [String] scene_name
20
+ # @param [String, nil] label_name
21
+ # @return [void]
22
+ # @raise [TyranoDsl::TyranoException]
23
+ def conditional_jump(variable, operator, value, scene_name, label_name = nil)
24
+ check_variable_exist(variable)
25
+ if value.is_a?(String) || value.is_a?(Symbol)
26
+ check_variable_exist(value)
27
+ end
28
+ unless VALID_OPERATORS.include? operator
29
+ raise TyranoDsl::TyranoException, "Unknown operator [#{operator}]"
30
+ end
31
+ add_parsed_word(
32
+ TyranoDsl::Vocabulary::CONDITIONAL_JUMP,
33
+ variable: variable,
34
+ operator: operator,
35
+ value: value,
36
+ scene_name: scene_name,
37
+ label_name: label_name
38
+ )
39
+ label = label_name ? context.world.label_value(label_name) : nil
40
+ context.world.jump_targets << TyranoDsl::Elements::JumpTarget.new(scene_name, label)
41
+ end
42
+ end
@@ -0,0 +1,32 @@
1
+ require_relative '../elements/background'
2
+ require_relative '../vocabulary'
3
+ require_relative 'parsing_words_module'
4
+
5
+
6
+ module TyranoDsl::ParsingWords::DeclareBackground
7
+
8
+ include TyranoDsl::ParsingWords::ParsingWordsModule
9
+
10
+ # @param [String] background_name
11
+ # @param [String] image_path
12
+ # @return [void]
13
+ # @raise [TyranoDsl::TyranoException]
14
+ def declare_background(background_name, image_path)
15
+ image_full_path = file_full_path(image_path)
16
+ if context.world.backgrounds.key? background_name
17
+ raise TyranoDsl::TyranoException, "Duplicated background [#{background_name}]"
18
+ end
19
+ context.world.backgrounds[background_name] =
20
+ TyranoDsl::Elements::Background.new(
21
+ background_name,
22
+ image_full_path,
23
+ context.world.backgrounds.length + 1
24
+ )
25
+ add_parsed_word(
26
+ TyranoDsl::Vocabulary::DECLARE_BACKGROUND,
27
+ name: background_name, image_path: image_full_path
28
+ )
29
+ end
30
+
31
+ end
32
+
@@ -0,0 +1,38 @@
1
+ require_relative '../elements/character'
2
+ require_relative '../vocabulary'
3
+ require_relative 'parsing_words_module'
4
+
5
+ module TyranoDsl::ParsingWords::DeclareCharacter
6
+
7
+ include TyranoDsl::ParsingWords::ParsingWordsModule
8
+
9
+ # @param [String] character_name
10
+ # @param [Hash{String => String}] stances
11
+ # @return [void]
12
+ # @raise [TyranoDsl::TyranoException]
13
+ def declare_character(character_name, stances)
14
+ symbolized_stances = symbolize_keys(stances)
15
+ symbolized_stances.each_pair do |name, path|
16
+ symbolized_stances[name] = file_full_path(path)
17
+ end
18
+
19
+ default_stance = symbolized_stances[:default]
20
+ unless default_stance
21
+ raise TyranoDsl::TyranoException, 'A default stance is required'
22
+ end
23
+ if context.world.characters.key? character_name
24
+ raise TyranoDsl::TyranoException, "Duplicated character [#{character_name}]"
25
+ end
26
+ context.world.characters[character_name] = TyranoDsl::Elements::Character.new(
27
+ character_name,
28
+ symbolized_stances,
29
+ context.world.characters.length + 1
30
+ )
31
+
32
+ add_parsed_word(
33
+ TyranoDsl::Vocabulary::DECLARE_CHARACTER,
34
+ name: character_name, stances: symbolized_stances, default_stance: default_stance
35
+ )
36
+ end
37
+
38
+ end
@@ -0,0 +1,20 @@
1
+ require_relative '../elements/background'
2
+ require_relative '../vocabulary'
3
+ require_relative 'parsing_words_module'
4
+
5
+ module TyranoDsl::ParsingWords::DeclareLabel
6
+
7
+ include TyranoDsl::ParsingWords::ParsingWordsModule
8
+
9
+ # @param [String] label_name
10
+ # @return [void]
11
+ # @raise [TyranoDsl::TyranoException]
12
+ def declare_label(label_name)
13
+ add_parsed_word(
14
+ TyranoDsl::Vocabulary::DECLARE_LABEL,
15
+ label_name: label_name
16
+ )
17
+ context.world.label_value(label_name)
18
+ end
19
+
20
+ end
@@ -0,0 +1,30 @@
1
+ require_relative '../elements/variable'
2
+ require_relative '../vocabulary'
3
+ require_relative 'parsing_words_module'
4
+
5
+ module TyranoDsl::ParsingWords::DeclareVariable
6
+
7
+ include TyranoDsl::ParsingWords::ParsingWordsModule
8
+
9
+ # @param [String\Symbol] variable_name
10
+ # @param [Float] initial_value
11
+ # @return [void]
12
+ # @raise [TyranoDsl::TyranoException]
13
+ def declare_variable(variable_name, initial_value)
14
+ if context.world.variables.key? variable_name
15
+ raise TyranoDsl::TyranoException, "Duplicated variable [#{variable_name}]"
16
+ else
17
+ context.world.variables[variable_name] = TyranoDsl::Elements::Variable.new(
18
+ variable_name,
19
+ "variable_#{context.world.variables.length + 1}",
20
+ initial_value
21
+ )
22
+ end
23
+ add_parsed_word(
24
+ TyranoDsl::Vocabulary::DECLARE_VARIABLE,
25
+ variable_name: variable_name,
26
+ initial_value: initial_value,
27
+ )
28
+ end
29
+
30
+ end
@@ -0,0 +1,22 @@
1
+ require_relative '../vocabulary'
2
+ require_relative 'parsing_words_module'
3
+
4
+ module TyranoDsl::ParsingWords::DisplayText
5
+
6
+ include TyranoDsl::ParsingWords::ParsingWordsModule
7
+
8
+ # @param [String, nil] character_name
9
+ # @param [String] text
10
+ # @return [void]
11
+ # @raise [TyranoDsl::TyranoException]
12
+ def display_text(character_name, text)
13
+ if character_name
14
+ check_character_exist(character_name)
15
+ end
16
+ add_parsed_word(
17
+ TyranoDsl::Vocabulary::DISPLAY_TEXT,
18
+ character_name: character_name, text: text
19
+ )
20
+ end
21
+
22
+ end
@@ -0,0 +1,20 @@
1
+ require_relative '../vocabulary'
2
+ require_relative 'parsing_words_module'
3
+
4
+
5
+ module TyranoDsl::ParsingWords::HideCharacter
6
+
7
+ include TyranoDsl::ParsingWords::ParsingWordsModule
8
+
9
+ # @param [String] character_name
10
+ # @return [void]
11
+ # @raise [TyranoDsl::TyranoException]
12
+ def hide_character(character_name)
13
+ check_character_exist(character_name)
14
+ add_parsed_word(
15
+ TyranoDsl::Vocabulary::HIDE_CHARACTER,
16
+ name: character_name
17
+ )
18
+ end
19
+
20
+ end
@@ -0,0 +1,16 @@
1
+ require_relative '../vocabulary'
2
+ require_relative 'parsing_words_module'
3
+
4
+ module TyranoDsl::ParsingWords::HideMessageWindow
5
+
6
+ include TyranoDsl::ParsingWords::ParsingWordsModule
7
+
8
+ # @return [void]
9
+ # @raise [TyranoDsl::TyranoException]
10
+ def hide_message_window
11
+ add_parsed_word(
12
+ TyranoDsl::Vocabulary::HIDE_MESSAGE_WINDOW,
13
+ )
14
+ end
15
+
16
+ end
@@ -0,0 +1,26 @@
1
+ require_relative '../vocabulary'
2
+ require_relative 'parsing_words_module'
3
+
4
+
5
+ module TyranoDsl::ParsingWords::IncludeFile
6
+
7
+ include TyranoDsl::ParsingWords::ParsingWordsModule
8
+
9
+ # @param [String] file_path
10
+ # @return [void]
11
+ # @raise [TyranoDsl::TyranoException]
12
+ def include_file(file_path)
13
+ log {"Reading content file at [#{file_path}]"}
14
+ complete_file_path = File.absolute_path(file_path, File.dirname(included_files_hierarchy.last))
15
+ unless File.exist? complete_file_path
16
+ raise TyranoDsl::TyranoException, "File not found [#{complete_file_path}]"
17
+ end
18
+ included_files_hierarchy << complete_file_path
19
+ log {"Start parsing [#{complete_file_path}]"}
20
+ file_content = IO.read(complete_file_path)
21
+ instance_eval(file_content, complete_file_path, 1)
22
+ log {"End parsing [#{complete_file_path}]"}
23
+ included_files_hierarchy.pop
24
+ end
25
+
26
+ end
@@ -0,0 +1,22 @@
1
+ require_relative '../elements/jump_target'
2
+ require_relative '../vocabulary'
3
+ require_relative 'parsing_words_module'
4
+
5
+ module TyranoDsl::ParsingWords::Jump
6
+
7
+ include TyranoDsl::ParsingWords::ParsingWordsModule
8
+
9
+ # @param [String] scene_name
10
+ # @param [String, nil] label_name
11
+ # @return [void]
12
+ # @raise [TyranoDsl::TyranoException]
13
+ def jump(scene_name, label_name = nil)
14
+ add_parsed_word(
15
+ TyranoDsl::Vocabulary::JUMP,
16
+ scene_name: scene_name,
17
+ label_name: label_name
18
+ )
19
+ label = label_name ? context.world.label_value(label_name) : nil
20
+ context.world.jump_targets << TyranoDsl::Elements::JumpTarget.new(scene_name, label)
21
+ end
22
+ end
@@ -0,0 +1,72 @@
1
+ require_relative '../tyrano_exception'
2
+ require_relative '../vocabulary'
3
+
4
+ module TyranoDsl
5
+ module ParsingWords
6
+
7
+ module ParsingWordsModule
8
+
9
+ protected
10
+
11
+ # @param [String] file_path
12
+ # @return [String]
13
+ # @raise [TyranoDsl::TyranoException]
14
+ def file_full_path(file_path)
15
+ absolute_path = File.absolute_path(file_path, File.dirname(included_files_hierarchy.last))
16
+ unless File.exist?(absolute_path)
17
+ raise TyranoDsl::TyranoException, "Missing file [#{absolute_path}]"
18
+ end
19
+ absolute_path
20
+ end
21
+
22
+ # @param [String] variable_name
23
+ # @return [void]
24
+ # @raise [TyranoDsl::TyranoException]
25
+ def check_variable_exist(variable_name)
26
+ unless context.world.variables.key?(variable_name)
27
+ raise_unknown('variable', variable_name, context.world.variables.keys)
28
+ end
29
+ end
30
+
31
+ # @param [String] character_name
32
+ # @param [String, nil] character_stance
33
+ # @return [void]
34
+ # @raise [TyranoDsl::TyranoException]
35
+ def check_character_exist(character_name, character_stance = nil)
36
+ character = context.world.characters[character_name]
37
+ unless character
38
+ raise_unknown('character', character_name, context.world.characters.keys)
39
+ end
40
+ if character_stance && !character.stances.key?(character_stance)
41
+ raise_unknown('stance', character_stance, character.stances.keys)
42
+ end
43
+ end
44
+
45
+ # @param [Hash] hash
46
+ # @return [Hash{Symbol=>Object}]
47
+ def symbolize_keys(hash)
48
+ result = {}
49
+ hash.keys.each do |key|
50
+ result[symbolize(key)] = hash[key]
51
+ end
52
+ result
53
+ end
54
+
55
+ # @param [String] string
56
+ # @return [Symbol]
57
+ def symbolize(string)
58
+ string.to_sym rescue string
59
+ end
60
+
61
+ # @param [String] type
62
+ # @param [String] unknown_name
63
+ # @param [Array<String>] current_elements
64
+ # @return [void]
65
+ # @raise [TyranoDsl::TyranoException]
66
+ def raise_unknown(type, unknown_name, current_elements)
67
+ raise TyranoDsl::TyranoException, "Unknown #{type} [#{unknown_name}], currently #{current_elements.length} defined: #{current_elements.sort.collect {|e| "[#{e}]"}.join(', ')}"
68
+ end
69
+
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,21 @@
1
+ require_relative '../vocabulary'
2
+ require_relative 'parsing_words_module'
3
+
4
+ module TyranoDsl::ParsingWords::SetBackground
5
+
6
+ include TyranoDsl::ParsingWords::ParsingWordsModule
7
+
8
+ # @param [String] background_name
9
+ # @return [void]
10
+ # @raise [TyranoDsl::TyranoException]
11
+ def set_background(background_name)
12
+ unless context.world.backgrounds.key? background_name
13
+ raise_unknown('background', background_name, context.world.backgrounds.keys)
14
+ end
15
+ add_parsed_word(
16
+ TyranoDsl::Vocabulary::SET_BACKGROUND,
17
+ name: background_name
18
+ )
19
+ end
20
+
21
+ end
@@ -0,0 +1,23 @@
1
+ require_relative '../vocabulary'
2
+ require_relative 'parsing_words_module'
3
+
4
+ module TyranoDsl::ParsingWords::SetCharacterStance
5
+
6
+ include TyranoDsl::ParsingWords::ParsingWordsModule
7
+
8
+ # @param [String] character_name
9
+ # @param [String] character_stance
10
+ # @return [void]
11
+ # @raise [TyranoDsl::TyranoException]
12
+ def set_character_stance(character_name, character_stance)
13
+ symbolized_stance = symbolize(character_stance)
14
+ check_character_exist(character_name, symbolized_stance)
15
+
16
+ add_parsed_word(
17
+ TyranoDsl::Vocabulary::SET_CHARACTER_STANCE,
18
+ name: character_name,
19
+ stance: symbolized_stance
20
+ )
21
+ end
22
+
23
+ end
@@ -0,0 +1,25 @@
1
+ require_relative '../vocabulary'
2
+ require_relative 'parsing_words_module'
3
+
4
+ module TyranoDsl::ParsingWords::SetTitleScreenBackground
5
+
6
+ include TyranoDsl::ParsingWords::ParsingWordsModule
7
+
8
+ # @param [String] background_name
9
+ # @return [void]
10
+ # @raise [TyranoDsl::TyranoException]
11
+ def set_title_screen_background(background_name)
12
+ unless context.world.backgrounds.key? background_name
13
+ raise_unknown('background', background_name, context.world.backgrounds.keys)
14
+ end
15
+ if context.world.title_screen.background
16
+ raise TyranoDsl::TyranoException, 'Title screen background already defined'
17
+ end
18
+ context.world.title_screen.background = background_name
19
+ add_parsed_word(
20
+ TyranoDsl::Vocabulary::SET_TITLE_SCREEN_BACKGROUND,
21
+ name: background_name
22
+ )
23
+ end
24
+
25
+ end
@@ -0,0 +1,26 @@
1
+ require_relative '../vocabulary'
2
+ require_relative 'parsing_words_module'
3
+
4
+ module TyranoDsl::ParsingWords::ShowCharacter
5
+
6
+ include TyranoDsl::ParsingWords::ParsingWordsModule
7
+
8
+ # @param [String] character_name
9
+ # @param [String] character_stance
10
+ # @param [Integer] left
11
+ # @param [Integer] top
12
+ # @return [void]
13
+ # @raise [TyranoDsl::TyranoException]
14
+ def show_character(character_name, character_stance, left, top)
15
+ symbolized_stance = symbolize(character_stance)
16
+ check_character_exist(character_name, symbolized_stance)
17
+ add_parsed_word(
18
+ TyranoDsl::Vocabulary::SHOW_CHARACTER,
19
+ name: character_name,
20
+ stance: symbolized_stance,
21
+ left: left,
22
+ top: top
23
+ )
24
+ end
25
+
26
+ end