chelsy 0.0.5 → 0.0.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ca69c9ae44f38d8c0857b220996ba0d61b317f9f
4
- data.tar.gz: 8bf0eae470330703ad014bedf2b721a78147e643
3
+ metadata.gz: 4878521c7b523d8c46ebc91d752c23af91616475
4
+ data.tar.gz: e74c0550a0a40cc62d3956d2c6379d3c33ceed9c
5
5
  SHA512:
6
- metadata.gz: 7f52743c6703285660c60b3f9da020d9a4e11ff57e6847142cbb3b91e5b041dce5fc4ee3ee19404a30667e987a0104afc78c2f8ef3f05270f51e0dc289b5e192
7
- data.tar.gz: 0aa25da10d2d50d6b620c3236edb4c98ca8def623b333b04c588963fd6bdfbf14f87a49ef7a6e3abff45d9447079680671968fd2c0ee7debb77fac7eeaa716a7
6
+ metadata.gz: edc3882ef58112153fe6af6817b0caba5dcfc1221d0cb080e164c58873431cca39677ec1b32f6df97271684c5cff35c8e1869b0413cdbf58e7f9c3236d99f7fe
7
+ data.tar.gz: 87b5270bd4276f3ec3b5d9009dda2d34cf89c84efe28c2c3d8b17874884e39de0788a07b2828d6d0642b64a9e2f8c182f50ca5a861f543cc7f94b32a6ae7daf7
data/README.md CHANGED
@@ -4,10 +4,12 @@
4
4
  [![Build Status](https://travis-ci.org/ishikawa/chelsy.svg?branch=master)](https://travis-ci.org/ishikawa/chelsy)
5
5
  [![Gem](https://img.shields.io/gem/v/chelsy.svg)](https://rubygems.org/gems/chelsy)
6
6
 
7
- > C code generator written in Ruby
7
+ > C code generator library written in Ruby
8
8
 
9
9
  **Chelsy** is C code generator library written in Ruby. You can construct AST objects and then transform it to C code.
10
10
 
11
+ **This library heavily under development. Anything may change at any time. The public API should not be considered stable.**
12
+
11
13
  ## Table of Contents
12
14
 
13
15
  - [Installation](#installation)
data/chelsy.gemspec CHANGED
@@ -10,8 +10,8 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ['takanori.ishikawa@gmail.com']
11
11
 
12
12
  spec.homepage = 'https://github.com/ishikawa/chelsy'
13
- spec.summary = 'C code generator written in Ruby'
14
- spec.description = 'C code generator written in Ruby'
13
+ spec.summary = 'C code generator library written in Ruby'
14
+ spec.description = 'C code generator library written in Ruby'
15
15
  spec.license = 'MIT'
16
16
 
17
17
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
data/lib/chelsy/ast.rb CHANGED
@@ -6,6 +6,21 @@ module Chelsy
6
6
  class Node
7
7
  def initialize(**opts)
8
8
  end
9
+
10
+ protected
11
+
12
+ # Returns an object as an immutable string.
13
+ #
14
+ # @param [Object] obj an Object
15
+ # @return [String] The string representation of `obj`. It's frozen (unmodifiable).
16
+ def immutable_stringify(obj)
17
+ str = obj.to_s
18
+ if str.frozen?
19
+ str
20
+ else
21
+ str.dup.freeze
22
+ end
23
+ end
9
24
  end
10
25
 
11
26
  # The class must provide a method validate_node`
@@ -107,6 +122,25 @@ module Chelsy
107
122
  end
108
123
  end
109
124
 
125
+ # This node represents arbitrary C code snippet. It can be an decsendant of any node.
126
+ # However it is used mainly in macro definition.
127
+ class Raw < Element
128
+ # @!attribute [r] code
129
+ # @return [String] C code snippet
130
+ attr_reader :code
131
+
132
+ # Initialize instance.
133
+ # @param [#to_s] code C code snippet
134
+ def initialize(code, **rest)
135
+ @code = immutable_stringify(code)
136
+ super **rest
137
+ end
138
+
139
+ def to_s
140
+ @code
141
+ end
142
+ end
143
+
110
144
  class Declarative < Element
111
145
  attr_reader :storage
112
146
 
@@ -922,16 +956,6 @@ module Chelsy
922
956
  class EmptyStmt < Stmt
923
957
  end
924
958
 
925
- class ExprStmt < Stmt
926
- attr_reader :expr
927
-
928
- def initialize(expr, **rest)
929
- @expr = Syntax::Expr.ensure(expr)
930
-
931
- super(**rest)
932
- end
933
- end
934
-
935
959
  # == 6.8.2 Compound statement
936
960
 
937
961
  class Block < Stmt
@@ -1130,7 +1154,7 @@ module Chelsy
1130
1154
  attr_reader :location
1131
1155
 
1132
1156
  def initialize(location, system: false, **rest)
1133
- @location = location.to_s.dup
1157
+ @location = immutable_stringify(location)
1134
1158
  @system = !!system
1135
1159
 
1136
1160
  super **rest
@@ -1151,7 +1175,7 @@ module Chelsy
1151
1175
  def initialize(name, params=nil, replacement, **rest)
1152
1176
  @name = Syntax::Ident.ensure(name)
1153
1177
  @params = IdentList.new(params) if params
1154
- @replacement = Syntax::Raw.ensure(replacement)
1178
+ @replacement = Syntax::MacroDefinition.ensure(replacement)
1155
1179
 
1156
1180
  super **rest
1157
1181
  end
@@ -1204,7 +1228,7 @@ module Chelsy
1204
1228
 
1205
1229
  def initialize(lineno, filename=nil, **rest)
1206
1230
  @lineno = Syntax::Int.ensure(lineno)
1207
- @filename = Syntax::Raw.ensure(filename) if filename
1231
+ @filename = immutable_stringify(filename) if filename
1208
1232
 
1209
1233
  super **rest
1210
1234
  end
@@ -1215,7 +1239,7 @@ module Chelsy
1215
1239
  attr_reader :pragma
1216
1240
 
1217
1241
  def initialize(pragma, **rest)
1218
- @pragma = Syntax::Raw.ensure(pragma)
1242
+ @pragma = immutable_stringify(pragma)
1219
1243
  super **rest
1220
1244
  end
1221
1245
  end
@@ -1244,7 +1268,6 @@ module Chelsy
1244
1268
  module Syntax
1245
1269
  TopLevel = Any.new('TopLevel', [Declarative])
1246
1270
  Type = Any.new('TypeSpecifier', [Chelsy::Type::Base, :void])
1247
- Raw = Any.new('Raw', [String])
1248
1271
  Int = Any.new('Int', [::Integer])
1249
1272
  Ident = Any.new('Identifier', [Symbol])
1250
1273
  Expr = Any.new('Expression', [Chelsy::Expr, Syntax::Ident])
@@ -1259,7 +1282,7 @@ module Chelsy
1259
1282
  EnumMember = Any.new('EnumMember', [Chelsy::EnumMember, Symbol])
1260
1283
  Initializer = Any.new('Initializer', [Syntax::Expr, Chelsy::Initializer, Chelsy::InitializerList])
1261
1284
  Stmt = Any.new('Statement', [
1262
- Syntax::Expr, # Treats Expr as ExprStmt
1285
+ Syntax::Expr, # Treats Expr as Expression Statement
1263
1286
  Chelsy::Stmt])
1264
1287
  BlockItem = Any.new('BlockItem', [
1265
1288
  Syntax::Stmt,
@@ -1267,5 +1290,8 @@ module Chelsy
1267
1290
  Declaration = Any.new('Declaration', [Chelsy::Declaration])
1268
1291
  StdcPragma = Any.new('STDC Pragma', [:FP_CONTRACT, :FENV_ACCESS, :CX_LIMITED_RANGE])
1269
1292
  StdcPragmaState = Any.new('STDC Pragma State', [:ON, :OFF, :DEFAULT])
1293
+ MacroDefinition = Any.new('Raw', [
1294
+ Chelsy::Raw,
1295
+ Syntax::BlockItem])
1270
1296
  end
1271
1297
  end
data/lib/chelsy/syntax.rb CHANGED
@@ -34,9 +34,9 @@ module Chelsy; module Syntax
34
34
  end
35
35
 
36
36
  def accept?(node)
37
- # Most C program uses C preprocessor, so we accept any raw string representation.
37
+ # Most C program uses C preprocessor, so we must accept any C code snippet.
38
38
  case node
39
- when String
39
+ when Chelsy::Raw
40
40
  true
41
41
  else
42
42
  @constraints.any? do |constraint|
@@ -70,6 +70,9 @@ module Chelsy
70
70
 
71
71
  def translate_element(node)
72
72
  case node
73
+ when Raw
74
+ node.code
75
+
73
76
  # Document
74
77
  when Document
75
78
  translate_document(node)
@@ -93,8 +96,6 @@ module Chelsy
93
96
  # Statements
94
97
  when EmptyStmt
95
98
  translate_empty_stmt(node)
96
- when ExprStmt
97
- translate_expr_stmt(node)
98
99
  when If
99
100
  translate_if(node)
100
101
  when Switch
@@ -588,9 +589,10 @@ module Chelsy
588
589
  src << ')'
589
590
  end
590
591
 
591
- unless node.replacement.empty?
592
+ replacement = translate(node.replacement)
593
+ unless replacement.empty?
592
594
  src << ' '
593
- src << node.replacement.to_s
595
+ src << replacement
594
596
  end
595
597
  end
596
598
  end
@@ -613,12 +615,12 @@ module Chelsy
613
615
 
614
616
  def translate_line_directive(node)
615
617
  "#line #{node.lineno}".tap do |src|
616
- src << " \"#{node.filename}\"" if node.filename
618
+ src << " \"#{translate node.filename}\"" if node.filename
617
619
  end
618
620
  end
619
621
 
620
622
  def translate_pragma_directive(node)
621
- "#pragma #{node.pragma}"
623
+ "#pragma #{translate node.pragma}"
622
624
  end
623
625
 
624
626
  private
@@ -1,3 +1,3 @@
1
1
  module Chelsy
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -10,18 +10,18 @@ doc.fragments << Comment::Multi.new([
10
10
  "Print Fahrenheit to Celsius table",
11
11
  "(fahr = 0, 20, ..., 300)"
12
12
  ])
13
- doc.fragments << Directive::Define.new(:LOWER, '0')
14
- doc.fragments << Directive::Define.new(:UPPER, '300')
15
- doc.fragments << Directive::Define.new(:STEP, '20')
13
+ doc.fragments << Directive::Define.new(:LOWER, Constant::Int.new(0))
14
+ doc.fragments << Directive::Define.new(:UPPER, Constant::Int.new(300))
15
+ doc.fragments << Directive::Define.new(:STEP, Constant::Int.new(20))
16
16
 
17
17
  doc << Function.new(:main,
18
18
  Type::Int.new, [
19
19
  Param.new(:argc, Type::Int.new),
20
20
  Param.new(:argv, Type::Pointer.new(Type::Pointer.new(Type::Char.new(const: true)))),
21
21
  ]) do |b|
22
- init = Declaration.new(:fahr, Type::Int.new, 'LOWER')
23
- cond = Operator::LessThanOrEqual.new(:fahr, 'UPPER')
24
- step = Operator::AssignAdd.new(:fahr, 'STEP')
22
+ init = Declaration.new(:fahr, Type::Int.new, :LOWER)
23
+ cond = Operator::LessThanOrEqual.new(:fahr, :UPPER)
24
+ step = Operator::AssignAdd.new(:fahr, :STEP)
25
25
 
26
26
  b << For.new(init, cond, step) do |body|
27
27
  celsius = Operator::Sub.new(:fahr, Constant::Int.new(32))
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chelsy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takanori Ishikawa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-26 00:00:00.000000000 Z
11
+ date: 2016-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,7 +80,7 @@ dependencies:
80
80
  - - ~>
81
81
  - !ruby/object:Gem::Version
82
82
  version: 0.9.5
83
- description: C code generator written in Ruby
83
+ description: C code generator library written in Ruby
84
84
  email:
85
85
  - takanori.ishikawa@gmail.com
86
86
  executables: []
@@ -129,5 +129,5 @@ rubyforge_project:
129
129
  rubygems_version: 2.0.14.1
130
130
  signing_key:
131
131
  specification_version: 4
132
- summary: C code generator written in Ruby
132
+ summary: C code generator library written in Ruby
133
133
  test_files: []