~didrocks/+junk/face-detection-15.04

« back to all changes in this revision

Viewing changes to facedetection/www/bower_components/hydrolysis/src/ast-utils/ast-value.ts

  • Committer: Didier Roche
  • Date: 2016-05-10 23:09:11 UTC
  • Revision ID: didier.roche@canonical.com-20160510230911-c7xr490zrj3yrzxd
New version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @license
 
3
 * Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
 
4
 * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
 
5
 * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
 
6
 * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
 
7
 * Code distributed by Google as part of the polymer project is also
 
8
 * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
 
9
 */
 
10
 
 
11
'use strict';
 
12
 
 
13
import * as estree from 'estree';
 
14
import {LiteralValue} from './descriptors';
 
15
 
 
16
// useful tool to visualize AST: http://esprima.org/demo/parse.html
 
17
 
 
18
/**
 
19
 * converts literal: {"type": "Literal", "value": 5,  "raw": "5" }
 
20
 * to string
 
21
 */
 
22
function literalToValue(literal: estree.Literal): LiteralValue {
 
23
  return literal.value;
 
24
}
 
25
 
 
26
/**
 
27
 * converts unary to string
 
28
 */
 
29
function unaryToValue(unary: estree.UnaryExpression):string {
 
30
  var argValue = expressionToValue(unary.argument);
 
31
  if (argValue === undefined)
 
32
    return;
 
33
  return unary.operator + argValue;
 
34
}
 
35
 
 
36
/**
 
37
 * converts identifier to its value
 
38
 * identifier { "type": "Identifier", "name": "Number }
 
39
 */
 
40
function identifierToValue(identifier: estree.Identifier):string {
 
41
  return identifier.name;
 
42
}
 
43
 
 
44
/**
 
45
 * Function is a block statement.
 
46
 */
 
47
function functionDeclarationToValue(
 
48
    fn: estree.FunctionDeclaration): LiteralValue {
 
49
  if (fn.body.type == "BlockStatement")
 
50
    return blockStatementToValue(fn.body);
 
51
}
 
52
 
 
53
function functionExpressionToValue(
 
54
    fn: estree.FunctionExpression): LiteralValue {
 
55
  if (fn.body.type == "BlockStatement")
 
56
    return blockStatementToValue(fn.body);
 
57
}
 
58
/**
 
59
 * Block statement: find last return statement, and return its value
 
60
 */
 
61
function blockStatementToValue(
 
62
    block: estree.BlockStatement): LiteralValue {
 
63
  for (var i=block.body.length - 1; i>= 0; i--) {
 
64
    if (block.body[i].type === "ReturnStatement")
 
65
      return returnStatementToValue(<estree.ReturnStatement>block.body[i]);
 
66
  }
 
67
}
 
68
 
 
69
/**
 
70
 * Evaluates return's argument
 
71
 */
 
72
function returnStatementToValue(ret: estree.ReturnStatement): LiteralValue {
 
73
  return expressionToValue(ret.argument);
 
74
}
 
75
 
 
76
/**
 
77
 * Enclose containing values in []
 
78
 */
 
79
function arrayExpressionToValue(arry: estree.ArrayExpression): string {
 
80
  var value = '[';
 
81
  for (var i=0; i<arry.elements.length; i++) {
 
82
    var v = expressionToValue(arry.elements[i]);
 
83
    if (v === undefined)
 
84
      continue;
 
85
    if (i !== 0)
 
86
      value += ', ';
 
87
    value += v;
 
88
  }
 
89
  value += ']';
 
90
  return value;
 
91
}
 
92
 
 
93
/**
 
94
 * Make it look like an object
 
95
 */
 
96
function objectExpressionToValue(obj: estree.ObjectExpression): string {
 
97
  var value = '{';
 
98
  for (var i=0; i<obj.properties.length; i++) {
 
99
    var k = expressionToValue(obj.properties[i].key);
 
100
    var v = expressionToValue(obj.properties[i].value);
 
101
    if (v === undefined)
 
102
      continue;
 
103
    if (i !== 0)
 
104
      value += ', ';
 
105
    value += '"' + k + '": ' + v;
 
106
  }
 
107
  value += '}';
 
108
  return value;
 
109
}
 
110
 
 
111
/**
 
112
 * BinaryExpressions are of the form "literal" + "literal"
 
113
 */
 
114
function binaryExpressionToValue(
 
115
    member: estree.BinaryExpression): number|string {
 
116
  if (member.operator == "+") {
 
117
    // We need to cast to `any` here because, while it's usually not the right
 
118
    // thing to do to use '+' on two values of a mix of types because it's
 
119
    // unpredictable, that is what the original code we're evaluating does.
 
120
    return <any>expressionToValue(member.left) + expressionToValue(member.right);
 
121
  }
 
122
  return;
 
123
}
 
124
 
 
125
/**
 
126
 * MemberExpression references a variable with name
 
127
 */
 
128
function memberExpressionToValue(member: estree.MemberExpression): string {
 
129
  return expressionToValue(member.object) + "." + expressionToValue(member.property);
 
130
}
 
131
 
 
132
/**
 
133
 * Tries to get the value of an expression. Returns undefined on failure.
 
134
 */
 
135
export function expressionToValue(valueExpression: estree.Node): LiteralValue {
 
136
  switch(valueExpression.type) {
 
137
    case 'Literal':
 
138
      return literalToValue(<estree.Literal>valueExpression);
 
139
    case 'UnaryExpression':
 
140
      return unaryToValue(<estree.UnaryExpression>valueExpression);
 
141
    case 'Identifier':
 
142
      return identifierToValue(<estree.Identifier>valueExpression);
 
143
    case 'FunctionDeclaration':
 
144
      return functionDeclarationToValue(<estree.FunctionDeclaration>valueExpression);
 
145
    case 'FunctionExpression':
 
146
      return functionExpressionToValue(<estree.FunctionExpression>valueExpression);
 
147
    case 'ArrayExpression':
 
148
      return arrayExpressionToValue(<estree.ArrayExpression>valueExpression);
 
149
    case 'ObjectExpression':
 
150
      return objectExpressionToValue(<estree.ObjectExpression>valueExpression);
 
151
    case 'Identifier':
 
152
      return identifierToValue(<estree.Identifier>valueExpression);
 
153
    case 'MemberExpression':
 
154
      return memberExpressionToValue(<estree.MemberExpression>valueExpression);
 
155
    case 'BinaryExpression':
 
156
      return binaryExpressionToValue(<estree.BinaryExpression>valueExpression);
 
157
    default:
 
158
      return;
 
159
  }
 
160
}
 
161
 
 
162
export var CANT_CONVERT = 'UNKNOWN';