Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tree: e4345fc96b
Fetching contributors…

Cannot retrieve contributors at this time

93 lines (81 sloc) 2.2 kb
/~
Expression calculator written in JS/CC (nodejs)
~/
/~
--- These are the token definitions ---
~/
! ' |\t' ; /~ Characters to be ignored... ~/
'\(' /~ Non-associative tokens ~/
'\)'
'[0-9]+\.?[0-9]*' NUMBER [* %match = parseFloat( %match ); *]
'0x[0-9A-Fa-f]+' Hex [* %match = parseFloat( %match ); *]
'PI'
'SQRT'
'ABS'
'FLOOR'
'CEIL'
'RAND'
'ROUND'
'MAX'
'MIN'
;
< '\+' /~ Left-associative tokens, lowest precedence ~/
'\-'
'^';
< '\*' /~ Left-associative tokens, highest precedence ~/
'/';
##
/~
--- And here's the grammar specification ---
~/
p: e [* console.log( %1 ); *]
;
/~
Don't confuse with the tokens:
Here, we use the unescaped values because these are not
interpretered as regular expressions at this position!
~/
e: e '+' e [* %% = %1 + %3; *]
| e '-' e [* %% = %1 - %3; *]
| e '^' e [* %% = Math.pow(%1, %3); *]
| e '*' e [* %% = %1 * %3; *]
| e '/' e [* %% = %1 / %3; *]
| '-' e &'*' [* %% = %2 * -1; *]
| PI [* %% = Math.PI; *]
| MAX e e [* %% = Math.max(%2,%3); *]
| MIN e e [* %% = Math.min(%2,%3); *]
| SQRT e [* %% = Math.sqrt(%2); *]
| FLOOR e [* %% = Math.floor(%2); *]
| CEIL e [* %% = Math.ceil(%2); *]
| ABS e [* %% = Math.abs(%2); *]
| ROUND e [* %% = Math.round(%2); *]
| RAND [* %% = Math.random(); *]
| '(' e ')' [* %% = %2; *]
| NUMBER
| Hex
;
/~
This is the parser entry point; Because this entry point could be
very individual, the compiler programmer has to decide which way
he want to read the source, parse it and report the errors, if
there are any.
~/
[*
if(process.argv.length > 2) {
var str = process.argv;
delete str[0];
delete str[1];
str = str.join(" ");
var error_cnt = 0;
var error_off = new Array();
var error_la = new Array();
if((error_cnt = __##PREFIX##parse(str, error_off, error_la)) > 0) {
for(var i = 0; i < error_cnt; i++ ) {
console.log( "Parse error near >" + str.substr( error_off[i], 30 ) + "<, expecting \"" + error_la[i].join() + "\"" );
}
}
}
else {
console.log( 'usage: proram <expr>' );
}
*]
Jump to Line
Something went wrong with that request. Please try again.