-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
346 additions
and
53 deletions.
There are no files selected for viewing
This file contains 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
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"; | ||
}; |
This file contains 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
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 |
This file contains 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
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)); | ||
} |
This file contains 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
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 | ||
. |
This file contains 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
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) | ||
|
This file contains 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
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 | ||
|
||
|
This file contains 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
This file contains 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
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); | ||
} | ||
}; |
Oops, something went wrong.