Last active
June 12, 2017 12:13
-
-
Save mehmetakifakkus/33874c311100512ceeea2d646c1ae028 to your computer and use it in GitHub Desktop.
If Else Parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
/* | |
* Simple Selection Grammar | |
* ========================== | |
* Simple IF ELSE Statement | |
*/ | |
{ | |
var time = 1; | |
function drawLine(line){ | |
setTimeout(function(){ | |
highlightLine(line.lineNumber); | |
insertTextAtCursor(line.evaluated, line.lineNumber); | |
console.log('line '+line.lineNumber +' is getting processed, result is: '+line.evaluated); | |
}, 1000 * time) | |
time++; | |
} | |
} | |
start | |
= statement* | |
statement | |
= if_statement | |
/ item:block_item {drawLine(item); return item;} | |
if_statement | |
= "if(" _ exp:expression_statement _ ")" _ c1:(compound_statement / (nl block_item)) _ nl _ | |
c2: ("else" _ (compound_statement / (nl block_item)))? | |
{ | |
drawLine(exp); | |
if(exp.evaluated){ | |
var lines = (c1[0] == null) ? c1[1] : c1; | |
if(lines.constructor.name == "Array") | |
for(var i=0; i < lines.length; i++) | |
drawLine(lines[i]) | |
else | |
drawLine(lines) | |
return lines; | |
} | |
else if(c2){ | |
var lines = (c2[2][0] == null) ? c2[2][1] : c2[2]; | |
if(lines.constructor.name == "Array") | |
for(var i=0; i < lines.length; i++) | |
drawLine(lines[i]) | |
else | |
drawLine(lines) | |
return lines; | |
} | |
return 'false returned if statement'; | |
} | |
compound_statement | |
= nl _'{' b:block_item_list '}' nl { | |
var loc = location(); | |
//console.log('block started at line '+loc.start.line); | |
return b | |
} | |
block_item_list | |
= bl:(nl block_item nl)* {var bl2 = bl.map(function(value,index) { return value[1]; }); | |
return bl2; | |
} | |
block_item | |
= print_statement | |
/ expression_statement | |
print_statement = "print" _ n:name _ comment? nl | |
{ return eval(n); } | |
expression_statement = _ t:term (expression_statement)* _ nl{ | |
var temp = eval(t) | |
if(temp.toString() == "[object Object]") | |
return t; | |
else | |
return {'evaluated': eval(t), lineNumber: location().start.line}; // evaluate it, then return it | |
} | |
term = f1:factor f2:(_ ("*" / "+" / "/" / "-" / operator) _ factor)* | |
{ | |
var text = f1+' '; | |
if(f2[0]) | |
text += f2[0][1] + ' ' + f2[0][3]; | |
return text; | |
} | |
factor = "(" expression_statement ")" | |
/ name | |
/ integer | |
///// Name = Variable | |
name = l:letter i:integer {return l+i} | |
/ l:letter {return l;} | |
letter "letter" | |
= [a-z]+ {return text()} | |
integer "integer" | |
= [0-9]+ { return parseInt(text(), 10); } | |
_ "whitespace" | |
= [ \t\r]* {return null;} | |
nl "newline" | |
= [\n]* {return null;} | |
comment | |
= "//" _ [a-zA-Z0-9 ]* _ nl | |
operator | |
= operator_text / operator_symbol | |
operator_text | |
= "and" { return "&&"; } | |
/ "or" { return "||"; } | |
/ "smaller" { return "<="; } | |
/ "smaller or equal" { return "<="; } | |
/ "greater" { return ">="; } | |
/ "greater or equal" { return ">="; } | |
/ "equal" { return "=="; } | |
/ "not equal" { return "!="; } | |
operator_symbol | |
= "&&" { return text(); } | |
/ "||" { return text(); } | |
/ "<=" { return text(); } | |
/ "<" { return text(); } | |
/ ">=" { return text(); } | |
/ ">" { return text(); } | |
/ "==" { return text(); } | |
/ "!=" { return text(); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment