Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #23741
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Why is it so convoluted to import a .js file in javascript? |
| Date | 2014-03-29 18:57 +0100 |
| Organization | PointedEars Software (PES) |
| Message-ID | <1658884.GDCoxO0Dda@PointedEars.de> (permalink) |
| References | (6 earlier) <e96817ba-43dd-412e-ac44-617254e21546@googlegroups.com> <XnsA2FEBA5B9A273eejj99@194.109.133.133> <00d68cb0-4463-426a-859d-125781cd2f56@googlegroups.com> <power-20140328185800@ram.dialup.fu-berlin.de> <eval-20140329180710@ram.dialup.fu-berlin.de> |
Stefan Ram wrote:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>I just wrote the following evaluator that can evaluate
>>expressions like »6+3*2^3+1«. All its errors and features
>>gaps are meant to be an exercise for the reader.
>
> The new version
>
> - now correctly treats 2^3^2 right-associatively,
> - has additional operators "/" and binary "-", and
> - can deal with multi-digit numerals.
>
> <!DOCTYPE HTML><html><head><title>Main</title>
> <meta charset="UTF-8"></head><body><pre><code><script>
Your code style, although apparently consistent, still is only barely
readable. Your code is very inefficient and error-prone.
The part of the code that does not depend on output belongs in the “head”
element.
> function Scanner( source ){ this.source = source; this.pos = 0; };
> Scanner.prototype =
> { check: function( pattern )
> { var next = this.source.substring( this.pos, this.pos + 1 );
var next = this.source.charAt(this.pos);
or in some implementations (especially those of ES5+)
var next = this.source[this.pos];
<http://PointedEars.de/es-matrix/?filter=string\[>
> if( pattern.exec( next )){ ++this.pos; return next }
> else return false; },
It does not make any sense to use regular expressions and at the same time
to not use the resulting matches.
> numeral: function()
> { var next = /[0-9]+/.exec( this.source.substring( this.pos ));
> this.pos += next[ 0 ].length; return next[ 0 ]; }, };
>
> var ex = Object.create( null );
> ex[ "^" ]= Math.pow;
> ex[ "*" ]= function( a, b ){ return a * b };
> ex[ "/" ]= function( a, b ){ return a / b };
> ex[ "+" ]= function( a, b ){ return a*1 + b*1 };
> ex[ "-" ]= function( a, b ){ return a - b };
This can be compacted and optimized into
var ex = Object.create(null, {
"^": {value: Math.pow},
"*": {value: function (a, b) { return a * b; }},
"/": {value: function (a, b) { return a / b; }},
"+": {value: function (a, b) { return (+a) + (+b); }},
"-": {value: function (a, b) { return a - b; }}
});
An important difference to the above is that these properties are read-only
and not enumerable. Otherwise the code begs the question why a simple, more
compatible, Object initialiser would not suffice:
var ex = {
// …
};
> var left_associative = Object.create( null );
> left_associative[ "^" ]= false;
> left_associative[ "*" ]= true;
> left_associative[ "/" ]= true;
> left_associative[ "+" ]= true;
> left_associative[ "-" ]= true;
Same here. In fact, it might be more efficient, and certainly makes more
sense data-wise, to have the associativity of an operator as the property of
an operator, not some unrelated object:
var operators = {
"…": {
call: function () { … },
leftAssociative: …
},
/∕ …
};
> function Parser( source ){ this.scanner = new Scanner( source ); };
> Parser.prototype =
As you have been told before, this overwrites the prototype property,
setting the value of the “constructor” property of Instance to a reference
to the value of the “constructor” property of the RHS value.
> { numeral: function( me ){ return me.scanner.numeral(); },
> parse: function( me, op, next )
> { var result = next( me ); var sym;
> while( sym = me.scanner.check( op ))
> result = ex[ sym ]( result,
> ( left_associative[ sym ]? next( me ): me.parse( me, op, next )));
> return result; },
Completely unmaintainable. Kids, don't try this at home.
> power: function( me ){ return me.parse( me, /[\^]/, me.numeral ); },
> product: function( me ){ return me.parse( me, /[*\/]/, me.power ); },
> sum: function( me ){ return me.parse( me, /[+-]/, me.product ); },
> start: function(){ return this.sum( this ); }};
It is pointless and less efficient to pass the “this” value to a function
called as a method of the “this” value.
({
foo: function (me) {
return me;
},
bar: function () {
this.foo(this);
}
}).bar();
is semantically equivalent to
({
foo: function () {
return this;
},
bar: function () {
this.foo();
}
}).bar();
[“this” has nothing to do with scope. That it had is a common beginner’s
misconception.]
> function evl( x ){ var parser = new Parser( x ); return parser.start(); }
>
> function check( x, y )
> { var e = evl( x ); var r = e == y*1;
Because we are dealing with floating-point values, converting to Number
explicitly with unary “+” is probably more efficient than converting
implicitly by multiplying by 1. It is also less likely to be upwards-
incompatible (should there in be an implementation in the future
that overloads “*” as string repetition operator in the future [1]).
[1] cf. <http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.prototype.repeat>
--
PointedEars
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-26 16:22 -0700
Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-27 01:37 -0700
Re: Why is it so convoluted to import a .js file in javascript? Tim Streater <timstreater@greenbee.net> - 2014-03-27 11:45 +0000
Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-27 09:36 -0700
Re: Why is it so convoluted to import a .js file in javascript? Tim Streater <timstreater@greenbee.net> - 2014-03-27 17:17 +0000
Re: Why is it so convoluted to import a .js file in javascript? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-03-27 19:48 +0100
Re: Why is it so convoluted to import a .js file in javascript? "se" <se@no-one.here> - 2014-03-27 20:52 +0100
Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 08:39 -0700
Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 08:59 -0700
Re: Why is it so convoluted to import a .js file in javascript? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-03-28 18:19 +0100
Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 10:41 -0700
eval() vs. own expression parser (was: Why is it so convoluted to import a .js file in javascript?) Christoph Michael Becker <cmbecker69@arcor.de> - 2014-03-28 19:11 +0100
Re: Why is it so convoluted to import a .js file in javascript? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-28 21:43 +0100
Re: Why is it so convoluted to import a .js file in javascript? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-29 18:57 +0100
Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 08:51 -0700
Re: Why is it so convoluted to import a .js file in javascript? Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2014-03-27 17:30 +0100
Re: Why is it so convoluted to import a .js file in javascript? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-03-27 09:39 +0100
Re: Why is it so convoluted to import a .js file in javascript? "se" <se@no-one.here> - 2014-03-27 21:12 +0100
Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 07:11 -0700
Re: Why is it so convoluted to import a .js file in javascript? John Harris <niam@jghnorth.org.uk.invalid> - 2014-03-28 14:30 +0000
Re: Why is it so convoluted to import a .js file in javascript? danca <cyberdanny@tiscalinet.it> - 2014-03-28 15:36 +0100
Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 08:17 -0700
Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 08:23 -0700
Re: Why is it so convoluted to import a .js file in javascript? danca <cyberdanny@tiscalinet.it> - 2014-03-29 16:42 +0100
Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 09:02 -0700
Re: Why is it so convoluted to import a .js file in javascript? "se" <se@no-one.here> - 2014-03-28 19:00 +0100
Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 11:34 -0700
Re: Why is it so convoluted to import a .js file in javascript? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-27 22:29 +0100
csiph-web