directory
- Implementing a programming language with javascript – Preface
- Implementing a programming language in javascript – Language vision
- Implement a programming language in javascript – write a parser
- Use javascript to implement a programming language – character input stream
aboutLambda anguage
Some ideas of
Yes, the language we are going to implement will be called λanguage.
The correct way of programming is to have clear ideas and logic first. So we should list the language features of λ Anguage here, but in this tutorial, I will give a few random examples to explain the language features.
# here is a comment
println("Hello world");
println(2 + 3 * 4);
Declare functions using the lambda keyword
fib = lambda (n) if n < 2 then n else fib(n - 1) + fib(n - 2);
println(fib(15))
print- range = lambda (a, b)if a <= b then {
print(a);
if a + 1 <= b {
print(",");
print-range(a + 1, b);
} else println("");
}
print-range(1, 5);
Copy the code
You should note that the variable names above contain the – sign, which is a personal preference, as the author does not like to use camel names and does not like to use underscores. The great thing about implementing a programming language yourself is that you can customize its syntax.
The output of the above code looks like this:
Hello world
14
610
1, 2, 3, 4, 5
Copy the code
λ Anluage may look like javascript, but they are not the same.
Lambda anguage
You don’t need to declare variables, just expressions- The return value of one expression can be used in other expressions
- Semicolons are used to separate statements
- Create a block with curly braces that return the value of the last statement. Curly braces are also an expression
The following procedures are legal:
a = {
fib(10); # There are no side effects, but it will be executed
fib(15) # The last semicolon can be omitted
};
print(a); # Print 610
Copy the code
Functions are declared by lambda or lambda. The keyword should be followed by a formal parameter wrapped in parentheses, such as
fib = lambda(num) num + 1;
Copy the code
The keyword is followed by a single expression, but a block of code can be generated by {}. The result of the execution of the last statement of the expression is passed to the caller as the return value.
Note that without var, you can declare a variable using something like IIFE in javascript. You can declare a variable as a parameter through lambda. Variables are the term block-level scopes. Functions are similar to closures in javascript.
A ternary expression in javascript can be written like this:
a = foo() ? bar() : baz(); // javascript
a = if foo() then bar() else baz(); # lambda anguage
Copy the code
The then keyword can be omitted in the case of {, as in print-range above. Everything else is required, create another conditional branch with else. The if expression returns false when else is missing and if evaluates to false. All but false are resolved to true, including the number 0 and the empty string.
Notice that we need to use a semicolon after each expression; To close.
Well, that’s about λanguage. Although it looks like a toy and lacks many features such as groups, objects, etc., it builds on these. If you can understand our tutorial well, the other syntax features are not a problem.
The original link: lisperator.net/pltut/dream