shaun 0.0.1 → 0.1.0

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/prettyprinter.rb +78 -0
  3. data/lib/shaun.rb +45 -8
  4. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e6e1e67d80575fc1d34735200b6388269d939c3f
4
- data.tar.gz: 4b321f883efb357cd4685e0642178c6d35c90076
3
+ metadata.gz: 3488c6165d5e961e63a1009586c268a1749d6dd3
4
+ data.tar.gz: 75b5def6f77b85f748262c9e6336e10cc1921e3d
5
5
  SHA512:
6
- metadata.gz: c06eaf873d54732a48cb1d2eefc376617be485f1acc379678c457fdd454f59b4bd632925c8d12eb40e5f571c0ff900bd33b92e574eb36aac5afd517d3d13c07b
7
- data.tar.gz: 8f27fd9d632f87fe720dab76fd1358733688275acaa16388bc9bc54e3d6955da25d76fa923dfc7114368fb41243cd90f7d2b1ccc43812f710ff8ab4cce03cd81
6
+ metadata.gz: 1ac1179a6baaf9e2137249d28c53ba92ec18a5390227715591ea5a72eab9a732194b76d14cf262533d7242481b14f2b1bce771dfec6b359f3b9339b65943a2b6
7
+ data.tar.gz: cfd4db81f5ce2bc67190d802092b0b16947c4c8ac271db3348afb094f5a253b48317695e1cc0eb808f43b9751a3e5ea37f35cf5a51873e1598face72a398c02c
@@ -0,0 +1,78 @@
1
+ module Shaun
2
+ # The SHAUN pretty printer. It prints a SHAUN value so that it is easy to read
3
+ class PrettyPrinter
4
+ # Create a pretty printer with the tabulation string (2 spaces by default)
5
+ def initialize(tabchar = ' ')
6
+ @tab = tabchar
7
+ @indent_level = 0
8
+ end
9
+
10
+ # Add a level of indentation
11
+ def indent
12
+ @indent_level = @indent_level + 1
13
+ end
14
+
15
+ # remove a level of indentation
16
+ def dedent
17
+ @indent_level = @indent_level - 1
18
+ end
19
+
20
+ # Get the indentation string
21
+ def indent_string
22
+ @tab * @indent_level
23
+ end
24
+
25
+ # Pretty print a number
26
+ def print_number(sn_num)
27
+ sn_num.to_s
28
+ end
29
+
30
+ # Pretty print a boolean
31
+ def print_bool(b)
32
+ b.to_s
33
+ end
34
+
35
+ # Pretty print a string
36
+ def print_string(s)
37
+ "\"#{s}\""
38
+ end
39
+
40
+ # Pretty print a null value
41
+ def print_nil
42
+ "null"
43
+ end
44
+
45
+ # Pretty print a list
46
+ def print_list(ary)
47
+ indent
48
+ contents = ary.map { |e| print_list_element e }.join "\n"
49
+ dedent
50
+ "[\n" + contents + "\n" + indent_string + "]"
51
+ end
52
+
53
+ # Pretty print an object
54
+ def print_object(obj)
55
+ indent
56
+
57
+ attributes = obj.instance_variables.map do |name|
58
+ value = obj.instance_variable_get name
59
+ attr_name = name.to_s.sub /@/, ''
60
+ print_object_attribute attr_name, value
61
+ end
62
+
63
+ contents = attributes.join "\n"
64
+ dedent
65
+ "{\n" + contents + "\n" + indent_string + "}"
66
+ end
67
+
68
+ private
69
+ def print_object_attribute(name, value)
70
+ contents = "#{name}: #{value.pretty_print(self)}"
71
+ indent_string + contents
72
+ end
73
+
74
+ def print_list_element(elem)
75
+ indent_string + elem.pretty_print(self)
76
+ end
77
+ end
78
+ end
data/lib/shaun.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require_relative 'prettyprinter.rb'
2
+
1
3
  # The main module that holds every SHAUN related classes and some helper functions
2
4
  module Shaun
3
5
  # Helper module for classes that are tranparent SHAUN values (String, Boolean, Object)
@@ -18,7 +20,7 @@ module Shaun
18
20
  # Object values are stored as instance attributes
19
21
  #
20
22
  # === Example
21
- # obj = Sn::Object.new({ greetings: 'hello' })
23
+ # obj = Shaun::Object.new({ greetings: 'hello' })
22
24
  # greetings = obj.greetings
23
25
  # obj.greetings = 'New hello!'
24
26
  def initialize(hash = {})
@@ -44,6 +46,11 @@ module Shaun
44
46
  end
45
47
  end
46
48
 
49
+ # Pretty print the SHAUN value
50
+ def pretty_print(pp)
51
+ pp.print_object self
52
+ end
53
+
47
54
  private
48
55
  # Get the object's metaclass
49
56
  def metaclass
@@ -93,6 +100,11 @@ module Shaun
93
100
  "#{@value.to_s}"
94
101
  end
95
102
  end
103
+
104
+ # Pretty print the SHAUN value
105
+ def pretty_print(pp)
106
+ pp.print_number self
107
+ end
96
108
  end
97
109
 
98
110
  # Helper method to create a SHAUN object
@@ -113,14 +125,14 @@ end
113
125
 
114
126
  class Hash
115
127
  def to_sn
116
- Sn::Object.new self
128
+ Shaun::Object.new self
117
129
  end
118
130
  end
119
131
 
120
132
  class Numeric
121
133
  # Cast to a SHAUN number
122
134
  def to_sn
123
- Sn::Number.new self
135
+ Shaun::Number.new self
124
136
  end
125
137
 
126
138
  # Patched method #method_missing in order to generate a SHAUN number
@@ -129,7 +141,7 @@ class Numeric
129
141
  res = m.match /([a-zA-Z_][a-zA-Z0-9_]*)/
130
142
  if res and params.empty?
131
143
  unit = res[1].to_s
132
- Sn::Number.new self, unit
144
+ Shaun::Number.new self, unit
133
145
  else
134
146
  super m, *params
135
147
  end
@@ -137,19 +149,39 @@ class Numeric
137
149
  end
138
150
 
139
151
  class String
140
- include Sn::ShaunValue
152
+ include Shaun::ShaunValue
153
+
154
+ # Pretty print the SHAUN value
155
+ def pretty_print(pp)
156
+ pp.print_string self
157
+ end
141
158
  end
142
159
 
143
160
  class TrueClass
144
- include Sn::ShaunValue
161
+ include Shaun::ShaunValue
162
+
163
+ # Pretty print the SHAUN value
164
+ def pretty_print(pp)
165
+ pp.print_bool self
166
+ end
145
167
  end
146
168
 
147
169
  class FalseClass
148
- include Sn::ShaunValue
170
+ include Shaun::ShaunValue
171
+
172
+ # Pretty print the SHAUN value
173
+ def pretty_print(pp)
174
+ pp.print_bool self
175
+ end
149
176
  end
150
177
 
151
178
  class NilClass
152
- include Sn::ShaunValue
179
+ include Shaun::ShaunValue
180
+
181
+ # Pretty print the SHAUN value
182
+ def pretty_print(pp)
183
+ pp.print_nil
184
+ end
153
185
  end
154
186
 
155
187
  class Array
@@ -158,4 +190,9 @@ class Array
158
190
  def to_sn
159
191
  map { |e| e.to_sn }
160
192
  end
193
+
194
+ # Pretty print the SHAUN value
195
+ def pretty_print(pp)
196
+ pp.print_list self
197
+ end
161
198
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shaun
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kévin Le Bon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-30 00:00:00.000000000 Z
11
+ date: 2018-01-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |2
14
14
  This gem is an Ruby implementation of the SHAUN data-notation language.
@@ -18,6 +18,7 @@ executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
+ - lib/prettyprinter.rb
21
22
  - lib/shaun.rb
22
23
  homepage:
23
24
  licenses: