Text-based replacement interpreter: summary

Source: Internet
Author: User
Tags exception handling expression garbage collection new features

\ (\newcommand{\mt}[1]{\text{#1}}\)

About grammar

The syntax we use now for this full bracket and prefix expression is called an "s expression." S-expression seems strange, in fact, is a minimalist style of grammar. The expression of an S-expression is generally designed so that the first word represents the category of the expression (such as an if or a let expression), and then lists all the elements of the expression, followed by a pair of parentheses to enclose the entire expression. For example, if expressions have three element conditional expressions and two branch expressions, the IF expression is $ (\mt{if} \; L \; M \; N) $; addition is a plus followed by an expression to add: $ (+ \; M \; N) $.

The S-expression syntax is less simple than the S-expression, which has almost no syntax. The code for the S expression is already a tree of abstract syntax. One obvious advantage of it is that the interpreter has little to do with parsing. On the grammar analysis, the principle of compiling the course is not done, I have nothing to say. All I want to say is some semantic content, so I want to avoid parsing as much as possible. Losing something means reducing a lot of trouble (the displacement of the conflict is enough to play for a while). There are sometimes unexpected bugs in parsing. For example, there is an example in the Unix hate manual, which assumes that P is a pointer to a floating-point number in the C language. Now to calculate the reciprocal of the *p, there is a small piece of code:

1 1/*p

Well?

The S-expression has been criticized more than the parentheses. In fact, many people (who should never have studied Lisp) dislike S-expressions not because they are more than parentheses, but because S-expressions are "not as good as" C (or Java, Python).

Later, Alligator

For the convenience of reference, we should give a name to the language we are realizing, call it alligator.

Alligator syntax: \BEGIN{EQUATION*}\BEGIN{ARRAY}{LCL} M, N, L &=& X \ &|& b \ &|& \lambda x.m \ &am p;|& (\mt{fix} \; X_1 \; X_2 \; M) \ &|& (+ \; M \; N) \ &|& (-\; M \; N) \ &|& (\mt{iszero} \; m) \ &|& (M \; N) \ \end{array}\end{equation*}

Value: \BEGIN{EQUATION*}\BEGIN{ARRAY}{LCL} V &=& X \ &|& b \ &|& \lambda x.m \end{array}\end{equatio n}

Macro: \BEGIN{EQUATION*}\BEGIN{ARRAY}{LCL} (\mt{if} \; L \; M \; N) &=& (((L; \lambda x.m) \ \lambda X.N) \ 0) \ && \text{}x \notin FV (M), X \notin FV (N) \ (\mt{le T} \; X \; N \; M) &=& (\lambda x.m \; N) \ \ (\mt{letrec} \; X_1 \; X_2 \; N \; M) &=& (\lambda x_1.m \; (\mt{fix} \; X_1 \; X_2 \; N)) \end{array}\end{equation*}

Alligator uses the Call-by-value invocation method, its evaluation process is as follows (continuation passing): \BEGIN{EQUATION*}\BEGIN{ARRAY}{LCL} \left<X, \ Kappa\right>_v &\rightarrow_v& \left<x, \kappa\right>_c \ \left<b, \kappa\right>_v &\ rightarrow_v& \left<b, \kappa\right>_c \ \left<\lambda x.m, \kappa\right>_v &\rightarrow_v& Left<\lambda x.m, \kappa\right>_c \ \left< (\mt{fix} \; X_1 \; X_2 \; M), \kappa\right>_v &\rightarrow_v& \left<\lambda x_2.m[x_1 \leftarrow (\mt{fix} \; X_1 \; X_2 \; M)], \kappa\right>_c \ \left< (o^n \; M_1 \; M_2 \; ... \; M_n), \kappa\right>_v &\rightarrow_v& \left<m_1, \left<\mt{opd}, \kappa, O^n, (m_2; ... \; M_n), () \right>\right>_v \ \left<v, \left<\mt{opd}, \kappa, O^n, (m_{i+1} \; M_n), (v_1;.; \; V_{i-1}) \right>\right>_c &\rightarrow_c& \left<m_{i+1}, \LEFT&LT;\MT{OPD}, \kappa, O^n, (...; M_n), (v_1;.; \; V_{I-1} V) \right>\right>_v \ \lefT<v, \LEFT&LT;\MT{OPD}, \kappa, O^n, (), (v_1; ... \; V_{n-1}) \right>\right>_c &\rightarrow_c& \left<v ', \kappa\right>_c \ && \text{where} V ' = O^n (V_1, ..., v_{n-1}, V) \ \left< (M \; N), \kappa\right>_v &\rightarrow_v& \left<m, \left<\mt{arg}, \kappa, N\right>\right>_v \ \left <v, \left<\mt{arg}, \kappa, N\right>\right>_c &\rightarrow_c& \left<n, \left<\mt{fun}, \ Kappa, V\right>\right>_v \ \left<v, \left<\mt{fun}, \kappa, \lambda X.l\right>\right>_c, &\ rightarrow_c& \left<l[x \leftarrow V], \kappa\right>_v \end{array}\end{equation*}

= = I'm a cross line ================================================

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/

Because the interpreter was so cheerful, I unwittingly wrote Alligator about two-thirds of the progress of the series. The following link is the code for the Alligator interpreter:

Https://github.com/sKabYY/Alligator

This alligator contains new features such as multi-parameter functions, mutual recursion, assignment, garbage collection, LETCC and CC, and exception handling.

Testcases.rkt is an example of a test. Running the Alligator.rkt file will test the example in Testcases.rkt.

1 Racket Alligator.rkt

Alligator-cps.rkt the CPS transformation and some simple CPS code optimizations for Alligator. A very interesting place is to finish the CPS transformation, some statements such as exception handling disappeared. CPS code optimization is written in a more chaotic, done beta expansion, ETA reduction and constant folding three optimizations. is in the experimental phase of the code, is to shoot the head on the written, estimated more bugs. Running the Alligator-cps.rkt file will test the example in Testcases.rkt.

1 Racket Alligator-cps.rkt

More output, you may need to redirect to the file and look again.

Long Way

I just want to pass the depressing time and casually write. It is now found that writing an article not only improves mood, but also benefits a deeper understanding. I'm going to make a list of what I want to write in the back to motivate myself.

1. Interpreter based on text substitution.

2. Environment. Introduce the environment, CEK, scope (static, dynamic), SECD, nameless variable, add multiple parameters.

3. State. Explicit references, implicit references, garbage collection, on-demand, values by reference.

4.Continuation. LETCC with CC, exception handling, multithreading.

5.CPS transformation, CPS code optimization?

6. Type system ... Pending

7. Object Oriented ... Pending

Program language I've just stepped across half my legs, and if there's a fallacy, welcome treatise.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.