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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | 29x 29x 29x 442x 29x 372x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 27x 27x 27x 27x 27x 27x 29x 106x 106x 106x 106x 106x 106x 106x 29x 27x 27x 27x 27x 27x 27x 27x 29x 37x 37x 37x 37x 37x 29x 23x 6x 37x 37x 29x 29x 29x 43x 43x 43x 29x 13x 15x 15x 15x 15x 15x 15x 13x 2x 13x 13x 13x 13x 29x 29x 29x 29x 29x 29x 35x 35x 35x 35x 35x 35x 5x 5x 35x 35x 35x 29x 8x 8x 8x 8x 8x 8x 6x 6x 8x 29x 36x 36x 36x 29x 59x 59x 59x 29x 29x 29x |
import { AST } from './ast';
/**
* Using Douglas Crockford paper on Vaughan Pratt "Top Down Operator Precedence"
* For more info about this look here
* https://crockford.com/javascript/tdop/tdop.html (added to docs folder so I dont loose it)
* Not everything is used
*
* This function adds symbols to our AST
*
*/
export function addSymbols(astInstance: AST) {
const ast = astInstance;
/**
* Symbol id and an optional binding power that defaults to 0 and returns a symbol object for that id.
* If the symbol already exists in the symbol_table, the function returns that symbol object.
* Otherwise, it makes a new symbol object that inherits from the symbolTemplate, stores it in the symbol container, and returns it.
* A symbol object initially contains an id, a value, a left binding power, and the stuff it inherits from the symbolTemplate.
*
*/
ast.symbol('(literal)').nud = function () {
return this;
};
ast.symbol('(variable)').nud = function () {
return this;
};
ast.symbol('(function)').nud = function () {
return this;
};
ast.symbol('(end)');
ast.symbol('(name)');
ast.symbol(':');
ast.symbol(';');
ast.symbol(')');
ast.symbol(']');
ast.symbol('}');
ast.symbol(',');
/**
* The infix function takes an id, a binding power, and an optional led function.
* If a led function is not provided, the infix function supplies a default led that is useful in most cases.
*
*/
ast.infix('+', 50);
ast.infix('-', 50);
ast.infix('*', 60);
ast.infix('%', 60);
ast.infix('/', 60);
ast.infix('===', 40);
ast.infix('!==', 40);
ast.infix('==', 40);
ast.infix('!=', 40);
ast.infix('<', 40);
ast.infix('<=', 40);
ast.infix('>', 40);
ast.infix('^', 70);
ast.infix('>=', 40);
ast.infix('?', 20, function (left: any) {
this.first = left;
this.second = ast.expression(0);
ast.advance(':');
this.third = ast.expression(0);
this.arity = 'ternary';
return this;
});
ast.infix('.', 80, function (left: any) {
this.first = left;
if (ast.currentToken.arity !== 'variable') {
ast.currentToken.error('Expected a property name.');
}
ast.currentToken.arity = 'variable';
this.second = ast.currentToken;
this.arity = 'binary';
this.isObject = true;
ast.advance();
return this;
});
ast.infix('[', 80, function (left: any) {
this.first = left;
this.second = ast.expression(0);
this.arity = 'binary';
this.isObject = true;
this.contextReset = true;
ast.advance(']');
return this;
});
ast.infix('(', 80, function (left: any) {
if (!left) {
return ast.expression(0);
}
const a: any[] = [];
this.arity = 'binary';
this.isFunction = true;
this.first = left;
this.second = a;
if (ast.currentToken.id !== ')') {
while (true) {
a.push(ast.expression(0));
if (ast.currentToken.id !== ',') {
break;
}
ast.advance(',');
}
}
ast.advance(')');
return this;
});
/**
* Prefix operators are right associative.
* A prefix does not have a left binding power because it does not bind to the left.
* Prefix operators can also sometimes be reserved words.
*
*/
ast.prefix('-');
ast.prefix('!');
// ast.prefix('typeof'); // TODO: I dont have anything for this in my tokenizer
ast.prefix('(', function () {
const e = ast.expression(0);
// TODO: check this one... will this even happend ?
if (ast.currentToken.value === '|') {
ast.valueConverter = ast.expression(0);
}
if (ast.currentToken.value === '&') {
ast.behavior = ast.expression(0);
} else {
ast.advance(')');
}
return e;
});
ast.prefix('{', function () {
const a = [];
if (ast.currentToken.value !== '}') {
while (true) {
const n = ast.currentToken;
ast.advance();
ast.advance(':');
const v = ast.expression(0);
v.key = n.value;
a.push(v);
if (ast.currentToken.value !== ',') {
break;
}
ast.advance(',');
}
}
ast.advance('}');
this.first = a;
this.arity = 'unary';
return this;
});
/* ast.stmt('{', function () {
const x = ast.statements();
ast.advance('}');
return x;
}); */
ast.assignment('=');
ast.assignment('+=');
ast.assignment('-=');
ast.assignment('*=');
ast.assignment('/=');
/**
* valueConverter
*
*/
ast.prefix('|', function () {
this.arity = 'valueConverter';
const value = ast.expression(0);
this.value = value.value;
const a: any = [];
this.args = a;
while (true) {
if (ast.currentToken.id !== ':') {
break;
}
ast.advance(',');
a.push(ast.expression(0));
}
// use last statement as argument
this.args.unshift(ast.statementsArray.pop());
ast.currentStatement = this;
return this;
});
/**
* behavior
*
*/
ast.prefix('&', function () {
this.arity = 'behavior';
const value = ast.expression(0);
this.value = value.value;
const a: any = [];
this.args = a;
while (true) {
if (ast.currentToken.id !== ':') {
break;
}
ast.advance(',');
a.push(ast.expression(0));
}
// do I want to add last argument ?
// this.args.push(ast.statementsArray);
return this;
});
/**
* expression
*
*/
ast.prefix('${', function () {
this.e = ast.expression(0);
ast.advance('}');
return this.e;
});
/**
* expression, alternative to ${
*
*/
ast.prefix('@{', function () {
const e = ast.expression(0);
ast.advance('}');
return e;
});
/**
* Those infix operators are left associative.
* We can also make right associative operators, such as short-circuiting logical operators, by reducing the right binding power.
*
*/
ast.infixr('&&', 30);
ast.infixr('||', 30);
/**
* expressions/part before expression is split with ';' operator
*
*/
ast.symbol(';').nud = function (): null {
return null;
};
}
|