Skip to content

Commit 30d4544

Browse files
author
Dnld
committed
refactors hadnling of classes and structs
1 parent 1754882 commit 30d4544

File tree

2 files changed

+58
-55
lines changed

2 files changed

+58
-55
lines changed

transpiler/lexer/lexer.js

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -317,63 +317,17 @@ module.exports = function(code) {
317317
continue;
318318
}
319319

320-
/////////////////////////////////////////////
321-
// //
322-
// classes and structures handling //
323-
// //
324-
///////////////////////////////////////////////////////////////////////////
325-
326320
// handles inheritance operators
327321
if (tokens.length > 2 && tokens[tokens.length - 2].value === ':' &&
328322
CLASS_NAMES[lastToken.value] && CLASS_NAMES[tokens[tokens.length - 3].value]) {
329323
tokens[tokens.length - 2].type = 'INHERITANCE_OPERATOR';
330324
}
331-
if (insideClass.length && insideClass[insideClass.length - 1].curly === 0 &&
332-
chunk === '{') {
333-
lexerFunctions.checkFor('CLASS_DEFINITION', chunk, tokens);
334-
insideClass[insideClass.length - 1].curly++;
335-
advanceAndClear(1);
336-
continue;
337-
}
338-
if (insideClass.length && insideClass[insideClass.length - 1].curly === 1 &&
339-
chunk === '}') {
340-
lexerFunctions.checkFor('CLASS_DEFINITION', chunk, tokens);
341-
insideClass.pop();
342-
advanceAndClear(1);
343-
lexerFunctions.handleEndOfFile(nextCol, tokens);
344-
continue;
345-
}
346-
if (insideStruct.length && insideStruct[insideStruct.length - 1].curly === 0 &&
347-
chunk === '{') {
348-
lexerFunctions.checkFor('STRUCT_DEFINITION', chunk, tokens);
349-
insideStruct[insideStruct.length - 1].curly++;
350-
advanceAndClear(1);
351-
continue;
352-
}
353-
if (insideStruct.length && insideStruct[insideStruct.length - 1].curly === 1 &&
354-
chunk === '}') {
355-
lexerFunctions.checkFor('STRUCT_DEFINITION', chunk, tokens);
356-
insideStruct.pop();
357-
advanceAndClear(1);
358-
lexerFunctions.handleEndOfFile(nextCol, tokens);
359-
continue;
360-
}
361-
if (tokens.length && (CLASS_NAMES[lastToken.value] ||
362-
STRUCT_NAMES[lastToken.value]) && chunk === '(') {
363-
lexerFunctions.checkFor('INITIALIZATION', chunk, tokens)
364-
var temp = {};
365-
temp.status = true;
366-
temp.parens = 1;
367-
insideInitialization.push(temp);
368-
advanceAndClear(1);
369-
continue;
370-
}
371-
if (chunk === ')' && insideInitialization.length &&
372-
insideInitialization[insideInitialization.length - 1].parens === 1) {
373-
lexerFunctions.checkFor('INITIALIZATION', chunk, tokens);
374-
insideInitialization.pop();
325+
326+
// handles classes and structs
327+
if (lexerFunctions.handleClassOrStruct(insideClass, insideStruct,
328+
insideInitialization, chunk, tokens, lastToken,
329+
nextCol, CLASS_NAMES, STRUCT_NAMES)) {
375330
advanceAndClear(1);
376-
lexerFunctions.handleEndOfFile(nextCol, tokens);
377331
continue;
378332
}
379333

@@ -396,8 +350,6 @@ module.exports = function(code) {
396350
continue;
397351
}
398352

399-
///////////////////////////////////////////////////////////////////////////
400-
401353
// main evaluation block
402354
if (!insideString.status && !insideNumber.status &&
403355
lexerFunctions.checkForEvaluationPoint(currCol, nextCol)) {

transpiler/lexer/lexerFunctions.js

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,59 @@ module.exports = {
250250
return true;
251251
}
252252
},
253-
253+
254+
255+
// handles classes and structures
256+
handleClassOrStruct: function(insideClass, insideStruct, insideInitialization,
257+
chunk, tokens, lastToken, nextCol, CLASS_NAMES,
258+
STRUCT_NAMES) {
259+
if (insideClass.length && insideClass[insideClass.length - 1].curly === 0 &&
260+
chunk === '{') {
261+
module.exports.checkFor('CLASS_DEFINITION', chunk, tokens);
262+
insideClass[insideClass.length - 1].curly++;
263+
return true;
264+
}
265+
if (insideClass.length && insideClass[insideClass.length - 1].curly === 1 &&
266+
chunk === '}') {
267+
module.exports.checkFor('CLASS_DEFINITION', chunk, tokens);
268+
insideClass.pop();
269+
module.exports.handleEndOfFile(nextCol, tokens);
270+
return true;
271+
}
272+
if (insideStruct.length && insideStruct[insideStruct.length - 1].curly === 0 &&
273+
chunk === '{') {
274+
module.exports.checkFor('STRUCT_DEFINITION', chunk, tokens);
275+
insideStruct[insideStruct.length - 1].curly++;
276+
return true;
277+
}
278+
if (insideStruct.length && insideStruct[insideStruct.length - 1].curly === 1 &&
279+
chunk === '}') {
280+
module.exports.checkFor('STRUCT_DEFINITION', chunk, tokens);
281+
insideStruct.pop();
282+
module.exports.handleEndOfFile(nextCol, tokens);
283+
return true;
284+
}
285+
if (tokens.length && (CLASS_NAMES[lastToken.value] ||
286+
STRUCT_NAMES[lastToken.value]) && chunk === '(') {
287+
module.exports.checkFor('INITIALIZATION', chunk, tokens)
288+
var temp = {};
289+
temp.status = true;
290+
temp.parens = 1;
291+
insideInitialization.push(temp);
292+
return true;
293+
}
294+
if (chunk === ')' && insideInitialization.length &&
295+
insideInitialization[insideInitialization.length - 1].parens === 1) {
296+
module.exports.checkFor('INITIALIZATION', chunk, tokens);
297+
insideInitialization.pop();
298+
module.exports.handleEndOfFile(nextCol, tokens);
299+
return true;
300+
}
301+
302+
return false;
303+
304+
},
305+
254306
checkForTupleStart: function(insideTuple, chunk, tokens, lastToken,
255307
currCol, nextCol, nextNextCol, cb) {
256308
if (!insideTuple.status && currCol === '(' && (lastToken.value === '=' ||
@@ -337,7 +389,6 @@ module.exports = {
337389
insideStruct.push(temp);
338390
STRUCT_NAMES[chunk] = true;
339391
}
340-
341392
return true;
342393
}
343394
return false;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy