All files ast-build.js

100% Statements 19/19
33.33% Branches 1/3
100% Functions 14/14
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243                      1x               34x                                   103x                             132x                               186x                       27x                         49x                             49x                           55x                           73x                                         18x                                                     4x                               7x                             14x 14x 16x         16x   14x     1x                              
/**
 * Universidad de La Laguna
 * Escuela Superior de Ingeniería y Tecnología
 * Grado en Ingeniería Informática
 * Procesadores de Lenguajes
 *
 * @author Juan Rodríguez Suárez
 * @since Mar 04 2024
 * @desc Contains all the functions to build the AST.
 */
 
const { $ } = require('./utils.js')
 
/**
 * @brief Builds the root of the AST
 * @param {object} child - The child node
 * @returns {object} The root node
 */
function buildRoot(child) {
  return {
    type: 'Program',
    body: [
      {
        type: 'ExpressionStatement',
        expression: child,
      }
    ],
    sourceType: 'script'
  };
}
 
/**
 * @brief Builds a literal node
 * @param {string} value - The value
 * @returns {object} The literal node
 */
function buildLiteral(value) {
  return {
    type: 'Literal',
    value,
    raw: `\"${value}\"`
  }
}
 
/**
 * @brief Builds a call expression node
 * @param {string} functionName - The function name
 * @param {Array} args - The arguments
 * @param {boolean} reservedWord - The reserved word
 * @returns {object} The call expression node
 */
function buildCallExpression(functionName, args, reservedWord = false) {
  return {
    type: 'CallExpression',
    callee: {
      type: 'Identifier',
      name: reservedWord ? functionName : $(functionName)
    },
    arguments: args
  }
}
 
/**
 * @brief Builds an identifier node
 * @param {string} name - The name
 * @returns {object} The identifier node
 */
function buildIdentifier(name) {
  return {
    type: 'Identifier',
    name: name,
  };
}
 
/**
 * @brief Builds a variable declaration node
 * @param {Array} declarations - The declarations
 * @returns {object} The variable declaration node
 */
function buildVariableDeclaration(declarations) {
  return {
    type: 'VariableDeclaration',
    declarations,
    kind: 'let',
  };
}
 
/**
 * @brief Builds a variable declarator node
 * @param {object} id - The id
 * @returns {object} The variable declarator node
 */
function buildVariableDeclarator(id) {
  return {
    type: 'VariableDeclarator',
    id: id,
    init: null,
  };
}
 
/**
 * @brief Builds an assignment expression node
 * @param {string} name - The name
 * @param {string} operator - The operator
 * @param {object} right - The right node
 * @returns {object} The assignment expression node
 */
function buildAssignmentExpression(name, operator, right) {
  return {
    type: 'AssignmentExpression',
    operator,
    left: buildIdentifier(name),
    right: right,
  };
}
 
/**
 * @brief Builds a sequence expression node
 * @param {Array} expressions - The expressions
 * @returns {object} The sequence expression node
 */
function buildSequenceExpression(expressions) {
  return {
    type: 'SequenceExpression',
    expressions,
  };
}
 
/**
 * @brief Builds a member expression node
 * @param {object} object - The object
 * @param {string} operator - The operator
 * @param {Array} args - The arguments
 * @returns {object} The member expression node
 */
function buildMemberExpression(object, operator, args) {
  return {
    type: 'CallExpression',
    callee: {
      type: 'MemberExpression',
      object,
      property: {
        type: 'Identifier',
        name: operator
      }
    },
    arguments: args
  };
}
 
/**
 * @brief Builds a function declaration node
 * @param {Array} params - The parameters
 * @param {object} expression - The return expression
 * @returns {object} The function declaration node
 */
function buildFunctionExpression(params, expression) {
  return {
    type: 'FunctionExpression',
    id: null,
    params,
    body: {
      type: 'BlockStatement',
      body: [
        {
          type: 'ReturnStatement',
          argument: expression
        }
      ]
    },
    generator: false,
    expression: false,
    async: false
  };
}
 
/**
 * @brief Builds a logical expression node
 * @param {object} left - The left node
 * @param {string} operator - The operator
 * @param {object} right - The right node
 * @returns {object} The logical expression node
 */
function buildLogicalExpression(left, operator, right) {
  return {
    type: 'LogicalExpression',
    operator,
    left,
    right
  }
}
 
/**
 * @brief Builds a unary expression node
 * @param {string} operator - The operator
 * @param {object} argument - The argument
 * @param {boolean} prefix - The prefix
 * @returns {object} The unary expression node
 */
function buildUnaryExpression(operator, argument, prefix) {
  return {
    type: 'UnaryExpression',
    operator,
    argument,
    prefix
  }
}
 
/**
 * @brief Builds a sequence of calls to an identifier
 * @param {string} id - The identifier
 * @param {Array} calls - The calls
 * @returns {object} The sequence of calls
 */
function buildIdCalls(id, calls) {
  let node = id;
  calls.forEach(ast => {
    let parent = {
      type: 'CallExpression',
      callee: node,
      arguments: ast
    };
    node = parent;
  });
  return node;
}
 
module.exports = {
  buildRoot,
  buildLiteral,
  buildCallExpression,
  buildIdentifier,
  buildVariableDeclaration,
  buildVariableDeclarator,
  buildAssignmentExpression,
  buildSequenceExpression,
  buildMemberExpression,
  buildFunctionExpression,
  buildLogicalExpression,
  buildUnaryExpression,
  buildIdCalls
}