Skip to content

Commit 88e8869

Browse files
committed
Light pass over chapter 12
1 parent 1839996 commit 88e8869

File tree

1 file changed

+19
-25
lines changed

1 file changed

+19
-25
lines changed

12_language.md

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ Because Egg, like JavaScript, allows any amount of whitespace between its elemen
126126

127127
{{index "literal expression", "SyntaxError type"}}
128128

129-
After skipping any leading space, `parseExpression` uses three ((regular expression))s to spot the three atomic elements that Egg supports: strings, numbers, and words. The parser constructs a different kind of data structure depending on which one matches. If the input does not match one of these three forms, it is not a valid expression, and the parser throws an error. We use `SyntaxError` instead of `Error` as the exception constructor, which is another standard error type, because it is a little more specific—it is also the error type thrown when an attempt is made to run an invalid JavaScript program.
129+
After skipping any leading space, `parseExpression` uses three ((regular expression))s to spot the three atomic elements that Egg supports: strings, numbers, and words. The parser constructs a different kind of data structure depending on which one matches. If the input does not match one of these three forms, it is not a valid expression, and the parser throws an error. We use the `SyntaxError` constructor here. This is an exception class defined by the standard, like `Error`, but more specific.
130130

131131
{{index "parseApply function"}}
132132

@@ -237,7 +237,7 @@ We use plain JavaScript function values to represent Egg's function values. We w
237237

238238
{{index readability, "evaluate function", recursion, parsing}}
239239

240-
The recursive structure of `evaluate` resembles the similar structure of the parser, and both mirror the structure of the language itself. It would also be possible to combinge the parser and the evaluator into one function, and evaluate during parsing. But splitting them up this way makes the program clearer and more flexible.
240+
The recursive structure of `evaluate` resembles the similar structure of the parser, and both mirror the structure of the language itself. It would also be possible to combine the parser and the evaluator into one function, and evaluate during parsing. But splitting them up this way makes the program clearer and more flexible.
241241

242242
{{index "Egg language", interpretation}}
243243

@@ -412,13 +412,13 @@ specialForms.fun = (args, scope) => {
412412
return expr.name;
413413
});
414414
415-
return function() {
416-
if (arguments.length != params.length) {
415+
return function(...args) {
416+
if (args.length != params.length) {
417417
throw new TypeError("Wrong number of arguments");
418418
}
419419
let localScope = Object.create(scope);
420-
for (let i = 0; i < arguments.length; i++) {
421-
localScope[params[i]] = arguments[i];
420+
for (let i = 0; i < args.length; i++) {
421+
localScope[params[i]] = args[i];
422422
}
423423
return evaluate(body, localScope);
424424
};
@@ -468,32 +468,26 @@ If you are interested in this topic and willing to spend some time on it, I enco
468468

469469
{{index "Egg language"}}
470470

471-
When we defined `if` and `while`, you probably noticed that they were more or less trivial wrappers around JavaScript's own `if` and `while`. Similarly, the values in Egg are just regular old JavaScript values.
471+
When we defined `if` and `while`, you probably noticed that they were more or less trivial wrappers around JavaScript's own `if` and `while`. Similarly, the values in Egg are just regular old JavaScript values. Bridging the gap to a more primitive system, such as the machine code the processor understands, is more effort—but the way it works resembles what we are doing here.
472472

473-
If you compare the implementation of Egg, built on top of JavaScript, with the amount of work and complexity required to build a programming language directly on the raw functionality provided by a machine, the difference is huge. Regardless, this example ideally gave you an impression of the way ((programming language))s work.
474-
475-
And when it comes to getting something done, cheating is more effective than doing everything yourself. Though the toy language in this chapter doesn't do anything that couldn't be done better in JavaScript, there _are_ situations where writing small languages helps get real work done.
473+
Though the toy language in this chapter doesn't do anything that couldn't be done better in JavaScript, there _are_ situations where writing small languages helps get real work done.
476474

477475
Such a language does not have to resemble a typical programming language. If JavaScript didn't come equipped with regular expressions, for example, you could write your own parser and evaluator for regular expressions.
478476

479-
{{index "artificial intelligence"}}
477+
{{index "parser generator"}}
480478

481-
Or imagine you are building a giant robotic ((dinosaur)) and need to program its ((behavior)). JavaScript might not be the most effective way to do this. You might instead opt for a language that looks like this:
479+
Or imagine you are building a program that makes it possible to quickly create parsers by providing a logical description of the language they need to parse. You could define a specific notation for that, and a compiler that compiles it to a parser program.
482480

483481
```{lang: null}
484-
behavior walk
485-
perform when
486-
destination ahead
487-
actions
488-
move left-foot
489-
move right-foot
490-
491-
behavior attack
492-
perform when
493-
Godzilla in-view
494-
actions
495-
fire laser-eyes
496-
launch arm-rockets
482+
expr = number | string | name | application
483+
484+
number = digit+
485+
486+
name = letter+
487+
488+
string = '"' (! '"')* '"'
489+
490+
application = expr '(' (expr (',' expr)*)? ')'
497491
```
498492

499493
{{index expressivity}}

0 commit comments

Comments
 (0)