I found an strange behavior of coffee compiler on an simple expression, which differs from a interactive compiler reaction at coffeescript.org site.
When I try to compile next string with coffee:
(console.log i; break) for i in [0..10]
I got:
SyntaxError: In repl, cannot use a pure statement in an expression.at SyntaxError (unknown source) ...
But the same expression in interactive compiler at coffescript.org compiled just fine, as expected by me to:
var i, _i;
for (i = _i = 0; _i <= 10; i = ++_i) {
console.log(i);
break;
}
Why coffee don't like () grouping here?
UPD
Another strange thing - it happens not always, sometimes, after a lot of tries and variations, coffee starts to compile absolutely the same expression normally, without errors.
But seems like in interactive mode, coffee fails always.
Another strange thing I found - this error happens only when I use the 'break' keyword. Expression '(console.log i; i+1) for i in [0..5]' works just fine and returns an array.
The problem is that the REPL tries to give you the result of every expression (and save it as _). So internally, it's compiling
_ = ((console.log i; break) for i in [0..10])
which breaks the compiler because you can't use break in a list comprehension.
I would recommend creating myfile.coffee in your favorite editor and running it (coffee myfile.coffee) rather than using the REPL.
Related
I'm in progress of learning Rust and I'm trying to apply similar style I'm using with C++. With C++ I would be using RAII and exceptions and the closest I can get with Rust is RAII combined with early return with error Result. As such, when I write code such as
fn parse_meminfo() -> Result<Meminfo, io::Error> {
// ...
let file = File::open("/proc/meminfo")?;
for line in io::BufReader::new(file).lines() {
let line = line?;
// ...
Is it possible to somehow avoid the fragment let line = line?; of code? I already tried
for line in io::BufReader::new(file).lines()?
and
for line? in io::BufReader::new(file).lines()
and
for Ok(line) in io::BufReader::new(file).lines()
and
for line in io::BufReader::new(file).lines().expect("failed to read line")
but rustc wasn't happy with any of those. First and fourth have the test in logically wrong position because ? and expect() are testing the iterator instead of the item, if I've understood correctly. Second was syntax error, and the syntax Ok(line) is not happy to ignore the error because that doesn't mean early return. I would like to have early return with the error if the iterator in the loop gives an error and adding one extra line of code just for that seems silly and that test seems a bit detached from the actual loop. Can you suggest anything better? If the let line = line?; is indeed the idiomatic style, then I guess I would have to learn to love it.
Is it possible to somehow avoid the fragment let line = line?; of code?
Depending how you're using line, you could just tack on the ? over there. expr? simply desugars to1
match expr {
Ok(v) => v,
Err(e) => return e.into()
}
So you can see how the second attempt would not work (the left hand side of the for...in is an irrefutable pattern, not an expression).
The error handling here is largely orthogonal to the iteration, and iteration doesn't (at this juncture, though it seems unlikely that'll be added) have first-class support for error signaling, so you have an Iterator<Item=Result<...>> and you handle the Result however you wish.
If you don't care for the error reporting you could always .lines().map(|v| v.expect(...) and that'll immediately panic at the first error, but I don't know that that's what you're looking for.
1: that's not quite true anymore because of the `Try` trait, but close enough still
I am trying to create a function in javascript having the same behavior as eval as it is not that much secure as safe-eval npm package. When I am trying to use safe-eval library rather than eval it doesn't support many of the functions as if, switch etc so it is giving me error as
SyntaxError: Unexpected token if
So I tried to write a code for same but facing some issues in that as well.
Is Anyone know how to use the safe-eval for condition execution or is anyone knows how to write a similar customized function/method for achieving the same goal.
thanks !!
The safe-eval npm page says of the input:
the JavaScript code must be an expression (something which evaluates to a value).
if(...) { ... } is a statement, not an expression.
The simple solution here is one that is already suggested by the documentation on that same NPM page: wrap your statements inside of a function expression that you immediately call.
var code = '(function square(b) { return b * b; })(5)'
var evaluated = safeEval(code)
You could put anything inside of that function, including statements:
var code = '(function square(b) { if(b === 5) { return "yes"; } else { return 0; } })(5)'
var evaluated = safeEval(code)
See the documentation: (emphasis added)
Currently, it works only with Node.js, and the JavaScript code must be an expression (something which evaluates to a value).
if is a statement, not an expression.
You can make it evaluate statements by wrapping them in an IIFE, which is an expression.
auto_ptr<MoistAir> ma(new MoistAir);
L1->setPabs(Pabs);
L1->setQm(qm2);
L1->setT(t1);
L1->setX(x1);
if ((L2->getQm() / L3->getQm()) > 1)
w = L3->getQm() / (A0 * 1.2);
else
w = L2->getQm() / (A0 * 1.2);
//This is a function i tried to add watch on
double MoistAir::getQm()
{
return Fqm;
}
I tries to add watch on the function in visual studio to get the result? Is it possible. Do i have to introduce a variable or step in to the function? I just want to mark the code as in C# and get value.
If i step in to then function i can see the result.
After some more reading/talking to person programming C/C++ told me how to do. maybe it's can help somebody else.
((L2)._Myptr)->getQm() 2.3999999756939365 double
In my case the object was a Shared_ptr, I used ((&Shared_PtrObj)->_Ptr)->objMethod(). This solved my issue and showed the exact output on watch window.
It seems that the variable is not getting any value. Try checking the return value of L3->getQm() by create a new variable and assigning it the result of L3->getQm().
I'm not very sure if your if-statement is correct, but in my experience, when I have two conditions in the same if-statement I usually use || or && depending on what I want.
Good luck with your app.
I want to write a simple parser for a nested block syntax, just hierarchical plain-text. For example:
Some regular text.
This is outputted as-is, foo{but THIS
is inside a foo block}.
bar{
Blocks can be multi-line
and baz{nested}
}
What's the simplest way to do this? I've already written 2 working implementations, but they are overly complex. I tried full-text regex matching, and streaming char-by-char analysis.
I have to teach the workings of it to people, so simplicity is paramount. I don't want to introduce a dependency on Lex/Yacc Flex/Bison (or PEGjs/Jison, actually, this is javascript).
The good choices probably boil down as follows:
Given your constaints, it's going to be recursive-descent. That's a fine way to go even without constraints.
you can either parse char-by-char (traditional) or write a lexical layer that uses the local string library to scan for { and }. Either way, you might want to return three terminal symbols plus EOF: BLOCK_OF_TEXT, LEFT_BRACE, and RIGHT_BRACE.
char c;
boolean ParseNestedBlocks(InputStream i)
{ if ParseStreamContent(i)
then { if c=="}" then return false
else return true
}
else return false;
boolean ParseSteamContent(InputStream i)
{ loop:
c = GetCharacter(i);
if c =="}" then return true;
if c== EOF then return true;
if c=="{"
{ if ParseStreamContent(i)
{ if c!="}" return false; }
else return false;
}
goto loop
}
Recently, I've been using parser combinators for some projects in pure Javascript. I pulled out the code into a separate project; you can find it here. This approach is similar to the recursive descent parsers that #DigitalRoss suggested, but with a more clear split between code that's specific to your parser and general parser-bookkeeping code.
A parser for your needs (if I understood your requirements correctly) would look something like this:
var open = literal("{"), // matches only '{'
close = literal("}"), // matches only '}'
normalChar = not1(alt(open, close)); // matches any char but '{' and '}'
var form = new Parser(function() {}); // forward declaration for mutual recursion
var block = node('block',
['open', open ],
['body', many0(form)],
['close', close ]);
form.parse = alt(normalChar, block).parse; // set 'form' to its actual value
var parser = many0(form);
and you'd use it like this:
// assuming 'parser' is the parser
var parseResult = parser.parse("abc{def{ghi{}oop}javascript}is great");
The parse result is a syntax tree.
In addition to backtracking, the library also helps you produce nice error messages and threads user state between parser calls. The latter two I've found very useful for generating brace error messages, reporting both the problem and the location of the offending brace tokens when: 1) there's an open brace but no close; 2) there's mismatched brace types -- i.e. (...] or {...); 3) a close brace without a matching open.
I am complete new to open source and would really like to use it more. I installed (x86)node.js from nodejs.org. I launched "Node.js command prompt" from the installed list, and executed node.exe. I am trying to to run some sample javascript. Why is it if I do:
>var life = 11;
undefined
^^^^^^^^^
why am I getting this message?
or
>a = [1,2,3]
[1,2,3]
>a.forEach(function (v) {console.log(v);});
1
2
3
undefined
^^^^^^^^^
//still get this, even though the script executed??
>
The console prints the return value of your script, which is undefined
Write this:
"hello";
And press enter. Now the return value should be "hello" and not undefined.
You could also do;
var life = 11; life;
undefined is just the return value of the statements you executed. Only meaningful/useful if you've executed a function, really.