Skip to content

Commit

Permalink
clearing out old riffraff
Browse files Browse the repository at this point in the history
  • Loading branch information
jostylr committed Jun 18, 2012
1 parent c990340 commit c0271b6
Show file tree
Hide file tree
Showing 14 changed files with 346 additions and 53 deletions.
82 changes: 82 additions & 0 deletions lessons/Lesson10.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
9.2

var compileExpr = function (expr) {
if (typeof expr === 'number') {
return expr.toString();
}
switch(expr.tag) {
case '+':
return '(' + compileExpr(expr.left) + ')+(' +
compileExpr(expr.right) + ')';
case '*':
return '(' + compileExpr(expr.left) + ')*(' +
compileExpr(expr.right) + ')';

case '-':
return '(' + compileExpr(expr.left) + ')-(' +
compileExpr(expr.right) + ')';

case '/':
return '(' + compileExpr(expr.left) + ')/(' +
compileExpr(expr.right) + ')';
case '<':
return '(' + compileExpr(expr.left) + ')<(' +
compileExpr(expr.right) + ')';


default:
throw new Error('Unknown tag ' + expr.tag);
}
};

9.3

var compileEnvironment = function (env) {
var str=[], i, n = env.length;
// Your code here
for (i = 0; i < n; i += 1) {
str.push('var ' + env[i][0] + " = "+ env[i][1].toString() + ";\n");
}
return str.join('');
};

9.4

var compileExpr = function (expr) {
if (typeof expr === 'number') {
return expr.toString();
}
switch(expr.tag) {
case '+':
return '(' + compileExpr(expr.left) + ')+(' +
compileExpr(expr.right) + ')';
case 'ident':
return expr.name;
case 'call':
return expr.name+"("+ expr.args.map(compileExpr).join(",")+")"; // Do stuff here
default:
throw new Error('Unknown tag ' + expr.tag);
}
};


9.5

var compileStatements = function (stmts, is_funcbody) {
// Your code here
return "var _res;\n" +
stmts.map(compileStatement).join("") +
(is_funcbody ? "return _res;\n" : "");
};

9.6

var compileRepeat = function (stmt) {
// Your code here
var body = "function () {\n" +
compileStatements(stmt.body, true) +
"}";
var expr = compileExpr(stmt.expr);
console.log("out", expr, body);
return "_res = repeat(" + expr + ", " + body + ");\n";
};
22 changes: 22 additions & 0 deletions lessons/parsingIdea.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Use peg.js to do block structure parsing and tokenizing

To do statement parsing, we do a stack based approach to convert postfix to infix.

3 + 4 - 2/5 + 6^2^3 = (+ 3 4 - (/ 2 5) (^ 6 2 3)) but ^ pops whereas the others shift.

{tag: "+", val : [3, 4, {tag : "-", val : {tag: "mul", val : [2, {tag:"/", val: 5}]}},
{tag: "pow", val : [6, 2, 3] } ]}


(3 - 2 - 4) = (3- 2) - 4 not 3 - (2 - 4) (+ 3 (- 2) (- 4) )

(5 / 2 / 3) = (5/2)/3 (* 5 /2 /3) since 5/2 = 5 * 1/2


no interval notation. instead 3 < x <= 5

sum_(i = 0)^5 is parsed as sum with a lower = (i = 0) and upper = (5)

a_i_j_k is parsed as a[i][j][k]

a(1, 2, 3) vs. a_1_2_3 a_'i_'j_'k
35 changes: 35 additions & 0 deletions math/examples
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Simple moving average:

Thm SMA. Given period then compute
Pf: Let nums

QED.

c = SMA(3)
data = (1, 2, 3, 4, 5, 5, 6, 3, 2, 1)
c(data_i)

function simple_moving_averager(period) {
var nums = [];
return function(num) {
nums.push(num);
if (nums.length > period)
nums.splice(0,1); // remove the first element of the array
var sum = 0;
for (var i in nums)
sum += nums[i];
var n = period;
if (nums.length < period)
n = nums.length;
return(sum/n);
}
}

var sma3 = simple_moving_averager(3);
var sma5 = simple_moving_averager(5);
var data = [1,2,3,4,5,5,4,3,2,1];
for (var i in data) {
var n = data[i];
// using WSH
WScript.Echo("Next number = " + n + ", SMA_3 = " + sma3(n) + ", SMA_5 = " + sma5(n));
}
28 changes: 28 additions & 0 deletions math/samples/factorial.mapeg
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
factorial (n) =
x = ( sum_(i=0)^n log(i) ) / ln(10)
[a, b] = split(x, 0)
10^a E b
.

factorial (n) =
for (i = 0..n)
j = j * i
.
.

factorial (n) =
if (n <= 1)
1
else
n*factorial(n-1)
.
.

factorial (n) =
m = 1
while (n > 1)
m = m*n
n = n-1
.
m
.
44 changes: 44 additions & 0 deletions math/samples/newton.mapeg
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
newton (f, x = 0, max = 20, precision = 1E-10, left = -100, right = 100) =
while[max, x=false] ( |f(x)| > precision)
x = a + f(x)/f'(x)
if (x !in (left, right) )
x = false //x = false can be checked
break
.
.
x
.

newton (sin(x), left = -2, right = 2)

newton (f, x = 0, max = 20, precision = 1E-10, left = -100, right = 100) =
sequence_(i = 0)^max
x = a + f(x)/f'(x)
if ( |f(x)| < precision)
break
else if (x !in (left, right) )
x = false
break
.
.
x
.


newton (f, x = 0, max = 20, precision = 1E-10, left = -100, right = 100) =
for (i = 0..max )
x = a + f(x)/f'(x)
if ( |f(x)| < precision)
break
else if (x !in (left, right) )
x = false
break
.
.
x
.

simplef (x) = sin(3x) + 2

if (2 != 0) newton(simplef)

35 changes: 35 additions & 0 deletions math/samples/sample.mapeg
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
a = 4

while (a > 1) {
a = a - 1
}


from peg:
[
{tag : "statement", val : [{tag: "tok", val : "a"}, {tag : "tok", val : "="}, {tag : "num", val : "4"}]},
{tag : "while",
cond : [{tag: "tok", val : "a"}, {tag : "tok", val : ">"}, {tag : "num", val : "1"}],
body : [[{tag: "tok", val : "a"}, {tag : "tok", val : "="}, {tag: "tok", val : "a"}, {tag : "tok", val : "-"}, {tag : "num", val : "1"}]]
}
]

While evaluating, rearranging of statments
{type : "=", val : [{tag: "tok", val : "a"}, {tag : "num", val : "4"}]
...
{type : "=", val : [{tag: "tok", val : "a"}, {type : "-", val : [{tag: "tok", val : "a"}, {tag : "num", val : "1"}]}]}


need to evaluate as operators and such are being defined.

type Complex (a, b)

// := adds to definition

+ := op ( (Complex, x), (+, 90, true), (Complex, y) ) :
Complex(x.re + y.re, x.im + y.im)
.

a + b I


45 changes: 45 additions & 0 deletions math/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,39 @@ Goal: Implement a full computing language using just the notions in mathematics.
Naming: Forcing a math peg into a programming hole. Also, PegJS powers the parsing.


## Peg processing

The idea is that the language should be entirely sculptable. Therefore, we start with the peg doing just tokenizing and parsing the blocks.

### Statements

expression ended by newline or ; Newlines automatically end lines unless \ ends a line which then continues. ws including // comments are allowed.

### Expressions

A set of tokens

### Blocks
These are bracketed: expressions, comma'd expression, and statements.

{([ and slashed: \{\(\[


A special expression block is absolute value. It is of the form | number/word ... | or | block ... |

### Tokens

* String is a "stuff" The double quote can be multiline.
* Special is 'wh$2 This is terminated by whitespace. The single quote does not have a match, it just waits for a whitespace.
* Words: `[a-zA-Z][a-zA-Z0-9]*` No underscore or other symbols.
* Symbols: non-word esque. It terminates at first word/number.


|(3 + z) + |3|x| | | word, number, bracketed expression

----
#Old

## Valid Variable Names

Single letters or 'Abce or "Abc de" := 5. A string is checked for existence and if it is not, then it is used as is. Otherwise, it is. To quote an existing string, use '"Abc de" is just Abc de.
Expand Down Expand Up @@ -67,3 +100,15 @@ Sum: sum_i=0^n a_i
### While loop
limit

---

The semicolon ends blocks. Certain constructs create blocks. Variables are scoped locally automatically.

Global scopre \x while \[name]x would be x in the named scope

---------





10 changes: 5 additions & 5 deletions scheem/run.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*globals module, require, console, exports*/

var fs = require('fs');
var fs = require('fs');

var pegjs = require('pegjs');
var pegjs = require('pegjs');

var scheem = pegjs.buildParser(fs.readFileSync("scheem.peg", "utf8"));
var scheem = pegjs.buildParser(fs.readFileSync("scheem.peg", "utf8"));

var parser = scheem.parse;
var parser = scheem.parse;

var evalScheem = require('./evalScheem').evalScheem;

module.exports = function (str) {
var par = parser(str);
//console.log(par);
return evalScheem(par);
}
};
Loading

0 comments on commit c0271b6

Please sign in to comment.