* @desc Parses the abstract syntax tree for *Variable Declaration* * @param {Object} varDecNode - An ast Node * @param {Array} retArr - return array string * @returns {Array} the append retArr
(varDecNode, retArr)
| 814 | * @returns {Array} the append retArr |
| 815 | */ |
| 816 | astVariableDeclaration(varDecNode, retArr) { |
| 817 | const declarations = varDecNode.declarations; |
| 818 | if (!declarations || !declarations[0] || !declarations[0].init) { |
| 819 | throw this.astErrorOutput('Unexpected expression', varDecNode); |
| 820 | } |
| 821 | const result = []; |
| 822 | let lastType = null; |
| 823 | const declarationSets = []; |
| 824 | let declarationSet = []; |
| 825 | for (let i = 0; i < declarations.length; i++) { |
| 826 | const declaration = declarations[i]; |
| 827 | const init = declaration.init; |
| 828 | const info = this.getDeclaration(declaration.id); |
| 829 | const actualType = this.getType(declaration.init); |
| 830 | let type = actualType; |
| 831 | if (type === 'LiteralInteger') { |
| 832 | if (info.suggestedType === 'Integer') { |
| 833 | type = 'Integer'; |
| 834 | } else { |
| 835 | // We had the choice to go either float or int, choosing float |
| 836 | type = 'Number'; |
| 837 | } |
| 838 | } |
| 839 | const markupType = typeMap[type]; |
| 840 | if (!markupType) { |
| 841 | throw this.astErrorOutput(`Markup type ${ type } not handled`, varDecNode); |
| 842 | } |
| 843 | const declarationResult = []; |
| 844 | if (actualType === 'Integer' && type === 'Integer') { |
| 845 | // Since we are assigning to a float, ensure valueType is reset to that |
| 846 | info.valueType = 'Number'; |
| 847 | if (i === 0 || lastType === null) { |
| 848 | declarationResult.push('float '); |
| 849 | } else if (type !== lastType) { |
| 850 | throw new Error('Unhandled declaration'); |
| 851 | } |
| 852 | lastType = type; |
| 853 | declarationResult.push(`user_${utils.sanitizeName(declaration.id.name)}=`); |
| 854 | declarationResult.push('float('); |
| 855 | this.astGeneric(init, declarationResult); |
| 856 | declarationResult.push(')'); |
| 857 | } else { |
| 858 | // Since we are assigning to a float, ensure valueType is reset to that |
| 859 | info.valueType = type; |
| 860 | if (i === 0 || lastType === null) { |
| 861 | declarationResult.push(`${markupType} `); |
| 862 | } else if (type !== lastType) { |
| 863 | declarationSets.push(declarationSet.join(',')); |
| 864 | declarationSet = []; |
| 865 | declarationResult.push(`${markupType} `); |
| 866 | } |
| 867 | lastType = type; |
| 868 | declarationResult.push(`user_${utils.sanitizeName(declaration.id.name)}=`); |
| 869 | if (actualType === 'Number' && type === 'Integer') { |
| 870 | if (init.left && init.left.type === 'Literal') { |
| 871 | this.astGeneric(init, declarationResult); |
| 872 | } else { |
| 873 | declarationResult.push('int('); |
no test coverage detected