iv-phonic 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,6 +5,7 @@ extern "C" {
5
5
  }
6
6
  #include <iv/ast_visitor.h>
7
7
  #include <iv/utils.h>
8
+ #include <iv/maybe.h>
8
9
  #include "factory.h"
9
10
  #include "encoding.h"
10
11
  #define SYM(str) ID2SYM(rb_intern(str))
@@ -78,8 +79,8 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
78
79
  rb_hash_aset(hash, SYM("type"), SYM("Declaration"));
79
80
  Visit(decl->name());
80
81
  rb_hash_aset(hash, SYM("name"), ret_);
81
- if (decl->expr()) {
82
- decl->expr()->Accept(this);
82
+ if (const core::Maybe<const Expression> expr = decl->expr()) {
83
+ (*expr).Accept(this);
83
84
  rb_hash_aset(hash, SYM("expr"), ret_);
84
85
  }
85
86
  SetLocation(hash, decl);
@@ -100,8 +101,8 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
100
101
  rb_hash_aset(hash, SYM("cond"), ret_);
101
102
  stmt->then_statement()->Accept(this);
102
103
  rb_hash_aset(hash, SYM("then"), ret_);
103
- if (stmt->else_statement()) {
104
- stmt->else_statement()->Accept(this);
104
+ if (const core::Maybe<const Statement> else_stmt = stmt->else_statement()) {
105
+ (*else_stmt).Accept(this);
105
106
  rb_hash_aset(hash, SYM("else"), ret_);
106
107
  }
107
108
  SetLocation(hash, stmt);
@@ -133,16 +134,16 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
133
134
  void Visit(const ForStatement* stmt) {
134
135
  VALUE hash = rb_hash_new();
135
136
  rb_hash_aset(hash, SYM("type"), SYM("ForStatement"));
136
- if (stmt->init()) {
137
- stmt->init()->Accept(this);
137
+ if (const core::Maybe<const Statement> init = stmt->init()) {
138
+ (*init).Accept(this);
138
139
  rb_hash_aset(hash, SYM("init"), ret_);
139
140
  }
140
- if (stmt->cond()) {
141
- stmt->cond()->Accept(this);
141
+ if (const core::Maybe<const Expression> cond = stmt->cond()) {
142
+ (*cond).Accept(this);
142
143
  rb_hash_aset(hash, SYM("cond"), ret_);
143
144
  }
144
- if (stmt->next()) {
145
- stmt->next()->Accept(this);
145
+ if (const core::Maybe<const Statement> next = stmt->next()) {
146
+ (*next).Accept(this);
146
147
  rb_hash_aset(hash, SYM("next"), ret_);
147
148
  }
148
149
  stmt->body()->Accept(this);
@@ -167,8 +168,8 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
167
168
  void Visit(const ContinueStatement* stmt) {
168
169
  VALUE hash = rb_hash_new();
169
170
  rb_hash_aset(hash, SYM("type"), SYM("ContinueStatement"));
170
- if (stmt->label()) {
171
- stmt->label()->Accept(this);
171
+ if (const core::Maybe<const Identifier> label = stmt->label()) {
172
+ (*label).Accept(this);
172
173
  rb_hash_aset(hash, SYM("label"), ret_);
173
174
  }
174
175
  SetLocation(hash, stmt);
@@ -178,8 +179,8 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
178
179
  void Visit(const BreakStatement* stmt) {
179
180
  VALUE hash = rb_hash_new();
180
181
  rb_hash_aset(hash, SYM("type"), SYM("BreakStatement"));
181
- if (stmt->label()) {
182
- stmt->label()->Accept(this);
182
+ if (const core::Maybe<const Identifier> label = stmt->label()) {
183
+ (*label).Accept(this);
183
184
  rb_hash_aset(hash, SYM("label"), ret_);
184
185
  }
185
186
  SetLocation(hash, stmt);
@@ -189,8 +190,10 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
189
190
  void Visit(const ReturnStatement* stmt) {
190
191
  VALUE hash = rb_hash_new();
191
192
  rb_hash_aset(hash, SYM("type"), SYM("ReturnStatement"));
192
- stmt->expr()->Accept(this);
193
- rb_hash_aset(hash, SYM("expr"), ret_);
193
+ if (const core::Maybe<const Expression> expr = stmt->expr()) {
194
+ (*expr).Accept(this);
195
+ rb_hash_aset(hash, SYM("expr"), ret_);
196
+ }
194
197
  SetLocation(hash, stmt);
195
198
  ret_ = hash;
196
199
  }
@@ -236,12 +239,12 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
236
239
  void Visit(const CaseClause* cl) {
237
240
  VALUE hash = rb_hash_new();
238
241
  rb_hash_aset(hash, SYM("type"), SYM("CaseClause"));
239
- if (cl->IsDefault()) {
240
- rb_hash_aset(hash, SYM("kind"), SYM("Default"));
241
- } else {
242
+ if (const core::Maybe<const Expression> expr = cl->expr()) {
242
243
  rb_hash_aset(hash, SYM("kind"), SYM("Case"));
243
- cl->expr()->Accept(this);
244
+ (*expr).Accept(this);
244
245
  rb_hash_aset(hash, SYM("expr"), ret_);
246
+ } else {
247
+ rb_hash_aset(hash, SYM("kind"), SYM("Default"));
245
248
  }
246
249
  VALUE stmts = rb_ary_new();
247
250
  for (Statements::const_iterator st = cl->body().begin(),
@@ -257,13 +260,28 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
257
260
  void Visit(const ThrowStatement* stmt) {
258
261
  VALUE hash = rb_hash_new();
259
262
  rb_hash_aset(hash, SYM("type"), SYM("ThrowStatement"));
263
+ stmt->expr()->Accept(this);
264
+ rb_hash_aset(hash, SYM("expr"), ret_);
260
265
  SetLocation(hash, stmt);
261
266
  ret_ = hash;
262
267
  }
263
268
 
264
269
  void Visit(const TryStatement* stmt) {
270
+ assert(stmt->catch_block() || stmt->finally_block());
265
271
  VALUE hash = rb_hash_new();
266
272
  rb_hash_aset(hash, SYM("type"), SYM("TryStatement"));
273
+ stmt->body()->Accept(this);
274
+ rb_hash_aset(hash, SYM("body"), ret_);
275
+ if (const core::Maybe<const Identifier> ident = stmt->catch_name()) {
276
+ Visit(ident.Address());
277
+ rb_hash_aset(hash, SYM("catch_name"), ret_);
278
+ Visit(stmt->catch_block().Address());
279
+ rb_hash_aset(hash, SYM("catch_block"), ret_);
280
+ }
281
+ if (const core::Maybe<const Block> block = stmt->finally_block()) {
282
+ Visit(block.Address());
283
+ rb_hash_aset(hash, SYM("finally_block"), ret_);
284
+ }
267
285
  SetLocation(hash, stmt);
268
286
  ret_ = hash;
269
287
  }
@@ -416,13 +434,6 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
416
434
  ret_ = hash;
417
435
  }
418
436
 
419
- void Visit(const Undefined* literal) {
420
- // Undefined has no location
421
- VALUE hash = rb_hash_new();
422
- rb_hash_aset(hash, SYM("type"), SYM("Undefined"));
423
- ret_ = hash;
424
- }
425
-
426
437
  void Visit(const RegExpLiteral* literal) {
427
438
  VALUE hash = rb_hash_new();
428
439
  rb_hash_aset(hash, SYM("type"), SYM("RegExpLiteral"));
@@ -448,16 +459,26 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
448
459
  VALUE hash = rb_hash_new();
449
460
  rb_hash_aset(hash, SYM("type"), SYM("ArrayLiteral"));
450
461
  VALUE array = rb_ary_new();
451
- for (Expressions::const_iterator it = literal->items().begin(),
462
+ for (MaybeExpressions::const_iterator it = literal->items().begin(),
452
463
  last = literal->items().end(); it != last; ++it) {
453
- (*it)->Accept(this);
454
- rb_ary_push(array, ret_);
464
+ if (*it) {
465
+ (**it).Accept(this);
466
+ rb_ary_push(array, ret_);
467
+ } else {
468
+ rb_ary_push(array, NewArrayHole());
469
+ }
455
470
  }
456
471
  rb_hash_aset(hash, SYM("value"), array);
457
472
  SetLocation(hash, literal);
458
473
  ret_ = hash;
459
474
  }
460
475
 
476
+ static VALUE NewArrayHole() {
477
+ VALUE hash = rb_hash_new();
478
+ rb_hash_aset(hash, SYM("type"), SYM("ArrayHole"));
479
+ return hash;
480
+ }
481
+
461
482
  void Visit(const ObjectLiteral* literal) {
462
483
  using std::tr1::get;
463
484
  VALUE hash = rb_hash_new();
@@ -497,8 +518,8 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
497
518
  void Visit(const FunctionLiteral* literal) {
498
519
  VALUE hash = rb_hash_new();
499
520
  rb_hash_aset(hash, SYM("type"), SYM("FunctionLiteral"));
500
- if (literal->name()) {
501
- Visit(literal->name());
521
+ if (const core::Maybe<const Identifier> name = literal->name()) {
522
+ Visit(name.Address());
502
523
  rb_hash_aset(hash, SYM("name"), ret_);
503
524
  }
504
525
  {
@@ -16,8 +16,7 @@ namespace phonic {
16
16
  class AstFactory : public core::Space<2> {
17
17
  public:
18
18
  AstFactory()
19
- : core::Space<2>(),
20
- undefined_instance_(new(this)Undefined()) {
19
+ : core::Space<2>() {
21
20
  }
22
21
 
23
22
  Scope* NewScope(FunctionLiteral::DeclType type) {
@@ -88,7 +87,7 @@ class AstFactory : public core::Space<2> {
88
87
  }
89
88
 
90
89
  FunctionLiteral* NewFunctionLiteral(FunctionLiteral::DeclType type,
91
- Identifier* name,
90
+ core::Maybe<Identifier> name,
92
91
  Identifiers* params,
93
92
  Statements* body,
94
93
  Scope* scope,
@@ -110,7 +109,7 @@ class AstFactory : public core::Space<2> {
110
109
  return func;
111
110
  }
112
111
 
113
- ArrayLiteral* NewArrayLiteral(Expressions* items,
112
+ ArrayLiteral* NewArrayLiteral(MaybeExpressions* items,
114
113
  std::size_t begin, std::size_t end) {
115
114
  ArrayLiteral* ary = new (this) ArrayLiteral(items);
116
115
  ary->Location(begin, end);
@@ -151,10 +150,6 @@ class AstFactory : public core::Space<2> {
151
150
  return th;
152
151
  }
153
152
 
154
- Undefined* NewUndefined() {
155
- return undefined_instance_;
156
- }
157
-
158
153
  TrueLiteral* NewTrueLiteral(std::size_t begin, std::size_t end) {
159
154
  TrueLiteral* tr = new (this) TrueLiteral();
160
155
  tr->Location(begin, end);
@@ -201,30 +196,29 @@ class AstFactory : public core::Space<2> {
201
196
  Declarations* decls,
202
197
  std::size_t begin,
203
198
  std::size_t end) {
204
- VariableStatement* var = new (this) VariableStatement(token, decls);
205
199
  assert(!decls->empty());
200
+ VariableStatement* var = new (this) VariableStatement(token, decls);
206
201
  var->Location(begin, end);
207
202
  return var;
208
203
  }
209
204
 
210
- Declaration* NewDeclaration(Identifier* name, Expression* expr) {
211
- assert(name && expr);
205
+ Declaration* NewDeclaration(Identifier* name, core::Maybe<Expression> expr) {
206
+ assert(name);
212
207
  Declaration* decl = new (this) Declaration(name, expr);
213
- const std::size_t end = (expr->AsUndefined()) ?
214
- name->end_position() : expr->end_position();
208
+ const std::size_t end = (expr) ? (*expr).end_position() : name->end_position();
215
209
  decl->Location(name->begin_position(), end);
216
210
  return decl;
217
211
  }
218
212
 
219
213
  IfStatement* NewIfStatement(Expression* cond,
220
214
  Statement* then_statement,
221
- Statement* else_statement,
215
+ core::Maybe<Statement> else_statement,
222
216
  std::size_t begin) {
223
217
  IfStatement* stmt = new (this) IfStatement(cond,
224
218
  then_statement, else_statement);
225
219
  assert(then_statement);
226
220
  const std::size_t end = (else_statement) ?
227
- else_statement->end_position() : then_statement->end_position();
221
+ (*else_statement).end_position() : then_statement->end_position();
228
222
  stmt->Location(begin, end);
229
223
  return stmt;
230
224
  }
@@ -258,9 +252,9 @@ class AstFactory : public core::Space<2> {
258
252
  }
259
253
 
260
254
  ForStatement* NewForStatement(Statement* body,
261
- Statement* init,
262
- Expression* cond,
263
- Statement* next,
255
+ core::Maybe<Statement> init,
256
+ core::Maybe<Expression> cond,
257
+ core::Maybe<Statement> next,
264
258
  std::size_t begin) {
265
259
  assert(body);
266
260
  ForStatement* stmt = new (this) ForStatement(body, init, cond, next);
@@ -274,7 +268,7 @@ class AstFactory : public core::Space<2> {
274
268
  return stmt;
275
269
  }
276
270
 
277
- ContinueStatement* NewContinueStatement(Identifier* label,
271
+ ContinueStatement* NewContinueStatement(core::Maybe<Identifier> label,
278
272
  IterationStatement** target,
279
273
  std::size_t begin,
280
274
  std::size_t end) {
@@ -283,7 +277,7 @@ class AstFactory : public core::Space<2> {
283
277
  return stmt;
284
278
  }
285
279
 
286
- BreakStatement* NewBreakStatement(Identifier* label,
280
+ BreakStatement* NewBreakStatement(core::Maybe<Identifier> label,
287
281
  BreakableStatement** target,
288
282
  std::size_t begin,
289
283
  std::size_t end) {
@@ -292,10 +286,9 @@ class AstFactory : public core::Space<2> {
292
286
  return stmt;
293
287
  }
294
288
 
295
- ReturnStatement* NewReturnStatement(Expression* expr,
289
+ ReturnStatement* NewReturnStatement(core::Maybe<Expression> expr,
296
290
  std::size_t begin,
297
291
  std::size_t end) {
298
- assert(expr);
299
292
  ReturnStatement* stmt = new (this) ReturnStatement(expr);
300
293
  stmt->Location(begin, end);
301
294
  return stmt;
@@ -317,7 +310,7 @@ class AstFactory : public core::Space<2> {
317
310
  }
318
311
 
319
312
  CaseClause* NewCaseClause(bool is_default,
320
- Expression* expr, Statements* body,
313
+ core::Maybe<Expression> expr, Statements* body,
321
314
  std::size_t begin,
322
315
  std::size_t end) {
323
316
  CaseClause* clause = new (this) CaseClause(is_default, expr, body);
@@ -336,9 +329,9 @@ class AstFactory : public core::Space<2> {
336
329
  }
337
330
 
338
331
  TryStatement* NewTryStatement(Block* try_block,
339
- Identifier* catch_name,
340
- Block* catch_block,
341
- Block* finally_block,
332
+ core::Maybe<Identifier> catch_name,
333
+ core::Maybe<Block> catch_block,
334
+ core::Maybe<Block> finally_block,
342
335
  std::size_t begin) {
343
336
  TryStatement* stmt = new (this) TryStatement(try_block,
344
337
  catch_name,
@@ -346,7 +339,7 @@ class AstFactory : public core::Space<2> {
346
339
  finally_block);
347
340
  assert(catch_block || finally_block);
348
341
  const std::size_t end = (finally_block) ?
349
- finally_block->end_position() : catch_block->end_position();
342
+ (*finally_block).end_position() : (*catch_block).end_position();
350
343
  stmt->Location(begin, end);
351
344
  return stmt;
352
345
  }
@@ -422,9 +415,6 @@ class AstFactory : public core::Space<2> {
422
415
  access->Location(expr->begin_position(), ident->end_position());
423
416
  return access;
424
417
  }
425
-
426
- private:
427
- Undefined* undefined_instance_;
428
418
  };
429
419
 
430
420
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: iv-phonic
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.5
5
+ version: 0.1.6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Constellation
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-02-09 00:00:00 +09:00
13
+ date: 2011-02-10 00:00:00 +09:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -73,6 +73,7 @@ files:
73
73
  - ext/include/iv/keyword.h
74
74
  - ext/include/iv/lexer.h
75
75
  - ext/include/iv/location.h
76
+ - ext/include/iv/maybe.h
76
77
  - ext/include/iv/mt19937.h
77
78
  - ext/include/iv/noncopyable.h
78
79
  - ext/include/iv/none.h