[seed] [calculator-mpfr] Stole prefix parser from ease to make a calculator for Matt to fill in with mpfr f



commit 0fc6c3c40112bc3f58d0f3c1b936c849b59719ce
Author: Tim Horton <hortont svn gnome org>
Date:   Sat Jul 4 16:44:38 2009 -0400

    [calculator-mpfr] Stole prefix parser from ease to make a calculator for Matt to fill in with mpfr functions

 examples/calculator-mpfr.js |  142 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 142 insertions(+), 0 deletions(-)
---
diff --git a/examples/calculator-mpfr.js b/examples/calculator-mpfr.js
new file mode 100755
index 0000000..2b6391b
--- /dev/null
+++ b/examples/calculator-mpfr.js
@@ -0,0 +1,142 @@
+#!/usr/bin/env seed
+
+readline = imports.readline;
+mpfr = imports.mpfr;
+
+var isAtom = function(a)
+{
+	return typeof a == 'string' || typeof a == 'number' || typeof a == 'boolean';
+};
+
+var car = function(s)
+{
+	return s[0];
+};
+
+var cdr = function(s)
+{
+	return s[1];
+};
+
+var firstSubExp = function(exp)
+{
+	return car(cdr(exp));
+};
+
+var secondSubExp = function(exp)
+{
+	return car(cdr(cdr(exp)));
+};
+
+var plus = function(n, m)
+{
+	return parseInt(n, 10)+parseInt(m, 10);
+}
+
+var minus = function(n, m)
+{
+	return parseInt(n, 10)-parseInt(m, 10);
+}
+
+var times = function(n, m)
+{
+	return parseInt(n, 10) * parseInt(m, 10);
+}
+
+var divide = function(n, m)
+{
+	return parseInt(n, 10) / parseInt(m, 10);
+}
+
+var nop = function(n, m)
+{
+	return 0;
+}
+
+var atomToFunction = function(x)
+{
+	if (x === '+')
+		return plus;
+	if (x === '-')
+		return minus;
+	if (x === '*')
+		return times;
+	if (x === '/')
+		return divide;
+	else
+	{
+		print("Syntax Error");
+		repl(); // TODO: cheating
+	}
+}
+
+var repl = function()
+{
+	while (1)
+	{
+		a = readline.readline(">> ");
+		if (a === "quit")
+			return;
+		print(value(parse(a)));
+	}
+}
+
+var value = function (exp)
+{
+	return isAtom(exp) ? exp :
+		atomToFunction(car(exp)) (
+			value(firstSubExp(exp)),
+			value(secondSubExp(exp)));		 
+}
+
+var parse = function (x)
+{
+	var tx = /\s*(\(|\)|[^\s()]+|$)/g, result;
+	tx.lastIndex = 0;
+	
+	result = function list()
+	{
+		var head = null,
+			neo  = null,
+			r    = tx.exec(x),
+			sexp = (r && r[1]) || '',
+			tail = null;
+
+		if (sexp != '(')
+		{
+			return sexp;
+		}
+		while (true)
+		{
+			sexp = list();
+			
+			if (sexp === '' || sexp == ')')
+			{
+				return head;
+			}
+			
+			neo = [sexp];
+			
+			if (tail)
+			{
+				tail[1] = neo;
+			}
+			else
+			{
+				tail = head = neo;
+			}
+			
+			tail = neo;
+		}
+	}();
+	
+	parse.lastIndex = tx.lastIndex;
+	return result;
+};
+
+var eval = function(exp)
+{
+	return value(s(exp));
+}
+
+repl();



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]