Skip to content
This repository has been archived by the owner on May 27, 2023. It is now read-only.
aantthony edited this page Feb 1, 2013 · 3 revisions

This is now outdated. It now uses Jison, which is a Bison-like parser generator for javascript. The grammar is stored in calculator.jison.

First, some text-replacements are run on the string, which replaces some latex commands with a more easily parable alternative. This changes "\frac{a}{b}" to "(a)/(b)", which will then be parsed by the main parser.

By default the string parsing supports these operators in order of increasing precedence (L=left associative, R=right associative, unspecified associativity means that it is associative):

From 04.default_language.js

var language = new Language([
	[';'],			/*L / R makes no difference???!??!? */
	[','],
	[['=', '+=', '-=', '*=', '/=', '%=', '&=', '^=', '|='],R],
	[['?',':'],R,2],
	[['∨']],
	[['&&']],
	[['|']],
	[['??????']],//XOR
	[['&']],
	[['==', '≠', '!==', '===']],
	[['<', '<=', '>', '>='],L],
	[['>>', '<<']],
	['±', R, 2],
	[['+'], true],
	[['-'], L],
	[['∫', '∑'], R, 1],
	[['*', '%'], R],
	[['@+', '@-', '@±'], R, 1], //unary plus/minus
	[['¬'], L, 1],
	['default', R, 2], //I changed this to R for 5sin(t)
	['∘', R, 2],
	[['/']],
	[['^']],//e**x
	['!', L, 1],
	[['~'], R, 1], //bitwise negation
	[['++', '++', '.', '->'],L,1],
	[['::']],
	[['_'], L, 2],
	['var', R, 1],
	['break', R, 0],
	['throw', R, 1],
	['\'', L, 1],
	['\u221A', R, 1], // Sqrt
	['#', R, 1]	/*anonymous function*/
]);

All parentheses ("[{(") are treated in the same way.

And then the expression is returned, but with the extra property 'unbound', which is an object containing all variable names which were not found in the Context specified. This is important because M('x').differentiate(M('x')) is zero and not one, because they are in fact different 'x' symbols.