diff options
author | ferrieux <ferrieux@users.sourceforge.net> | 2011-10-25 04:21:18 (GMT) |
---|---|---|
committer | ferrieux <ferrieux@users.sourceforge.net> | 2011-10-25 04:21:18 (GMT) |
commit | b49b3f56856ff27dfbc2718addf96ea34b39c197 (patch) | |
tree | 5510d9138365922ec82291b0a55b02ed1215cb7d | |
parent | 0232bc3edd3d17516380f5521092119022c5f626 (diff) | |
download | tcl-ferrieux_nacl.zip tcl-ferrieux_nacl.tar.gz tcl-ferrieux_nacl.tar.bz2 |
Add a local copy of reference JS demo at same fps. Add an interactive tclsh page for easy timings, including toy number manipulation.ferrieux_nacl
-rw-r--r-- | nacl/demo/ballsjs.html | 274 | ||||
-rw-r--r-- | nacl/demo/inter.html | 39 | ||||
-rw-r--r-- | nacl/demo/inter.natcl | 36 | ||||
-rw-r--r-- | nacl/demo/kinetic2d-v1.0.3.js | 357 | ||||
-rw-r--r-- | nacl/demo/loader.js | 12 | ||||
-rw-r--r-- | nacl/demo/rowland.natcl | 20 | ||||
-rw-r--r-- | nacl/init.natcl | 4 |
7 files changed, 736 insertions, 6 deletions
diff --git a/nacl/demo/ballsjs.html b/nacl/demo/ballsjs.html new file mode 100644 index 0000000..c68133b --- /dev/null +++ b/nacl/demo/ballsjs.html @@ -0,0 +1,274 @@ +<!DOCTYPE HTML> +<html> + <head> + <style> + body { + margin: 0px; + padding: 0px; + } + + #myCanvas { + border: 1px solid #9C9898; + } + </style> + <script src="kinetic2d-v1.0.3.js"> + </script> + <script> + function initBalls(){ + balls = []; + + var blue = "#3A5BCD"; + var red = "#EF2B36"; + var yellow = "#FFC636"; + var green = "#02A817"; + + // G + balls.push(new Ball(173, 63, 0, 0, blue)); + balls.push(new Ball(158, 53, 0, 0, blue)); + balls.push(new Ball(143, 52, 0, 0, blue)); + balls.push(new Ball(130, 53, 0, 0, blue)); + balls.push(new Ball(117, 58, 0, 0, blue)); + balls.push(new Ball(110, 70, 0, 0, blue)); + balls.push(new Ball(102, 82, 0, 0, blue)); + balls.push(new Ball(104, 96, 0, 0, blue)); + balls.push(new Ball(105, 107, 0, 0, blue)); + balls.push(new Ball(110, 120, 0, 0, blue)); + balls.push(new Ball(124, 130, 0, 0, blue)); + balls.push(new Ball(139, 136, 0, 0, blue)); + balls.push(new Ball(152, 136, 0, 0, blue)); + balls.push(new Ball(166, 136, 0, 0, blue)); + balls.push(new Ball(174, 127, 0, 0, blue)); + balls.push(new Ball(179, 110, 0, 0, blue)); + balls.push(new Ball(166, 109, 0, 0, blue)); + balls.push(new Ball(156, 110, 0, 0, blue)); + + // O + balls.push(new Ball(210, 81, 0, 0, red)); + balls.push(new Ball(197, 91, 0, 0, red)); + balls.push(new Ball(196, 103, 0, 0, red)); + balls.push(new Ball(200, 116, 0, 0, red)); + balls.push(new Ball(209, 127, 0, 0, red)); + balls.push(new Ball(223, 130, 0, 0, red)); + balls.push(new Ball(237, 127, 0, 0, red)); + balls.push(new Ball(244, 114, 0, 0, red)); + balls.push(new Ball(242, 98, 0, 0, red)); + balls.push(new Ball(237, 86, 0, 0, red)); + balls.push(new Ball(225, 81, 0, 0, red)); + + // O + var oOffset = 67; + balls.push(new Ball(oOffset + 210, 81, 0, 0, yellow)); + balls.push(new Ball(oOffset + 197, 91, 0, 0, yellow)); + balls.push(new Ball(oOffset + 196, 103, 0, 0, yellow)); + balls.push(new Ball(oOffset + 200, 116, 0, 0, yellow)); + balls.push(new Ball(oOffset + 209, 127, 0, 0, yellow)); + balls.push(new Ball(oOffset + 223, 130, 0, 0, yellow)); + balls.push(new Ball(oOffset + 237, 127, 0, 0, yellow)); + balls.push(new Ball(oOffset + 244, 114, 0, 0, yellow)); + balls.push(new Ball(oOffset + 242, 98, 0, 0, yellow)); + balls.push(new Ball(oOffset + 237, 86, 0, 0, yellow)); + balls.push(new Ball(oOffset + 225, 81, 0, 0, yellow)); + + // G + balls.push(new Ball(370, 80, 0, 0, blue)); + balls.push(new Ball(358, 79, 0, 0, blue)); + balls.push(new Ball(346, 79, 0, 0, blue)); + balls.push(new Ball(335, 84, 0, 0, blue)); + balls.push(new Ball(330, 98, 0, 0, blue)); + balls.push(new Ball(334, 111, 0, 0, blue)); + balls.push(new Ball(348, 116, 0, 0, blue)); + balls.push(new Ball(362, 109, 0, 0, blue)); + balls.push(new Ball(362, 94, 0, 0, blue)); + balls.push(new Ball(355, 128, 0, 0, blue)); + balls.push(new Ball(340, 135, 0, 0, blue)); + balls.push(new Ball(327, 142, 0, 0, blue)); + balls.push(new Ball(325, 155, 0, 0, blue)); + balls.push(new Ball(339, 165, 0, 0, blue)); + balls.push(new Ball(352, 166, 0, 0, blue)); + balls.push(new Ball(367, 161, 0, 0, blue)); + balls.push(new Ball(371, 149, 0, 0, blue)); + balls.push(new Ball(366, 137, 0, 0, blue)); + + // L + balls.push(new Ball(394, 49, 0, 0, green)); + balls.push(new Ball(381, 50, 0, 0, green)); + balls.push(new Ball(391, 61, 0, 0, green)); + balls.push(new Ball(390, 73, 0, 0, green)); + balls.push(new Ball(392, 89, 0, 0, green)); + balls.push(new Ball(390, 105, 0, 0, green)); + balls.push(new Ball(390, 118, 0, 0, green)); + balls.push(new Ball(388, 128, 0, 0, green)); + balls.push(new Ball(400, 128, 0, 0, green)); + + // E + balls.push(new Ball(426, 101, 0, 0, red)); + balls.push(new Ball(436, 98, 0, 0, red)); + balls.push(new Ball(451, 95, 0, 0, red)); + balls.push(new Ball(449, 83, 0, 0, red)); + balls.push(new Ball(443, 78, 0, 0, red)); + balls.push(new Ball(430, 77, 0, 0, red)); + balls.push(new Ball(418, 82, 0, 0, red)); + balls.push(new Ball(414, 93, 0, 0, red)); + balls.push(new Ball(412, 108, 0, 0, red)); + balls.push(new Ball(420, 120, 0, 0, red)); + balls.push(new Ball(430, 127, 0, 0, red)); + balls.push(new Ball(442, 130, 0, 0, red)); + balls.push(new Ball(450, 125, 0, 0, red)); + + return balls; + } + + function updateBalls(kin, balls){ + var canvas = kin.getCanvas(); + var context = kin.getContext(); + var collisionDamper = 0.3; + var floorFriction = 0.0005 * kin.getTimeInterval(); + var mouseForceMultiplier = 1 * kin.getTimeInterval(); + var restoreForce = 0.002 * kin.getTimeInterval(); + + for (var n = 0; n < balls.length; n++) { + var ball = balls[n]; + // set ball position based on velocity + ball.y += ball.vy; + ball.x += ball.vx; + + // restore forces + if (ball.x > ball.origX) { + ball.vx -= restoreForce; + } + else { + ball.vx += restoreForce; + } + if (ball.y > ball.origY) { + ball.vy -= restoreForce; + } + else { + ball.vy += restoreForce; + } + + // mouse forces + var mousePos = kin.getMousePos(); + mouseX = mousePos === null ? 99999 : mousePos.x; + mouseY = mousePos === null ? 99999 : mousePos.y; + var distX = ball.x - mouseX; + var distY = ball.y - mouseY; + + var radius = Math.sqrt(Math.pow(distX, 2) + + Math.pow(distY, 2)); + + var totalDist = Math.abs(distX) + Math.abs(distY); + + var forceX = (Math.abs(distX) / totalDist) * + (1 / radius) * + mouseForceMultiplier; + var forceY = (Math.abs(distY) / totalDist) * + (1 / radius) * + mouseForceMultiplier; + + if (distX > 0) { // mouse is left of ball + ball.vx += forceX; + } + else { + ball.vx -= forceX; + } + if (distY > 0) { // mouse is on top of ball + ball.vy += forceY; + } + else { + ball.vy -= forceY; + } + + // floor friction + if (ball.vx > 0) { + ball.vx -= floorFriction; + } + else if (ball.vx < 0) { + ball.vx += floorFriction; + } + if (ball.vy > 0) { + ball.vy -= floorFriction; + } + else if (ball.vy < 0) { + ball.vy += floorFriction; + } + + // floor condition + if (ball.y > (canvas.height - ball.radius)) { + ball.y = canvas.height - ball.radius - 2; + ball.vy *= -1; + ball.vy *= (1 - collisionDamper); + } + + // ceiling condition + if (ball.y < (ball.radius)) { + ball.y = ball.radius + 2; + ball.vy *= -1; + ball.vy *= (1 - collisionDamper); + } + + // right wall condition + if (ball.x > (canvas.width - ball.radius)) { + ball.x = canvas.width - ball.radius - 2; + ball.vx *= -1; + ball.vx *= (1 - collisionDamper); + } + + // left wall condition + if (ball.x < (ball.radius)) { + ball.x = ball.radius + 2; + ball.vx *= -1; + ball.vx *= (1 - collisionDamper); + } + } + } + + function Ball(x, y, vx, vy, color){ + this.x = x; + this.y = y; + this.vx = vx; + this.vy = vy; + this.color = color; + this.origX = x; + this.origY = y; + this.radius = 10; + } + + window.onload = function(){ + kin = new Kinetic_2d("myCanvas"); + var canvas = kin.getCanvas(); + var context = kin.getContext(); + var mouseX = 99999; + var mouseY = 99999; + + canvas.onmouseout = function(){ + mouseX = 99999; + mouseY = 99999; + }; + + var balls = initBalls(); + + kin.setDrawStage(function(){ + updateBalls(this, balls); + this.clear(); + var mousePos = kin.getMousePos(); + mouseX = mousePos === null ? 99999 : mousePos.x; + mouseY = mousePos === null ? 99999 : mousePos.y; + + for (var n = 0; n < balls.length; n++) { + var ball = balls[n]; + context.beginPath(); + context.arc(ball.x, ball.y, ball.radius, 0, 2 * Math.PI, false); + context.fillStyle = ball.color; + context.fill(); + } + }); + + kin.startAnimation(); + }; + </script> + </head> + <body onmousedown="return false;"> + <canvas id="myCanvas" width="578" height="200"> + </canvas> + </body> +</html> diff --git a/nacl/demo/inter.html b/nacl/demo/inter.html new file mode 100644 index 0000000..8aa26b9 --- /dev/null +++ b/nacl/demo/inter.html @@ -0,0 +1,39 @@ +<!DOCTYPE html> +<html> + <head> + <script type="text/javascript" src="loader.js"></script> + </head> + <body> + <div id="listener"> + <script type="text/javascript"> + document.getElementById('listener') + .addEventListener('load', moduleDidLoad, true); + </script> + + <textarea id="txt" cols="160" rows="40"></textarea> + + <embed + name='nacl_module' + id='tcl' + width=0 height=0 + src='../tcl.nmf' + type='application/x-nacl' + verbose=0 + source='inter.natcl'> + + <h2>Status</h2> + <div id="modstatus">NO-STATUS</div> + </embed> + </div> + + <p style="font-size : smaller;"> + To run this demo, you need + <ul style="font-size : smaller;"> + <li> An x86-family processor</li> + <li> Chrome 15 or higher</li> + <li> Enabled "Native Client" item in about:flags</li> + <li> if everything else fails, try the Nacl demos at <a href="http://code.google.com/chrome/nativeclient/docs/examples.html">http://code.google.com/chrome/nativeclient/docs/examples.html</a></li> + </ul> + </p> + </body> +</html> diff --git a/nacl/demo/inter.natcl b/nacl/demo/inter.natcl new file mode 100644 index 0000000..3f15426 --- /dev/null +++ b/nacl/demo/inter.natcl @@ -0,0 +1,36 @@ +# sourced by inter.html demo + +::nacl::js { + t0=document.getElementById('txt'); + function bot(w) { + len=w.value.length+1; + w.setSelectionRange(len,len); + w.focus(); + } + t0.value = "# Type Tcl code in there\n"; + t0.onkeydown=function(event) { + if (event.which==13) { + bot(t0); + tclDoCoro("swallow \""+tclEsc(t0.value.replace(/.*\n/g,''))+"\""); + bot(t0); + } + return true; + } + bot(t0); +} + +proc swallow s { + append ::accu $s \n + if {[info complete $::accu]} { + set x $::accu + unset ::accu + set res [uplevel #0 $x] + ::nacl::js "t0.value+=[::nacl::jsquote $res\n];bot(t0);" + } +} + +proc ::nacl::bgerror s { + ::nacl::js "t0.value+=[::nacl::jsquote ###$s\n];bot(t0);" +} + +source rowland.natcl diff --git a/nacl/demo/kinetic2d-v1.0.3.js b/nacl/demo/kinetic2d-v1.0.3.js new file mode 100644 index 0000000..8ed85eb --- /dev/null +++ b/nacl/demo/kinetic2d-v1.0.3.js @@ -0,0 +1,357 @@ +/** + * KineticJS 2d JavaScript Library v1.0.3 + * http://www.kineticjs.com/ + * Copyright 2011, Eric Rowell + * Licensed under the MIT or GPL Version 2 licenses. + * Date: September 2 2011 + * + * Copyright (C) 2011 by Eric Rowell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +var Kinetic_2d = function(canvasId) { + this.canvas = document.getElementById(canvasId); + this.context = this.canvas.getContext("2d"); + this.drawStage = undefined; + this.listening = false; + + // desktop flags + this.mousePos = null; + this._mouseDown = false; + this._mouseUp = false; + this._mouseOver = false; + this._mouseMove = false; + + // mobile flags + this.touchPos = null; + this._touchStart = false; + this._touchMove = false; + this._touchEnd = false; + + // Region Events + this._currentRegion = null; + this._regionIndex = 0; + this._lastRegionIndex = -1; + this._mouseOverRegionIndex = -1; + + // Animation + this.t = 0; + this.timeInterval = 0; + this.startTime = 0; + this.lastTime = 0; + this.frame = 0; + this.animating = false; + + this.mouseDown = false; + + // provided by Paul Irish + window.requestAnimFrame = (function(callback) { + return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || + function(callback) { + window.setTimeout(callback, 1000 / 40); + }; + + })(); +}; + +// ======================================= GENERAL +// ======================================= + +Kinetic_2d.prototype.getContext = function() { + return this.context; +}; + +Kinetic_2d.prototype.getCanvas = function() { + return this.canvas; +}; + +Kinetic_2d.prototype.clear = function() { + this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); +}; + +Kinetic_2d.prototype.getCanvasPos = function() { + var obj = this.getCanvas(); + var top = 0; + var left = 0; + while(obj.tagName != "BODY") { + top += obj.offsetTop; + left += obj.offsetLeft; + obj = obj.offsetParent; + } + return { + top : top, + left : left + }; +}; + +Kinetic_2d.prototype._drawStage = function() { + if(this.drawStage !== undefined) { + this.drawStage(); + + // desktop flags + this._mouseOver = false; + this._mouseMove = false; + this._mouseDown = false; + this._mouseUp = false; + + // mobile touch flags + this._touchStart = false; + this._touchMove = false; + this._touchEnd = false; + } +}; + +Kinetic_2d.prototype.setDrawStage = function(func) { + this.drawStage = func; + this.listen(); +}; + +// ======================================= CANVAS EVENTS + +Kinetic_2d.prototype.handleEvent = function(evt) { + if(!evt) { + evt = window.event; + } + + this.setMousePosition(evt); + this.setTouchPosition(evt); + this._regionIndex = 0; + + if(!this.animating) { + this._drawStage(); + } +}; + +Kinetic_2d.prototype.listen = function() { + var that = this; + this._drawStage(); + + // desktop events + this.canvas.addEventListener("mousedown", function(evt) { + that._mouseDown = true; + that.mouseDown = true; + that.handleEvent(evt); + }, false); + + this.canvas.addEventListener("mousemove", function(evt) { + that.handleEvent(evt); + }, false); + + this.canvas.addEventListener("mouseup", function(evt) { + that._mouseUp = true; + that.mouseDown = false; + that.handleEvent(evt); + }, false); + + this.canvas.addEventListener("mouseover", function(evt) { + that.handleEvent(evt); + }, false); + + this.canvas.addEventListener("mouseout", function(evt) { + that.mousePos = null; + }, false); + + // mobile events + this.canvas.addEventListener("touchstart", function(evt) { + evt.preventDefault(); + that._touchStart = true; + that.handleEvent(evt); + }, false); + + this.canvas.addEventListener("touchmove", function(evt) { + evt.preventDefault(); + that.handleEvent(evt); + }, false); + + this.canvas.addEventListener("touchend", function(evt) { + evt.preventDefault(); + that._touchEnd = true; + that.handleEvent(evt); + }, false); + +}; + +Kinetic_2d.prototype.getMousePos = function(evt) { + return this.mousePos; +}; + +Kinetic_2d.prototype.getTouchPos = function(evt) { + return this.touchPos; +}; + +Kinetic_2d.prototype.setMousePosition = function(evt) { + var mouseX = evt.clientX - this.getCanvasPos().left + window.pageXOffset; + var mouseY = evt.clientY - this.getCanvasPos().top + window.pageYOffset; + this.mousePos = { + x : mouseX, + y : mouseY + }; +}; + +Kinetic_2d.prototype.setTouchPosition = function(evt) { + if(evt.touches !== undefined && evt.touches.length == 1) { // Only deal with + // one finger + var touch = evt.touches[0]; + // Get the information for finger #1 + var touchX = touch.pageX - this.getCanvasPos().left + window.pageXOffset; + var touchY = touch.pageY - this.getCanvasPos().top + window.pageYOffset; + + this.touchPos = { + x : touchX, + y : touchY + }; + } +}; + +Kinetic_2d.prototype.isMouseDown = function() { + return this.mouseDown; +}; + +// ======================================= REGION EVENTS + +Kinetic_2d.prototype.beginRegion = function() { + this._currentRegion = {}; + this._regionIndex++; +}; + +Kinetic_2d.prototype.addRegionEventListener = function(type, func) { + var event = (type.indexOf('touch') == -1) ? 'on' + type : type; + this._currentRegion[event] = func; +}; + +Kinetic_2d.prototype.closeRegion = function() { + var pos = this.touchPos || this.mousePos; + + if(pos !== null && this.context.isPointInPath(pos.x, pos.y)) { + if(this._lastRegionIndex != this._regionIndex) { + this._lastRegionIndex = this._regionIndex; + } + + // handle onmousedown + if(this._mouseDown && this._currentRegion.onmousedown !== undefined) { + this._currentRegion.onmousedown(); + this._mouseDown = false; + } + + // handle onmouseup + else if(this._mouseUp && this._currentRegion.onmouseup !== undefined) { + this._currentRegion.onmouseup(); + this._mouseUp = false; + } + + // handle onmouseover + else if(!this._mouseOver && this._regionIndex != this._mouseOverRegionIndex && this._currentRegion.onmouseover !== undefined) { + this._currentRegion.onmouseover(); + this._mouseOver = true; + this._mouseOverRegionIndex = this._regionIndex; + } + + // handle onmousemove + else if(!this._mouseMove && this._currentRegion.onmousemove !== undefined) { + this._currentRegion.onmousemove(); + this._mouseMove = true; + } + + // handle touchstart + if(this._touchStart && this._currentRegion._touchStart !== undefined) { + this._currentRegion._touchStart(); + this._touchStart = false; + } + + // handle touchend + if(this._touchEnd && this._currentRegion._touchEnd !== undefined) { + this._currentRegion._touchEnd(); + this._touchEnd = false; + } + + // handle touchmove + if(!this._touchMove && this._currentRegion._touchMove !== undefined) { + this._currentRegion._touchMove(); + this._touchMove = true; + } + + } + else if(this._regionIndex == this._lastRegionIndex) { + this._lastRegionIndex = -1; + this._mouseOverRegionIndex = -1; + + // handle mouseout condition + if(this._currentRegion.onmouseout !== undefined) { + this._currentRegion.onmouseout(); + } + } +}; + +// ======================================= ANIMATION +// ======================================= + +Kinetic_2d.prototype.isAnimating = function() { + return this.animating; +}; + +Kinetic_2d.prototype.getFrame = function() { + return this.frame; +}; + +Kinetic_2d.prototype.startAnimation = function() { + this.animating = true; + var date = new Date(); + this.startTime = date.getTime(); + this.lastTime = this.startTime; + + this._drawStage(); + + this.animationLoop(); +}; + +Kinetic_2d.prototype.stopAnimation = function() { + this.animating = false; +}; + +Kinetic_2d.prototype.getTimeInterval = function() { + return this.timeInterval; +}; + +Kinetic_2d.prototype.getTime = function() { + return this.t; +}; + +Kinetic_2d.prototype.getFps = function() { + return this.timeInterval > 0 ? 1000 / this.timeInterval : 0; +}; + +Kinetic_2d.prototype.animationLoop = function() { + var that = this; + + this.frame++; + var date = new Date(); + var thisTime = date.getTime(); + this.timeInterval = thisTime - this.lastTime; + this.t += this.timeInterval; + this.lastTime = thisTime; + + this._drawStage(); + + if(this.animating) { + requestAnimFrame(function() { + that.animationLoop(); + }); + + } +}; diff --git a/nacl/demo/loader.js b/nacl/demo/loader.js index 51e4e8f..433857e 100644 --- a/nacl/demo/loader.js +++ b/nacl/demo/loader.js @@ -17,11 +17,15 @@ function printf() // to [bgerror]. function tclEsc(text) { - return text.replace(/[][\\$""]/g,'\\$0'); + return text.replace(/([\[\]\\$""{}])/g,'\\$1'); } function tclDo(s) { - tclModule.postMessage("eval:::nacl::wrap {" + s + "}"); + //console.log("TclDo: "+s); + tclModule.postMessage("eval:::nacl::wrap {" + s + "}"); +} +function tclDoCoro(s) { + tclModule.postMessage("eval:coroutine ::main_coro ::nacl::wrap \"" + tclEsc(s) + "\""); } function tcl() { @@ -33,8 +37,8 @@ function handleMessage(message_event) { try { t = message_event.data; - //printf("ret:"+t); - eval(t); + //console.log("JSdo:"+t); + window.eval(t); } catch(err) { //printf("JS-err:"+err); alert("ERROR:"+err); diff --git a/nacl/demo/rowland.natcl b/nacl/demo/rowland.natcl new file mode 100644 index 0000000..11e4cf1 --- /dev/null +++ b/nacl/demo/rowland.natcl @@ -0,0 +1,20 @@ +proc tcl::mathfunc::gcd {a b} { + while {$b} {set b [expr {$a % [set a $b]}]} + return $a +} +# Rowland's: compute a(n)-a(n-1) where a(1)=7 and +# a(n) = a(n – 1) + gcd(n, a(n – 1)) + +proc rowland m { + set a 7 + set n 1 + set out {} + while {$n<=$m} { + incr n + set b $a + set a [expr {$b+gcd($n,$b)}] + set p [expr {$a-$b}] + if {$p>1} {lappend out $p} + } + return $out +} diff --git a/nacl/init.natcl b/nacl/init.natcl index 853b1ab..0e9c4e6 100644 --- a/nacl/init.natcl +++ b/nacl/init.natcl @@ -52,12 +52,12 @@ namespace eval ::nacl { } proc alert {args} { - js "alert([jsquote [join $args]])" + js "alert([::nacl::jsquote [join $args]])" } proc bgerror {args} { printf "### BGERROR: [join $args]\n# [info errorstack]" - js "alert([jsquote [join $args]])" + js "alert([::nacl::jsquote [join $args]])" } proc wrap {s} { |